Search in sources :

Example 21 with IAionBlockchain

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

the class RequestBlocksHandlerTest method testReceive_correctMessage_withHashOnSideChain.

@Test
public void testReceive_correctMessage_withHashOnSideChain() {
    Block first = consecutiveBlocks.get(0);
    byte[] hash = first.getHash();
    Block last = consecutiveBlocks.get(3);
    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(List.of(last));
    IP2pMgr p2p = mock(P2pMgr.class);
    RequestBlocksHandler handler = new RequestBlocksHandler(log, chain, p2p);
    // receive correct message
    RequestBlocks request = new RequestBlocks(hash, 4, false);
    handler.receive(peerId, displayId, request.encode());
    verify(log, times(1)).debug("<request-blocks from-block={} count={} order={}>", Hex.toHexString(hash), 4, "ASC");
    verify(chain, times(1)).getBlockByHash(hash);
    verify(chain, times(1)).getBlocksByRange(first.getNumber(), last.getNumber());
    ResponseBlocks expectedResponse = new ResponseBlocks(List.of(first));
    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) ResponseBlocks(org.aion.zero.impl.sync.msg.ResponseBlocks) Test(org.junit.Test)

Example 22 with IAionBlockchain

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

the class RequestBlocksHandlerTest method testReceive_correctMessage_ascending_withHeight.

@Test
public void testReceive_correctMessage_ascending_withHeight() {
    Block first = consecutiveBlocks.get(0);
    Block last = consecutiveBlocks.get(3);
    Logger log = mock(Logger.class);
    when(log.isDebugEnabled()).thenReturn(true);
    IAionBlockchain chain = mock(AionBlockchainImpl.class);
    when(chain.getBlocksByRange(first.getNumber(), last.getNumber())).thenReturn(consecutiveBlocks);
    IP2pMgr p2p = mock(P2pMgr.class);
    RequestBlocksHandler handler = new RequestBlocksHandler(log, chain, p2p);
    // receive correct message
    RequestBlocks request = new RequestBlocks(first.getNumber(), 4, false);
    handler.receive(peerId, displayId, request.encode());
    verify(log, times(1)).debug("<request-blocks from-block={} count={} order={}>", first.getNumber(), 4, "ASC");
    verify(chain, times(1)).getBlocksByRange(first.getNumber(), last.getNumber());
    ResponseBlocks expectedResponse = new ResponseBlocks(consecutiveBlocks);
    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) ResponseBlocks(org.aion.zero.impl.sync.msg.ResponseBlocks) Test(org.junit.Test)

Example 23 with IAionBlockchain

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

Example 24 with IAionBlockchain

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

the class RequestTrieDataHandlerTest method testReceive_correctMessage_nullValue.

@Test
public void testReceive_correctMessage_nullValue() {
    Logger log = mock(Logger.class);
    when(log.isDebugEnabled()).thenReturn(true);
    IAionBlockchain chain = mock(AionBlockchainImpl.class);
    when(chain.getTrieNode(nodeKey, STATE)).thenReturn(null);
    IP2pMgr p2p = mock(P2pMgr.class);
    RequestTrieDataHandler handler = new RequestTrieDataHandler(log, chain, p2p);
    // receive correct message
    byte[] encoding = RLP.encodeList(RLP.encodeElement(nodeKey), RLP.encodeString(STATE.toString()), RLP.encodeInt(0));
    handler.receive(peerId, displayId, encoding);
    verify(log, times(1)).debug("<req-trie from-db={} key={} peer={}>", STATE, wrappedNodeKey, displayId);
    verify(chain, times(1)).getTrieNode(nodeKey, STATE);
    verifyZeroInteractions(p2p);
}
Also used : IAionBlockchain(org.aion.zero.impl.blockchain.IAionBlockchain) Logger(org.slf4j.Logger) IP2pMgr(org.aion.p2p.IP2pMgr) Test(org.junit.Test)

Example 25 with IAionBlockchain

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

the class RequestTrieDataHandlerTest method testReceive_correctMessage_limitOne.

@Test
public void testReceive_correctMessage_limitOne() {
    Logger log = mock(Logger.class);
    when(log.isDebugEnabled()).thenReturn(true);
    IAionBlockchain chain = mock(AionBlockchainImpl.class);
    when(chain.getTrieNode(nodeKey, STATE)).thenReturn(leafValue);
    IP2pMgr p2p = mock(P2pMgr.class);
    RequestTrieDataHandler handler = new RequestTrieDataHandler(log, chain, p2p);
    // receive correct message
    byte[] encoding = RLP.encodeList(RLP.encodeElement(nodeKey), RLP.encodeString(STATE.toString()), RLP.encodeInt(1));
    handler.receive(peerId, displayId, encoding);
    verify(log, times(1)).debug("<req-trie from-db={} key={} peer={}>", STATE, wrappedNodeKey, displayId);
    verify(chain, times(1)).getTrieNode(nodeKey, STATE);
    ResponseTrieData expectedResponse = new ResponseTrieData(wrappedNodeKey, leafValue, STATE);
    verify(p2p, times(1)).send(peerId, displayId, expectedResponse);
}
Also used : ResponseTrieData(org.aion.zero.impl.sync.msg.ResponseTrieData) IAionBlockchain(org.aion.zero.impl.blockchain.IAionBlockchain) Logger(org.slf4j.Logger) IP2pMgr(org.aion.p2p.IP2pMgr) Test(org.junit.Test)

Aggregations

IAionBlockchain (org.aion.zero.impl.blockchain.IAionBlockchain)35 Test (org.junit.Test)35 IP2pMgr (org.aion.p2p.IP2pMgr)24 Logger (org.slf4j.Logger)24 Block (org.aion.zero.impl.types.Block)18 AionTransaction (org.aion.base.AionTransaction)11 AionAddress (org.aion.types.AionAddress)10 ForkUtility (org.aion.zero.impl.forks.ForkUtility)10 ResponseBlocks (org.aion.zero.impl.sync.msg.ResponseBlocks)7 RequestBlocks (org.aion.zero.impl.sync.msg.RequestBlocks)5 ResponseTrieData (org.aion.zero.impl.sync.msg.ResponseTrieData)3 LinkedList (java.util.LinkedList)2 ImportResult (org.aion.zero.impl.core.ImportResult)1