Search in sources :

Example 1 with StakeOwnership

use of com.radixdlt.application.system.state.StakeOwnership in project radixdlt by radixdlt.

the class EpochUpdateConstraintScrypt method main.

@Override
public void main(Loader os) {
    os.substate(new SubstateDefinition<>(ValidatorStakeData.class, SubstateTypeId.VALIDATOR_STAKE_DATA.id(), buf -> {
        REFieldSerialization.deserializeReservedByte(buf);
        var isRegistered = REFieldSerialization.deserializeBoolean(buf);
        var amount = REFieldSerialization.deserializeUInt256(buf);
        var delegate = REFieldSerialization.deserializeKey(buf);
        var ownership = REFieldSerialization.deserializeUInt256(buf);
        var rakePercentage = REFieldSerialization.deserializeInt(buf);
        var ownerAddress = REFieldSerialization.deserializeAccountREAddr(buf);
        return ValidatorStakeData.create(delegate, amount, ownership, rakePercentage, ownerAddress, isRegistered);
    }, (s, buf) -> {
        REFieldSerialization.serializeReservedByte(buf);
        REFieldSerialization.serializeBoolean(buf, s.isRegistered());
        buf.put(s.amount().toByteArray());
        REFieldSerialization.serializeKey(buf, s.validatorKey());
        buf.put(s.totalOwnership().toByteArray());
        buf.putInt(s.rakePercentage());
        REFieldSerialization.serializeREAddr(buf, s.ownerAddr());
    }, buf -> REFieldSerialization.deserializeKey(buf), (k, buf) -> REFieldSerialization.serializeKey(buf, (ECPublicKey) k), k -> ValidatorStakeData.createVirtual((ECPublicKey) k)));
    os.substate(new SubstateDefinition<>(StakeOwnership.class, SubstateTypeId.STAKE_OWNERSHIP.id(), buf -> {
        REFieldSerialization.deserializeReservedByte(buf);
        var delegate = REFieldSerialization.deserializeKey(buf);
        var owner = REFieldSerialization.deserializeAccountREAddr(buf);
        var amount = REFieldSerialization.deserializeNonZeroUInt256(buf);
        return new StakeOwnership(delegate, owner, amount);
    }, (s, buf) -> {
        REFieldSerialization.serializeReservedByte(buf);
        REFieldSerialization.serializeKey(buf, s.delegateKey());
        REFieldSerialization.serializeREAddr(buf, s.owner());
        buf.put(s.amount().toByteArray());
    }));
    os.substate(new SubstateDefinition<>(ExitingStake.class, SubstateTypeId.EXITING_STAKE.id(), buf -> {
        REFieldSerialization.deserializeReservedByte(buf);
        var epochUnlocked = REFieldSerialization.deserializeNonNegativeLong(buf);
        var delegate = REFieldSerialization.deserializeKey(buf);
        var owner = REFieldSerialization.deserializeAccountREAddr(buf);
        var amount = REFieldSerialization.deserializeNonZeroUInt256(buf);
        return new ExitingStake(epochUnlocked, delegate, owner, amount);
    }, (s, buf) -> {
        REFieldSerialization.serializeReservedByte(buf);
        buf.putLong(s.epochUnlocked());
        REFieldSerialization.serializeKey(buf, s.delegateKey());
        REFieldSerialization.serializeREAddr(buf, s.owner());
        buf.put(s.amount().toByteArray());
    }));
    registerGenesisTransitions(os);
    // Epoch update
    epochUpdate(os);
}
Also used : SubstateTypeId(com.radixdlt.atom.SubstateTypeId) java.util(java.util) StakeOwnership(com.radixdlt.application.system.state.StakeOwnership) PreparedUnstakeOwnership(com.radixdlt.application.tokens.state.PreparedUnstakeOwnership) ValidatorStakeData(com.radixdlt.application.system.state.ValidatorStakeData) NextValidatorSetEvent(com.radixdlt.constraintmachine.REEvent.NextValidatorSetEvent) ValidatorRegisteredCopy(com.radixdlt.application.validators.state.ValidatorRegisteredCopy) MismatchException(com.radixdlt.constraintmachine.exceptions.MismatchException) HasEpochData(com.radixdlt.application.system.state.HasEpochData) ExitingStake(com.radixdlt.application.tokens.state.ExitingStake) SubstateDefinition(com.radixdlt.atomos.SubstateDefinition) ECPublicKey(com.radixdlt.crypto.ECPublicKey) Function(java.util.function.Function) Supplier(java.util.function.Supplier) RoundData(com.radixdlt.application.system.state.RoundData) Longs(com.radixdlt.utils.Longs) ConstraintScrypt(com.radixdlt.atomos.ConstraintScrypt) ValidatorOwnerCopy(com.radixdlt.application.validators.state.ValidatorOwnerCopy) ProcedureException(com.radixdlt.constraintmachine.exceptions.ProcedureException) EpochData(com.radixdlt.application.system.state.EpochData) ValidatorBFTData(com.radixdlt.application.system.state.ValidatorBFTData) UInt256(com.radixdlt.utils.UInt256) UnsignedBytes(com.google.common.primitives.UnsignedBytes) PreparedStake(com.radixdlt.application.tokens.state.PreparedStake) Loader(com.radixdlt.atomos.Loader) REFieldSerialization(com.radixdlt.atom.REFieldSerialization) TokensInAccount(com.radixdlt.application.tokens.state.TokensInAccount) REAddr(com.radixdlt.identifiers.REAddr) Streams(com.google.common.collect.Streams) Collectors(java.util.stream.Collectors) com.radixdlt.constraintmachine(com.radixdlt.constraintmachine) RAKE_MAX(com.radixdlt.application.validators.scrypt.ValidatorUpdateRakeConstraintScrypt.RAKE_MAX) ValidatorFeeCopy(com.radixdlt.application.validators.state.ValidatorFeeCopy) KeyComparator(com.radixdlt.utils.KeyComparator) ValidatorBFTDataEvent(com.radixdlt.constraintmachine.REEvent.ValidatorBFTDataEvent) StakeOwnership(com.radixdlt.application.system.state.StakeOwnership) ValidatorStakeData(com.radixdlt.application.system.state.ValidatorStakeData) ExitingStake(com.radixdlt.application.tokens.state.ExitingStake)

Example 2 with StakeOwnership

use of com.radixdlt.application.system.state.StakeOwnership 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 StakeOwnership

use of com.radixdlt.application.system.state.StakeOwnership 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 Streams (com.google.common.collect.Streams)1 UnsignedBytes (com.google.common.primitives.UnsignedBytes)1 EpochData (com.radixdlt.application.system.state.EpochData)1 HasEpochData (com.radixdlt.application.system.state.HasEpochData)1 RoundData (com.radixdlt.application.system.state.RoundData)1 ValidatorBFTData (com.radixdlt.application.system.state.ValidatorBFTData)1 ValidatorStakeData (com.radixdlt.application.system.state.ValidatorStakeData)1 ExitingStake (com.radixdlt.application.tokens.state.ExitingStake)1 PreparedStake (com.radixdlt.application.tokens.state.PreparedStake)1 TokensInAccount (com.radixdlt.application.tokens.state.TokensInAccount)1 RAKE_MAX (com.radixdlt.application.validators.scrypt.ValidatorUpdateRakeConstraintScrypt.RAKE_MAX)1 ValidatorFeeCopy (com.radixdlt.application.validators.state.ValidatorFeeCopy)1 ValidatorOwnerCopy (com.radixdlt.application.validators.state.ValidatorOwnerCopy)1 ValidatorRegisteredCopy (com.radixdlt.application.validators.state.ValidatorRegisteredCopy)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