Search in sources :

Example 1 with Bytes4

use of tech.pegasys.teku.infrastructure.bytes.Bytes4 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 2 with Bytes4

use of tech.pegasys.teku.infrastructure.bytes.Bytes4 in project teku by ConsenSys.

the class ActiveEth2P2PNetwork method getEth2Context.

private SafeFuture<Eth2Context> getEth2Context() {
    final ChainHead chainHead = recentChainData.getChainHead().orElseThrow();
    final Bytes4 forkDigest = recentChainData.getCurrentForkInfo().orElseThrow().getForkDigest(spec);
    final UInt64 currentSlot = recentChainData.getCurrentSlot().orElseThrow();
    final UInt64 currentEpoch = spec.computeEpochAtSlot(currentSlot);
    return chainHead.getState().thenApply(chainHeadState -> {
        final UInt64 activeValidatorsEpoch = spec.getMaxLookaheadEpoch(chainHeadState).min(currentEpoch);
        final int activeValidators = spec.countActiveValidators(chainHeadState, activeValidatorsEpoch);
        return Eth2Context.builder().currentSlot(currentSlot).activeValidatorCount(activeValidators).forkDigest(forkDigest).gossipEncoding(gossipEncoding).build();
    });
}
Also used : Bytes4(tech.pegasys.teku.infrastructure.bytes.Bytes4) UInt64(tech.pegasys.teku.infrastructure.unsigned.UInt64) ChainHead(tech.pegasys.teku.storage.client.ChainHead)

Example 3 with Bytes4

use of tech.pegasys.teku.infrastructure.bytes.Bytes4 in project teku by ConsenSys.

the class Eth2GossipTopicFilter method computeRelevantTopics.

private Set<String> computeRelevantTopics(final RecentChainData recentChainData, final GossipEncoding gossipEncoding) {
    final ForkInfo forkInfo = recentChainData.getCurrentForkInfo().orElseThrow();
    final Bytes4 forkDigest = forkInfo.getForkDigest(spec);
    final Set<String> topics = getAllTopics(gossipEncoding, forkDigest);
    spec.getForkSchedule().getForks().stream().filter(fork -> fork.getEpoch().isGreaterThanOrEqualTo(forkInfo.getFork().getEpoch())).map(futureFork -> spec.atEpoch(futureFork.getEpoch()).miscHelpers().computeForkDigest(futureFork.getCurrent_version(), forkInfo.getGenesisValidatorsRoot())).forEach(futureForkDigest -> topics.addAll(getAllTopics(gossipEncoding, futureForkDigest)));
    return topics;
}
Also used : Logger(org.apache.logging.log4j.Logger) RecentChainData(tech.pegasys.teku.storage.client.RecentChainData) GossipTopicFilter(tech.pegasys.teku.networking.p2p.libp2p.gossip.GossipTopicFilter) Suppliers(com.google.common.base.Suppliers) Set(java.util.Set) ForkInfo(tech.pegasys.teku.spec.datastructures.state.ForkInfo) GossipTopics.getAllTopics(tech.pegasys.teku.networking.eth2.gossip.topics.GossipTopics.getAllTopics) Bytes4(tech.pegasys.teku.infrastructure.bytes.Bytes4) Spec(tech.pegasys.teku.spec.Spec) Supplier(java.util.function.Supplier) LogManager(org.apache.logging.log4j.LogManager) GossipEncoding(tech.pegasys.teku.networking.eth2.gossip.encoding.GossipEncoding) ForkInfo(tech.pegasys.teku.spec.datastructures.state.ForkInfo) Bytes4(tech.pegasys.teku.infrastructure.bytes.Bytes4)

Example 4 with Bytes4

use of tech.pegasys.teku.infrastructure.bytes.Bytes4 in project teku by ConsenSys.

the class GossipTopicsTest method getTopic.

@Test
public void getTopic() {
    final Bytes4 forkDigest = Bytes4.fromHexString("0x01020304");
    final String topicName = "test";
    final String actual = GossipTopics.getTopic(forkDigest, topicName, gossipEncoding);
    assertThat(actual).isEqualTo("/eth2/01020304/test/ssz_snappy");
}
Also used : Bytes4(tech.pegasys.teku.infrastructure.bytes.Bytes4) Test(org.junit.jupiter.api.Test)

Example 5 with Bytes4

use of tech.pegasys.teku.infrastructure.bytes.Bytes4 in project teku by ConsenSys.

the class PeerStatusFactory method random.

public PeerStatus random() {
    final Bytes4 fork = spec.getGenesisSpecConfig().getGenesisForkVersion();
    final Bytes32 finalizedRoot = randomBytes32();
    final UInt64 finalizedEpoch = randomLong(0, 10);
    final Bytes32 headRoot = randomBytes32();
    final long minHeadSlot = (finalizedEpoch.longValue() + 2) * spec.getGenesisSpecConfig().getSlotsPerEpoch();
    final UInt64 headSlot = randomLong(minHeadSlot, minHeadSlot + 5);
    return new PeerStatus(fork, finalizedRoot, finalizedEpoch, headRoot, headSlot);
}
Also used : Bytes4(tech.pegasys.teku.infrastructure.bytes.Bytes4) UInt64(tech.pegasys.teku.infrastructure.unsigned.UInt64) Bytes32(org.apache.tuweni.bytes.Bytes32)

Aggregations

Bytes4 (tech.pegasys.teku.infrastructure.bytes.Bytes4)33 Test (org.junit.jupiter.api.Test)20 Bytes32 (org.apache.tuweni.bytes.Bytes32)5 UInt64 (tech.pegasys.teku.infrastructure.unsigned.UInt64)5 File (java.io.File)4 Bytes (org.apache.tuweni.bytes.Bytes)4 Eth2TopicHandler (tech.pegasys.teku.networking.eth2.gossip.topics.topichandlers.Eth2TopicHandler)4 SpecVersion (tech.pegasys.teku.spec.SpecVersion)4 Eth1Address (tech.pegasys.teku.spec.datastructures.eth1.Eth1Address)4 SignedBeaconBlock (tech.pegasys.teku.spec.datastructures.blocks.SignedBeaconBlock)3 EnrForkId (tech.pegasys.teku.spec.datastructures.networking.libp2p.rpc.EnrForkId)3 ForkInfo (tech.pegasys.teku.spec.datastructures.state.ForkInfo)3 SingleProtocolEth2RpcMethod (tech.pegasys.teku.networking.eth2.rpc.core.methods.SingleProtocolEth2RpcMethod)2 SignedBeaconBlockSchema (tech.pegasys.teku.spec.datastructures.blocks.SignedBeaconBlockSchema)2 Fork (tech.pegasys.teku.spec.datastructures.state.Fork)2 DatabaseStorageException (tech.pegasys.teku.storage.server.DatabaseStorageException)2 Suppliers (com.google.common.base.Suppliers)1 Optional (java.util.Optional)1 Random (java.util.Random)1 Set (java.util.Set)1