Search in sources :

Example 1 with EndProcedure

use of com.radixdlt.constraintmachine.EndProcedure in project radixdlt by radixdlt.

the class StakingConstraintScryptV4 method defineStaking.

private void defineStaking(Loader os) {
    // Stake
    os.procedure(new ReadProcedure<>(TokenHoldingBucket.class, AllowDelegationFlag.class, u -> new Authorization(PermissionLevel.USER, (r, c) -> {
    }), (s, d, r) -> {
        var nextState = (!d.allowsDelegation()) ? new OwnerStakePrepare(s, d) : new StakePrepare(s, d.validatorKey(), p -> true);
        return ReducerResult.incomplete(nextState);
    }));
    os.procedure(new ReadProcedure<>(OwnerStakePrepare.class, ValidatorOwnerCopy.class, u -> new Authorization(PermissionLevel.USER, (r, c) -> {
    }), (s, d, r) -> {
        var nextState = s.readOwner(d);
        return ReducerResult.incomplete(nextState);
    }));
    os.procedure(new UpProcedure<>(StakePrepare.class, PreparedStake.class, u -> new Authorization(PermissionLevel.USER, (r, c) -> {
    }), (s, u, c, r) -> {
        var nextState = s.withdrawTo(u);
        return ReducerResult.incomplete(nextState);
    }));
    // Unstake
    os.procedure(new DownProcedure<>(VoidReducerState.class, StakeOwnership.class, d -> d.bucket().withdrawAuthorization(), (d, s, r, c) -> ReducerResult.incomplete(new StakeOwnershipHoldingBucket(d))));
    // Additional Unstake
    os.procedure(new DownProcedure<>(StakeOwnershipHoldingBucket.class, StakeOwnership.class, d -> d.bucket().withdrawAuthorization(), (d, s, r, c) -> {
        s.depositOwnership(d);
        return ReducerResult.incomplete(s);
    }));
    // Change
    os.procedure(new UpProcedure<>(StakeOwnershipHoldingBucket.class, StakeOwnership.class, u -> new Authorization(PermissionLevel.USER, (r, c) -> {
    }), (s, u, c, r) -> {
        var ownership = s.withdrawOwnership(u.amount());
        if (!ownership.equals(u)) {
            throw new MismatchException(ownership, u);
        }
        return ReducerResult.incomplete(s);
    }));
    os.procedure(new UpProcedure<>(StakeOwnershipHoldingBucket.class, PreparedUnstakeOwnership.class, u -> new Authorization(PermissionLevel.USER, (r, c) -> {
    }), (s, u, c, r) -> {
        var unstake = s.unstake(u.amount());
        if (!unstake.equals(u)) {
            throw new MismatchException(unstake, u);
        }
        return ReducerResult.incomplete(s);
    }));
    // Deallocate Stake Holding Bucket
    os.procedure(new EndProcedure<>(StakeOwnershipHoldingBucket.class, s -> new Authorization(PermissionLevel.USER, (r, c) -> {
    }), (s, c, r) -> s.destroy()));
}
Also used : SubstateTypeId(com.radixdlt.atom.SubstateTypeId) EndProcedure(com.radixdlt.constraintmachine.EndProcedure) StakeOwnership(com.radixdlt.application.system.state.StakeOwnership) PreparedUnstakeOwnership(com.radixdlt.application.tokens.state.PreparedUnstakeOwnership) VoidReducerState(com.radixdlt.constraintmachine.VoidReducerState) NotEnoughResourcesException(com.radixdlt.constraintmachine.exceptions.NotEnoughResourcesException) AllowDelegationFlag(com.radixdlt.application.validators.state.AllowDelegationFlag) InvalidDelegationException(com.radixdlt.constraintmachine.exceptions.InvalidDelegationException) Authorization(com.radixdlt.constraintmachine.Authorization) MismatchException(com.radixdlt.constraintmachine.exceptions.MismatchException) SubstateDefinition(com.radixdlt.atomos.SubstateDefinition) PermissionLevel(com.radixdlt.constraintmachine.PermissionLevel) ECPublicKey(com.radixdlt.crypto.ECPublicKey) 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) UInt256(com.radixdlt.utils.UInt256) InvalidResourceException(com.radixdlt.constraintmachine.exceptions.InvalidResourceException) PreparedStake(com.radixdlt.application.tokens.state.PreparedStake) ReducerResult(com.radixdlt.constraintmachine.ReducerResult) ReadProcedure(com.radixdlt.constraintmachine.ReadProcedure) Loader(com.radixdlt.atomos.Loader) ReducerState(com.radixdlt.constraintmachine.ReducerState) REFieldSerialization(com.radixdlt.atom.REFieldSerialization) Predicate(java.util.function.Predicate) REAddr(com.radixdlt.identifiers.REAddr) MinimumStakeException(com.radixdlt.constraintmachine.exceptions.MinimumStakeException) StakeOwnership(com.radixdlt.application.system.state.StakeOwnership) PreparedStake(com.radixdlt.application.tokens.state.PreparedStake) VoidReducerState(com.radixdlt.constraintmachine.VoidReducerState) Authorization(com.radixdlt.constraintmachine.Authorization) PreparedUnstakeOwnership(com.radixdlt.application.tokens.state.PreparedUnstakeOwnership) ValidatorOwnerCopy(com.radixdlt.application.validators.state.ValidatorOwnerCopy) MismatchException(com.radixdlt.constraintmachine.exceptions.MismatchException) AllowDelegationFlag(com.radixdlt.application.validators.state.AllowDelegationFlag)

Aggregations

StakeOwnership (com.radixdlt.application.system.state.StakeOwnership)1 PreparedStake (com.radixdlt.application.tokens.state.PreparedStake)1 PreparedUnstakeOwnership (com.radixdlt.application.tokens.state.PreparedUnstakeOwnership)1 AllowDelegationFlag (com.radixdlt.application.validators.state.AllowDelegationFlag)1 ValidatorOwnerCopy (com.radixdlt.application.validators.state.ValidatorOwnerCopy)1 REFieldSerialization (com.radixdlt.atom.REFieldSerialization)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 Authorization (com.radixdlt.constraintmachine.Authorization)1 DownProcedure (com.radixdlt.constraintmachine.DownProcedure)1 EndProcedure (com.radixdlt.constraintmachine.EndProcedure)1 PermissionLevel (com.radixdlt.constraintmachine.PermissionLevel)1 ReadProcedure (com.radixdlt.constraintmachine.ReadProcedure)1 ReducerResult (com.radixdlt.constraintmachine.ReducerResult)1 ReducerState (com.radixdlt.constraintmachine.ReducerState)1 UpProcedure (com.radixdlt.constraintmachine.UpProcedure)1 VoidReducerState (com.radixdlt.constraintmachine.VoidReducerState)1 InvalidDelegationException (com.radixdlt.constraintmachine.exceptions.InvalidDelegationException)1