use of com.radixdlt.utils.UInt256 in project radixdlt by radixdlt.
the class WeightedRotatingLeadersTest method setUp.
private void setUp(int validatorSetSize, int sizeOfCache) {
this.validatorsInOrder = Stream.generate(() -> ECKeyPair.generateNew().getPublicKey()).limit(validatorSetSize).map(BFTNode::create).map(node -> BFTValidator.from(node, UInt256.ONE)).sorted(Comparator.comparing(v -> v.getNode().getKey(), KeyComparator.instance().reversed())).collect(ImmutableList.toImmutableList());
BFTValidatorSet validatorSet = BFTValidatorSet.from(validatorsInOrder);
this.weightedRotatingLeaders = new WeightedRotatingLeaders(validatorSet, sizeOfCache);
this.weightedRotatingLeaders2 = new WeightedRotatingLeaders(validatorSet, sizeOfCache);
}
use of com.radixdlt.utils.UInt256 in project radixdlt by radixdlt.
the class REParser method parse.
public ParsedTxn parse(Txn txn) throws TxnParseException {
UInt256 feePaid = null;
ECDSASignature sig = null;
int sigPosition = 0;
var parserState = new ParserState(txn);
var buf = ByteBuffer.wrap(txn.getPayload());
while (buf.hasRemaining()) {
if (sig != null) {
throw new TxnParseException(parserState, "Signature must be last");
}
var curPos = buf.position();
parserState.pos(curPos);
final var inst = readInstruction(parserState, buf);
parserState.nextInstruction(inst);
if (inst.isStateUpdate()) {
parserState.substateUpdate(inst.getMicroOp().getOp());
} else if (inst.getMicroOp().getOp() == REOp.READ || inst.getMicroOp().getOp() == REOp.READINDEX) {
parserState.read();
} else if (inst.getMicroOp() == REInstruction.REMicroOp.HEADER) {
parserState.header(inst.getData());
} else if (inst.getMicroOp() == REInstruction.REMicroOp.SYSCALL) {
try {
var callData = inst.<CallData>getData();
byte id = callData.get(0);
var syscall = Syscall.of(id).orElseThrow(() -> new TxnParseException(parserState, "Invalid call data type: " + id));
switch(syscall) {
case FEE_RESERVE_PUT:
if (feePaid != null) {
throw new TxnParseException(parserState, "Should only pay fees once.");
}
feePaid = callData.getUInt256(1);
break;
case FEE_RESERVE_TAKE:
if (feePaid == null) {
throw new TxnParseException(parserState, "No fees paid");
}
var takeAmount = callData.getUInt256(1);
if (takeAmount.compareTo(feePaid) > 0) {
throw new TxnParseException(parserState, "Trying to take more fees than paid");
}
feePaid = feePaid.subtract(takeAmount);
break;
case READDR_CLAIM:
break;
// TODO: Along with FeeConstraintScrypt.java
default:
throw new TxnParseException(parserState, "Invalid call data type: " + id);
}
} catch (CallDataAccessException | TrailingBytesException e) {
throw new TxnParseException(parserState, e);
}
} else if (inst.getMicroOp() == REInstruction.REMicroOp.MSG) {
parserState.msg(inst.getData());
} else if (inst.getMicroOp() == REInstruction.REMicroOp.END) {
parserState.end();
} else if (inst.getMicroOp() == REInstruction.REMicroOp.SIG) {
sigPosition = curPos;
sig = inst.getData();
} else {
throw new TxnParseException(parserState, "Unknown CM Op " + inst.getMicroOp());
}
}
parserState.finish();
return new ParsedTxn(txn, feePaid, parserState.instructions, parserState.msg, sig == null ? null : Pair.of(calculatePayloadHash(txn, sigPosition), sig), parserState.disableResourceAllocAndDestroy);
}
use of com.radixdlt.utils.UInt256 in project radixdlt by radixdlt.
the class RadixEngineStateComputerTest method getExternalModule.
private Module getExternalModule() {
return new AbstractModule() {
@Override
public void configure() {
var validatorSet = BFTValidatorSet.from(registeredNodes.stream().map(ECKeyPair::getPublicKey).map(BFTNode::create).map(n -> BFTValidator.from(n, UInt256.ONE)));
bind(ProposerElection.class).toInstance(new WeightedRotatingLeaders(validatorSet));
bind(Serialization.class).toInstance(serialization);
bind(Hasher.class).toInstance(Sha256Hasher.withDefaultSerialization());
bind(new TypeLiteral<EngineStore<LedgerAndBFTProof>>() {
}).toInstance(engineStore);
bind(PersistentVertexStore.class).toInstance(mock(PersistentVertexStore.class));
install(MempoolConfig.asModule(10, 10));
install(new MainnetForksModule());
install(new RadixEngineForksLatestOnlyModule());
install(new ForksModule());
// HACK
bind(CommittedReader.class).toInstance(new NoOpCommittedReader());
bind(ForksEpochStore.class).toInstance(new NoOpForksEpochStore());
bind(LedgerAccumulator.class).to(SimpleLedgerAccumulatorAndVerifier.class);
bind(new TypeLiteral<EventDispatcher<MempoolAddSuccess>>() {
}).toInstance(TypedMocks.rmock(EventDispatcher.class));
bind(new TypeLiteral<EventDispatcher<InvalidProposedTxn>>() {
}).toInstance(TypedMocks.rmock(EventDispatcher.class));
bind(new TypeLiteral<EventDispatcher<TxnsRemovedFromMempool>>() {
}).toInstance(TypedMocks.rmock(EventDispatcher.class));
bind(new TypeLiteral<EventDispatcher<REOutput>>() {
}).toInstance(TypedMocks.rmock(EventDispatcher.class));
bind(new TypeLiteral<EventDispatcher<MempoolRelayTrigger>>() {
}).toInstance(TypedMocks.rmock(EventDispatcher.class));
bind(new TypeLiteral<EventDispatcher<LedgerUpdate>>() {
}).toInstance(TypedMocks.rmock(EventDispatcher.class));
bind(SystemCounters.class).to(SystemCountersImpl.class);
}
};
}
use of com.radixdlt.utils.UInt256 in project radixdlt by radixdlt.
the class MempoolTransactionHandlerTest 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 ConstructionFinalizeTest method buildUnsignedTransferTxn.
private UnsignedTxnData buildUnsignedTransferTxn(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.buildForExternalSign();
}
Aggregations