Search in sources :

Example 6 with SubstateDefinition

use of com.radixdlt.atomos.SubstateDefinition 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)

Example 7 with SubstateDefinition

use of com.radixdlt.atomos.SubstateDefinition in project radixdlt by radixdlt.

the class ValidatorUpdateOwnerConstraintScrypt method main.

@Override
public void main(Loader os) {
    os.substate(new SubstateDefinition<>(ValidatorOwnerCopy.class, SubstateTypeId.VALIDATOR_OWNER_COPY.id(), buf -> {
        REFieldSerialization.deserializeReservedByte(buf);
        OptionalLong epochUpdate = REFieldSerialization.deserializeOptionalNonNegativeLong(buf);
        var key = REFieldSerialization.deserializeKey(buf);
        var owner = REFieldSerialization.deserializeAccountREAddr(buf);
        return new ValidatorOwnerCopy(epochUpdate, key, owner);
    }, (s, buf) -> {
        REFieldSerialization.serializeReservedByte(buf);
        REFieldSerialization.serializeOptionalLong(buf, s.epochUpdate());
        REFieldSerialization.serializeKey(buf, s.validatorKey());
        REFieldSerialization.serializeREAddr(buf, s.owner());
    }, buf -> REFieldSerialization.deserializeKey(buf), (k, buf) -> REFieldSerialization.serializeKey(buf, (ECPublicKey) k), k -> ValidatorOwnerCopy.createVirtual((ECPublicKey) k)));
    os.procedure(new DownProcedure<>(VoidReducerState.class, ValidatorOwnerCopy.class, d -> new Authorization(PermissionLevel.USER, (r, c) -> {
        if (!c.key().map(d.validatorKey()::equals).orElse(false)) {
            throw new AuthorizationException("Key does not match.");
        }
    }), (d, s, r, c) -> ReducerResult.incomplete(new UpdatingOwnerNeedToReadEpoch(d.validatorKey()))));
    os.procedure(new ReadProcedure<>(UpdatingOwnerNeedToReadEpoch.class, EpochData.class, u -> new Authorization(PermissionLevel.USER, (r, c) -> {
    }), (s, u, r) -> ReducerResult.incomplete(s.readEpoch(u))));
    os.procedure(new UpProcedure<>(UpdatingValidatorOwner.class, ValidatorOwnerCopy.class, u -> new Authorization(PermissionLevel.USER, (r, c) -> {
    }), (s, u, c, r) -> {
        s.update(u);
        return ReducerResult.complete();
    }));
}
Also used : SubstateTypeId(com.radixdlt.atom.SubstateTypeId) AuthorizationException(com.radixdlt.constraintmachine.exceptions.AuthorizationException) ReducerResult(com.radixdlt.constraintmachine.ReducerResult) ReadProcedure(com.radixdlt.constraintmachine.ReadProcedure) VoidReducerState(com.radixdlt.constraintmachine.VoidReducerState) Loader(com.radixdlt.atomos.Loader) ReducerState(com.radixdlt.constraintmachine.ReducerState) REFieldSerialization(com.radixdlt.atom.REFieldSerialization) Authorization(com.radixdlt.constraintmachine.Authorization) SubstateDefinition(com.radixdlt.atomos.SubstateDefinition) PermissionLevel(com.radixdlt.constraintmachine.PermissionLevel) ECPublicKey(com.radixdlt.crypto.ECPublicKey) OptionalLong(java.util.OptionalLong) UpProcedure(com.radixdlt.constraintmachine.UpProcedure) ConstraintScrypt(com.radixdlt.atomos.ConstraintScrypt) ValidatorOwnerCopy(com.radixdlt.application.validators.state.ValidatorOwnerCopy) DownProcedure(com.radixdlt.constraintmachine.DownProcedure) ProcedureException(com.radixdlt.constraintmachine.exceptions.ProcedureException) EpochData(com.radixdlt.application.system.state.EpochData) EpochData(com.radixdlt.application.system.state.EpochData) VoidReducerState(com.radixdlt.constraintmachine.VoidReducerState) AuthorizationException(com.radixdlt.constraintmachine.exceptions.AuthorizationException) Authorization(com.radixdlt.constraintmachine.Authorization) ValidatorOwnerCopy(com.radixdlt.application.validators.state.ValidatorOwnerCopy) OptionalLong(java.util.OptionalLong)

Aggregations

REFieldSerialization (com.radixdlt.atom.REFieldSerialization)7 SubstateTypeId (com.radixdlt.atom.SubstateTypeId)7 ConstraintScrypt (com.radixdlt.atomos.ConstraintScrypt)7 Loader (com.radixdlt.atomos.Loader)7 SubstateDefinition (com.radixdlt.atomos.SubstateDefinition)7 ProcedureException (com.radixdlt.constraintmachine.exceptions.ProcedureException)7 ECPublicKey (com.radixdlt.crypto.ECPublicKey)6 Authorization (com.radixdlt.constraintmachine.Authorization)5 DownProcedure (com.radixdlt.constraintmachine.DownProcedure)5 PermissionLevel (com.radixdlt.constraintmachine.PermissionLevel)5 ReducerResult (com.radixdlt.constraintmachine.ReducerResult)5 ReducerState (com.radixdlt.constraintmachine.ReducerState)5 UpProcedure (com.radixdlt.constraintmachine.UpProcedure)5 VoidReducerState (com.radixdlt.constraintmachine.VoidReducerState)5 EpochData (com.radixdlt.application.system.state.EpochData)4 ReadProcedure (com.radixdlt.constraintmachine.ReadProcedure)4 ValidatorOwnerCopy (com.radixdlt.application.validators.state.ValidatorOwnerCopy)3 AuthorizationException (com.radixdlt.constraintmachine.exceptions.AuthorizationException)3 UInt256 (com.radixdlt.utils.UInt256)3 OptionalLong (java.util.OptionalLong)3