Skip to content
Monpediaby Unity Nodes
Category LabsSpecification

Staking Contract Properties

Andreas Lynge25 pages

This document defines the properties of a mathematical model of the staking contract and serves as the test specification for a staking contract fuzz tester. Section 1 describes the structure of the contract in Solidity-style pseudocode. Section 2 introduces the notation and the model. Section 3 states the invariants that every valid state must satisfy, and Section 4 specifies pre-conditions and p

Open the PDF (opens in a new tab)category-labs.github.io
Contents21 sections

Staking Contract Properties

Andreas Lynge September 4, 2025 Abstract This document defines the properties of a mathematical model of the staking contract and serves as the test specification for a staking contract fuzz tester. Section 1 describes the structure of the contract in Solidity-style pseudocode. Section 2 introduces the notation and the model. Section 3 states the invariants that every valid state must satisfy, and Section 4 specifies pre-conditions and post-conditions for the contract’s interface functions.

Contents

1 The Staking Contract 1.1 Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1.2 Fields . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1.3 Precompile Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1.4 Syscall Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

2 2 3 4 6

2

Model and Notation 2.1 Notation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2.2 State Extensions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2.3 Constants . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2.4 Utilities . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

6 6 8 9 9

3

Invariants 3.1 Valset Invariants . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3.2 ValExecution Invariants . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3.3 Delegator Invariants . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3.4 Accumulated Reward per Token Invariants . . . . . . . . . . . . . . . . . . . . . 3.5 Linked List Invariants . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3.6 Solvency Invariants . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

11 11 12 12 13 15 15

4

Function Specifications 4.1 Specification of precompile_add_validator . . . . . . . . . . . . . . . . . . . .

16 17

p. 2

4.2 Specification of precompile_delegate . . . . . . . . . . . . . . . . . . . . . . . . 4.3 Specification of precompile_undelegate . . . . . . . . . . . . . . . . . . . . . . . 4.4 Specification of precompile_compound . . . . . . . . . . . . . . . . . . . . . . . . 4.5 Specification of precompile_withdraw . . . . . . . . . . . . . . . . . . . . . . . . 4.6 Specification of precompile_change_commission . . . . . . . . . . . . . . . . . . 4.7 Specification of precompile_claim_rewards . . . . . . . . . . . . . . . . . . . . 4.8 Specification of precompile_external_reward . . . . . . . . . . . . . . . . . . . 4.9 Specification of syscall_reward . . . . . . . . . . . . . . . . . . . . . . . . . . . 4.10 Specification of syscall_snapshot . . . . . . . . . . . . . . . . . . . . . . . . . . 4.11 Specification of syscall_on_epoch_change . . . . . . . . . . . . . . . . . . . . . 4.12 Specification of precompile_get_delegator . . . . . . . . . . . . . . . . . . . . 4.13 Specification of distribute_priority_fees . . . . . . . . . . . . . . . . . . . .

1

18 19 20 21 21 22 22 23 23 24 24 24

The Staking Contract

This section describes the structure of the staking contract using a Solidity-style pseudocode notation.

1.1

Types

The staking contract makes use of the following struct types. struct ListNode { uint64 inext; uint64 iprev; address anext; address aprev; } struct Delegator { uint256 accumulated_reward_per_token; uint256 rewards; uint256 stake; uint256 delta_stake; uint256 next_delta_stake; uint64 delta_epoch; uint64 next_delta_epoch; ListNode list_node; } struct KeysPacked { bytes secp_pubkey; bytes bls_pubkey; } struct ValExecution {

p. 3

KeysPacked keys; address auth_address; uint64 flags; uint256 accumulated_reward_per_token; uint256 stake; uint256 commission; uint256 unclaimed_rewards; } struct ConsensusView { uint256 stake; uint256 commission; } struct WithdrawalRequest { uint256 amount; uint256 acc; uint64 epoch; }

1.2

Fields

The staking contract has the following fields. // Current epoch count (affects the active validator set and stake) uint64 epoch; // Whether the staking contract is in the epoch delay period // (affects when new stake delegations are activated) bool in_epoch_delay_period; // Validator ID of the most recently added validator // (incremented for each new validator) uint64 last_val_id; // Set of validators that are potential candidates to be active // in the next epoch uint64[] valset_execution; // Set of validators that have been chosen as active, in this or // the next epoch, depending on the value of in_epoch_delay_period uint64[] valset_consensus; // The previous valset_consensus. If in_epoch_delay_period is true, // then valset_snapshot is the set of currently active validators; // otherwise valset_consensus is the set of active validators. uint64[] valset_snapshot; // Auth address to corresponding validator ID mapping(address => uint64) val_id; // Validator ID of the proposer of each block

p. 4

uint64 proposer_val_id; // Address of BLS public key to corresponding validator ID mapping(address => uint64) val_id_bls; // Bit set corresponding to valset_execution mapping(uint64 => uint256) val_bitset_bucket; // Validator ID to validator state mapping(uint64 => ValExecution) val_execution; // Validator ID to state related to validators in valset_consensus mapping(uint64 => ConsensusView) consensus_view; // Validator ID to state related to validators in valset_snapshot mapping(uint64 => ConsensusView) snapshot_view; // Validator ID and address to corresponding delegator state mapping(uint64 => mapping(address => Delegator)) delegator; // Validator ID, address, and withdrawal ID to corresponding // withdrawal request mapping(uint64 => mapping(address => mapping(uint8 => WithdrawalRequest))) withdrawal_request; // Epoch and validator ID to the accumulated reward per token for // the given validator in the given epoch mapping(uint64 => mapping(uint64 => uint256)) accumulated_reward_per_token;

1.3

Precompile Interface

function precompile_get_validator(uint64 val_id) external view returns ( address auth_address, uint64 flags, uint256 stake, uint256 accumulated_reward_per_token, uint256 commission, uint256 unclaimed_rewards, uint256 consensus_view_stake, uint256 consensus_view_commission, uint256 snapshot_view_stake, uint256 snapshot_view_commission ); function precompile_get_delegator(uint64 val_id, address delegator) external /* not view */ returns ( uint256 stake, uint256 accumulated_reward_per_token, uint256 rewards,

p. 5

uint256 delta_stake, uint256 next_delta_stake, uint64 delta_epoch, uint64 next_delta_epoch ); function precompile_get_withdrawal_request(uint64 val_id, address delegator) external view returns (uint256 amount, uint256 acc, uint64 epoch); function precompile_get_consensus_valset(uint32 start_index) external view returns (bool done, uint32 next_index, uint64[]); function precompile_get_snapshot_valset(uint32 start_index) external view returns (bool done, uint32 next_index, uint64[]); function precompile_get_execution_valset(uint32 start_index) external view returns (bool done, uint32 next_index, uint64[]); function precompile_get_delegations(address delegator, uint64 start_val_id) external view returns (bool done, uint64 next_val_id, uint64[]); function precompile_get_delegators(uint64 val_id, address start_delegator) external view returns (bool done, address next_delegator, address[]); function precompile_get_epoch() external view returns (uint64); function precompile_fallback() external pure; function precompile_add_validator( bytes message, bytes secp_signature, bytes bls_signature ) external payable returns (uint64); function precompile_delegate(uint64 val_id) external payable returns (bool); function precompile_undelegate(uint64 val_id, uint256 stake) external returns (bool); function precompile_compound(uint64 val_id) external returns (bool); function precompile_withdraw(uint64 val_id, uint8 withdrawal_id) external returns (bool); function precompile_claim_rewards(uint64 val_id) external returns (bool); function precompile_change_commission(uint64 val_id, uint256 new_commission) external returns (bool); function precompile_external_reward(uint64 val_id) external payable returns (bool);

p. 6

1.4

Syscall Interface

function syscall_on_epoch_change(uint64 next_epoch) external; function syscall_reward(address block_author, uint256 raw_reward) external; function syscall_snapshot() external; function distribute_priority_fees(uint256 fees) external;

2

Model and Notation

2.1

Notation

The model uses the following notation for basic sets. • The set of all 20-byte Ethereum addresses is modeled as the integers modulo 2160 , address = Z/2160 Z = {0, 1, . . . , 2160 − 1}. • The set of all 32-byte Ethereum words is the integers modulo 2256 , uint256 = Z/2256 Z = {0, 1, . . . , 2256 − 1}. • The set of all 8-byte values is uint64 = Z/264 Z = {0, 1, . . . , 264 − 1}. • The set of all 1-byte values is uint8 = Z/28 Z = {0, 1, . . . , 28 − 1}. • The set containing two elements is modeled as bool = Z/2Z = {0, 1}. Addition, multiplication, and additive inverse in Z/mZ are defined as the usual ring operations. Division of x, y ∈ Z/mZ with y ̸= 0 is defined by real division rounded down, as is typical in programming languages, ⌊︃ ⌋︃ i(x) x = , y i (y) where ⌊·⌋ : { x ∈ R | 0 ≤ x < m} → Z/mZ is the floor function and i : Z/mZ → R is the canonical inclusion. Structs are modeled as products, and dot notation is used for the projections. For example, the ConsensusView struct type is modeled as the product ConsensusView = uint256 × uint256 with projections x.stake ∈ uint256,

x.commission ∈ uint256,

for x ∈ ConsensusView.

p. 7

An array is modeled as a length together with a finite sequence. For example, the array type uint256[] is modeled by the set of pairs of the form (︁ )︁ n, ( ai )0≤i<n ∈ uint256[], (︁ )︁ where n ∈ uint64 and ai ∈ uint256. With x = n, ( ai )0≤i<n ∈ uint256[], define x.length = n,

xi = ai

for 0 ≤ i < n.

The bytes Solidity type is modeled by a uint8 array, bytes = uint8[]. Mappings are modeled as functions. For instance, mapping(uint64 => mapping(address => Delegator)) {︁ }︁ = f : uint64 × address → Delegator , that is, this mapping type is modeled by the set of all binary functions from uint64 × address to Delegator. The set State denotes the set of all valid Monad blockchain states. For any s ∈ State, the staking contract fields under s satisfy the invariants defined in Section 3, and the staking contract interface functions satisfy the properties defined in Section 4. The staking contract fields are generally modeled with the same names as in the Solidity pseudocode and are accessed via dot notation. For example, the field mapping(address => uint64) val_id;

is a projection of State, s.val_id : address → uint64,

for s ∈ State.

The precompile_* staking contract interface functions are modeled as functions with the same names. For example, function precompile_get_delegations(address delegator, uint64 start_val_id) external view returns (bool done, uint64 next_val_id, uint64[]);

is modeled as a partial function precompile_get_delegations : address × uint64 × address × uint256 × State ⇀ bool × uint64 × uint64[] × State. The last three arguments, address × uint256 × State, model the sender address, the message value, and the input state of the Monad blockchain, respectively. The remaining arguments, address × uint64, are translated directly from the input type of the staking contract interface function. The last output, State, is the resulting Monad blockchain state, and the remaining

p. 8

outputs, bool × uint64 × uint64[], are translated directly from the output type of the staking contract interface function. The syscall staking contract interface functions are modeled in a similar way, except that their arguments do not include a sender address or a message value. For example, the function syscall_on_epoch_change(uint64 next_epoch) external;

staking contract interface function is modeled by a partial function syscall_on_epoch_change : uint64 × State ⇀ State. For any s ∈ State, s.balance_of : address → uint256 is the function returning the balance of the given address in state s. Finally, for readability, the field name accumulated_reward_per_token is abbreviated as acc_rpt in mathematical formulas; the pseudocode listings retain the full name.

2.2

State Extensions

The model of the staking contract has fields in addition to the fields of the C++ code. In Solidity pseudocode notation, these extra fields are: // An upper bound on reward rounding errors uint256 error_bound; // unit_bias_rewards(v, a) is the sum of rewards distributed to // delegator (v, a) but not yet claimed or compounded mapping(uint64 => mapping(address => uint256)) unit_bias_rewards; // The state of the active consensus validators mapping(uint64 => ConsensusView) active_consensus_view; // delegator_stake(v, a, e) is the stake of delegator (v, a) // in epoch e mapping(uint64 => mapping(address => mapping(uint64 => uint256))) delegator_stake; // withdrawal_stake(v, a, e) is the stake of delegator (v, a) in // epoch e which has been undelegated but is still eligible for // rewards mapping(uint64 => mapping(address => mapping(uint64 => uint256))) withdrawal_stake;

These fields of the model are explicitly updated by the post-conditions of the partial interface functions, which are defined in Section 4. The fields are modeled as projections of the state, analogously to the other fields:

p. 9

• s.error_bound ∈ uint256 • s.unit_bias_rewards : uint64 × address → uint256 • s.active_consensus_view : uint64 → ConsensusView • s.delegator_stake : uint64 × address × uint64 → uint256 • s.withdrawal_stake : uint64 × address × uint64 → uint256 where s ∈ State.

2.3

Constants

• STAKING_CA ∈ address refers to the staking contract address. It is used in particular to access the balance of the staking contract. • UNIT_BIAS ∈ uint256 is a constant value used in the staking contract to limit rounding errors in MON rewards. • ACTIVE_VALSET_SIZE ∈ uint64 is an upper bound on the number of active validators. • ACTIVE_VALIDATOR_STAKE ∈ uint256 is a lower bound on the stake of active validators. • MIN_VALIDATE_STAKE ∈ uint256 is the threshold below which a validator is considered withdrawn. • The constant flags – ValidatorFlagsStakeTooLow = 1 ∈ uint64 and – ValidatorFlagWithdrawn = 2 ∈ uint64 are used to indicate validator status. • DUST_THRESHOLD ∈ uint256 is the minimum non-zero delegator stake. • WITHDRAWAL_DELAY ∈ uint64 is the number of epochs a delegator must wait before withdrawing. • MON ∈ uint256 is defined by MON = 1018 . • MAX_COMMISSION = MON is the upper bound on commission. • MIN_EXTERNAL_REWARD, MAX_EXTERNAL_REWARD ∈ uint256 are, respectively, a lower and an upper bound on external rewards.

2.4

Utilities

The relation BitTest ⊆ (Z/2n Z) × (Z/2n Z), for n ∈ N, is defined by: BitTest( x, b) holds if and only if • b = 2m for some m ∈ N0 , and • q( x ) ≥ b, for q : Z → Z/2bZ the quotient map. Intuitively, the BitTest relation can be used to test whether b has exactly one bit set and

p. 10

whether that bit is also set in x. The following four functions are important for the solvency invariants and reward distributions: • calculate_rewards : uint2563 → uint256, • withdrawal_reward : uint64 × address × uint8 × State → uint256, • unaccumulated_rewards : uint64 × address × State → uint256, and • pending_rewards : uint64 × address × State → uint256, defined as follows. First, calculate_rewards( x, q, p) =

x (q − p) . UNIT_BIAS

Second, {︄ withdrawal_reward(v, a, i, s) =

calculate_rewards(w.amount, q, w.acc)

if w.epoch > 0,

0

if w.epoch = 0,

where • w = s.withdrawal_request(v, a, i ) and {︄ s.val_execution(v).acc_rpt if s.epoch < w.epoch, • q= s.acc_rpt(w.epoch, v) if s.epoch ≥ w.epoch. Third, unaccumulated_rewards(v, a, s) = r1 + r2 + r3 , where, writing D = s.delegator(v, a) for brevity: • t1 = D.stake • p1 = D.acc_rpt • d1 = s.acc_rpt( D.delta_epoch, v) {︄ p1 if δ = 0 or δ > s.epoch • q1 = d1 if δ ̸= 0 and δ ≤ s.epoch

for δ = D.delta_epoch

• r1 = calculate_rewards(t1 , q1 , p1 ) {︄ D.delta_stake if δ ≤ s.epoch • t2 = t1 + 0 if δ > s.epoch • p2 = q1

for δ = D.delta_epoch

• d2 = s.acc_rpt( D.next_delta_epoch, v) {︄ p2 if δ = 0 or δ > s.epoch • q2 = for δ = D.next_delta_epoch d2 if δ ̸= 0 and δ ≤ s.epoch • r2 = calculate_rewards(t2 , q2 , p2 )

p. 11

{︄ • t3 = t2 +

D.next_delta_stake

if δ ≤ s.epoch

0

if δ > s.epoch

for δ = D.next_delta_epoch

• p3 = q2 • q3 = s.val_execution(v).acc_rpt • r3 = calculate_rewards(t3 , q3 , p3 ) Finally, the pending_rewards function is defined by pending_rewards(v, a, s) = unaccumulated_rewards(v, a, s)

+

∑ withdrawal_reward(v, a, i, s).

i ∈uint8

3

Invariants

Let s ∈ State be any state. The invariants defined in this section are satisfied by s. Unless quantified otherwise, the invariants hold for all v ∈ uint64, a ∈ address, and i ∈ uint8.

3.1

Valset Invariants

• s.valset_execution consists of pairwise distinct elements. • If v is in s.valset_execution, then s.val_execution(v).auth_address ̸= 0. • s.valset_consensus consists of pairwise distinct elements. • s.valset_consensus.length ≤ ACTIVE_VALSET_SIZE. • s.valset_snapshot consists of pairwise distinct elements. • s.valset_snapshot.length ≤ ACTIVE_VALSET_SIZE. • If v is in s.valset_consensus, then v is in s.valset_execution.

≥

• v is in s.valset_consensus if and only if s.consensus_view(v).stake ACTIVE_VALIDATOR_STAKE. • v is in s.valset_snapshot if and only if s.snapshot_view(v).stake ACTIVE_VALIDATOR_STAKE.

≥

• s.consensus_view(v).commission ≤ MAX_COMMISSION. • s.snapshot_view(v).commission ≤ MAX_COMMISSION. • BitTest(s.val_bitset_bucket(v), b) holds if and only if v is in s.valset_execution, where b = 2r and r is the remainder after integer division of i (v) by 256, for i the inclusion uint64 → Z. • If both s.val_execution(v).stake ≥ ACTIVE_VALIDATOR_STAKE and d.stake + d.delta_stake + d.next_delta_stake ≥ MIN_VALIDATE_STAKE, where d = s.delegator(v, s.val_execution(v).auth_address), then v is an element of s.valset_execution.

p. 12

• s.in_epoch_delay_period = 0 implies s.active_consensus_view(v) = s.consensus_view(v) for all v ∈ uint64. • s.in_epoch_delay_period = 1 implies s.active_consensus_view(v) = s.snapshot_view(v) for all v ∈ uint64.

3.2

ValExecution Invariants

• For 1 ≤ v ≤ s.last_val_id, s.val_execution(v).auth_address ̸= 0. • For v > s.last_val_id or v = 0, s.val_execution(v).auth_address = 0. • For v ≥ 1, if s.val_execution(v + 1).auth_address s.val_execution(v).auth_address ̸= 0.

̸=

0, then

• For all 0 < v ≤ s.last_val_id with a = s.val_execution(v).auth_address, s.delegator(v, a).stake + s.delegator(v, a).delta_stake

+ s.delegator(v, a).next_delta_stake ≥ MIN_VALIDATE_STAKE if and only if BitTest(s.val_execution(v).flags, ValidatorFlagWithdrawn) is false. • For all v ∈ uint64, – s.val_execution(v).commission ≤ MAX_COMMISSION and – s.val_execution(v).flags < 4. • For 0 < v ≤ s.last_val_id, s.val_execution(v).stake ≥ ACTIVE_VALIDATOR_STAKE if and only if BitTest(s.val_execution(v).flags, ValidatorFlagsStakeTooLow) is false. • For v ∈ uint64, s.val_id(y) ̸= 0 if and only if 1 ≤ v ≤ s.last_val_id, for y the SECP address of the SECP public key s.val_execution(v).keys.secp_pubkey. • For v ∈ uint64, s.val_id_bls(z) ̸= 0 if and only if 1 ≤ v ≤ s.last_val_id, for z the BLS address of the BLS public key s.val_execution(v).keys.bls_pubkey.

3.3

Delegator Invariants

For all v ∈ uint64, a ∈ address, and i ∈ uint8: • s.delegator(v, a).next_delta_stake = 0 or s.delegator(v, a).next_delta_stake ≥ DUST_THRESHOLD. • s.delegator(v, a).delta_stake = 0 or s.delegator(v, a).delta_stake ≥ DUST_THRESHOLD. • s.delegator(v, a).stake = 0 or s.delegator(v, a).stake ≥ DUST_THRESHOLD. • s.delegator(v, a).next_delta_stake = 0 if and only if s.delegator(v, a).next_delta_epoch = 0. • s.delegator(v, a).delta_stake = 0 if and only if s.delegator(v, a).delta_epoch = 0. • If s.delegator(v, a).delta_epoch ̸= 0 and s.delegator(v, a).next_delta_epoch ̸= 0,

p. 13

then s.delegator(v, a).next_delta_epoch = 1 + s.delegator(v, a).delta_epoch. • s.delegator(v, a).delta_epoch ≤ s.epoch + 1. • s.delegator(v, a).next_delta_epoch ≤ s.epoch + 2. • s.withdrawal_request(v, a, i ).amount = 0 if and only if s.withdrawal_request(v, a, i ).epoch = 0, for all v, a, i. • With r = s.delegator(v, a).rewards + pending_rewards(v, a, s) and E = s.error_bound + 3 + 256, s.unit_bias_rewards(v, a) ≤ (r + E) · UNIT_BIAS

≤ s.unit_bias_rewards(v, a) + E · UNIT_BIAS. Note that the additional 3 + 256 term accounts for potential rounding errors, in wei, from pending_rewards. • s.delegator_stake(v, a, s.epoch) = s.delegator(v, a).stake + d1 + d2 , where – δ1 = s.delegator(v, a).delta_epoch, {︄ s.delegator(v, a).delta_stake – d1 = 0

if δ1 ≤ s.epoch if δ1 > s.epoch

– δ2 = s.delegator(v, a).next_delta_epoch, {︄ s.delegator(v, a).next_delta_stake – d2 = 0

if δ2 ≤ s.epoch if δ2 > s.epoch

• s.withdrawal_stake(v, a, s.epoch) = ∑i∈uint8 wi , where – τi = s.withdrawal_request(v, a, i ).epoch, {︄ s.withdrawal_request(v, a, i ).amount if τi > s.epoch – wi = 0 if τi ≤ s.epoch • For all v ∈ uint64, s.active_consensus_view(v).stake =

(︂

+

(︂

)︂

∑

s.delegator_stake(v, a, s.epoch)

∑

)︂ s.withdrawal_stake(v, a, s.epoch) .

a∈address

a∈address

3.4

Accumulated Reward per Token Invariants

In the model, each entry of acc_rpt carries two components: value, the accumulated reward per token itself, and refcount, a reference count. Where acc_rpt is used as a number elsewhere in this document, it refers to the value component.

p. 14

• For all t, v ∈ uint64 with v > 0, (︂ s.acc_rpt(t, v).refcount =

∑

(︁ )︁)︂ I s.delegator(v, a).delta_epoch, t

∑

(︁ )︁)︂ I s.delegator(v, a).next_delta_epoch, t

∑

(︁ )︁)︂ I s.withdrawal_request(v, a, i ).epoch, t ,

a∈address

+

(︂

+

(︂

a∈address

a∈address i ∈uint8

where I : uint642 → uint256, {︄ I ( x, y) =

1

if x = y,

0

if x ̸= y.

• For all t > s.epoch + 2 and v ∈ uint64, s.acc_rpt(t, v).refcount = 0. • s.acc_rpt(t, 0).refcount = 0 for all t ∈ uint64. • For all t, v ∈ uint64, s.acc_rpt(t, v).refcount = 0 implies s.acc_rpt(t, v).value = 0. • For t1 , t2 , v ∈ uint64 such that t1 ≤ t2 , s.acc_rpt(t2 , v).refcount ̸= 0 implies s.acc_rpt(t1 , v).value ≤ s.acc_rpt(t2 , v).value. • For all t, v ∈ uint64, s.acc_rpt(t, v).value ≤ s.val_execution(v).acc_rpt. • For v ∈ uint64, a ∈ address, and i ∈ uint8, (︁ )︁ s.acc_rpt s.withdrawal_request(v, a, i ).epoch, v ≥ s.withdrawal_request(v, a, i ).acc. • For v ∈ uint64 and a ∈ address, – s.delegator(v, a).acc_rpt ≤ s.val_execution(v).acc_rpt. – 0 < s.delegator(v, a).delta_epoch ≤ s.epoch implies (︁ )︁ s.delegator(v, a).acc_rpt ≤ s.acc_rpt s.delegator(v, a).delta_epoch, v . – 0 < s.delegator(v, a).next_delta_epoch ≤ s.epoch implies (︁ )︁ s.delegator(v, a).acc_rpt ≤ s.acc_rpt s.delegator(v, a).next_delta_epoch, v . – 0 < s.delegator(v, a).next_delta_epoch ≤ s.epoch implies (︁ )︁ s.acc_rpt s.delegator(v, a).delta_epoch, v (︁ )︁ ≤ s.acc_rpt s.delegator(v, a).next_delta_epoch, v .

p. 15

3.5

Linked List Invariants

The following two invariants are required by the linked lists: • s.last_val_id < 264 − 1. • s.delegator(v, 2160 − 1).stake = 0 for all v ∈ uint64. The linked list invariants and later sections make use of the following linked list traversal sequences: (︁ )︁ • delegator_list(v, s) = A0 (v), A1 (v), . . . , for – v ∈ uint64, – A0 (v) = s.delegator(v, 2160 − 1).list_node.anext, {︄ s.delegator(v, Ai (v)).list_node.anext – A i +1 ( v ) = 0 (︁ )︁ • validator_list( a, s) = V0 ( a), V1 ( a), . . . , for

if Ai (v) ̸= 0 if Ai (v) = 0

– a ∈ address, – V0 ( a) = s.delegator(264 − 1, a).list_node.inext, {︄ s.delegator(Vi ( a), a).list_node.inext – Vi+1 ( a) = 0

if Vi ( a) ̸= 0 if Vi ( a) = 0

It is an invariant that these sequences converge to 0: • There exists an M ≥ 0 such that Ai (v) = 0 for all i ≥ M and all v ∈ uint64. • There exists an N ≥ 0 such that Vi ( a) = 0 for all i ≥ N and all a ∈ address. The final linked list invariants are: (︁ )︁ • The elements A0 (v), . . . , A M−1 (v) are pairwise distinct, for all v ∈ uint64. (︁ )︁ • The elements V0 ( a), . . . , VN −1 ( a) are pairwise distinct, for all a ∈ address. )︁ (︁ )︁ (︁ • v is in V0 ( a), . . . , VN −1 ( a) if and only if a is in A0 (v), . . . , A M−1 (v) , for all v, a. (︁ )︁ • a is in A0 (v), . . . , A M−1 (v) if and only if s.delegator(v, a).stake + s.delegator(v, a).delta_stake

+ s.delegator(v, a).next_delta_stake ̸= 0, for all v, a.

3.6

Solvency Invariants

The first solvency invariant expresses that stake and rewards are exactly accounted for by the balance of the staking contract: (︂ )︂ s.balance_of(STAKING_CA) = s.val_execution ( v ) .stake ∑ v∈uint64

p. 16

+

(︂

∑

s.val_execution(v).unclaimed_rewards

∑

s.delegator(v, a).rewards

∑

)︂ s.withdrawal_request(v, a, i ).amount .

)︂

v∈uint64

+

(︂

)︂

v∈uint64 a∈address

+

(︂

v∈uint64 a∈address i ∈uint8

Moreover, all pending rewards are accounted for, modulo rounding errors: for all v ∈ uint64,

∑

pending_rewards(v, a, s) ≤ s.val_execution(v).unclaimed_rewards

a∈address

≤ β+

∑

pending_rewards(v, a, s),

a∈address

where β ∈ uint256 is a rounding error upper bound, defined by β = s.error_bound (︂ + ∑

(︁ )︁)︂ I s.withdrawal_request(v, a, i ).amount

v∈uint64, a∈address, i ∈uint8

+

(︂

∑

(︁ )︁)︂ 3 · I d.stake + d.delta_stake + d.next_delta_stake ,

v∈uint64, a∈address

with d = s.delegator(v, a) in the last sum, and using {︄ 1 if x ̸= 0, I (x) = 0 if x = 0. The last solvency invariant states that a validator’s stake is the same as the delegated stake: (︂ )︂ s.val_execution(v).stake = s.delegator ( v, a ) .stake ∑ a∈address

+

(︂

+

(︂

)︂

∑

s.delegator(v, a).delta_stake

∑

)︂ s.delegator(v, a).next_delta_stake ,

a∈address

a∈address

for all v ∈ uint64.

4

Function Specifications

This section specifies some of the pre-conditions and post-conditions of the partial interface functions. The pre-conditions define the subset of the domain (inputs) on which the partial interface functions are defined. The post-conditions define a subset of the codomain (outputs)

p. 17

within which the results of the partial interface functions lie. Throughout, s̃ denotes the resulting (output) state. All the staking contract partial interface functions have pre-conditions and post-conditions related to the structure of their inputs and outputs. These are implicitly accounted for in the model by restricting the interface functions to specific domains and codomains (input and output sets). It is important to note that the specifications below include the transfer of MON balances involved in calling payable staking contract interface functions, which is needed by the solvency invariants. This transfer is not part of the staking contract itself; it is instead handled by Monad before entering the staking contract.

4.1

Specification of precompile_add_validator

precompile_add_validator : bytes × bytes × bytes × address × uint256 × State ⇀ uint64 × State The result of precompile_add_validator(m, b, c, α, µ, s) is defined if the following pre-conditions are satisfied: • m can be decoded into the following structure (decoding details are given in the source code):

(b0 , c0 , a, x, p) ∈ bytes × bytes × address × uint256 × uint256 • s.balance_of(STAKING_CA) + µ ≥ µ (no overflow) • s.last_val_id + 1 ≥ 1 (no overflow) • x=µ • µ ≤ UNIT_BIAS (this is a pre-condition because of error-bound tracking; it is not part of the C++ code) • µ ≥ MIN_VALIDATE_STAKE • µ ≥ DUST_THRESHOLD • b has the structure of a SECP signature • b0 has the structure of a SECP public key • the signature b verifies that b0 has signed the message m • c has the structure of a BLS signature • c0 has the structure of a BLS public key • the signature c verifies that c0 has signed the message m • p ≤ MAX_COMMISSION • s.val_id(y) = 0, for y the SECP address from b0 • s.val_id_bls(z) = 0, for z the BLS address from c0

p. 18

The post-conditions on

(v, s̃) = precompile_add_validator(m, b, c, α, µ, s) are: • s̃.balance_of(STAKING_CA) = s.balance_of(STAKING_CA) + µ • s̃.last_val_id = v = s.last_val_id + 1 • s̃.val_execution(v).keys.secp_pubkey = b0 • s̃.val_execution(v).keys.bls_pubkey = c0 • s̃.val_execution(v).commission = p • s̃.val_execution(v).stake = µ • s.in_epoch_delay_period = 1 implies s̃.delegator_stake(v, a, e) = µ for all e ≥ s.epoch + 2 • s.in_epoch_delay_period = 0 implies s̃.delegator_stake(v, a, e) = µ for all e ≥ s.epoch + 1 • v is an element of s̃.valset_execution implies µ ≥ ACTIVE_VALIDATOR_STAKE • a is an element of delegator_list(v, s̃)

4.2

Specification of precompile_delegate precompile_delegate : uint64 × address × uint256 × State ⇀ bool × State The pre-conditions of precompile_delegate(v, α, µ, s) are: • s.balance_of(STAKING_CA) + µ ≥ µ (no overflow) • µ = 0 or µ ≥ DUST_THRESHOLD • µ ̸= 0 implies s.val_execution(v) ̸= 0 • s.val_execution(v).stake + µ ≤ UNIT_BIAS (this is a pre-condition because of error-bound tracking) The post-conditions of (r, s̃) = precompile_delegate(v, α, µ, s) are: • r = 1, and if µ ̸= 0, the following post-conditions also hold: • s̃.balance_of(STAKING_CA) = s.balance_of(STAKING_CA) + µ • s.in_epoch_delay_period = 1 implies s̃.delegator_stake(v, α, e) s.delegator_stake(v, α, e) + µ for all e ≥ s.epoch + 2

=

• s.in_epoch_delay_period = 0 implies s̃.delegator_stake(v, α, e) s.delegator_stake(v, α, e) + µ for all e ≥ s.epoch + 1

=

p. 19

• s̃.val_execution(v).stake = s.val_execution(v).stake + µ • α is an element of delegator_list(v, s̃) • s̃.error_bound = s.error_bound + 3

4.3

Specification of precompile_undelegate

precompile_undelegate : uint64 × uint256 × uint8 × address × uint256 × State ⇀ bool × State The pre-conditions of precompile_undelegate(v, d, i, α, µ, s) are: • µ=0 • d ≤ σ1 + σ2 + σ3 • d > 0 implies – s.withdrawal_request(v, α, i ).amount = 0 and – s.epoch + WITHDRAWAL_DELAY + 2 ≥ s.epoch (no overflow) where • σ1 = s.delegator(v, α).stake, {︄ s.delegator(v, α).delta_stake if s.epoch ≤ δ • σ2 = 0 if s.epoch > δ for δ = s.delegator(v, α).delta_epoch, {︄ s.delegator(v, α).next_delta_stake if s.epoch ≤ δ • σ3 = 0 if s.epoch > δ for δ = s.delegator(v, α).next_delta_epoch. The post-conditions of (r, s̃) = precompile_undelegate(v, d, i, α, µ, s) are: • r = 1, and if d > 0, also the post-conditions: • s̃.withdrawal_request(v, α, i ).amount = d˜ • s.in_epoch_delay_period = 1 implies s̃.withdrawal_request(v, α, i ).epoch = s.epoch + 2 • s.in_epoch_delay_period = 0 implies s̃.withdrawal_request(v, α, i ).epoch = s.epoch + 1 • For s.epoch ≤ e < s̃.withdrawal_request(v, α, i ).epoch, s̃.withdrawal_stake(v, α, e) = s.withdrawal_stake(v, α, e) + d˜ • s̃.val_execution(v).stake = s.val_execution(v).stake − d˜

p. 20

• If both s̃.val_execution(v).stake ≥ ACTIVE_VALIDATOR_STAKE and s.delegator(v, α).stake + s.delegator(v, α).delta_stake

+ s.delegator(v, α).next_delta_stake ≥ MIN_VALIDATE_STAKE, then v is an element of s̃.valset_execution • For all e ≥ s.epoch, s̃.delegator_stake(v, α, e) = s.delegator_stake(v, α, e) − d˜ • σ1 = σ2 = σ3 = 0 implies that α is not in delegator_list(v, s̃) • s̃.error_bound = s.error_bound + 3 where

{︄ d˜ =

4.4

d

if x − d ≥ DUST_THRESHOLD

x

if x − d < DUST_THRESHOLD

for x = σ1 + σ2 + σ3 .

Specification of precompile_compound precompile_compound : uint64 × address × uint256 × State ⇀ bool × State The pre-conditions of precompile_compound(v, α, µ, s) are: • µ=0 • x = 0 or x ≥ DUST_THRESHOLD • s.val_execution(v).stake + x ≤ UNIT_BIAS (this is a pre-condition because of error-bound tracking) where x = s.delegator(v, α).rewards + unaccumulated_rewards(v, α, s). The post-conditions of (r, s̃) = precompile_compound(v, α, µ, s) are: • r = 1, and if x > 0, the additional post-conditions: • s.in_epoch_delay_period = 1 implies s̃.delegator_stake(v, α, e) s.delegator_stake(v, α, e) + x for all e ≥ s.epoch + 2

=

• s.in_epoch_delay_period = 0 implies s̃.delegator_stake(v, α, e) s.delegator_stake(v, α, e) + x for all e ≥ s.epoch + 1

=

• s̃.val_execution(v).stake = s.val_execution(v).stake + x • If both s̃.val_execution(v).stake ≥ ACTIVE_VALIDATOR_STAKE and s.delegator(v, α).stake + s.delegator(v, α).delta_stake

+ s.delegator(v, α).next_delta_stake ≥ MIN_VALIDATE_STAKE, then v is an element of s̃.valset_execution

p. 21

• s̃.error_bound = s.error_bound + 3 • s̃.unit_bias_rewards(v, α) = s.unit_bias_rewards(v, α) − x · UNIT_BIAS

4.5

Specification of precompile_withdraw

precompile_withdraw : uint64 × uint8 × address × uint256 × State ⇀ bool × State The pre-conditions of precompile_withdraw(v, i, α, µ, s) are: • µ=0 • s.withdrawal_request(v, α, i ).amount > 0 • s.epoch + WITHDRAWAL_DELAY ≥ WITHDRAWAL_DELAY (no overflow) • s.withdrawal_request(v, α, i ).epoch + WITHDRAWAL_DELAY ≤ s.epoch • s.balance_of(α) + y ≥ y (no overflow) where x = withdrawal_reward(v, α, i, s), y = s.withdrawal_request(v, α, i ).amount + x. The post-conditions of (r, s̃) = precompile_withdraw(v, i, α, µ, s) are: • s̃.balance_of(STAKING_CA) = s.balance_of(STAKING_CA) − y • s̃.balance_of(α) = s.balance_of(α) + y • s̃.val_execution(v).unclaimed_rewards = s.val_execution(v).unclaimed_rewards − x • s̃.error_bound = s.error_bound + 1 • s̃.unit_bias_rewards(v, α) = s.unit_bias_rewards(v, α) − x · UNIT_BIAS

4.6

Specification of precompile_change_commission precompile_change_commission : uint64 × uint256 × address × uint256 × State ⇀ bool × State The pre-conditions of precompile_change_commission(v, p, α, µ, s) are: • µ=0 • α = s.val_execution(v).auth_address • p ≤ MAX_COMMISSION The post-conditions of (r, s̃) = precompile_change_commission(v, p, α, µ, s) are: • r=1 • s̃.val_execution(v).commission = p

p. 22

4.7

Specification of precompile_claim_rewards precompile_claim_rewards : uint64 × address × uint256 × State ⇀ bool × State The pre-conditions of precompile_claim_rewards(v, α, µ, s) are: • µ=0 The post-conditions of (r, s̃) = precompile_claim_rewards(v, α, µ, s) are: • r=1 • s̃.balance_of(α) = s.balance_of(α) + x • s̃.balance_of(STAKING_CA) = s.balance_of(STAKING_CA) − x • s̃.error_bound = s.error_bound + 3 • s̃.unit_bias_rewards(v, α) = s.unit_bias_rewards(v, α) − x · UNIT_BIAS where x = s.delegator(v, α).rewards + unaccumulated_rewards(v, α, s).

4.8

Specification of precompile_external_reward

precompile_external_reward : uint64 × address × uint256 × State ⇀ bool × State The pre-conditions of precompile_external_reward(v, α, µ, s) are: • s.active_consensus_view(v).stake ̸= 0 • MIN_EXTERNAL_REWARD ≤ µ ≤ MAX_EXTERNAL_REWARD The post-conditions of (r, s̃) = precompile_external_reward(v, α, µ, s) are: • r=1 • s̃.error_bound = s.error_bound + 1 • For all a ∈ address, s̃.unit_bias_rewards(v, a) = s.unit_bias_rewards(v, a) + R · D ( a) • s̃.val_execution(v).unclaimed_rewards = s.val_execution(v).unclaimed_rewards + µ where • R = (µ · UNIT_BIAS)/s.active_consensus_view(v).stake and • D ( a) = s.delegator_stake(v, a, s.epoch) + s.withdrawal_stake(v, a, s.epoch).

p. 23

4.9

Specification of syscall_reward syscall_reward : uint64 × uint256 × State ⇀ State The pre-conditions of syscall_reward(v, ρ, s) are: • s.active_consensus_view(v).stake ̸= 0 The post-conditions of s̃ = syscall_reward(v, ρ, s) are: • s̃.error_bound = s.error_bound + 1 • s̃.unit_bias_rewards(v, A) = s.unit_bias_rewards(v, A) + c · UNIT_BIAS + R · D ( A) • For all a ∈ address such that a ̸= s.unit_bias_rewards(v, a) + R · D ( a)

A, s̃.unit_bias_rewards(v, a)

=

• s̃.val_execution(v).unclaimed_rewards = s.val_execution(v).unclaimed_rewards + ρ−c • s̃.proposer_val_id = v where • A = s.val_execution(v).auth_address, • c = (ρ · s.active_consensus_view(v).commission)/MON, • R = ((ρ − c) · UNIT_BIAS)/s.active_consensus_view(v).stake, and • D ( a) = s.delegator_stake(v, a, s.epoch) + s.withdrawal_stake(v, a, s.epoch).

4.10

Specification of syscall_snapshot syscall_snapshot : State ⇀ State

The pre-conditions of syscall_snapshot(s) are: • s.in_epoch_delay_period = 0 • s.epoch ̸= 0 (not part of the C++ code) The post-conditions of s̃ = syscall_snapshot(s) are: • s̃.in_epoch_delay_period = 1 • For all v ∈ uint64, v an element of s̃.valset_execution implies s̃.val_execution(v).stake ≥ ACTIVE_VALIDATOR_STAKE • For all v, u ∈ uint64, if v is in s.valset_consensus and u is not in s.valset_consensus, then either – s.val_execution(v).stake ≥ s.val_execution(u).stake, or

p. 24

– the inequality s.delegator(u, A).stake + s.delegator(u, A).delta_stake

+ s.delegator(u, A).next_delta_stake < MIN_VALIDATE_STAKE holds, for A = s.val_execution(u).auth_address.

4.11

Specification of syscall_on_epoch_change syscall_on_epoch_change : uint64 × State ⇀ State

The pre-conditions of syscall_on_epoch_change(e, s) are: • e > s.epoch • The following pre-condition is not part of the C++ code: if s.epoch ̸= 0 (if bootstrapped), then – e = s.epoch + 1 and – s.in_epoch_delay_period = 1. The post-conditions of s̃ = syscall_on_epoch_change(e, s) are: • s̃.in_epoch_delay_period = 0 • s̃.epoch = e • For all v ∈ uint64, s̃.active_consensus_view(v) = s̃.consensus_view(v)

4.12

Specification of precompile_get_delegator

The precompile_get_delegator function increases error_bound by 3 and increases the delegator’s rewards field by unaccumulated_rewards, after which unaccumulated_rewards becomes zero.

4.13

Specification of distribute_priority_fees distribute_priority_fees : uint256 × State ⇀ State

The pre-conditions of distribute_priority_fees(ρ, s) are: • v ̸= 0 • s.active_consensus_view(v).stake ̸= 0 where • v = s.proposer_val_id,

p. 25

• c = (ρ · s.active_consensus_view(v).commission)/MON, • R = ((ρ − c) · UNIT_BIAS)/s.active_consensus_view(v).stake, • D ( a) = s.delegator_stake(v, a, s.epoch) + s.withdrawal_stake(v, a, s.epoch), and • A = s.val_execution(v).auth_address. The post-conditions of s̃ = distribute_priority_fees(ρ, s) are: • If ρ − c ≥ MIN_EXTERNAL_REWARD, then – s̃.balance_of(STAKING_CA) = s.balance_of(STAKING_CA) + ρ, – s̃.error_bound = s.error_bound + 1, – s̃.unit_bias_rewards(v, A) = s.unit_bias_rewards(v, A) + c · UNIT_BIAS + R · D ( A), – for all a ∈ address such that a ̸= A, s̃.unit_bias_rewards(v, a) = s.unit_bias_rewards(v, a) + R · D ( a), and – s̃.val_execution(v).unclaimed_rewards = s.val_execution(v).unclaimed_rewards + ρ − c. • Otherwise, if ρ − c < MIN_EXTERNAL_REWARD, then – s̃.balance_of(STAKING_CA) = s.balance_of(STAKING_CA) + c, – s̃.error_bound = s.error_bound, – s̃.unit_bias_rewards(v, A) = s.unit_bias_rewards(v, A) + c · UNIT_BIAS, – s̃.unit_bias_rewards(v, a) = s.unit_bias_rewards(v, a) for all a ̸= A, and – s̃.val_execution(v).unclaimed_rewards = s.val_execution(v).unclaimed_rewards.