Search in sources :

Example 1 with Peer

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

the class DecidingSyncStateTest method forwardsSynchronization_genesisIsConnected.

@Test
public void forwardsSynchronization_genesisIsConnected() {
    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);
    when(blockStore.getMinNumber()).thenReturn(0L);
    Block block = mock(Block.class);
    long myBestBlockNumber = 90L;
    when(block.getNumber()).thenReturn(myBestBlockNumber);
    when(blockStore.getBestBlock()).thenReturn(block);
    when(status.getBestBlockNumber()).thenReturn(myBestBlockNumber + 1);
    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 2 with Peer

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

the class DownloadingBackwardsBodiesSyncStateTest method connecting_notGenesis.

@Test
public void connecting_notGenesis() {
    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 = 2; 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)));
    }
    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)

Example 3 with Peer

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

the class DecidingSyncState method tryStartSyncing.

private void tryStartSyncing() {
    Optional<Peer> bestPeerOpt = peersInformation.getBestPeer();
    Optional<Long> peerBestBlockNumOpt = bestPeerOpt.flatMap(this::getPeerBestBlockNumber);
    if (!bestPeerOpt.isPresent() || !peerBestBlockNumOpt.isPresent()) {
        // no best peer, skip
        syncEventsHandler.onLongSyncUpdate(false, null);
        return;
    }
    Peer bestPeer = bestPeerOpt.get();
    long peerBestBlockNum = peerBestBlockNumOpt.get();
    if (shouldLongSync(peerBestBlockNum)) {
        // start "long" / "forward" sync
        syncEventsHandler.onLongSyncUpdate(true, peerBestBlockNum);
        syncEventsHandler.startSyncing(bestPeer);
    } else {
        // start "short" / "backward" sync
        syncEventsHandler.onLongSyncUpdate(false, null);
        syncEventsHandler.backwardSyncing(bestPeer);
    }
}
Also used : Peer(co.rsk.net.Peer)

Example 4 with Peer

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

the class SimpleChannelManager method getMessageChannel.

public Peer getMessageChannel(SimpleNode sender, SimpleNode receiver) {
    Peer channel = simpleChannels.get(sender.getNodeID());
    if (channel != null) {
        return channel;
    }
    channel = new SimpleNodeChannel(sender, receiver);
    simpleChannels.put(channel.getPeerNodeID(), channel);
    return channel;
}
Also used : Peer(co.rsk.net.Peer) SimpleNodeChannel(co.rsk.net.simples.SimpleNodeChannel)

Example 5 with Peer

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

the class DecidingSyncStateTest method backwardsSynchronization.

@Test
public void backwardsSynchronization() {
    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);
    when(blockStore.getMinNumber()).thenReturn(1L);
    Block block = mock(Block.class);
    long myBestBlockNumber = 90L;
    when(block.getNumber()).thenReturn(myBestBlockNumber);
    when(blockStore.getBestBlock()).thenReturn(block);
    when(status.getBestBlockNumber()).thenReturn(myBestBlockNumber + syncConfiguration.getLongSyncLimit());
    syncState.newPeerStatus();
    verify(syncEventsHandler).backwardSyncing(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)

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