Library QedkConcrete.publictransaction
The fee quoted before execution
From Stdlib Require Import NArith ZArith Lia List.
Require Import RocqOfOCaml.RocqOfOCaml.
Require Import MonadVM.Execution QedkConcrete.validation.
Set Default Goal Selector "!".
Import ListNotations.
Module E := Execution.Make.
Module B := State.BlockState.
Module T := Chain.Ethereum.Transaction.
Module A := Chain.Ethereum.Account.
Module W := State.WorldState.
Section PublicTransaction.
Require Import RocqOfOCaml.RocqOfOCaml.
Require Import MonadVM.Execution QedkConcrete.validation.
Set Default Goal Selector "!".
Import ListNotations.
Module E := Execution.Make.
Module B := State.BlockState.
Module T := Chain.Ethereum.Transaction.
Module A := Chain.Ethereum.Account.
Module W := State.WorldState.
Section PublicTransaction.
F supplies the chain ID and revision of the executor being checked.
Takes one signed transaction tx. Return Some sender by recovering its
signature with the configured chain ID, or None on failure. No account
state is read, and this does not check the sender's nonce or balance.
header is the candidate's block header, supplying the base fee; tx is
its signed transaction. Return Some (sender, fee) with the recovered
sender and gas-fee bound F(tx), in wei: gas limit times effective gas price.
None means sender recovery or gas-price calculation failed. Some does
not assert sufficient balance, nonce equality or other transaction validity.
Definition transactionQuote (header : Chain.Ethereum.Block.Header.t) (tx : T.t)
: option (Chain.Ethereum.Address.t × N) :=
match transactionSender tx,
@Gas.tx_effective_gas_price (Chain.Ethereum.Block.Header.t.base_fee_per_gas header) tx with
| Some sender, Some price ⇒ Some (sender, (T.gas_limit tx × price)%N)
| _, _ ⇒ None
end.
Lemma validated_transaction_quote state tx validation :
@E.validate_transaction F state tx = inl validation →
transactionQuote (Chain.Ethereum.Block.t.header (B.t.current_block state)) tx =
Some (E.transaction_validation.sender validation,
(T.gas_limit tx × E.transaction_validation.effective_gas_price validation)%N).
Proof.
intro Hvalidated. unfold E.validate_transaction in Hvalidated. cbn zeta in Hvalidated.
do 13 (apply validation.bind_success in Hvalidated;
let step := fresh "step" in let Hstep := fresh "Hstep" in
destruct Hvalidated as [step [Hstep Hvalidated]];
cbn beta iota zeta in Hvalidated).
injection Hvalidated as Heq. subst validation.
unfold transactionQuote. fold (transactionSender tx) in Hstep1.
destruct (transactionSender tx) as [sender|] eqn:Hsender; [|discriminate Hstep1].
injection Hstep1 as <-.
destruct (@Gas.tx_effective_gas_price (Chain.Ethereum.Block.Header.t.base_fee_per_gas
(Chain.Ethereum.Block.t.header (B.t.current_block state))) tx) as [price|] eqn:Hprice;
[|discriminate Hstep9].
injection Hstep9 as <-.
reflexivity.
Qed.
: option (Chain.Ethereum.Address.t × N) :=
match transactionSender tx,
@Gas.tx_effective_gas_price (Chain.Ethereum.Block.Header.t.base_fee_per_gas header) tx with
| Some sender, Some price ⇒ Some (sender, (T.gas_limit tx × price)%N)
| _, _ ⇒ None
end.
Lemma validated_transaction_quote state tx validation :
@E.validate_transaction F state tx = inl validation →
transactionQuote (Chain.Ethereum.Block.t.header (B.t.current_block state)) tx =
Some (E.transaction_validation.sender validation,
(T.gas_limit tx × E.transaction_validation.effective_gas_price validation)%N).
Proof.
intro Hvalidated. unfold E.validate_transaction in Hvalidated. cbn zeta in Hvalidated.
do 13 (apply validation.bind_success in Hvalidated;
let step := fresh "step" in let Hstep := fresh "Hstep" in
destruct Hvalidated as [step [Hstep Hvalidated]];
cbn beta iota zeta in Hvalidated).
injection Hvalidated as Heq. subst validation.
unfold transactionQuote. fold (transactionSender tx) in Hstep1.
destruct (transactionSender tx) as [sender|] eqn:Hsender; [|discriminate Hstep1].
injection Hstep1 as <-.
destruct (@Gas.tx_effective_gas_price (Chain.Ethereum.Block.Header.t.base_fee_per_gas
(Chain.Ethereum.Block.t.header (B.t.current_block state))) tx) as [price|] eqn:Hprice;
[|discriminate Hstep9].
injection Hstep9 as <-.
reflexivity.
Qed.
This statement deliberately has no successful-validation premise.
Earlier validation failures remain possible, but cannot report fee
insolvency once the public quote fits the actual sender balance.
Theorem affordable_quote_no_insufficient_balance state tx sender fee :
transactionQuote (Chain.Ethereum.Block.t.header (B.t.current_block state)) tx = Some (sender, fee) →
(Z.of_N fee ≤ Numeric.U256.to_z (A.t.balance
(State.op_pointcaretlparenrparen (B.t.world_state state) (@W.account None sender))))%Z →
∀ details,
@E.validate_transaction F state tx ≠
@E.invalid_transaction F E.transaction_validation (B.t.current_block state) tx
(Execution.Error.Insufficient_balance details).
Proof.
unfold transactionQuote.
destruct (transactionSender tx) as [recovered|] eqn:Hsender; [|discriminate].
destruct (@Gas.tx_effective_gas_price (Chain.Ethereum.Block.Header.t.base_fee_per_gas
(Chain.Ethereum.Block.t.header (B.t.current_block state))) tx) as [price|] eqn:Hprice;
[|discriminate].
intro Hquote. injection Hquote as <- <-. intros Hbalance details.
unfold E.validate_transaction. cbn zeta. fold (transactionSender tx). rewrite Hsender.
rewrite Hprice.
assert (Hfee : Support.UintN.op_gt
(@Support.UintN.op_star (T.gas_limit tx) price)
(@Numeric.U256.to_uint (A.t.balance (State.op_pointcaretlparenrparen
(B.t.world_state state) (@W.account None recovered)))) = false).
{ apply N.ltb_ge. change (@Support.UintN.op_star (T.gas_limit tx) price)
with ((T.gas_limit tx × price)%N).
rewrite reservecheck.u256_to_uint by (pose proof (N2Z.is_nonneg (T.gas_limit tx × price)); lia).
apply N2Z.inj_le. rewrite Z2N.id by (pose proof (N2Z.is_nonneg (T.gas_limit tx × price)); lia).
exact Hbalance. }
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).
change (@Result._return) with (fun (A Error : Set) (value : A) ⇒ @inl A Error value).
cbv beta iota zeta. rewrite Hfee.
unfold E.validate_authorizations.
change (@Result.when_) with (fun (Error : Set) (condition : bool) (failure : unit + Error) ⇒
if condition then failure else inl tt).
cbv beta iota zeta.
destruct tx; cbn beta iota zeta.
all: repeat (cbn [Result.op_letdollar Result.when_ E.invalid_transaction];
try discriminate;
match goal 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).
all: cbn [Result.op_letdollar Result.when_ E.invalid_transaction]; discriminate.
Qed.
End PublicTransaction.
transactionQuote (Chain.Ethereum.Block.t.header (B.t.current_block state)) tx = Some (sender, fee) →
(Z.of_N fee ≤ Numeric.U256.to_z (A.t.balance
(State.op_pointcaretlparenrparen (B.t.world_state state) (@W.account None sender))))%Z →
∀ details,
@E.validate_transaction F state tx ≠
@E.invalid_transaction F E.transaction_validation (B.t.current_block state) tx
(Execution.Error.Insufficient_balance details).
Proof.
unfold transactionQuote.
destruct (transactionSender tx) as [recovered|] eqn:Hsender; [|discriminate].
destruct (@Gas.tx_effective_gas_price (Chain.Ethereum.Block.Header.t.base_fee_per_gas
(Chain.Ethereum.Block.t.header (B.t.current_block state))) tx) as [price|] eqn:Hprice;
[|discriminate].
intro Hquote. injection Hquote as <- <-. intros Hbalance details.
unfold E.validate_transaction. cbn zeta. fold (transactionSender tx). rewrite Hsender.
rewrite Hprice.
assert (Hfee : Support.UintN.op_gt
(@Support.UintN.op_star (T.gas_limit tx) price)
(@Numeric.U256.to_uint (A.t.balance (State.op_pointcaretlparenrparen
(B.t.world_state state) (@W.account None recovered)))) = false).
{ apply N.ltb_ge. change (@Support.UintN.op_star (T.gas_limit tx) price)
with ((T.gas_limit tx × price)%N).
rewrite reservecheck.u256_to_uint by (pose proof (N2Z.is_nonneg (T.gas_limit tx × price)); lia).
apply N2Z.inj_le. rewrite Z2N.id by (pose proof (N2Z.is_nonneg (T.gas_limit tx × price)); lia).
exact Hbalance. }
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).
change (@Result._return) with (fun (A Error : Set) (value : A) ⇒ @inl A Error value).
cbv beta iota zeta. rewrite Hfee.
unfold E.validate_authorizations.
change (@Result.when_) with (fun (Error : Set) (condition : bool) (failure : unit + Error) ⇒
if condition then failure else inl tt).
cbv beta iota zeta.
destruct tx; cbn beta iota zeta.
all: repeat (cbn [Result.op_letdollar Result.when_ E.invalid_transaction];
try discriminate;
match goal 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).
all: cbn [Result.op_letdollar Result.when_ E.invalid_transaction]; discriminate.
Qed.
End PublicTransaction.