Library QedkConcrete.reserveconfigtests
Reserve increases: activation and usable consensus budgets
From Stdlib Require Import List String NArith ZArith Lia.
Require Import QedkConcrete.consensuscorrectness QedkConcrete.reservestorage
QedkConcrete.reservewrites QedkConcrete.consensusdecisions
QedkConcrete.reserveconfigfixture QedkConcrete.consensushistory
QedkConcrete.authorizationnonces QedkConcrete.transactionchecks
QedkConcrete.worldaccounts QedkConcrete.publictransaction.
Set Default Goal Selector "!".
Import ListNotations.
Local Open Scope N_scope.
#[local] Hint Extern 100 (Basics.Unreachable _) ⇒
(apply Opaque.legacy_witness; Opaque.opaque_inhabit 64%nat) : typeclass_instances.
Module C := MonadVM.Reserve_config.
Module RB := MonadVM.Reserve_balance.
Module U := MonadVM.Numeric.U256.
#[local] Instance executor : E.FArgs := executionArgs
{| Execution.Make_Params_signature.chain_id := 1;
Execution.Make_Params_signature.revision := Support.Variants.Revision.Eleven;
Execution.Make_Params_signature.trace := false |}.
Require Import QedkConcrete.consensuscorrectness QedkConcrete.reservestorage
QedkConcrete.reservewrites QedkConcrete.consensusdecisions
QedkConcrete.reserveconfigfixture QedkConcrete.consensushistory
QedkConcrete.authorizationnonces QedkConcrete.transactionchecks
QedkConcrete.worldaccounts QedkConcrete.publictransaction.
Set Default Goal Selector "!".
Import ListNotations.
Local Open Scope N_scope.
#[local] Hint Extern 100 (Basics.Unreachable _) ⇒
(apply Opaque.legacy_witness; Opaque.opaque_inhabit 64%nat) : typeclass_instances.
Module C := MonadVM.Reserve_config.
Module RB := MonadVM.Reserve_balance.
Module U := MonadVM.Numeric.U256.
#[local] Instance executor : E.FArgs := executionArgs
{| Execution.Make_Params_signature.chain_id := 1;
Execution.Make_Params_signature.revision := Support.Variants.Revision.Eleven;
Execution.Make_Params_signature.trace := false |}.
Amounts are in wei; alice is one concrete 20-byte address.
Definition oldReserve : N := 10 × 10^18.
Definition increasedReserve : N := 100 × 10^18.
Definition alice : Addr.t := signedAlice.
Definition increasedReserve : N := 100 × 10^18.
Definition alice : Addr.t := signedAlice.
The only funded account has 100 MON. Reserve storage is initially empty,
so its effective reserve is the default 10 MON. Nonce 1 represents the
setting transaction's nonce 0 already consumed. The block-100 setting
below calls the generated storage setter directly; it does not execute
that transaction or account for its fee.
Definition beforeSetting : W.t :=
{| W.t.history := [];
W.t.accounts := [(alice, A.t.with_nonce 1%Z
(A.t.with_balance (Z.of_N increasedReserve) A.empty))];
W.t.next_emptying_transaction_block := [] |}.
Definition increasedRecord : C.t :=
{| C.t.settled := oldReserve; C.t.pending := increasedReserve;
C.t.valid_from := 103 |}.
{| W.t.history := [];
W.t.accounts := [(alice, A.t.with_nonce 1%Z
(A.t.with_balance (Z.of_N increasedReserve) A.empty))];
W.t.next_emptying_transaction_block := [] |}.
Definition increasedRecord : C.t :=
{| C.t.settled := oldReserve; C.t.pending := increasedReserve;
C.t.valid_from := 103 |}.
Return the setter's post-state. setting_succeeds proves that the failure
alternative is not selected; this fixture does not assume setter success.
Definition afterSetting : W.t :=
match RB.set_reserve_balance beforeSetting 100 alice (Z.of_N increasedReserve) with
| Some post ⇒ post
| None ⇒ beforeSetting
end.
Lemma initial_accounts_wellformed : stringmaps.WellFormed (W.t.accounts beforeSetting).
Proof. repeat constructor. Qed.
Lemma initial_reserve_storage_empty :
A.t.storage (accountAt beforeSetting RB.precompile_address) = [].
Proof. vm_compute. reflexivity. Qed.
Lemma initial_record_unconfigured :
RB.stored_config beforeSetting alice =
{| C.t.settled := 0; C.t.pending := 0; C.t.valid_from := 0 |}.
Proof.
unfold RB.stored_config. change (A.t.storage _) with
(A.t.storage (accountAt beforeSetting RB.precompile_address)).
rewrite initial_reserve_storage_empty. vm_compute. reflexivity.
Qed.
Example setting_computes_activation_height :
C.set RB.execution_consensus_delay 100 (U.to_uint (Z.of_N increasedReserve))
(RB.stored_config beforeSetting alice) = Some increasedRecord.
Proof. rewrite initial_record_unconfigured. vm_compute. reflexivity. Qed.
Example setting_succeeds :
RB.set_reserve_balance beforeSetting 100 alice (Z.of_N increasedReserve) =
Some afterSetting.
Proof.
unfold afterSetting, RB.set_reserve_balance.
rewrite setting_computes_activation_height. reflexivity.
Qed.
Example setting_stores_pending_increase :
RB.stored_config afterSetting alice = increasedRecord.
Proof.
eapply setter_reads_record; [exact initial_accounts_wellformed| | | | | |].
- change (stringmaps.WellFormed (A.t.storage
(accountAt beforeSetting RB.precompile_address))).
rewrite initial_reserve_storage_empty. apply stringmaps.empty_wellformed.
- vm_compute. reflexivity.
- vm_compute. reflexivity.
- vm_compute. reflexivity.
- exact setting_computes_activation_height.
- exact setting_succeeds.
Qed.
match RB.set_reserve_balance beforeSetting 100 alice (Z.of_N increasedReserve) with
| Some post ⇒ post
| None ⇒ beforeSetting
end.
Lemma initial_accounts_wellformed : stringmaps.WellFormed (W.t.accounts beforeSetting).
Proof. repeat constructor. Qed.
Lemma initial_reserve_storage_empty :
A.t.storage (accountAt beforeSetting RB.precompile_address) = [].
Proof. vm_compute. reflexivity. Qed.
Lemma initial_record_unconfigured :
RB.stored_config beforeSetting alice =
{| C.t.settled := 0; C.t.pending := 0; C.t.valid_from := 0 |}.
Proof.
unfold RB.stored_config. change (A.t.storage _) with
(A.t.storage (accountAt beforeSetting RB.precompile_address)).
rewrite initial_reserve_storage_empty. vm_compute. reflexivity.
Qed.
Example setting_computes_activation_height :
C.set RB.execution_consensus_delay 100 (U.to_uint (Z.of_N increasedReserve))
(RB.stored_config beforeSetting alice) = Some increasedRecord.
Proof. rewrite initial_record_unconfigured. vm_compute. reflexivity. Qed.
Example setting_succeeds :
RB.set_reserve_balance beforeSetting 100 alice (Z.of_N increasedReserve) =
Some afterSetting.
Proof.
unfold afterSetting, RB.set_reserve_balance.
rewrite setting_computes_activation_height. reflexivity.
Qed.
Example setting_stores_pending_increase :
RB.stored_config afterSetting alice = increasedRecord.
Proof.
eapply setter_reads_record; [exact initial_accounts_wellformed| | | | | |].
- change (stringmaps.WellFormed (A.t.storage
(accountAt beforeSetting RB.precompile_address))).
rewrite initial_reserve_storage_empty. apply stringmaps.empty_wellformed.
- vm_compute. reflexivity.
- vm_compute. reflexivity.
- vm_compute. reflexivity.
- exact setting_computes_activation_height.
- exact setting_succeeds.
Qed.
The setter changes configuration, not Alice's balance.
Lemma setting_preserves_alice_balance :
U.to_z (A.t.balance (accountAt afterSetting alice)) = Z.of_N increasedReserve.
Proof.
unfold accountAt.
rewrite (setter_preserves_balances beforeSetting 100 alice
(Z.of_N increasedReserve) afterSetting alice initial_accounts_wellformed setting_succeeds).
vm_compute. reflexivity.
Qed.
Example increase_not_effective_at_102 :
reserveAt afterSetting 102 alice = Z.of_N oldReserve.
Proof.
unfold reserveAt, RB.user_reserve_balance.
rewrite setting_stores_pending_increase. vm_compute. reflexivity.
Qed.
Example increase_effective_at_103 :
reserveAt afterSetting 103 alice = Z.of_N increasedReserve.
Proof.
unfold reserveAt, RB.user_reserve_balance.
rewrite setting_stores_pending_increase. vm_compute. reflexivity.
Qed.
U.to_z (A.t.balance (accountAt afterSetting alice)) = Z.of_N increasedReserve.
Proof.
unfold accountAt.
rewrite (setter_preserves_balances beforeSetting 100 alice
(Z.of_N increasedReserve) afterSetting alice initial_accounts_wellformed setting_succeeds).
vm_compute. reflexivity.
Qed.
Example increase_not_effective_at_102 :
reserveAt afterSetting 102 alice = Z.of_N oldReserve.
Proof.
unfold reserveAt, RB.user_reserve_balance.
rewrite setting_stores_pending_increase. vm_compute. reflexivity.
Qed.
Example increase_effective_at_103 :
reserveAt afterSetting 103 alice = Z.of_N increasedReserve.
Proof.
unfold reserveAt, RB.user_reserve_balance.
rewrite setting_stores_pending_increase. vm_compute. reflexivity.
Qed.
No further setting or promotion is needed at later heights either.
Example increase_remains_effective b : 103 ≤ b →
reserveAt afterSetting b alice = Z.of_N increasedReserve.
Proof.
intro Hb. unfold reserveAt, RB.user_reserve_balance.
rewrite setting_stores_pending_increase.
change (U.of_uint_exn (if N.ltb b 103 then oldReserve
else increasedReserve) = Z.of_N increasedReserve).
rewrite (proj2 (N.ltb_ge b 103) Hb). vm_compute. reflexivity.
Qed.
reserveAt afterSetting b alice = Z.of_N increasedReserve.
Proof.
intro Hb. unfold reserveAt, RB.user_reserve_balance.
rewrite setting_stores_pending_increase.
change (U.of_uint_exn (if N.ltb b 103 then oldReserve
else increasedReserve) = Z.of_N increasedReserve).
rewrite (proj2 (N.ltb_ge b 103) Hb). vm_compute. reflexivity.
Qed.
The getter reports no pending change once it has activated; the original
stored record remains increasedRecord, as proved above.
Example getter_reports_effective_increase :
C.read (RB.stored_config afterSetting alice) 103 =
{| C.t.settled := increasedReserve; C.t.pending := increasedReserve;
C.t.valid_from := 0 |}.
Proof. rewrite setting_stores_pending_increase. vm_compute. reflexivity. Qed.
C.read (RB.stored_config afterSetting alice) 103 =
{| C.t.settled := increasedReserve; C.t.pending := increasedReserve;
C.t.valid_from := 0 |}.
Proof. rewrite setting_stores_pending_increase. vm_compute. reflexivity. Qed.
An empty block at height b, used only by the consensus checks. Other
header fields are immaterial here; this is not a native block-validity test.
Definition emptyBlock (b : N) : Block.t :=
Block.t.with_header (Header.t.with_number b Header.empty) Block.empty.
Block.t.with_header (Header.t.with_number b Header.empty) Block.empty.
Initialize at height, then run the actual consensus checks on empty
blocks at heights. Return Alice's final gas budget, or None if rejected.
afterSetting supplies unchanged account balances and configuration in
each example; no execution of these blocks is being assumed or tested.
Definition budgetAfterEmptyBlocks (height : N) (heights : list N) : option Z :=
option_map (fun next ⇒ consensusBudgets next alice)
(checkBlocks afterSetting (initialConsensusState afterSetting height)
(map emptyBlock heights)).
option_map (fun next ⇒ consensusBudgets next alice)
(checkBlocks afterSetting (initialConsensusState afterSetting height)
(map emptyBlock heights)).
At block 103 the reserve has increased, but an existing gas budget is
only capped, not replenished. The activation block may be empty.
Example snapshot_100_budget_stays_10 :
budgetAfterEmptyBlocks 100 [101; 102; 103] = Some (10 × 10^18)%Z.
Proof.
cbn [budgetAfterEmptyBlocks map emptyBlock checkBlocks checkTransactions
initialConsensusState consensusBudgets Block.t.with_header
Header.t.with_number Header.empty Block.empty Block.t.header
Block.t.transactions Header.t.number option_map].
rewrite setting_preserves_alice_balance.
unfold reserveAt, RB.user_reserve_balance.
rewrite !setting_stores_pending_increase. vm_compute. reflexivity.
Qed.
budgetAfterEmptyBlocks 100 [101; 102; 103] = Some (10 × 10^18)%Z.
Proof.
cbn [budgetAfterEmptyBlocks map emptyBlock checkBlocks checkTransactions
initialConsensusState consensusBudgets Block.t.with_header
Header.t.with_number Header.empty Block.empty Block.t.header
Block.t.transactions Header.t.number option_map].
rewrite setting_preserves_alice_balance.
unfold reserveAt, RB.user_reserve_balance.
rewrite !setting_stores_pending_increase. vm_compute. reflexivity.
Qed.
A snapshot before activation still initializes the old budget, even when
all candidate blocks are at or after activation.
Example snapshot_102_budget_stays_10 :
budgetAfterEmptyBlocks 102 [103; 104; 105] = Some (10 × 10^18)%Z.
Proof.
cbn [budgetAfterEmptyBlocks map emptyBlock checkBlocks checkTransactions
initialConsensusState consensusBudgets Block.t.with_header
Header.t.with_number Header.empty Block.empty Block.t.header
Block.t.transactions Header.t.number option_map].
rewrite setting_preserves_alice_balance.
unfold reserveAt, RB.user_reserve_balance.
rewrite !setting_stores_pending_increase. vm_compute. reflexivity.
Qed.
budgetAfterEmptyBlocks 102 [103; 104; 105] = Some (10 × 10^18)%Z.
Proof.
cbn [budgetAfterEmptyBlocks map emptyBlock checkBlocks checkTransactions
initialConsensusState consensusBudgets Block.t.with_header
Header.t.with_number Header.empty Block.empty Block.t.header
Block.t.transactions Header.t.number option_map].
rewrite setting_preserves_alice_balance.
unfold reserveAt, RB.user_reserve_balance.
rewrite !setting_stores_pending_increase. vm_compute. reflexivity.
Qed.
From snapshot 103, initialization reads the increased reserve. Checking
the next three empty blocks preserves the full 100 MON budget.
Example snapshot_103_budget_is_100 :
budgetAfterEmptyBlocks 103 [104; 105; 106] = Some (100 × 10^18)%Z.
Proof.
cbn [budgetAfterEmptyBlocks map emptyBlock checkBlocks checkTransactions
initialConsensusState consensusBudgets Block.t.with_header
Header.t.with_number Header.empty Block.empty Block.t.header
Block.t.transactions Header.t.number option_map].
rewrite setting_preserves_alice_balance.
unfold reserveAt, RB.user_reserve_balance.
rewrite !setting_stores_pending_increase. vm_compute. reflexivity.
Qed.
budgetAfterEmptyBlocks 103 [104; 105; 106] = Some (100 × 10^18)%Z.
Proof.
cbn [budgetAfterEmptyBlocks map emptyBlock checkBlocks checkTransactions
initialConsensusState consensusBudgets Block.t.with_header
Header.t.with_number Header.empty Block.empty Block.t.header
Block.t.transactions Header.t.number option_map].
rewrite setting_preserves_alice_balance.
unfold reserveAt, RB.user_reserve_balance.
rewrite !setting_stores_pending_increase. vm_compute. reflexivity.
Qed.
These two examples isolate the fee-budget subcheck of a non-emptying
transaction (including a transaction with an applied self-authorization).
With no preceding fee debits, a fee of 20 MON fails using snapshot 102,
but succeeds using snapshot 103. Nonce, signature and other transaction
checks are separate; these statements do not assert whole-tx acceptance.
Example ordinary_fee_20_rejected_from_snapshot_102 :
match budgetAfterEmptyBlocks 102 [103; 104; 105] with
| None ⇒ None
| Some previous ⇒ checkSenderBudget false
(U.to_z (A.t.balance (accountAt afterSetting alice)))
(U.to_z (reserveAt afterSetting 105 alice)) previous (20 × 10^18) 0
end = None.
Proof. rewrite snapshot_102_budget_stays_10. vm_compute. reflexivity. Qed.
Example ordinary_fee_20_accepted_from_snapshot_103 :
match budgetAfterEmptyBlocks 103 [104; 105; 106] with
| None ⇒ None
| Some previous ⇒ checkSenderBudget false
(U.to_z (A.t.balance (accountAt afterSetting alice)))
(U.to_z (reserveAt afterSetting 106 alice)) previous (20 × 10^18) 0
end = Some (80 × 10^18)%Z.
Proof. rewrite snapshot_103_budget_is_100. vm_compute. reflexivity. Qed.
match budgetAfterEmptyBlocks 102 [103; 104; 105] with
| None ⇒ None
| Some previous ⇒ checkSenderBudget false
(U.to_z (A.t.balance (accountAt afterSetting alice)))
(U.to_z (reserveAt afterSetting 105 alice)) previous (20 × 10^18) 0
end = None.
Proof. rewrite snapshot_102_budget_stays_10. vm_compute. reflexivity. Qed.
Example ordinary_fee_20_accepted_from_snapshot_103 :
match budgetAfterEmptyBlocks 103 [104; 105; 106] with
| None ⇒ None
| Some previous ⇒ checkSenderBudget false
(U.to_z (A.t.balance (accountAt afterSetting alice)))
(U.to_z (reserveAt afterSetting 106 alice)) previous (20 × 10^18) 0
end = Some (80 × 10^18)%Z.
Proof. rewrite snapshot_103_budget_is_100. vm_compute. reflexivity. Qed.
The setter leaves Alice's full account unchanged: in particular its
nonce and code, needed by the complete transaction checks.
Lemma setting_preserves_alice_account :
accountAt afterSetting alice = accountAt beforeSetting alice.
Proof.
unfold afterSetting, RB.set_reserve_balance.
rewrite setting_computes_activation_height. cbn beta iota zeta.
match goal with
| |- accountAt (State.op_pointcaretlparenrparenltminus _ _ ?storage) alice = _ ⇒
let slots := fresh "slots" in generalize storage; intro slots
end.
match goal with
| |- accountAt (State.op_pointcaretlparenrparenltminus ?world ?lens ?storage) alice = _ ⇒
change (accountAt (State.op_pointcaretlparenrparenltminus world
(@W.account (Some false) RB.precompile_address)
(A.storage.(Lens.t.set) storage (accountAt world RB.precompile_address)))
alice = accountAt beforeSetting alice)
end.
unfold accountAt.
rewrite account_read_after_write by
(apply (@account_write_wellformed false); exact initial_accounts_wellformed).
change (String.eqb RB.precompile_address alice) with false. cbn iota.
unfold RB.activate_precompile.
change (State.op_pointcaretlparenrparen
(State.op_pointcaretlparenrparenltminus beforeSetting
(@W.account (Some false) RB.precompile_address)
(A.t.with_nonce (Numeric.U64.max
(A.t.nonce (accountAt beforeSetting RB.precompile_address)) Numeric.U64.one)
(accountAt beforeSetting RB.precompile_address))) (@W.account None alice) =
State.op_pointcaretlparenrparen beforeSetting (@W.account None alice)).
rewrite account_read_after_write by exact initial_accounts_wellformed.
change (String.eqb RB.precompile_address alice) with false. reflexivity.
Qed.
accountAt afterSetting alice = accountAt beforeSetting alice.
Proof.
unfold afterSetting, RB.set_reserve_balance.
rewrite setting_computes_activation_height. cbn beta iota zeta.
match goal with
| |- accountAt (State.op_pointcaretlparenrparenltminus _ _ ?storage) alice = _ ⇒
let slots := fresh "slots" in generalize storage; intro slots
end.
match goal with
| |- accountAt (State.op_pointcaretlparenrparenltminus ?world ?lens ?storage) alice = _ ⇒
change (accountAt (State.op_pointcaretlparenrparenltminus world
(@W.account (Some false) RB.precompile_address)
(A.storage.(Lens.t.set) storage (accountAt world RB.precompile_address)))
alice = accountAt beforeSetting alice)
end.
unfold accountAt.
rewrite account_read_after_write by
(apply (@account_write_wellformed false); exact initial_accounts_wellformed).
change (String.eqb RB.precompile_address alice) with false. cbn iota.
unfold RB.activate_precompile.
change (State.op_pointcaretlparenrparen
(State.op_pointcaretlparenrparenltminus beforeSetting
(@W.account (Some false) RB.precompile_address)
(A.t.with_nonce (Numeric.U64.max
(A.t.nonce (accountAt beforeSetting RB.precompile_address)) Numeric.U64.one)
(accountAt beforeSetting RB.precompile_address))) (@W.account None alice) =
State.op_pointcaretlparenrparen beforeSetting (@W.account None alice)).
rewrite account_read_after_write by exact initial_accounts_wellformed.
change (String.eqb RB.precompile_address alice) with false. reflexivity.
Qed.