Search in sources :

Example 1 with RpcEncoding

use of tech.pegasys.teku.networking.eth2.rpc.core.encodings.RpcEncoding in project teku by ConsenSys.

the class BlockPropagationIntegrationTest method shouldFetchUnknownAncestorsOfPropagatedBlock.

@Test
public void shouldFetchUnknownAncestorsOfPropagatedBlock() throws Exception {
    final GossipEncoding gossipEncoding = GossipEncoding.SSZ_SNAPPY;
    UInt64 currentSlot = SpecConfig.GENESIS_SLOT;
    // Setup node 1
    SyncingNodeManager node1 = SyncingNodeManager.create(asyncRunner, networkFactory, validatorKeys, c -> c.rpcEncoding(rpcEncoding).gossipEncoding(gossipEncoding));
    node1.chainUtil().setSlot(currentSlot);
    // Add some blocks to node1, which node 2 will need to fetch
    final List<SignedBeaconBlock> blocksToFetch = new ArrayList<>();
    for (int i = 0; i < 3; i++) {
        currentSlot = currentSlot.plus(UInt64.ONE);
        final SignedBeaconBlock block = node1.chainUtil().createAndImportBlockAtSlot(currentSlot);
        blocksToFetch.add(block);
    }
    // Setup node 2
    SyncingNodeManager node2 = SyncingNodeManager.create(asyncRunner, networkFactory, validatorKeys, c -> c.rpcEncoding(rpcEncoding).gossipEncoding(gossipEncoding));
    // Connect networks
    Waiter.waitFor(node1.connect(node2));
    // Wait for connections to get set up
    Waiter.waitFor(() -> {
        assertThat(node1.network().getPeerCount()).isEqualTo(1);
        assertThat(node2.network().getPeerCount()).isEqualTo(1);
    });
    // Wait for subscriptions to complete (jvm-libp2p does this asynchronously)
    Thread.sleep(2000);
    // Update slot so that blocks can be imported
    currentSlot = currentSlot.plus(UInt64.ONE);
    node1.setSlot(currentSlot);
    node2.setSlot(currentSlot);
    // Propagate new block
    final SignedBeaconBlock newBlock = node1.chainUtil().createBlockAtSlot(currentSlot);
    node1.gossipBlock(newBlock);
    // Verify that node2 fetches required blocks in response
    Waiter.waitFor(() -> {
        for (SignedBeaconBlock block : blocksToFetch) {
            final Bytes32 blockRoot = block.getMessage().hashTreeRoot();
            assertThatSafeFuture(node2.recentChainData().retrieveBlockByRoot(blockRoot)).isCompletedWithNonEmptyOptional();
        }
        // Last block should be imported as well
        final Bytes32 newBlockRoot = newBlock.getMessage().hashTreeRoot();
        assertThatSafeFuture(node2.recentChainData().retrieveBlockByRoot(newBlockRoot)).isCompletedWithNonEmptyOptional();
    });
}
Also used : GossipEncoding(tech.pegasys.teku.networking.eth2.gossip.encoding.GossipEncoding) ArrayList(java.util.ArrayList) UInt64(tech.pegasys.teku.infrastructure.unsigned.UInt64) SignedBeaconBlock(tech.pegasys.teku.spec.datastructures.blocks.SignedBeaconBlock) Bytes32(org.apache.tuweni.bytes.Bytes32) Test(org.junit.jupiter.api.Test)

Example 2 with RpcEncoding

use of tech.pegasys.teku.networking.eth2.rpc.core.encodings.RpcEncoding in project teku by ConsenSys.

the class BeaconChainMethods method createMetadata.

private static Eth2RpcMethod<EmptyMessage, MetadataMessage> createMetadata(final Spec spec, final AsyncRunner asyncRunner, final MetadataMessagesFactory metadataMessagesFactory, final PeerLookup peerLookup, final RpcEncoding rpcEncoding) {
    final MetadataMessageHandler messageHandler = new MetadataMessageHandler(spec, metadataMessagesFactory);
    final EmptyMessageSchema requestType = EmptyMessage.SSZ_SCHEMA;
    final boolean expectResponse = true;
    final SszSchema<MetadataMessage> phase0MetadataSchema = SszSchema.as(MetadataMessage.class, spec.forMilestone(SpecMilestone.PHASE0).getSchemaDefinitions().getMetadataMessageSchema());
    final RpcContextCodec<?, MetadataMessage> phase0ContextCodec = RpcContextCodec.noop(phase0MetadataSchema);
    final SingleProtocolEth2RpcMethod<EmptyMessage, MetadataMessage> v1Method = new SingleProtocolEth2RpcMethod<>(asyncRunner, BeaconChainMethodIds.GET_METADATA, 1, rpcEncoding, requestType, expectResponse, phase0ContextCodec, messageHandler, peerLookup);
    if (spec.isMilestoneSupported(SpecMilestone.ALTAIR)) {
        final SszSchema<MetadataMessage> altairMetadataSchema = SszSchema.as(MetadataMessage.class, spec.forMilestone(SpecMilestone.ALTAIR).getSchemaDefinitions().getMetadataMessageSchema());
        final RpcContextCodec<?, MetadataMessage> altairContextCodec = RpcContextCodec.noop(altairMetadataSchema);
        final SingleProtocolEth2RpcMethod<EmptyMessage, MetadataMessage> v2Method = new SingleProtocolEth2RpcMethod<>(asyncRunner, BeaconChainMethodIds.GET_METADATA, 2, rpcEncoding, requestType, expectResponse, altairContextCodec, messageHandler, peerLookup);
        return VersionedEth2RpcMethod.create(rpcEncoding, requestType, expectResponse, List.of(v2Method, v1Method));
    } else {
        return v1Method;
    }
}
Also used : MetadataMessageHandler(tech.pegasys.teku.networking.eth2.rpc.beaconchain.methods.MetadataMessageHandler) SingleProtocolEth2RpcMethod(tech.pegasys.teku.networking.eth2.rpc.core.methods.SingleProtocolEth2RpcMethod) MetadataMessage(tech.pegasys.teku.spec.datastructures.networking.libp2p.rpc.metadata.MetadataMessage) EmptyMessageSchema(tech.pegasys.teku.spec.datastructures.networking.libp2p.rpc.EmptyMessage.EmptyMessageSchema) EmptyMessage(tech.pegasys.teku.spec.datastructures.networking.libp2p.rpc.EmptyMessage)

Example 3 with RpcEncoding

use of tech.pegasys.teku.networking.eth2.rpc.core.encodings.RpcEncoding in project teku by ConsenSys.

the class BeaconChainMethods method createBeaconBlocksByRange.

private static Eth2RpcMethod<BeaconBlocksByRangeRequestMessage, SignedBeaconBlock> createBeaconBlocksByRange(final Spec spec, final AsyncRunner asyncRunner, final RecentChainData recentChainData, final CombinedChainDataClient combinedChainDataClient, final PeerLookup peerLookup, final RpcEncoding rpcEncoding) {
    final BeaconBlocksByRangeMessageHandler beaconBlocksByRangeHandler = new BeaconBlocksByRangeMessageHandler(spec, combinedChainDataClient, MAX_BLOCK_BY_RANGE_REQUEST_SIZE);
    // V1 request only deal with Phase0 blocks
    final SignedBeaconBlockSchema phase0BlockSchema = spec.forMilestone(SpecMilestone.PHASE0).getSchemaDefinitions().getSignedBeaconBlockSchema();
    final RpcContextCodec<?, SignedBeaconBlock> noContextCodec = RpcContextCodec.noop(phase0BlockSchema);
    final BeaconBlocksByRangeRequestMessageSchema requestType = BeaconBlocksByRangeRequestMessage.SSZ_SCHEMA;
    final boolean expectResponseToRequest = true;
    final SingleProtocolEth2RpcMethod<BeaconBlocksByRangeRequestMessage, SignedBeaconBlock> v1Method = new SingleProtocolEth2RpcMethod<>(asyncRunner, BeaconChainMethodIds.BEACON_BLOCKS_BY_RANGE, 1, rpcEncoding, requestType, expectResponseToRequest, noContextCodec, beaconBlocksByRangeHandler, peerLookup);
    if (spec.isMilestoneSupported(SpecMilestone.ALTAIR)) {
        final RpcContextCodec<Bytes4, SignedBeaconBlock> forkDigestContextCodec = RpcContextCodec.forkDigest(spec, recentChainData, ForkDigestPayloadContext.SIGNED_BEACONBLOCK);
        final SingleProtocolEth2RpcMethod<BeaconBlocksByRangeRequestMessage, SignedBeaconBlock> v2Method = new SingleProtocolEth2RpcMethod<>(asyncRunner, BeaconChainMethodIds.BEACON_BLOCKS_BY_RANGE, 2, rpcEncoding, requestType, expectResponseToRequest, forkDigestContextCodec, beaconBlocksByRangeHandler, peerLookup);
        return VersionedEth2RpcMethod.create(rpcEncoding, requestType, expectResponseToRequest, List.of(v2Method, v1Method));
    } else {
        return v1Method;
    }
}
Also used : SingleProtocolEth2RpcMethod(tech.pegasys.teku.networking.eth2.rpc.core.methods.SingleProtocolEth2RpcMethod) BeaconBlocksByRangeRequestMessageSchema(tech.pegasys.teku.spec.datastructures.networking.libp2p.rpc.BeaconBlocksByRangeRequestMessage.BeaconBlocksByRangeRequestMessageSchema) Bytes4(tech.pegasys.teku.infrastructure.bytes.Bytes4) BeaconBlocksByRangeMessageHandler(tech.pegasys.teku.networking.eth2.rpc.beaconchain.methods.BeaconBlocksByRangeMessageHandler) SignedBeaconBlock(tech.pegasys.teku.spec.datastructures.blocks.SignedBeaconBlock) SignedBeaconBlockSchema(tech.pegasys.teku.spec.datastructures.blocks.SignedBeaconBlockSchema) BeaconBlocksByRangeRequestMessage(tech.pegasys.teku.spec.datastructures.networking.libp2p.rpc.BeaconBlocksByRangeRequestMessage)

Example 4 with RpcEncoding

use of tech.pegasys.teku.networking.eth2.rpc.core.encodings.RpcEncoding in project teku by ConsenSys.

the class PeerStatusIntegrationTest method shouldExchangeStatusMessagesOnConnection.

@Test
public void shouldExchangeStatusMessagesOnConnection() throws Exception {
    final StorageSystem system2 = createStorageSystem();
    final RecentChainData recentChainData2 = system2.recentChainData();
    assertThat(recentChainData1.getBestBlockRoot()).isEqualTo(recentChainData2.getBestBlockRoot());
    final Eth2P2PNetwork network1 = networkFactory.builder().spec(spec).rpcEncoding(rpcEncoding).recentChainData(recentChainData1).startNetwork();
    final Eth2P2PNetwork network2 = networkFactory.builder().spec(spec).rpcEncoding(rpcEncoding).recentChainData(recentChainData2).startNetwork();
    waitFor(network1.connect(network1.createPeerAddress(network2.getNodeAddress())));
    waitFor(() -> {
        assertThat(network1.getPeerCount()).isEqualTo(1);
        assertThat(network2.getPeerCount()).isEqualTo(1);
    });
    final Eth2Peer network2ViewOfPeer1 = network2.getPeer(network1.getNodeId()).orElseThrow();
    assertStatusMatchesStorage(recentChainData1, network2ViewOfPeer1.getStatus());
    final Eth2Peer network1ViewOfPeer2 = network1.getPeer(network2.getNodeId()).orElseThrow();
    assertStatusMatchesStorage(recentChainData2, network1ViewOfPeer2.getStatus());
    // When the finalized epoch is genesis we should use a zero finalized root (from the state)
    // This differs from what recentChainData.getFinalizedCheckpoint will have at genesis
    assertThat(network1ViewOfPeer2.getStatus().getFinalizedRoot()).isEqualTo(Bytes32.ZERO);
}
Also used : RecentChainData(tech.pegasys.teku.storage.client.RecentChainData) Eth2Peer(tech.pegasys.teku.networking.eth2.peers.Eth2Peer) StorageSystem(tech.pegasys.teku.storage.storageSystem.StorageSystem) Test(org.junit.jupiter.api.Test)

Example 5 with RpcEncoding

use of tech.pegasys.teku.networking.eth2.rpc.core.encodings.RpcEncoding in project teku by ConsenSys.

the class PeerStatusIntegrationTest method shouldUpdatePeerStatus.

@Test
public void shouldUpdatePeerStatus() throws Exception {
    final Eth2P2PNetwork network1 = networkFactory.builder().spec(spec).rpcEncoding(rpcEncoding).recentChainData(recentChainData1).startNetwork();
    final StorageSystem storageSystem2 = createStorageSystem();
    final RecentChainData recentChainData2 = storageSystem2.recentChainData();
    final Eth2P2PNetwork network2 = networkFactory.builder().spec(spec).rpcEncoding(rpcEncoding).recentChainData(recentChainData2).peer(network1).startNetwork();
    final Eth2Peer network2ViewOfPeer1 = network2.getPeer(network1.getNodeId()).orElseThrow();
    assertStatusMatchesStorage(recentChainData1, network2ViewOfPeer1.getStatus());
    // Peer 1 advances
    this.storageSystem.chainUpdater().advanceChain(10);
    final PeerStatus updatedStatusData = waitFor(network2ViewOfPeer1.sendStatus());
    assertStatusMatchesStorage(recentChainData1, updatedStatusData);
    assertStatusMatchesStorage(recentChainData1, network2ViewOfPeer1.getStatus());
}
Also used : RecentChainData(tech.pegasys.teku.storage.client.RecentChainData) PeerStatus(tech.pegasys.teku.networking.eth2.peers.PeerStatus) Eth2Peer(tech.pegasys.teku.networking.eth2.peers.Eth2Peer) StorageSystem(tech.pegasys.teku.storage.storageSystem.StorageSystem) Test(org.junit.jupiter.api.Test)

Aggregations

Test (org.junit.jupiter.api.Test)6 Eth2Peer (tech.pegasys.teku.networking.eth2.peers.Eth2Peer)6 SingleProtocolEth2RpcMethod (tech.pegasys.teku.networking.eth2.rpc.core.methods.SingleProtocolEth2RpcMethod)6 StorageSystem (tech.pegasys.teku.storage.storageSystem.StorageSystem)5 RecentChainData (tech.pegasys.teku.storage.client.RecentChainData)4 Bytes4 (tech.pegasys.teku.infrastructure.bytes.Bytes4)3 SignedBeaconBlock (tech.pegasys.teku.spec.datastructures.blocks.SignedBeaconBlock)3 Bytes (org.apache.tuweni.bytes.Bytes)2 Bytes32 (org.apache.tuweni.bytes.Bytes32)2 UInt64 (tech.pegasys.teku.infrastructure.unsigned.UInt64)2 GossipEncoding (tech.pegasys.teku.networking.eth2.gossip.encoding.GossipEncoding)2 RpcEncoding (tech.pegasys.teku.networking.eth2.rpc.core.encodings.RpcEncoding)2 Spec (tech.pegasys.teku.spec.Spec)2 SignedBeaconBlockSchema (tech.pegasys.teku.spec.datastructures.blocks.SignedBeaconBlockSchema)2 StatusMessage (tech.pegasys.teku.spec.datastructures.networking.libp2p.rpc.StatusMessage)2 Duration (java.time.Duration)1 ArrayList (java.util.ArrayList)1 ExecutionException (java.util.concurrent.ExecutionException)1 Assertions (org.assertj.core.api.Assertions)1 Condition (org.assertj.core.api.Condition)1