Search in sources :

Example 6 with PendingTxCallback

use of org.aion.zero.impl.blockchain.AionImpl.PendingTxCallback in project aion by aionnetwork.

the class AionHubTest method MockHubInst_wStartAtGenesis.

@Test
public void MockHubInst_wStartAtGenesis() {
    StandaloneBlockchain.Builder builder = new StandaloneBlockchain.Builder();
    StandaloneBlockchain.Bundle bundle = builder.withValidatorConfiguration("simple").build();
    StandaloneBlockchain chain = bundle.bc;
    chain.setBestBlock(chain.getGenesis());
    AionHub hub = AionHub.createForTesting(CfgAion.inst(), chain, new PendingTxCallback(new ArrayList<>()), new NetworkBestBlockCallback(AionImpl.inst()), new TransactionBroadcastCallback(AionImpl.inst()));
    checkHubNullity(hub);
    Block blk = hub.getStartingBlock();
    assertThat(blk).isNotNull();
    assertThat(blk.getNumber()).isEqualTo(0);
    hub.close();
    assertThat(hub.isRunning()).isFalse();
}
Also used : TransactionBroadcastCallback(org.aion.zero.impl.blockchain.AionImpl.TransactionBroadcastCallback) NetworkBestBlockCallback(org.aion.zero.impl.blockchain.AionImpl.NetworkBestBlockCallback) PendingTxCallback(org.aion.zero.impl.blockchain.AionImpl.PendingTxCallback) ArrayList(java.util.ArrayList) MiningBlock(org.aion.zero.impl.types.MiningBlock) Block(org.aion.zero.impl.types.Block) Test(org.junit.Test)

Example 7 with PendingTxCallback

use of org.aion.zero.impl.blockchain.AionImpl.PendingTxCallback in project aion by aionnetwork.

the class BlockPropagationTest method testIgnoreSameBlock.

@Test
public void testIgnoreSameBlock() {
    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());
    SyncStats syncStats = new SyncStats(bundle.bc.getBestBlock().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(handler.processIncomingBlock(senderMock.getIdHash(), "test", block)).isEqualTo(BlockPropagationHandler.PropStatus.DROPPED);
    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) Test(org.junit.Test)

Example 8 with PendingTxCallback

use of org.aion.zero.impl.blockchain.AionImpl.PendingTxCallback in project aion by aionnetwork.

the class PendingStateTest method testAionPendingStateInit.

@Test
public void testAionPendingStateInit() {
    CfgAion.inst().getTx().setSeedMode(true);
    // NullPointerException should not happens
    AionHub.createForTesting(CfgAion.inst(), blockchain, new PendingTxCallback(new ArrayList<>()), new NetworkBestBlockCallback(AionImpl.inst()), new TransactionBroadcastCallback(AionImpl.inst()));
    CfgAion.inst().getTx().setSeedMode(false);
}
Also used : TransactionBroadcastCallback(org.aion.zero.impl.blockchain.AionImpl.TransactionBroadcastCallback) NetworkBestBlockCallback(org.aion.zero.impl.blockchain.AionImpl.NetworkBestBlockCallback) PendingTxCallback(org.aion.zero.impl.blockchain.AionImpl.PendingTxCallback) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Example 9 with PendingTxCallback

use of org.aion.zero.impl.blockchain.AionImpl.PendingTxCallback in project aion by aionnetwork.

the class AionHubTest method MockHubInst_wStartRollback.

@Test
public void MockHubInst_wStartRollback() {
    StandaloneBlockchain.Builder builder = new StandaloneBlockchain.Builder();
    StandaloneBlockchain.Bundle bundle = builder.withValidatorConfiguration("simple").withDefaultAccounts().build();
    int NUMBER_OF_BLOCKS = 10, MAX_TX_PER_BLOCK = 60;
    StandaloneBlockchain chain = bundle.bc;
    AionRepositoryImpl repo = chain.getRepository();
    BlockContext context;
    List<AionTransaction> txs;
    // first half of blocks will be correct
    long time = System.currentTimeMillis();
    for (int i = 0; i < NUMBER_OF_BLOCKS / 2; i++) {
        txs = BlockchainTestUtils.generateTransactions(MAX_TX_PER_BLOCK, bundle.privateKeys, repo);
        context = chain.createNewMiningBlockInternal(chain.getBestBlock(), txs, true, time / 10000L);
        assertThat(chain.tryToConnect(context.block)).isEqualTo(ImportResult.IMPORTED_BEST);
    }
    BigInteger td6 = BigInteger.ZERO;
    // second half of blocks will miss the state root
    List<byte[]> statesToDelete = new ArrayList<>();
    List<MiningBlock> blocksToImport = new ArrayList<>();
    for (int i = 0; i < NUMBER_OF_BLOCKS / 2; i++) {
        txs = BlockchainTestUtils.generateTransactions(MAX_TX_PER_BLOCK, bundle.privateKeys, repo);
        context = chain.createNewMiningBlockInternal(chain.getBestBlock(), txs, true, time / 10000L);
        assertThat(chain.tryToConnect(context.block)).isEqualTo(ImportResult.IMPORTED_BEST);
        statesToDelete.add(context.block.getStateRoot());
        blocksToImport.add(context.block);
        if (context.block.getNumber() == 6) {
            td6 = context.block.getTotalDifficulty();
        }
    }
    Block bestBlock = chain.getBestBlock();
    assertThat(bestBlock.getNumber()).isEqualTo(NUMBER_OF_BLOCKS);
    BigInteger td2 = chain.getTotalDifficulty();
    assertTrue(td2.longValue() > td6.longValue());
    // delete some world state root entries from the database
    TrieImpl trie = (TrieImpl) repo.getWorldState();
    MockDB database = (MockDB) repo.getStateDatabase();
    repo.flush();
    for (byte[] key : statesToDelete) {
        database.deleteAndCommit(key);
        assertThat(trie.isValidRoot(key)).isFalse();
    }
    // ensure that the world state was corrupted
    assertThat(trie.isValidRoot(chain.getBestBlock().getStateRoot())).isFalse();
    assertNotEquals(td6, chain.getTotalDifficulty());
    // Also, missing the block DB
    blocksToImport.remove(0);
    database = (MockDB) repo.getBlockDatabase();
    for (MiningBlock b : blocksToImport) {
        database.deleteAndCommit(b.getHash());
    }
    repo.flush();
    // recovery should be called by loadBlockchain()
    AionHub hub = AionHub.createForTesting(CfgAion.inst(), chain, new PendingTxCallback(new ArrayList<>()), new NetworkBestBlockCallback(AionImpl.inst()), new TransactionBroadcastCallback(AionImpl.inst()));
    checkHubNullity(hub);
    assertEquals(td6, chain.getTotalDifficulty());
    Block blk = hub.getStartingBlock();
    assertThat(blk).isNotNull();
    assertThat(blk.getNumber()).isEqualTo(6);
    // ensure that the world state is ok
    assertThat(trie.isValidRoot(chain.getBestBlock().getStateRoot())).isTrue();
    hub.close();
    assertThat(hub.isRunning()).isFalse();
}
Also used : TransactionBroadcastCallback(org.aion.zero.impl.blockchain.AionImpl.TransactionBroadcastCallback) NetworkBestBlockCallback(org.aion.zero.impl.blockchain.AionImpl.NetworkBestBlockCallback) BlockContext(org.aion.zero.impl.types.BlockContext) PendingTxCallback(org.aion.zero.impl.blockchain.AionImpl.PendingTxCallback) MockDB(org.aion.db.impl.mockdb.MockDB) ArrayList(java.util.ArrayList) AionRepositoryImpl(org.aion.zero.impl.db.AionRepositoryImpl) AionTransaction(org.aion.base.AionTransaction) MiningBlock(org.aion.zero.impl.types.MiningBlock) TrieImpl(org.aion.zero.impl.trie.TrieImpl) BigInteger(java.math.BigInteger) MiningBlock(org.aion.zero.impl.types.MiningBlock) Block(org.aion.zero.impl.types.Block) Test(org.junit.Test)

Example 10 with PendingTxCallback

use of org.aion.zero.impl.blockchain.AionImpl.PendingTxCallback in project aion by aionnetwork.

the class AionHubTest method MockHubInst_wStartRecovery.

@Test
public void MockHubInst_wStartRecovery() {
    StandaloneBlockchain.Builder builder = new StandaloneBlockchain.Builder();
    StandaloneBlockchain.Bundle bundle = builder.withValidatorConfiguration("simple").withDefaultAccounts().build();
    int NUMBER_OF_BLOCKS = 10, MAX_TX_PER_BLOCK = 60;
    StandaloneBlockchain chain = bundle.bc;
    AionRepositoryImpl repo = chain.getRepository();
    BlockContext context;
    List<AionTransaction> txs;
    // first half of blocks will be correct
    long time = System.currentTimeMillis();
    for (int i = 0; i < NUMBER_OF_BLOCKS / 2; i++) {
        txs = BlockchainTestUtils.generateTransactions(MAX_TX_PER_BLOCK, bundle.privateKeys, repo);
        context = chain.createNewMiningBlockInternal(chain.getBestBlock(), txs, true, time / 10000L);
        assertThat(chain.tryToConnect(context.block)).isEqualTo(ImportResult.IMPORTED_BEST);
    }
    // second half of blocks will miss the state root
    List<byte[]> statesToDelete = new ArrayList<>();
    List<MiningBlock> blocksToImport = new ArrayList<>();
    for (int i = 0; i < NUMBER_OF_BLOCKS / 2; i++) {
        txs = BlockchainTestUtils.generateTransactions(MAX_TX_PER_BLOCK, bundle.privateKeys, repo);
        context = chain.createNewMiningBlockInternal(chain.getBestBlock(), txs, true, time / 10000L);
        assertThat(chain.tryToConnect(context.block)).isEqualTo(ImportResult.IMPORTED_BEST);
        statesToDelete.add(context.block.getStateRoot());
        blocksToImport.add(context.block);
    }
    Block bestBlock = chain.getBestBlock();
    assertThat(bestBlock.getNumber()).isEqualTo(NUMBER_OF_BLOCKS);
    // delete some world state root entries from the database
    TrieImpl trie = (TrieImpl) repo.getWorldState();
    MockDB database = (MockDB) repo.getStateDatabase();
    repo.flush();
    for (byte[] key : statesToDelete) {
        database.deleteAndCommit(key);
        assertThat(trie.isValidRoot(key)).isFalse();
    }
    // ensure that the world state was corrupted
    assertThat(trie.isValidRoot(chain.getBestBlock().getStateRoot())).isFalse();
    // recovery should be called by loadBlockchain()
    AionHub hub = AionHub.createForTesting(CfgAion.inst(), chain, new PendingTxCallback(new ArrayList<>()), new NetworkBestBlockCallback(AionImpl.inst()), new TransactionBroadcastCallback(AionImpl.inst()));
    checkHubNullity(hub);
    Block blk = hub.getStartingBlock();
    assertThat(blk).isNotNull();
    assertThat(blk.getNumber()).isEqualTo((long) NUMBER_OF_BLOCKS);
    // ensure that the blockchain is ok
    assertThat(chain.getBestBlockHash()).isEqualTo(bestBlock.getHash());
    // ensure that the world state is ok
    assertThat(trie.isValidRoot(chain.getBestBlock().getStateRoot())).isTrue();
    hub.close();
    assertThat(hub.isRunning()).isFalse();
}
Also used : TransactionBroadcastCallback(org.aion.zero.impl.blockchain.AionImpl.TransactionBroadcastCallback) NetworkBestBlockCallback(org.aion.zero.impl.blockchain.AionImpl.NetworkBestBlockCallback) BlockContext(org.aion.zero.impl.types.BlockContext) PendingTxCallback(org.aion.zero.impl.blockchain.AionImpl.PendingTxCallback) MockDB(org.aion.db.impl.mockdb.MockDB) ArrayList(java.util.ArrayList) AionRepositoryImpl(org.aion.zero.impl.db.AionRepositoryImpl) AionTransaction(org.aion.base.AionTransaction) MiningBlock(org.aion.zero.impl.types.MiningBlock) TrieImpl(org.aion.zero.impl.trie.TrieImpl) MiningBlock(org.aion.zero.impl.types.MiningBlock) Block(org.aion.zero.impl.types.Block) Test(org.junit.Test)

Aggregations

NetworkBestBlockCallback (org.aion.zero.impl.blockchain.AionImpl.NetworkBestBlockCallback)10 PendingTxCallback (org.aion.zero.impl.blockchain.AionImpl.PendingTxCallback)10 TransactionBroadcastCallback (org.aion.zero.impl.blockchain.AionImpl.TransactionBroadcastCallback)10 Test (org.junit.Test)9 MiningBlock (org.aion.zero.impl.types.MiningBlock)8 ArrayList (java.util.ArrayList)6 BigInteger (java.math.BigInteger)5 StandaloneBlockchain (org.aion.zero.impl.blockchain.StandaloneBlockchain)5 Block (org.aion.zero.impl.types.Block)5 HashMap (java.util.HashMap)4 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)4 ECKey (org.aion.crypto.ECKey)4 INode (org.aion.p2p.INode)4 Msg (org.aion.p2p.Msg)4 AionPendingStateImpl (org.aion.zero.impl.pendingState.AionPendingStateImpl)4 BlockPropagationHandler (org.aion.zero.impl.sync.handler.BlockPropagationHandler)4 AionTransaction (org.aion.base.AionTransaction)2 MockDB (org.aion.db.impl.mockdb.MockDB)2 AionRepositoryImpl (org.aion.zero.impl.db.AionRepositoryImpl)2 TrieImpl (org.aion.zero.impl.trie.TrieImpl)2