Search in sources :

Example 16 with IAionBlockchain

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

the class RequestBlocksHandlerTest method testHeader.

@Test
public void testHeader() {
    Logger log = mock(Logger.class);
    IAionBlockchain chain = mock(AionBlockchainImpl.class);
    IP2pMgr p2p = mock(P2pMgr.class);
    RequestBlocksHandler handler = new RequestBlocksHandler(log, chain, p2p);
    // check handler header
    assertThat(handler.getHeader().getVer()).isEqualTo(Ver.V1);
    assertThat(handler.getHeader().getAction()).isEqualTo(Act.REQUEST_BLOCKS);
}
Also used : IAionBlockchain(org.aion.zero.impl.blockchain.IAionBlockchain) Logger(org.slf4j.Logger) IP2pMgr(org.aion.p2p.IP2pMgr) Test(org.junit.Test)

Example 17 with IAionBlockchain

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

the class RequestBlocksHandlerTest method testReceive_correctMessage_nullValue_withHeight.

@Test
public void testReceive_correctMessage_nullValue_withHeight() {
    Logger log = mock(Logger.class);
    when(log.isDebugEnabled()).thenReturn(true);
    IAionBlockchain chain = mock(AionBlockchainImpl.class);
    when(chain.getBlocksByRange(10L, 1L)).thenReturn(null);
    IP2pMgr p2p = mock(P2pMgr.class);
    RequestBlocksHandler handler = new RequestBlocksHandler(log, chain, p2p);
    // receive correct message
    byte[] encoding = RLP.encodeList(RLP.encodeByte(isTrue), RLP.encode(10L), RLP.encodeInt(10), RLP.encodeByte(isTrue));
    handler.receive(peerId, displayId, encoding);
    verify(log, times(1)).debug("<request-blocks from-block={} count={} order={}>", 10L, 10, "DESC");
    verify(chain, times(1)).getBlocksByRange(10L, 1L);
    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 18 with IAionBlockchain

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

the class RequestBlocksHandlerTest method testReceive_nullMessage.

@Test
public void testReceive_nullMessage() {
    Logger log = mock(Logger.class);
    IAionBlockchain chain = mock(AionBlockchainImpl.class);
    IP2pMgr p2p = mock(P2pMgr.class);
    RequestBlocksHandler handler = new RequestBlocksHandler(log, chain, p2p);
    // receive null message
    handler.receive(peerId, displayId, null);
    verify(log, times(1)).debug("<request-blocks empty message from peer={}>", displayId);
    verifyZeroInteractions(chain);
    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 19 with IAionBlockchain

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

the class RequestBlocksHandlerTest method testReceive_incorrectMessage.

@Test
public void testReceive_incorrectMessage() {
    Logger log = mock(Logger.class);
    when(log.isTraceEnabled()).thenReturn(false);
    IAionBlockchain chain = mock(AionBlockchainImpl.class);
    IP2pMgr p2p = mock(P2pMgr.class);
    RequestBlocksHandler handler = new RequestBlocksHandler(log, chain, p2p);
    // receive incorrect message
    byte[] incorrectEncoding = RLP.encodeList(RLP.encodeByte(isTrue), RLP.encode(10L), RLP.encodeInt(10), RLP.encode(Byte.MAX_VALUE + 1));
    handler.receive(peerId, displayId, incorrectEncoding);
    verify(log, times(1)).error("<request-blocks decode-error msg-bytes={} peer={}>", incorrectEncoding.length, displayId);
    verifyZeroInteractions(chain);
    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 20 with IAionBlockchain

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

the class RequestBlocksHandlerTest method testReceive_correctMessage_nullValue_withHash.

@Test
public void testReceive_correctMessage_nullValue_withHash() {
    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(null);
    IP2pMgr p2p = mock(P2pMgr.class);
    RequestBlocksHandler handler = new RequestBlocksHandler(log, chain, p2p);
    // receive correct message
    byte[] encoding = RLP.encodeList(RLP.encodeByte(isFalse), RLP.encode(hash), RLP.encodeInt(4), RLP.encodeByte(isFalse));
    handler.receive(peerId, displayId, encoding);
    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 : 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)

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