Search in sources :

Example 1 with REProcessedTxn

use of com.radixdlt.constraintmachine.REProcessedTxn in project radixdlt by radixdlt.

the class RadixEngine method executeInternal.

private RadixEngineResult executeInternal(EngineStore.EngineStoreInTransaction<M> engineStoreInTransaction, List<Txn> txns, M meta, 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 = meta != null ? 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 {
        batchVerifier.testMetadata(meta, processedTxns);
    } catch (MetadataException e) {
        logger.error("Invalid metadata: " + processedTxns);
        throw e;
    }
    if (meta != null) {
        engineStoreInTransaction.storeMetadata(meta);
    }
    return RadixEngineResult.create(processedTxns, verificationStopwatch.elapsed(TimeUnit.MILLISECONDS), storageStopwatch.elapsed(TimeUnit.MILLISECONDS));
}
Also used : ConstraintMachineException(com.radixdlt.constraintmachine.exceptions.ConstraintMachineException) ExecutionContext(com.radixdlt.constraintmachine.ExecutionContext) AuthorizationException(com.radixdlt.constraintmachine.exceptions.AuthorizationException) ArrayList(java.util.ArrayList) TxnParseException(com.radixdlt.engine.parser.exceptions.TxnParseException) REProcessedTxn(com.radixdlt.constraintmachine.REProcessedTxn) AuthorizationException(com.radixdlt.constraintmachine.exceptions.AuthorizationException) TxBuilderException(com.radixdlt.atom.TxBuilderException) ConstraintMachineException(com.radixdlt.constraintmachine.exceptions.ConstraintMachineException) TxnParseException(com.radixdlt.engine.parser.exceptions.TxnParseException) DeserializeException(com.radixdlt.serialization.DeserializeException) FeeReserveCompleteException(com.radixdlt.application.system.construction.FeeReserveCompleteException)

Example 2 with REProcessedTxn

use of com.radixdlt.constraintmachine.REProcessedTxn in project radixdlt by radixdlt.

the class InMemorySystemInfoTest method createLedgerUpdate.

private LedgerUpdate createLedgerUpdate(BFTNode self) {
    var events = List.<REEvent>of(new ValidatorBFTDataEvent(self.getKey(), 10, 1));
    var txn = new REProcessedTxn(null, null, null, events);
    var output = ImmutableClassToInstanceMap.<Object, REOutput>of(REOutput.class, REOutput.create(List.of(txn)));
    return new LedgerUpdate(mock(VerifiedTxnsAndProof.class), output);
}
Also used : REOutput(com.radixdlt.statecomputer.REOutput) LedgerUpdate(com.radixdlt.ledger.LedgerUpdate) VerifiedTxnsAndProof(com.radixdlt.ledger.VerifiedTxnsAndProof) REEvent(com.radixdlt.constraintmachine.REEvent) ValidatorBFTDataEvent(com.radixdlt.constraintmachine.REEvent.ValidatorBFTDataEvent) REProcessedTxn(com.radixdlt.constraintmachine.REProcessedTxn)

Example 3 with REProcessedTxn

use of com.radixdlt.constraintmachine.REProcessedTxn in project radixdlt by radixdlt.

the class InMemoryEngineStore method transaction.

@Override
public <R> R transaction(TransactionEngineStoreConsumer<M, R> consumer) throws RadixEngineException {
    return consumer.start(new EngineStoreInTransaction<>() {

        @Override
        public void storeTxn(REProcessedTxn txn) {
            synchronized (lock) {
                txn.stateUpdates().forEach(update -> {
                    store.storedState.put(update.getId(), update);
                    // FIXME: Superhack
                    if (update.isBootUp()) {
                        if (update.getParsed() instanceof TokenResource) {
                            var tokenDef = (TokenResource) update.getParsed();
                            store.resources.put(tokenDef.addr(), update::getStateBuf);
                        } else if (update.getParsed() instanceof VirtualParent) {
                            var p = (VirtualParent) update.getParsed();
                            var typeByte = p.data()[0];
                            var mapKey = SystemMapKey.ofSystem(typeByte);
                            store.maps.put(mapKey, update.getRawSubstateBytes());
                        } else if (update.getParsed() instanceof ValidatorData) {
                            var data = (ValidatorData) update.getParsed();
                            var mapKey = SystemMapKey.ofSystem(update.typeByte(), data.validatorKey().getCompressedBytes());
                            store.maps.put(mapKey, update.getRawSubstateBytes());
                        } else if (update.getParsed() instanceof SystemData) {
                            var mapKey = SystemMapKey.ofSystem(update.typeByte());
                            store.maps.put(mapKey, update.getRawSubstateBytes());
                        }
                    } else if (update.isShutDown()) {
                        if (update.getParsed() instanceof ValidatorData) {
                            var data = (ValidatorData) update.getParsed();
                            var mapKey = SystemMapKey.ofSystem(update.typeByte(), data.validatorKey().getCompressedBytes());
                            store.maps.remove(mapKey);
                        } else if (update.getParsed() instanceof SystemData) {
                            var mapKey = SystemMapKey.ofSystem(update.typeByte());
                            store.maps.remove(mapKey);
                        }
                    }
                });
            }
        }

        @Override
        public void storeMetadata(M metadata) {
            store.metadata = metadata;
        }

        @Override
        public ByteBuffer verifyVirtualSubstate(SubstateId substateId) throws VirtualSubstateAlreadyDownException, VirtualParentStateDoesNotExist {
            synchronized (lock) {
                var parent = substateId.getVirtualParent().orElseThrow();
                var update = store.storedState.get(parent);
                if (update == null || !(update.getParsed() instanceof VirtualParent)) {
                    throw new VirtualParentStateDoesNotExist(parent);
                }
                var inst = store.storedState.get(substateId);
                if (inst != null && inst.isShutDown()) {
                    throw new VirtualSubstateAlreadyDownException(substateId);
                }
                return update.getStateBuf();
            }
        }

        @Override
        public Optional<ByteBuffer> loadSubstate(SubstateId substateId) {
            synchronized (lock) {
                var inst = store.storedState.get(substateId);
                if (inst == null || !inst.isBootUp()) {
                    return Optional.empty();
                }
                return Optional.of(inst.getStateBuf());
            }
        }

        @Override
        public CloseableCursor<RawSubstateBytes> openIndexedCursor(SubstateIndex<?> index) {
            return InMemoryEngineStore.this.openIndexedCursor(index);
        }

        @Override
        public Optional<ByteBuffer> loadResource(REAddr addr) {
            synchronized (lock) {
                var supplier = store.resources.get(addr);
                return supplier == null ? Optional.empty() : Optional.of(supplier.get());
            }
        }
    });
}
Also used : SubstateId(com.radixdlt.atom.SubstateId) REStateUpdate(com.radixdlt.constraintmachine.REStateUpdate) RadixEngineException(com.radixdlt.engine.RadixEngineException) Inject(com.google.inject.Inject) HashMap(java.util.HashMap) Supplier(java.util.function.Supplier) ByteBuffer(java.nio.ByteBuffer) RawSubstateBytes(com.radixdlt.constraintmachine.RawSubstateBytes) ArrayList(java.util.ArrayList) Map(java.util.Map) SystemMapKey(com.radixdlt.constraintmachine.SystemMapKey) REProcessedTxn(com.radixdlt.constraintmachine.REProcessedTxn) UnsignedBytes(com.google.common.primitives.UnsignedBytes) SubstateIndex(com.radixdlt.constraintmachine.SubstateIndex) TokenResource(com.radixdlt.application.tokens.state.TokenResource) REAddr(com.radixdlt.identifiers.REAddr) ValidatorData(com.radixdlt.application.validators.state.ValidatorData) VirtualParent(com.radixdlt.application.system.state.VirtualParent) VirtualSubstateAlreadyDownException(com.radixdlt.constraintmachine.exceptions.VirtualSubstateAlreadyDownException) List(java.util.List) CloseableCursor(com.radixdlt.atom.CloseableCursor) VirtualParentStateDoesNotExist(com.radixdlt.constraintmachine.exceptions.VirtualParentStateDoesNotExist) Optional(java.util.Optional) Comparator(java.util.Comparator) SystemData(com.radixdlt.application.system.state.SystemData) ValidatorData(com.radixdlt.application.validators.state.ValidatorData) CloseableCursor(com.radixdlt.atom.CloseableCursor) TokenResource(com.radixdlt.application.tokens.state.TokenResource) Optional(java.util.Optional) SystemData(com.radixdlt.application.system.state.SystemData) REProcessedTxn(com.radixdlt.constraintmachine.REProcessedTxn) ByteBuffer(java.nio.ByteBuffer) VirtualParentStateDoesNotExist(com.radixdlt.constraintmachine.exceptions.VirtualParentStateDoesNotExist) SubstateId(com.radixdlt.atom.SubstateId) VirtualSubstateAlreadyDownException(com.radixdlt.constraintmachine.exceptions.VirtualSubstateAlreadyDownException) VirtualParent(com.radixdlt.application.system.state.VirtualParent) REAddr(com.radixdlt.identifiers.REAddr)

Example 4 with REProcessedTxn

use of com.radixdlt.constraintmachine.REProcessedTxn 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;
    }
}
Also used : SubstateSerialization(com.radixdlt.constraintmachine.SubstateSerialization) TxAction(com.radixdlt.atom.TxAction) BiFunction(java.util.function.BiFunction) TxBuilder(com.radixdlt.atom.TxBuilder) TxnConstructionRequest(com.radixdlt.atom.TxnConstructionRequest) ResourceInBucket(com.radixdlt.application.tokens.ResourceInBucket) VirtualSubstateDeserialization(com.radixdlt.constraintmachine.VirtualSubstateDeserialization) ParsedTxn(com.radixdlt.engine.parser.ParsedTxn) Map(java.util.Map) SystemMapKey(com.radixdlt.constraintmachine.SystemMapKey) AuthorizationException(com.radixdlt.constraintmachine.exceptions.AuthorizationException) Predicate(java.util.function.Predicate) Set(java.util.Set) ExecutionContext(com.radixdlt.constraintmachine.ExecutionContext) REAddr(com.radixdlt.identifiers.REAddr) TxBuilderException(com.radixdlt.atom.TxBuilderException) EngineStore(com.radixdlt.store.EngineStore) Particle(com.radixdlt.constraintmachine.Particle) Objects(java.util.Objects) List(java.util.List) Logger(org.apache.logging.log4j.Logger) ConstraintMachineException(com.radixdlt.constraintmachine.exceptions.ConstraintMachineException) Optional(java.util.Optional) TxnParseException(com.radixdlt.engine.parser.exceptions.TxnParseException) SubstateId(com.radixdlt.atom.SubstateId) UInt384(com.radixdlt.utils.UInt384) Stopwatch(com.google.common.base.Stopwatch) SubstateStore(com.radixdlt.atom.SubstateStore) HashMap(java.util.HashMap) PermissionLevel(com.radixdlt.constraintmachine.PermissionLevel) ECPublicKey(com.radixdlt.crypto.ECPublicKey) AtomicReference(java.util.concurrent.atomic.AtomicReference) Function(java.util.function.Function) RawSubstateBytes(com.radixdlt.constraintmachine.RawSubstateBytes) ArrayList(java.util.ArrayList) TransientEngineStore(com.radixdlt.store.TransientEngineStore) SubstateDeserialization(com.radixdlt.constraintmachine.SubstateDeserialization) REParser(com.radixdlt.engine.parser.REParser) UInt256(com.radixdlt.utils.UInt256) REProcessedTxn(com.radixdlt.constraintmachine.REProcessedTxn) SubstateIndex(com.radixdlt.constraintmachine.SubstateIndex) ConstraintMachineConfig(com.radixdlt.constraintmachine.ConstraintMachineConfig) Txn(com.radixdlt.atom.Txn) TimeUnit(java.util.concurrent.TimeUnit) CloseableCursor(com.radixdlt.atom.CloseableCursor) ConstraintMachine(com.radixdlt.constraintmachine.ConstraintMachine) DeserializeException(com.radixdlt.serialization.DeserializeException) FeeReserveCompleteException(com.radixdlt.application.system.construction.FeeReserveCompleteException) LogManager(org.apache.logging.log4j.LogManager) REConstructor(com.radixdlt.atom.REConstructor) AuthorizationException(com.radixdlt.constraintmachine.exceptions.AuthorizationException) ArrayList(java.util.ArrayList) TxnParseException(com.radixdlt.engine.parser.exceptions.TxnParseException) REProcessedTxn(com.radixdlt.constraintmachine.REProcessedTxn) AuthorizationException(com.radixdlt.constraintmachine.exceptions.AuthorizationException) TxBuilderException(com.radixdlt.atom.TxBuilderException) ConstraintMachineException(com.radixdlt.constraintmachine.exceptions.ConstraintMachineException) TxnParseException(com.radixdlt.engine.parser.exceptions.TxnParseException) DeserializeException(com.radixdlt.serialization.DeserializeException) FeeReserveCompleteException(com.radixdlt.application.system.construction.FeeReserveCompleteException) ConstraintMachineException(com.radixdlt.constraintmachine.exceptions.ConstraintMachineException) ExecutionContext(com.radixdlt.constraintmachine.ExecutionContext)

Example 5 with REProcessedTxn

use of com.radixdlt.constraintmachine.REProcessedTxn in project radixdlt by radixdlt.

the class ConstructionParseHandler method handleRequest.

@Override
public ConstructionParseResponse handleRequest(ConstructionParseRequest request) throws CoreApiException {
    modelMapper.verifyNetwork(request.getNetworkIdentifier());
    var txn = modelMapper.bytes(request.getTransaction());
    REProcessedTxn processed;
    try {
        processed = radixEngineStateComputer.test(txn, request.getSigned());
    } catch (RadixEngineException e) {
        throw modelMapper.radixEngineException(e);
    }
    var response = new ConstructionParseResponse();
    var transaction = modelMapper.transaction(processed, this::symbol);
    transaction.getOperationGroups().forEach(response::addOperationGroupsItem);
    response.metadata(new ParsedTransactionMetadata().fee(transaction.getMetadata().getFee()).message(transaction.getMetadata().getMessage()));
    return response;
}
Also used : ConstructionParseResponse(com.radixdlt.api.core.openapitools.model.ConstructionParseResponse) ParsedTransactionMetadata(com.radixdlt.api.core.openapitools.model.ParsedTransactionMetadata) RadixEngineException(com.radixdlt.engine.RadixEngineException) REProcessedTxn(com.radixdlt.constraintmachine.REProcessedTxn)

Aggregations

REProcessedTxn (com.radixdlt.constraintmachine.REProcessedTxn)6 CloseableCursor (com.radixdlt.atom.CloseableCursor)3 SubstateId (com.radixdlt.atom.SubstateId)3 REAddr (com.radixdlt.identifiers.REAddr)3 DeserializeException (com.radixdlt.serialization.DeserializeException)3 ArrayList (java.util.ArrayList)3 Optional (java.util.Optional)3 FeeReserveCompleteException (com.radixdlt.application.system.construction.FeeReserveCompleteException)2 TxBuilderException (com.radixdlt.atom.TxBuilderException)2 ExecutionContext (com.radixdlt.constraintmachine.ExecutionContext)2 RawSubstateBytes (com.radixdlt.constraintmachine.RawSubstateBytes)2 SubstateIndex (com.radixdlt.constraintmachine.SubstateIndex)2 SystemMapKey (com.radixdlt.constraintmachine.SystemMapKey)2 AuthorizationException (com.radixdlt.constraintmachine.exceptions.AuthorizationException)2 ConstraintMachineException (com.radixdlt.constraintmachine.exceptions.ConstraintMachineException)2 RadixEngineException (com.radixdlt.engine.RadixEngineException)2 TxnParseException (com.radixdlt.engine.parser.exceptions.TxnParseException)2 HashMap (java.util.HashMap)2 List (java.util.List)2 Map (java.util.Map)2