use of com.radixdlt.atom.SubstateId 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());
}
}
});
}
use of com.radixdlt.atom.SubstateId 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;
}
use of com.radixdlt.atom.SubstateId in project radixdlt by radixdlt.
the class BerkeleyLedgerEntryStore method transaction.
@Override
public <R> R transaction(TransactionEngineStoreConsumer<LedgerAndBFTProof, R> consumer) throws RadixEngineException {
var dbTxn = createTransaction();
try {
var result = consumer.start(new EngineStoreInTransaction<>() {
@Override
public void storeTxn(REProcessedTxn txn) {
BerkeleyLedgerEntryStore.this.storeTxn(dbTxn, txn);
}
@Override
public void storeMetadata(LedgerAndBFTProof metadata) {
BerkeleyLedgerEntryStore.this.storeMetadata(dbTxn, metadata);
}
@Override
public ByteBuffer verifyVirtualSubstate(SubstateId substateId) throws VirtualSubstateAlreadyDownException, VirtualParentStateDoesNotExist {
var parent = substateId.getVirtualParent().orElseThrow();
var parentState = BerkeleyLedgerEntryStore.this.loadSubstate(dbTxn, parent);
if (parentState.isEmpty()) {
throw new VirtualParentStateDoesNotExist(parent);
}
var buf = parentState.get();
if (buf.get() != SubstateTypeId.VIRTUAL_PARENT.id()) {
throw new VirtualParentStateDoesNotExist(parent);
}
buf.position(buf.position() - 1);
if (BerkeleyLedgerEntryStore.this.isVirtualDown(dbTxn, substateId)) {
throw new VirtualSubstateAlreadyDownException(substateId);
}
return buf;
}
@Override
public Optional<ByteBuffer> loadSubstate(SubstateId substateId) {
return BerkeleyLedgerEntryStore.this.loadSubstate(dbTxn, substateId);
}
@Override
public CloseableCursor<RawSubstateBytes> openIndexedCursor(SubstateIndex<?> index) {
return BerkeleyLedgerEntryStore.this.openIndexedCursor(dbTxn, index);
}
@Override
public Optional<ByteBuffer> loadResource(REAddr addr) {
return BerkeleyLedgerEntryStore.this.loadAddr(dbTxn, addr);
}
});
dbTxn.commit();
return result;
} catch (Exception e) {
dbTxn.abort();
throw e;
}
}
Aggregations