Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ZKS-04] Track committee size. #2213

Merged
merged 20 commits into from
Feb 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
201 changes: 136 additions & 65 deletions synthesizer/program/src/resources/credits.aleo
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,19 @@ struct committee_state:

/**********************************************************************************************************************/

/// The `metadata` mapping stores:
/// - The number of members in the committee.
/// - The number of delegators.
mapping metadata:
// The key represents the index at which the count is stored.
// - This address (aleo1qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq3ljyzc) stores the number of **members** in the committee.
// - This address (aleo1qgqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqanmpl0) stores the number of **delegators**.
key as address.public;
// The value represents the count.
value as u32.public;

/**********************************************************************************************************************/

// The `bonded` mapping represents the amount of microcredits that are currently bonded.
mapping bonded:
// The key represents the address of the staker, which includes the validators and their delegators.
Expand Down Expand Up @@ -124,54 +137,73 @@ finalize bond_public:

/* Committee */

// Check if the validator is already in the committee.
contains committee[r0] into r4;
// If the validator is already in the committee, jump to the `continue_bond_validator` logic.
branch.eq r4 true to continue_bond_validator;

// Get the committee size.
get.or_use metadata[aleo1qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq3ljyzc] 0u32 into r5;
// Increment the committee size by one.
add r5 1u32 into r6;
// Determine if the committee size is less than or equal to 200.
lte r6 200u32 into r7;
// Enforce that the committee size is less than or equal to 200.
assert.eq r7 true;
// Set the new committee size.
set r6 into metadata[aleo1qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq3ljyzc];

// Continues the rest of the `bond_validator` logic.
position continue_bond_validator;

// Construct the initial committee state.
// Note: We set the initial 'is_open' state to 'true'.
cast 0u64 true into r4 as committee_state;
cast 0u64 true into r8 as committee_state;
// Retrieve the committee state of the specified validator.
get.or_use committee[r0] r4 into r5;
get.or_use committee[r0] r8 into r9;
// Ensure that the validator is open to stakers.
assert.eq r5.is_open true;
assert.eq r9.is_open true;

// Increment the stake for the specified validator.
add r5.microcredits r2 into r6;
add r9.microcredits r2 into r10;
// Construct the updated committee state.
cast r6 r5.is_open into r7 as committee_state;
cast r10 r9.is_open into r11 as committee_state;

/* Bonded */

// Construct the initial bond state.
cast r1 0u64 into r8 as bond_state;
cast r0 0u64 into r12 as bond_state;
// Get the bond state for the caller, or default to the initial bond state.
get.or_use bonded[r0] r8 into r9;
get.or_use bonded[r0] r12 into r13;
// Enforce the validator matches in the bond state.
assert.eq r9.validator r1;
assert.eq r13.validator r0;

// Increment the microcredits in the bond state.
add r9.microcredits r2 into r10;
add r9.microcredits r2 into r14;
// Determine if the amount is at least one million credits.
gte r10 1_000_000_000_000u64 into r11;
gte r14 1_000_000_000_000u64 into r15;
// Enforce the amount is at least one million credits.
assert.eq r11 true;
assert.eq r15 true;

// Construct the updated bond state.
cast r1 r10 into r12 as bond_state;
cast r0 r14 into r16 as bond_state;

/* Account */

// Get the balance of the caller.
// If the account does not exist, this finalize scope will fail.
get account[r0] into r13;
get account[r0] into r17;
// Decrement the balance of the caller.
sub r13 r2 into r14;
sub r17 r2 into r18;

/* Writes */

// Update the committee state of the specified validator.
set r7 into committee[r0];
set r11 into committee[r0];
// Update the bond state for the caller.
set r12 into bonded[r0];
set r16 into bonded[r0];
// Update the balance of the caller.
set r14 into account[r0];
set r18 into account[r0];

// Ends the `bond_validator` logic.
branch.eq true true to end;
Expand All @@ -184,56 +216,74 @@ finalize bond_public:
/* Committee */

// Check if the caller is a validator.
contains committee[r0] into r15;
contains committee[r0] into r19;
// Enforce the caller is *not* a validator.
assert.eq r15 false;
assert.eq r19 false;

// Get the stake for the specified validator.
// If the validator does not exist, this finalize scope will fail.
get committee[r1] into r16;
get committee[r1] into r20;
// Ensure that the validator is open to stakers.
assert.eq r16.is_open true;
assert.eq r20.is_open true;

// Increment the stake for the specified validator.
add r16.microcredits r2 into r17;
add r20.microcredits r2 into r21;
// Construct the updated committee state.
cast r17 r16.is_open into r18 as committee_state;
cast r21 r20.is_open into r22 as committee_state;

// Check if the delegator is already bonded to the validator.
contains bonded[r0] into r23;
// If the delegator is already bonded to the validator, jump to the `continue_bond_delegator` logic.
branch.eq r23 true to continue_bond_delegator;
// Get the number of delegators.
get.or_use metadata[aleo1qgqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqanmpl0] 0u32 into r24;
// Increment the number of bonded delegators by one.
add r24 1u32 into r25;
// Determine if the number of delegators is less than or equal to 100_000.
lte r25 100_000u32 into r26;
// Enforce that the number of delegators is less than or equal to 100_000.
assert.eq r26 true;
// Set the new number of delegators.
set r25 into metadata[aleo1qgqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqanmpl0];

// Continues the rest of the `bond_delegator` logic.
position continue_bond_delegator;

/* Bonded */

// Construct the initial bond state.
cast r1 0u64 into r19 as bond_state;
cast r1 0u64 into r27 as bond_state;
// Get the bond state for the caller, or default to the initial bond state.
get.or_use bonded[r0] r19 into r20;
get.or_use bonded[r0] r27 into r28;
// Enforce the validator matches in the bond state.
assert.eq r20.validator r1;
assert.eq r28.validator r1;

// Increment the microcredits in the bond state.
add r20.microcredits r2 into r21;
add r28.microcredits r2 into r29;
// Determine if the amount is at least 10 credits.
gte r21 10_000_000u64 into r22;
gte r29 10_000_000u64 into r30;
// Enforce the amount is at least 10 credits.
assert.eq r22 true;
assert.eq r30 true;

// Construct the updated bond state.
cast r1 r21 into r23 as bond_state;
cast r1 r29 into r31 as bond_state;

/* Account */

// Get the balance of the caller.
// If the account does not exist, this finalize scope will fail.
get account[r0] into r24;
get account[r0] into r32;
// Decrement the balance of the caller.
sub r24 r2 into r25;
sub r32 r2 into r33;

/* Writes */

// Update the committee state for the specified validator.
set r18 into committee[r1];
set r22 into committee[r1];
// Update the bond state for the caller.
set r23 into bonded[r0];
set r31 into bonded[r0];
// Update the balance of the caller.
set r25 into account[r0];
set r33 into account[r0];

// The terminus.
position end;
Expand Down Expand Up @@ -348,6 +398,13 @@ finalize unbond_public:
// Remove the validator from the committee.
remove committee[r0];

// Get the committee size.
get metadata[aleo1qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq3ljyzc] into r15;
// Decrement the committee size by one.
sub r15 1u32 into r16;
// Set the new committee size.
set r16 into metadata[aleo1qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq3ljyzc];

/* Bonded */

// Remove the bond state for the validator.
Expand All @@ -356,12 +413,12 @@ finalize unbond_public:
/* Unbonding */

// Increment the microcredits in the unbond state.
add r3.microcredits r8.microcredits into r15;
add r3.microcredits r8.microcredits into r17;

// Construct the updated unbond state.
cast r15 r4 into r16 as unbond_state;
cast r17 r4 into r18 as unbond_state;
// Update the unbond state for the caller.
set r16 into unbonding[r0];
set r18 into unbonding[r0];

// Ends the `remove_validator` logic.
branch.eq true true to end;
Expand All @@ -372,17 +429,17 @@ finalize unbond_public:
position unbond_delegator;

// Get the bond state for the caller, or fail if it does not exist.
get bonded[r0] into r17;
get bonded[r0] into r19;
// Decrement the microcredits in the bond state.
sub r17.microcredits r1 into r18;
sub r19.microcredits r1 into r20;

// Determine if the remaining bond is at least 10 credits.
gte r18 10_000_000u64 into r19;
gte r20 10_000_000u64 into r21;

// If the remaining balance is at least 10 credits, jump to the `decrement_delegator` logic.
branch.eq r19 true to decrement_delegator;
branch.eq r21 true to decrement_delegator;
// If the remaining balance is less than 10 credits, jump to the `remove_delegator` logic.
branch.eq r19 false to remove_delegator;
branch.eq r21 false to remove_delegator;

/*** Decrement Delegator ***/

Expand All @@ -393,30 +450,30 @@ finalize unbond_public:

// Get the stake for the specified validator.
// If the validator does not exist, this finalize scope will fail.
get committee[r17.validator] into r20;
get committee[r19.validator] into r22;
// Decrement the stake for the specified validator.
sub r20.microcredits r1 into r21;
sub r22.microcredits r1 into r23;
// Construct the updated committee state.
cast r21 r20.is_open into r22 as committee_state;
cast r23 r22.is_open into r24 as committee_state;
// Update the stake for the specified validator.
set r22 into committee[r17.validator];
set r24 into committee[r19.validator];

/* Bonded */

// Construct the updated bond state.
cast r17.validator r18 into r23 as bond_state;
cast r19.validator r20 into r25 as bond_state;
// Update the bond state for the caller.
set r23 into bonded[r0];
set r25 into bonded[r0];

/* Unbonding */

// Increment the microcredits in the unbond state.
add r3.microcredits r1 into r24;
add r3.microcredits r1 into r26;

// Construct the updated unbond state.
cast r24 r4 into r25 as unbond_state;
cast r26 r4 into r27 as unbond_state;
// Update the unbond state for the caller.
set r25 into unbonding[r0];
set r27 into unbonding[r0];

// Ends the `decrement_delegator` logic.
branch.eq true true to end;
Expand All @@ -430,13 +487,20 @@ finalize unbond_public:

// Get the stake for the specified validator.
// If the validator does not exist, this finalize scope will fail.
get committee[r17.validator] into r26;
get committee[r19.validator] into r28;
// Decrement the stake for the specified validator.
sub r26.microcredits r17.microcredits into r27;
sub r28.microcredits r19.microcredits into r29;
// Construct the updated committee state.
cast r27 r26.is_open into r28 as committee_state;
cast r29 r28.is_open into r30 as committee_state;
// Update the stake for the specified validator.
set r28 into committee[r17.validator];
set r30 into committee[r19.validator];

// Get the number of delegators.
get metadata[aleo1qgqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqanmpl0] into r31;
// Decrement the number of bonded delegators by one.
sub r31 1u32 into r32;
// Set the new number of delegators.
set r32 into metadata[aleo1qgqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqanmpl0];

/* Bonded */

Expand All @@ -446,12 +510,12 @@ finalize unbond_public:
/* Unbonding */

// Increment the microcredits in the unbond state.
add r3.microcredits r17.microcredits into r29;
add r3.microcredits r19.microcredits into r33;

// Construct the updated unbond state.
cast r29 r4 into r30 as unbond_state;
cast r33 r4 into r34 as unbond_state;
// Update the unbond state for the caller.
set r30 into unbonding[r0];
set r34 into unbonding[r0];

// The terminus.
position end;
Expand Down Expand Up @@ -505,23 +569,28 @@ finalize unbond_delegator_as_validator:
// Construct the updated committee state.
cast r5 r2.is_open into r6 as committee_state;

// Get the number of delegators.
get metadata[aleo1qgqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqanmpl0] into r7;
// Decrement the number of delegators by one.
sub r7 1u32 into r8;

/* End Committee */

/* Start Unbond */

// Construct the initial unbond state.
cast 0u64 0u32 into r7 as unbond_state;
cast 0u64 0u32 into r9 as unbond_state;
// Get the unbond state for the delegator, or default to the initial unbond state.
get.or_use unbonding[r1] r7 into r8;
get.or_use unbonding[r1] r9 into r10;

// Increment the microcredits in the unbond state.
add r8.microcredits r4.microcredits into r9;
add r10.microcredits r4.microcredits into r11;
// Compute the height at which the unbonding will be complete, starting from the current block.
// Note: Calling unbond across multiple blocks before the unbonding is complete will reset the height each time.
add block.height 360u32 into r10;
add block.height 360u32 into r12;

// Construct the updated unbond state.
cast r9 r10 into r11 as unbond_state;
cast r11 r12 into r13 as unbond_state;

/* End Unbond */

Expand All @@ -532,7 +601,9 @@ finalize unbond_delegator_as_validator:
// Remove the bond state for the delegator.
remove bonded[r1];
// Update the unbond state for the delegator.
set r11 into unbonding[r1];
set r13 into unbonding[r1];
// Update the number of delegators.
set r8 into metadata[aleo1qgqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqanmpl0];

/* End Writes */

Expand Down
Loading