Search in sources :

Example 1 with Hasher

use of com.radixdlt.crypto.Hasher in project radixdlt by radixdlt.

the class EpochManagerTest method should_not_send_consensus_messages_if_not_part_of_new_epoch.

@Test
public void should_not_send_consensus_messages_if_not_part_of_new_epoch() {
    // Arrange
    epochManager.start();
    BFTValidatorSet nextValidatorSet = BFTValidatorSet.from(Stream.of(BFTValidator.from(BFTNode.random(), UInt256.ONE)));
    var accumulatorState = new AccumulatorState(0, HashUtils.zero256());
    LedgerHeader header = LedgerHeader.genesis(accumulatorState, nextValidatorSet, 0);
    UnverifiedVertex genesisVertex = UnverifiedVertex.createGenesis(header);
    VerifiedVertex verifiedGenesisVertex = new VerifiedVertex(genesisVertex, hasher.hash(genesisVertex));
    LedgerHeader nextLedgerHeader = LedgerHeader.create(header.getEpoch() + 1, View.genesis(), header.getAccumulatorState(), header.timestamp());
    var genesisQC = QuorumCertificate.ofGenesis(verifiedGenesisVertex, nextLedgerHeader);
    var proposerElection = new WeightedRotatingLeaders(nextValidatorSet);
    var bftConfiguration = new BFTConfiguration(proposerElection, nextValidatorSet, VerifiedVertexStoreState.create(HighQC.from(genesisQC), verifiedGenesisVertex, Optional.empty(), hasher));
    LedgerProof proof = mock(LedgerProof.class);
    when(proof.getEpoch()).thenReturn(header.getEpoch() + 1);
    var epochChange = new EpochChange(proof, bftConfiguration);
    var ledgerUpdate = new LedgerUpdate(mock(VerifiedTxnsAndProof.class), ImmutableClassToInstanceMap.of(EpochChange.class, epochChange));
    // Act
    epochManager.epochsLedgerUpdateEventProcessor().process(ledgerUpdate);
    // Assert
    verify(proposalDispatcher, never()).dispatch(any(Iterable.class), argThat(p -> p.getEpoch() == epochChange.getEpoch()));
    verify(voteDispatcher, never()).dispatch(any(BFTNode.class), any());
}
Also used : LedgerUpdate(com.radixdlt.ledger.LedgerUpdate) Module(com.google.inject.Module) ScheduledLocalTimeout(com.radixdlt.consensus.liveness.ScheduledLocalTimeout) GetVerticesRequest(com.radixdlt.consensus.sync.GetVerticesRequest) EpochsConsensusModule(com.radixdlt.EpochsConsensusModule) ArgumentMatchers.argThat(org.mockito.ArgumentMatchers.argThat) Inject(com.google.inject.Inject) PacemakerTimeout(com.radixdlt.consensus.bft.PacemakerTimeout) Hasher(com.radixdlt.crypto.Hasher) HashSigner(com.radixdlt.consensus.HashSigner) BFTCommittedUpdate(com.radixdlt.consensus.bft.BFTCommittedUpdate) MempoolAdd(com.radixdlt.mempool.MempoolAdd) RemoteEventDispatcher(com.radixdlt.environment.RemoteEventDispatcher) VertexRequestTimeout(com.radixdlt.consensus.sync.VertexRequestTimeout) UnverifiedVertex(com.radixdlt.consensus.UnverifiedVertex) VerifiedTxnsAndProof(com.radixdlt.ledger.VerifiedTxnsAndProof) Map(java.util.Map) ViewQuorumReached(com.radixdlt.consensus.bft.ViewQuorumReached) LedgerProof(com.radixdlt.consensus.LedgerProof) View(com.radixdlt.consensus.bft.View) HashUtils(com.radixdlt.crypto.HashUtils) SystemCounters(com.radixdlt.counters.SystemCounters) LedgerModule(com.radixdlt.LedgerModule) WeightedRotatingLeaders(com.radixdlt.consensus.liveness.WeightedRotatingLeaders) PersistentSafetyStateStore(com.radixdlt.consensus.safety.PersistentSafetyStateStore) StateComputerResult(com.radixdlt.ledger.StateComputerLedger.StateComputerResult) StateComputer(com.radixdlt.ledger.StateComputerLedger.StateComputer) GetVerticesRequestRateLimit(com.radixdlt.middleware2.network.GetVerticesRequestRateLimit) BFTRebuildUpdate(com.radixdlt.consensus.bft.BFTRebuildUpdate) LedgerHeader(com.radixdlt.consensus.LedgerHeader) SystemCountersImpl(com.radixdlt.counters.SystemCountersImpl) List(java.util.List) Stream(java.util.stream.Stream) BFTHighQCUpdate(com.radixdlt.consensus.bft.BFTHighQCUpdate) PacemakerMaxExponent(com.radixdlt.consensus.bft.PacemakerMaxExponent) Optional(java.util.Optional) ImmutableClassToInstanceMap(com.google.common.collect.ImmutableClassToInstanceMap) TypeLiteral(com.google.inject.TypeLiteral) BFTSyncPatienceMillis(com.radixdlt.consensus.sync.BFTSyncPatienceMillis) Mockito.mock(org.mockito.Mockito.mock) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) HighQC(com.radixdlt.consensus.HighQC) ScheduledEventDispatcher(com.radixdlt.environment.ScheduledEventDispatcher) Proposal(com.radixdlt.consensus.Proposal) LocalTimeoutOccurrence(com.radixdlt.consensus.liveness.LocalTimeoutOccurrence) BFTValidatorSet(com.radixdlt.consensus.bft.BFTValidatorSet) NextTxnsGenerator(com.radixdlt.consensus.liveness.NextTxnsGenerator) PreparedTxn(com.radixdlt.ledger.StateComputerLedger.PreparedTxn) RateLimiter(com.google.common.util.concurrent.RateLimiter) TypedMocks.rmock(com.radixdlt.utils.TypedMocks.rmock) LocalSyncRequest(com.radixdlt.sync.messages.local.LocalSyncRequest) LedgerStatusUpdate(com.radixdlt.sync.messages.remote.LedgerStatusUpdate) BFTValidator(com.radixdlt.consensus.bft.BFTValidator) CryptoModule(com.radixdlt.CryptoModule) Vote(com.radixdlt.consensus.Vote) LastEpochProof(com.radixdlt.store.LastEpochProof) BFTConfiguration(com.radixdlt.consensus.BFTConfiguration) ViewUpdate(com.radixdlt.consensus.bft.ViewUpdate) AccumulatorState(com.radixdlt.ledger.AccumulatorState) UInt256(com.radixdlt.utils.UInt256) Nullable(javax.annotation.Nullable) Before(org.junit.Before) TimeSupplier(com.radixdlt.utils.TimeSupplier) EventDispatcher(com.radixdlt.environment.EventDispatcher) ConsensusModule(com.radixdlt.ConsensusModule) Txn(com.radixdlt.atom.Txn) BFTInsertUpdate(com.radixdlt.consensus.bft.BFTInsertUpdate) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) VerifiedVertexStoreState(com.radixdlt.consensus.bft.VerifiedVertexStoreState) QuorumCertificate(com.radixdlt.consensus.QuorumCertificate) Mockito.verify(org.mockito.Mockito.verify) LedgerUpdate(com.radixdlt.ledger.LedgerUpdate) Consumer(java.util.function.Consumer) Mockito.never(org.mockito.Mockito.never) Provides(com.google.inject.Provides) VerifiedVertex(com.radixdlt.consensus.bft.VerifiedVertex) ECKeyPair(com.radixdlt.crypto.ECKeyPair) NoVote(com.radixdlt.consensus.bft.NoVote) PacemakerRate(com.radixdlt.consensus.bft.PacemakerRate) Self(com.radixdlt.consensus.bft.Self) Guice(com.google.inject.Guice) BFTNode(com.radixdlt.consensus.bft.BFTNode) PersistentVertexStore(com.radixdlt.consensus.bft.PersistentVertexStore) EpochLocalTimeoutOccurrence(com.radixdlt.consensus.liveness.EpochLocalTimeoutOccurrence) Mempool(com.radixdlt.mempool.Mempool) LastProof(com.radixdlt.store.LastProof) GetVerticesErrorResponse(com.radixdlt.consensus.sync.GetVerticesErrorResponse) GetVerticesResponse(com.radixdlt.consensus.sync.GetVerticesResponse) AbstractModule(com.google.inject.AbstractModule) BFTNode(com.radixdlt.consensus.bft.BFTNode) AccumulatorState(com.radixdlt.ledger.AccumulatorState) BFTValidatorSet(com.radixdlt.consensus.bft.BFTValidatorSet) UnverifiedVertex(com.radixdlt.consensus.UnverifiedVertex) VerifiedVertex(com.radixdlt.consensus.bft.VerifiedVertex) LedgerHeader(com.radixdlt.consensus.LedgerHeader) BFTConfiguration(com.radixdlt.consensus.BFTConfiguration) VerifiedTxnsAndProof(com.radixdlt.ledger.VerifiedTxnsAndProof) LedgerProof(com.radixdlt.consensus.LedgerProof) WeightedRotatingLeaders(com.radixdlt.consensus.liveness.WeightedRotatingLeaders) Test(org.junit.Test)

Example 2 with Hasher

use of com.radixdlt.crypto.Hasher in project radixdlt by radixdlt.

the class SafetyRulesTest method when_vote_with_qc_on_different_locked_view__then_exception_is_thrown.

@Test
public void when_vote_with_qc_on_different_locked_view__then_exception_is_thrown() {
    Hasher hasher = mock(Hasher.class);
    when(hasher.hash(any())).thenReturn(mock(HashCode.class));
    HashSigner hashSigner = mock(HashSigner.class);
    when(hashSigner.sign(any(HashCode.class))).thenReturn(ECDSASignature.zeroSignature());
    Vote lastVote = mock(Vote.class);
    when(lastVote.getView()).thenReturn(View.of(1));
    SafetyRules safetyRules = new SafetyRules(BFTNode.random(), new SafetyState(View.of(2), Optional.of(lastVote)), mock(PersistentSafetyStateStore.class), hasher, hashSigner);
    VerifiedVertex vertex = mock(VerifiedVertex.class);
    when(vertex.getView()).thenReturn(View.of(3));
    BFTHeader parent = mock(BFTHeader.class);
    when(parent.getView()).thenReturn(View.of(0));
    when(vertex.getParentHeader()).thenReturn(parent);
    assertThat(safetyRules.voteFor(vertex, mock(BFTHeader.class), 0L, mock(HighQC.class))).isEmpty();
}
Also used : VerifiedVertex(com.radixdlt.consensus.bft.VerifiedVertex) HighQC(com.radixdlt.consensus.HighQC) BFTHeader(com.radixdlt.consensus.BFTHeader) Hasher(com.radixdlt.crypto.Hasher) Vote(com.radixdlt.consensus.Vote) HashCode(com.google.common.hash.HashCode) HashSigner(com.radixdlt.consensus.HashSigner) Test(org.junit.Test)

Example 3 with Hasher

use of com.radixdlt.crypto.Hasher in project radixdlt by radixdlt.

the class MessageCentralValidatorSync method responses.

public Flowable<RemoteEvent<GetVerticesResponse>> responses() {
    return this.createFlowable(GetVerticesResponseMessage.class, (src, msg) -> {
        BFTNode node = BFTNode.create(src.getPublicKey());
        // TODO: Move hasher to a more appropriate place
        ImmutableList<VerifiedVertex> hashedVertices = msg.getVertices().stream().map(v -> new VerifiedVertex(v, hasher.hash(v))).collect(ImmutableList.toImmutableList());
        return RemoteEvent.create(node, new GetVerticesResponse(hashedVertices));
    });
}
Also used : VerifiedVertex(com.radixdlt.consensus.bft.VerifiedVertex) GetVerticesRequest(com.radixdlt.consensus.sync.GetVerticesRequest) Flowable(io.reactivex.rxjava3.core.Flowable) Message(org.radix.network.messaging.Message) Inject(com.google.inject.Inject) BiFunction(java.util.function.BiFunction) Hasher(com.radixdlt.crypto.Hasher) RemoteEvent(com.radixdlt.environment.rx.RemoteEvent) Objects(java.util.Objects) RemoteEventDispatcher(com.radixdlt.environment.RemoteEventDispatcher) VerifiedVertex(com.radixdlt.consensus.bft.VerifiedVertex) ImmutableList(com.google.common.collect.ImmutableList) NodeId(com.radixdlt.network.p2p.NodeId) BackpressureStrategy(io.reactivex.rxjava3.core.BackpressureStrategy) BFTNode(com.radixdlt.consensus.bft.BFTNode) MessageCentral(com.radixdlt.network.messaging.MessageCentral) GetVerticesErrorResponse(com.radixdlt.consensus.sync.GetVerticesErrorResponse) GetVerticesResponse(com.radixdlt.consensus.sync.GetVerticesResponse) BFTNode(com.radixdlt.consensus.bft.BFTNode) GetVerticesResponse(com.radixdlt.consensus.sync.GetVerticesResponse)

Example 4 with Hasher

use of com.radixdlt.crypto.Hasher in project radixdlt by radixdlt.

the class MockedCryptoModule method hasher.

@Provides
private Hasher hasher(Serialization serialization, SystemCounters counters) {
    AtomicBoolean running = new AtomicBoolean(false);
    Hasher hasher = new Hasher() {

        @Override
        public int bytes() {
            return 32;
        }

        @Override
        public HashCode hash(Object o) {
            byte[] dson = timeWhinge("Serialization", () -> serialization.toDson(o, Output.HASH));
            return this.hashBytes(dson);
        }

        @Override
        public HashCode hashBytes(byte[] bytes) {
            byte[] hashCode = timeWhinge("Hashing", () -> hashFunction.hashBytes(bytes).asBytes());
            return HashCode.fromBytes(hashCode);
        }

        private <T> T timeWhinge(String what, Supplier<T> exec) {
            long start = System.nanoTime();
            T result = exec.get();
            long end = System.nanoTime();
            long durationMs = (end - start) / 1_000_000L;
            if (durationMs > 50) {
                log.warn("{} took {}ms", what, durationMs);
            }
            return result;
        }
    };
    // Make sure classes etc loaded, as first use seems to take some time
    // Arbitrary serializable class
    Object dummyObject = ECDSASignature.zeroSignature();
    hasher.hash(dummyObject);
    running.set(true);
    return hasher;
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Hasher(com.radixdlt.crypto.Hasher) Supplier(java.util.function.Supplier) Provides(com.google.inject.Provides)

Example 5 with Hasher

use of com.radixdlt.crypto.Hasher in project radixdlt by radixdlt.

the class MockedStateComputer method commit.

@Override
public void commit(VerifiedTxnsAndProof txnsAndProof, VerifiedVertexStoreState vertexStoreState) {
    var output = txnsAndProof.getProof().getNextValidatorSet().map(validatorSet -> {
        LedgerProof header = txnsAndProof.getProof();
        UnverifiedVertex genesisVertex = UnverifiedVertex.createGenesis(header.getRaw());
        VerifiedVertex verifiedGenesisVertex = new VerifiedVertex(genesisVertex, hasher.hash(genesisVertex));
        LedgerHeader nextLedgerHeader = LedgerHeader.create(header.getEpoch() + 1, View.genesis(), header.getAccumulatorState(), header.timestamp());
        QuorumCertificate genesisQC = QuorumCertificate.ofGenesis(verifiedGenesisVertex, nextLedgerHeader);
        final var initialState = VerifiedVertexStoreState.create(HighQC.from(genesisQC), verifiedGenesisVertex, Optional.empty(), hasher);
        var proposerElection = new WeightedRotatingLeaders(validatorSet);
        var bftConfiguration = new BFTConfiguration(proposerElection, validatorSet, initialState);
        return new EpochChange(header, bftConfiguration);
    }).map(e -> ImmutableClassToInstanceMap.<Object, EpochChange>of(EpochChange.class, e)).orElse(ImmutableClassToInstanceMap.of());
    var ledgerUpdate = new LedgerUpdate(txnsAndProof, output);
    ledgerUpdateDispatcher.dispatch(ledgerUpdate);
}
Also used : VerifiedVertex(com.radixdlt.consensus.bft.VerifiedVertex) HighQC(com.radixdlt.consensus.HighQC) Inject(com.google.inject.Inject) Hasher(com.radixdlt.crypto.Hasher) MempoolAdd(com.radixdlt.mempool.MempoolAdd) UnverifiedVertex(com.radixdlt.consensus.UnverifiedVertex) VerifiedTxnsAndProof(com.radixdlt.ledger.VerifiedTxnsAndProof) Map(java.util.Map) BFTConfiguration(com.radixdlt.consensus.BFTConfiguration) MockPrepared(com.radixdlt.ledger.MockPrepared) LedgerProof(com.radixdlt.consensus.LedgerProof) View(com.radixdlt.consensus.bft.View) Nullable(javax.annotation.Nullable) EventDispatcher(com.radixdlt.environment.EventDispatcher) WeightedRotatingLeaders(com.radixdlt.consensus.liveness.WeightedRotatingLeaders) Txn(com.radixdlt.atom.Txn) StateComputer(com.radixdlt.ledger.StateComputerLedger.StateComputer) VerifiedVertexStoreState(com.radixdlt.consensus.bft.VerifiedVertexStoreState) QuorumCertificate(com.radixdlt.consensus.QuorumCertificate) Collectors(java.util.stream.Collectors) EpochChange(com.radixdlt.consensus.epoch.EpochChange) LedgerUpdate(com.radixdlt.ledger.LedgerUpdate) LedgerHeader(com.radixdlt.consensus.LedgerHeader) StateComputerLedger(com.radixdlt.ledger.StateComputerLedger) List(java.util.List) VerifiedVertex(com.radixdlt.consensus.bft.VerifiedVertex) BFTNode(com.radixdlt.consensus.bft.BFTNode) Optional(java.util.Optional) ImmutableClassToInstanceMap(com.google.common.collect.ImmutableClassToInstanceMap) LedgerUpdate(com.radixdlt.ledger.LedgerUpdate) LedgerHeader(com.radixdlt.consensus.LedgerHeader) EpochChange(com.radixdlt.consensus.epoch.EpochChange) BFTConfiguration(com.radixdlt.consensus.BFTConfiguration) LedgerProof(com.radixdlt.consensus.LedgerProof) QuorumCertificate(com.radixdlt.consensus.QuorumCertificate) UnverifiedVertex(com.radixdlt.consensus.UnverifiedVertex) WeightedRotatingLeaders(com.radixdlt.consensus.liveness.WeightedRotatingLeaders)

Aggregations

Hasher (com.radixdlt.crypto.Hasher)7 VerifiedVertex (com.radixdlt.consensus.bft.VerifiedVertex)5 HashSigner (com.radixdlt.consensus.HashSigner)4 HighQC (com.radixdlt.consensus.HighQC)4 BFTNode (com.radixdlt.consensus.bft.BFTNode)4 Inject (com.google.inject.Inject)3 BFTConfiguration (com.radixdlt.consensus.BFTConfiguration)3 Vote (com.radixdlt.consensus.Vote)3 ImmutableClassToInstanceMap (com.google.common.collect.ImmutableClassToInstanceMap)2 HashCode (com.google.common.hash.HashCode)2 AbstractModule (com.google.inject.AbstractModule)2 Provides (com.google.inject.Provides)2 TypeLiteral (com.google.inject.TypeLiteral)2 Txn (com.radixdlt.atom.Txn)2 LedgerHeader (com.radixdlt.consensus.LedgerHeader)2 LedgerProof (com.radixdlt.consensus.LedgerProof)2 Proposal (com.radixdlt.consensus.Proposal)2 QuorumCertificate (com.radixdlt.consensus.QuorumCertificate)2 UnverifiedVertex (com.radixdlt.consensus.UnverifiedVertex)2 BFTCommittedUpdate (com.radixdlt.consensus.bft.BFTCommittedUpdate)2