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());
}
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);
}
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);
}
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()));
}
}
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());
}
Aggregations