Skip to content

Entities

{-# OPTIONS --safe #-}

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

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

open import Ledger.Prelude renaming (filterˢ to filter)
open import Ledger.Prelude.Numeric.UnitInterval
open import Ledger.Dijkstra.Specification.Certs govStructure
open RewardAddress
open PParams

Auxiliary Types and Functions

record EntitiesEnv : Type where
  field
    epoch                   : Epoch
    pp                      : PParams
    coldCredentials         :  Credential
    legacyMode              : Bool
    rewards₀                : Rewards

record SubEntitiesEnv : Type where
  field
    epoch                   : Epoch
    pp                      : PParams
    coldCredentials         :  Credential
    rewards₀                : Rewards
unquoteDecl HasCast-EntitiesEnv HasCast-SubEntitiesEnv = derive-HasCast
  ( (quote SubEntitiesEnv , HasCast-SubEntitiesEnv) 
  [ (quote EntitiesEnv , HasCast-EntitiesEnv) ])

instance
  EntitiesEnv-HasEpoch : HasEpoch EntitiesEnv
  EntitiesEnv-HasEpoch .EpochOf = EntitiesEnv.epoch

  EntitiesEnv-HasColdCredentials : HasColdCredentials EntitiesEnv
  EntitiesEnv-HasColdCredentials .ColdCredentialsOf = EntitiesEnv.coldCredentials

  EntitiesEnv-HasPParams : HasPParams EntitiesEnv
  EntitiesEnv-HasPParams .PParamsOf = EntitiesEnv.pp

Since it underpins both applyDirectDeposits and applyWithdrawals, the applyToRewards function bears explaining. Given three arguments — a binary function f (e.g., addition or subtraction), a map m from RewardAddress to Coin (e.g., direct deposits or withdrawals), and a Rewards map (of account balances) — for each (addr , amt), the applyToRewards function does the following:

  1. Look up stake addr in the accumulator.
  2. If found with current balance bal, replace the entry with (stake addr, f bal amt). Note. since ∪ˡ is left-biased, the fresh singleton wins at stake addr and all other entries of acc are preserved; no explicit complement restriction is needed.
  3. If not found (defensive; the caller's precondition will rule this out), return acc unchanged; this keeps applyToRewards total.
applyToRewards : (Coin  Coin  Coin)  (RewardAddress  Coin)  Rewards  Rewards
applyToRewards f m rwds =
  foldl  acc (addr , amt)  maybe  bal   stake addr , f bal amt  ∪ˡ acc) acc (lookupᵐ? acc (stake addr)))
        rwds
        (setToList (m ˢ))

applyDirectDeposits : DirectDeposits  Rewards  Rewards
applyDirectDeposits = applyToRewards _+_

applyWithdrawals : Withdrawals  Rewards  Rewards
applyWithdrawals = applyToRewards _∸_

ENTITIES Transition System

In Dijkstra, the new ENTITIES rule subsumes the pre-Dijkstra CERTS rule. This rule in addition to processing certificates via CERTS, processes withdrawals, direct deposits, and account balance intervals.

CIP-159 introduces two new fields to transactions: directDeposits and balanceIntervals. Direct deposits represent value that flows from the transaction into account addresses. Balance intervals enable transactions to assert predicates about account balances.

Withdrawals

The ENTITIES rule applies withdrawals, via applyWithdrawals before certificate evaluation. In the Dijkstra era, withdrawals can be partial, unless in legacy mode. Whether legacy mode applies is decided in the LEDGER rule (via isLegacyMode, defined in the Utxow module) and provided here through the legacyMode field of EntitiesEnv.

Direct Deposits

The ENTITIES rule applies direct deposits to the CertState after CERTS.

open GovVote using (voter)

private variable
  txTop : TopLevelTx
  txSub : SubLevelTx
  rewards rewards' : Rewards
  dReps : DReps
  stakeDelegs stakeDelegs' : StakeDelegs
  ccHotKeys : CCHotKeys
  voteDelegs voteDelegs' : VoteDelegs
  rewards₀ : Rewards
  depositsᵍ depositsᵈ depositsᵈ' : Credential  Coin
  legacyMode : Bool

  e : Epoch
  pp : PParams
  cc :  Credential

  gState' : GState
  pState pState' : PState
data _⊢_⇀⦇_,SUBENTITIES⦈_ : SubEntitiesEnv  CertState  SubLevelTx  CertState  Type where

  SUBENTITIES :
    let refresh         = mapPartial (isGovVoterDRep  voter) (fromList (ListOfGovVotesOf txSub))
        refreshedDReps  = mapValueRestricted (const (e + pp .drepActivity)) dReps refresh
        activeVDelegs   = mapˢ vDelegCredential (dom (DRepsOf gState'))
                                fromList (vDelegNoConfidence  vDelegAbstain  [])

        withdrawals               = WithdrawalsOf txSub
        withdrawalsCredentials    = mapˢ stake (dom withdrawals)
        accountBalanceIntervals   = BalanceIntervalsOf txSub
        directDeposits            = DirectDepositsOf txSub
        directDepositsCredentials = mapˢ stake (dom directDeposits)
    in
     ∀[ a  dom withdrawals ] NetworkIdOf a  NetworkId
     withdrawalsCredentials  dom rewards₀
     withdrawalsCredentials  dom rewards

     dom accountBalanceIntervals  dom rewards
     ∀[ (c , interval)  accountBalanceIntervals ˢ ]
        (InBalanceInterval (maybe id 0 (lookupᵐ? rewards c)) interval)

     $\begin{pmatrix} \,\href{Ledger.Dijkstra.Specification.Entities.html#4523}{\htmlId{5729}{\htmlClass{Generalizable}{\text{e}}}}\, \\ \,\href{Ledger.Dijkstra.Specification.Entities.html#4535}{\htmlId{5733}{\htmlClass{Generalizable}{\text{pp}}}}\, \\ \,\href{Ledger.Dijkstra.Specification.Entities.html#4550}{\htmlId{5738}{\htmlClass{Generalizable}{\text{cc}}}}\, \end{pmatrix}$  $\begin{pmatrix} \begin{pmatrix} \,\href{Ledger.Dijkstra.Specification.Entities.html#4390}{\htmlId{5749}{\htmlClass{Generalizable}{\text{voteDelegs}}}}\, \\ \,\href{Ledger.Dijkstra.Specification.Entities.html#4325}{\htmlId{5762}{\htmlClass{Generalizable}{\text{stakeDelegs}}}}\, \\ \,\href{Ledger.Dijkstra.Specification.Entities.html#2911}{\htmlId{5776}{\htmlClass{Function}{\text{applyWithdrawals}}}}\, \,\href{Ledger.Dijkstra.Specification.Entities.html#5094}{\htmlId{5793}{\htmlClass{Bound}{\text{withdrawals}}}}\, \,\href{Ledger.Dijkstra.Specification.Entities.html#4280}{\htmlId{5805}{\htmlClass{Generalizable}{\text{rewards}}}}\, \\ \,\href{Ledger.Dijkstra.Specification.Entities.html#4459}{\htmlId{5815}{\htmlClass{Generalizable}{\text{depositsᵈ}}}}\, \end{pmatrix} \\ \,\href{Ledger.Dijkstra.Specification.Entities.html#4590}{\htmlId{5829}{\htmlClass{Generalizable}{\text{pState}}}}\, \\ \begin{pmatrix} \,\href{Ledger.Dijkstra.Specification.Entities.html#4847}{\htmlId{5840}{\htmlClass{Bound}{\text{refreshedDReps}}}}\, \\ \,\href{Ledger.Dijkstra.Specification.Entities.html#4366}{\htmlId{5857}{\htmlClass{Generalizable}{\text{ccHotKeys}}}}\, \\ \,\href{Ledger.Dijkstra.Specification.Entities.html#4449}{\htmlId{5869}{\htmlClass{Generalizable}{\text{depositsᵍ}}}}\, \end{pmatrix} \end{pmatrix}$ ⇀⦇ DCertsOf txSub  ,CERTS⦈ $\begin{pmatrix} \begin{pmatrix} \,\href{Ledger.Dijkstra.Specification.Entities.html#4401}{\htmlId{5914}{\htmlClass{Generalizable}{\text{voteDelegs'}}}}\, \\ \,\href{Ledger.Dijkstra.Specification.Entities.html#4337}{\htmlId{5928}{\htmlClass{Generalizable}{\text{stakeDelegs'}}}}\, \\ \,\href{Ledger.Dijkstra.Specification.Entities.html#4288}{\htmlId{5943}{\htmlClass{Generalizable}{\text{rewards'}}}}\, \\ \,\href{Ledger.Dijkstra.Specification.Entities.html#4469}{\htmlId{5954}{\htmlClass{Generalizable}{\text{depositsᵈ'}}}}\, \end{pmatrix} \\ \,\href{Ledger.Dijkstra.Specification.Entities.html#4597}{\htmlId{5969}{\htmlClass{Generalizable}{\text{pState'}}}}\, \\ \,\href{Ledger.Dijkstra.Specification.Entities.html#4571}{\htmlId{5979}{\htmlClass{Generalizable}{\text{gState'}}}}\, \end{pmatrix}$

     ∀[ a  dom directDeposits ] NetworkIdOf a  NetworkId
     directDepositsCredentials  dom rewards'
      ────────────────────────────────
      $\begin{pmatrix} \,\href{Ledger.Dijkstra.Specification.Entities.html#4523}{\htmlId{6144}{\htmlClass{Generalizable}{\text{e}}}}\, \\ \,\href{Ledger.Dijkstra.Specification.Entities.html#4535}{\htmlId{6148}{\htmlClass{Generalizable}{\text{pp}}}}\, \\ \,\href{Ledger.Dijkstra.Specification.Entities.html#4550}{\htmlId{6153}{\htmlClass{Generalizable}{\text{cc}}}}\, \\ \,\href{Ledger.Dijkstra.Specification.Entities.html#4428}{\htmlId{6158}{\htmlClass{Generalizable}{\text{rewards₀}}}}\, \end{pmatrix}$  $\begin{pmatrix} \begin{pmatrix} \,\href{Ledger.Dijkstra.Specification.Entities.html#4390}{\htmlId{6175}{\htmlClass{Generalizable}{\text{voteDelegs}}}}\, \\ \,\href{Ledger.Dijkstra.Specification.Entities.html#4325}{\htmlId{6188}{\htmlClass{Generalizable}{\text{stakeDelegs}}}}\, \\ \,\href{Ledger.Dijkstra.Specification.Entities.html#4280}{\htmlId{6202}{\htmlClass{Generalizable}{\text{rewards}}}}\, \\ \,\href{Ledger.Dijkstra.Specification.Entities.html#4459}{\htmlId{6212}{\htmlClass{Generalizable}{\text{depositsᵈ}}}}\, \end{pmatrix} \\ \,\href{Ledger.Dijkstra.Specification.Entities.html#4590}{\htmlId{6226}{\htmlClass{Generalizable}{\text{pState}}}}\, \\ \begin{pmatrix} \,\href{Ledger.Dijkstra.Specification.Entities.html#4309}{\htmlId{6237}{\htmlClass{Generalizable}{\text{dReps}}}}\, \\ \,\href{Ledger.Dijkstra.Specification.Entities.html#4366}{\htmlId{6245}{\htmlClass{Generalizable}{\text{ccHotKeys}}}}\, \\ \,\href{Ledger.Dijkstra.Specification.Entities.html#4449}{\htmlId{6257}{\htmlClass{Generalizable}{\text{depositsᵍ}}}}\, \end{pmatrix} \end{pmatrix}$ ⇀⦇ txSub ,SUBENTITIES⦈ $\begin{pmatrix} \begin{pmatrix} \,\href{Ledger.Dijkstra.Specification.Entities.html#4401}{\htmlId{6298}{\htmlClass{Generalizable}{\text{voteDelegs'}}}}\, \,\href{Axiom.Set.Map.html#17775}{\htmlId{6310}{\htmlClass{Function Operator}{\text{∣\^{}}}}}\, \,\href{Ledger.Dijkstra.Specification.Entities.html#4937}{\htmlId{6313}{\htmlClass{Bound}{\text{activeVDelegs}}}}\, \\ \,\href{Ledger.Dijkstra.Specification.Entities.html#4337}{\htmlId{6329}{\htmlClass{Generalizable}{\text{stakeDelegs'}}}}\, \\ \,\href{Ledger.Dijkstra.Specification.Entities.html#2812}{\htmlId{6344}{\htmlClass{Function}{\text{applyDirectDeposits}}}}\, \,\href{Ledger.Dijkstra.Specification.Entities.html#5276}{\htmlId{6364}{\htmlClass{Bound}{\text{directDeposits}}}}\, \,\href{Ledger.Dijkstra.Specification.Entities.html#4288}{\htmlId{6379}{\htmlClass{Generalizable}{\text{rewards'}}}}\, \\ \,\href{Ledger.Dijkstra.Specification.Entities.html#4469}{\htmlId{6390}{\htmlClass{Generalizable}{\text{depositsᵈ'}}}}\, \end{pmatrix} \\ \,\href{Ledger.Dijkstra.Specification.Entities.html#4597}{\htmlId{6405}{\htmlClass{Generalizable}{\text{pState'}}}}\, \\ \,\href{Ledger.Dijkstra.Specification.Entities.html#4571}{\htmlId{6415}{\htmlClass{Generalizable}{\text{gState'}}}}\, \end{pmatrix}$
data _⊢_⇀⦇_,ENTITIES⦈_ : EntitiesEnv  CertState  TopLevelTx  CertState  Type where

  ENTITIES :
    let refresh         = mapPartial (isGovVoterDRep  voter) (fromList (ListOfGovVotesOf txTop))
        refreshedDReps  = mapValueRestricted (const (e + pp .drepActivity)) dReps refresh
        activeVDelegs   = mapˢ vDelegCredential (dom (DRepsOf gState'))
                                fromList (vDelegNoConfidence  vDelegAbstain  [])

        withdrawalsSubTxs          = foldl  acc txSub  acc ∪⁺ WithdrawalsOf txSub)  (SubTransactionsOf txTop)
        withdrawals                = WithdrawalsOf txTop
        withdrawalsCredentials     = mapˢ stake (dom withdrawals)
        accountBalanceIntervals    = BalanceIntervalsOf txTop
        directDeposits             = DirectDepositsOf txTop
        directDepositsCredentials  = mapˢ stake (dom directDeposits)
    in

     ∀[ a  dom withdrawals ] NetworkIdOf a  NetworkId
     withdrawalsCredentials  dom rewards

     (legacyMode  true 
         ∀[ (addr , amt)  withdrawals ˢ ] amt  maybe id 0 (lookupᵐ? rewards (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 rewards
     ∀[ (c , interval)  accountBalanceIntervals ˢ ]
        (InBalanceInterval (maybe id 0 (lookupᵐ? rewards c)) interval)

     $\begin{pmatrix} \,\href{Ledger.Dijkstra.Specification.Entities.html#4523}{\htmlId{8020}{\htmlClass{Generalizable}{\text{e}}}}\, \\ \,\href{Ledger.Dijkstra.Specification.Entities.html#4535}{\htmlId{8024}{\htmlClass{Generalizable}{\text{pp}}}}\, \\ \,\href{Ledger.Dijkstra.Specification.Entities.html#4550}{\htmlId{8029}{\htmlClass{Generalizable}{\text{cc}}}}\, \end{pmatrix}$  $\begin{pmatrix} \begin{pmatrix} \,\href{Ledger.Dijkstra.Specification.Entities.html#4390}{\htmlId{8040}{\htmlClass{Generalizable}{\text{voteDelegs}}}}\, \\ \,\href{Ledger.Dijkstra.Specification.Entities.html#4325}{\htmlId{8053}{\htmlClass{Generalizable}{\text{stakeDelegs}}}}\, \\ \,\href{Ledger.Dijkstra.Specification.Entities.html#2911}{\htmlId{8067}{\htmlClass{Function}{\text{applyWithdrawals}}}}\, \,\href{Ledger.Dijkstra.Specification.Entities.html#7006}{\htmlId{8084}{\htmlClass{Bound}{\text{withdrawals}}}}\, \,\href{Ledger.Dijkstra.Specification.Entities.html#4280}{\htmlId{8096}{\htmlClass{Generalizable}{\text{rewards}}}}\, \\ \,\href{Ledger.Dijkstra.Specification.Entities.html#4459}{\htmlId{8106}{\htmlClass{Generalizable}{\text{depositsᵈ}}}}\, \end{pmatrix} \\ \,\href{Ledger.Dijkstra.Specification.Entities.html#4590}{\htmlId{8120}{\htmlClass{Generalizable}{\text{pState}}}}\, \\ \begin{pmatrix} \,\href{Ledger.Dijkstra.Specification.Entities.html#6645}{\htmlId{8131}{\htmlClass{Bound}{\text{refreshedDReps}}}}\, \\ \,\href{Ledger.Dijkstra.Specification.Entities.html#4366}{\htmlId{8148}{\htmlClass{Generalizable}{\text{ccHotKeys}}}}\, \\ \,\href{Ledger.Dijkstra.Specification.Entities.html#4449}{\htmlId{8160}{\htmlClass{Generalizable}{\text{depositsᵍ}}}}\, \end{pmatrix} \end{pmatrix}$ ⇀⦇ DCertsOf txTop  ,CERTS⦈ $\begin{pmatrix} \begin{pmatrix} \,\href{Ledger.Dijkstra.Specification.Entities.html#4401}{\htmlId{8205}{\htmlClass{Generalizable}{\text{voteDelegs'}}}}\, \\ \,\href{Ledger.Dijkstra.Specification.Entities.html#4337}{\htmlId{8219}{\htmlClass{Generalizable}{\text{stakeDelegs'}}}}\, \\ \,\href{Ledger.Dijkstra.Specification.Entities.html#4288}{\htmlId{8234}{\htmlClass{Generalizable}{\text{rewards'}}}}\, \\ \,\href{Ledger.Dijkstra.Specification.Entities.html#4469}{\htmlId{8245}{\htmlClass{Generalizable}{\text{depositsᵈ'}}}}\, \end{pmatrix} \\ \,\href{Ledger.Dijkstra.Specification.Entities.html#4597}{\htmlId{8260}{\htmlClass{Generalizable}{\text{pState'}}}}\, \\ \,\href{Ledger.Dijkstra.Specification.Entities.html#4571}{\htmlId{8270}{\htmlClass{Generalizable}{\text{gState'}}}}\, \end{pmatrix}$

     ∀[ a  dom directDeposits ] NetworkIdOf a  NetworkId
     directDepositsCredentials  dom rewards'
      ────────────────────────────────
      $\begin{pmatrix} \,\href{Ledger.Dijkstra.Specification.Entities.html#4523}{\htmlId{8435}{\htmlClass{Generalizable}{\text{e}}}}\, \\ \,\href{Ledger.Dijkstra.Specification.Entities.html#4535}{\htmlId{8439}{\htmlClass{Generalizable}{\text{pp}}}}\, \\ \,\href{Ledger.Dijkstra.Specification.Entities.html#4550}{\htmlId{8444}{\htmlClass{Generalizable}{\text{cc}}}}\, \\ \,\href{Ledger.Dijkstra.Specification.Entities.html#4502}{\htmlId{8449}{\htmlClass{Generalizable}{\text{legacyMode}}}}\, \\ \,\href{Ledger.Dijkstra.Specification.Entities.html#4428}{\htmlId{8462}{\htmlClass{Generalizable}{\text{rewards₀}}}}\, \end{pmatrix}$  $\begin{pmatrix} \begin{pmatrix} \,\href{Ledger.Dijkstra.Specification.Entities.html#4390}{\htmlId{8479}{\htmlClass{Generalizable}{\text{voteDelegs}}}}\, \\ \,\href{Ledger.Dijkstra.Specification.Entities.html#4325}{\htmlId{8492}{\htmlClass{Generalizable}{\text{stakeDelegs}}}}\, \\ \,\href{Ledger.Dijkstra.Specification.Entities.html#4280}{\htmlId{8506}{\htmlClass{Generalizable}{\text{rewards}}}}\, \\ \,\href{Ledger.Dijkstra.Specification.Entities.html#4459}{\htmlId{8516}{\htmlClass{Generalizable}{\text{depositsᵈ}}}}\, \end{pmatrix} \\ \,\href{Ledger.Dijkstra.Specification.Entities.html#4590}{\htmlId{8530}{\htmlClass{Generalizable}{\text{pState}}}}\, \\ \begin{pmatrix} \,\href{Ledger.Dijkstra.Specification.Entities.html#4309}{\htmlId{8541}{\htmlClass{Generalizable}{\text{dReps}}}}\, \\ \,\href{Ledger.Dijkstra.Specification.Entities.html#4366}{\htmlId{8549}{\htmlClass{Generalizable}{\text{ccHotKeys}}}}\, \\ \,\href{Ledger.Dijkstra.Specification.Entities.html#4449}{\htmlId{8561}{\htmlClass{Generalizable}{\text{depositsᵍ}}}}\, \end{pmatrix} \end{pmatrix}$ ⇀⦇ txTop ,ENTITIES⦈ $\begin{pmatrix} \begin{pmatrix} \,\href{Ledger.Dijkstra.Specification.Entities.html#4401}{\htmlId{8599}{\htmlClass{Generalizable}{\text{voteDelegs'}}}}\, \,\href{Axiom.Set.Map.html#17775}{\htmlId{8611}{\htmlClass{Function Operator}{\text{∣\^{}}}}}\, \,\href{Ledger.Dijkstra.Specification.Entities.html#6735}{\htmlId{8614}{\htmlClass{Bound}{\text{activeVDelegs}}}}\, \\ \,\href{Ledger.Dijkstra.Specification.Entities.html#4337}{\htmlId{8630}{\htmlClass{Generalizable}{\text{stakeDelegs'}}}}\, \\ \,\href{Ledger.Dijkstra.Specification.Entities.html#2812}{\htmlId{8645}{\htmlClass{Function}{\text{applyDirectDeposits}}}}\, \,\href{Ledger.Dijkstra.Specification.Entities.html#7191}{\htmlId{8665}{\htmlClass{Bound}{\text{directDeposits}}}}\, \,\href{Ledger.Dijkstra.Specification.Entities.html#4288}{\htmlId{8680}{\htmlClass{Generalizable}{\text{rewards'}}}}\, \\ \,\href{Ledger.Dijkstra.Specification.Entities.html#4469}{\htmlId{8691}{\htmlClass{Generalizable}{\text{depositsᵈ'}}}}\, \end{pmatrix} \\ \,\href{Ledger.Dijkstra.Specification.Entities.html#4597}{\htmlId{8706}{\htmlClass{Generalizable}{\text{pState'}}}}\, \\ \,\href{Ledger.Dijkstra.Specification.Entities.html#4571}{\htmlId{8716}{\htmlClass{Generalizable}{\text{gState'}}}}\, \end{pmatrix}$