Search in sources :

Example 6 with LedgerHeader

use of com.radixdlt.hotstuff.LedgerHeader in project radixdlt by radixdlt.

the class StateComputerLedgerTest method should_do_nothing_if_committing_lower_state_version.

@Test
public void should_do_nothing_if_committing_lower_state_version() {
    // Arrange
    genesisIsEndOfEpoch(false);
    when(stateComputer.prepare(any(), any(), anyLong())).thenReturn(new StateComputerResult(ImmutableList.of(successfulNextCommand), ImmutableMap.of()));
    final AccumulatorState accumulatorState = new AccumulatorState(genesisStateVersion - 1, HashUtils.zero256());
    final LedgerHeader ledgerHeader = LedgerHeader.create(genesisEpoch, View.of(2), accumulatorState, 1234);
    final LedgerProof header = new LedgerProof(HashUtils.random256(), ledgerHeader, new TimestampedECDSASignatures());
    var verified = VerifiedTxnsAndProof.create(List.of(nextTxn), header);
    // Act
    sut.syncEventProcessor().process(verified);
    // Assert
    verify(stateComputer, never()).commit(any(), any());
    verify(mempool, never()).committed(any());
}
Also used : TimestampedECDSASignatures(com.radixdlt.hotstuff.TimestampedECDSASignatures) LedgerHeader(com.radixdlt.hotstuff.LedgerHeader) LedgerProof(com.radixdlt.hotstuff.LedgerProof) StateComputerResult(com.radixdlt.ledger.StateComputerLedger.StateComputerResult) Test(org.junit.Test)

Example 7 with LedgerHeader

use of com.radixdlt.hotstuff.LedgerHeader in project radixdlt by radixdlt.

the class GetVerticesErrorResponseMessageSerializeTest method get.

private static GetVerticesErrorResponseMessage get() {
    var accumulatorState = new AccumulatorState(0, HashUtils.zero256());
    LedgerHeader ledgerHeader = LedgerHeaderMock.get();
    VerifiedVertex verifiedVertex = new VerifiedVertex(UnverifiedVertex.createGenesis(ledgerHeader), HashUtils.zero256());
    QuorumCertificate qc = QuorumCertificate.ofGenesis(verifiedVertex, ledgerHeader);
    HighQC highQC = HighQC.from(qc, qc, Optional.empty());
    final var request = new GetVerticesRequestMessage(HashUtils.random256(), 3);
    return new GetVerticesErrorResponseMessage(highQC, request);
}
Also used : VerifiedVertex(com.radixdlt.hotstuff.bft.VerifiedVertex) HighQC(com.radixdlt.hotstuff.HighQC) LedgerHeader(com.radixdlt.hotstuff.LedgerHeader) AccumulatorState(com.radixdlt.ledger.AccumulatorState) QuorumCertificate(com.radixdlt.hotstuff.QuorumCertificate)

Example 8 with LedgerHeader

use of com.radixdlt.hotstuff.LedgerHeader 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.hotstuff.bft.VerifiedVertex) LedgerProof(com.radixdlt.hotstuff.LedgerProof) Inject(com.google.inject.Inject) WeightedRotatingLeaders(com.radixdlt.hotstuff.liveness.WeightedRotatingLeaders) Hasher(com.radixdlt.crypto.Hasher) HighQC(com.radixdlt.hotstuff.HighQC) VerifiedVertexStoreState(com.radixdlt.hotstuff.bft.VerifiedVertexStoreState) MempoolAdd(com.radixdlt.mempool.MempoolAdd) QuorumCertificate(com.radixdlt.hotstuff.QuorumCertificate) LedgerHeader(com.radixdlt.hotstuff.LedgerHeader) View(com.radixdlt.hotstuff.bft.View) BFTConfiguration(com.radixdlt.hotstuff.BFTConfiguration) VerifiedTxnsAndProof(com.radixdlt.ledger.VerifiedTxnsAndProof) Map(java.util.Map) MockPrepared(com.radixdlt.ledger.MockPrepared) Nullable(javax.annotation.Nullable) EventDispatcher(com.radixdlt.environment.EventDispatcher) Txn(com.radixdlt.atom.Txn) VerifiedVertex(com.radixdlt.hotstuff.bft.VerifiedVertex) StateComputer(com.radixdlt.ledger.StateComputerLedger.StateComputer) EpochChange(com.radixdlt.hotstuff.epoch.EpochChange) Collectors(java.util.stream.Collectors) UnverifiedVertex(com.radixdlt.hotstuff.UnverifiedVertex) BFTNode(com.radixdlt.hotstuff.bft.BFTNode) LedgerUpdate(com.radixdlt.ledger.LedgerUpdate) StateComputerLedger(com.radixdlt.ledger.StateComputerLedger) List(java.util.List) Optional(java.util.Optional) ImmutableClassToInstanceMap(com.google.common.collect.ImmutableClassToInstanceMap) LedgerUpdate(com.radixdlt.ledger.LedgerUpdate) LedgerHeader(com.radixdlt.hotstuff.LedgerHeader) EpochChange(com.radixdlt.hotstuff.epoch.EpochChange) BFTConfiguration(com.radixdlt.hotstuff.BFTConfiguration) LedgerProof(com.radixdlt.hotstuff.LedgerProof) QuorumCertificate(com.radixdlt.hotstuff.QuorumCertificate) UnverifiedVertex(com.radixdlt.hotstuff.UnverifiedVertex) WeightedRotatingLeaders(com.radixdlt.hotstuff.liveness.WeightedRotatingLeaders)

Example 9 with LedgerHeader

use of com.radixdlt.hotstuff.LedgerHeader in project radixdlt by radixdlt.

the class StateComputerLedger method prepare.

@Override
public Optional<PreparedVertex> prepare(LinkedList<PreparedVertex> previous, VerifiedVertex vertex) {
    final LedgerHeader parentHeader = vertex.getParentHeader().getLedgerHeader();
    final AccumulatorState parentAccumulatorState = parentHeader.getAccumulatorState();
    final ImmutableList<PreparedTxn> prevCommands = previous.stream().flatMap(PreparedVertex::successfulCommands).collect(ImmutableList.toImmutableList());
    final long quorumTimestamp;
    // one view per epoch but good enough for now
    if (vertex.getParentHeader().getView().isGenesis()) {
        quorumTimestamp = vertex.getParentHeader().getLedgerHeader().timestamp();
    } else {
        quorumTimestamp = vertex.getQC().getTimestampedSignatures().weightedTimestamp();
    }
    synchronized (lock) {
        if (this.currentLedgerHeader.getStateVersion() > parentAccumulatorState.getStateVersion()) {
            return Optional.empty();
        }
        // Don't execute atom if in process of epoch change
        if (parentHeader.isEndOfEpoch()) {
            final long localTimestamp = timeSupplier.currentTime();
            final PreparedVertex preparedVertex = vertex.withHeader(parentHeader.updateViewAndTimestamp(vertex.getView(), quorumTimestamp), localTimestamp).andTxns(ImmutableList.of(), ImmutableMap.of());
            return Optional.of(preparedVertex);
        }
        final var maybeCommands = this.verifier.verifyAndGetExtension(this.currentLedgerHeader.getAccumulatorState(), prevCommands, p -> p.txn().getId().asHashCode(), parentAccumulatorState);
        // Can possibly get here without maliciousness if parent vertex isn't locked by everyone else
        if (maybeCommands.isEmpty()) {
            return Optional.empty();
        }
        final var concatenatedCommands = maybeCommands.get();
        final StateComputerResult result = stateComputer.prepare(concatenatedCommands, vertex, quorumTimestamp);
        AccumulatorState accumulatorState = parentHeader.getAccumulatorState();
        for (PreparedTxn txn : result.getSuccessfulCommands()) {
            accumulatorState = this.accumulator.accumulate(accumulatorState, txn.txn().getId().asHashCode());
        }
        final LedgerHeader ledgerHeader = LedgerHeader.create(parentHeader.getEpoch(), vertex.getView(), accumulatorState, quorumTimestamp, result.getNextValidatorSet().orElse(null));
        final long localTimestamp = timeSupplier.currentTime();
        return Optional.of(vertex.withHeader(ledgerHeader, localTimestamp).andTxns(result.getSuccessfulCommands(), result.getFailedCommands()));
    }
}
Also used : LedgerHeader(com.radixdlt.hotstuff.LedgerHeader) PreparedVertex(com.radixdlt.hotstuff.bft.PreparedVertex)

Example 10 with LedgerHeader

use of com.radixdlt.hotstuff.LedgerHeader 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) BFTHighQCUpdate(com.radixdlt.hotstuff.bft.BFTHighQCUpdate) ArgumentMatchers.argThat(org.mockito.ArgumentMatchers.argThat) Inject(com.google.inject.Inject) PersistentSafetyStateStore(com.radixdlt.hotstuff.safety.PersistentSafetyStateStore) Hasher(com.radixdlt.crypto.Hasher) HighQC(com.radixdlt.hotstuff.HighQC) EpochLocalTimeoutOccurrence(com.radixdlt.hotstuff.liveness.EpochLocalTimeoutOccurrence) VerifiedVertexStoreState(com.radixdlt.hotstuff.bft.VerifiedVertexStoreState) MempoolAdd(com.radixdlt.mempool.MempoolAdd) RemoteEventDispatcher(com.radixdlt.environment.RemoteEventDispatcher) QuorumCertificate(com.radixdlt.hotstuff.QuorumCertificate) LedgerHeader(com.radixdlt.hotstuff.LedgerHeader) EpochsConsensusModule(com.radixdlt.modules.EpochsConsensusModule) VerifiedTxnsAndProof(com.radixdlt.ledger.VerifiedTxnsAndProof) Map(java.util.Map) PersistentVertexStore(com.radixdlt.hotstuff.bft.PersistentVertexStore) PacemakerTimeout(com.radixdlt.hotstuff.bft.PacemakerTimeout) GetVerticesErrorResponse(com.radixdlt.hotstuff.sync.GetVerticesErrorResponse) BFTRebuildUpdate(com.radixdlt.hotstuff.bft.BFTRebuildUpdate) HashUtils(com.radixdlt.crypto.HashUtils) BFTSyncPatienceMillis(com.radixdlt.hotstuff.sync.BFTSyncPatienceMillis) BFTInsertUpdate(com.radixdlt.hotstuff.bft.BFTInsertUpdate) SystemCounters(com.radixdlt.counters.SystemCounters) Vote(com.radixdlt.hotstuff.Vote) ConsensusModule(com.radixdlt.modules.ConsensusModule) NextTxnsGenerator(com.radixdlt.hotstuff.liveness.NextTxnsGenerator) StateComputerResult(com.radixdlt.ledger.StateComputerLedger.StateComputerResult) VerifiedVertex(com.radixdlt.hotstuff.bft.VerifiedVertex) ViewUpdate(com.radixdlt.hotstuff.bft.ViewUpdate) StateComputer(com.radixdlt.ledger.StateComputerLedger.StateComputer) GetVerticesRequestRateLimit(com.radixdlt.middleware2.network.GetVerticesRequestRateLimit) PacemakerRate(com.radixdlt.hotstuff.bft.PacemakerRate) SystemCountersImpl(com.radixdlt.counters.SystemCountersImpl) List(java.util.List) Stream(java.util.stream.Stream) PacemakerMaxExponent(com.radixdlt.hotstuff.bft.PacemakerMaxExponent) LedgerModule(com.radixdlt.modules.LedgerModule) Optional(java.util.Optional) ImmutableClassToInstanceMap(com.google.common.collect.ImmutableClassToInstanceMap) TypeLiteral(com.google.inject.TypeLiteral) Proposal(com.radixdlt.hotstuff.Proposal) ViewQuorumReached(com.radixdlt.hotstuff.bft.ViewQuorumReached) Mockito.mock(org.mockito.Mockito.mock) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) BFTCommittedUpdate(com.radixdlt.hotstuff.bft.BFTCommittedUpdate) CryptoModule(com.radixdlt.modules.CryptoModule) BFTValidatorSet(com.radixdlt.hotstuff.bft.BFTValidatorSet) ScheduledEventDispatcher(com.radixdlt.environment.ScheduledEventDispatcher) LedgerProof(com.radixdlt.hotstuff.LedgerProof) WeightedRotatingLeaders(com.radixdlt.hotstuff.liveness.WeightedRotatingLeaders) GetVerticesResponse(com.radixdlt.hotstuff.sync.GetVerticesResponse) PreparedTxn(com.radixdlt.ledger.StateComputerLedger.PreparedTxn) RateLimiter(com.google.common.util.concurrent.RateLimiter) TypedMocks.rmock(com.radixdlt.utils.TypedMocks.rmock) HashSigner(com.radixdlt.hotstuff.HashSigner) LocalSyncRequest(com.radixdlt.sync.messages.local.LocalSyncRequest) LedgerStatusUpdate(com.radixdlt.sync.messages.remote.LedgerStatusUpdate) View(com.radixdlt.hotstuff.bft.View) LastEpochProof(com.radixdlt.store.LastEpochProof) BFTConfiguration(com.radixdlt.hotstuff.BFTConfiguration) 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) ScheduledLocalTimeout(com.radixdlt.hotstuff.liveness.ScheduledLocalTimeout) NoVote(com.radixdlt.hotstuff.bft.NoVote) Txn(com.radixdlt.atom.Txn) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) Mockito.verify(org.mockito.Mockito.verify) UnverifiedVertex(com.radixdlt.hotstuff.UnverifiedVertex) BFTNode(com.radixdlt.hotstuff.bft.BFTNode) VertexRequestTimeout(com.radixdlt.hotstuff.sync.VertexRequestTimeout) LedgerUpdate(com.radixdlt.ledger.LedgerUpdate) Consumer(java.util.function.Consumer) Mockito.never(org.mockito.Mockito.never) Provides(com.google.inject.Provides) ECKeyPair(com.radixdlt.crypto.ECKeyPair) GetVerticesRequest(com.radixdlt.hotstuff.sync.GetVerticesRequest) BFTValidator(com.radixdlt.hotstuff.bft.BFTValidator) LocalTimeoutOccurrence(com.radixdlt.hotstuff.liveness.LocalTimeoutOccurrence) Guice(com.google.inject.Guice) Self(com.radixdlt.hotstuff.bft.Self) Mempool(com.radixdlt.mempool.Mempool) LastProof(com.radixdlt.store.LastProof) AbstractModule(com.google.inject.AbstractModule) BFTNode(com.radixdlt.hotstuff.bft.BFTNode) AccumulatorState(com.radixdlt.ledger.AccumulatorState) BFTValidatorSet(com.radixdlt.hotstuff.bft.BFTValidatorSet) UnverifiedVertex(com.radixdlt.hotstuff.UnverifiedVertex) VerifiedVertex(com.radixdlt.hotstuff.bft.VerifiedVertex) LedgerHeader(com.radixdlt.hotstuff.LedgerHeader) BFTConfiguration(com.radixdlt.hotstuff.BFTConfiguration) VerifiedTxnsAndProof(com.radixdlt.ledger.VerifiedTxnsAndProof) LedgerProof(com.radixdlt.hotstuff.LedgerProof) WeightedRotatingLeaders(com.radixdlt.hotstuff.liveness.WeightedRotatingLeaders) Test(org.junit.Test)

Aggregations

LedgerHeader (com.radixdlt.hotstuff.LedgerHeader)13 View (com.radixdlt.hotstuff.bft.View)7 QuorumCertificate (com.radixdlt.hotstuff.QuorumCertificate)6 BFTHeader (com.radixdlt.hotstuff.BFTHeader)5 UnverifiedVertex (com.radixdlt.hotstuff.UnverifiedVertex)5 VerifiedVertex (com.radixdlt.hotstuff.bft.VerifiedVertex)5 HighQC (com.radixdlt.hotstuff.HighQC)4 TimestampedECDSASignatures (com.radixdlt.hotstuff.TimestampedECDSASignatures)4 VoteData (com.radixdlt.hotstuff.VoteData)4 BFTNode (com.radixdlt.hotstuff.bft.BFTNode)4 Provides (com.google.inject.Provides)3 BFTConfiguration (com.radixdlt.hotstuff.BFTConfiguration)3 LedgerProof (com.radixdlt.hotstuff.LedgerProof)3 WeightedRotatingLeaders (com.radixdlt.hotstuff.liveness.WeightedRotatingLeaders)3 ImmutableClassToInstanceMap (com.google.common.collect.ImmutableClassToInstanceMap)2 HashCode (com.google.common.hash.HashCode)2 Inject (com.google.inject.Inject)2 Txn (com.radixdlt.atom.Txn)2 Hasher (com.radixdlt.crypto.Hasher)2 EventDispatcher (com.radixdlt.environment.EventDispatcher)2