Search in sources :

Example 1 with BlockPropagationHandler

use of org.aion.zero.impl.sync.handler.BlockPropagationHandler 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();
    AionBlock block = bundle.bc.createNewBlock(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);
    P2pMock p2pMock = new P2pMock(node) {

        @Override
        public void send(int _nodeId, Msg _msg) {
            throw new RuntimeException("should not have called send");
        }
    };
    StandaloneBlockchain.Bundle anotherBundle = new StandaloneBlockchain.Builder().withValidatorConfiguration("simple").withDefaultAccounts(accounts).build();
    BlockPropagationHandler handler = new BlockPropagationHandler(1024, // NOTE: not the same blockchain that generated the block
    anotherBundle.bc, p2pMock, anotherBundle.bc.getBlockHeaderValidator());
    assertThat(handler.processIncomingBlock(senderMock.getIdHash(), block)).isEqualTo(BlockPropagationHandler.PropStatus.CONNECTED);
}
Also used : BlockPropagationHandler(org.aion.zero.impl.sync.handler.BlockPropagationHandler) ECKey(org.aion.crypto.ECKey) StandaloneBlockchain(org.aion.zero.impl.StandaloneBlockchain) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) BigInteger(java.math.BigInteger) AionBlock(org.aion.zero.impl.types.AionBlock) Test(org.junit.Test)

Example 2 with BlockPropagationHandler

use of org.aion.zero.impl.sync.handler.BlockPropagationHandler 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();
    AionBlock block = bundle.bc.createNewBlock(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, 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).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());
    AionBlock bestBlock = bundle.bc.getBestBlock();
    assertThat(bestBlock.getHash()).isEqualTo(anotherBundle.bc.genesis.getHash());
    BlockPropagationHandler handler = new BlockPropagationHandler(1024, // NOTE: not the same blockchain that generated the block
    anotherBundle.bc, p2pMock, anotherBundle.bc.getBlockHeaderValidator());
    // block is processed
    assertThat(handler.processIncomingBlock(senderMock.getIdHash(), block)).isEqualTo(BlockPropagationHandler.PropStatus.PROP_CONNECTED);
    assertThat(times.get()).isEqualTo(1);
}
Also used : BlockPropagationHandler(org.aion.zero.impl.sync.handler.BlockPropagationHandler) ECKey(org.aion.crypto.ECKey) StandaloneBlockchain(org.aion.zero.impl.StandaloneBlockchain) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) BigInteger(java.math.BigInteger) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AionBlock(org.aion.zero.impl.types.AionBlock) Test(org.junit.Test)

Example 3 with BlockPropagationHandler

use of org.aion.zero.impl.sync.handler.BlockPropagationHandler 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();
    AionBlock block = bundle.bc.createNewBlock(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, Msg _msg) {
            sendCount.getAndIncrement();
        }
    };
    StandaloneBlockchain.Bundle anotherBundle = new StandaloneBlockchain.Builder().withValidatorConfiguration("simple").withDefaultAccounts(accounts).build();
    BlockPropagationHandler handler = new BlockPropagationHandler(1024, // NOTE: not the same blockchain that generated the block
    anotherBundle.bc, p2pMock, anotherBundle.bc.getBlockHeaderValidator());
    // 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(), block)).isEqualTo(BlockPropagationHandler.PropStatus.DROPPED);
    // we expect the counter to be incremented once (on propagation)
    assertThat(sendCount.get()).isEqualTo(1);
}
Also used : BlockPropagationHandler(org.aion.zero.impl.sync.handler.BlockPropagationHandler) ECKey(org.aion.crypto.ECKey) StandaloneBlockchain(org.aion.zero.impl.StandaloneBlockchain) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) BigInteger(java.math.BigInteger) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AionBlock(org.aion.zero.impl.types.AionBlock) Test(org.junit.Test)

Example 4 with BlockPropagationHandler

use of org.aion.zero.impl.sync.handler.BlockPropagationHandler 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();
    AionBlock block = bundle.bc.createNewBlock(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, 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).build();
    assertThat(bundle.bc.genesis.getHash()).isEqualTo(anotherBundle.bc.genesis.getHash());
    BlockPropagationHandler handler = new BlockPropagationHandler(1024, // NOTE: not the same blockchain that generated the block
    anotherBundle.bc, p2pMock, anotherBundle.bc.getBlockHeaderValidator());
    // block is processed
    assertThat(handler.processIncomingBlock(senderMock.getIdHash(), block)).isEqualTo(BlockPropagationHandler.PropStatus.PROP_CONNECTED);
    assertThat(handler.processIncomingBlock(senderMock.getIdHash(), block)).isEqualTo(BlockPropagationHandler.PropStatus.DROPPED);
    assertThat(times.get()).isEqualTo(1);
}
Also used : BlockPropagationHandler(org.aion.zero.impl.sync.handler.BlockPropagationHandler) ECKey(org.aion.crypto.ECKey) StandaloneBlockchain(org.aion.zero.impl.StandaloneBlockchain) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) BigInteger(java.math.BigInteger) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AionBlock(org.aion.zero.impl.types.AionBlock) Test(org.junit.Test)

Aggregations

BigInteger (java.math.BigInteger)4 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)4 ECKey (org.aion.crypto.ECKey)4 StandaloneBlockchain (org.aion.zero.impl.StandaloneBlockchain)4 BlockPropagationHandler (org.aion.zero.impl.sync.handler.BlockPropagationHandler)4 AionBlock (org.aion.zero.impl.types.AionBlock)4 Test (org.junit.Test)4