Monad 백서

Monad

원문

bitpro가 원문에서 글자만 모은 내용이에요. 표·그림·수식은 빠지거나 깨질 수 있어요. 전체 75쪽 중 35쪽

``solidity theme={null} validator_id = msg.input.val_id; // set validator information val_execution[validator_id] = ValExecution{ uint256 stake += msg.value(); } // set delegator information DelInfo current_delegator = delegator[validator_id][msg.sender]; // apply get_current_stake() first. This updates the delegator stake // to be inline with the current stake activated in consensus. get_current_stake(); // apply add_stake() second. uint256[4] add_stake_info = add_stake(msg.value()); current_delegator = DelInfo{ uint256 delta_stake = add_stake_info[0]; uint256 next_delta_stake = add_stake_info[1]; uint64 delta_epoch = add_stake_info[2]; uint64 next_delta_epoch = add_stake_info[3]; } // set epoch accumulator epoch_acc[validator_id][getEpoch()].ref_count += 1; // set flags set_flags(); // push validator id if val_execution[validator_id].stake() >= ACTIVE_VALIDATOR_STAKE and validator_id not in execution_valset: execution_valset.push(validator_id); def add_stake(uint256 amount): uint256 _delta_stake; uint256 _next_delta_stake; uint64 _delta_epoch; uint64 _next_delta_epoch; if not in_epoch_delay_rounds: _delta_stake = current_delegator.delta_stake() + amount; _next_delta_stake = 0; _delta_epoch = current_epoch + 1; _next_delta_epoch = 0; else: _delta_stake = 0; _next_delta_stake = current_delegator.next_delta_stake() + amount; _delta_epoch = 0; _next_delta_epoch = current_epoch + 2; return [_delta_stake, _next_delta_stake, _delta_epoch, _next_delta_epoch]; def maybe_process_next_epoch_state(): """ Helper function to process and update rewards based on the current epoch state. """ if ( epoch_acc[validator_id][current_delegator.delta_epoch()] != 0 and current_epoch > current_delegator.delta_epoch() and current_delegator.delta_epoch() > 0 ): // Compute rewards from the last checked epoch. _rewards += current_delegator.stake() * ( epoch_acc[validator_id][current_delegator.delta_epoch()].val() - current_delegator.acc() ) // Promote stake to active in delegator view. current_delegator.stake() += current_delegator.delta_stake() current_delegator.acc() = ( epoch_acc[validator_id][current_delegator.delta_epoch()].val() ) current_delegator.delta_epoch() = current_delegator.next_delta_epoch() current_delegator.delta_stake() = current_delegator.next_delta_stake() current_delegator.next_delta_epoch() = 0 current_delegator.next_delta_stake() = 0 epoch_acc[validator_id][current_delegator.delta_epoch].ref_count -= 1 def get_current_stake(): uint256 _rewards = 0; // Process next epoch rewards and increment stake maybe_process_next_epoch_state() // Perform again to capture max two additional epochs maybe_process_next_epoch_state() current_delegator.rewards() += _rewards; return _rewards; ``` </Accordion> ### undelegate Deducts `amount` from the delegator account and moves it to a withdrawal request object, where it remains in a pending state for `WITHDRAWAL_DELAY` epochs before the funds are claimable via the [`withdraw`](#withdraw) function. **Function selector** ``` undelegate(uint64,uint256,uint8) : 0x5cf41514 ``` **Function signature** ```solidity theme={null} function undelegate( uint64 validatorId, uint256 amount, uint8 withdrawId ) external returns (bool success); ``` **Parameters** 1. `validatorId` - id of the validator to which sender previously delegated, from which we are removing delegation 2. `amount` - amount to undelegate, in Monad wei 3. `withdrawId` - integer between 0 and 255, inclusive, which serves as the identifier for a delegator's withdrawal. For each (validator, delegator) tuple, there can be a maximum of 256 in-flight withdrawal requests **Gas cost** 147,750 **Behavior** * The delegator account is determined by `msg.sender`. * `validatorId` must correspond to a valid validator to which the sender previously delegated * The delegator must have stake >= amount. * If the withdrawal causes `Val(validatorId).stake()` to drop below `ACTIVE_VALIDATOR_STAKE`, then the validator is scheduled to be removed from the valset. * If the `authAddress` on a validator undelegates enough of their own stake to drop below `MIN_AUTH_ADDRESS_STAKE`, then the validator is scheduled to be removed from the valset. * The function will revert if there is a pending withdrawal with the same `withdrawId`. `withdrawId`s can be reused after calling [`withdraw`](#withdraw). * A delegator can only remove a stake after it has been activated. This is the stake field in the delegator struct. Pending delegations cannot be removed until they are active. * The delegator stake becomes inactive in the valset * in epoch `n+1` if the request is before the boundary block * in epoch `n+2` otherwise * The delegator stake becomes withdrawable, and thus no longer subject to slashing * in epoch `n + 1 + WITHDRAWAL_DELAY` if the request is before the boundary block * in epoch `n + 2 + WITHDRAWAL_DELAY` otherwise <img alt="timeline of undelegation and withdrawal" /> <p>Timeline of withdrawability of stake relative to <code>undelegate</code> command</p> <Accordion title="Pseudocode"> ```solidity theme={null} uint64 validator_id = msg.input.val_id; uint256 amount = msg.input.amount; uint8 withdraw_id = msg.input.withdraw_id; ValExecution current_validator = val_execution[validator_id]; // set validator information current_validator = ValExecution{ uint256 stake -= amount; } // apply get_current_stake() first. get_current_stake(); DelInfo current_delegator = delegator[validator_id][msg.sender]; // set delegator information current_delegator = DelInfo{ uint256 stake -= amount; } // set withdraw request withdrawal[validator_id][msg.sender][withdraw_id] = WithdrawalRequest{ uint256 amount = amount; uint256 acc = current_validator.acc(); uint64 epoch = getEpoch(); }; // set epoch accumulator epoch_acc[validator_id][getEpoch()].ref_count += 1; // schedule validator to leave set if current_validator.stake < ACTIVE_VALIDATOR_STAKE and validator_id in execution_valset: current_validator.set_flag(INSUFFICIENT_STAKE); if (current_delegator.stake <= MIN_AUTH_ADDRESS_STAKE and validator_id in execution_valset) and msg.sender == current_validator.auth_address: current_validator.set_flag(INSUFFICIENT_VALIDATOR_STAKE); ``` </Accordion> ### withdraw Completes an undelegation action (which started with a call to the `undelegate` function), sending the amount to `msg.sender`, provided that sufficient epochs have passed. **Function selector** ``` withdraw(uint64,uint8) : 0xaed2ee73 ``` **Function signature** ```solidity theme={null} function withdraw( uint64 validatorId, uint8 withdrawId ) external returns (bool success); ``` **Parameters** 1. `validatorId` - id of the validator to which sender previously delegated, from which we previously issued an `undelegate` command 2. `withdrawId` - identifier for a delegator's previously created withdrawal; the same id previously supplied to [`undelegate`](#undelegate). For each (validator, delegator) tuple, there can be a maximum of 256 in-flight withdrawal requests. **Gas cost** 68,675 **Behavior** * The delegator is `msg.sender`. The withdrawal is identified by `msg.sender`, `validatorId`, and `withdrawId` * The withdraw action can take place once the undelegation is complete, and the withdraw delay has passed: * in epoch `n + 1 + WITHDRAWAL_DELAY` if the undelegate request is before the boundary block * in epoch `n + 2 + WITHDRAWAL_DELAY` otherwise <Accordion title="Pseudocode"> ```solidity theme={null} uint64 validator_id = msg.input.val_id; uint8 withdraw_id = msg.input.withdraw_id; WithdrawalRequest current_withdraw = withdrawal[validator_id][msg.sender][withdraw_id]; // Compute any additional rewards and transfer funds to delegator transfer(msg.sender, current_withdraw.amount + get_withdraw_rewards()); // unset withdraw request withdrawal[validator_id][msg.sender][withdraw_id] = WithdrawalRequest{ uint256 amount = 0, uint256 acc = 0, uint64 epoch = 0 }; def get_withdraw_rewards(): epoch_acc[validator_id][current_withdraw.epoch].ref_count -= 1; return current_withdraw.amount() * (epoch_acc[validator_id][current_withdraw.epoch()].val() - current_withdraw.acc()); ``` </Accordion> ### compound Converts the delegator's accumulated rewards into additional stake. **Function selector** ``` compound(uint64) : 0xb34fea67 ``` **Function signature** ```solidity theme={null} function compound( uint64 validatorId ) external returns (bool success); ``` **Parameters** 1. `validatorId` - id of the validator to which sender previously delegated, for which we are compounding rewards **Gas cost** 289,325 **Behavior** * The account compounded is determined by `msg.sender`. If a delegator account does not exist, then the call reverts * `validatorId` must correspond to a valid validator to which the sender previously delegated * The delegator rewards become active in the valset * in epoch `n+1` if the request is before the boundary block * in epoch `n+2` otherwise. <Accordion title="Pseudocode"> ```solidity theme={null} validator_id = msg.input.val_id; // set delegator information DelInfo current_delegator = delegator[validator_id][msg.sender]; // apply get_current_stake() first. This updates the delegator stake // to be inline with the current stake activated in consensus. rewards_compounded = get_current_stake(); // apply add_stake() second. uint256[4] add_stake_info = add_stake(rewards_compounded); // set delegator information current_delegator = DelInfo{ uint256 delta_stake = add_stake_info[0]; uint256 next_delta_stake = add_stake_info[1]; uint64 delta_epoch = add_stake_info[2]; uint64 next_delta_epoch = add_stake_info[3]; uint256 rewards = 0; } // set validator information val_execution[validator_id] = ValExecution{ uint256 stake += rewards_compounded; } // set accumulator epoch_acc[validator_id][getEpoch()] = Accumulator{ uint256 ref_count += 1; } // set flags set_flags(); // push validator id if val_execution[validator_id].stake() >= ACTIVE_VALIDATOR_STAKE and validator_id not in execution_valset: execution_valset.push(validator_id); ``` </Accordion> ### claimRewards Allows a delegator to claim any rewards instead of [compounding](#compound) them. **Function selector** ``` claimRewards(uint64) : 0xa76e2ca5 ``` **Function signature** ```solidity theme={null} function claimRewards( uint64 validatorId ) external returns (bool success); ``` **Parameters** 1. `validatorId` - id of the validator to which sender previously delegated, for which we are claiming rewards **Gas cost** 155,375 **Behavior** * `validatorId` must correspond to a valid validator to which the sender previously delegated * If delegator account does not exist for this `(validatorId, msg.sender)` tuple, then the call reverts * The delegator's accumulated rewards are transferred to their delegation <Accordion title="Pseudocode"> ```solidity theme={null} // set delegator information DelInfo current_delegator = delegator[validator_id][msg.sender]; // apply get_current_stake() first. uint256 current_rewards = get_current_stake(); // set delegator information current_delegator = DelInfo{ uint256 rewards = 0; ) // send rewards to delegator transfer(msg.sender, current_rewards); ``` </Accordion> ### changeCommission Allows the `authAddress` for a validator to modify the commission for the validator. **Function selector** ``` changeCommission(uint64,uint256) : 0x9bdcc3c8 ``` **Function signature** ```solidity theme={null} function changeCommission( uint64 validatorId, uint256 commission ) external returns (bool success); ``` **Parameters** 1. `validatorId` - id of the validator, who would like to change their commission rate 2. `commission` - commission rate taken from block rewards, expressed in 1e18 units (e.g., 10% = 1e17) **Gas cost** 39,475 **Behavior** * The `msg.sender` must be the `authAddress` for the respective validator Id. * The commission cannot be set larger than `MAX_COMMISSION` (currently 100%). * The change in commission occurs in the following epochs: * in epoch `n+1` if request is not in the epoch delay rounds. * in epoch `n+2` if request is in the epoch delay rounds. <Accordion title="Pseudocode"> ```solidity theme={null} validator_id = msg.input.val_id; val_execution[validator_id] = ValExecution{ uint256 commission = msg.input.commission; } ``` </Accordion> ### externalReward Allows anyone to send extra MON to the stakers of a particular validator, typically called by the validator themselves to share extra tips to their delegators. **Function selector** ``` externalReward(uint64) : 0xe4b3303b ``` **Function signature** ```solidity theme={null} function externalReward( uint64 validatorId ) external payable returns (bool success); ``` **Parameters** 1. `validatorId` - id of the validator 2. `msg.value` - the MON to add to unclaimed rewards **Gas cost** 66,575 **Behavior** * This can only be called for a validator currently in the consensus validator set; otherwise the transaction reverts. * `msg.value` must be between 1 MON and 1,000,000 MON; otherwise the transaction reverts. * Commission is not deducted from this and diverted to the validator's `auth_address`. If you wish for a portion to be deducted, it should be deducted before sending. <Accordion title="Pseudocode"> ```solidity theme={null} validator_id = msg.input.val_id; require(msg.value >= 1e18 && msg.value <= 1e24, "Reward out of bounds"); require(val_consensus[validator_id] > 0 , "Validator not active"); val_execution[validator_id].unclaimed_reward += msg.value; val_execution[val_id].acc += msg.value / val_consensus[val_id].stake(); ``` </Accordion> ## View functions <Note> Because only `CALL`s are allowed to the staking precompile, all view functions are given the default `nonpayable` state mutability. </Note> ### getValidator Returns a complete view of the validator's state across execution, consensus, and snapshot contexts. * [`ValExecution`](#validator-structs) (execution view) * Stake and commission (consensus view) * Stake and commission (snapshot view) **Function selector** ``` getValidator(uint64) : 0x2b6d639a ``` **Function signature** ```solidity theme={null} function getValidator( uint64 validatorId ) external returns ( address authAddress, uint64 flags, uint256 stake, uint256 accRewardPerToken, uint256 commission, uint256 unclaimedRewards, uint256 consensusStake, uint256 consensusCommission, uint256 snapshotStake, uint256 snapshotCommission, bytes memory secpPubkey, bytes memory blsPubkey ); ``` **Parameters** 1. `validatorId` - id of the validator **Gas cost** 97,200 ### getDelegator Returns the delegator's [`DelInfo`](#delegator-structs) for the specified validator, providing a view of the delegator's stake, accumulated rewards, and pending changes in stake. **Function selector** ``` getDelegator(uint64,address) : 0x573c1ce0 ``` **Function signature** ```solidity theme={null} function getDelegator( uint64 validatorId, address delegator ) external returns ( uint256 stake, uint256 accRewardPerToken, uint256 unclaimedRewards, uint256 deltaStake, uint256 nextDeltaStake, uint64 deltaEpoch, uint64 nextDeltaEpoch ); ``` **Parameters** 1. `validatorId` - id of the validator 2. `delegator` - address of the delegator about whose stake we are inquiring **Gas cost** 184,900 ### getWithdrawalRequest Returns the pending [`WithdrawalRequest`](#delegator-structs) for the `(validatorId, delegator, withdrawId)` tuple. **Function selector** ``` getWithdrawalRequest(uint64,address,uint8) : 0x56fa2045 ``` **Function signature** ```solidity theme={null} function getWithdrawalRequest( uint64 validatorId, address delegator, uint8 withdrawId ) external returns ( uint256 withdrawalAmount, uint256 accRewardPerToken, uint64 withdrawEpoch ); ``` **Gas cost** 24,300 ### get\*ValidatorSet Returns the consensus, snapshot, and execution validator IDs, respectively. **Function selectors** ``` getConsensusValidatorSet(uint32) : 0xfb29b729 getSnapshotValidatorSet(uint32) : 0xde66a368 getExecutionValidatorSet(uint32) : 0x7cb074df ``` **Function signatures** ```solidity theme={null} function getConsensusValidatorSet( uint32 startIndex ) external returns (bool isDone, uint32 nextIndex, uint64[] memory valIds); function getSnapshotValidatorSet( uint32 startIndex ) external returns (bool isDone, uint32 nextIndex, uint64[] memory valIds); function getExecutionValidatorSet( uint32 startIndex ) external returns (bool isDone, uint32 nextIndex, uint64[] memory valIds); ``` **Parameters** 1. `startIndex` - since the list being looked up is potentially very long, each of these functions is paginated, returning a fixed-length subset of the desired list. Pass `startIndex` to indicate where in the list to start. **Gas cost** 814,000 gas (assuming `PAGINATED_RESULTS_SIZE = 100`). **Behavior** `getExecutionValidatorSet()` returns the entire active validator set of all validators that meet the staking criteria to be considered for inclusion. `getSnapshotValidatorSet()` returns the subset of validators that have been chosen to be leaders in the next epoch. `getConsensusValidatorSet()` returns the subset of validators that are leaders in the current epoch. Each call retrieves up to `PAGINATED_RESULTS_SIZE` validator IDs starting from `startIndex` and returns a tuple `(bool done, uint32 nextIndex, uint256[] valids)`. The `bool isDone` indicates whether the end of the list was reached. The `uint32 nextIndex` is the last slot in the array. ### getDelegations Returns a paginated list of validator IDs that a given delegator address has delegated to. **Function selector** ``` getDelegations(address,uint64) : 0x4fd66050 ``` **Function signature** ```solidity theme={null} function getDelegations( address delegator, uint64 startValId ) external returns (bool isDone, uint64 nextValId, uint64[] memory valIds); ``` **Parameters** 1. `delegator` - the address whose delegations we want to look up 2. `startValId` **Gas cost** 814,000 **Behavior** Each call retrieves up to `PAGINATED_RESULTS_SIZE` validator ids starting from `startValId` and returns a tuple `(bool isDone, uint64 nextValId, uint64[] valIds)` with delegation from the input `delegator` address. The `bool isdone` indicates whether the end of the list was reached. The `uint64 nextValId` is the id after the last element in `valIds`. Use it as the `startValId` for the next call. If `delegator` has delegated to over `PAGINATED_RESULTS_SIZE` validator ids, multiple calls are required (while `isDone` is false). To capture the full set, make the first function call using `startValId = 0`. ### getDelegators Returns a paginated list of delegator addresses for a given validator. **Function selector** ``` getDelegators(uint64,address) : 0xa0843a26 ``` **Function signature** ```solidity theme={null} function getDelegators( uint64 validatorId, address startDelegator ) external returns (bool isDone, address nextDelegator, address[] memory delegators); ``` **Parameters** 1. `validatorId` - the id of the validator for which we want to know the delegators 2. `startDelegator` **Gas cost** 814,000 **Behavior** Each call retrieves up to `PAGINATED_RESULTS_SIZE` delegator addresses starting from `startDelegator` and returns a tuple `(bool isDone, address nextDelegator, address[] delegators)` with delegation to the input `validatorId`. The `bool isDone` indicates the end of the list was reached. The `nextDelegator` is the address immediately after the last element in `delegators`. Use it as `startDelegator` for the next call. To capture the full set, the function should be called with `startDelegator = 0`. <Note> The number of delegators to a given validator can be very large, so it
프로젝트로 돌아가기