Search in sources :

Example 1 with AionPendingStateImpl

use of org.aion.zero.impl.pendingState.AionPendingStateImpl in project aion by aionnetwork.

the class AionHub method initializeHub.

private void initializeHub(CfgAion _cfgAion, AionBlockchainImpl _blockchain, PendingTxCallback pendingTxCallback, NetworkBestBlockCallback networkBestBlockCallback, TransactionBroadcastCallback transactionBroadcastCallback, boolean forTest) {
    this.cfg = _cfgAion;
    // load event manager before init blockchain instance
    loadEventMgr(forTest);
    registerBlockEvents();
    // the current unit tests require passing in a different repository instance
    // during normal execution we need to instantiate the repository
    // for this reason we pass in null when a new instance is required
    this.blockchain = _blockchain == null ? new AionBlockchainImpl(cfg, eventMgr, forTest) : _blockchain;
    try {
        this.blockchain.load(cfg.getGenesis(), genLOG);
    } catch (IllegalStateException e) {
        genLOG.error("Found database corruption, please re-import your database by using ./aion.sh -n <network> --redo-import", e);
        System.exit(SystemExitCodes.DATABASE_CORRUPTION);
    }
    this.startingBlock = this.blockchain.getBestBlock();
    if (blockchain.forkUtility.is040ForkActive(blockchain.getBestBlock().getNumber())) {
        TransactionTypeRule.allowAVMContractTransaction();
    }
    this.mempool = new AionPendingStateImpl(blockchain, cfg.getConsensus().getEnergyStrategy().getUpperBound(), cfg.getTx().getTxPendingTimeout(), cfg.getTx().getPoolBackup(), cfg.getTx().isSeedMode(), cfg.getTx().getPoolDump(), pendingTxCallback, networkBestBlockCallback, transactionBroadcastCallback, forTest);
    if (cfg.getTx().isSeedMode()) {
        genLOG.info("Seed node mode enabled!");
    }
    /*
         * p2p hook up start sync mgr needs to be initialed after loadBlockchain()
         * method
         */
    CfgNetP2p cfgNetP2p = this.cfg.getNet().getP2p();
    this.chainId = this.cfg.getNet().getId();
    // there are two p2p implementation , now just point to impl1.
    this.p2pMgr = new P2pMgr(AionLoggerFactory.getLogger(LogEnum.P2P.name()), AionLoggerFactory.getLogger(LogEnum.SURVEY.name()), this.chainId, Version.KERNEL_VERSION, this.cfg.getId(), cfgNetP2p.getIp(), cfgNetP2p.getPort(), this.cfg.getNet().getNodes(), cfgNetP2p.getDiscover(), cfgNetP2p.getMaxTempNodes(), cfgNetP2p.getMaxActiveNodes(), cfgNetP2p.getBootlistSyncOnly(), cfgNetP2p.getErrorTolerance());
    this.syncMgr = new SyncMgr(blockchain, p2pMgr, eventMgr, cfg.getSync().getShowStatus(), cfg.getSync().getShowStatistics(), cfg.getNet().getP2p().getMaxActiveNodes());
    ChainConfiguration chainConfig = new ChainConfiguration();
    this.propHandler = new BlockPropagationHandler(1024, this.blockchain, syncMgr.getSyncStats(), p2pMgr, chainConfig.createBlockHeaderValidator(), cfg.getNet().getP2p().inSyncOnlyMode(), apiVersion, mempool);
    registerCallback();
    if (!forTest) {
        p2pMgr.run();
    }
    if (!AionBlockchainImpl.enableFullSyncCheck) {
        this.pow = new AionPoW();
        this.pow.init(blockchain, mempool, eventMgr, syncMgr);
    }
    SelfNodeStatusCallback callback = new SelfNodeStatusCallback(p2pMgr);
    callback.updateBlockStatus(blockchain.getBestBlock().getNumber(), blockchain.getBestBlock().getHash(), blockchain.getTotalDifficulty());
    blockchain.setNodeStatusCallback(callback);
    blockchain.setBestBlockImportCallback(new BestBlockImportCallback(mempool));
}
Also used : AionPoW(org.aion.zero.impl.pow.AionPoW) AionPendingStateImpl(org.aion.zero.impl.pendingState.AionPendingStateImpl) IP2pMgr(org.aion.p2p.IP2pMgr) P2pMgr(org.aion.p2p.impl1.P2pMgr) CfgNetP2p(org.aion.zero.impl.config.CfgNetP2p) BlockPropagationHandler(org.aion.zero.impl.sync.handler.BlockPropagationHandler) SyncMgr(org.aion.zero.impl.sync.SyncMgr)

Example 2 with AionPendingStateImpl

use of org.aion.zero.impl.pendingState.AionPendingStateImpl 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 3 with AionPendingStateImpl

use of org.aion.zero.impl.pendingState.AionPendingStateImpl in project aion by aionnetwork.

the class BlockPropagationTest method testBlockPropagationReceiver.

/**
 * Test that we don't propagate back to the sender
 */
@Test
public void testBlockPropagationReceiver() {
    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());
    NodeMock senderMock = new NodeMock(sender, 1);
    Map<Integer, INode> node = new HashMap<>();
    node.put(1, senderMock);
    SyncStats syncStats = new SyncStats(block.getNumber(), true);
    P2pMock p2pMock = new P2pMock(node) {

        @Override
        public void send(int _nodeId, String s, Msg _msg) {
            throw new RuntimeException("should not have called send");
        }
    };
    StandaloneBlockchain.Bundle anotherBundle = new StandaloneBlockchain.Builder().withValidatorConfiguration("simple").withDefaultAccounts(accounts).withEventManger(this.loadEventMgr()).build();
    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));
    assertThat(handler.processIncomingBlock(senderMock.getIdHash(), "test", block)).isEqualTo(BlockPropagationHandler.PropStatus.CONNECTED);
}
Also used : Msg(org.aion.p2p.Msg) AionPendingStateImpl(org.aion.zero.impl.pendingState.AionPendingStateImpl) INode(org.aion.p2p.INode) TransactionBroadcastCallback(org.aion.zero.impl.blockchain.AionImpl.TransactionBroadcastCallback) BlockPropagationHandler(org.aion.zero.impl.sync.handler.BlockPropagationHandler) HashMap(java.util.HashMap) NetworkBestBlockCallback(org.aion.zero.impl.blockchain.AionImpl.NetworkBestBlockCallback) PendingTxCallback(org.aion.zero.impl.blockchain.AionImpl.PendingTxCallback) ECKey(org.aion.crypto.ECKey) StandaloneBlockchain(org.aion.zero.impl.blockchain.StandaloneBlockchain) MiningBlock(org.aion.zero.impl.types.MiningBlock) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) BigInteger(java.math.BigInteger) Test(org.junit.Test)

Example 4 with AionPendingStateImpl

use of org.aion.zero.impl.pendingState.AionPendingStateImpl in project aion by aionnetwork.

the class BlockPropagationTest method testIgnoreSelfBlock.

// this test scenario: we propagate a block out, and someone propagates back
@Test
public void testIgnoreSelfBlock() {
    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());
    NodeMock senderMock = new NodeMock(sender, 1);
    Map<Integer, INode> node = new HashMap<>();
    node.put(1, senderMock);
    AtomicInteger sendCount = new AtomicInteger();
    P2pMock p2pMock = new P2pMock(node) {

        @Override
        public void send(int _nodeId, String s, Msg _msg) {
            sendCount.getAndIncrement();
        }
    };
    StandaloneBlockchain.Bundle anotherBundle = new StandaloneBlockchain.Builder().withValidatorConfiguration("simple").withDefaultAccounts(accounts).withEventManger(this.loadEventMgr()).build();
    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));
    // pretend that we propagate the new block
    // send counter incremented
    handler.propagateNewBlock(block);
    // recall that we're using another blockchain and faked the propagation
    // so our blockchain should view this block as a new block
    // therefore if the filter fails, this block will actually be CONNECTED
    assertThat(handler.processIncomingBlock(senderMock.getIdHash(), "test", block)).isEqualTo(BlockPropagationHandler.PropStatus.DROPPED);
    // we expect the counter to be incremented once (on propagation)
    assertThat(sendCount.get()).isEqualTo(1);
}
Also used : Msg(org.aion.p2p.Msg) AionPendingStateImpl(org.aion.zero.impl.pendingState.AionPendingStateImpl) INode(org.aion.p2p.INode) TransactionBroadcastCallback(org.aion.zero.impl.blockchain.AionImpl.TransactionBroadcastCallback) BlockPropagationHandler(org.aion.zero.impl.sync.handler.BlockPropagationHandler) HashMap(java.util.HashMap) NetworkBestBlockCallback(org.aion.zero.impl.blockchain.AionImpl.NetworkBestBlockCallback) PendingTxCallback(org.aion.zero.impl.blockchain.AionImpl.PendingTxCallback) ECKey(org.aion.crypto.ECKey) StandaloneBlockchain(org.aion.zero.impl.blockchain.StandaloneBlockchain) MiningBlock(org.aion.zero.impl.types.MiningBlock) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) BigInteger(java.math.BigInteger) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Test(org.junit.Test)

Example 5 with AionPendingStateImpl

use of org.aion.zero.impl.pendingState.AionPendingStateImpl 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)

Aggregations

AionPendingStateImpl (org.aion.zero.impl.pendingState.AionPendingStateImpl)5 BlockPropagationHandler (org.aion.zero.impl.sync.handler.BlockPropagationHandler)5 BigInteger (java.math.BigInteger)4 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 NetworkBestBlockCallback (org.aion.zero.impl.blockchain.AionImpl.NetworkBestBlockCallback)4 PendingTxCallback (org.aion.zero.impl.blockchain.AionImpl.PendingTxCallback)4 TransactionBroadcastCallback (org.aion.zero.impl.blockchain.AionImpl.TransactionBroadcastCallback)4 StandaloneBlockchain (org.aion.zero.impl.blockchain.StandaloneBlockchain)4 MiningBlock (org.aion.zero.impl.types.MiningBlock)4 Test (org.junit.Test)4 IP2pMgr (org.aion.p2p.IP2pMgr)1 P2pMgr (org.aion.p2p.impl1.P2pMgr)1 CfgNetP2p (org.aion.zero.impl.config.CfgNetP2p)1 AionPoW (org.aion.zero.impl.pow.AionPoW)1 SyncMgr (org.aion.zero.impl.sync.SyncMgr)1 Block (org.aion.zero.impl.types.Block)1