use of com.radixdlt.application.system.state.SystemData in project radixdlt by radixdlt.
the class BerkeleyLedgerEntryStore method executeStateUpdate.
private void executeStateUpdate(com.sleepycat.je.Transaction txn, REStateUpdate stateUpdate) {
if (stateUpdate.isBootUp()) {
var buf = stateUpdate.getStateBuf();
upParticle(txn, buf, stateUpdate.getId());
// FIXME: Superhack
if (stateUpdate.getParsed() instanceof TokenResource) {
var p = (TokenResource) stateUpdate.getParsed();
var addr = p.addr();
var buf2 = stateUpdate.getStateBuf();
var value = new DatabaseEntry(buf2.array(), buf2.position(), buf2.remaining());
resourceDatabase.putNoOverwrite(txn, new DatabaseEntry(addr.getBytes()), value);
}
// TODO: and stateful reads, move this into a separate store at some point.
if (stateUpdate.getParsed() instanceof VirtualParent) {
var p = (VirtualParent) stateUpdate.getParsed();
var typeByte = p.data()[0];
var mapKey = SystemMapKey.ofSystem(typeByte);
insertIntoMapDatabaseOrFail(txn, mapKey, stateUpdate.getId());
} else if (stateUpdate.getParsed() instanceof ResourceData) {
var p = (ResourceData) stateUpdate.getParsed();
var mapKey = SystemMapKey.ofResourceData(p.addr(), stateUpdate.typeByte());
insertIntoMapDatabaseOrFail(txn, mapKey, stateUpdate.getId());
} else if (stateUpdate.getParsed() instanceof ValidatorData) {
var p = (ValidatorData) stateUpdate.getParsed();
var mapKey = SystemMapKey.ofSystem(stateUpdate.typeByte(), p.validatorKey().getCompressedBytes());
insertIntoMapDatabaseOrFail(txn, mapKey, stateUpdate.getId());
} else if (stateUpdate.getParsed() instanceof SystemData) {
var mapKey = SystemMapKey.ofSystem(stateUpdate.typeByte());
insertIntoMapDatabaseOrFail(txn, mapKey, stateUpdate.getId());
}
} else if (stateUpdate.isShutDown()) {
if (stateUpdate.getId().isVirtual()) {
downVirtualSubstate(txn, stateUpdate.getId());
} else {
downSubstate(txn, stateUpdate.getId());
if (stateUpdate.getParsed() instanceof ResourceData) {
var p = (ResourceData) stateUpdate.getParsed();
var mapKey = SystemMapKey.ofResourceData(p.addr(), stateUpdate.typeByte());
deleteFromMapDatabaseOrFail(txn, mapKey);
} else if (stateUpdate.getParsed() instanceof ValidatorData) {
var p = (ValidatorData) stateUpdate.getParsed();
var mapKey = SystemMapKey.ofSystem(stateUpdate.typeByte(), p.validatorKey().getCompressedBytes());
deleteFromMapDatabaseOrFail(txn, mapKey);
} else if (stateUpdate.getParsed() instanceof SystemData) {
var mapKey = SystemMapKey.ofSystem(stateUpdate.typeByte());
deleteFromMapDatabaseOrFail(txn, mapKey);
}
}
} else {
throw new IllegalStateException("Must bootup or shutdown to update particle: " + stateUpdate);
}
}
use of com.radixdlt.application.system.state.SystemData 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());
}
}
});
}
Aggregations