Search in sources :

Example 1 with ProcessedAttestationListener

use of tech.pegasys.teku.spec.datastructures.attestation.ProcessedAttestationListener in project teku by ConsenSys.

the class GossipMessageHandlerIntegrationTest method shouldNotGossipAttestationsWhenPeerDeregistersFromTopic.

@Test
public void shouldNotGossipAttestationsWhenPeerDeregistersFromTopic() throws Exception {
    final GossipEncoding gossipEncoding = GossipEncoding.SSZ_SNAPPY;
    List<ValidateableAttestation> node2attestations = new ArrayList<>();
    Subscribers<ProcessedAttestationListener> processedAttestationSubscribers = Subscribers.create(false);
    final Consumer<Eth2P2PNetworkBuilder> networkBuilder1 = b -> {
        b.gossipEncoding(gossipEncoding);
        b.gossipedAttestationProcessor((__) -> SafeFuture.completedFuture(InternalValidationResult.ACCEPT));
        b.processedAttestationSubscriptionProvider(processedAttestationSubscribers::subscribe);
    };
    final Consumer<Eth2P2PNetworkBuilder> networkBuilder2 = b -> {
        b.gossipEncoding(gossipEncoding);
        b.gossipedAttestationProcessor((attestation) -> {
            node2attestations.add(attestation);
            return SafeFuture.completedFuture(InternalValidationResult.ACCEPT);
        });
    };
    // Setup network 1
    final NodeManager node1 = createNodeManager(networkBuilder1);
    final Eth2P2PNetwork network1 = node1.network();
    // Setup network 2
    final NodeManager node2 = createNodeManager(networkBuilder2);
    final Eth2P2PNetwork network2 = node2.network();
    // Connect networks 1 -> 2
    waitFor(node1.connect(node2));
    // Wait for connections to get set up
    Waiter.waitFor(() -> {
        assertThat(network1.getPeerCount()).isEqualTo(1);
        assertThat(network2.getPeerCount()).isEqualTo(1);
    });
    // Propagate attestation from network 1
    AttestationGenerator attestationGenerator = new AttestationGenerator(spec, validatorKeys);
    final StateAndBlockSummary bestBlockAndState = getChainHead(node1);
    Attestation attestation = attestationGenerator.validAttestation(bestBlockAndState);
    final int subnetId = spec.computeSubnetForAttestation(bestBlockAndState.getState(), attestation);
    ValidateableAttestation validAttestation = ValidateableAttestation.fromNetwork(spec, attestation, subnetId);
    node1.network().subscribeToAttestationSubnetId(subnetId);
    node2.network().subscribeToAttestationSubnetId(subnetId);
    waitForTopicRegistration();
    processedAttestationSubscribers.forEach(s -> s.accept(validAttestation));
    waitForMessageToBeDelivered();
    assertThat(node2attestations.size()).isEqualTo(1);
    assertThat(node2attestations.get(0)).isEqualTo(validAttestation);
    node1.network().unsubscribeFromAttestationSubnetId(subnetId);
    node2.network().unsubscribeFromAttestationSubnetId(subnetId);
    waitForTopicDeregistration();
    processedAttestationSubscribers.forEach(s -> s.accept(validAttestation));
    ensureConditionRemainsMet(() -> {
        assertThat(node2attestations.size()).isEqualTo(1);
        assertThat(node2attestations.get(0)).isEqualTo(validAttestation);
    });
}
Also used : Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) SafeFuture(tech.pegasys.teku.infrastructure.async.SafeFuture) BLSKeyPair(tech.pegasys.teku.bls.BLSKeyPair) Subscribers(tech.pegasys.teku.infrastructure.subscribers.Subscribers) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) StateAndBlockSummary(tech.pegasys.teku.spec.datastructures.blocks.StateAndBlockSummary) ProcessedAttestationListener(tech.pegasys.teku.spec.datastructures.attestation.ProcessedAttestationListener) AttestationGenerator(tech.pegasys.teku.core.AttestationGenerator) UInt64(tech.pegasys.teku.infrastructure.unsigned.UInt64) Spec(tech.pegasys.teku.spec.Spec) SignedBeaconBlock(tech.pegasys.teku.spec.datastructures.blocks.SignedBeaconBlock) Attestation(tech.pegasys.teku.spec.datastructures.operations.Attestation) Waiter.ensureConditionRemainsMet(tech.pegasys.teku.infrastructure.async.Waiter.ensureConditionRemainsMet) Eth2P2PNetworkBuilder(tech.pegasys.teku.networking.eth2.Eth2P2PNetworkFactory.Eth2P2PNetworkBuilder) 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) ValidateableAttestation(tech.pegasys.teku.spec.datastructures.attestation.ValidateableAttestation) BLSKeyGenerator(tech.pegasys.teku.bls.BLSKeyGenerator) InternalValidationResult(tech.pegasys.teku.statetransition.validation.InternalValidationResult) SafeFutureAssert.safeJoin(tech.pegasys.teku.infrastructure.async.SafeFutureAssert.safeJoin) GossipEncoding(tech.pegasys.teku.networking.eth2.gossip.encoding.GossipEncoding) StateAndBlockSummary(tech.pegasys.teku.spec.datastructures.blocks.StateAndBlockSummary) AttestationGenerator(tech.pegasys.teku.core.AttestationGenerator) Eth2P2PNetworkBuilder(tech.pegasys.teku.networking.eth2.Eth2P2PNetworkFactory.Eth2P2PNetworkBuilder) GossipEncoding(tech.pegasys.teku.networking.eth2.gossip.encoding.GossipEncoding) ArrayList(java.util.ArrayList) Attestation(tech.pegasys.teku.spec.datastructures.operations.Attestation) ValidateableAttestation(tech.pegasys.teku.spec.datastructures.attestation.ValidateableAttestation) ProcessedAttestationListener(tech.pegasys.teku.spec.datastructures.attestation.ProcessedAttestationListener) ValidateableAttestation(tech.pegasys.teku.spec.datastructures.attestation.ValidateableAttestation) Test(org.junit.jupiter.api.Test)

Example 2 with ProcessedAttestationListener

use of tech.pegasys.teku.spec.datastructures.attestation.ProcessedAttestationListener in project teku by ConsenSys.

the class GossipMessageHandlerIntegrationTest method shouldNotGossipAttestationsAcrossPeersThatAreNotOnTheSameSubnet.

@Test
public void shouldNotGossipAttestationsAcrossPeersThatAreNotOnTheSameSubnet() throws Exception {
    final GossipEncoding gossipEncoding = GossipEncoding.SSZ_SNAPPY;
    List<ValidateableAttestation> node2attestations = new ArrayList<>();
    Subscribers<ProcessedAttestationListener> processedAttestationSubscribers = Subscribers.create(false);
    final Consumer<Eth2P2PNetworkBuilder> networkBuilder1 = b -> {
        b.gossipEncoding(gossipEncoding);
        b.processedAttestationSubscriptionProvider(processedAttestationSubscribers::subscribe);
    };
    final Consumer<Eth2P2PNetworkBuilder> networkBuilder2 = b -> {
        b.gossipEncoding(gossipEncoding);
        b.gossipedAttestationProcessor((attestation) -> {
            node2attestations.add(attestation);
            return SafeFuture.completedFuture(InternalValidationResult.ACCEPT);
        });
    };
    // Setup network 1
    final NodeManager node1 = createNodeManager(networkBuilder1);
    final Eth2P2PNetwork network1 = node1.network();
    // Setup network 2
    final NodeManager node2 = createNodeManager(networkBuilder2);
    final Eth2P2PNetwork network2 = node2.network();
    // Connect networks 1 -> 2
    waitFor(node1.connect(node2));
    // Wait for connections to get set up
    Waiter.waitFor(() -> {
        assertThat(network1.getPeerCount()).isEqualTo(1);
        assertThat(network2.getPeerCount()).isEqualTo(1);
    });
    // Propagate attestation from network 1
    AttestationGenerator attestationGenerator = new AttestationGenerator(spec, validatorKeys);
    final StateAndBlockSummary bestBlockAndState = getChainHead(node1);
    Attestation validAttestation = attestationGenerator.validAttestation(bestBlockAndState);
    processedAttestationSubscribers.forEach(s -> s.accept(ValidateableAttestation.from(spec, validAttestation)));
    ensureConditionRemainsMet(() -> assertThat(node2attestations).isEmpty());
}
Also used : Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) SafeFuture(tech.pegasys.teku.infrastructure.async.SafeFuture) BLSKeyPair(tech.pegasys.teku.bls.BLSKeyPair) Subscribers(tech.pegasys.teku.infrastructure.subscribers.Subscribers) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) StateAndBlockSummary(tech.pegasys.teku.spec.datastructures.blocks.StateAndBlockSummary) ProcessedAttestationListener(tech.pegasys.teku.spec.datastructures.attestation.ProcessedAttestationListener) AttestationGenerator(tech.pegasys.teku.core.AttestationGenerator) UInt64(tech.pegasys.teku.infrastructure.unsigned.UInt64) Spec(tech.pegasys.teku.spec.Spec) SignedBeaconBlock(tech.pegasys.teku.spec.datastructures.blocks.SignedBeaconBlock) Attestation(tech.pegasys.teku.spec.datastructures.operations.Attestation) Waiter.ensureConditionRemainsMet(tech.pegasys.teku.infrastructure.async.Waiter.ensureConditionRemainsMet) Eth2P2PNetworkBuilder(tech.pegasys.teku.networking.eth2.Eth2P2PNetworkFactory.Eth2P2PNetworkBuilder) 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) ValidateableAttestation(tech.pegasys.teku.spec.datastructures.attestation.ValidateableAttestation) BLSKeyGenerator(tech.pegasys.teku.bls.BLSKeyGenerator) InternalValidationResult(tech.pegasys.teku.statetransition.validation.InternalValidationResult) SafeFutureAssert.safeJoin(tech.pegasys.teku.infrastructure.async.SafeFutureAssert.safeJoin) GossipEncoding(tech.pegasys.teku.networking.eth2.gossip.encoding.GossipEncoding) StateAndBlockSummary(tech.pegasys.teku.spec.datastructures.blocks.StateAndBlockSummary) AttestationGenerator(tech.pegasys.teku.core.AttestationGenerator) Eth2P2PNetworkBuilder(tech.pegasys.teku.networking.eth2.Eth2P2PNetworkFactory.Eth2P2PNetworkBuilder) GossipEncoding(tech.pegasys.teku.networking.eth2.gossip.encoding.GossipEncoding) ArrayList(java.util.ArrayList) Attestation(tech.pegasys.teku.spec.datastructures.operations.Attestation) ValidateableAttestation(tech.pegasys.teku.spec.datastructures.attestation.ValidateableAttestation) ProcessedAttestationListener(tech.pegasys.teku.spec.datastructures.attestation.ProcessedAttestationListener) ValidateableAttestation(tech.pegasys.teku.spec.datastructures.attestation.ValidateableAttestation) Test(org.junit.jupiter.api.Test)

Example 3 with ProcessedAttestationListener

use of tech.pegasys.teku.spec.datastructures.attestation.ProcessedAttestationListener in project teku by ConsenSys.

the class GossipMessageHandlerIntegrationTest method shouldGossipAttestationsAcrossPeersThatAreOnTheSameSubnet.

@Test
public void shouldGossipAttestationsAcrossPeersThatAreOnTheSameSubnet() throws Exception {
    final GossipEncoding gossipEncoding = GossipEncoding.SSZ_SNAPPY;
    List<ValidateableAttestation> node2attestations = new ArrayList<>();
    Subscribers<ProcessedAttestationListener> processedAttestationSubscribers = Subscribers.create(false);
    final Consumer<Eth2P2PNetworkBuilder> networkBuilder1 = b -> {
        b.gossipEncoding(gossipEncoding);
        b.processedAttestationSubscriptionProvider(processedAttestationSubscribers::subscribe);
    };
    final Consumer<Eth2P2PNetworkBuilder> networkBuilder2 = b -> {
        b.gossipEncoding(gossipEncoding);
        b.gossipedAttestationProcessor((attestation) -> {
            node2attestations.add(attestation);
            return SafeFuture.completedFuture(InternalValidationResult.ACCEPT);
        });
    };
    // Setup network 1
    final NodeManager node1 = createNodeManager(networkBuilder1);
    final Eth2P2PNetwork network1 = node1.network();
    // Setup network 2
    final NodeManager node2 = createNodeManager(networkBuilder2);
    final Eth2P2PNetwork network2 = node2.network();
    // Connect networks 1 -> 2
    waitFor(node1.connect(node2));
    // Wait for connections to get set up
    Waiter.waitFor(() -> {
        assertThat(network1.getPeerCount()).isEqualTo(1);
        assertThat(network2.getPeerCount()).isEqualTo(1);
    });
    // Propagate attestation from network 1
    AttestationGenerator attestationGenerator = new AttestationGenerator(spec, validatorKeys);
    final StateAndBlockSummary bestBlockAndState = getChainHead(node1);
    ValidateableAttestation validAttestation = ValidateableAttestation.from(spec, attestationGenerator.validAttestation(bestBlockAndState));
    final int subnetId = spec.computeSubnetForAttestation(bestBlockAndState.getState(), validAttestation.getAttestation());
    node1.network().subscribeToAttestationSubnetId(subnetId);
    node2.network().subscribeToAttestationSubnetId(subnetId);
    waitForTopicRegistration();
    processedAttestationSubscribers.forEach(s -> s.accept(validAttestation));
    Waiter.waitFor(() -> {
        assertThat(node2attestations.size()).isEqualTo(1);
        assertThat(node2attestations.get(0)).isEqualTo(validAttestation);
    });
}
Also used : Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) SafeFuture(tech.pegasys.teku.infrastructure.async.SafeFuture) BLSKeyPair(tech.pegasys.teku.bls.BLSKeyPair) Subscribers(tech.pegasys.teku.infrastructure.subscribers.Subscribers) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) StateAndBlockSummary(tech.pegasys.teku.spec.datastructures.blocks.StateAndBlockSummary) ProcessedAttestationListener(tech.pegasys.teku.spec.datastructures.attestation.ProcessedAttestationListener) AttestationGenerator(tech.pegasys.teku.core.AttestationGenerator) UInt64(tech.pegasys.teku.infrastructure.unsigned.UInt64) Spec(tech.pegasys.teku.spec.Spec) SignedBeaconBlock(tech.pegasys.teku.spec.datastructures.blocks.SignedBeaconBlock) Attestation(tech.pegasys.teku.spec.datastructures.operations.Attestation) Waiter.ensureConditionRemainsMet(tech.pegasys.teku.infrastructure.async.Waiter.ensureConditionRemainsMet) Eth2P2PNetworkBuilder(tech.pegasys.teku.networking.eth2.Eth2P2PNetworkFactory.Eth2P2PNetworkBuilder) 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) ValidateableAttestation(tech.pegasys.teku.spec.datastructures.attestation.ValidateableAttestation) BLSKeyGenerator(tech.pegasys.teku.bls.BLSKeyGenerator) InternalValidationResult(tech.pegasys.teku.statetransition.validation.InternalValidationResult) SafeFutureAssert.safeJoin(tech.pegasys.teku.infrastructure.async.SafeFutureAssert.safeJoin) GossipEncoding(tech.pegasys.teku.networking.eth2.gossip.encoding.GossipEncoding) StateAndBlockSummary(tech.pegasys.teku.spec.datastructures.blocks.StateAndBlockSummary) AttestationGenerator(tech.pegasys.teku.core.AttestationGenerator) Eth2P2PNetworkBuilder(tech.pegasys.teku.networking.eth2.Eth2P2PNetworkFactory.Eth2P2PNetworkBuilder) GossipEncoding(tech.pegasys.teku.networking.eth2.gossip.encoding.GossipEncoding) ArrayList(java.util.ArrayList) ProcessedAttestationListener(tech.pegasys.teku.spec.datastructures.attestation.ProcessedAttestationListener) ValidateableAttestation(tech.pegasys.teku.spec.datastructures.attestation.ValidateableAttestation) Test(org.junit.jupiter.api.Test)

Aggregations

ArrayList (java.util.ArrayList)3 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 AttestationGenerator (tech.pegasys.teku.core.AttestationGenerator)3 SafeFuture (tech.pegasys.teku.infrastructure.async.SafeFuture)3 SafeFutureAssert.safeJoin (tech.pegasys.teku.infrastructure.async.SafeFutureAssert.safeJoin)3 Waiter (tech.pegasys.teku.infrastructure.async.Waiter)3 Waiter.ensureConditionRemainsMet (tech.pegasys.teku.infrastructure.async.Waiter.ensureConditionRemainsMet)3 Waiter.waitFor (tech.pegasys.teku.infrastructure.async.Waiter.waitFor)3 Subscribers (tech.pegasys.teku.infrastructure.subscribers.Subscribers)3 UInt64 (tech.pegasys.teku.infrastructure.unsigned.UInt64)3 Eth2P2PNetworkBuilder (tech.pegasys.teku.networking.eth2.Eth2P2PNetworkFactory.Eth2P2PNetworkBuilder)3 GossipEncoding (tech.pegasys.teku.networking.eth2.gossip.encoding.GossipEncoding)3