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();
});
}
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;
}
}
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;
}
}
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);
}
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());
}
Aggregations