Search in sources :

Example 1 with Bytes

use of com.radixdlt.utils.Bytes 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);
    }));
}
Also used : SubstateTypeId(com.radixdlt.atom.SubstateTypeId) ReducerResult(com.radixdlt.constraintmachine.ReducerResult) Arrays(java.util.Arrays) VoidReducerState(com.radixdlt.constraintmachine.VoidReducerState) Loader(com.radixdlt.atomos.Loader) ReducerState(com.radixdlt.constraintmachine.ReducerState) Authorization(com.radixdlt.constraintmachine.Authorization) ExecutionContext(com.radixdlt.constraintmachine.ExecutionContext) PermissionLevel(com.radixdlt.constraintmachine.PermissionLevel) REAddr(com.radixdlt.identifiers.REAddr) SystemCallProcedure(com.radixdlt.constraintmachine.SystemCallProcedure) RoundData(com.radixdlt.application.system.state.RoundData) VirtualParent(com.radixdlt.application.system.state.VirtualParent) InvalidHashedKeyException(com.radixdlt.constraintmachine.exceptions.InvalidHashedKeyException) Bytes(com.radixdlt.utils.Bytes) UpProcedure(com.radixdlt.constraintmachine.UpProcedure) TokenHoldingBucket(com.radixdlt.application.tokens.scrypt.TokenHoldingBucket) UnclaimedREAddr(com.radixdlt.application.system.state.UnclaimedREAddr) ConstraintScrypt(com.radixdlt.atomos.ConstraintScrypt) DownProcedure(com.radixdlt.constraintmachine.DownProcedure) ProcedureException(com.radixdlt.constraintmachine.exceptions.ProcedureException) LinkedList(java.util.LinkedList) EpochData(com.radixdlt.application.system.state.EpochData) EpochData(com.radixdlt.application.system.state.EpochData) VoidReducerState(com.radixdlt.constraintmachine.VoidReducerState) RoundData(com.radixdlt.application.system.state.RoundData) UnclaimedREAddr(com.radixdlt.application.system.state.UnclaimedREAddr) PermissionLevel(com.radixdlt.constraintmachine.PermissionLevel) TokenHoldingBucket(com.radixdlt.application.tokens.scrypt.TokenHoldingBucket) Authorization(com.radixdlt.constraintmachine.Authorization) ProcedureException(com.radixdlt.constraintmachine.exceptions.ProcedureException) VirtualParent(com.radixdlt.application.system.state.VirtualParent)

Example 2 with Bytes

use of com.radixdlt.utils.Bytes in project radixdlt by radixdlt.

the class TransactionsHandler method construct.

private CommittedTransaction construct(Txn txn, RecoverableProcessedTxn recoveryInfo, AccumulatorState accumulatorState) {
    var parser = radixEngineProvider.get().getParser();
    ParsedTxn parsedTxn;
    try {
        parsedTxn = parser.parse(txn);
    } catch (TxnParseException e) {
        throw new IllegalStateException("Could not parse already committed transaction", e);
    }
    var committedTransaction = new CommittedTransaction();
    recoveryInfo.recoverStateUpdates(parsedTxn).stream().map(stateUpdateGroup -> {
        var operationGroup = new OperationGroup();
        stateUpdateGroup.stream().map(stateUpdate -> {
            var substateOperation = stateUpdate.recover(radixEngineProvider);
            return coreModelMapper.operation(substateOperation.getSubstate(), substateOperation.getSubstateId(), substateOperation.isBootUp(), this::symbol);
        }).forEach(operationGroup::addOperationsItem);
        return operationGroup;
    }).forEach(committedTransaction::addOperationGroupsItem);
    var signedBy = parsedTxn.getPayloadHashAndSig().map(hashAndSig -> {
        var hash = hashAndSig.getFirst();
        var sig = hashAndSig.getSecond();
        return ECPublicKey.recoverFrom(hash, sig).orElseThrow(() -> new IllegalStateException("Invalid signature on already committed transaction"));
    });
    var transactionIdentifier = coreModelMapper.transactionIdentifier(txn.getId());
    return committedTransaction.committedStateIdentifier(coreModelMapper.stateIdentifier(accumulatorState)).metadata(new CommittedTransactionMetadata().fee(coreModelMapper.nativeTokenAmount(parsedTxn.getFeePaid())).message(parsedTxn.getMsg().map(Bytes::toHexString).orElse(null)).size(txn.getPayload().length).hex(Bytes.toHexString(txn.getPayload())).signedBy(signedBy.map(coreModelMapper::publicKey).orElse(null))).transactionIdentifier(transactionIdentifier);
}
Also used : SubstateTypeId(com.radixdlt.atom.SubstateTypeId) LedgerAndBFTProof(com.radixdlt.statecomputer.LedgerAndBFTProof) Inject(com.google.inject.Inject) RecoverableProcessedTxn(com.radixdlt.api.core.reconstruction.RecoverableProcessedTxn) StateIdentifier(com.radixdlt.api.core.openapitools.model.StateIdentifier) ECPublicKey(com.radixdlt.crypto.ECPublicKey) OperationGroup(com.radixdlt.api.core.openapitools.model.OperationGroup) ParsedTxn(com.radixdlt.engine.parser.ParsedTxn) CommittedTransaction(com.radixdlt.api.core.openapitools.model.CommittedTransaction) CoreJsonRpcHandler(com.radixdlt.api.core.CoreJsonRpcHandler) BerkeleyLedgerEntryStore(com.radixdlt.store.berkeley.BerkeleyLedgerEntryStore) SystemMapKey(com.radixdlt.constraintmachine.SystemMapKey) AccumulatorState(com.radixdlt.ledger.AccumulatorState) BerkeleyRecoverableProcessedTxnStore(com.radixdlt.api.core.reconstruction.BerkeleyRecoverableProcessedTxnStore) CoreModelMapper(com.radixdlt.api.core.model.CoreModelMapper) TokenResourceMetadata(com.radixdlt.application.tokens.state.TokenResourceMetadata) CoreApiException(com.radixdlt.api.core.model.CoreApiException) RadixEngine(com.radixdlt.engine.RadixEngine) Txn(com.radixdlt.atom.Txn) REAddr(com.radixdlt.identifiers.REAddr) CommittedTransactionsRequest(com.radixdlt.api.core.openapitools.model.CommittedTransactionsRequest) Provider(com.google.inject.Provider) Bytes(com.radixdlt.utils.Bytes) CommittedTransactionMetadata(com.radixdlt.api.core.openapitools.model.CommittedTransactionMetadata) CommittedTransactionsResponse(com.radixdlt.api.core.openapitools.model.CommittedTransactionsResponse) TxnParseException(com.radixdlt.engine.parser.exceptions.TxnParseException) LedgerAccumulator(com.radixdlt.ledger.LedgerAccumulator) OperationGroup(com.radixdlt.api.core.openapitools.model.OperationGroup) TxnParseException(com.radixdlt.engine.parser.exceptions.TxnParseException) CommittedTransactionMetadata(com.radixdlt.api.core.openapitools.model.CommittedTransactionMetadata) CommittedTransaction(com.radixdlt.api.core.openapitools.model.CommittedTransaction) ParsedTxn(com.radixdlt.engine.parser.ParsedTxn)

Aggregations

SubstateTypeId (com.radixdlt.atom.SubstateTypeId)2 REAddr (com.radixdlt.identifiers.REAddr)2 Bytes (com.radixdlt.utils.Bytes)2 Inject (com.google.inject.Inject)1 Provider (com.google.inject.Provider)1 CoreJsonRpcHandler (com.radixdlt.api.core.CoreJsonRpcHandler)1 CoreApiException (com.radixdlt.api.core.model.CoreApiException)1 CoreModelMapper (com.radixdlt.api.core.model.CoreModelMapper)1 CommittedTransaction (com.radixdlt.api.core.openapitools.model.CommittedTransaction)1 CommittedTransactionMetadata (com.radixdlt.api.core.openapitools.model.CommittedTransactionMetadata)1 CommittedTransactionsRequest (com.radixdlt.api.core.openapitools.model.CommittedTransactionsRequest)1 CommittedTransactionsResponse (com.radixdlt.api.core.openapitools.model.CommittedTransactionsResponse)1 OperationGroup (com.radixdlt.api.core.openapitools.model.OperationGroup)1 StateIdentifier (com.radixdlt.api.core.openapitools.model.StateIdentifier)1 BerkeleyRecoverableProcessedTxnStore (com.radixdlt.api.core.reconstruction.BerkeleyRecoverableProcessedTxnStore)1 RecoverableProcessedTxn (com.radixdlt.api.core.reconstruction.RecoverableProcessedTxn)1 EpochData (com.radixdlt.application.system.state.EpochData)1 RoundData (com.radixdlt.application.system.state.RoundData)1 UnclaimedREAddr (com.radixdlt.application.system.state.UnclaimedREAddr)1 VirtualParent (com.radixdlt.application.system.state.VirtualParent)1