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);
}
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();
}
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();
}
Aggregations