Search in sources :

Example 36 with Block

use of org.aion.zero.impl.types.Block in project aion by aionnetwork.

the class AionBlockchainImpl method getListOfHeadersStartFrom.

/**
 * Returns up to limit headers found with following search parameters
 *
 * @param blockNumber Identifier of start block, by number
 * @param limit Maximum number of headers in return
 * @return {@link MiningBlockHeader}'s list or empty list if none found
 */
@Override
public List<BlockHeader> getListOfHeadersStartFrom(long blockNumber, int limit) {
    if (limit <= 0) {
        return emptyList();
    }
    // identifying block we'll move from
    Block startBlock = getBlockByNumber(blockNumber);
    // if nothing found on main chain, return empty array
    if (startBlock == null) {
        return emptyList();
    }
    List<BlockHeader> headers;
    long bestNumber = bestBlock.getNumber();
    headers = getContinuousHeaders(bestNumber, blockNumber, limit);
    return headers;
}
Also used : Block(org.aion.zero.impl.types.Block) BlockDetailsValidator.isValidBlock(org.aion.zero.impl.valid.BlockDetailsValidator.isValidBlock) GenesisStakingBlock(org.aion.zero.impl.types.GenesisStakingBlock) RetValidPreBlock(org.aion.zero.impl.types.RetValidPreBlock) MiningBlock(org.aion.zero.impl.types.MiningBlock) EventBlock(org.aion.evtmgr.impl.evt.EventBlock) StakingBlock(org.aion.zero.impl.types.StakingBlock) BlockHeader(org.aion.zero.impl.types.BlockHeader) MiningBlockHeader(org.aion.zero.impl.types.MiningBlockHeader) StakingBlockHeader(org.aion.zero.impl.types.StakingBlockHeader)

Example 37 with Block

use of org.aion.zero.impl.types.Block in project aion by aionnetwork.

the class AionImpl method getStakingBlockTemplate.

// Returns null if we're waiting on a Mining block, or if creating a new block template failed for some reason
@Override
public StakingBlock getStakingBlockTemplate(byte[] newSeed, byte[] signingPublicKey, byte[] coinbase) {
    lock.lock();
    try {
        Block best = getBlockchain().getBestBlock();
        LOG_GEN.debug("getStakingBlockTemplate best:{}", best);
        if (best.getHeader().getSealType() == Seal.PROOF_OF_STAKE) {
            return null;
        } else {
            return getBlockchain().createStakingBlockTemplate(best, getAionHub().getPendingState().getPendingTransactions(), signingPublicKey, newSeed, coinbase);
        }
    } finally {
        lock.unlock();
    }
}
Also used : StakingBlock(org.aion.zero.impl.types.StakingBlock) Block(org.aion.zero.impl.types.Block)

Example 38 with Block

use of org.aion.zero.impl.types.Block in project aion by aionnetwork.

the class AionImpl method addNewBlock.

/**
 * @implNote import a new block from the api server or the internal PoW miner, the kernel will
 * reject to import a new block has the same or less than the kernel block height to reduce the orphan
 * block happens (AKI-707)
 */
public ImportResult addNewBlock(Block block) {
    getLock().lock();
    try {
        Block bestBlock = getAionHub().getBlockchain().getBestBlock();
        ImportResult importResult;
        if (bestBlock.getNumber() >= block.getNumber()) {
            importResult = ImportResult.INVALID_BLOCK;
        } else {
            importResult = this.aionHub.getBlockchain().tryToConnect(new BlockWrapper(block, true, false, false, false));
        }
        LOG_GEN.debug("ImportResult:{} new block:{}", importResult, block);
        if (importResult == ImportResult.IMPORTED_BEST) {
            this.aionHub.getPropHandler().propagateNewBlock(block);
        }
        return importResult;
    } finally {
        getLock().unlock();
    }
}
Also used : ImportResult(org.aion.zero.impl.core.ImportResult) StakingBlock(org.aion.zero.impl.types.StakingBlock) Block(org.aion.zero.impl.types.Block)

Example 39 with Block

use of org.aion.zero.impl.types.Block in project aion by aionnetwork.

the class BlockPropagationTest method testPropagateBlockToPeer.

// given two peers, and one sends you a new block, propagate to the other
@Test
public void testPropagateBlockToPeer() {
    List<ECKey> accounts = generateDefaultAccounts();
    StandaloneBlockchain.Bundle bundle = new StandaloneBlockchain.Builder().withValidatorConfiguration("simple").withDefaultAccounts(accounts).build();
    MiningBlock block = bundle.bc.createNewMiningBlock(bundle.bc.getGenesis(), Collections.EMPTY_LIST, true);
    assertThat(block.getNumber()).isEqualTo(1);
    byte[] sender = HashUtil.h256("node1".getBytes());
    byte[] receiver = HashUtil.h256("receiver".getBytes());
    NodeMock senderMock = new NodeMock(sender, 1);
    NodeMock receiverMock = new NodeMock(receiver, 0);
    Map<Integer, INode> node = new HashMap<>();
    node.put(1, senderMock);
    node.put(2, receiverMock);
    AtomicInteger times = new AtomicInteger();
    P2pMock p2pMock = new P2pMock(node) {

        @Override
        public void send(int _nodeId, String s, Msg _msg) {
            if (_nodeId != receiverMock.getIdHash()) {
                throw new RuntimeException("should only send to receiver");
            }
            times.getAndIncrement();
        }
    };
    StandaloneBlockchain.Bundle anotherBundle = new StandaloneBlockchain.Builder().withValidatorConfiguration("simple").withDefaultAccounts(accounts).withEventManger(this.loadEventMgr()).build();
    assertThat(bundle.bc.genesis.getHash()).isEqualTo(anotherBundle.bc.genesis.getHash());
    assertThat(block.getParentHash()).isEqualTo(bundle.bc.genesis.getHash());
    assertThat(block.getParentHash()).isEqualTo(anotherBundle.bc.genesis.getHash());
    Block bestBlock = bundle.bc.getBestBlock();
    assertThat(bestBlock.getHash()).isEqualTo(anotherBundle.bc.genesis.getHash());
    SyncStats syncStats = new SyncStats(bestBlock.getNumber(), true);
    BlockPropagationHandler handler = new BlockPropagationHandler(1024, // NOTE: not the same blockchain that generated the block
    anotherBundle.bc, syncStats, p2pMock, anotherBundle.bc.getBlockHeaderValidator(), false, (byte) 2, new AionPendingStateImpl(anotherBundle.bc, blockEnergyUpperBound, pendingTransactionTimeout, enablePoolBackup, enableSeedMode, enablePoolDump, new PendingTxCallback(new ArrayList<>()), new NetworkBestBlockCallback(AionImpl.inst()), new TransactionBroadcastCallback(AionImpl.inst()), true));
    // block is processed
    assertThat(handler.processIncomingBlock(senderMock.getIdHash(), "test", block)).isEqualTo(BlockPropagationHandler.PropStatus.PROP_CONNECTED);
    assertThat(times.get()).isEqualTo(1);
}
Also used : INode(org.aion.p2p.INode) TransactionBroadcastCallback(org.aion.zero.impl.blockchain.AionImpl.TransactionBroadcastCallback) HashMap(java.util.HashMap) ECKey(org.aion.crypto.ECKey) MiningBlock(org.aion.zero.impl.types.MiningBlock) Msg(org.aion.p2p.Msg) AionPendingStateImpl(org.aion.zero.impl.pendingState.AionPendingStateImpl) BlockPropagationHandler(org.aion.zero.impl.sync.handler.BlockPropagationHandler) NetworkBestBlockCallback(org.aion.zero.impl.blockchain.AionImpl.NetworkBestBlockCallback) PendingTxCallback(org.aion.zero.impl.blockchain.AionImpl.PendingTxCallback) StandaloneBlockchain(org.aion.zero.impl.blockchain.StandaloneBlockchain) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) BigInteger(java.math.BigInteger) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) MiningBlock(org.aion.zero.impl.types.MiningBlock) Block(org.aion.zero.impl.types.Block) Test(org.junit.Test)

Example 40 with Block

use of org.aion.zero.impl.types.Block in project aion by aionnetwork.

the class AionBlockStoreTest method testGetTwoGenerationBlocksByHashWithInfo.

@Test
public void testGetTwoGenerationBlocksByHashWithInfo() {
    Block grandparent = consecutiveBlocks.get(0);
    Block parent = consecutiveBlocks.get(1);
    AionBlockStore store = new AionBlockStore(index, blocks, false);
    // does not require accurate total difficulty
    store.saveBlock(grandparent, BigInteger.TWO, true);
    store.saveBlock(parent, BigInteger.TEN, true);
    Block[] blocks = store.getTwoGenerationBlocksByHashWithInfo(parent.getHash());
    assertThat(blocks.length).isEqualTo(2);
    assertThat(blocks[0]).isEqualTo(parent);
    assertThat(blocks[0].getTotalDifficulty()).isEqualTo(BigInteger.TEN);
    assertThat(blocks[0].isMainChain()).isTrue();
    assertThat(blocks[1]).isEqualTo(grandparent);
    assertThat(blocks[1].getTotalDifficulty()).isEqualTo(BigInteger.TWO);
    assertThat(blocks[1].isMainChain()).isTrue();
}
Also used : MiningBlock(org.aion.zero.impl.types.MiningBlock) Block(org.aion.zero.impl.types.Block) Test(org.junit.Test)

Aggregations

Block (org.aion.zero.impl.types.Block)283 MiningBlock (org.aion.zero.impl.types.MiningBlock)155 Test (org.junit.Test)148 AionTransaction (org.aion.base.AionTransaction)106 ImportResult (org.aion.zero.impl.core.ImportResult)86 ArrayList (java.util.ArrayList)63 AionAddress (org.aion.types.AionAddress)61 StakingBlock (org.aion.zero.impl.types.StakingBlock)58 AionBlockSummary (org.aion.zero.impl.types.AionBlockSummary)57 BigInteger (java.math.BigInteger)55 AionRepositoryImpl (org.aion.zero.impl.db.AionRepositoryImpl)34 ByteArrayWrapper (org.aion.util.types.ByteArrayWrapper)30 AionTxReceipt (org.aion.base.AionTxReceipt)29 Hex.toHexString (org.aion.util.conversions.Hex.toHexString)28 AccountState (org.aion.base.AccountState)26 EventBlock (org.aion.evtmgr.impl.evt.EventBlock)26 JSONArray (org.json.JSONArray)26 JSONObject (org.json.JSONObject)26 MiningBlockHeader (org.aion.zero.impl.types.MiningBlockHeader)22 AionTxExecSummary (org.aion.base.AionTxExecSummary)20