Library SetcodeConcrete.transactionbudget
Preserving the consensus reserve budget across SETCODE transactions
- its remaining budget is already zero,
- it still has empty or delegated code (protected by guardedAccount),
- it has already sent a transaction earlier in the current block, or
- it sent a transaction in an earlier block within the consensus delay.
From stdpp Require Import decidable list.
From Stdlib Require Import List String NArith ZArith Bool Lia.
Require Import RocqOfOCaml.RocqOfOCaml MonadVM.Execution.
Require Import QedkConcrete.worldaccounts QedkConcrete.validation
QedkConcrete.publictransaction QedkConcrete.reservecheck
QedkConcrete.reservestorage QedkConcrete.reserveencoding
QedkConcrete.hoststoragewords QedkConcrete.hostaccountformat
QedkConcrete.hashwords QedkConcrete.assemblychecked
QedkConcrete.nativeinputs QedkConcrete.supply QedkConcrete.wordrange
QedkConcrete.protectedstorage QedkConcrete.protectedstoragenative
QedkConcrete.protectedstorageboundarycore QedkConcrete.persistentcode
QedkConcrete.transactionframes QedkConcrete.transactionchecks
QedkConcrete.authorization.
Require Import SetcodeConcrete.consensuschecks SetcodeConcrete.reserve
SetcodeConcrete.transactionentry SetcodeConcrete.transactionentrysafe
SetcodeConcrete.transactionsupply SetcodeConcrete.supplysequence
SetcodeConcrete.nonceagreement SetcodeConcrete.nonceagreementsequence
SetcodeConcrete.activityagreement SetcodeConcrete.emptyingagreement
SetcodeConcrete.transactionactivity SetcodeConcrete.transactionregular
SetcodeConcrete.regularaccount SetcodeConcrete.executionregular
SetcodeConcrete.codeevolution SetcodeConcrete.transactioncode
SetcodeConcrete.transactionnonce SetcodeConcrete.blockconfig
SetcodeConcrete.transactionconfig SetcodeConcrete.validatedexecution
SetcodeConcrete.transactionaccountfinalize
SetcodeConcrete.guardedaccountframe SetcodeConcrete.transactionguardedreserve.
Set Default Goal Selector "!".
Import ListNotations.
Local Open Scope Z_scope.
#[local] Hint Extern 100 (Basics.Unreachable _) ⇒
(apply Opaque.legacy_witness; Opaque.opaque_inhabit 64%nat) : typeclass_instances.
Module E := Execution.Make.
Module S := State.TransactionState.
Module B := State.BlockState.
Module W := consensuschecks.W.
Module A := consensuschecks.A.
Module T := consensuschecks.T.
Module U := Numeric.U256.
Module Addr := consensuschecks.Addr.
Module Block := consensuschecks.Block.
Local Abbreviation number state :=
(Block.Header.t.number (Block.t.header (B.t.current_block state))).
Local Abbreviation native := Reserve_balance.precompile_address.
Local Abbreviation accountAt := consensuschecks.accountAt.
Section Definitions.
Context {F : E.FArgs}.
Definition sentBy (tx : T.t) (addr : Addr.t) : bool :=
match T.sender E.Params.(Execution.Make_Params_signature.chain_id) tx with
| Some sender ⇒ Addr.op_eq sender addr
| None ⇒ false
end.
Definition sentInPriorTransactions (block : B.t) (addr : Addr.t) : bool :=
OCamlList._exists (fun tx ⇒ sentBy tx addr)
(OCamlList.map fst (B.t.transactions_processed block)).
Definition sentInHistory (world : W.t) (height : N) (addr : Addr.t) : bool :=
OCamlList._exists
(fun b ⇒
Support.UintN.op_lt (Block.Header.t.number (Block.t.header b)) height &&
Support.UintN.op_lt height
(Support.UintN.op_plus (Block.Header.t.number (Block.t.header b))
Reserve_balance.execution_consensus_delay) &&
OCamlList._exists (fun tx ⇒ sentBy tx addr) (Block.t.transactions b))
(W.t.history world).
Definition blockSentInWindow (block : B.t) (addr : Addr.t) : bool :=
sentInPriorTransactions block addr ||
sentInHistory (B.t.world_state block) (number block) addr.
Lemma sent_in_window_block_eq block sender tx addr :
Reserve_balance.sent_in_window
(S.t.with_tx_chain_id E.Params.(Execution.Make_Params_signature.chain_id)
(S.make block sender tx)) addr =
blockSentInWindow block addr.
Proof. reflexivity. Qed.
Record TransactionBudgetBacking (snapshot : W.t) (previous : AdmissionState)
(state : B.t) (senders : list Addr.t) (until : N) : Prop := {
tx_backing_balance : balanceInvariant (B.t.world_state state);
tx_backing_format : worldFormat (B.t.world_state state);
tx_backing_nonce :
NonceAgreement (eligibleAt snapshot) (admissionNonces previous)
(B.t.world_state state) senders;
tx_backing_activity :
ActivityAgreement snapshot (admissionActivity previous)
(B.t.world_state state) until;
tx_backing_empty_code :
EmptyCodeAgreement snapshot (admissionActivity previous)
(B.t.world_state state) senders;
tx_backing_config :
∃ initialStorage,
stringmaps.WellFormed initialStorage ∧
storageWords initialStorage ∧
(∀ a,
Reserve_balance.user_reserve_balance snapshot (number state) a =
initialThreshold initialStorage (number state) a) ∧
worldConfig (number state) initialStorage (B.t.world_state state);
tx_backing_budgets :
∀ a, In a senders →
0 ≤ admissionBudgets previous a ∧
admissionBudgets previous a ≤ balanceAt (B.t.world_state state) a ∧
admissionBudgets previous a ≤ reserveAt snapshot (number state) a;
tx_backing_idle :
∀ a, In a senders → ¬ In a (admissionActivity previous) →
A.t.code (accountAt snapshot a) = Byte_string.Bytes.empty →
A.t.code (accountAt (B.t.world_state state) a) = Byte_string.Bytes.empty ∧
balanceAt snapshot a ≤ balanceAt (B.t.world_state state) a;
tx_backing_status :
∀ a, In a senders →
admissionBudgets previous a = 0 ∨
A.is_smart_contract (accountAt (B.t.world_state state) a) = false ∨
sentInPriorTransactions state a = true ∨
(sentInHistory (B.t.world_state state) (number state) a = true ∧
∀ target, (number state < target ≤ until)%N →
sentInHistory (B.t.world_state state) target a = true)
}.
Lemma tx_backing_user_reserve_eq snapshot previous state senders until a :
TransactionBudgetBacking snapshot previous state senders until →
Reserve_balance.user_reserve_balance (B.t.world_state state) (number state) a =
Reserve_balance.user_reserve_balance snapshot (number state) a.
Proof.
intro Hbacking.
destruct (tx_backing_config _ _ _ _ _ Hbacking)
as [initialStorage [Hmap [Hwords [Hsnap [_ [_ [_ [updates Hwrites]]]]]]]].
pose proof (reserve_storage_writes_threshold_any_account _ _ _ _ a (number state)
Hmap Hwords Hwrites ltac:(lia)) as Hth.
rewrite Hsnap.
unfold Reserve_balance.user_reserve_balance, Reserve_balance.stored_config, initialThreshold.
f_equal. exact Hth.
Qed.
Lemma tx_backing_reserve_eq snapshot previous state senders until a :
TransactionBudgetBacking snapshot previous state senders until →
reserveAt (B.t.world_state state) (number state) a =
reserveAt snapshot (number state) a.
Proof.
intro Hbacking. unfold reserveAt.
rewrite (tx_backing_user_reserve_eq _ _ _ _ _ a Hbacking). reflexivity.
Qed.
End Definitions.
Section Execution.
Context {F : E.FArgs}.
Context {UI0 : Basics.Unreachable Support.UintN.Impl.t}
{UB : Basics.Unreachable Bloom.t}
{U8 : Basics.Unreachable Byte_string.B8.(Byte_string.Fixed.Fixed_result.t)}.
Local Abbreviation args := (@E.Instantiation_fargs F).
Local Abbreviation params := args.(Host.Instantiate.ChainParams).
Context (cold warm access write reserve : N).
Context (Hrevision : E.Params.(Execution.Make_Params_signature.revision) =
Support.Variants.Revision.Setcode (cold, warm, access, write, reserve)).
Context (Hcold : (0 < cold)%N) (Hwarm : (0 < warm)%N).
Context (Hhash : keccak256OutputSize) (Hnative : @nativeOutputBounds args).
Lemma ocaml_list_exists_append {A : Set} (f : A → bool) (l r : list A) :
OCamlList._exists f (l ++ r) = (OCamlList._exists f l || OCamlList._exists f r).
Proof.
induction l as [|x l IH]; [reflexivity|].
cbn. rewrite IH, Bool.orb_assoc. reflexivity.
Qed.
Lemma ocaml_list_map_snoc {A B : Set} (f : A → B) :
∀ (l : list A) (x : A),
OCamlList.map f (Result.List.append l [x]) = OCamlList.map f l ++ [f x].
Proof.
fix IH 1. intros [|a [|b l]] x; [reflexivity|reflexivity|].
cbn. f_equal. f_equal. apply IH.
Qed.
Lemma delete_accounts_transactions_processed block addresses :
B.t.transactions_processed (OCamlSeq.fold_left
(fun s addr ⇒ State.op_pointcaretlparenrparenltminus s
(B.account_opt None addr) None) block addresses) =
B.t.transactions_processed block.
Proof.
revert block. induction addresses as [|addr addresses IH]; intro block.
- reflexivity.
- cbn [OCamlSeq.fold_left]. rewrite IH. reflexivity.
Qed.
Lemma process_transaction_transactions_processed block tx post :
E.process_transaction block tx = inl post →
OCamlList.map fst (B.t.transactions_processed post) =
OCamlList.map fst (B.t.transactions_processed block) ++ [tx].
Proof.
intro Hrun. unfold E.process_transaction in Hrun.
destruct (E.validate_transaction block tx) as [[sender fee price intrinsic]|error];
[|discriminate Hrun].
change (@Result.op_letdollar) with
(fun (A B C : Set) (r : A + B) (f : A → C + B) ⇒
match r with inl value ⇒ f value | inr error ⇒ inr error end) in Hrun.
cbn beta iota zeta delta [E.transaction_validation.sender E.transaction_validation.total_fee
E.transaction_validation.intrinsic_gas E.transaction_validation.effective_gas_price] in Hrun.
match type of Hrun with context [Setcode_rules.advance_sender_nonce ?flag _ _] ⇒
set (migrated := flag) in × end.
match type of Hrun with context [Result.List.fold_left ?f ?start (T.authorization_list tx)] ⇒
destruct (Result.List.fold_left f start (T.authorization_list tx)) as [authorities prepared]
end.
cbn beta iota zeta in Hrun.
match type of Hrun with context [E.Host.call_from_eoa tx ?msg ?initial] ⇒
destruct (E.Host.call_from_eoa tx msg initial) as [result afterVm]
end.
destruct migrated; cbn beta iota zeta in Hrun.
all: repeat match type of Hrun with
context [if Evmc.Message.CallKind.is_create ?kind then _ else _] ⇒
destruct (Evmc.Message.CallKind.is_create kind); cbn beta iota zeta in Hrun
end.
all: injection Hrun as <-.
all: cbn beta iota zeta delta [B.t.with_transactions_processed
B.t.with_transaction_frames B.t.with_gas_used B.t.transactions_processed].
all: change (@Result.Seq.fold_left) with (@OCamlSeq.fold_left).
all: rewrite delete_accounts_transactions_processed.
all: exact (ocaml_list_map_snoc fst (B.t.transactions_processed block) _).
Qed.
Lemma process_transaction_sent_in_prior block tx post addr :
E.process_transaction block tx = inl post →
sentInPriorTransactions post addr =
(sentInPriorTransactions block addr || sentBy tx addr).
Proof.
intro Hrun. unfold sentInPriorTransactions.
rewrite (process_transaction_transactions_processed block tx post Hrun).
rewrite ocaml_list_exists_append. cbn. rewrite Bool.orb_false_r. reflexivity.
Qed.
Lemma sent_by_sender tx sender :
T.sender E.Params.(Execution.Make_Params_signature.chain_id) tx = Some sender →
sentBy tx sender = true.
Proof.
intro Hsender. unfold sentBy. rewrite Hsender.
change (Z.eqb (OCamlString.compare sender sender) 0 = true).
rewrite OCamlString.compare_matches_structural.
unfold OCamlString.compare_structural.
rewrite String.eqb_refl. reflexivity.
Qed.
Lemma sender_not_created snapshot previous state tx validation senders a :
NonceAgreement (eligibleAt snapshot) (admissionNonces previous)
(B.t.world_state state) senders →
(∀ v, E.validate_transaction state tx = inl v →
∀ q, In q senders → A.is_smart_contract (accountAt (B.t.world_state state) q) = false →
¬ In q (S.t.accounts_created_in_current_transaction (transactionFinished state tx v))) →
E.validate_transaction state tx = inl validation →
In a senders →
¬ In a (S.t.accounts_created_in_current_transaction (transactionFinished state tx validation)).
Proof using F Hcold Hrevision Hwarm access cold reserve warm write.
intros Hnonce Hcreation Hv Ha.
destruct (A.is_smart_contract (accountAt (B.t.world_state state) a)) eqn:Hsc.
- destruct (Hnonce a Ha) as [_ [Hmap _]].
assert (Hreg : worldAccount a (fun code _ ⇒ regularCode code) (B.t.world_state state))
by (split; [exact Hmap|apply regular_code_contract; exact Hsc]).
pose proof (transaction_entry_regular state tx validation a Hreg) as Hentry.
assert (Hgas : 0 ≤ Evmc.Message.t.gas (fst (transactionVmInput state tx validation)) < 2^63).
{ unfold transactionVmInput. destruct (transactionPrefix state tx validation).
exact (proj1 (prepared_message_gas state tx validation _ Hv)). }
assert (Hfin : regularAccount a (transactionFinished state tx validation)).
{ unfold transactionFinished.
destruct (transactionVmInput state tx validation) as [msg entry].
cbn beta iota zeta delta [fst snd] in Hentry, Hgas.
destruct (migratedSender state (E.transaction_validation.sender validation)); [exact Hentry|].
exact (@execution_call_from_eoa_preserves_regular F a cold warm access write reserve
Hcold Hwarm tx msg Hrevision Hgas entry Hentry). }
destruct Hfin as [code [_ HP]].
exact (persistent_code_uncreated _ _ _ HP).
- exact (Hcreation validation Hv a Ha Hsc).
Qed.
Lemma validate_transaction_error_form state tx details :
E.validate_transaction state tx = inr (Execution.Error.Invalid_transaction details) →
E.validate_transaction state tx =
E.invalid_transaction (B.t.current_block state) tx
(Execution.Error.t.Invalid_transaction.reason details).
Proof using F.
intro Hfail. rewrite Hfail.
unfold E.validate_transaction, E.validate_authorizations in Hfail.
change (@Result.op_letdollar) with
(fun (A Error C : Set) (first : A + Error) (next : A → C + Error) ⇒
match first with inl value ⇒ next value | inr error ⇒ inr error end) in Hfail.
change (@Result._return) with (fun (A Error : Set) (value : A) ⇒ @inl A Error value) in Hfail.
change (@Result.when_) with (fun (Error : Set) (condition : bool) (failure : unit + Error) ⇒
if condition then failure else inl tt) in Hfail.
cbv beta iota zeta in Hfail.
destruct tx; cbn beta iota zeta in Hfail;
repeat (match type of Hfail with
| context [match ?x with Some _ ⇒ _ | None ⇒ _ end] ⇒ destruct x
| context [match ?x with [] ⇒ _ | _ :: _ ⇒ _ end] ⇒ destruct x
| context [if ?b then _ else _] ⇒ destruct b
end; cbn [Result.op_letdollar Result.when_ E.invalid_transaction] in Hfail;
try discriminate Hfail;
try (injection Hfail as <-; reflexivity)).
Qed.
Lemma affordable_no_insufficient_balance state tx sender price details fee :
T.sender E.Params.(Execution.Make_Params_signature.chain_id) tx = Some sender →
Gas.tx_effective_gas_price
(Block.Header.t.base_fee_per_gas (Block.t.header (B.t.current_block state))) tx = Some price →
Z.of_N (T.gas_limit tx × price) ≤ balanceAt (B.t.world_state state) sender →
E.validate_transaction state tx = inr (Execution.Error.Invalid_transaction details) →
Execution.Error.t.Invalid_transaction.reason details ≠
Execution.Error.Insufficient_balance fee.
Proof using F.
intros Hsender Hprice Hbal Hfail Hreason.
assert (Hquote : transactionQuote (Block.t.header (B.t.current_block state)) tx =
Some (sender, (T.gas_limit tx × price)%N)).
{ unfold transactionQuote, transactionSender. rewrite Hsender, Hprice. reflexivity. }
pose proof (affordable_quote_no_insufficient_balance state tx sender
(T.gas_limit tx × price)%N Hquote Hbal fee) as Hnot.
apply Hnot.
rewrite (validate_transaction_error_form state tx details Hfail), Hreason.
reflexivity.
Qed.
Lemma allows_emptying_destruct snapshot previous sender height authorities :
allowsEmptying snapshot previous sender height authorities = true →
A.t.code (accountAt snapshot sender) = Byte_string.Bytes.empty ∧
(emptyingAfter snapshot sender ≤ height)%N ∧
¬ In sender authorities ∧
¬ In sender (admissionActivity previous).
Proof using.
intro Hallow. unfold allowsEmptying in Hallow.
apply Bool.andb_true_iff in Hallow as [Hleft HnotIn].
apply Bool.andb_true_iff in Hleft as [Hcode Hheight].
change (Z.eqb (OCamlString.compare
(A.t.code (accountAt snapshot sender)) Byte_string.Bytes.empty) 0 = true) in Hcode.
apply Z.eqb_eq, address_compare_eq in Hcode.
apply bool_decide_eq_true in Hheight.
apply bool_decide_eq_true in HnotIn.
rewrite elem_of_app, !list_elem_of_In in HnotIn.
repeat split; try assumption; tauto.
Qed.
Theorem checked_transaction_no_insufficient_balance snapshot previous next
state tx senders until details fee :
(∀ a, T.sender E.Params.(Execution.Make_Params_signature.chain_id) tx = Some a →
In a senders) →
TransactionBudgetBacking snapshot previous state senders until →
checkTransaction snapshot (Block.t.header (B.t.current_block state)) previous tx = Some next →
E.process_transaction state tx = inr (Execution.Error.Invalid_transaction details) →
Execution.Error.t.Invalid_transaction.reason details ≠
Execution.Error.Insufficient_balance fee.
Proof using F Hcold Hwarm cold warm.
intros HsenderIn Hbacking Hcheck Hrun.
pose proof (transaction_error_from_validation state tx _ Hrun) as Hfail.
unfold checkTransaction in Hcheck.
destruct (T.sender _ tx) as [sender|] eqn:Hsender; [|discriminate].
destruct (Gas.tx_effective_gas_price _ tx) as [price|] eqn:Hprice; [|discriminate].
destruct (transactionChecks _ _ _ _); [|discriminate].
cbn zeta in Hcheck.
match type of Hcheck with context [if ?test then _ else _] ⇒
destruct test eqn:Hafford end; [|discriminate].
apply bool_decide_eq_true in Hafford.
assert (Hin : In sender senders) by (exact (HsenderIn sender eq_refl)).
assert (Hbal : Z.of_N (T.gas_limit tx × price) ≤ balanceAt (B.t.world_state state) sender).
{ destruct (allowsEmptying snapshot previous sender (number state) (recoveredAuthorities tx))
eqn:Hempty.
- destruct (allows_emptying_destruct _ _ _ _ _ Hempty) as [Hcode [_ [_ HnotPast]]].
destruct (tx_backing_idle _ _ _ _ _ Hbacking sender Hin HnotPast Hcode) as [_ HidleBal].
lia.
- destruct (tx_backing_budgets _ _ _ _ _ Hbacking sender Hin) as [_ [Hbudget _]].
lia. }
exact (affordable_no_insufficient_balance state tx sender price details fee
Hsender Hprice Hbal Hfail).
Qed.
Theorem checked_transaction_budget_backing snapshot previous next
state tx post senders until :
wordRange (Support.UintN.to_z (Chain.Monad.PARAMS.chain_id params)) →
headerWords (Block.t.header (B.t.current_block state)) →
(∀ a, In a senders → a ≠ native) →
Forall (fun auth ⇒ OCamlString.length (T.Authorization.t.address auth) = 20)
(T.authorization_list tx) →
transactionValueAndDataBounds tx →
signaturesAvoidAddress tx native →
(∀ a, T.sender E.Params.(Execution.Make_Params_signature.chain_id) tx = Some a →
In a senders) →
(∀ v, E.validate_transaction state tx = inl v →
∀ a, In a senders → A.is_smart_contract (accountAt (B.t.world_state state) a) = false →
¬ In a (S.t.accounts_created_in_current_transaction (transactionFinished state tx v))) →
(number state ≤ until)%N →
(until < number state + 3)%N →
TransactionBudgetBacking snapshot previous state senders until →
checkTransaction snapshot (Block.t.header (B.t.current_block state)) previous tx = Some next →
E.process_transaction state tx = inl post →
TransactionBudgetBacking snapshot next post senders until.
Proof using F Hcold Hhash Hnative Hrevision Hwarm U8 UB UI0 access cold reserve warm write.
intros Hchain Hheader HnotNative Hwidth Hvalue Hsig HsenderIn Hcreation HleUntil HltDelay
Hbacking Hcheck Hrun.
assert (Hv : ∃ validation, E.validate_transaction state tx = inl validation).
{ unfold E.process_transaction in Hrun.
destruct (E.validate_transaction state tx) as [v|err]; [eexists; reflexivity|discriminate]. }
destruct Hv as [validation Hv].
pose proof (process_transaction_current_block state tx post Hrun) as Hcurrent.
pose proof (tx_backing_balance _ _ _ _ _ Hbacking) as Hinitial.
pose proof (tx_backing_format _ _ _ _ _ Hbacking) as Hformat.
pose proof (tx_backing_nonce _ _ _ _ _ Hbacking) as Hnonce.
pose proof (tx_backing_activity _ _ _ _ _ Hbacking) as Hactivity.
pose proof (tx_backing_empty_code _ _ _ _ _ Hbacking) as HemptyCode.
pose proof (tx_backing_config _ _ _ _ _ Hbacking) as Hconfig.
pose proof (tx_backing_budgets _ _ _ _ _ Hbacking) as Hbudgets.
pose proof (tx_backing_idle _ _ _ _ _ Hbacking) as Hidle.
pose proof (tx_backing_status _ _ _ _ _ Hbacking) as Hstatus.
pose proof (@process_transaction_preserves_supply F UI0 UB U8
cold warm access write reserve Hrevision Hcold Hwarm Hhash Hnative
state tx post Hchain (proj1 (proj2 Hheader)) (proj2 (proj2 Hheader))
Hinitial Hformat Hwidth Hvalue Hrun) as [HpostBal [_ HpostFormat]].
pose proof (checked_transaction_activity_agreement cold warm access write reserve
Hrevision Hcold Hwarm snapshot previous next until state tx post
Hactivity HltDelay Hcheck Hrun) as HpostActivity.
pose proof (checked_transaction_empty_code cold warm access write reserve
Hrevision Hcold Hwarm snapshot previous next state tx post senders
HnotNative Hnonce HemptyCode Hcreation Hcheck Hrun) as HpostEmptyCode.
pose proof (process_transaction_activity cold warm access write reserve
Hrevision Hcold Hwarm state tx validation post (activity_map _ _ _ _ Hactivity)
Hv Hrun) as [HhistoryEq _].
unfold checkTransaction in Hcheck.
destruct (T.sender _ tx) as [sender|] eqn:Hsender; [|discriminate].
destruct (Gas.tx_effective_gas_price _ tx) as [price|] eqn:Hprice; [|discriminate].
destruct (transactionChecks _ _ _ _) eqn:HtxChecks; [|discriminate].
cbn zeta in Hcheck.
match type of Hcheck with context [if ?test then _ else _] ⇒
destruct test eqn:Hafford end; [|discriminate].
apply bool_decide_eq_true in Hafford.
injection Hcheck as <-.
assert (HsenderEq : E.transaction_validation.sender validation = sender ∧
(T.gas_limit tx × E.transaction_validation.effective_gas_price validation)%N =
(T.gas_limit tx × price)%N).
{ pose proof (validated_transaction_quote state tx validation Hv) as Hquote.
unfold transactionQuote, transactionSender in Hquote.
rewrite Hsender, Hprice in Hquote. injection Hquote as H1 H2.
split; symmetry; assumption. }
destruct HsenderEq as [HsenderVal HfeeN].
pose proof (account_balance_bounded_by_supply (B.t.world_state state)
(E.transaction_validation.sender validation) (proj1 (proj2 Hinitial))) as HsenderSupply.
pose proof (proj2 (proj2 Hinitial)) as HtotalSupply.
pose proof (validated_debit_range state tx validation Hv
ltac:(cbn beta in HsenderSupply; lia)) as [HfeeVal _].
rewrite HfeeN in HfeeVal.
assert (HsenderInList : In sender senders) by (exact (HsenderIn sender eq_refl)).
pose proof (process_transaction_nonce_agreement cold warm access write reserve
Hrevision Hcold Hwarm state tx post (eligibleAt snapshot) (admissionNonces previous)
senders sender HnotNative Hwidth Hsender Hnonce Hcreation Hrun) as HpostNonce.
assert (HpostConfig : ∃ initialStorage,
stringmaps.WellFormed initialStorage ∧
storageWords initialStorage ∧
(∀ a, Reserve_balance.user_reserve_balance snapshot (number post) a =
initialThreshold initialStorage (number post) a) ∧
worldConfig (number post) initialStorage (B.t.world_state post)).
{ destruct Hconfig as [initialStorage [HstMap [HstWords [HstSnap HstWorld]]]].
∃ initialStorage. rewrite Hcurrent.
refine (conj HstMap (conj HstWords (conj HstSnap _))).
exact (process_transaction_only_delayed_settings cold warm access write reserve
Hrevision Hcold Hwarm state tx validation post initialStorage Hsig Hv Hrun HstWorld). }
assert (HpostIdle : ∀ a, In a senders →
¬ In a (sender :: recoveredAuthorities tx ++ admissionActivity previous) →
A.t.code (accountAt snapshot a) = Byte_string.Bytes.empty →
A.t.code (accountAt (B.t.world_state post) a) = Byte_string.Bytes.empty ∧
balanceAt snapshot a ≤ balanceAt (B.t.world_state post) a).
{ intros a Ha HnotAct HsnapEmpty.
assert (HneqSender : sender ≠ a) by (intro Heq; apply HnotAct; left; exact Heq).
assert (HnotAuth : ¬ In a (recoveredAuthorities tx))
by (intro Hin; apply HnotAct; right; apply in_app_iff; left; exact Hin).
assert (HnotPast : ¬ In a (admissionActivity previous))
by (intro Hin; apply HnotAct; right; apply in_app_iff; right; exact Hin).
destruct (Hidle a Ha HnotPast HsnapEmpty) as [HstateEmpty HstateBal].
pose proof (sender_not_created snapshot previous state tx validation senders a
Hnonce Hcreation Hv Ha) as HnotCreated.
pose proof (process_transaction_empty_without_recovery cold warm access write reserve
Hrevision Hcold Hwarm Hhash Hnative state tx validation post a
Hchain Hheader Hinitial Hformat Hwidth Hvalue (HnotNative a Ha) HnotAuth
Hv Hrun HstateEmpty HnotCreated) as [HpostCode HpostBalBound].
assert (HeqbFalse : String.eqb (E.transaction_validation.sender validation) a = false)
by (apply String.eqb_neq; congruence).
rewrite HeqbFalse in HpostBalBound.
unfold balanceAt, accountAt in ×. split; [exact HpostCode|lia]. }
assert (HpostBoth : ∀ a, In a senders →
let emptying := allowsEmptying snapshot previous sender (number state) (recoveredAuthorities tx) in
let available := if emptying then balanceAt snapshot sender else admissionBudgets previous sender in
let nextBudget := if bool_decide (sender = a)
then (if emptying
then Z.min (Z.max (available -
Z.of_N (T.gas_limit tx × price) - U.to_z (T.value tx)) 0)
(reserveAt snapshot (number state) sender)
else available - Z.of_N (T.gas_limit tx × price))
else admissionBudgets previous a in
(0 ≤ nextBudget ∧
nextBudget ≤ balanceAt (B.t.world_state post) a ∧
nextBudget ≤ reserveAt snapshot (number post) a) ∧
(nextBudget = 0 ∨
A.is_smart_contract (accountAt (B.t.world_state post) a) = false ∨
sentInPriorTransactions post a = true ∨
(sentInHistory (B.t.world_state post) (number post) a = true ∧
∀ target, (number post < target ≤ until)%N →
sentInHistory (B.t.world_state post) target a = true))).
{ intros a Ha. cbn zeta. rewrite Hcurrent.
pose proof (sender_not_created snapshot previous state tx validation senders a
Hnonce Hcreation Hv Ha) as HnotCreated.
pose proof (account_balance_bounded_by_supply (B.t.world_state post) a
(proj1 (proj2 HpostBal))) as HpostNonneg.
change (0 ≤ balanceAt (B.t.world_state post) a ≤ totalBalance (B.t.world_state post))
in HpostNonneg.
destruct (bool_decide (sender = a)) eqn:HeqSender.
- apply bool_decide_eq_true in HeqSender. subst a.
assert (HsentPost : sentInPriorTransactions post sender = true).
{ rewrite (process_transaction_sent_in_prior state tx post sender Hrun).
rewrite (sent_by_sender tx sender Hsender), Bool.orb_true_r. reflexivity. }
split; [|right; right; left; exact HsentPost].
destruct (Hbudgets sender HsenderInList) as [HprevNonneg [HprevBal HprevRes]].
destruct (allowsEmptying snapshot previous sender (number state) (recoveredAuthorities tx))
eqn:Hallow.
+ destruct (allows_emptying_destruct _ _ _ _ _ Hallow)
as [HsnapEmpty [_ [HnotAuth HnotPast]]].
destruct (Hidle sender HsenderInList HnotPast HsnapEmpty)
as [HstateEmpty HstateBal].
pose proof (process_transaction_empty_without_recovery cold warm access write reserve
Hrevision Hcold Hwarm Hhash Hnative state tx validation post sender
Hchain Hheader Hinitial Hformat Hwidth Hvalue (HnotNative sender HsenderInList)
HnotAuth Hv Hrun HstateEmpty HnotCreated) as [_ HemptyBound].
rewrite HsenderVal, String.eqb_refl, HfeeVal in HemptyBound.
change (Z.max (balanceAt (B.t.world_state state) sender -
(Z.of_N (T.gas_limit tx × price) + U.to_z (T.value tx))) 0 ≤
balanceAt (B.t.world_state post) sender) in HemptyBound.
split; [lia|]. split; [lia|apply Z.le_min_r].
+ assert (HmayEmpty : mayEmpty (snd (transactionVmInput state tx validation)) = false).
{ apply Bool.not_true_is_false. intro Hmay.
pose proof (execution_emptying_implies_admission cold warm access write reserve
Hrevision Hcold Hwarm snapshot previous state tx validation senders until
ltac:(rewrite HsenderVal; exact HsenderInList)
(proj1 Hinitial) Hactivity HemptyCode HleUntil Hmay) as HallowTrue.
rewrite HsenderVal in HallowTrue. congruence. }
pose proof (process_transaction_ordinary_sender_bound cold warm access write reserve
Hrevision Hcold Hwarm Hhash Hnative state tx validation post
Hchain Hheader Hinitial Hformat Hwidth Hvalue Hv Hrun
ltac:(rewrite HsenderVal; exact (HnotNative sender HsenderInList))
HmayEmpty ltac:(rewrite HsenderVal; exact HnotCreated)) as HordBound.
cbn zeta in HordBound.
rewrite HsenderVal, HfeeVal in HordBound.
rewrite (tx_backing_user_reserve_eq _ _ _ _ _ sender Hbacking) in HordBound.
change (Z.min (balanceAt (B.t.world_state state) sender)
(reserveAt snapshot (number state) sender) -
Z.of_N (T.gas_limit tx × price) ≤
balanceAt (B.t.world_state post) sender) in HordBound.
pose proof (N2Z.is_nonneg (T.gas_limit tx × price)).
split; [lia|]. split; [lia|lia].
- apply bool_decide_eq_false in HeqSender.
assert (Hother : a ≠ E.transaction_validation.sender validation) by congruence.
destruct (Hbudgets a Ha) as [HprevNonneg [HprevBal HprevRes]].
assert (HhistEq : ∀ t, sentInHistory (B.t.world_state post) t a =
sentInHistory (B.t.world_state state) t a).
{ intro t. unfold sentInHistory. rewrite HhistoryEq. reflexivity. }
destruct (Hstatus a Ha) as [Hzero|[Hnoncontract|[Hprior|[HhistNow HhistFuture]]]].
+ split; [split; [exact HprevNonneg|split; [rewrite Hzero; exact (proj1 HpostNonneg)|exact HprevRes]]|left; exact Hzero].
+ destruct (Z.eq_dec (admissionBudgets previous a) 0) as [Hzero|Hnonzero].
× split; [split; [exact HprevNonneg|split; [rewrite Hzero; exact (proj1 HpostNonneg)|exact HprevRes]]|left; exact Hzero].
× assert (HresPos : Reserve_balance.user_reserve_balance
(B.t.world_state state) (number state) a ≠ 0).
{ intro HzeroRes.
pose proof (tx_backing_user_reserve_eq _ _ _ _ _ a Hbacking) as HresEq.
rewrite HzeroRes in HresEq.
unfold reserveAt in HprevRes. rewrite <- HresEq in HprevRes.
change (admissionBudgets previous a ≤ 0) in HprevRes.
apply Hnonzero. exact (Z.le_antisymm _ _ HprevRes HprevNonneg). }
destruct Hconfig as [initialStorage [HstMap [HstWords [_ HstWorld]]]].
pose proof (process_transaction_protects_guarded_nonsender cold warm access write reserve
Hrevision Hcold Hwarm Hhash Hnative state tx validation post a initialStorage
Hchain Hheader Hinitial Hformat Hwidth Hvalue Hsig HstMap HstWords HstWorld
Hv Hrun Hother (HnotNative a Ha) Hnoncontract HresPos HnotCreated)
as [HpostSc HguardedBound].
rewrite (tx_backing_user_reserve_eq _ _ _ _ _ a Hbacking) in HguardedBound.
change (Z.min (balanceAt (B.t.world_state state) a)
(reserveAt snapshot (number state) a) ≤
balanceAt (B.t.world_state post) a) in HguardedBound.
split.
-- split; [exact HprevNonneg|split; [lia|exact HprevRes]].
-- right. left. exact HpostSc.
+ assert (Hrecent : Reserve_balance.sent_in_window
(S.t.with_tx_chain_id E.Params.(Execution.Make_Params_signature.chain_id)
(S.make state (E.transaction_validation.sender validation) tx)) a = true).
{ rewrite sent_in_window_block_eq. unfold blockSentInWindow.
rewrite Hprior. reflexivity. }
pose proof (process_transaction_recent_sender_bound cold warm access write reserve
Hrevision Hcold Hwarm Hhash Hnative state tx validation post a
Hchain Hheader Hinitial Hformat Hwidth Hvalue Hv Hrun Hother Hrecent HnotCreated)
as HrecentBound.
cbv beta in HrecentBound.
rewrite (tx_backing_user_reserve_eq _ _ _ _ _ a Hbacking) in HrecentBound.
change (Z.min (balanceAt (B.t.world_state state) a)
(reserveAt snapshot (number state) a) ≤
balanceAt (B.t.world_state post) a) in HrecentBound.
assert (HpostPrior : sentInPriorTransactions post a = true).
{ rewrite (process_transaction_sent_in_prior state tx post a Hrun), Hprior.
reflexivity. }
split.
× split; [exact HprevNonneg|split; [lia|exact HprevRes]].
× right. right. left. exact HpostPrior.
+ assert (Hrecent : Reserve_balance.sent_in_window
(S.t.with_tx_chain_id E.Params.(Execution.Make_Params_signature.chain_id)
(S.make state (E.transaction_validation.sender validation) tx)) a = true).
{ rewrite sent_in_window_block_eq. unfold blockSentInWindow.
rewrite HhistNow, Bool.orb_true_r. reflexivity. }
pose proof (process_transaction_recent_sender_bound cold warm access write reserve
Hrevision Hcold Hwarm Hhash Hnative state tx validation post a
Hchain Hheader Hinitial Hformat Hwidth Hvalue Hv Hrun Hother Hrecent HnotCreated)
as HrecentBound.
cbv beta in HrecentBound.
rewrite (tx_backing_user_reserve_eq _ _ _ _ _ a Hbacking) in HrecentBound.
change (Z.min (balanceAt (B.t.world_state state) a)
(reserveAt snapshot (number state) a) ≤
balanceAt (B.t.world_state post) a) in HrecentBound.
split.
× split; [exact HprevNonneg|split; [lia|exact HprevRes]].
× right. right. right. split; [rewrite HhistEq; exact HhistNow|].
intros target Htarget. rewrite HhistEq. exact (HhistFuture target Htarget). }
constructor; try assumption.
- intros a Ha. exact (proj1 (HpostBoth a Ha)).
- intros a Ha. exact (proj2 (HpostBoth a Ha)).
Qed.
Theorem checked_transactions_budget_backing snapshot previous next
state txs senders until :
wordRange (Support.UintN.to_z (Chain.Monad.PARAMS.chain_id params)) →
headerWords (Block.t.header (B.t.current_block state)) →
(∀ a, In a senders → a ≠ native) →
Forall (fun tx ⇒
transactionValueAndDataBounds tx ∧
Forall (fun auth ⇒ OCamlString.length (T.Authorization.t.address auth) = 20)
(T.authorization_list tx) ∧
signaturesAvoidAddress tx native ∧
(∀ a, T.sender E.Params.(Execution.Make_Params_signature.chain_id) tx = Some a →
In a senders)) txs →
transactionsAvoidSenderCreation senders state txs →
(number state ≤ until)%N →
(until < number state + 3)%N →
TransactionBudgetBacking snapshot previous state senders until →
checkTransactions snapshot (Block.t.header (B.t.current_block state)) previous txs = Some next →
match Result.List.fold_leftM E.process_transaction state txs with
| inl post ⇒
TransactionBudgetBacking snapshot next post senders until ∧
B.t.current_block post = B.t.current_block state ∧
W.t.history (B.t.world_state post) = W.t.history (B.t.world_state state) ∧
OCamlList.map fst (B.t.transactions_processed post) =
OCamlList.map fst (B.t.transactions_processed state) ++ txs
| inr (Execution.Error.Invalid_transaction details) ⇒
∀ fee,
Execution.Error.t.Invalid_transaction.reason details ≠
Execution.Error.Insufficient_balance fee
| inr _ ⇒ True
end.
Proof using F Hcold Hhash Hnative Hrevision Hwarm U8 UB UI0 access cold reserve warm write.
intros Hchain Hheader HnotNative Htxs.
revert previous state Hheader.
induction Htxs as [|tx rest [Hvalue [Hwidth [Hsig HsenderIn]]] Hrest IH];
intros previous state Hheader Hcreation HleUntil HltDelay Hbacking Hcheck.
- cbn [checkTransactions] in Hcheck. injection Hcheck as <-.
cbn [Result.List.fold_leftM].
rewrite app_nil_r.
split; [exact Hbacking|split; [reflexivity|split; [reflexivity|reflexivity]]].
- cbn [checkTransactions] in Hcheck.
destruct (checkTransaction snapshot (Block.t.header (B.t.current_block state)) previous tx)
as [middle|] eqn:Htx; [|discriminate].
destruct Hcreation as [HcreateHead HcreateTail].
change (Result.List.fold_leftM E.process_transaction state (tx :: rest)) with
(Result.op_letdollar (E.process_transaction state tx)
(fun after ⇒ Result.List.fold_leftM E.process_transaction after rest)).
destruct (E.process_transaction state tx) as [after|error] eqn:Hstep.
+ assert (Hv : ∃ v, E.validate_transaction state tx = inl v).
{ unfold E.process_transaction in Hstep.
destruct (E.validate_transaction state tx); [eexists; reflexivity|discriminate]. }
destruct Hv as [v Hv].
pose proof (process_transaction_current_block state tx after Hstep) as Hcurr.
pose proof (process_transaction_transactions_processed state tx after Hstep) as Hproc.
pose proof (process_transaction_activity cold warm access write reserve
Hrevision Hcold Hwarm state tx v after
(activity_map _ _ _ _ (tx_backing_activity _ _ _ _ _ Hbacking)) Hv Hstep) as [Hhist _].
pose proof (checked_transaction_budget_backing snapshot previous middle state tx after
senders until Hchain Hheader HnotNative Hwidth Hvalue Hsig HsenderIn HcreateHead
HleUntil HltDelay Hbacking Htx Hstep) as HafterBacking.
assert (HafterHeader : headerWords (Block.t.header (B.t.current_block after)))
by (rewrite Hcurr; exact Hheader).
assert (HafterLe : (number after ≤ until)%N) by (cbv beta; rewrite Hcurr; exact HleUntil).
assert (HafterLt : (until < number after + 3)%N) by (cbv beta; rewrite Hcurr; exact HltDelay).
assert (HafterCheck : checkTransactions snapshot (Block.t.header (B.t.current_block after))
middle rest = Some next) by (rewrite Hcurr; exact Hcheck).
specialize (IH middle after HafterHeader (HcreateTail after eq_refl)
HafterLe HafterLt HafterBacking HafterCheck).
change (Result.op_letdollar (inl after)
(fun after ⇒ Result.List.fold_leftM E.process_transaction after rest))
with (Result.List.fold_leftM E.process_transaction after rest).
destruct (Result.List.fold_leftM E.process_transaction after rest) as [post|err]; [|exact IH].
destruct IH as [HpostBacking [HpostCurr [HpostHist HpostProc]]].
split; [exact HpostBacking|].
split; [congruence|].
split; [congruence|].
rewrite HpostProc, Hproc, <- app_assoc. reflexivity.
+ change (Result.op_letdollar (inr error)
(fun after ⇒ Result.List.fold_leftM E.process_transaction after rest))
with (@inr B.t Execution.Error.t error).
destruct error as [|details]; [exact I|].
intro fee.
exact (checked_transaction_no_insufficient_balance snapshot previous middle state tx
senders until details fee HsenderIn Hbacking Htx Hstep).
Qed.
End Execution.
Print Assumptions checked_transaction_no_insufficient_balance.
Print Assumptions checked_transaction_budget_backing.
Print Assumptions checked_transactions_budget_backing.
From Stdlib Require Import List String NArith ZArith Bool Lia.
Require Import RocqOfOCaml.RocqOfOCaml MonadVM.Execution.
Require Import QedkConcrete.worldaccounts QedkConcrete.validation
QedkConcrete.publictransaction QedkConcrete.reservecheck
QedkConcrete.reservestorage QedkConcrete.reserveencoding
QedkConcrete.hoststoragewords QedkConcrete.hostaccountformat
QedkConcrete.hashwords QedkConcrete.assemblychecked
QedkConcrete.nativeinputs QedkConcrete.supply QedkConcrete.wordrange
QedkConcrete.protectedstorage QedkConcrete.protectedstoragenative
QedkConcrete.protectedstorageboundarycore QedkConcrete.persistentcode
QedkConcrete.transactionframes QedkConcrete.transactionchecks
QedkConcrete.authorization.
Require Import SetcodeConcrete.consensuschecks SetcodeConcrete.reserve
SetcodeConcrete.transactionentry SetcodeConcrete.transactionentrysafe
SetcodeConcrete.transactionsupply SetcodeConcrete.supplysequence
SetcodeConcrete.nonceagreement SetcodeConcrete.nonceagreementsequence
SetcodeConcrete.activityagreement SetcodeConcrete.emptyingagreement
SetcodeConcrete.transactionactivity SetcodeConcrete.transactionregular
SetcodeConcrete.regularaccount SetcodeConcrete.executionregular
SetcodeConcrete.codeevolution SetcodeConcrete.transactioncode
SetcodeConcrete.transactionnonce SetcodeConcrete.blockconfig
SetcodeConcrete.transactionconfig SetcodeConcrete.validatedexecution
SetcodeConcrete.transactionaccountfinalize
SetcodeConcrete.guardedaccountframe SetcodeConcrete.transactionguardedreserve.
Set Default Goal Selector "!".
Import ListNotations.
Local Open Scope Z_scope.
#[local] Hint Extern 100 (Basics.Unreachable _) ⇒
(apply Opaque.legacy_witness; Opaque.opaque_inhabit 64%nat) : typeclass_instances.
Module E := Execution.Make.
Module S := State.TransactionState.
Module B := State.BlockState.
Module W := consensuschecks.W.
Module A := consensuschecks.A.
Module T := consensuschecks.T.
Module U := Numeric.U256.
Module Addr := consensuschecks.Addr.
Module Block := consensuschecks.Block.
Local Abbreviation number state :=
(Block.Header.t.number (Block.t.header (B.t.current_block state))).
Local Abbreviation native := Reserve_balance.precompile_address.
Local Abbreviation accountAt := consensuschecks.accountAt.
Section Definitions.
Context {F : E.FArgs}.
Definition sentBy (tx : T.t) (addr : Addr.t) : bool :=
match T.sender E.Params.(Execution.Make_Params_signature.chain_id) tx with
| Some sender ⇒ Addr.op_eq sender addr
| None ⇒ false
end.
Definition sentInPriorTransactions (block : B.t) (addr : Addr.t) : bool :=
OCamlList._exists (fun tx ⇒ sentBy tx addr)
(OCamlList.map fst (B.t.transactions_processed block)).
Definition sentInHistory (world : W.t) (height : N) (addr : Addr.t) : bool :=
OCamlList._exists
(fun b ⇒
Support.UintN.op_lt (Block.Header.t.number (Block.t.header b)) height &&
Support.UintN.op_lt height
(Support.UintN.op_plus (Block.Header.t.number (Block.t.header b))
Reserve_balance.execution_consensus_delay) &&
OCamlList._exists (fun tx ⇒ sentBy tx addr) (Block.t.transactions b))
(W.t.history world).
Definition blockSentInWindow (block : B.t) (addr : Addr.t) : bool :=
sentInPriorTransactions block addr ||
sentInHistory (B.t.world_state block) (number block) addr.
Lemma sent_in_window_block_eq block sender tx addr :
Reserve_balance.sent_in_window
(S.t.with_tx_chain_id E.Params.(Execution.Make_Params_signature.chain_id)
(S.make block sender tx)) addr =
blockSentInWindow block addr.
Proof. reflexivity. Qed.
Record TransactionBudgetBacking (snapshot : W.t) (previous : AdmissionState)
(state : B.t) (senders : list Addr.t) (until : N) : Prop := {
tx_backing_balance : balanceInvariant (B.t.world_state state);
tx_backing_format : worldFormat (B.t.world_state state);
tx_backing_nonce :
NonceAgreement (eligibleAt snapshot) (admissionNonces previous)
(B.t.world_state state) senders;
tx_backing_activity :
ActivityAgreement snapshot (admissionActivity previous)
(B.t.world_state state) until;
tx_backing_empty_code :
EmptyCodeAgreement snapshot (admissionActivity previous)
(B.t.world_state state) senders;
tx_backing_config :
∃ initialStorage,
stringmaps.WellFormed initialStorage ∧
storageWords initialStorage ∧
(∀ a,
Reserve_balance.user_reserve_balance snapshot (number state) a =
initialThreshold initialStorage (number state) a) ∧
worldConfig (number state) initialStorage (B.t.world_state state);
tx_backing_budgets :
∀ a, In a senders →
0 ≤ admissionBudgets previous a ∧
admissionBudgets previous a ≤ balanceAt (B.t.world_state state) a ∧
admissionBudgets previous a ≤ reserveAt snapshot (number state) a;
tx_backing_idle :
∀ a, In a senders → ¬ In a (admissionActivity previous) →
A.t.code (accountAt snapshot a) = Byte_string.Bytes.empty →
A.t.code (accountAt (B.t.world_state state) a) = Byte_string.Bytes.empty ∧
balanceAt snapshot a ≤ balanceAt (B.t.world_state state) a;
tx_backing_status :
∀ a, In a senders →
admissionBudgets previous a = 0 ∨
A.is_smart_contract (accountAt (B.t.world_state state) a) = false ∨
sentInPriorTransactions state a = true ∨
(sentInHistory (B.t.world_state state) (number state) a = true ∧
∀ target, (number state < target ≤ until)%N →
sentInHistory (B.t.world_state state) target a = true)
}.
Lemma tx_backing_user_reserve_eq snapshot previous state senders until a :
TransactionBudgetBacking snapshot previous state senders until →
Reserve_balance.user_reserve_balance (B.t.world_state state) (number state) a =
Reserve_balance.user_reserve_balance snapshot (number state) a.
Proof.
intro Hbacking.
destruct (tx_backing_config _ _ _ _ _ Hbacking)
as [initialStorage [Hmap [Hwords [Hsnap [_ [_ [_ [updates Hwrites]]]]]]]].
pose proof (reserve_storage_writes_threshold_any_account _ _ _ _ a (number state)
Hmap Hwords Hwrites ltac:(lia)) as Hth.
rewrite Hsnap.
unfold Reserve_balance.user_reserve_balance, Reserve_balance.stored_config, initialThreshold.
f_equal. exact Hth.
Qed.
Lemma tx_backing_reserve_eq snapshot previous state senders until a :
TransactionBudgetBacking snapshot previous state senders until →
reserveAt (B.t.world_state state) (number state) a =
reserveAt snapshot (number state) a.
Proof.
intro Hbacking. unfold reserveAt.
rewrite (tx_backing_user_reserve_eq _ _ _ _ _ a Hbacking). reflexivity.
Qed.
End Definitions.
Section Execution.
Context {F : E.FArgs}.
Context {UI0 : Basics.Unreachable Support.UintN.Impl.t}
{UB : Basics.Unreachable Bloom.t}
{U8 : Basics.Unreachable Byte_string.B8.(Byte_string.Fixed.Fixed_result.t)}.
Local Abbreviation args := (@E.Instantiation_fargs F).
Local Abbreviation params := args.(Host.Instantiate.ChainParams).
Context (cold warm access write reserve : N).
Context (Hrevision : E.Params.(Execution.Make_Params_signature.revision) =
Support.Variants.Revision.Setcode (cold, warm, access, write, reserve)).
Context (Hcold : (0 < cold)%N) (Hwarm : (0 < warm)%N).
Context (Hhash : keccak256OutputSize) (Hnative : @nativeOutputBounds args).
Lemma ocaml_list_exists_append {A : Set} (f : A → bool) (l r : list A) :
OCamlList._exists f (l ++ r) = (OCamlList._exists f l || OCamlList._exists f r).
Proof.
induction l as [|x l IH]; [reflexivity|].
cbn. rewrite IH, Bool.orb_assoc. reflexivity.
Qed.
Lemma ocaml_list_map_snoc {A B : Set} (f : A → B) :
∀ (l : list A) (x : A),
OCamlList.map f (Result.List.append l [x]) = OCamlList.map f l ++ [f x].
Proof.
fix IH 1. intros [|a [|b l]] x; [reflexivity|reflexivity|].
cbn. f_equal. f_equal. apply IH.
Qed.
Lemma delete_accounts_transactions_processed block addresses :
B.t.transactions_processed (OCamlSeq.fold_left
(fun s addr ⇒ State.op_pointcaretlparenrparenltminus s
(B.account_opt None addr) None) block addresses) =
B.t.transactions_processed block.
Proof.
revert block. induction addresses as [|addr addresses IH]; intro block.
- reflexivity.
- cbn [OCamlSeq.fold_left]. rewrite IH. reflexivity.
Qed.
Lemma process_transaction_transactions_processed block tx post :
E.process_transaction block tx = inl post →
OCamlList.map fst (B.t.transactions_processed post) =
OCamlList.map fst (B.t.transactions_processed block) ++ [tx].
Proof.
intro Hrun. unfold E.process_transaction in Hrun.
destruct (E.validate_transaction block tx) as [[sender fee price intrinsic]|error];
[|discriminate Hrun].
change (@Result.op_letdollar) with
(fun (A B C : Set) (r : A + B) (f : A → C + B) ⇒
match r with inl value ⇒ f value | inr error ⇒ inr error end) in Hrun.
cbn beta iota zeta delta [E.transaction_validation.sender E.transaction_validation.total_fee
E.transaction_validation.intrinsic_gas E.transaction_validation.effective_gas_price] in Hrun.
match type of Hrun with context [Setcode_rules.advance_sender_nonce ?flag _ _] ⇒
set (migrated := flag) in × end.
match type of Hrun with context [Result.List.fold_left ?f ?start (T.authorization_list tx)] ⇒
destruct (Result.List.fold_left f start (T.authorization_list tx)) as [authorities prepared]
end.
cbn beta iota zeta in Hrun.
match type of Hrun with context [E.Host.call_from_eoa tx ?msg ?initial] ⇒
destruct (E.Host.call_from_eoa tx msg initial) as [result afterVm]
end.
destruct migrated; cbn beta iota zeta in Hrun.
all: repeat match type of Hrun with
context [if Evmc.Message.CallKind.is_create ?kind then _ else _] ⇒
destruct (Evmc.Message.CallKind.is_create kind); cbn beta iota zeta in Hrun
end.
all: injection Hrun as <-.
all: cbn beta iota zeta delta [B.t.with_transactions_processed
B.t.with_transaction_frames B.t.with_gas_used B.t.transactions_processed].
all: change (@Result.Seq.fold_left) with (@OCamlSeq.fold_left).
all: rewrite delete_accounts_transactions_processed.
all: exact (ocaml_list_map_snoc fst (B.t.transactions_processed block) _).
Qed.
Lemma process_transaction_sent_in_prior block tx post addr :
E.process_transaction block tx = inl post →
sentInPriorTransactions post addr =
(sentInPriorTransactions block addr || sentBy tx addr).
Proof.
intro Hrun. unfold sentInPriorTransactions.
rewrite (process_transaction_transactions_processed block tx post Hrun).
rewrite ocaml_list_exists_append. cbn. rewrite Bool.orb_false_r. reflexivity.
Qed.
Lemma sent_by_sender tx sender :
T.sender E.Params.(Execution.Make_Params_signature.chain_id) tx = Some sender →
sentBy tx sender = true.
Proof.
intro Hsender. unfold sentBy. rewrite Hsender.
change (Z.eqb (OCamlString.compare sender sender) 0 = true).
rewrite OCamlString.compare_matches_structural.
unfold OCamlString.compare_structural.
rewrite String.eqb_refl. reflexivity.
Qed.
Lemma sender_not_created snapshot previous state tx validation senders a :
NonceAgreement (eligibleAt snapshot) (admissionNonces previous)
(B.t.world_state state) senders →
(∀ v, E.validate_transaction state tx = inl v →
∀ q, In q senders → A.is_smart_contract (accountAt (B.t.world_state state) q) = false →
¬ In q (S.t.accounts_created_in_current_transaction (transactionFinished state tx v))) →
E.validate_transaction state tx = inl validation →
In a senders →
¬ In a (S.t.accounts_created_in_current_transaction (transactionFinished state tx validation)).
Proof using F Hcold Hrevision Hwarm access cold reserve warm write.
intros Hnonce Hcreation Hv Ha.
destruct (A.is_smart_contract (accountAt (B.t.world_state state) a)) eqn:Hsc.
- destruct (Hnonce a Ha) as [_ [Hmap _]].
assert (Hreg : worldAccount a (fun code _ ⇒ regularCode code) (B.t.world_state state))
by (split; [exact Hmap|apply regular_code_contract; exact Hsc]).
pose proof (transaction_entry_regular state tx validation a Hreg) as Hentry.
assert (Hgas : 0 ≤ Evmc.Message.t.gas (fst (transactionVmInput state tx validation)) < 2^63).
{ unfold transactionVmInput. destruct (transactionPrefix state tx validation).
exact (proj1 (prepared_message_gas state tx validation _ Hv)). }
assert (Hfin : regularAccount a (transactionFinished state tx validation)).
{ unfold transactionFinished.
destruct (transactionVmInput state tx validation) as [msg entry].
cbn beta iota zeta delta [fst snd] in Hentry, Hgas.
destruct (migratedSender state (E.transaction_validation.sender validation)); [exact Hentry|].
exact (@execution_call_from_eoa_preserves_regular F a cold warm access write reserve
Hcold Hwarm tx msg Hrevision Hgas entry Hentry). }
destruct Hfin as [code [_ HP]].
exact (persistent_code_uncreated _ _ _ HP).
- exact (Hcreation validation Hv a Ha Hsc).
Qed.
Lemma validate_transaction_error_form state tx details :
E.validate_transaction state tx = inr (Execution.Error.Invalid_transaction details) →
E.validate_transaction state tx =
E.invalid_transaction (B.t.current_block state) tx
(Execution.Error.t.Invalid_transaction.reason details).
Proof using F.
intro Hfail. rewrite Hfail.
unfold E.validate_transaction, E.validate_authorizations in Hfail.
change (@Result.op_letdollar) with
(fun (A Error C : Set) (first : A + Error) (next : A → C + Error) ⇒
match first with inl value ⇒ next value | inr error ⇒ inr error end) in Hfail.
change (@Result._return) with (fun (A Error : Set) (value : A) ⇒ @inl A Error value) in Hfail.
change (@Result.when_) with (fun (Error : Set) (condition : bool) (failure : unit + Error) ⇒
if condition then failure else inl tt) in Hfail.
cbv beta iota zeta in Hfail.
destruct tx; cbn beta iota zeta in Hfail;
repeat (match type of Hfail with
| context [match ?x with Some _ ⇒ _ | None ⇒ _ end] ⇒ destruct x
| context [match ?x with [] ⇒ _ | _ :: _ ⇒ _ end] ⇒ destruct x
| context [if ?b then _ else _] ⇒ destruct b
end; cbn [Result.op_letdollar Result.when_ E.invalid_transaction] in Hfail;
try discriminate Hfail;
try (injection Hfail as <-; reflexivity)).
Qed.
Lemma affordable_no_insufficient_balance state tx sender price details fee :
T.sender E.Params.(Execution.Make_Params_signature.chain_id) tx = Some sender →
Gas.tx_effective_gas_price
(Block.Header.t.base_fee_per_gas (Block.t.header (B.t.current_block state))) tx = Some price →
Z.of_N (T.gas_limit tx × price) ≤ balanceAt (B.t.world_state state) sender →
E.validate_transaction state tx = inr (Execution.Error.Invalid_transaction details) →
Execution.Error.t.Invalid_transaction.reason details ≠
Execution.Error.Insufficient_balance fee.
Proof using F.
intros Hsender Hprice Hbal Hfail Hreason.
assert (Hquote : transactionQuote (Block.t.header (B.t.current_block state)) tx =
Some (sender, (T.gas_limit tx × price)%N)).
{ unfold transactionQuote, transactionSender. rewrite Hsender, Hprice. reflexivity. }
pose proof (affordable_quote_no_insufficient_balance state tx sender
(T.gas_limit tx × price)%N Hquote Hbal fee) as Hnot.
apply Hnot.
rewrite (validate_transaction_error_form state tx details Hfail), Hreason.
reflexivity.
Qed.
Lemma allows_emptying_destruct snapshot previous sender height authorities :
allowsEmptying snapshot previous sender height authorities = true →
A.t.code (accountAt snapshot sender) = Byte_string.Bytes.empty ∧
(emptyingAfter snapshot sender ≤ height)%N ∧
¬ In sender authorities ∧
¬ In sender (admissionActivity previous).
Proof using.
intro Hallow. unfold allowsEmptying in Hallow.
apply Bool.andb_true_iff in Hallow as [Hleft HnotIn].
apply Bool.andb_true_iff in Hleft as [Hcode Hheight].
change (Z.eqb (OCamlString.compare
(A.t.code (accountAt snapshot sender)) Byte_string.Bytes.empty) 0 = true) in Hcode.
apply Z.eqb_eq, address_compare_eq in Hcode.
apply bool_decide_eq_true in Hheight.
apply bool_decide_eq_true in HnotIn.
rewrite elem_of_app, !list_elem_of_In in HnotIn.
repeat split; try assumption; tauto.
Qed.
Theorem checked_transaction_no_insufficient_balance snapshot previous next
state tx senders until details fee :
(∀ a, T.sender E.Params.(Execution.Make_Params_signature.chain_id) tx = Some a →
In a senders) →
TransactionBudgetBacking snapshot previous state senders until →
checkTransaction snapshot (Block.t.header (B.t.current_block state)) previous tx = Some next →
E.process_transaction state tx = inr (Execution.Error.Invalid_transaction details) →
Execution.Error.t.Invalid_transaction.reason details ≠
Execution.Error.Insufficient_balance fee.
Proof using F Hcold Hwarm cold warm.
intros HsenderIn Hbacking Hcheck Hrun.
pose proof (transaction_error_from_validation state tx _ Hrun) as Hfail.
unfold checkTransaction in Hcheck.
destruct (T.sender _ tx) as [sender|] eqn:Hsender; [|discriminate].
destruct (Gas.tx_effective_gas_price _ tx) as [price|] eqn:Hprice; [|discriminate].
destruct (transactionChecks _ _ _ _); [|discriminate].
cbn zeta in Hcheck.
match type of Hcheck with context [if ?test then _ else _] ⇒
destruct test eqn:Hafford end; [|discriminate].
apply bool_decide_eq_true in Hafford.
assert (Hin : In sender senders) by (exact (HsenderIn sender eq_refl)).
assert (Hbal : Z.of_N (T.gas_limit tx × price) ≤ balanceAt (B.t.world_state state) sender).
{ destruct (allowsEmptying snapshot previous sender (number state) (recoveredAuthorities tx))
eqn:Hempty.
- destruct (allows_emptying_destruct _ _ _ _ _ Hempty) as [Hcode [_ [_ HnotPast]]].
destruct (tx_backing_idle _ _ _ _ _ Hbacking sender Hin HnotPast Hcode) as [_ HidleBal].
lia.
- destruct (tx_backing_budgets _ _ _ _ _ Hbacking sender Hin) as [_ [Hbudget _]].
lia. }
exact (affordable_no_insufficient_balance state tx sender price details fee
Hsender Hprice Hbal Hfail).
Qed.
Theorem checked_transaction_budget_backing snapshot previous next
state tx post senders until :
wordRange (Support.UintN.to_z (Chain.Monad.PARAMS.chain_id params)) →
headerWords (Block.t.header (B.t.current_block state)) →
(∀ a, In a senders → a ≠ native) →
Forall (fun auth ⇒ OCamlString.length (T.Authorization.t.address auth) = 20)
(T.authorization_list tx) →
transactionValueAndDataBounds tx →
signaturesAvoidAddress tx native →
(∀ a, T.sender E.Params.(Execution.Make_Params_signature.chain_id) tx = Some a →
In a senders) →
(∀ v, E.validate_transaction state tx = inl v →
∀ a, In a senders → A.is_smart_contract (accountAt (B.t.world_state state) a) = false →
¬ In a (S.t.accounts_created_in_current_transaction (transactionFinished state tx v))) →
(number state ≤ until)%N →
(until < number state + 3)%N →
TransactionBudgetBacking snapshot previous state senders until →
checkTransaction snapshot (Block.t.header (B.t.current_block state)) previous tx = Some next →
E.process_transaction state tx = inl post →
TransactionBudgetBacking snapshot next post senders until.
Proof using F Hcold Hhash Hnative Hrevision Hwarm U8 UB UI0 access cold reserve warm write.
intros Hchain Hheader HnotNative Hwidth Hvalue Hsig HsenderIn Hcreation HleUntil HltDelay
Hbacking Hcheck Hrun.
assert (Hv : ∃ validation, E.validate_transaction state tx = inl validation).
{ unfold E.process_transaction in Hrun.
destruct (E.validate_transaction state tx) as [v|err]; [eexists; reflexivity|discriminate]. }
destruct Hv as [validation Hv].
pose proof (process_transaction_current_block state tx post Hrun) as Hcurrent.
pose proof (tx_backing_balance _ _ _ _ _ Hbacking) as Hinitial.
pose proof (tx_backing_format _ _ _ _ _ Hbacking) as Hformat.
pose proof (tx_backing_nonce _ _ _ _ _ Hbacking) as Hnonce.
pose proof (tx_backing_activity _ _ _ _ _ Hbacking) as Hactivity.
pose proof (tx_backing_empty_code _ _ _ _ _ Hbacking) as HemptyCode.
pose proof (tx_backing_config _ _ _ _ _ Hbacking) as Hconfig.
pose proof (tx_backing_budgets _ _ _ _ _ Hbacking) as Hbudgets.
pose proof (tx_backing_idle _ _ _ _ _ Hbacking) as Hidle.
pose proof (tx_backing_status _ _ _ _ _ Hbacking) as Hstatus.
pose proof (@process_transaction_preserves_supply F UI0 UB U8
cold warm access write reserve Hrevision Hcold Hwarm Hhash Hnative
state tx post Hchain (proj1 (proj2 Hheader)) (proj2 (proj2 Hheader))
Hinitial Hformat Hwidth Hvalue Hrun) as [HpostBal [_ HpostFormat]].
pose proof (checked_transaction_activity_agreement cold warm access write reserve
Hrevision Hcold Hwarm snapshot previous next until state tx post
Hactivity HltDelay Hcheck Hrun) as HpostActivity.
pose proof (checked_transaction_empty_code cold warm access write reserve
Hrevision Hcold Hwarm snapshot previous next state tx post senders
HnotNative Hnonce HemptyCode Hcreation Hcheck Hrun) as HpostEmptyCode.
pose proof (process_transaction_activity cold warm access write reserve
Hrevision Hcold Hwarm state tx validation post (activity_map _ _ _ _ Hactivity)
Hv Hrun) as [HhistoryEq _].
unfold checkTransaction in Hcheck.
destruct (T.sender _ tx) as [sender|] eqn:Hsender; [|discriminate].
destruct (Gas.tx_effective_gas_price _ tx) as [price|] eqn:Hprice; [|discriminate].
destruct (transactionChecks _ _ _ _) eqn:HtxChecks; [|discriminate].
cbn zeta in Hcheck.
match type of Hcheck with context [if ?test then _ else _] ⇒
destruct test eqn:Hafford end; [|discriminate].
apply bool_decide_eq_true in Hafford.
injection Hcheck as <-.
assert (HsenderEq : E.transaction_validation.sender validation = sender ∧
(T.gas_limit tx × E.transaction_validation.effective_gas_price validation)%N =
(T.gas_limit tx × price)%N).
{ pose proof (validated_transaction_quote state tx validation Hv) as Hquote.
unfold transactionQuote, transactionSender in Hquote.
rewrite Hsender, Hprice in Hquote. injection Hquote as H1 H2.
split; symmetry; assumption. }
destruct HsenderEq as [HsenderVal HfeeN].
pose proof (account_balance_bounded_by_supply (B.t.world_state state)
(E.transaction_validation.sender validation) (proj1 (proj2 Hinitial))) as HsenderSupply.
pose proof (proj2 (proj2 Hinitial)) as HtotalSupply.
pose proof (validated_debit_range state tx validation Hv
ltac:(cbn beta in HsenderSupply; lia)) as [HfeeVal _].
rewrite HfeeN in HfeeVal.
assert (HsenderInList : In sender senders) by (exact (HsenderIn sender eq_refl)).
pose proof (process_transaction_nonce_agreement cold warm access write reserve
Hrevision Hcold Hwarm state tx post (eligibleAt snapshot) (admissionNonces previous)
senders sender HnotNative Hwidth Hsender Hnonce Hcreation Hrun) as HpostNonce.
assert (HpostConfig : ∃ initialStorage,
stringmaps.WellFormed initialStorage ∧
storageWords initialStorage ∧
(∀ a, Reserve_balance.user_reserve_balance snapshot (number post) a =
initialThreshold initialStorage (number post) a) ∧
worldConfig (number post) initialStorage (B.t.world_state post)).
{ destruct Hconfig as [initialStorage [HstMap [HstWords [HstSnap HstWorld]]]].
∃ initialStorage. rewrite Hcurrent.
refine (conj HstMap (conj HstWords (conj HstSnap _))).
exact (process_transaction_only_delayed_settings cold warm access write reserve
Hrevision Hcold Hwarm state tx validation post initialStorage Hsig Hv Hrun HstWorld). }
assert (HpostIdle : ∀ a, In a senders →
¬ In a (sender :: recoveredAuthorities tx ++ admissionActivity previous) →
A.t.code (accountAt snapshot a) = Byte_string.Bytes.empty →
A.t.code (accountAt (B.t.world_state post) a) = Byte_string.Bytes.empty ∧
balanceAt snapshot a ≤ balanceAt (B.t.world_state post) a).
{ intros a Ha HnotAct HsnapEmpty.
assert (HneqSender : sender ≠ a) by (intro Heq; apply HnotAct; left; exact Heq).
assert (HnotAuth : ¬ In a (recoveredAuthorities tx))
by (intro Hin; apply HnotAct; right; apply in_app_iff; left; exact Hin).
assert (HnotPast : ¬ In a (admissionActivity previous))
by (intro Hin; apply HnotAct; right; apply in_app_iff; right; exact Hin).
destruct (Hidle a Ha HnotPast HsnapEmpty) as [HstateEmpty HstateBal].
pose proof (sender_not_created snapshot previous state tx validation senders a
Hnonce Hcreation Hv Ha) as HnotCreated.
pose proof (process_transaction_empty_without_recovery cold warm access write reserve
Hrevision Hcold Hwarm Hhash Hnative state tx validation post a
Hchain Hheader Hinitial Hformat Hwidth Hvalue (HnotNative a Ha) HnotAuth
Hv Hrun HstateEmpty HnotCreated) as [HpostCode HpostBalBound].
assert (HeqbFalse : String.eqb (E.transaction_validation.sender validation) a = false)
by (apply String.eqb_neq; congruence).
rewrite HeqbFalse in HpostBalBound.
unfold balanceAt, accountAt in ×. split; [exact HpostCode|lia]. }
assert (HpostBoth : ∀ a, In a senders →
let emptying := allowsEmptying snapshot previous sender (number state) (recoveredAuthorities tx) in
let available := if emptying then balanceAt snapshot sender else admissionBudgets previous sender in
let nextBudget := if bool_decide (sender = a)
then (if emptying
then Z.min (Z.max (available -
Z.of_N (T.gas_limit tx × price) - U.to_z (T.value tx)) 0)
(reserveAt snapshot (number state) sender)
else available - Z.of_N (T.gas_limit tx × price))
else admissionBudgets previous a in
(0 ≤ nextBudget ∧
nextBudget ≤ balanceAt (B.t.world_state post) a ∧
nextBudget ≤ reserveAt snapshot (number post) a) ∧
(nextBudget = 0 ∨
A.is_smart_contract (accountAt (B.t.world_state post) a) = false ∨
sentInPriorTransactions post a = true ∨
(sentInHistory (B.t.world_state post) (number post) a = true ∧
∀ target, (number post < target ≤ until)%N →
sentInHistory (B.t.world_state post) target a = true))).
{ intros a Ha. cbn zeta. rewrite Hcurrent.
pose proof (sender_not_created snapshot previous state tx validation senders a
Hnonce Hcreation Hv Ha) as HnotCreated.
pose proof (account_balance_bounded_by_supply (B.t.world_state post) a
(proj1 (proj2 HpostBal))) as HpostNonneg.
change (0 ≤ balanceAt (B.t.world_state post) a ≤ totalBalance (B.t.world_state post))
in HpostNonneg.
destruct (bool_decide (sender = a)) eqn:HeqSender.
- apply bool_decide_eq_true in HeqSender. subst a.
assert (HsentPost : sentInPriorTransactions post sender = true).
{ rewrite (process_transaction_sent_in_prior state tx post sender Hrun).
rewrite (sent_by_sender tx sender Hsender), Bool.orb_true_r. reflexivity. }
split; [|right; right; left; exact HsentPost].
destruct (Hbudgets sender HsenderInList) as [HprevNonneg [HprevBal HprevRes]].
destruct (allowsEmptying snapshot previous sender (number state) (recoveredAuthorities tx))
eqn:Hallow.
+ destruct (allows_emptying_destruct _ _ _ _ _ Hallow)
as [HsnapEmpty [_ [HnotAuth HnotPast]]].
destruct (Hidle sender HsenderInList HnotPast HsnapEmpty)
as [HstateEmpty HstateBal].
pose proof (process_transaction_empty_without_recovery cold warm access write reserve
Hrevision Hcold Hwarm Hhash Hnative state tx validation post sender
Hchain Hheader Hinitial Hformat Hwidth Hvalue (HnotNative sender HsenderInList)
HnotAuth Hv Hrun HstateEmpty HnotCreated) as [_ HemptyBound].
rewrite HsenderVal, String.eqb_refl, HfeeVal in HemptyBound.
change (Z.max (balanceAt (B.t.world_state state) sender -
(Z.of_N (T.gas_limit tx × price) + U.to_z (T.value tx))) 0 ≤
balanceAt (B.t.world_state post) sender) in HemptyBound.
split; [lia|]. split; [lia|apply Z.le_min_r].
+ assert (HmayEmpty : mayEmpty (snd (transactionVmInput state tx validation)) = false).
{ apply Bool.not_true_is_false. intro Hmay.
pose proof (execution_emptying_implies_admission cold warm access write reserve
Hrevision Hcold Hwarm snapshot previous state tx validation senders until
ltac:(rewrite HsenderVal; exact HsenderInList)
(proj1 Hinitial) Hactivity HemptyCode HleUntil Hmay) as HallowTrue.
rewrite HsenderVal in HallowTrue. congruence. }
pose proof (process_transaction_ordinary_sender_bound cold warm access write reserve
Hrevision Hcold Hwarm Hhash Hnative state tx validation post
Hchain Hheader Hinitial Hformat Hwidth Hvalue Hv Hrun
ltac:(rewrite HsenderVal; exact (HnotNative sender HsenderInList))
HmayEmpty ltac:(rewrite HsenderVal; exact HnotCreated)) as HordBound.
cbn zeta in HordBound.
rewrite HsenderVal, HfeeVal in HordBound.
rewrite (tx_backing_user_reserve_eq _ _ _ _ _ sender Hbacking) in HordBound.
change (Z.min (balanceAt (B.t.world_state state) sender)
(reserveAt snapshot (number state) sender) -
Z.of_N (T.gas_limit tx × price) ≤
balanceAt (B.t.world_state post) sender) in HordBound.
pose proof (N2Z.is_nonneg (T.gas_limit tx × price)).
split; [lia|]. split; [lia|lia].
- apply bool_decide_eq_false in HeqSender.
assert (Hother : a ≠ E.transaction_validation.sender validation) by congruence.
destruct (Hbudgets a Ha) as [HprevNonneg [HprevBal HprevRes]].
assert (HhistEq : ∀ t, sentInHistory (B.t.world_state post) t a =
sentInHistory (B.t.world_state state) t a).
{ intro t. unfold sentInHistory. rewrite HhistoryEq. reflexivity. }
destruct (Hstatus a Ha) as [Hzero|[Hnoncontract|[Hprior|[HhistNow HhistFuture]]]].
+ split; [split; [exact HprevNonneg|split; [rewrite Hzero; exact (proj1 HpostNonneg)|exact HprevRes]]|left; exact Hzero].
+ destruct (Z.eq_dec (admissionBudgets previous a) 0) as [Hzero|Hnonzero].
× split; [split; [exact HprevNonneg|split; [rewrite Hzero; exact (proj1 HpostNonneg)|exact HprevRes]]|left; exact Hzero].
× assert (HresPos : Reserve_balance.user_reserve_balance
(B.t.world_state state) (number state) a ≠ 0).
{ intro HzeroRes.
pose proof (tx_backing_user_reserve_eq _ _ _ _ _ a Hbacking) as HresEq.
rewrite HzeroRes in HresEq.
unfold reserveAt in HprevRes. rewrite <- HresEq in HprevRes.
change (admissionBudgets previous a ≤ 0) in HprevRes.
apply Hnonzero. exact (Z.le_antisymm _ _ HprevRes HprevNonneg). }
destruct Hconfig as [initialStorage [HstMap [HstWords [_ HstWorld]]]].
pose proof (process_transaction_protects_guarded_nonsender cold warm access write reserve
Hrevision Hcold Hwarm Hhash Hnative state tx validation post a initialStorage
Hchain Hheader Hinitial Hformat Hwidth Hvalue Hsig HstMap HstWords HstWorld
Hv Hrun Hother (HnotNative a Ha) Hnoncontract HresPos HnotCreated)
as [HpostSc HguardedBound].
rewrite (tx_backing_user_reserve_eq _ _ _ _ _ a Hbacking) in HguardedBound.
change (Z.min (balanceAt (B.t.world_state state) a)
(reserveAt snapshot (number state) a) ≤
balanceAt (B.t.world_state post) a) in HguardedBound.
split.
-- split; [exact HprevNonneg|split; [lia|exact HprevRes]].
-- right. left. exact HpostSc.
+ assert (Hrecent : Reserve_balance.sent_in_window
(S.t.with_tx_chain_id E.Params.(Execution.Make_Params_signature.chain_id)
(S.make state (E.transaction_validation.sender validation) tx)) a = true).
{ rewrite sent_in_window_block_eq. unfold blockSentInWindow.
rewrite Hprior. reflexivity. }
pose proof (process_transaction_recent_sender_bound cold warm access write reserve
Hrevision Hcold Hwarm Hhash Hnative state tx validation post a
Hchain Hheader Hinitial Hformat Hwidth Hvalue Hv Hrun Hother Hrecent HnotCreated)
as HrecentBound.
cbv beta in HrecentBound.
rewrite (tx_backing_user_reserve_eq _ _ _ _ _ a Hbacking) in HrecentBound.
change (Z.min (balanceAt (B.t.world_state state) a)
(reserveAt snapshot (number state) a) ≤
balanceAt (B.t.world_state post) a) in HrecentBound.
assert (HpostPrior : sentInPriorTransactions post a = true).
{ rewrite (process_transaction_sent_in_prior state tx post a Hrun), Hprior.
reflexivity. }
split.
× split; [exact HprevNonneg|split; [lia|exact HprevRes]].
× right. right. left. exact HpostPrior.
+ assert (Hrecent : Reserve_balance.sent_in_window
(S.t.with_tx_chain_id E.Params.(Execution.Make_Params_signature.chain_id)
(S.make state (E.transaction_validation.sender validation) tx)) a = true).
{ rewrite sent_in_window_block_eq. unfold blockSentInWindow.
rewrite HhistNow, Bool.orb_true_r. reflexivity. }
pose proof (process_transaction_recent_sender_bound cold warm access write reserve
Hrevision Hcold Hwarm Hhash Hnative state tx validation post a
Hchain Hheader Hinitial Hformat Hwidth Hvalue Hv Hrun Hother Hrecent HnotCreated)
as HrecentBound.
cbv beta in HrecentBound.
rewrite (tx_backing_user_reserve_eq _ _ _ _ _ a Hbacking) in HrecentBound.
change (Z.min (balanceAt (B.t.world_state state) a)
(reserveAt snapshot (number state) a) ≤
balanceAt (B.t.world_state post) a) in HrecentBound.
split.
× split; [exact HprevNonneg|split; [lia|exact HprevRes]].
× right. right. right. split; [rewrite HhistEq; exact HhistNow|].
intros target Htarget. rewrite HhistEq. exact (HhistFuture target Htarget). }
constructor; try assumption.
- intros a Ha. exact (proj1 (HpostBoth a Ha)).
- intros a Ha. exact (proj2 (HpostBoth a Ha)).
Qed.
Theorem checked_transactions_budget_backing snapshot previous next
state txs senders until :
wordRange (Support.UintN.to_z (Chain.Monad.PARAMS.chain_id params)) →
headerWords (Block.t.header (B.t.current_block state)) →
(∀ a, In a senders → a ≠ native) →
Forall (fun tx ⇒
transactionValueAndDataBounds tx ∧
Forall (fun auth ⇒ OCamlString.length (T.Authorization.t.address auth) = 20)
(T.authorization_list tx) ∧
signaturesAvoidAddress tx native ∧
(∀ a, T.sender E.Params.(Execution.Make_Params_signature.chain_id) tx = Some a →
In a senders)) txs →
transactionsAvoidSenderCreation senders state txs →
(number state ≤ until)%N →
(until < number state + 3)%N →
TransactionBudgetBacking snapshot previous state senders until →
checkTransactions snapshot (Block.t.header (B.t.current_block state)) previous txs = Some next →
match Result.List.fold_leftM E.process_transaction state txs with
| inl post ⇒
TransactionBudgetBacking snapshot next post senders until ∧
B.t.current_block post = B.t.current_block state ∧
W.t.history (B.t.world_state post) = W.t.history (B.t.world_state state) ∧
OCamlList.map fst (B.t.transactions_processed post) =
OCamlList.map fst (B.t.transactions_processed state) ++ txs
| inr (Execution.Error.Invalid_transaction details) ⇒
∀ fee,
Execution.Error.t.Invalid_transaction.reason details ≠
Execution.Error.Insufficient_balance fee
| inr _ ⇒ True
end.
Proof using F Hcold Hhash Hnative Hrevision Hwarm U8 UB UI0 access cold reserve warm write.
intros Hchain Hheader HnotNative Htxs.
revert previous state Hheader.
induction Htxs as [|tx rest [Hvalue [Hwidth [Hsig HsenderIn]]] Hrest IH];
intros previous state Hheader Hcreation HleUntil HltDelay Hbacking Hcheck.
- cbn [checkTransactions] in Hcheck. injection Hcheck as <-.
cbn [Result.List.fold_leftM].
rewrite app_nil_r.
split; [exact Hbacking|split; [reflexivity|split; [reflexivity|reflexivity]]].
- cbn [checkTransactions] in Hcheck.
destruct (checkTransaction snapshot (Block.t.header (B.t.current_block state)) previous tx)
as [middle|] eqn:Htx; [|discriminate].
destruct Hcreation as [HcreateHead HcreateTail].
change (Result.List.fold_leftM E.process_transaction state (tx :: rest)) with
(Result.op_letdollar (E.process_transaction state tx)
(fun after ⇒ Result.List.fold_leftM E.process_transaction after rest)).
destruct (E.process_transaction state tx) as [after|error] eqn:Hstep.
+ assert (Hv : ∃ v, E.validate_transaction state tx = inl v).
{ unfold E.process_transaction in Hstep.
destruct (E.validate_transaction state tx); [eexists; reflexivity|discriminate]. }
destruct Hv as [v Hv].
pose proof (process_transaction_current_block state tx after Hstep) as Hcurr.
pose proof (process_transaction_transactions_processed state tx after Hstep) as Hproc.
pose proof (process_transaction_activity cold warm access write reserve
Hrevision Hcold Hwarm state tx v after
(activity_map _ _ _ _ (tx_backing_activity _ _ _ _ _ Hbacking)) Hv Hstep) as [Hhist _].
pose proof (checked_transaction_budget_backing snapshot previous middle state tx after
senders until Hchain Hheader HnotNative Hwidth Hvalue Hsig HsenderIn HcreateHead
HleUntil HltDelay Hbacking Htx Hstep) as HafterBacking.
assert (HafterHeader : headerWords (Block.t.header (B.t.current_block after)))
by (rewrite Hcurr; exact Hheader).
assert (HafterLe : (number after ≤ until)%N) by (cbv beta; rewrite Hcurr; exact HleUntil).
assert (HafterLt : (until < number after + 3)%N) by (cbv beta; rewrite Hcurr; exact HltDelay).
assert (HafterCheck : checkTransactions snapshot (Block.t.header (B.t.current_block after))
middle rest = Some next) by (rewrite Hcurr; exact Hcheck).
specialize (IH middle after HafterHeader (HcreateTail after eq_refl)
HafterLe HafterLt HafterBacking HafterCheck).
change (Result.op_letdollar (inl after)
(fun after ⇒ Result.List.fold_leftM E.process_transaction after rest))
with (Result.List.fold_leftM E.process_transaction after rest).
destruct (Result.List.fold_leftM E.process_transaction after rest) as [post|err]; [|exact IH].
destruct IH as [HpostBacking [HpostCurr [HpostHist HpostProc]]].
split; [exact HpostBacking|].
split; [congruence|].
split; [congruence|].
rewrite HpostProc, Hproc, <- app_assoc. reflexivity.
+ change (Result.op_letdollar (inr error)
(fun after ⇒ Result.List.fold_leftM E.process_transaction after rest))
with (@inr B.t Execution.Error.t error).
destruct error as [|details]; [exact I|].
intro fee.
exact (checked_transaction_no_insufficient_balance snapshot previous middle state tx
senders until details fee HsenderIn Hbacking Htx Hstep).
Qed.
End Execution.
Print Assumptions checked_transaction_no_insufficient_balance.
Print Assumptions checked_transaction_budget_backing.
Print Assumptions checked_transactions_budget_backing.