Search in sources :

Example 6 with RequestBlocks

use of org.aion.zero.impl.sync.msg.RequestBlocks in project aion by aionnetwork.

the class RequestBlocksHandlerTest method testReceive_correctMessage_descending_withHash.

@Test
public void testReceive_correctMessage_descending_withHash() {
    Block first = consecutiveBlocks.get(3);
    byte[] hash = first.getHash();
    Block last = consecutiveBlocks.get(0);
    // reverse the list order
    LinkedList<Block> reverse = new LinkedList<>();
    for (Block b : consecutiveBlocks) {
        reverse.addFirst(b);
    }
    Logger log = mock(Logger.class);
    when(log.isDebugEnabled()).thenReturn(true);
    IAionBlockchain chain = mock(AionBlockchainImpl.class);
    when(chain.getBlockByHash(hash)).thenReturn(first);
    when(chain.getBlocksByRange(first.getNumber(), last.getNumber())).thenReturn(reverse);
    IP2pMgr p2p = mock(P2pMgr.class);
    RequestBlocksHandler handler = new RequestBlocksHandler(log, chain, p2p);
    // receive correct message
    RequestBlocks request = new RequestBlocks(hash, 4, true);
    handler.receive(peerId, displayId, request.encode());
    verify(log, times(1)).debug("<request-blocks from-block={} count={} order={}>", Hex.toHexString(hash), 4, "DESC");
    verify(chain, times(1)).getBlockByHash(hash);
    verify(chain, times(1)).getBlocksByRange(first.getNumber(), last.getNumber());
    ResponseBlocks expectedResponse = new ResponseBlocks(reverse);
    verify(p2p, times(1)).send(peerId, displayId, expectedResponse);
}
Also used : RequestBlocks(org.aion.zero.impl.sync.msg.RequestBlocks) Block(org.aion.zero.impl.types.Block) IAionBlockchain(org.aion.zero.impl.blockchain.IAionBlockchain) Logger(org.slf4j.Logger) IP2pMgr(org.aion.p2p.IP2pMgr) LinkedList(java.util.LinkedList) ResponseBlocks(org.aion.zero.impl.sync.msg.ResponseBlocks) Test(org.junit.Test)

Aggregations

RequestBlocks (org.aion.zero.impl.sync.msg.RequestBlocks)6 ResponseBlocks (org.aion.zero.impl.sync.msg.ResponseBlocks)6 Block (org.aion.zero.impl.types.Block)6 IP2pMgr (org.aion.p2p.IP2pMgr)5 IAionBlockchain (org.aion.zero.impl.blockchain.IAionBlockchain)5 Test (org.junit.Test)5 Logger (org.slf4j.Logger)5 LinkedList (java.util.LinkedList)2