Computational

{-# OPTIONS --safe #-}

open import Ledger.Dijkstra.Specification.Transaction using (TransactionStructure)

module Ledger.Dijkstra.Specification.Entities.Properties.Computational
  (txs : TransactionStructure) (open TransactionStructure txs) where

open import Ledger.Prelude
open import Ledger.Dijkstra.Specification.Certs govStructure
open import Ledger.Dijkstra.Specification.Entities txs
open import Ledger.Dijkstra.Specification.Gov govStructure

open import Ledger.Dijkstra.Specification.Certs.Properties.Computational govStructure
open import stdlib.Data.Maybe
open import stdlib-meta.Tactic.GenError using (genErrors)
import Data.Maybe.Relation.Unary.Any as M
import Data.Maybe.Relation.Unary.All as M

open RewardAddress

open GovVote

module Computational-CERTS = Computational Computational-CERTS

instance
  Computational-SUBENTITIES : Computational _⊢_⇀⦇_,SUBENTITIES⦈_ String
  Computational-SUBENTITIES = record {go}
    where
      module go (Γ : SubEntitiesEnv) (s₀ : CertState) (txSub : SubLevelTx)
                (let module Γ = SubEntitiesEnv Γ
                     refresh         = mapPartial (isGovVoterDRep ∘ voter) (fromList (ListOfGovVotesOf txSub))
                     refreshedDReps  = mapValueRestricted (const (Γ.epoch  + Γ.pp .PParams.drepActivity)) (DRepsOf s₀) refresh

                     withdrawals               = WithdrawalsOf txSub
                     withdrawalsCredentials    = mapˢ stake (dom withdrawals)
                     accountBalanceIntervals   = BalanceIntervalsOf txSub
                     directDeposits            = DirectDepositsOf txSub
                     directDepositsCredentials = mapˢ stake (dom directDeposits)

                ) where

        s₁ : CertState
        s₁ = $\begin{pmatrix} \begin{pmatrix} \,\href{Ledger.Dijkstra.Specification.Gov.Actions.html#4773}{\htmlId{1877}{\htmlClass{Field}{\text{VoteDelegsOf}}}}\, \,\href{Ledger.Dijkstra.Specification.Entities.Properties.Computational.html#1112}{\htmlId{1890}{\htmlClass{Bound}{\text{s₀}}}}\, \\ \,\href{Ledger.Dijkstra.Specification.Certs.html#7315}{\htmlId{1895}{\htmlClass{Field}{\text{StakeDelegsOf}}}}\, \,\href{Ledger.Dijkstra.Specification.Entities.Properties.Computational.html#1112}{\htmlId{1909}{\htmlClass{Bound}{\text{s₀}}}}\, \\ \,\href{Ledger.Dijkstra.Specification.Entities.html#2912}{\htmlId{1914}{\htmlClass{Function}{\text{applyWithdrawals}}}}\, \,\href{Ledger.Dijkstra.Specification.Entities.Properties.Computational.html#1458}{\htmlId{1931}{\htmlClass{Bound}{\text{withdrawals}}}}\, \,\htmlId{1943}{\htmlClass{Symbol}{\text{(}}}\,\,\href{Ledger.Dijkstra.Specification.Certs.html#7094}{\htmlId{1944}{\htmlClass{Field}{\text{RewardsOf}}}}\, \,\href{Ledger.Dijkstra.Specification.Entities.Properties.Computational.html#1112}{\htmlId{1954}{\htmlClass{Bound}{\text{s₀}}}}\,\,\htmlId{1956}{\htmlClass{Symbol}{\text{)}}}\, \\ \,\href{Ledger.Dijkstra.Specification.Certs.html#6375}{\htmlId{1960}{\htmlClass{Field}{\text{DepositsOf}}}}\, \,\htmlId{1971}{\htmlClass{Symbol}{\text{(}}}\,\,\href{Ledger.Dijkstra.Specification.Certs.html#7439}{\htmlId{1972}{\htmlClass{Field}{\text{DStateOf}}}}\, \,\href{Ledger.Dijkstra.Specification.Entities.Properties.Computational.html#1112}{\htmlId{1981}{\htmlClass{Bound}{\text{s₀}}}}\,\,\htmlId{1983}{\htmlClass{Symbol}{\text{)}}}\, \end{pmatrix}
               \\ \,\href{Ledger.Dijkstra.Specification.Certs.html#7547}{\htmlId{2004}{\htmlClass{Field}{\text{PStateOf}}}}\, \,\href{Ledger.Dijkstra.Specification.Entities.Properties.Computational.html#1112}{\htmlId{2013}{\htmlClass{Bound}{\text{s₀}}}}\,
               \\ \begin{pmatrix} \,\href{Ledger.Dijkstra.Specification.Entities.Properties.Computational.html#1330}{\htmlId{2035}{\htmlClass{Bound}{\text{refreshedDReps}}}}\,
                 \\ \,\href{Ledger.Dijkstra.Specification.Certs.html#6492}{\htmlId{2069}{\htmlClass{Field}{\text{CCHotKeysOf}}}}\, \,\href{Ledger.Dijkstra.Specification.Entities.Properties.Computational.html#1112}{\htmlId{2081}{\htmlClass{Bound}{\text{s₀}}}}\,
                 \\ \,\href{Ledger.Dijkstra.Specification.Certs.html#6375}{\htmlId{2103}{\htmlClass{Field}{\text{DepositsOf}}}}\, \,\htmlId{2114}{\htmlClass{Symbol}{\text{(}}}\,\,\href{Ledger.Dijkstra.Specification.Certs.html#7655}{\htmlId{2115}{\htmlClass{Field}{\text{GStateOf}}}}\, \,\href{Ledger.Dijkstra.Specification.Entities.Properties.Computational.html#1112}{\htmlId{2124}{\htmlClass{Bound}{\text{s₀}}}}\,\,\htmlId{2126}{\htmlClass{Symbol}{\text{)}}}\, \end{pmatrix} \end{pmatrix}$

        H : Type
        H =
            ∙ ∀[ a ∈ dom withdrawals ] NetworkIdOf a ≡ NetworkId
            ∙ withdrawalsCredentials ⊆ dom Γ.rewards₀
            ∙ withdrawalsCredentials ⊆ dom (RewardsOf s₀)

            ∙ dom accountBalanceIntervals ⊆ dom (RewardsOf s₀)
            ∙ ∀[ (c , interval) ∈ accountBalanceIntervals ˢ ]
                (InBalanceInterval (maybe id 0 (lookupᵐ? (RewardsOf s₀) c)) interval)

            ∙ ∀[ a ∈ dom directDeposits ] NetworkIdOf a ≡ NetworkId

        H? : Dec H
        H? = ¿ H ¿

        credit : CertState → CertState
        credit s₂ = record s₂
          { dState = record (DStateOf s₂)
            { rewards = applyDirectDeposits directDeposits (RewardsOf s₂) } }

        lift-step : H → ∀ {s₂} → $\begin{pmatrix} \,\href{Ledger.Dijkstra.Specification.Entities.html#888}{\htmlId{2885}{\htmlClass{Function}{\text{Γ.epoch}}}}\, \\ \,\href{Ledger.Dijkstra.Specification.Entities.html#924}{\htmlId{2895}{\htmlClass{Function}{\text{Γ.pp}}}}\, \\ \,\href{Ledger.Dijkstra.Specification.Entities.html#962}{\htmlId{2902}{\htmlClass{Function}{\text{Γ.coldCredentials}}}}\, \end{pmatrix}$ ⊢ s₁ ⇀⦇ DCertsOf txSub ,CERTS⦈ s₂
                  → directDepositsCredentials ⊆ dom (RewardsOf s₂)
                  → Γ ⊢ s₀ ⇀⦇ txSub ,SUBENTITIES⦈ credit s₂
        lift-step (p₁ , p₂ , p₃ , p₄ , p₅ , p₆) h p₇ =
          SUBENTITIES (p₁ , p₂ , p₃ , p₄ , p₅ , h , p₆ , p₇)

        finish : H → (s₂ : CertState) → $\begin{pmatrix} \,\href{Ledger.Dijkstra.Specification.Entities.html#888}{\htmlId{3242}{\htmlClass{Function}{\text{Γ.epoch}}}}\, \\ \,\href{Ledger.Dijkstra.Specification.Entities.html#924}{\htmlId{3252}{\htmlClass{Function}{\text{Γ.pp}}}}\, \\ \,\href{Ledger.Dijkstra.Specification.Entities.html#962}{\htmlId{3259}{\htmlClass{Function}{\text{Γ.coldCredentials}}}}\, \end{pmatrix}$ ⊢ s₁ ⇀⦇ DCertsOf txSub ,CERTS⦈ s₂
               → Dec (directDepositsCredentials ⊆ dom (RewardsOf s₂))
               → ComputationResult String (∃[ s' ] Γ ⊢ s₀ ⇀⦇ txSub ,SUBENTITIES⦈ s')
        finish p s₂ h (no _) = failure ""
        finish p s₂ h (yes q) = success (credit s₂ , lift-step p h q)

        computeProof-aux : Dec H
                         → ComputationResult String (∃[ s₂ ] $\begin{pmatrix} \,\href{Ledger.Dijkstra.Specification.Entities.html#888}{\htmlId{3677}{\htmlClass{Function}{\text{Γ.epoch}}}}\, \\ \,\href{Ledger.Dijkstra.Specification.Entities.html#924}{\htmlId{3687}{\htmlClass{Function}{\text{Γ.pp}}}}\, \\ \,\href{Ledger.Dijkstra.Specification.Entities.html#962}{\htmlId{3694}{\htmlClass{Function}{\text{Γ.coldCredentials}}}}\, \end{pmatrix}$ ⊢ s₁ ⇀⦇ DCertsOf txSub ,CERTS⦈ s₂)
                         → ComputationResult String (∃[ s' ] Γ ⊢ s₀ ⇀⦇ txSub ,SUBENTITIES⦈ s')
        computeProof-aux (no _) _ = failure ""
        computeProof-aux (yes _) (failure e) = failure e
        computeProof-aux (yes p) (success (s₂ , h)) =
          finish p s₂ h ¿ directDepositsCredentials ⊆ dom (RewardsOf s₂) ¿

        computeProof : ComputationResult String (∃[ s' ] Γ ⊢ s₀ ⇀⦇ txSub ,SUBENTITIES⦈ s')
        computeProof = computeProof-aux H?
          (Computational-CERTS.computeProof $\begin{pmatrix} \,\href{Ledger.Dijkstra.Specification.Entities.html#888}{\htmlId{4258}{\htmlClass{Function}{\text{Γ.epoch}}}}\, \\ \,\href{Ledger.Dijkstra.Specification.Entities.html#924}{\htmlId{4268}{\htmlClass{Function}{\text{Γ.pp}}}}\, \\ \,\href{Ledger.Dijkstra.Specification.Entities.html#962}{\htmlId{4275}{\htmlClass{Function}{\text{Γ.coldCredentials}}}}\, \end{pmatrix}$ s₁ (DCertsOf txSub))

        finish-complete : (p : H) (s₂ : CertState) (h : $\begin{pmatrix} \,\href{Ledger.Dijkstra.Specification.Entities.html#888}{\htmlId{4375}{\htmlClass{Function}{\text{Γ.epoch}}}}\, \\ \,\href{Ledger.Dijkstra.Specification.Entities.html#924}{\htmlId{4385}{\htmlClass{Function}{\text{Γ.pp}}}}\, \\ \,\href{Ledger.Dijkstra.Specification.Entities.html#962}{\htmlId{4392}{\htmlClass{Function}{\text{Γ.coldCredentials}}}}\, \end{pmatrix}$ ⊢ s₁ ⇀⦇ DCertsOf txSub ,CERTS⦈ s₂)
                         → (d : Dec (directDepositsCredentials ⊆ dom (RewardsOf s₂)))
                         → directDepositsCredentials ⊆ dom (RewardsOf s₂)
                         → map proj₁ (finish p s₂ h d) ≡ success (credit s₂)
        finish-complete p s₂ h (no ¬q) q = ⊥-elim (¬q q)
        finish-complete p s₂ h (yes _) _ = refl

        completeness-aux : (d : Dec H)
                         → (r : ComputationResult String (∃[ s₂ ] $\begin{pmatrix} \,\href{Ledger.Dijkstra.Specification.Entities.html#888}{\htmlId{4897}{\htmlClass{Function}{\text{Γ.epoch}}}}\, \\ \,\href{Ledger.Dijkstra.Specification.Entities.html#924}{\htmlId{4907}{\htmlClass{Function}{\text{Γ.pp}}}}\, \\ \,\href{Ledger.Dijkstra.Specification.Entities.html#962}{\htmlId{4914}{\htmlClass{Function}{\text{Γ.coldCredentials}}}}\, \end{pmatrix}$ ⊢ s₁ ⇀⦇ DCertsOf txSub ,CERTS⦈ s₂))
                         → {s₂ : CertState} → H → $\begin{pmatrix} \,\href{Ledger.Dijkstra.Specification.Entities.html#888}{\htmlId{5022}{\htmlClass{Function}{\text{Γ.epoch}}}}\, \\ \,\href{Ledger.Dijkstra.Specification.Entities.html#924}{\htmlId{5032}{\htmlClass{Function}{\text{Γ.pp}}}}\, \\ \,\href{Ledger.Dijkstra.Specification.Entities.html#962}{\htmlId{5039}{\htmlClass{Function}{\text{Γ.coldCredentials}}}}\, \end{pmatrix}$ ⊢ s₁ ⇀⦇ DCertsOf txSub ,CERTS⦈ s₂
                         → directDepositsCredentials ⊆ dom (RewardsOf s₂)
                         → map proj₁ r ≡ success s₂
                         → map proj₁ (computeProof-aux d r) ≡ success (credit s₂)
        completeness-aux (no ¬p) _ p _ _ _ = ⊥-elim (¬p p)
        completeness-aux (yes _) (failure _) _ _ _ ()
        completeness-aux (yes p) (success (s₂ , h)) _ _ q refl =
          finish-complete p s₂ h ¿ directDepositsCredentials ⊆ dom (RewardsOf s₂) ¿ q

        completeness : ∀ s' → Γ ⊢ s₀ ⇀⦇ txSub ,SUBENTITIES⦈ s' → map proj₁ computeProof ≡ success s'
        completeness s' (SUBENTITIES (p₁ , p₂ , p₃ , p₄ , p₅ , h , p₆ , p₇)) =
          completeness-aux H?
            (Computational-CERTS.computeProof $\begin{pmatrix} \,\href{Ledger.Dijkstra.Specification.Entities.html#888}{\htmlId{5824}{\htmlClass{Function}{\text{Γ.epoch}}}}\, \\ \,\href{Ledger.Dijkstra.Specification.Entities.html#924}{\htmlId{5834}{\htmlClass{Function}{\text{Γ.pp}}}}\, \\ \,\href{Ledger.Dijkstra.Specification.Entities.html#962}{\htmlId{5841}{\htmlClass{Function}{\text{Γ.coldCredentials}}}}\, \end{pmatrix}$ s₁ (DCertsOf txSub))
            (p₁ , p₂ , p₃ , p₄ , p₅ , p₆) h p₇ (Computational-CERTS.completeness _ _ _ _ h)

  Computational-ENTITIES : Computational _⊢_⇀⦇_,ENTITIES⦈_ String
  Computational-ENTITIES = record {go}
    where
      module go (Γ : EntitiesEnv) (s₀ : CertState) (txTop : TopLevelTx)
                (let module Γ = EntitiesEnv Γ
                     refresh         = mapPartial (isGovVoterDRep ∘ voter) (fromList (ListOfGovVotesOf txTop))
                     refreshedDReps  = mapValueRestricted (const (Γ.epoch  + Γ.pp .PParams.drepActivity)) (DRepsOf s₀) refresh

                     withdrawalsSubTxs          = foldl (λ acc txSub → acc ∪⁺ WithdrawalsOf txSub) ∅ (SubTransactionsOf txTop)
                     withdrawals               = WithdrawalsOf txTop
                     withdrawalsCredentials    = mapˢ stake (dom withdrawals)
                     accountBalanceIntervals   = BalanceIntervalsOf txTop
                     startingAccountBalanceIntervals = StartingBalanceIntervalsOf txTop
                     directDeposits            = DirectDepositsOf txTop
                     directDepositsCredentials = mapˢ stake (dom directDeposits)

                ) where

        s₁ : CertState
        s₁ = $\begin{pmatrix} \begin{pmatrix} \,\href{Ledger.Dijkstra.Specification.Gov.Actions.html#4773}{\htmlId{7110}{\htmlClass{Field}{\text{VoteDelegsOf}}}}\, \,\href{Ledger.Dijkstra.Specification.Entities.Properties.Computational.html#6125}{\htmlId{7123}{\htmlClass{Bound}{\text{s₀}}}}\, \\ \,\href{Ledger.Dijkstra.Specification.Certs.html#7315}{\htmlId{7128}{\htmlClass{Field}{\text{StakeDelegsOf}}}}\, \,\href{Ledger.Dijkstra.Specification.Entities.Properties.Computational.html#6125}{\htmlId{7142}{\htmlClass{Bound}{\text{s₀}}}}\, \\ \,\href{Ledger.Dijkstra.Specification.Entities.html#2912}{\htmlId{7147}{\htmlClass{Function}{\text{applyWithdrawals}}}}\, \,\href{Ledger.Dijkstra.Specification.Entities.Properties.Computational.html#6595}{\htmlId{7164}{\htmlClass{Bound}{\text{withdrawals}}}}\, \,\htmlId{7176}{\htmlClass{Symbol}{\text{(}}}\,\,\href{Ledger.Dijkstra.Specification.Certs.html#7094}{\htmlId{7177}{\htmlClass{Field}{\text{RewardsOf}}}}\, \,\href{Ledger.Dijkstra.Specification.Entities.Properties.Computational.html#6125}{\htmlId{7187}{\htmlClass{Bound}{\text{s₀}}}}\,\,\htmlId{7189}{\htmlClass{Symbol}{\text{)}}}\, \\ \,\href{Ledger.Dijkstra.Specification.Certs.html#6375}{\htmlId{7193}{\htmlClass{Field}{\text{DepositsOf}}}}\, \,\htmlId{7204}{\htmlClass{Symbol}{\text{(}}}\,\,\href{Ledger.Dijkstra.Specification.Certs.html#7439}{\htmlId{7205}{\htmlClass{Field}{\text{DStateOf}}}}\, \,\href{Ledger.Dijkstra.Specification.Entities.Properties.Computational.html#6125}{\htmlId{7214}{\htmlClass{Bound}{\text{s₀}}}}\,\,\htmlId{7216}{\htmlClass{Symbol}{\text{)}}}\, \end{pmatrix}
               \\ \,\href{Ledger.Dijkstra.Specification.Certs.html#7547}{\htmlId{7237}{\htmlClass{Field}{\text{PStateOf}}}}\, \,\href{Ledger.Dijkstra.Specification.Entities.Properties.Computational.html#6125}{\htmlId{7246}{\htmlClass{Bound}{\text{s₀}}}}\,
               \\ \begin{pmatrix} \,\href{Ledger.Dijkstra.Specification.Entities.Properties.Computational.html#6340}{\htmlId{7268}{\htmlClass{Bound}{\text{refreshedDReps}}}}\,
                 \\ \,\href{Ledger.Dijkstra.Specification.Certs.html#6492}{\htmlId{7302}{\htmlClass{Field}{\text{CCHotKeysOf}}}}\, \,\href{Ledger.Dijkstra.Specification.Entities.Properties.Computational.html#6125}{\htmlId{7314}{\htmlClass{Bound}{\text{s₀}}}}\,
                 \\ \,\href{Ledger.Dijkstra.Specification.Certs.html#6375}{\htmlId{7336}{\htmlClass{Field}{\text{DepositsOf}}}}\, \,\htmlId{7347}{\htmlClass{Symbol}{\text{(}}}\,\,\href{Ledger.Dijkstra.Specification.Certs.html#7655}{\htmlId{7348}{\htmlClass{Field}{\text{GStateOf}}}}\, \,\href{Ledger.Dijkstra.Specification.Entities.Properties.Computational.html#6125}{\htmlId{7357}{\htmlClass{Bound}{\text{s₀}}}}\,\,\htmlId{7359}{\htmlClass{Symbol}{\text{)}}}\, \end{pmatrix} \end{pmatrix}$

        H : Type
        H =
            ∙ ∀[ a ∈ dom withdrawals ] NetworkIdOf a ≡ NetworkId
            ∙ withdrawalsCredentials ⊆ dom (RewardsOf s₀)

            ∙ (Γ.legacyMode ≡ true →
                ∙ ∀[ (addr , amt) ∈ withdrawals ˢ ] amt ≡ maybe id 0 (lookupᵐ? (RewardsOf s₀) (stake addr))
                ∙ ∀[ (addr , amt) ∈ withdrawalsSubTxs ˢ ] amt ≤ maybe id 0 (lookupᵐ? Γ.rewards₀ (stake addr)))

            ∙ (Γ.legacyMode ≡ false →
                ∙ withdrawalsCredentials ⊆ dom Γ.rewards₀
                ∙ ∀[ (addr , amt) ∈ (withdrawalsSubTxs ∪⁺ withdrawals) ˢ ] amt ≤ maybe id 0 (lookupᵐ? Γ.rewards₀ (stake addr)))

            ∙ dom accountBalanceIntervals ⊆ dom (RewardsOf s₀)
            ∙ ∀[ (c , interval) ∈ accountBalanceIntervals ˢ ]
                (InBalanceInterval (maybe id 0 (lookupᵐ? (RewardsOf s₀) c)) interval)

            ∙ dom startingAccountBalanceIntervals ⊆ dom Γ.rewards₀
            ∙ ∀[ (c , interval) ∈ startingAccountBalanceIntervals ˢ ]
                (InBalanceInterval (maybe id 0 (lookupᵐ? Γ.rewards₀ c)) interval)

            ∙ ∀[ a ∈ dom directDeposits ] NetworkIdOf a ≡ NetworkId

        H? : Dec H
        H? = ¿ H ¿

        credit : CertState → CertState
        credit s₂ = record s₂
          { dState = record (DStateOf s₂)
            { rewards = applyDirectDeposits directDeposits (RewardsOf s₂) } }

        lift-step : H → ∀ {s₂} → $\begin{pmatrix} \,\href{Ledger.Dijkstra.Specification.Entities.html#654}{\htmlId{8766}{\htmlClass{Function}{\text{Γ.epoch}}}}\, \\ \,\href{Ledger.Dijkstra.Specification.Entities.html#690}{\htmlId{8776}{\htmlClass{Function}{\text{Γ.pp}}}}\, \\ \,\href{Ledger.Dijkstra.Specification.Entities.html#728}{\htmlId{8783}{\htmlClass{Function}{\text{Γ.coldCredentials}}}}\, \end{pmatrix}$ ⊢ s₁ ⇀⦇ DCertsOf txTop ,CERTS⦈ s₂
                  → directDepositsCredentials ⊆ dom (RewardsOf s₂)
                  → Γ ⊢ s₀ ⇀⦇ txTop ,ENTITIES⦈ credit s₂
        lift-step (p₁ , p₂ , p₃ , p₄ , p₅ , p₆ , p₇ , p₈ , p₉) h p₁₀ =
          ENTITIES (p₁ , p₂ , p₃ , p₄ , p₅ , p₆ , p₇ , p₈ , h , p₉ , p₁₀)

        finish : H → (s₂ : CertState) → $\begin{pmatrix} \,\href{Ledger.Dijkstra.Specification.Entities.html#654}{\htmlId{9149}{\htmlClass{Function}{\text{Γ.epoch}}}}\, \\ \,\href{Ledger.Dijkstra.Specification.Entities.html#690}{\htmlId{9159}{\htmlClass{Function}{\text{Γ.pp}}}}\, \\ \,\href{Ledger.Dijkstra.Specification.Entities.html#728}{\htmlId{9166}{\htmlClass{Function}{\text{Γ.coldCredentials}}}}\, \end{pmatrix}$ ⊢ s₁ ⇀⦇ DCertsOf txTop ,CERTS⦈ s₂
               → Dec (directDepositsCredentials ⊆ dom (RewardsOf s₂))
               → ComputationResult String (∃[ s' ] Γ ⊢ s₀ ⇀⦇ txTop ,ENTITIES⦈ s')
        finish p s₂ h (no _) = failure ""
        finish p s₂ h (yes q) = success (credit s₂ , lift-step p h q)

        computeProof-aux : Dec H
                         → ComputationResult String (∃[ s₂ ] $\begin{pmatrix} \,\href{Ledger.Dijkstra.Specification.Entities.html#654}{\htmlId{9581}{\htmlClass{Function}{\text{Γ.epoch}}}}\, \\ \,\href{Ledger.Dijkstra.Specification.Entities.html#690}{\htmlId{9591}{\htmlClass{Function}{\text{Γ.pp}}}}\, \\ \,\href{Ledger.Dijkstra.Specification.Entities.html#728}{\htmlId{9598}{\htmlClass{Function}{\text{Γ.coldCredentials}}}}\, \end{pmatrix}$ ⊢ s₁ ⇀⦇ DCertsOf txTop ,CERTS⦈ s₂)
                         → ComputationResult String (∃[ s' ] Γ ⊢ s₀ ⇀⦇ txTop ,ENTITIES⦈ s')
        computeProof-aux (no _) _ = failure ""
        computeProof-aux (yes _) (failure e) = failure e
        computeProof-aux (yes p) (success (s₂ , h)) =
          finish p s₂ h ¿ directDepositsCredentials ⊆ dom (RewardsOf s₂) ¿

        computeProof : ComputationResult String (∃[ s' ] Γ ⊢ s₀ ⇀⦇ txTop ,ENTITIES⦈ s')
        computeProof = computeProof-aux H?
          (Computational-CERTS.computeProof $\begin{pmatrix} \,\href{Ledger.Dijkstra.Specification.Entities.html#654}{\htmlId{10156}{\htmlClass{Function}{\text{Γ.epoch}}}}\, \\ \,\href{Ledger.Dijkstra.Specification.Entities.html#690}{\htmlId{10166}{\htmlClass{Function}{\text{Γ.pp}}}}\, \\ \,\href{Ledger.Dijkstra.Specification.Entities.html#728}{\htmlId{10173}{\htmlClass{Function}{\text{Γ.coldCredentials}}}}\, \end{pmatrix}$ s₁ (DCertsOf txTop))

        finish-complete : (p : H) (s₂ : CertState) (h : $\begin{pmatrix} \,\href{Ledger.Dijkstra.Specification.Entities.html#654}{\htmlId{10273}{\htmlClass{Function}{\text{Γ.epoch}}}}\, \\ \,\href{Ledger.Dijkstra.Specification.Entities.html#690}{\htmlId{10283}{\htmlClass{Function}{\text{Γ.pp}}}}\, \\ \,\href{Ledger.Dijkstra.Specification.Entities.html#728}{\htmlId{10290}{\htmlClass{Function}{\text{Γ.coldCredentials}}}}\, \end{pmatrix}$ ⊢ s₁ ⇀⦇ DCertsOf txTop ,CERTS⦈ s₂)
                         → (d : Dec (directDepositsCredentials ⊆ dom (RewardsOf s₂)))
                         → directDepositsCredentials ⊆ dom (RewardsOf s₂)
                         → map proj₁ (finish p s₂ h d) ≡ success (credit s₂)
        finish-complete p s₂ h (no ¬q) q = ⊥-elim (¬q q)
        finish-complete p s₂ h (yes _) _ = refl

        completeness-aux : (d : Dec H)
                         → (r : ComputationResult String (∃[ s₂ ] $\begin{pmatrix} \,\href{Ledger.Dijkstra.Specification.Entities.html#654}{\htmlId{10795}{\htmlClass{Function}{\text{Γ.epoch}}}}\, \\ \,\href{Ledger.Dijkstra.Specification.Entities.html#690}{\htmlId{10805}{\htmlClass{Function}{\text{Γ.pp}}}}\, \\ \,\href{Ledger.Dijkstra.Specification.Entities.html#728}{\htmlId{10812}{\htmlClass{Function}{\text{Γ.coldCredentials}}}}\, \end{pmatrix}$ ⊢ s₁ ⇀⦇ DCertsOf txTop ,CERTS⦈ s₂))
                         → {s₂ : CertState} → H → $\begin{pmatrix} \,\href{Ledger.Dijkstra.Specification.Entities.html#654}{\htmlId{10920}{\htmlClass{Function}{\text{Γ.epoch}}}}\, \\ \,\href{Ledger.Dijkstra.Specification.Entities.html#690}{\htmlId{10930}{\htmlClass{Function}{\text{Γ.pp}}}}\, \\ \,\href{Ledger.Dijkstra.Specification.Entities.html#728}{\htmlId{10937}{\htmlClass{Function}{\text{Γ.coldCredentials}}}}\, \end{pmatrix}$ ⊢ s₁ ⇀⦇ DCertsOf txTop ,CERTS⦈ s₂
                         → directDepositsCredentials ⊆ dom (RewardsOf s₂)
                         → map proj₁ r ≡ success s₂
                         → map proj₁ (computeProof-aux d r) ≡ success (credit s₂)
        completeness-aux (no ¬p) _ p _ _ _ = ⊥-elim (¬p p)
        completeness-aux (yes _) (failure _) _ _ _ ()
        completeness-aux (yes p) (success (s₂ , h)) _ _ q refl =
          finish-complete p s₂ h ¿ directDepositsCredentials ⊆ dom (RewardsOf s₂) ¿ q

        completeness : ∀ s' → Γ ⊢ s₀ ⇀⦇ txTop ,ENTITIES⦈ s' → map proj₁ computeProof ≡ success s'
        completeness s' (ENTITIES (p₁ , p₂ , p₃ , p₄ , p₅ , p₆ , p₇ , p₈ , h , p₉ , p₁₀)) =
          completeness-aux H?
            (Computational-CERTS.computeProof $\begin{pmatrix} \,\href{Ledger.Dijkstra.Specification.Entities.html#654}{\htmlId{11732}{\htmlClass{Function}{\text{Γ.epoch}}}}\, \\ \,\href{Ledger.Dijkstra.Specification.Entities.html#690}{\htmlId{11742}{\htmlClass{Function}{\text{Γ.pp}}}}\, \\ \,\href{Ledger.Dijkstra.Specification.Entities.html#728}{\htmlId{11749}{\htmlClass{Function}{\text{Γ.coldCredentials}}}}\, \end{pmatrix}$ s₁ (DCertsOf txTop))
            (p₁ , p₂ , p₃ , p₄ , p₅ , p₆ , p₇ , p₈ , p₉) h p₁₀ (Computational-CERTS.completeness _ _ _ _ h)