Search in sources :

Example 1 with TokenResource

use of com.radixdlt.application.tokens.state.TokenResource 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);
    }
}
Also used : ResourceData(com.radixdlt.application.tokens.state.ResourceData) ValidatorData(com.radixdlt.application.validators.state.ValidatorData) TokenResource(com.radixdlt.application.tokens.state.TokenResource) SystemData(com.radixdlt.application.system.state.SystemData) DatabaseEntry(com.sleepycat.je.DatabaseEntry) VirtualParent(com.radixdlt.application.system.state.VirtualParent)

Example 2 with TokenResource

use of com.radixdlt.application.tokens.state.TokenResource 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 3 with TokenResource

use of com.radixdlt.application.tokens.state.TokenResource in project radixdlt by radixdlt.

the class TokensConstraintScryptV3 method registerParticles.

private void registerParticles(Loader os) {
    os.substate(new SubstateDefinition<>(TokenResource.class, SubstateTypeId.TOKEN_RESOURCE.id(), buf -> {
        REFieldSerialization.deserializeReservedByte(buf);
        var addr = REFieldSerialization.deserializeResourceAddr(buf);
        var granularity = REFieldSerialization.deserializeNonZeroUInt256(buf);
        if (!granularity.equals(UInt256.ONE)) {
            throw new DeserializeException("Granularity must be one.");
        }
        var isMutable = REFieldSerialization.deserializeBoolean(buf);
        var minter = REFieldSerialization.deserializeOptionalKey(buf);
        return new TokenResource(addr, granularity, isMutable, minter.orElse(null));
    }, (s, buf) -> {
        REFieldSerialization.serializeReservedByte(buf);
        REFieldSerialization.serializeREAddr(buf, s.addr());
        REFieldSerialization.serializeUInt256(buf, UInt256.ONE);
        REFieldSerialization.serializeBoolean(buf, s.isMutable());
        REFieldSerialization.serializeOptionalKey(buf, s.optionalOwner());
    }));
    os.substate(new SubstateDefinition<>(TokenResourceMetadata.class, SubstateTypeId.TOKEN_RESOURCE_METADATA.id(), buf -> {
        REFieldSerialization.deserializeReservedByte(buf);
        var addr = REFieldSerialization.deserializeResourceAddr(buf);
        var symbol = REFieldSerialization.deserializeString(buf);
        var name = REFieldSerialization.deserializeString(buf);
        var description = REFieldSerialization.deserializeString(buf);
        var url = REFieldSerialization.deserializeUrl(buf);
        var iconUrl = REFieldSerialization.deserializeUrl(buf);
        return new TokenResourceMetadata(addr, symbol, name, description, iconUrl, url);
    }, (s, buf) -> {
        REFieldSerialization.serializeReservedByte(buf);
        REFieldSerialization.serializeREAddr(buf, s.addr());
        REFieldSerialization.serializeString(buf, s.symbol());
        REFieldSerialization.serializeString(buf, s.name());
        REFieldSerialization.serializeString(buf, s.description());
        REFieldSerialization.serializeString(buf, s.url());
        REFieldSerialization.serializeString(buf, s.iconUrl());
    }));
    os.substate(new SubstateDefinition<>(TokensInAccount.class, SubstateTypeId.TOKENS.id(), buf -> {
        REFieldSerialization.deserializeReservedByte(buf);
        var holdingAddr = REFieldSerialization.deserializeAccountREAddr(buf);
        var addr = REFieldSerialization.deserializeResourceAddr(buf);
        var amount = REFieldSerialization.deserializeNonZeroUInt256(buf);
        return new TokensInAccount(holdingAddr, addr, amount);
    }, (s, buf) -> {
        REFieldSerialization.serializeReservedByte(buf);
        REFieldSerialization.serializeREAddr(buf, s.holdingAddress());
        REFieldSerialization.serializeREAddr(buf, s.resourceAddr());
        buf.put(s.amount().toByteArray());
    }));
}
Also used : SubstateTypeId(com.radixdlt.atom.SubstateTypeId) TokenResourceMetadata(com.radixdlt.application.tokens.state.TokenResourceMetadata) Loader(com.radixdlt.atomos.Loader) REFieldSerialization(com.radixdlt.atom.REFieldSerialization) TokenResource(com.radixdlt.application.tokens.state.TokenResource) Set(java.util.Set) TokensInAccount(com.radixdlt.application.tokens.state.TokensInAccount) SubstateDefinition(com.radixdlt.atomos.SubstateDefinition) StandardCharsets(java.nio.charset.StandardCharsets) com.radixdlt.constraintmachine(com.radixdlt.constraintmachine) ResourceCreatedEvent(com.radixdlt.constraintmachine.REEvent.ResourceCreatedEvent) ReservedSymbolException(com.radixdlt.constraintmachine.exceptions.ReservedSymbolException) SystemConstraintScrypt(com.radixdlt.application.system.scrypt.SystemConstraintScrypt) ConstraintScrypt(com.radixdlt.atomos.ConstraintScrypt) ProcedureException(com.radixdlt.constraintmachine.exceptions.ProcedureException) Pattern(java.util.regex.Pattern) DeserializeException(com.radixdlt.serialization.DeserializeException) UInt256(com.radixdlt.utils.UInt256) TokenResource(com.radixdlt.application.tokens.state.TokenResource) TokenResourceMetadata(com.radixdlt.application.tokens.state.TokenResourceMetadata) TokensInAccount(com.radixdlt.application.tokens.state.TokensInAccount) DeserializeException(com.radixdlt.serialization.DeserializeException)

Aggregations

TokenResource (com.radixdlt.application.tokens.state.TokenResource)3 SystemData (com.radixdlt.application.system.state.SystemData)2 VirtualParent (com.radixdlt.application.system.state.VirtualParent)2 ValidatorData (com.radixdlt.application.validators.state.ValidatorData)2 UnsignedBytes (com.google.common.primitives.UnsignedBytes)1 Inject (com.google.inject.Inject)1 SystemConstraintScrypt (com.radixdlt.application.system.scrypt.SystemConstraintScrypt)1 ResourceData (com.radixdlt.application.tokens.state.ResourceData)1 TokenResourceMetadata (com.radixdlt.application.tokens.state.TokenResourceMetadata)1 TokensInAccount (com.radixdlt.application.tokens.state.TokensInAccount)1 CloseableCursor (com.radixdlt.atom.CloseableCursor)1 REFieldSerialization (com.radixdlt.atom.REFieldSerialization)1 SubstateId (com.radixdlt.atom.SubstateId)1 SubstateTypeId (com.radixdlt.atom.SubstateTypeId)1 ConstraintScrypt (com.radixdlt.atomos.ConstraintScrypt)1 Loader (com.radixdlt.atomos.Loader)1 SubstateDefinition (com.radixdlt.atomos.SubstateDefinition)1 com.radixdlt.constraintmachine (com.radixdlt.constraintmachine)1 ResourceCreatedEvent (com.radixdlt.constraintmachine.REEvent.ResourceCreatedEvent)1 REProcessedTxn (com.radixdlt.constraintmachine.REProcessedTxn)1