Search in sources :

Example 1 with PreparedUnstakeOwnership

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

the class StakingConstraintScryptV4 method main.

@Override
public void main(Loader os) {
    os.substate(new SubstateDefinition<>(PreparedStake.class, SubstateTypeId.PREPARED_STAKE.id(), buf -> {
        REFieldSerialization.deserializeReservedByte(buf);
        var delegate = REFieldSerialization.deserializeKey(buf);
        var owner = REFieldSerialization.deserializeAccountREAddr(buf);
        var amount = REFieldSerialization.deserializeNonZeroUInt256(buf);
        return new PreparedStake(amount, owner, delegate);
    }, (s, buf) -> {
        REFieldSerialization.serializeReservedByte(buf);
        REFieldSerialization.serializeKey(buf, s.delegateKey());
        REFieldSerialization.serializeREAddr(buf, s.owner());
        buf.put(s.amount().toByteArray());
    }));
    os.substate(new SubstateDefinition<>(PreparedUnstakeOwnership.class, SubstateTypeId.PREPARED_UNSTAKE.id(), buf -> {
        REFieldSerialization.deserializeReservedByte(buf);
        var delegate = REFieldSerialization.deserializeKey(buf);
        var owner = REFieldSerialization.deserializeAccountREAddr(buf);
        var amount = REFieldSerialization.deserializeNonZeroUInt256(buf);
        return new PreparedUnstakeOwnership(delegate, owner, amount);
    }, (s, buf) -> {
        REFieldSerialization.serializeReservedByte(buf);
        REFieldSerialization.serializeKey(buf, s.delegateKey());
        REFieldSerialization.serializeREAddr(buf, s.owner());
        buf.put(s.amount().toByteArray());
    }));
    defineStaking(os);
}
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) PreparedStake(com.radixdlt.application.tokens.state.PreparedStake) PreparedUnstakeOwnership(com.radixdlt.application.tokens.state.PreparedUnstakeOwnership)

Example 2 with PreparedUnstakeOwnership

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

the class UnstakeOwnershipConstructor method construct.

@Override
public void construct(UnstakeOwnership action, TxBuilder txBuilder) throws TxBuilderException {
    var buf = ByteBuffer.allocate(2 + ECPublicKey.COMPRESSED_BYTES + (1 + ECPublicKey.COMPRESSED_BYTES));
    buf.put(SubstateTypeId.STAKE_OWNERSHIP.id());
    buf.put((byte) 0);
    buf.put(action.fromDelegate().getCompressedBytes());
    buf.put(action.accountAddr().getBytes());
    if (buf.hasRemaining()) {
        // Sanity
        throw new IllegalStateException();
    }
    var index = SubstateIndex.create(buf.array(), StakeOwnership.class);
    var change = txBuilder.downFungible(index, p -> p.owner().equals(action.accountAddr()) && p.delegateKey().equals(action.fromDelegate()), action.amount(), available -> {
        var from = StakeOwnershipBucket.from(action.fromDelegate(), action.accountAddr());
        return new NotEnoughResourcesException(from, action.amount(), available);
    });
    if (!change.isZero()) {
        txBuilder.up(new StakeOwnership(action.fromDelegate(), action.accountAddr(), change));
    }
    txBuilder.up(new PreparedUnstakeOwnership(action.fromDelegate(), action.accountAddr(), action.amount()));
    txBuilder.end();
}
Also used : StakeOwnership(com.radixdlt.application.system.state.StakeOwnership) PreparedUnstakeOwnership(com.radixdlt.application.tokens.state.PreparedUnstakeOwnership)

Example 3 with PreparedUnstakeOwnership

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

the class UnstakeTokensConstructorV2 method construct.

@Override
public void construct(UnstakeTokens action, TxBuilder txBuilder) throws TxBuilderException {
    var validatorStake = txBuilder.find(ValidatorStakeData.class, action.fromDelegate());
    var ownershipAmt = action.amount().multiply(validatorStake.totalOwnership()).divide(validatorStake.amount());
    // TODO: construct this in substate definition
    var buf = ByteBuffer.allocate(2 + ECPublicKey.COMPRESSED_BYTES + (1 + ECPublicKey.COMPRESSED_BYTES));
    buf.put(SubstateTypeId.STAKE_OWNERSHIP.id());
    buf.put((byte) 0);
    buf.put(action.fromDelegate().getCompressedBytes());
    buf.put(action.accountAddr().getBytes());
    if (buf.hasRemaining()) {
        // Sanity
        throw new IllegalStateException();
    }
    var index = SubstateIndex.create(buf.array(), StakeOwnership.class);
    var change = txBuilder.downFungible(index, p -> p.owner().equals(action.accountAddr()) && p.delegateKey().equals(action.fromDelegate()), ownershipAmt, available -> {
        var from = StakeOwnershipBucket.from(action.fromDelegate(), action.accountAddr());
        return new NotEnoughResourcesException(from, action.amount(), available);
    });
    if (!change.isZero()) {
        txBuilder.up(new StakeOwnership(action.fromDelegate(), action.accountAddr(), change));
    }
    txBuilder.up(new PreparedUnstakeOwnership(action.fromDelegate(), action.accountAddr(), ownershipAmt));
    txBuilder.end();
}
Also used : StakeOwnership(com.radixdlt.application.system.state.StakeOwnership) PreparedUnstakeOwnership(com.radixdlt.application.tokens.state.PreparedUnstakeOwnership)

Aggregations

StakeOwnership (com.radixdlt.application.system.state.StakeOwnership)3 PreparedUnstakeOwnership (com.radixdlt.application.tokens.state.PreparedUnstakeOwnership)3 PreparedStake (com.radixdlt.application.tokens.state.PreparedStake)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