Library SetcodeConcrete.emptyingagreement

Execution cannot grant an emptying exception rejected by admission

Public activity records every recovered authority, not just applied authorizations. Together with actual code preservation, this prevents an account from clearing its code and emptying in the same window.
For a protected sender with no recorded activity since snapshot, empty code in world implies it already had empty code in snapshot. An account is allowed to acquire regular code; it is not assumed to retain its original delegation marker.
Definition EmptyCodeAgreement snapshot activity world senders :=
  ∀ a, In a senders → ¬ In a activity →
    A.t.code (consensuschecks.accountAt world a) = Byte_string.Bytes.empty →
    A.t.code (consensuschecks.accountAt snapshot a) = Byte_string.Bytes.empty.

Lemma initial_empty_code_agreement snapshot senders :
  EmptyCodeAgreement snapshot [] snapshot senders.
Proof. intros a Ha Hnot Hcode. exact Hcode. Qed.

Section Execution.
Context {F : E.FArgs}.
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).

Theorem checked_transaction_empty_code snapshot previous next state tx post senders :
  (∀ a, In a senders → a ≠ Reserve_balance.precompile_address) →
  NonceAgreement (eligibleAt snapshot) (admissionNonces previous) (B.t.world_state state) senders →
  EmptyCodeAgreement snapshot (admissionActivity previous) (B.t.world_state state) senders →
  (∀ validation, E.validate_transaction state tx = inl validation →
    ∀ a, In a senders → A.is_smart_contract (consensuschecks.accountAt (B.t.world_state state) a) = false →
    ¬ In a (S.t.accounts_created_in_current_transaction (transactionFinished state tx validation))) →
  checkTransaction snapshot (Block.t.header (B.t.current_block state)) previous tx = Some next →
  E.process_transaction state tx = inl post →
  EmptyCodeAgreement snapshot (admissionActivity next) (B.t.world_state post) senders.
Proof.
  intros Hnative Hnonce Hbefore Hcreation Hcheck Hrun.
  assert (Hv : ∃ validation, E.validate_transaction state tx = inl validation).
  { unfold E.process_transaction in Hrun. destruct (E.validate_transaction state tx);
      [eexists; reflexivity|discriminate]. }
  destruct Hv as [validation Hv].
  pose proof (validated_sender _ _ _ Hv) as Hsender.
  unfold checkTransaction in Hcheck. rewrite Hsender in Hcheck.
  destruct (Gas.tx_effective_gas_price _ tx); [|discriminate].
  match type of Hcheck with context [if ?test then _ else _] ⇒ destruct test end; [|discriminate].
  cbn zeta in Hcheck.
  match type of Hcheck with context [if ?test then _ else _] ⇒ destruct test end; [|discriminate].
  injection Hcheck as <-.
  intros a Ha Hnot Hempty. cbn [admissionActivity] in Hnot.
  assert (Hauthority : ¬ In a (recoveredAuthorities tx)) by
    (intro Hin; apply Hnot; right; apply in_app_iff; left; exact Hin).
  assert (Hpast : ¬ In a (admissionActivity previous)) by
    (intro Hin; apply Hnot; right; apply in_app_iff; right; exact Hin).
  apply (Hbefore a Ha Hpast).
  exact (process_transaction_empty_code_requires_empty_before cold warm access write reserve
    Hrevision Hcold Hwarm state tx validation post a (Hnative a Ha)
    (proj1 (proj2 (Hnonce a Ha))) Hauthority Hv Hrun (Hcreation validation Hv a Ha) Hempty).
Qed.

Theorem checked_transactions_empty_code snapshot previous next state txs post senders :
  (∀ a, In a senders → a ≠ Reserve_balance.precompile_address) →
  Forall (fun tx ⇒ Forall (fun auth ⇒ OCamlString.length (T.Authorization.t.address auth) = 20%Z)
    (T.authorization_list tx)) txs →
  NonceAgreement (eligibleAt snapshot) (admissionNonces previous) (B.t.world_state state) senders →
  EmptyCodeAgreement snapshot (admissionActivity previous) (B.t.world_state state) senders →
  transactionsAvoidSenderCreation senders state txs →
  checkTransactions snapshot (Block.t.header (B.t.current_block state)) previous txs = Some next →
  Result.List.fold_leftM E.process_transaction state txs = inl post →
  EmptyCodeAgreement snapshot (admissionActivity next) (B.t.world_state post) senders.
Proof.
  intros Hnative Hwidth. revert previous state.
  induction Hwidth as [|tx rest Hwidth Hrest IH];
    intros previous state Hnonce Hcode Hcreation Hcheck Hrun.
  - cbn [checkTransactions] in Hcheck. injection Hcheck as <-.
    injection Hrun as <-. exact Hcode.
  - cbn [checkTransactions] in Hcheck.
    destruct (checkTransaction snapshot (Block.t.header (B.t.current_block state)) previous tx)
      as [middle|] eqn:Htx; [|discriminate].
    change (Result.op_letdollar (E.process_transaction state tx)
      (fun after ⇒ Result.List.fold_leftM E.process_transaction after rest) = inl post) in Hrun.
    destruct (E.process_transaction state tx) as [after|error] eqn:Hstep; [|discriminate].
    destruct Hcreation as [Hcreation Htail].
    assert (Hone : transactionsAvoidSenderCreation senders state [tx])
      by (split; [exact Hcreation|intros; exact I]).
    assert (HcheckOne : checkTransactions snapshot (Block.t.header (B.t.current_block state))
      previous [tx] = Some middle) by (cbn [checkTransactions]; rewrite Htx; reflexivity).
    assert (HrunOne : Result.List.fold_leftM E.process_transaction state [tx] = inl after)
      by (change (Result.op_letdollar (E.process_transaction state tx)
        (fun s ⇒ inl s) = inl after); rewrite Hstep; reflexivity).
    apply (IH middle after).
    + exact (transactions_nonce_agreement cold warm access write reserve Hrevision Hcold Hwarm
        snapshot previous middle state [tx] after senders Hnative
        ltac:(constructor; [exact Hwidth|constructor]) Hnonce Hone HcheckOne HrunOne).
    + exact (checked_transaction_empty_code snapshot previous middle state tx after senders
        Hnative Hnonce Hcode Hcreation Htx Hstep).
    + exact (Htail after Hstep).
    + rewrite (process_transaction_current_block state tx after Hstep). exact Hcheck.
    + exact Hrun.
Qed.

The VM-entry counter includes this transaction's recovered authorities, while its sender is recorded only at transaction completion.
Lemma transaction_entry_counter state tx validation query :
  stringmaps.WellFormed (W.t.next_emptying_transaction_block (B.t.world_state state)) →
  emptyingAfter (S.t.world_state (snd (transactionVmInput state tx validation))) query =
    if existsb (fun a ⇒ String.eqb a query) (recoveredAuthorities tx)
    then (Block.Header.t.number (Block.t.header (B.t.current_block state)) + 3)%N
    else emptyingAfter (B.t.world_state state) query.
Proof.
  intro Hmap. pose proof (transaction_prefix_context state tx validation) as Hcontext.
  unfold transactionVmInput. destruct (transactionPrefix state tx validation) as [applied prepared].
  cbn beta iota zeta delta [fst snd] in Hcontext |- ×.
  unfold migrationContext, transactionContext in Hcontext.
  injection Hcontext as Hsnapshot Hblock Hsender Hlimit Hprice Hhistory Hcounters Hprior Hchain.
  assert (Hprepared : stringmaps.WellFormed (W.t.next_emptying_transaction_block (S.t.world_state prepared)))
    by (rewrite Hcounters; exact Hmap).
  pose proof (bump_counters_exact prepared (recoveredAuthorities tx) Hprepared) as [_ [_ [_ Hread]]].
  rewrite Hrevision. cbn beta iota zeta.
  rewrite native_recovered_authorities.
  change (emptyingAfter (S.t.world_state
    (E.bump_emptying_transaction_counters (recoveredAuthorities tx) prepared)) query =
    if existsb (fun a ⇒ String.eqb a query) (recoveredAuthorities tx)
    then (Block.Header.t.number (Block.t.header (B.t.current_block state)) + 3)%N
    else emptyingAfter (B.t.world_state state) query).
  unfold emptyingAfter. rewrite Hread, Hblock.
  destruct (existsb _ _); [reflexivity|].
  unfold State.op_pointcaretlparenrparen, W.next_emptying_transaction_block_for.
  cbn. rewrite Hcounters. reflexivity.
Qed.

If execution grants the emptying exception, the original public check grants it too. Thus public ordinary transactions remain ordinary at execution. The code/activity invariants are preservation properties, not additional assumptions of the intended whole-window theorem.
Theorem execution_emptying_implies_admission snapshot previous state tx validation senders until :
  In (E.transaction_validation.sender validation) senders →
  stringmaps.WellFormed (W.t.accounts (B.t.world_state state)) →
  ActivityAgreement snapshot (admissionActivity previous) (B.t.world_state state) until →
  EmptyCodeAgreement snapshot (admissionActivity previous) (B.t.world_state state) senders →
  (Block.Header.t.number (Block.t.header (B.t.current_block state)) ≤ until)%N →
  mayEmpty (snd (transactionVmInput state tx validation)) = true →
  allowsEmptying snapshot previous (E.transaction_validation.sender validation)
    (Block.Header.t.number (Block.t.header (B.t.current_block state))) (recoveredAuthorities tx) = true.
Proof.
  intros HsenderIn Hmap Hactivity Hcode Htime Hempty.
  pose proof (transaction_entry_metadata state tx validation) as [_ [Hblock Hsender]].
  rewrite emptying_requires_empty_code in Hempty.
  rewrite Hsender, Hblock in Hempty. apply Bool.andb_true_iff in Hempty as [Hcounter Hempty].
  change (N.leb (emptyingAfter (S.t.world_state (snd (transactionVmInput state tx validation)))
    (E.transaction_validation.sender validation))
    (Block.Header.t.number (Block.t.header (B.t.current_block state))) = true) in Hcounter.
  rewrite (transaction_entry_counter state tx validation _ (activity_map _ _ _ _ Hactivity)) in Hcounter.
  assert (HnotAuth : ¬ In (E.transaction_validation.sender validation) (recoveredAuthorities tx)).
  { intro Hin. rewrite (proj2 (existsb_address _ _) Hin) in Hcounter.
    apply N.leb_le in Hcounter. lia. }
  assert (Habsent : existsb (fun a ⇒ String.eqb a (E.transaction_validation.sender validation))
    (recoveredAuthorities tx) = false).
  { apply Bool.not_true_is_false. intro Hyes. apply HnotAuth, existsb_address, Hyes. }
  rewrite Habsent in Hcounter. apply N.leb_le in Hcounter.
  destruct (proj1 (activity_counter_eligibility _ _ _ _ _ _ Hactivity Htime) Hcounter)
    as [Hsnapshot HnotPast].
  change (Z.eqb (OCamlString.compare
    (A.t.code (reserve.accountAt (snd (transactionVmInput state tx validation))
      (E.transaction_validation.sender validation))) Byte_string.Bytes.empty) 0 = true) in Hempty.
  apply Z.eqb_eq, address_compare_eq in Hempty.
  pose proof (transaction_entry_code_without_recovery state tx validation
    (E.transaction_validation.sender validation) Hmap HnotAuth) as Hsame.
  assert (Hbefore : A.t.code (consensuschecks.accountAt (B.t.world_state state)
    (E.transaction_validation.sender validation)) = Byte_string.Bytes.empty)
    by (exact (eq_trans (eq_sym Hsame) Hempty)).
  pose proof (Hcode _ HsenderIn HnotPast Hbefore) as HsnapshotCode.
  unfold allowsEmptying. rewrite HsnapshotCode.
  change ((true && bool_decide (emptyingAfter snapshot (E.transaction_validation.sender validation) ≤
    Block.Header.t.number (Block.t.header (B.t.current_block state)))%N &&
    bool_decide (¬ elem_of (E.transaction_validation.sender validation)
      (recoveredAuthorities tx ++ admissionActivity previous))) = true).
  apply Bool.andb_true_iff. split.
  - apply Bool.andb_true_iff. split; [reflexivity|apply bool_decide_eq_true; exact Hsnapshot].
  - apply bool_decide_eq_true. rewrite elem_of_app, !list_elem_of_In. tauto.
Qed.
End Execution.
Print Assumptions execution_emptying_implies_admission.