use of com.radixdlt.constraintmachine.PermissionLevel in project radixdlt by radixdlt.
the class RadixEngine method executeInternal.
private RadixEngineResult<M> executeInternal(EngineStore.EngineStoreInTransaction<M> engineStoreInTransaction, List<Txn> txns, Optional<M> metaOpt, PermissionLevel permissionLevel, boolean skipAuthorization) throws RadixEngineException {
var processedTxns = new ArrayList<REProcessedTxn>();
// FIXME: This is quite the hack to increase sigsLeft for execution on noncommits (e.g. mempool)
// FIXME: Should probably just change metering
// Start with 0
var sigsLeft = metaOpt.isPresent() ? 0 : 1000;
var storageStopwatch = Stopwatch.createUnstarted();
var verificationStopwatch = Stopwatch.createUnstarted();
for (int i = 0; i < txns.size(); i++) {
var txn = txns.get(i);
verificationStopwatch.start();
var context = new ExecutionContext(txn, permissionLevel, skipAuthorization, sigsLeft);
final REProcessedTxn processedTxn;
try {
processedTxn = this.verify(engineStoreInTransaction, txn, context);
} catch (TxnParseException | AuthorizationException | ConstraintMachineException e) {
throw new RadixEngineException(i, txns.size(), txn, e);
}
verificationStopwatch.stop();
// Carry sigs left to the next transaction
sigsLeft = context.sigsLeft();
storageStopwatch.start();
try {
engineStoreInTransaction.storeTxn(processedTxn);
} catch (Exception e) {
logger.error("Store of atom failed: " + processedTxn, e);
throw e;
}
storageStopwatch.stop();
processedTxns.add(processedTxn);
}
try {
final var resultMetadata = metaOpt.map(meta -> {
final var postProcessedMetadata = postProcessor.process(meta, engineStoreInTransaction, processedTxns);
engineStoreInTransaction.storeMetadata(postProcessedMetadata);
return postProcessedMetadata;
}).orElse(null);
return RadixEngineResult.create(processedTxns, resultMetadata, verificationStopwatch.elapsed(TimeUnit.MILLISECONDS), storageStopwatch.elapsed(TimeUnit.MILLISECONDS));
} catch (PostProcessorException e) {
logger.error("Invalid metadata: " + processedTxns);
throw e;
}
}
use of com.radixdlt.constraintmachine.PermissionLevel in project radixdlt by radixdlt.
the class SystemConstraintScrypt method main.
@Override
public void main(Loader os) {
os.substate(VirtualParent.SUBSTATE_DEFINITION);
// TODO: Down singleton
os.procedure(new UpProcedure<>(VoidReducerState.class, VirtualParent.class, u -> new Authorization(PermissionLevel.SYSTEM, (r, c) -> {
}), (s, u, c, r) -> {
if (u.data().length != 1) {
throw new ProcedureException("Invalid data: " + Bytes.toHexString(u.data()));
}
if (u.data()[0] != SubstateTypeId.UNCLAIMED_READDR.id()) {
throw new ProcedureException("Invalid data: " + Bytes.toHexString(u.data()));
}
return ReducerResult.complete();
}));
os.substate(EpochData.SUBSTATE_DEFINITION);
os.substate(RoundData.SUBSTATE_DEFINITION);
os.procedure(new SystemCallProcedure<>(TokenHoldingBucket.class, REAddr.ofSystem(), () -> new Authorization(PermissionLevel.USER, (r, c) -> {
}), (s, d, c) -> {
var id = d.get(0);
var syscall = Syscall.of(id).orElseThrow(() -> new ProcedureException("Invalid call type " + id));
if (syscall != Syscall.FEE_RESERVE_PUT) {
throw new ProcedureException("Invalid call type: " + syscall);
}
var amt = d.getUInt256(1);
var tokens = s.withdraw(REAddr.ofNativeToken(), amt);
c.depositFeeReserve(tokens);
return ReducerResult.incomplete(s);
}));
os.procedure(new SystemCallProcedure<>(VoidReducerState.class, REAddr.ofSystem(), () -> new Authorization(PermissionLevel.USER, (r, c) -> {
}), (s, d, c) -> {
var id = d.get(0);
var syscall = Syscall.of(id).orElseThrow(() -> new ProcedureException("Invalid call type " + id));
if (syscall == Syscall.FEE_RESERVE_TAKE) {
var amt = d.getUInt256(1);
var tokens = c.withdrawFeeReserve(amt);
return ReducerResult.incomplete(new TokenHoldingBucket(tokens));
} else if (syscall == Syscall.READDR_CLAIM) {
var bytes = d.getRemainingBytes(1);
if (bytes.length > MAX_SYMBOL_LENGTH) {
throw new ProcedureException("Address claim too large.");
}
return ReducerResult.incomplete(new REAddrClaimStart(bytes));
} else {
throw new ProcedureException("Invalid call type: " + syscall);
}
}));
// PUB_KEY type is already claimed by accounts
os.substate(UnclaimedREAddr.SUBSTATE_DEFINITION);
os.procedure(new DownProcedure<>(REAddrClaimStart.class, UnclaimedREAddr.class, d -> {
final PermissionLevel permissionLevel;
if (d.addr().isNativeToken() || d.addr().isSystem()) {
permissionLevel = PermissionLevel.SYSTEM;
} else {
permissionLevel = PermissionLevel.USER;
}
return new Authorization(permissionLevel, (r, ctx) -> {
});
}, (d, s, r, c) -> ReducerResult.incomplete(s.claim(d, c))));
// For Mainnet Genesis
os.procedure(new UpProcedure<>(SystemConstraintScrypt.REAddrClaim.class, EpochData.class, u -> new Authorization(PermissionLevel.SYSTEM, (r, c) -> {
}), (s, u, c, r) -> {
if (u.epoch() != 0) {
throw new ProcedureException("First epoch must be 0.");
}
return ReducerResult.incomplete(new AllocatingSystem());
}));
os.procedure(new UpProcedure<>(AllocatingSystem.class, RoundData.class, u -> new Authorization(PermissionLevel.SYSTEM, (r, c) -> {
}), (s, u, c, r) -> {
if (u.view() != 0) {
throw new ProcedureException("First view must be 0.");
}
return ReducerResult.incomplete(new AllocatingVirtualState());
}));
os.procedure(new UpProcedure<>(AllocatingVirtualState.class, VirtualParent.class, u -> new Authorization(PermissionLevel.SYSTEM, (r, c) -> {
}), (s, u, c, r) -> {
var next = s.createVirtualSubstate(u);
return next == null ? ReducerResult.complete() : ReducerResult.incomplete(next);
}));
}
Aggregations