Library SetcodeConcrete.reserve
What the MIP-19 execution reserve check protects
From Stdlib Require Import ZArith NArith List Bool Lia.
Require Import RocqOfOCaml.RocqOfOCaml MonadVM.Reserve_balance
QedkConcrete.reservecheck.
Set Default Goal Selector "!".
Local Open Scope Z_scope.
Module S := State.TransactionState.
Module W := State.WorldState.
Module A := Chain.Ethereum.Account.
Module Addr := Chain.Ethereum.Address.
Module U := Numeric.U256.
Module Uint := Support.UintN.
Module Seq := RocqOfOCaml.OCamlSeq.
Require Import RocqOfOCaml.RocqOfOCaml MonadVM.Reserve_balance
QedkConcrete.reservecheck.
Set Default Goal Selector "!".
Local Open Scope Z_scope.
Module S := State.TransactionState.
Module W := State.WorldState.
Module A := Chain.Ethereum.Account.
Module Addr := Chain.Ethereum.Address.
Module U := Numeric.U256.
Module Uint := Support.UintN.
Module Seq := RocqOfOCaml.OCamlSeq.
s is the transaction state at the reserve check; addr is the account
to inspect. Return its current account record, or the native empty
account if absent. This is not the transaction-entry account record.
Definition accountAt s addr : A.t :=
match Addr.Map.find_opt addr (W.t.accounts (S.t.world_state s)) with
| Some a ⇒ a | None ⇒ A.empty end.
match Addr.Map.find_opt addr (W.t.accounts (S.t.world_state s)) with
| Some a ⇒ a | None ⇒ A.empty end.
original is addr's account record before the transaction, including
before its fee debit. Return the smaller of that balance and the reserve
configured in s.initial_world_state for s.current_block's height.
A pending setting is effective when its activation height is reached.
Definition protected s addr original : U.t := U.min
(Reserve_balance.user_reserve_balance (S.t.initial_world_state s)
(Chain.Ethereum.Block.Header.t.number
(Chain.Ethereum.Block.t.header (S.t.current_block s))) addr)
(A.t.balance original).
(Reserve_balance.user_reserve_balance (S.t.initial_world_state s)
(Chain.Ethereum.Block.Header.t.number
(Chain.Ethereum.Block.t.header (S.t.current_block s))) addr)
(A.t.balance original).
For the current sender in s, return whether emptying is allowed after
processing the transaction's authorizations. The existing activity check
is necessary, and MIP-19 additionally requires currently empty code.
Definition mayEmpty s : bool :=
fst (Reserve_balance.is_emptying_tx s) &&
Byte_string.Bytes.op_eq (A.t.code (accountAt s (S.t.tx_origin s))) Byte_string.Bytes.empty.
fst (Reserve_balance.is_emptying_tx s) &&
Byte_string.Bytes.op_eq (A.t.code (accountAt s (S.t.tx_origin s))) Byte_string.Bytes.empty.
For an account not exempted by the generated check, return whether post
violates its protected balance. Sender fees are deducted from the
protected amount only after checking that the fee fits; other accounts
must retain the entire amount. s supplies sender, gas fee and height.
Definition reserveViolated s addr original (post : A.t) : bool :=
let reserve := protected s addr original in
if Addr.op_eq addr (S.t.tx_origin s) then
if mayEmpty s then false else
let fee := (S.t.tx_gas_limit s × S.t.tx_gas_price s)%N in
Uint.op_gt fee (U.to_uint reserve) ||
U.op_lt (A.t.balance post) (U.op_minus reserve (U.of_uint_exn fee))
else U.op_lt (A.t.balance post) reserve.
Lemma checked_account prices s addr original :
fst (Reserve_balance.dipped_into_reserve (Support.Variants.Revision.Setcode prices) s) = false →
In (addr, original) (W.t.accounts (S.t.initial_world_state s)) →
Addr._Set.(OCamlSet.S.mem) addr (S.t.self_destruct s) = false →
Setcode_rules.contract_exempt (A.is_smart_contract (accountAt s addr))
(Addr.op_eq addr (S.t.tx_origin s)) (Reserve_balance.sent_in_window s addr) = false →
reserveViolated s addr original (accountAt s addr) = false.
Proof.
intro Hcheck.
change (Seq._exists
(fun '(a, old, post) ⇒ reserveViolated s a old post)
(Seq.filter_map
(fun '(a, old) ⇒ if Setcode_rules.contract_exempt (A.is_smart_contract (accountAt s a))
(Addr.op_eq a (S.t.tx_origin s)) (Reserve_balance.sent_in_window s a)
then None else Some (a, old, accountAt s a))
(Seq.filter (fun '(a, _) ⇒ negb
(Addr._Set.(OCamlSet.S.mem) a (S.t.self_destruct s)))
(Seq.of_list (W.t.accounts (S.t.initial_world_state s))))) = false) in Hcheck.
intros Hin Hdes Hexempt.
refine (seq_checked_member _ _ _ _ (addr, original)
(addr, original, accountAt s addr) Hcheck Hin _ _).
- cbn beta iota zeta. rewrite Hdes. reflexivity.
- cbn beta iota zeta. rewrite Hexempt. reflexivity.
Qed.
let reserve := protected s addr original in
if Addr.op_eq addr (S.t.tx_origin s) then
if mayEmpty s then false else
let fee := (S.t.tx_gas_limit s × S.t.tx_gas_price s)%N in
Uint.op_gt fee (U.to_uint reserve) ||
U.op_lt (A.t.balance post) (U.op_minus reserve (U.of_uint_exn fee))
else U.op_lt (A.t.balance post) reserve.
Lemma checked_account prices s addr original :
fst (Reserve_balance.dipped_into_reserve (Support.Variants.Revision.Setcode prices) s) = false →
In (addr, original) (W.t.accounts (S.t.initial_world_state s)) →
Addr._Set.(OCamlSet.S.mem) addr (S.t.self_destruct s) = false →
Setcode_rules.contract_exempt (A.is_smart_contract (accountAt s addr))
(Addr.op_eq addr (S.t.tx_origin s)) (Reserve_balance.sent_in_window s addr) = false →
reserveViolated s addr original (accountAt s addr) = false.
Proof.
intro Hcheck.
change (Seq._exists
(fun '(a, old, post) ⇒ reserveViolated s a old post)
(Seq.filter_map
(fun '(a, old) ⇒ if Setcode_rules.contract_exempt (A.is_smart_contract (accountAt s a))
(Addr.op_eq a (S.t.tx_origin s)) (Reserve_balance.sent_in_window s a)
then None else Some (a, old, accountAt s a))
(Seq.filter (fun '(a, _) ⇒ negb
(Addr._Set.(OCamlSet.S.mem) a (S.t.self_destruct s)))
(Seq.of_list (W.t.accounts (S.t.initial_world_state s))))) = false) in Hcheck.
intros Hin Hdes Hexempt.
refine (seq_checked_member _ _ _ _ (addr, original)
(addr, original, accountAt s addr) Hcheck Hin _ _).
- cbn beta iota zeta. rewrite Hdes. reflexivity.
- cbn beta iota zeta. rewrite Hexempt. reflexivity.
Qed.
Recent senders retain their full protected amount when called by another
transaction, even after adoption. No non-contract-code premise is needed.
Theorem recent_nonsender_retains_reserve prices s addr original :
fst (Reserve_balance.dipped_into_reserve (Support.Variants.Revision.Setcode prices) s) = false →
In (addr, original) (W.t.accounts (S.t.initial_world_state s)) →
Addr._Set.(OCamlSet.S.mem) addr (S.t.self_destruct s) = false →
Addr.op_eq addr (S.t.tx_origin s) = false →
Reserve_balance.sent_in_window s addr = true →
protected s addr original ≤ A.t.balance (accountAt s addr).
Proof.
intros Hcheck Hin Hdes Hsender Hrecent.
assert (Hexempt : Setcode_rules.contract_exempt (A.is_smart_contract (accountAt s addr))
(Addr.op_eq addr (S.t.tx_origin s)) (Reserve_balance.sent_in_window s addr) = false).
{ unfold Setcode_rules.contract_exempt. rewrite Hrecent. now rewrite Bool.andb_false_r, Bool.andb_false_r. }
pose proof (checked_account prices s addr original Hcheck Hin Hdes Hexempt) as Hbound.
unfold reserveViolated in Hbound. rewrite Hsender, u256_lt in Hbound.
apply Z.ltb_ge in Hbound. exact Hbound.
Qed.
fst (Reserve_balance.dipped_into_reserve (Support.Variants.Revision.Setcode prices) s) = false →
In (addr, original) (W.t.accounts (S.t.initial_world_state s)) →
Addr._Set.(OCamlSet.S.mem) addr (S.t.self_destruct s) = false →
Addr.op_eq addr (S.t.tx_origin s) = false →
Reserve_balance.sent_in_window s addr = true →
protected s addr original ≤ A.t.balance (accountAt s addr).
Proof.
intros Hcheck Hin Hdes Hsender Hrecent.
assert (Hexempt : Setcode_rules.contract_exempt (A.is_smart_contract (accountAt s addr))
(Addr.op_eq addr (S.t.tx_origin s)) (Reserve_balance.sent_in_window s addr) = false).
{ unfold Setcode_rules.contract_exempt. rewrite Hrecent. now rewrite Bool.andb_false_r, Bool.andb_false_r. }
pose proof (checked_account prices s addr original Hcheck Hin Hdes Hexempt) as Hbound.
unfold reserveViolated in Hbound. rewrite Hsender, u256_lt in Hbound.
apply Z.ltb_ge in Hbound. exact Hbound.
Qed.
A sender not permitted to empty keeps its protected amount minus the
fee. The sender is never exempt merely because its code is regular.
Theorem sender_retains_reserve_minus_fee prices s original :
let addr := S.t.tx_origin s in
let reserve := protected s addr original in
let fee := (S.t.tx_gas_limit s × S.t.tx_gas_price s)%N in
fst (Reserve_balance.dipped_into_reserve (Support.Variants.Revision.Setcode prices) s) = false →
In (addr, original) (W.t.accounts (S.t.initial_world_state s)) →
Addr._Set.(OCamlSet.S.mem) addr (S.t.self_destruct s) = false →
mayEmpty s = false →
0 ≤ reserve < 2^256 →
Z.of_N fee ≤ reserve ∧ reserve - Z.of_N fee ≤ A.t.balance (accountAt s addr).
Proof.
cbn zeta. intros Hcheck Hin Hdes Hempty Hrange.
change (0 ≤ U.to_z (protected s (S.t.tx_origin s) original) < 2^256) in Hrange.
assert (Hexempt : Setcode_rules.contract_exempt (A.is_smart_contract (accountAt s (S.t.tx_origin s)))
(Addr.op_eq (S.t.tx_origin s) (S.t.tx_origin s))
(Reserve_balance.sent_in_window s (S.t.tx_origin s)) = false).
{ rewrite address_eq_refl. unfold Setcode_rules.contract_exempt. now rewrite Bool.andb_false_r. }
pose proof (checked_account prices s (S.t.tx_origin s) original Hcheck Hin Hdes Hexempt) as Hbound.
unfold reserveViolated in Hbound.
rewrite address_eq_refl, Hempty in Hbound.
apply Bool.orb_false_iff in Hbound as [Hfee Hbal].
unfold Uint.op_gt in Hfee.
rewrite u256_to_uint in Hfee by lia.
apply N.ltb_ge in Hfee. apply N2Z.inj_le in Hfee.
rewrite Z2N.id in Hfee by lia.
assert (Hof : U.to_z (U.of_uint_exn (S.t.tx_gas_limit s × S.t.tx_gas_price s)%N) =
Z.of_N (S.t.tx_gas_limit s × S.t.tx_gas_price s)%N) by (apply u256_of_uint; lia).
rewrite u256_lt in Hbal.
rewrite u256_sub in Hbal by (rewrite Hof; pose proof
(N2Z.is_nonneg (S.t.tx_gas_limit s × S.t.tx_gas_price s)%N); lia).
rewrite Hof in Hbal. apply Z.ltb_ge in Hbal. split; assumption.
Qed.
let addr := S.t.tx_origin s in
let reserve := protected s addr original in
let fee := (S.t.tx_gas_limit s × S.t.tx_gas_price s)%N in
fst (Reserve_balance.dipped_into_reserve (Support.Variants.Revision.Setcode prices) s) = false →
In (addr, original) (W.t.accounts (S.t.initial_world_state s)) →
Addr._Set.(OCamlSet.S.mem) addr (S.t.self_destruct s) = false →
mayEmpty s = false →
0 ≤ reserve < 2^256 →
Z.of_N fee ≤ reserve ∧ reserve - Z.of_N fee ≤ A.t.balance (accountAt s addr).
Proof.
cbn zeta. intros Hcheck Hin Hdes Hempty Hrange.
change (0 ≤ U.to_z (protected s (S.t.tx_origin s) original) < 2^256) in Hrange.
assert (Hexempt : Setcode_rules.contract_exempt (A.is_smart_contract (accountAt s (S.t.tx_origin s)))
(Addr.op_eq (S.t.tx_origin s) (S.t.tx_origin s))
(Reserve_balance.sent_in_window s (S.t.tx_origin s)) = false).
{ rewrite address_eq_refl. unfold Setcode_rules.contract_exempt. now rewrite Bool.andb_false_r. }
pose proof (checked_account prices s (S.t.tx_origin s) original Hcheck Hin Hdes Hexempt) as Hbound.
unfold reserveViolated in Hbound.
rewrite address_eq_refl, Hempty in Hbound.
apply Bool.orb_false_iff in Hbound as [Hfee Hbal].
unfold Uint.op_gt in Hfee.
rewrite u256_to_uint in Hfee by lia.
apply N.ltb_ge in Hfee. apply N2Z.inj_le in Hfee.
rewrite Z2N.id in Hfee by lia.
assert (Hof : U.to_z (U.of_uint_exn (S.t.tx_gas_limit s × S.t.tx_gas_price s)%N) =
Z.of_N (S.t.tx_gas_limit s × S.t.tx_gas_price s)%N) by (apply u256_of_uint; lia).
rewrite u256_lt in Hbal.
rewrite u256_sub in Hbal by (rewrite Hof; pose proof
(N2Z.is_nonneg (S.t.tx_gas_limit s × S.t.tx_gas_price s)%N); lia).
rewrite Hof in Hbal. apply Z.ltb_ge in Hbal. split; assumption.
Qed.
Nonempty sender code disables emptying, including after migration.
Theorem nonempty_sender_retains_reserve_minus_fee prices s original :
let addr := S.t.tx_origin s in
let reserve := protected s addr original in
let fee := (S.t.tx_gas_limit s × S.t.tx_gas_price s)%N in
fst (Reserve_balance.dipped_into_reserve (Support.Variants.Revision.Setcode prices) s) = false →
In (addr, original) (W.t.accounts (S.t.initial_world_state s)) →
Addr._Set.(OCamlSet.S.mem) addr (S.t.self_destruct s) = false →
Byte_string.Bytes.op_eq (A.t.code (accountAt s addr)) Byte_string.Bytes.empty = false →
0 ≤ reserve < 2^256 →
Z.of_N fee ≤ reserve ∧ reserve - Z.of_N fee ≤ A.t.balance (accountAt s addr).
Proof.
cbn zeta. intros Hcheck Hin Hdes Hcode Hrange.
apply (sender_retains_reserve_minus_fee prices s original Hcheck Hin Hdes).
- unfold mayEmpty. rewrite Hcode, Bool.andb_false_r. reflexivity.
- exact Hrange.
Qed.
Print Assumptions recent_nonsender_retains_reserve.
Print Assumptions nonempty_sender_retains_reserve_minus_fee.
let addr := S.t.tx_origin s in
let reserve := protected s addr original in
let fee := (S.t.tx_gas_limit s × S.t.tx_gas_price s)%N in
fst (Reserve_balance.dipped_into_reserve (Support.Variants.Revision.Setcode prices) s) = false →
In (addr, original) (W.t.accounts (S.t.initial_world_state s)) →
Addr._Set.(OCamlSet.S.mem) addr (S.t.self_destruct s) = false →
Byte_string.Bytes.op_eq (A.t.code (accountAt s addr)) Byte_string.Bytes.empty = false →
0 ≤ reserve < 2^256 →
Z.of_N fee ≤ reserve ∧ reserve - Z.of_N fee ≤ A.t.balance (accountAt s addr).
Proof.
cbn zeta. intros Hcheck Hin Hdes Hcode Hrange.
apply (sender_retains_reserve_minus_fee prices s original Hcheck Hin Hdes).
- unfold mayEmpty. rewrite Hcode, Bool.andb_false_r. reflexivity.
- exact Hrange.
Qed.
Print Assumptions recent_nonsender_retains_reserve.
Print Assumptions nonempty_sender_retains_reserve_minus_fee.