Skip to content

Commit

Permalink
Modifying ledger and adding working script examples
Browse files Browse the repository at this point in the history
  • Loading branch information
Ali-Hill committed Apr 16, 2024
1 parent 97394e5 commit 210ed31
Show file tree
Hide file tree
Showing 17 changed files with 433 additions and 200 deletions.
3 changes: 3 additions & 0 deletions src/Everything.agda
Expand Up @@ -14,3 +14,6 @@ import MidnightExample.HSLedger
-- ** not currently used
import Tactic.DeriveComp
import Foreign.Convertible.DerivingTest

-- ** Verifying Script Examples
import ScriptVerification.Examples
2 changes: 1 addition & 1 deletion src/Interface/Hashable.agda
Expand Up @@ -6,7 +6,7 @@ open import Function.Definitions using (Injective)

record Hashable (T THash : Set) : Set where
field hash : T THash
hashInj : Injective _≡_ _≡_ hash
-- hashInj : Injective _≡_ _≡_ hash

open Hashable ⦃...⦄ public

Expand Down
4 changes: 2 additions & 2 deletions src/Ledger/Foreign/HSLedger.agda
Expand Up @@ -24,14 +24,14 @@ open import Interface.HasOrder.Instance

module _ {A : Set} ⦃ _ : DecEq A ⦄ where instance
∀Hashable : Hashable A A
∀Hashable = λ where .hash id; .hashInj refl refl
∀Hashable = λ where .hash id

∀isHashableSet : isHashableSet A
∀isHashableSet = mkIsHashableSet A

instance
Hashable-⊤ : Hashable ⊤ ℕ
Hashable-⊤ = λ where .hash tt 0; .hashInj _ refl
Hashable-⊤ = λ where .hash tt 0

module Implementation where
Network =
Expand Down
3 changes: 2 additions & 1 deletion src/Ledger/Script.lagda
Expand Up @@ -34,7 +34,7 @@ record PlutusStructure : Set₁ where
Language PlutusScript CostModel Prices LangDepView ExUnits : Set
⦃ ExUnit-CommutativeMonoid ⦄ : IsCommutativeMonoid' 0ℓ 0ℓ ExUnits
⦃ Hashable-PlutusScript ⦄ : Hashable PlutusScript ScriptHash
⦃ DecEq-PlutusScript ⦄ : DecEq PlutusScript
-- ⦃ DecEq-PlutusScript ⦄ : DecEq PlutusScript
⦃ DecEq-CostModel ⦄ : DecEq CostModel
⦃ DecEq-LangDepView ⦄ : DecEq LangDepView

Expand Down Expand Up @@ -173,6 +173,7 @@ record ScriptStructure : Set₁ where

Script = P1Script ⊎ P2Script


open import Data.Empty
open import Agda.Builtin.Equality
open import Relation.Binary.PropositionalEquality
Expand Down
5 changes: 2 additions & 3 deletions src/Ledger/ScriptValidation.agda
Expand Up @@ -10,7 +10,6 @@ open import Ledger.Transaction
open import Ledger.Abstract
open import Ledger.Crypto


module Ledger.ScriptValidation
(txs : _) (open TransactionStructure txs)
(abs : AbstractFunctions txs) (open AbstractFunctions abs) (open indexOf indexOfImp)
Expand Down Expand Up @@ -51,7 +50,6 @@ getDatum tx utxo (Spend txin) = let open Tx tx; open TxWitnesses wits in
(lookupᵐ? utxo txin)
getDatum tx utxo _ = []


record TxInfo : Set where
field realizedInputs : UTxO
txouts : Ix ⇀ TxOut
Expand Down Expand Up @@ -148,7 +146,8 @@ valContext txinfo sp = toData (txinfo , sp)
-- need to update costmodels to add the language map in order to check
-- (Language ↦ CostModel) ∈ costmdls ↦ (Language ↦ CostModel)

abstract

opaque

collectPhaseTwoScriptInputs' : PParams Tx UTxO (ScriptPurpose × ScriptHash)
Maybe (Script × List Data × ExUnits × CostModel)
Expand Down
7 changes: 6 additions & 1 deletion src/Ledger/Set/HashMap.agda
Expand Up @@ -5,11 +5,16 @@ open import Data.Product.Ext
open import Interface.Hashable
open import Ledger.Set.Theory

module Ledger.Set.HashMap {A B : Set} ⦃ _ : Hashable A B ⦄ (X : ℙ A) where
module Ledger.Set.HashMap {A B : Set} ⦃ _ : Hashable A B ⦄ ⦃ _ : DecEq B ⦄ (X : ℙ A) where

setToHashRel : ℙ (B × A)
setToHashRel = mapˡ hash (mapˢ ×-dup X)

setToHashMap : B ⇀ A
setToHashMap = fromListᵐ (setToList setToHashRel)

{-
setToHashMap : B ⇀ A
setToHashMap = setToHashRel ᵐ
where instance _ = record { isLeftUnique = mapˡ∘map⦅×-dup⦆-uniq {inj = hashInj} }
-}
12 changes: 7 additions & 5 deletions src/Ledger/Utxo.lagda
Expand Up @@ -124,12 +124,14 @@ module _ (let open Tx; open TxBody; open TxWitnesses) where opaque
\end{code}
\end{NoConway}
\begin{code}
minfee : PParams → UTxO → Tx → Coin
minfee pp utxo tx = pp .a * tx .body .txsize + pp .b
+ txscriptfee (pp .prices) (totExUnits tx)
+ pp .minFeeRefScriptCoinsPerByte
*↓ ∑ˢ[ x ← refScripts tx utxo ] scriptSize x
open import Axiom.Set.Sum

minfee : PParams → UTxO → Tx → Coin
minfee pp utxo tx =
pp .a * tx .body .txsize + pp .b
+ txscriptfee (pp .prices) (totExUnits tx)
+ pp .minFeeRefScriptCoinsPerByte
*↓ ∑[ x ← (mapValues scriptSize $ setToHashMap (refScripts tx utxo)) ] x
\end{code}
\begin{code}[hide]
module _ where
Expand Down
3 changes: 1 addition & 2 deletions src/MidnightExample/HSLedger.agda
Expand Up @@ -14,11 +14,10 @@ open F using (Hash)
private variable A B : Set
instance
_ : Hashable String Hash
_ = λ where .hash F.hash; .hashInj F.hash-inj
_ = λ where .hash F.hash
private
Show⇒Hashable : ⦃ Show A ⦄ Hashable A Hash
Show⇒Hashable .hash = hash ∘ show
Show⇒Hashable .hashInj = hash-inj where postulate hash-inj : _
instance
Show-List : ⦃ Hashable A Hash ⦄ Show (List A)
Show-List .show = foldr (λ a acc show (hash a) ++ "," ++ acc) ""
Expand Down
2 changes: 0 additions & 2 deletions src/MidnightExample/Ledger.lagda
Expand Up @@ -23,7 +23,6 @@ instance

Hashable-ℕ : Hashable ℕ Hash
Hashable-ℕ .hash n = hash (+ n)
Hashable-ℕ .hashInj = ℤ.+-injective ∘ hashInj

_<ᵇ_ : Maybe ℕ → Maybe ℕ → Bool
just a <ᵇ just b = a Prelude.<ᵇ b
Expand Down Expand Up @@ -89,7 +88,6 @@ txDelta-injective {dec} {dec} refl = refl
instance
Hashable-Tx : Hashable Tx Hash
Hashable-Tx .hash = hash ∘ txDelta
Hashable-Tx .hashInj = txDelta-injective ∘ hashInj
\end{code}
\caption{Transactions}
\end{figure*}
Expand Down
115 changes: 3 additions & 112 deletions src/ScriptVerification/Examples.agda
@@ -1,113 +1,4 @@
open import Ledger.Prelude hiding (fromList; ε); open Computational
open import ScriptVerification.Prelude
module ScriptVerification.Examples where

module ScriptVerification.Examples
(scriptImp : ScriptImplementation ℕ) (open ScriptImplementation scriptImp)
where

open import ScriptVerification.LedgerImplementation ℕ scriptImp
open import ScriptVerification.Lib ℕ scriptImp

open Implementation

open import Ledger.ScriptValidation SVTransactionStructure SVAbstractFunctions
open import Data.Empty
-- open import Ledger.UTxo SVTransactionStructure SVAbstractFunctions
open import Ledger.Utxo SVTransactionStructure SVAbstractFunctions
open import Ledger.Transaction
open TransactionStructure SVTransactionStructure
open import Ledger.Types.Epoch
open EpochStructure SVEpochStructure

open import Data.Rational

succeedIf1 : PlutusScript
succeedIf1 zero _ = false
succeedIf1 (suc zero) _ = true
succeedIf1 (suc (suc x)) _ = false

succeedIf1' : Bool
succeedIf1' zero _ = false
succeedIf1' (suc zero) _ = true
succeedIf1' (suc (suc x)) _ = false

test : PlutusScript
test x y with deserialise x
... | just x₁ = succeedIf1' x₁ y
... | nothing = false

initEnv : UTxOEnv
initEnv = createEnv 0

initState : UTxO
initState = fromList' (createInitUtxoState 5 10)

initTxOut : TxOut
initTxOut = inj₁ (record { net = tt ;
pay = inj₂ succeedIf1 ;
stake = inj₂ succeedIf1 })
, 10 , nothing

exTx : Tx
exTx = record { body = record
{ txins =
; txouts = fromListIx ((6 , initTxOut) ∷ [])
; txfee = 10
; mint = 0
; txvldt = nothing , nothing
; txcerts = []
; txwdrls =
; txvote = []
; txprop = []
; txdonation = 0
; txup = nothing
; txADhash = nothing
; netwrk = just tt
; txsize = 10
; txid = 6
; collateral =
; reqSigHash =-- maybe need this
; scriptIntHash = nothing -- not sure
} ;
wits = record { vkSigs = ∅ ;
scripts = ∅ ;
txdats = ∅ ;
txrdmrs = ∅ } ;
isValid = false ;
txAD = nothing }


example : Bool
example = evalScripts exTx (collectPhaseTwoScriptInputs (UTxOEnv.pparams initEnv) exTx initState)

example2 : Bool
example2 = not true


-- scriptsNoTest : {!!}
-- scriptsNoTest = Scripts-No {!!}


{-
initList : List (TxIn × TxOut)
initList = ((1 , 1) , inj₂ (record { net = tt ; pay = inj₁ 1 ; attrsSize = 1 }) , (1 , nothing)) ∷ []
initUTxO : UTxO
initUTxO = fromListᵐ initList
exTx : Tx
exTx = {!!}
example : Bool
example = evalScripts exTx (collectPhaseTwoScriptInputs (UTxOEnv.pparams initEnv) exTx initUTxO)
-}


{-
initState : UTxOState
initState = ?
transaction : Tx
transaction = ?
-}
-- _⊢_⇀⦇_,UTXOS⦈_ : UTxOEnv → UTxOState → Tx → UTxOState → Set
import ScriptVerification.SucceedIfNumber
import ScriptVerification.HelloWorld
128 changes: 128 additions & 0 deletions src/ScriptVerification/HelloWorld.agda
@@ -0,0 +1,128 @@
open import Ledger.Prelude hiding (fromList; ε); open Computational
open import ScriptVerification.Prelude

module ScriptVerification.HelloWorld where

scriptImp : ScriptImplementation String String
scriptImp = record { serialise = id ;
deserialise = λ x just x ;
toData' = λ x "dummy" }

open import ScriptVerification.LedgerImplementation String String scriptImp
open import ScriptVerification.Lib String String scriptImp
open import Ledger.ScriptValidation SVTransactionStructure SVAbstractFunctions
open import Data.Empty
open import Ledger.Utxo SVTransactionStructure SVAbstractFunctions
open import Ledger.Transaction
open TransactionStructure SVTransactionStructure
open import Ledger.Types.Epoch
open EpochStructure SVEpochStructure
open Implementation

-- true if redeemer is "Hello World"
helloWorld' : Maybe String Maybe String Bool
helloWorld' _ (just s) = ⌊ (s ≟ "Hello World") ⌋
helloWorld' _ _ = false

helloWorld : PlutusScript
helloWorld = 777 , applyScript helloWorld'

initEnv : UTxOEnv
initEnv = createEnv 0

initTxOut : TxOut
initTxOut = inj₁ (record { net = tt ;
pay = inj₂ 777 ;
stake = inj₂ 777 })
, 10 , nothing

script : TxIn × TxOut
script = (6 , 6) , initTxOut

initState : UTxO
initState = fromList' (script ∷ (createInitUtxoState 5 10))

succeedTx : Tx
succeedTx = record { body = record
{ txins = Ledger.Prelude.fromList ((6 , 6) ∷ [])
; txouts =-- fromListIx ((6 , initTxOut) ∷ [])
; txfee = 10
; mint = 0
; txvldt = nothing , nothing
; txcerts = []
; txwdrls =
; txvote = []
; txprop = []
; txdonation = 0
; txup = nothing
; txADhash = nothing
; netwrk = just tt
; txsize = 10
; txid = 7
; collateral =
; reqSigHash =-- maybe need this
; scriptIntHash = nothing -- not sure
} ;
wits = record { vkSigs = ∅ ;
scripts = Ledger.Prelude.fromList ((inj₂ helloWorld) ∷ []) ;
txdats = ∅ ;
txrdmrs = fromListᵐ (((Spend , 6) , "Hello World" , (5 , 5)) ∷ []) } ;
isValid = true ;
txAD = nothing }

failTx : Tx
failTx = record { body = record
{ txins = Ledger.Prelude.fromList ((6 , 6) ∷ [])
; txouts =-- fromListIx ((6 , initTxOut) ∷ [])
; txfee = 10
; mint = 0
; txvldt = nothing , nothing
; txcerts = []
; txwdrls =
; txvote = []
; txprop = []
; txdonation = 0
; txup = nothing
; txADhash = nothing
; netwrk = just tt
; txsize = 10
; txid = 7
; collateral =
; reqSigHash =-- maybe need this
; scriptIntHash = nothing -- not sure
} ;
wits = record { vkSigs = ∅ ;
scripts = Ledger.Prelude.fromList ((inj₂ helloWorld) ∷ []) ;
txdats = ∅ ;
txrdmrs = fromListᵐ (((Spend , 6) , "Hello World!" , (5 , 5)) ∷ []) } ;
isValid = true ;
txAD = nothing }

succeedState : List (Script × List Implementation.Data × Implementation.ExUnits × Implementation.CostModel)
succeedState = (collectPhaseTwoScriptInputs (UTxOEnv.pparams initEnv) succeedTx initState)

succeedExample : Bool
succeedExample = evalScripts succeedTx succeedState

failState : List (Script × List Implementation.Data × Implementation.ExUnits × Implementation.CostModel)
failState = (collectPhaseTwoScriptInputs (UTxOEnv.pparams initEnv) failTx initState)

failExample : Bool
failExample = evalScripts failTx failState

opaque
unfolding collectPhaseTwoScriptInputs
unfolding setToList

-- need to check that the state is non-empty otherwise evalScripts will always return true
_ : notEmpty succeedState ≡ ⊤
_ = refl

_ : succeedExample ≡ true
_ = refl

_ : notEmpty failState ≡ ⊤
_ = refl

_ : failExample ≡ false
_ = refl

0 comments on commit 210ed31

Please sign in to comment.