snarkVM icon indicating copy to clipboard operation
snarkVM copied to clipboard

[Proposal] I propose separating the functionalities of bond_validator and bond_delegator within the bond_public method.

Open OzielLa opened this issue 2 years ago • 0 comments

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

// This function allows any staker to bond their microcredits to a validator.
// The corresponding functions for 'bond_public' are 'unbond_public' and 'claim_unbond_public'.
function bond_public:
    // Input the validator's address.
    input r0 as address.public;
    // Input the amount of microcredits to bond.
    input r1 as u64.public;

    // Determine if the amount is at least one credit.
    gte r1 1_000_000u64 into r2;
    // Enforce the amount is at least one credit.
    assert.eq r2 true;

    // Bond the specified amount of microcredits to the specified validator.
    async bond_public self.caller r0 r1 into r3;
    // Output the finalize future.
    output r3 as credits.aleo/bond_public.future;

finalize bond_public:
    // Input the staker's address.
    input r0 as address.public;
    // Input the validator's address.
    input r1 as address.public;
    // Input the amount of microcredits to bond.
    input r2 as u64.public;

    // Determine whether the caller is a validator.
    assert.neq r0 r1;

    /* Committee */

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

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

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

    /* Bonded */

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

    // Increment the microcredits in the bond state.
    add r8.microcredits r2 into r9;
    // Determine if the amount is at least 10 credits.
    gte r9 10_000_000u64 into r10;
    // Enforce the amount is at least 10 credits.
    assert.eq r10 true;

    // Construct the updated bond state.
    cast r1 r9 into r11 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 r12;
    // Decrement the balance of the caller.
    sub r12 r2 into r13;

    /* Writes */

    // Update the committee state for the specified validator.
    set r6 into committee[r1];
    // Update the bond state for the caller.
    set r11 into bonded[r0];
    // Update the balance of the caller.
    set r13 into account[r0];

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

// This function allows any staker to bond their microcredits to a validator.
// The corresponding functions for 'bond_public' are 'unbond_public' and 'claim_unbond_public'.
function validator_bond_public:
    // Input the amount of microcredits to bond.
    input r0 as u64.public;

    // Determine if the amount is at least one credit.
    gte r0 1_000_000u64 into r1;
    // Enforce the amount is at least one credit.
    assert.eq r1 true;

    // Bond the specified amount of microcredits to the specified validator.
    async validator_bond_public self.caller r0 into r2;
    // Output the finalize future.
    output r2 as credits.aleo/validator_bond_public.future;

finalize validator_bond_public:
    // Input the staker's address.
    input r0 as address.public;
    // Input the amount of microcredits to bond.
    input r1 as u64.public;

    /* Committee */

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

    // Increment the stake for the specified validator.
    add r3.microcredits r1 into r4;
    // Construct the updated committee state.
    cast r4 r3.is_open into r5 as committee_state;

    /* Bonded */

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

    // Increment the microcredits in the bond state.
    add r7.microcredits r1 into r8;
    // Determine if the amount is at least one million credits.
    gte r8 1_000_000_000_000u64 into r9;
    // Enforce the amount is at least one million credits.
    assert.eq r8 true;

    // Construct the updated bond state.
    cast r1 r8 into r10 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 r11;
    // Decrement the balance of the caller.
    sub r11 r1 into r12;

    /* Writes */

    // Update the committee state of the specified validator.
    set r5 into committee[r0];
    // Update the bond state for the caller.
    set r10 into bonded[r0];
    // Update the balance of the caller.
    set r12 into account[r0];

OzielLa avatar Nov 21 '23 09:11 OzielLa