Search in sources :

Example 1 with OperationProcessor

use of tech.pegasys.teku.networking.eth2.gossip.topics.OperationProcessor in project teku by ConsenSys.

the class ProposerSlashingGossipIntegrationTest method shouldGossipToPeers.

@Test
public void shouldGossipToPeers() throws Exception {
    final GossipEncoding gossipEncoding = GossipEncoding.SSZ_SNAPPY;
    // Set up publishers & consumers
    Set<ProposerSlashing> receivedGossip = new HashSet<>();
    final OperationProcessor<ProposerSlashing> operationProcessor = (slashing) -> {
        receivedGossip.add(slashing);
        return SafeFuture.completedFuture(InternalValidationResult.ACCEPT);
    };
    // Setup network 1
    final Consumer<Eth2P2PNetworkBuilder> networkBuilder = b -> b.gossipEncoding(gossipEncoding);
    NodeManager node1 = createNodeManager(networkBuilder);
    // Setup network 2
    final Consumer<Eth2P2PNetworkBuilder> networkBuilder2 = b -> b.gossipEncoding(gossipEncoding).gossipedProposerSlashingProcessor(operationProcessor);
    NodeManager node2 = createNodeManager(networkBuilder2);
    // Connect networks 1 -> 2
    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);
    // Create and publish slashing
    final ProposerSlashing slashing = dataStructureUtil.randomProposerSlashing();
    node1.network().publishProposerSlashing(slashing);
    // Verify the slashing was gossiped across the network
    Waiter.waitFor(() -> assertThat(receivedGossip).containsExactly(slashing));
}
Also used : Eth2P2PNetworkBuilder(tech.pegasys.teku.networking.eth2.Eth2P2PNetworkFactory.Eth2P2PNetworkBuilder) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) OperationProcessor(tech.pegasys.teku.networking.eth2.gossip.topics.OperationProcessor) Set(java.util.Set) SafeFuture(tech.pegasys.teku.infrastructure.async.SafeFuture) BLSKeyPair(tech.pegasys.teku.bls.BLSKeyPair) HashSet(java.util.HashSet) Consumer(java.util.function.Consumer) Test(org.junit.jupiter.api.Test) List(java.util.List) AfterEach(org.junit.jupiter.api.AfterEach) TestSpecFactory(tech.pegasys.teku.spec.TestSpecFactory) Waiter.waitFor(tech.pegasys.teku.infrastructure.async.Waiter.waitFor) ProposerSlashing(tech.pegasys.teku.spec.datastructures.operations.ProposerSlashing) Waiter(tech.pegasys.teku.infrastructure.async.Waiter) DataStructureUtil(tech.pegasys.teku.spec.util.DataStructureUtil) Spec(tech.pegasys.teku.spec.Spec) BLSKeyGenerator(tech.pegasys.teku.bls.BLSKeyGenerator) InternalValidationResult(tech.pegasys.teku.statetransition.validation.InternalValidationResult) GossipEncoding(tech.pegasys.teku.networking.eth2.gossip.encoding.GossipEncoding) ProposerSlashing(tech.pegasys.teku.spec.datastructures.operations.ProposerSlashing) Eth2P2PNetworkBuilder(tech.pegasys.teku.networking.eth2.Eth2P2PNetworkFactory.Eth2P2PNetworkBuilder) GossipEncoding(tech.pegasys.teku.networking.eth2.gossip.encoding.GossipEncoding) HashSet(java.util.HashSet) Test(org.junit.jupiter.api.Test)

Example 2 with OperationProcessor

use of tech.pegasys.teku.networking.eth2.gossip.topics.OperationProcessor in project teku by ConsenSys.

the class VoluntaryExitGossipIntegrationTest method shouldGossipVoluntaryExitToPeers.

@Test
public void shouldGossipVoluntaryExitToPeers() throws Exception {
    final GossipEncoding gossipEncoding = GossipEncoding.SSZ_SNAPPY;
    final SpecConfig config = spec.getGenesisSpecConfig();
    final UInt64 blockSlot = config.getShardCommitteePeriod().plus(2).times(config.getSlotsPerEpoch());
    // Set up publishers & consumers
    Set<SignedVoluntaryExit> receivedVoluntaryExits = new HashSet<>();
    final OperationProcessor<SignedVoluntaryExit> operationProcessor = (voluntaryExit) -> {
        receivedVoluntaryExits.add(voluntaryExit);
        return SafeFuture.completedFuture(InternalValidationResult.ACCEPT);
    };
    // Setup network 1
    final Consumer<Eth2P2PNetworkBuilder> networkBuilder = b -> b.gossipEncoding(gossipEncoding);
    NodeManager node1 = createNodeManager(networkBuilder);
    node1.chainUtil().setSlot(blockSlot);
    // Setup network 2
    final Consumer<Eth2P2PNetworkBuilder> networkBuilder2 = b -> b.gossipEncoding(gossipEncoding).gossipedVoluntaryExitProcessor(operationProcessor);
    NodeManager node2 = createNodeManager(networkBuilder2);
    node2.chainUtil().setSlot(blockSlot);
    // Connect networks 1 -> 2
    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);
    // Create voluntary exit
    final SignedBeaconBlock block = node1.chainUtil().createAndImportBlockAtSlot(blockSlot);
    final SafeFuture<Optional<BeaconState>> stateFuture = node1.storageClient().getStore().retrieveBlockState(block.getRoot());
    assertThat(stateFuture).isCompleted();
    final BeaconState state = stateFuture.join().orElseThrow();
    final VoluntaryExitGenerator exitGenerator = new VoluntaryExitGenerator(spec, node1.chainUtil().getValidatorKeys());
    final SignedVoluntaryExit voluntaryExit = exitGenerator.valid(state, 0);
    // Publish voluntary exit
    node1.network().publishVoluntaryExit(voluntaryExit);
    // Verify the expected exit was gossiped across the network
    Waiter.waitFor(() -> assertThat(receivedVoluntaryExits).containsExactly(voluntaryExit));
}
Also used : Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) OperationProcessor(tech.pegasys.teku.networking.eth2.gossip.topics.OperationProcessor) SafeFuture(tech.pegasys.teku.infrastructure.async.SafeFuture) SignedVoluntaryExit(tech.pegasys.teku.spec.datastructures.operations.SignedVoluntaryExit) BLSKeyPair(tech.pegasys.teku.bls.BLSKeyPair) HashSet(java.util.HashSet) UInt64(tech.pegasys.teku.infrastructure.unsigned.UInt64) Spec(tech.pegasys.teku.spec.Spec) SignedBeaconBlock(tech.pegasys.teku.spec.datastructures.blocks.SignedBeaconBlock) VoluntaryExitGenerator(tech.pegasys.teku.core.VoluntaryExitGenerator) Eth2P2PNetworkBuilder(tech.pegasys.teku.networking.eth2.Eth2P2PNetworkFactory.Eth2P2PNetworkBuilder) SpecConfig(tech.pegasys.teku.spec.config.SpecConfig) Set(java.util.Set) Consumer(java.util.function.Consumer) Test(org.junit.jupiter.api.Test) List(java.util.List) AfterEach(org.junit.jupiter.api.AfterEach) TestSpecFactory(tech.pegasys.teku.spec.TestSpecFactory) Waiter.waitFor(tech.pegasys.teku.infrastructure.async.Waiter.waitFor) Waiter(tech.pegasys.teku.infrastructure.async.Waiter) Optional(java.util.Optional) BLSKeyGenerator(tech.pegasys.teku.bls.BLSKeyGenerator) InternalValidationResult(tech.pegasys.teku.statetransition.validation.InternalValidationResult) GossipEncoding(tech.pegasys.teku.networking.eth2.gossip.encoding.GossipEncoding) BeaconState(tech.pegasys.teku.spec.datastructures.state.beaconstate.BeaconState) SignedVoluntaryExit(tech.pegasys.teku.spec.datastructures.operations.SignedVoluntaryExit) Optional(java.util.Optional) Eth2P2PNetworkBuilder(tech.pegasys.teku.networking.eth2.Eth2P2PNetworkFactory.Eth2P2PNetworkBuilder) GossipEncoding(tech.pegasys.teku.networking.eth2.gossip.encoding.GossipEncoding) VoluntaryExitGenerator(tech.pegasys.teku.core.VoluntaryExitGenerator) SignedBeaconBlock(tech.pegasys.teku.spec.datastructures.blocks.SignedBeaconBlock) BeaconState(tech.pegasys.teku.spec.datastructures.state.beaconstate.BeaconState) SpecConfig(tech.pegasys.teku.spec.config.SpecConfig) UInt64(tech.pegasys.teku.infrastructure.unsigned.UInt64) HashSet(java.util.HashSet) Test(org.junit.jupiter.api.Test)

Example 3 with OperationProcessor

use of tech.pegasys.teku.networking.eth2.gossip.topics.OperationProcessor in project teku by ConsenSys.

the class AttesterSlashingGossipIntegrationTest method shouldGossipToPeers.

@Test
public void shouldGossipToPeers() throws Exception {
    final GossipEncoding gossipEncoding = GossipEncoding.SSZ_SNAPPY;
    // Set up publishers & consumers
    Set<AttesterSlashing> receivedGossip = new HashSet<>();
    final OperationProcessor<AttesterSlashing> operationProcessor = (slashing) -> {
        receivedGossip.add(slashing);
        return SafeFuture.completedFuture(InternalValidationResult.ACCEPT);
    };
    // Setup network 1
    final Consumer<Eth2P2PNetworkBuilder> networkBuilder = b -> b.gossipEncoding(gossipEncoding);
    NodeManager node1 = createNodeManager(networkBuilder);
    // Setup network 2
    final Consumer<Eth2P2PNetworkBuilder> networkBuilder2 = b -> b.gossipEncoding(gossipEncoding).gossipedAttesterSlashingProcessor(operationProcessor);
    NodeManager node2 = createNodeManager(networkBuilder2);
    // Connect networks 1 -> 2
    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);
    // Create and publish slashing
    final AttesterSlashing slashing = dataStructureUtil.randomAttesterSlashing();
    node1.network().publishAttesterSlashing(slashing);
    // Verify the slashing was gossiped across the network
    Waiter.waitFor(() -> {
        assertThat(receivedGossip).containsExactly(slashing);
    });
}
Also used : Eth2P2PNetworkBuilder(tech.pegasys.teku.networking.eth2.Eth2P2PNetworkFactory.Eth2P2PNetworkBuilder) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) OperationProcessor(tech.pegasys.teku.networking.eth2.gossip.topics.OperationProcessor) Set(java.util.Set) SafeFuture(tech.pegasys.teku.infrastructure.async.SafeFuture) BLSKeyPair(tech.pegasys.teku.bls.BLSKeyPair) AttesterSlashing(tech.pegasys.teku.spec.datastructures.operations.AttesterSlashing) HashSet(java.util.HashSet) Consumer(java.util.function.Consumer) Test(org.junit.jupiter.api.Test) List(java.util.List) AfterEach(org.junit.jupiter.api.AfterEach) Waiter.waitFor(tech.pegasys.teku.infrastructure.async.Waiter.waitFor) Waiter(tech.pegasys.teku.infrastructure.async.Waiter) DataStructureUtil(tech.pegasys.teku.spec.util.DataStructureUtil) BLSKeyGenerator(tech.pegasys.teku.bls.BLSKeyGenerator) InternalValidationResult(tech.pegasys.teku.statetransition.validation.InternalValidationResult) GossipEncoding(tech.pegasys.teku.networking.eth2.gossip.encoding.GossipEncoding) AttesterSlashing(tech.pegasys.teku.spec.datastructures.operations.AttesterSlashing) Eth2P2PNetworkBuilder(tech.pegasys.teku.networking.eth2.Eth2P2PNetworkFactory.Eth2P2PNetworkBuilder) GossipEncoding(tech.pegasys.teku.networking.eth2.gossip.encoding.GossipEncoding) HashSet(java.util.HashSet) Test(org.junit.jupiter.api.Test)

Aggregations

HashSet (java.util.HashSet)3 List (java.util.List)3 Set (java.util.Set)3 Consumer (java.util.function.Consumer)3 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)3 AfterEach (org.junit.jupiter.api.AfterEach)3 Test (org.junit.jupiter.api.Test)3 BLSKeyGenerator (tech.pegasys.teku.bls.BLSKeyGenerator)3 BLSKeyPair (tech.pegasys.teku.bls.BLSKeyPair)3 SafeFuture (tech.pegasys.teku.infrastructure.async.SafeFuture)3 Waiter (tech.pegasys.teku.infrastructure.async.Waiter)3 Waiter.waitFor (tech.pegasys.teku.infrastructure.async.Waiter.waitFor)3 Eth2P2PNetworkBuilder (tech.pegasys.teku.networking.eth2.Eth2P2PNetworkFactory.Eth2P2PNetworkBuilder)3 GossipEncoding (tech.pegasys.teku.networking.eth2.gossip.encoding.GossipEncoding)3 OperationProcessor (tech.pegasys.teku.networking.eth2.gossip.topics.OperationProcessor)3 InternalValidationResult (tech.pegasys.teku.statetransition.validation.InternalValidationResult)3 Spec (tech.pegasys.teku.spec.Spec)2 TestSpecFactory (tech.pegasys.teku.spec.TestSpecFactory)2 DataStructureUtil (tech.pegasys.teku.spec.util.DataStructureUtil)2 Optional (java.util.Optional)1