Search in sources :

Example 6 with Peer

use of co.rsk.net.Peer in project rskj by rsksmart.

the class DecidingSyncStateTest method forwardsSynchronization.

@Test
public void forwardsSynchronization() {
    SyncConfiguration syncConfiguration = SyncConfiguration.DEFAULT;
    PeersInformation peersInformation = mock(PeersInformation.class);
    SyncEventsHandler syncEventsHandler = mock(SyncEventsHandler.class);
    BlockStore blockStore = mock(BlockStore.class);
    SyncState syncState = new DecidingSyncState(syncConfiguration, syncEventsHandler, peersInformation, blockStore);
    when(peersInformation.count()).thenReturn(syncConfiguration.getExpectedPeers() + 1);
    Peer peer = mock(Peer.class);
    when(peersInformation.getBestPeer()).thenReturn(Optional.of(peer));
    SyncPeerStatus syncPeerStatus = mock(SyncPeerStatus.class);
    Status status = mock(Status.class);
    when(syncPeerStatus.getStatus()).thenReturn(status);
    when(peersInformation.getPeer(peer)).thenReturn(syncPeerStatus);
    Block block = mock(Block.class);
    long myBestBlockNumber = 90L;
    when(block.getNumber()).thenReturn(myBestBlockNumber);
    when(blockStore.getBestBlock()).thenReturn(block);
    when(blockStore.getMinNumber()).thenReturn(1L);
    when(status.getBestBlockNumber()).thenReturn(myBestBlockNumber + 1 + syncConfiguration.getLongSyncLimit());
    syncState.newPeerStatus();
    verify(syncEventsHandler).startSyncing(peer);
}
Also used : Status(co.rsk.net.Status) BlockStore(org.ethereum.db.BlockStore) Peer(co.rsk.net.Peer) SimplePeer(co.rsk.net.simples.SimplePeer) Block(org.ethereum.core.Block) Test(org.junit.Test)

Example 7 with Peer

use of co.rsk.net.Peer in project rskj by rsksmart.

the class DownloadingBackwardsBodiesSyncStateTest method connectingUntilGenesis.

@Test
public void connectingUntilGenesis() {
    LinkedList<BlockHeader> toRequest = new LinkedList<>();
    LinkedList<BodyResponseMessage> responses = new LinkedList<>();
    LinkedList<Block> expectedBlocks = new LinkedList<>();
    Function<Long, BlockDifficulty> difficultyForBlockNumber = (n) -> new BlockDifficulty(BigInteger.valueOf(n * (n + 1) / 2));
    // their indexes and each one is the children of the previous block.
    for (long i = 1; i <= 10; i++) {
        BlockHeader headerToRequest = mock(BlockHeader.class);
        when(headerToRequest.getNumber()).thenReturn(i);
        Keccak256 headerHash = new Keccak256(ByteUtil.leftPadBytes(ByteUtil.longToBytes(i), 32));
        when(headerToRequest.getHash()).thenReturn(headerHash);
        toRequest.addFirst(headerToRequest);
        when(syncEventsHandler.sendBodyRequest(any(), eq(headerToRequest))).thenReturn(i);
        BodyResponseMessage response = new BodyResponseMessage(i, new LinkedList<>(), new LinkedList<>());
        responses.addFirst(response);
        Block block = mock(Block.class);
        expectedBlocks.addFirst(block);
        when(block.getNumber()).thenReturn(i);
        when(block.getHash()).thenReturn(headerHash);
        when(blockFactory.newBlock(headerToRequest, response.getTransactions(), response.getUncles())).thenReturn(block);
        when(block.isParentOf(any())).thenReturn(true);
        when(blockStore.getTotalDifficultyForHash(headerHash.getBytes())).thenReturn(difficultyForBlockNumber.apply(i));
        when(block.getCumulativeDifficulty()).thenReturn(new BlockDifficulty(BigInteger.valueOf(i)));
    }
    when(genesis.isParentOf(expectedBlocks.getLast())).thenReturn(true);
    when(genesis.getCumulativeDifficulty()).thenReturn(new BlockDifficulty(BigInteger.valueOf(0L)));
    Keccak256 childHash = new Keccak256(ByteUtil.leftPadBytes(ByteUtil.intToBytes(11), 32));
    when(child.getHash()).thenReturn(childHash);
    when(blockStore.getTotalDifficultyForHash(childHash.getBytes())).thenReturn(difficultyForBlockNumber.apply(11L));
    when(child.getCumulativeDifficulty()).thenReturn(new BlockDifficulty(BigInteger.valueOf(11L)));
    when(child.getNumber()).thenReturn(11L);
    DownloadingBackwardsBodiesSyncState target = new DownloadingBackwardsBodiesSyncState(syncConfiguration, syncEventsHandler, peersInformation, genesis, blockFactory, blockStore, child, toRequest, peer);
    while (!responses.isEmpty()) {
        target.onEnter();
        target.newBody(responses.pop(), mock(Peer.class));
        Block block = expectedBlocks.pop();
        BlockDifficulty expectedDifficulty = difficultyForBlockNumber.apply(block.getNumber());
        verify(blockStore).saveBlock(eq(block), eq(expectedDifficulty), eq(true));
    }
}
Also used : BlockDifficulty(co.rsk.core.BlockDifficulty) Peer(co.rsk.net.Peer) Test(org.junit.Test) BlockStore(org.ethereum.db.BlockStore) Keccak256(co.rsk.crypto.Keccak256) Function(java.util.function.Function) Mockito(org.mockito.Mockito) List(java.util.List) ByteUtil(org.ethereum.util.ByteUtil) BigInteger(java.math.BigInteger) LinkedList(java.util.LinkedList) BodyResponseMessage(co.rsk.net.messages.BodyResponseMessage) org.ethereum.core(org.ethereum.core) Before(org.junit.Before) BodyResponseMessage(co.rsk.net.messages.BodyResponseMessage) Peer(co.rsk.net.Peer) Keccak256(co.rsk.crypto.Keccak256) LinkedList(java.util.LinkedList) BlockDifficulty(co.rsk.core.BlockDifficulty) Test(org.junit.Test)

Aggregations

Peer (co.rsk.net.Peer)7 BlockStore (org.ethereum.db.BlockStore)5 Test (org.junit.Test)5 Status (co.rsk.net.Status)3 SimplePeer (co.rsk.net.simples.SimplePeer)3 Block (org.ethereum.core.Block)3 BlockDifficulty (co.rsk.core.BlockDifficulty)2 Keccak256 (co.rsk.crypto.Keccak256)2 BodyResponseMessage (co.rsk.net.messages.BodyResponseMessage)2 BigInteger (java.math.BigInteger)2 LinkedList (java.util.LinkedList)2 List (java.util.List)2 Function (java.util.function.Function)2 org.ethereum.core (org.ethereum.core)2 ByteUtil (org.ethereum.util.ByteUtil)2 Before (org.junit.Before)2 Mockito (org.mockito.Mockito)2 SimpleNodeChannel (co.rsk.net.simples.SimpleNodeChannel)1