Search in sources :

Example 1 with TrailingBytesException

use of com.radixdlt.engine.parser.exceptions.TrailingBytesException in project radixdlt by radixdlt.

the class ConstraintMachine method statefulVerify.

/**
 * Executes transition procedures and witness validators in a particle group and validates that
 * the particle group is well formed.
 */
List<List<REStateUpdate>> statefulVerify(ExecutionContext context, CMValidationState validationState, List<REInstruction> instructions) throws ConstraintMachineException {
    int instIndex = 0;
    var expectEnd = false;
    ReducerState reducerState = null;
    var readableAddrs = validationState.resources();
    var groupedStateUpdates = new ArrayList<List<REStateUpdate>>();
    var stateUpdates = new ArrayList<REStateUpdate>();
    meter.onStart(context);
    for (REInstruction inst : instructions) {
        try {
            if (expectEnd && inst.getMicroOp() != REInstruction.REMicroOp.END) {
                throw new MissingExpectedEndException();
            }
            if (inst.getMicroOp() == REInstruction.REMicroOp.SYSCALL) {
                CallData callData = inst.getData();
                var opSignature = OpSignature.ofMethod(inst.getMicroOp().getOp(), REAddr.ofSystem());
                var methodProcedure = loadProcedure(reducerState, opSignature);
                reducerState = callProcedure(methodProcedure, callData, reducerState, readableAddrs, context);
            } else if (inst.getMicroOp().getOp() == REOp.READ) {
                final Particle nextParticle;
                if (inst.getMicroOp() == REInstruction.REMicroOp.VREAD) {
                    SubstateId substateId = inst.getData();
                    nextParticle = validationState.virtualRead(substateId);
                } else if (inst.getMicroOp() == REInstruction.REMicroOp.READ) {
                    SubstateId substateId = inst.getData();
                    nextParticle = validationState.read(substateId);
                } else if (inst.getMicroOp() == REInstruction.REMicroOp.LREAD) {
                    SubstateId substateId = inst.getData();
                    nextParticle = validationState.localRead(substateId.getIndex().orElseThrow());
                } else if (inst.getMicroOp() == REInstruction.REMicroOp.LVREAD) {
                    SubstateId substateId = inst.getData();
                    nextParticle = validationState.localVirtualRead(substateId);
                } else {
                    throw new IllegalStateException("Unknown read op " + inst.getMicroOp());
                }
                var eventId = OpSignature.ofSubstateUpdate(inst.getMicroOp().getOp(), nextParticle.getClass());
                var methodProcedure = loadProcedure(reducerState, eventId);
                reducerState = callProcedure(methodProcedure, nextParticle, reducerState, readableAddrs, context);
                expectEnd = reducerState == null;
            } else if (inst.getMicroOp().getOp() == REOp.DOWNINDEX || inst.getMicroOp().getOp() == REOp.READINDEX) {
                byte[] raw = inst.getData();
                var index = SubstateIndex.create(raw, validationState.deserialization.byteToClass(raw[0]));
                var substateCursor = validationState.getIndexedCursor(index);
                var tmp = stateUpdates;
                final int tmpInstIndex = instIndex;
                var iterator = new Iterator<Particle>() {

                    @Override
                    public boolean hasNext() {
                        return substateCursor.hasNext();
                    }

                    @Override
                    public Particle next() {
                        // FIXME: this is a hack
                        // FIXME: do this via shutdownAll state update rather than individually
                        var substate = substateCursor.next();
                        if (inst.getMicroOp().getOp() == REOp.DOWNINDEX) {
                            var typeByte = deserialization.classToByte(substate.getParticle().getClass());
                            tmp.add(REStateUpdate.of(REOp.DOWN, tmpInstIndex, substate.getId(), typeByte, substate.getParticle(), null));
                        }
                        return substate.getParticle();
                    }
                };
                var substateIterator = new IndexedSubstateIterator<>(index, iterator);
                try {
                    var eventId = OpSignature.ofSubstateUpdate(inst.getMicroOp().getOp(), index.getSubstateClass());
                    var methodProcedure = loadProcedure(reducerState, eventId);
                    reducerState = callProcedure(methodProcedure, substateIterator, reducerState, readableAddrs, context);
                } finally {
                    substateCursor.close();
                }
            } else if (inst.isStateUpdate()) {
                final SubstateId substateId;
                final Particle nextParticle;
                final Supplier<ByteBuffer> substateBuffer;
                if (inst.getMicroOp() == REInstruction.REMicroOp.UP) {
                    // TODO: Cleanup indexing of substate class
                    UpSubstate upSubstate = inst.getData();
                    var buf = upSubstate.getSubstateBuffer();
                    nextParticle = validationState.deserialization.deserialize(buf);
                    if (buf.hasRemaining()) {
                        throw new TrailingBytesException("Substate has trailing bytes.");
                    }
                    substateId = upSubstate.getSubstateId();
                    substateBuffer = upSubstate::getSubstateBuffer;
                    validationState.bootUp(Substate.create(nextParticle, substateId), upSubstate::getSubstateBuffer);
                } else if (inst.getMicroOp() == REInstruction.REMicroOp.VDOWN) {
                    substateId = inst.getData();
                    substateBuffer = null;
                    nextParticle = validationState.virtualShutdown(substateId);
                } else if (inst.getMicroOp() == REInstruction.REMicroOp.DOWN) {
                    substateId = inst.getData();
                    substateBuffer = null;
                    nextParticle = validationState.shutdown(substateId);
                } else if (inst.getMicroOp() == REInstruction.REMicroOp.LDOWN) {
                    substateId = inst.getData();
                    substateBuffer = null;
                    nextParticle = validationState.localShutdown(substateId.getIndex().orElseThrow());
                } else if (inst.getMicroOp() == REInstruction.REMicroOp.LVDOWN) {
                    substateId = inst.getData();
                    substateBuffer = null;
                    nextParticle = validationState.localVirtualShutdown(substateId);
                } else {
                    throw new IllegalStateException("Unhandled op: " + inst.getMicroOp());
                }
                var op = inst.getMicroOp().getOp();
                var typeByte = deserialization.classToByte(nextParticle.getClass());
                stateUpdates.add(REStateUpdate.of(op, instIndex, substateId, typeByte, nextParticle, substateBuffer));
                var eventId = OpSignature.ofSubstateUpdate(op, nextParticle.getClass());
                var methodProcedure = loadProcedure(reducerState, eventId);
                reducerState = callProcedure(methodProcedure, nextParticle, reducerState, readableAddrs, context);
                expectEnd = reducerState == null;
            } else if (inst.getMicroOp() == REInstruction.REMicroOp.END) {
                groupedStateUpdates.add(stateUpdates);
                stateUpdates = new ArrayList<>();
                if (reducerState != null) {
                    var eventId = OpSignature.ofSubstateUpdate(inst.getMicroOp().getOp(), null);
                    var methodProcedure = loadProcedure(reducerState, eventId);
                    reducerState = callProcedure(methodProcedure, reducerState, reducerState, readableAddrs, context);
                }
                expectEnd = false;
            } else if (inst.getMicroOp() == REInstruction.REMicroOp.SIG) {
                if (context.permissionLevel() != PermissionLevel.SYSTEM) {
                    meter.onSigInstruction(context);
                }
            } else {
                // Collect no-ops here
                if (inst.getMicroOp() != REInstruction.REMicroOp.MSG && inst.getMicroOp() != REInstruction.REMicroOp.HEADER) {
                    throw new ProcedureException("Unknown op " + inst.getMicroOp());
                }
            }
        } catch (Exception e) {
            throw new ConstraintMachineException(instIndex, instructions, reducerState, e);
        }
        instIndex++;
    }
    try {
        context.destroy();
    } catch (Exception e) {
        throw new ConstraintMachineException(instIndex, instructions, reducerState, e);
    }
    return groupedStateUpdates;
}
Also used : ArrayList(java.util.ArrayList) MeterException(com.radixdlt.constraintmachine.exceptions.MeterException) ProcedureException(com.radixdlt.constraintmachine.exceptions.ProcedureException) MissingProcedureException(com.radixdlt.constraintmachine.exceptions.MissingProcedureException) AuthorizationException(com.radixdlt.constraintmachine.exceptions.AuthorizationException) SignedSystemException(com.radixdlt.constraintmachine.exceptions.SignedSystemException) SubstateNotFoundException(com.radixdlt.constraintmachine.exceptions.SubstateNotFoundException) NotAResourceException(com.radixdlt.constraintmachine.exceptions.NotAResourceException) VirtualSubstateAlreadyDownException(com.radixdlt.constraintmachine.exceptions.VirtualSubstateAlreadyDownException) ConstraintMachineException(com.radixdlt.constraintmachine.exceptions.ConstraintMachineException) LocalSubstateNotFoundException(com.radixdlt.constraintmachine.exceptions.LocalSubstateNotFoundException) TrailingBytesException(com.radixdlt.engine.parser.exceptions.TrailingBytesException) InvalidPermissionException(com.radixdlt.constraintmachine.exceptions.InvalidPermissionException) TxnParseException(com.radixdlt.engine.parser.exceptions.TxnParseException) DeserializeException(com.radixdlt.serialization.DeserializeException) ConstraintMachineException(com.radixdlt.constraintmachine.exceptions.ConstraintMachineException) TrailingBytesException(com.radixdlt.engine.parser.exceptions.TrailingBytesException) Iterator(java.util.Iterator) SubstateId(com.radixdlt.atom.SubstateId) Supplier(java.util.function.Supplier) ProcedureException(com.radixdlt.constraintmachine.exceptions.ProcedureException) MissingProcedureException(com.radixdlt.constraintmachine.exceptions.MissingProcedureException)

Example 2 with TrailingBytesException

use of com.radixdlt.engine.parser.exceptions.TrailingBytesException in project radixdlt by radixdlt.

the class VirtualSubstateDeserialization method keyToSubstate.

public Particle keyToSubstate(byte typeByte, ByteBuffer buf) throws DeserializeException {
    var deserializer = byteToDeserializer.get(typeByte);
    if (deserializer == null) {
        throw new DeserializeException("Unknown byte type: " + typeByte);
    }
    try {
        var key = deserializer.keyDeserializer().deserialize(buf);
        var rawSubstate = deserializer.virtualMapper().map(key);
        if (buf.hasRemaining()) {
            throw new TrailingBytesException("Buffer has " + buf.remaining() + " bytes remaining.");
        }
        return rawSubstate;
    } catch (Exception e) {
        throw new SubstateDeserializationException(deserializer.substateClass(), e);
    }
}
Also used : TrailingBytesException(com.radixdlt.engine.parser.exceptions.TrailingBytesException) SubstateDeserializationException(com.radixdlt.engine.parser.exceptions.SubstateDeserializationException) DeserializeException(com.radixdlt.serialization.DeserializeException) SubstateDeserializationException(com.radixdlt.engine.parser.exceptions.SubstateDeserializationException) TrailingBytesException(com.radixdlt.engine.parser.exceptions.TrailingBytesException) DeserializeException(com.radixdlt.serialization.DeserializeException)

Aggregations

TrailingBytesException (com.radixdlt.engine.parser.exceptions.TrailingBytesException)2 DeserializeException (com.radixdlt.serialization.DeserializeException)2 SubstateId (com.radixdlt.atom.SubstateId)1 AuthorizationException (com.radixdlt.constraintmachine.exceptions.AuthorizationException)1 ConstraintMachineException (com.radixdlt.constraintmachine.exceptions.ConstraintMachineException)1 InvalidPermissionException (com.radixdlt.constraintmachine.exceptions.InvalidPermissionException)1 LocalSubstateNotFoundException (com.radixdlt.constraintmachine.exceptions.LocalSubstateNotFoundException)1 MeterException (com.radixdlt.constraintmachine.exceptions.MeterException)1 MissingProcedureException (com.radixdlt.constraintmachine.exceptions.MissingProcedureException)1 NotAResourceException (com.radixdlt.constraintmachine.exceptions.NotAResourceException)1 ProcedureException (com.radixdlt.constraintmachine.exceptions.ProcedureException)1 SignedSystemException (com.radixdlt.constraintmachine.exceptions.SignedSystemException)1 SubstateNotFoundException (com.radixdlt.constraintmachine.exceptions.SubstateNotFoundException)1 VirtualSubstateAlreadyDownException (com.radixdlt.constraintmachine.exceptions.VirtualSubstateAlreadyDownException)1 SubstateDeserializationException (com.radixdlt.engine.parser.exceptions.SubstateDeserializationException)1 TxnParseException (com.radixdlt.engine.parser.exceptions.TxnParseException)1 ArrayList (java.util.ArrayList)1 Iterator (java.util.Iterator)1 Supplier (java.util.function.Supplier)1