use of com.radixdlt.crypto.Hasher in project radixdlt by radixdlt.
the class SafetyRulesTest method setup.
@Before
public void setup() {
this.safetyState = mock(SafetyState.class);
Hasher hasher = mock(Hasher.class);
when(hasher.hash(any())).thenReturn(HashUtils.random256());
when(hasher.hashBytes(any())).thenReturn(HashUtils.random256());
HashSigner hashSigner = mock(HashSigner.class);
when(hashSigner.sign(any(HashCode.class))).thenReturn(ECDSASignature.zeroSignature());
final var hashVerifier = mock(HashVerifier.class);
final var validatorSet = mock(BFTValidatorSet.class);
this.safetyRules = new SafetyRules(mock(BFTNode.class), safetyState, mock(PersistentSafetyStateStore.class), hasher, hashSigner, hashVerifier, validatorSet);
}
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));
final var hashVerifier = mock(HashVerifier.class);
final var validatorSet = mock(BFTValidatorSet.class);
final var safetyRules = new SafetyRules(BFTNode.random(), new SafetyState(View.of(2), Optional.of(lastVote)), mock(PersistentSafetyStateStore.class), hasher, hashSigner, hashVerifier, validatorSet);
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();
}
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);
}
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());
}
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;
}
Aggregations