use of com.radixdlt.utils.UInt256 in project radixdlt by radixdlt.
the class ValidationState method addSignature.
/**
* Adds key and signature to our list of signing keys and signatures. Note that it is assumed that
* signature validation is performed elsewhere.
*
* @param node The node
* @param timestamp The timestamp of the signature
* @param signature The signature to verify
* @return whether the key was added or not
*/
public boolean addSignature(BFTNode node, long timestamp, ECDSASignature signature) {
if (validatorSet.containsNode(node) && !this.signedNodes.containsKey(node)) {
this.signedNodes.computeIfAbsent(node, k -> {
UInt256 weight = this.validatorSet.getPower(node);
this.signedPower = this.signedPower.add(weight);
return TimestampedECDSASignature.from(timestamp, signature);
});
return true;
}
return false;
}
use of com.radixdlt.utils.UInt256 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.utils.UInt256 in project radixdlt by radixdlt.
the class TokensConstraintScryptV3 method defineTokenCreation.
private void defineTokenCreation(Loader os) {
os.procedure(new UpProcedure<>(SystemConstraintScrypt.REAddrClaim.class, TokenResource.class, u -> new Authorization(PermissionLevel.USER, (r, c) -> {
}), (s, u, c, r) -> {
if (!u.addr().equals(s.getAddr())) {
throw new ProcedureException("Addresses don't match");
}
var str = new String(s.getArg());
if (reservedSymbols.contains(str) && c.permissionLevel() != PermissionLevel.SYSTEM) {
throw new ReservedSymbolException(str);
}
if (!tokenSymbolPattern.matcher(str).matches()) {
throw new ProcedureException("invalid token symbol: " + str);
}
if (u.isMutable()) {
return ReducerResult.incomplete(new NeedMetadata(s.getArg(), u));
}
if (!u.granularity().equals(UInt256.ONE)) {
throw new ProcedureException("Granularity must be one.");
}
return ReducerResult.incomplete(new NeedFixedTokenSupply(s.getArg(), u));
}));
os.procedure(new UpProcedure<>(NeedFixedTokenSupply.class, TokensInAccount.class, u -> new Authorization(PermissionLevel.USER, (r, c) -> {
}), (s, u, c, r) -> {
if (!u.resourceAddr().equals(s.tokenResource.addr())) {
throw new ProcedureException("Addresses don't match.");
}
return ReducerResult.incomplete(new NeedMetadata(s.arg, s.tokenResource));
}));
os.procedure(new UpProcedure<>(NeedMetadata.class, TokenResourceMetadata.class, u -> new Authorization(PermissionLevel.USER, (r, c) -> {
}), (s, u, c, r) -> {
s.metadata(u, c);
return ReducerResult.complete();
}));
}
use of com.radixdlt.utils.UInt256 in project radixdlt by radixdlt.
the class MempoolHandlerTest method buildSignedTxn.
private Txn buildSignedTxn(REAddr from, REAddr to) throws Exception {
final UInt256 toTransfer = getLiquidAmount().toSubunits().subtract(Amount.ofTokens(1).toSubunits());
var entityOperationGroups = List.of(List.of(EntityOperation.from(new AccountVaultEntity(from), ResourceOperation.withdraw(new TokenResource("xrd", REAddr.ofNativeToken()), toTransfer)), EntityOperation.from(new AccountVaultEntity(to), ResourceOperation.deposit(new TokenResource("xrd", REAddr.ofNativeToken()), toTransfer))));
var operationTxBuilder = new OperationTxBuilder(null, entityOperationGroups, currentForkView);
var builder = radixEngine.constructWithFees(operationTxBuilder, false, from, NotEnoughNativeTokensForFeesException::new);
return builder.signAndBuild(hashSigner::sign);
}
use of com.radixdlt.utils.UInt256 in project radixdlt by radixdlt.
the class ProposerLoadBalancedTest method when_run_100_nodes_with_very_large_period__then_proposals_should_be_proportional.
@Test
public void when_run_100_nodes_with_very_large_period__then_proposals_should_be_proportional() {
final int numNodes = 100;
final long numProposals = 1_000L;
ImmutableList<UInt256> weights = generatePrimes(100).mapToObj(UInt256::from).collect(ImmutableList.toImmutableList());
UInt256 sum = weights.stream().reduce(UInt256.ZERO, UInt256::add);
UInt256 numViews256 = UInt256.from(numProposals);
long[] values = weights.stream().map(w -> w.multiply(numViews256).divide(sum)).mapToLong(v -> v.getLow().getLow()).toArray();
ImmutableList<Long> proposals = this.run(numNodes, numProposals, EpochNodeWeightMapping.computed(numNodes, weights::get));
// Correct number of total proposals
assertThat(proposals.stream().mapToLong(Long::longValue).sum()).isEqualTo(numProposals);
// Same as calculated value, +/- 1 (rounding and ordering)
for (int i = 0; i < values.length; ++i) {
assertThat(proposals.get(i).longValue()).isBetween(values[i] - 1, values[i] + 1);
}
}
Aggregations