use of com.radixdlt.constraintmachine.ReadProcedure 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()));
}
use of com.radixdlt.constraintmachine.ReadProcedure in project radixdlt by radixdlt.
the class ValidatorRegisterConstraintScrypt method main.
@Override
public void main(Loader os) {
os.substate(new SubstateDefinition<>(ValidatorRegisteredCopy.class, SubstateTypeId.VALIDATOR_REGISTERED_FLAG_COPY.id(), buf -> {
REFieldSerialization.deserializeReservedByte(buf);
var epochUpdate = REFieldSerialization.deserializeOptionalNonNegativeLong(buf);
var key = REFieldSerialization.deserializeKey(buf);
var flag = REFieldSerialization.deserializeBoolean(buf);
return new ValidatorRegisteredCopy(epochUpdate, key, flag);
}, (s, buf) -> {
REFieldSerialization.serializeReservedByte(buf);
REFieldSerialization.serializeOptionalLong(buf, s.epochUpdate());
REFieldSerialization.serializeKey(buf, s.validatorKey());
buf.put((byte) (s.isRegistered() ? 1 : 0));
}, buf -> REFieldSerialization.deserializeKey(buf), (k, buf) -> REFieldSerialization.serializeKey(buf, (ECPublicKey) k), k -> new ValidatorRegisteredCopy(OptionalLong.empty(), (ECPublicKey) k, false)));
os.procedure(new DownProcedure<>(VoidReducerState.class, ValidatorRegisteredCopy.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) -> {
return ReducerResult.incomplete(new UpdatingRegisteredNeedToReadEpoch(d.validatorKey()));
}));
os.procedure(new ReadProcedure<>(UpdatingRegisteredNeedToReadEpoch.class, EpochData.class, u -> new Authorization(PermissionLevel.USER, (r, c) -> {
}), (s, u, r) -> ReducerResult.incomplete(s.readEpoch(u))));
os.procedure(new UpProcedure<>(UpdatingRegistered.class, ValidatorRegisteredCopy.class, u -> new Authorization(PermissionLevel.USER, (r, c) -> {
}), (s, u, c, r) -> {
s.update(u);
return ReducerResult.complete();
}));
}
use of com.radixdlt.constraintmachine.ReadProcedure in project radixdlt by radixdlt.
the class ValidatorUpdateRakeConstraintScrypt method main.
@Override
public void main(Loader os) {
os.substate(new SubstateDefinition<>(ValidatorFeeCopy.class, SubstateTypeId.VALIDATOR_RAKE_COPY.id(), buf -> {
REFieldSerialization.deserializeReservedByte(buf);
OptionalLong epochUpdate = REFieldSerialization.deserializeOptionalNonNegativeLong(buf);
var key = REFieldSerialization.deserializeKey(buf);
var curRakePercentage = REFieldSerialization.deserializeInt(buf);
if (curRakePercentage < RAKE_MIN || curRakePercentage > RAKE_MAX) {
throw new DeserializeException("Invalid rake percentage " + curRakePercentage);
}
return new ValidatorFeeCopy(epochUpdate, key, curRakePercentage);
}, (s, buf) -> {
REFieldSerialization.serializeReservedByte(buf);
REFieldSerialization.serializeOptionalLong(buf, s.epochUpdate());
REFieldSerialization.serializeKey(buf, s.validatorKey());
buf.putInt(s.curRakePercentage());
}, buf -> REFieldSerialization.deserializeKey(buf), (k, buf) -> REFieldSerialization.serializeKey(buf, (ECPublicKey) k), k -> ValidatorFeeCopy.createVirtual((ECPublicKey) k)));
os.procedure(new DownProcedure<>(VoidReducerState.class, ValidatorFeeCopy.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) -> {
return ReducerResult.incomplete(new UpdatingRakeNeedToReadCurrentRake(d.validatorKey()));
}));
os.procedure(new ReadProcedure<>(UpdatingRakeNeedToReadEpoch.class, EpochData.class, u -> new Authorization(PermissionLevel.USER, (r, c) -> {
}), (s, u, r) -> ReducerResult.incomplete(s.readEpoch(u))));
os.procedure(new ReadProcedure<>(UpdatingRakeNeedToReadCurrentRake.class, ValidatorStakeData.class, u -> new Authorization(PermissionLevel.USER, (r, c) -> {
}), (s, u, r) -> ReducerResult.incomplete(s.readValidatorStakeState(u))));
os.procedure(new UpProcedure<>(UpdatingRakeReady.class, ValidatorFeeCopy.class, u -> new Authorization(PermissionLevel.USER, (r, c) -> {
}), (s, u, c, r) -> {
s.update(u);
return ReducerResult.complete();
}));
}
use of com.radixdlt.constraintmachine.ReadProcedure 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();
}));
}
Aggregations