use of com.radixdlt.utils.UInt256 in project radixdlt by radixdlt.
the class ConstructionParseTransferTest method buildUnsignedTransferTxn.
private byte[] buildUnsignedTransferTxn(REAddr from, REAddr to) throws Exception {
final UInt256 toTransfer = transferAmount();
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);
var unsignedTransaction = builder.buildForExternalSign();
return unsignedTransaction.blob();
}
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 WeightedRotatingLeadersTest method when_validators_distributed_by_fibonacci__then_leaders_also_distributed_in_fibonacci.
@Test
public void when_validators_distributed_by_fibonacci__then_leaders_also_distributed_in_fibonacci() {
// fibonacci sequence can quickly explode so keep sizes small
final int validatorSetSize = 8;
final int sizeOfCache = 4;
final Supplier<IntStream> fibonacci = () -> Stream.iterate(new int[] { 1, 1 }, t -> new int[] { t[1], t[0] + t[1] }).mapToInt(t -> t[0]).limit(validatorSetSize);
final int sumOfPower = fibonacci.get().sum();
this.validatorsInOrder = fibonacci.get().mapToObj(p -> BFTValidator.from(BFTNode.random(), UInt256.from(p))).collect(ImmutableList.toImmutableList());
BFTValidatorSet validatorSet = BFTValidatorSet.from(validatorsInOrder);
this.weightedRotatingLeaders = new WeightedRotatingLeaders(validatorSet, sizeOfCache);
Map<BFTNode, UInt256> proposerCounts = Stream.iterate(View.of(0), View::next).limit(sumOfPower).map(this.weightedRotatingLeaders::getProposer).collect(groupingBy(p -> p, collectingAndThen(counting(), UInt256::from)));
Map<BFTNode, UInt256> expected = validatorsInOrder.stream().collect(toMap(BFTValidator::getNode, BFTValidator::getPower));
assertThat(proposerCounts).isEqualTo(expected);
}
use of com.radixdlt.utils.UInt256 in project radixdlt by radixdlt.
the class ProposerLoadBalancedTest method when_run_3_nodes_with_large_lcm_weighting__then_proposals_should_be_proportional.
@Test
public void when_run_3_nodes_with_large_lcm_weighting__then_proposals_should_be_proportional() {
final int numNodes = 3;
final long numProposals = 100_000L;
ImmutableList<UInt256> weights = ImmutableList.of(// Some large primes with product/LCM > 2^64 but < 2^256
UInt256.from("941083981"), UInt256.from("961748927"), UInt256.from("982451653"));
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);
}
}
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;
}
Aggregations