Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a data-backed list to ScriptContext #6597

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -13,24 +13,25 @@ import PlutusLedgerApi.V1.Data.Value
import PlutusTx qualified
import PlutusTx.Builtins qualified as PlutusTx
import PlutusTx.Data.AssocMap qualified as Map
import PlutusTx.Data.List qualified as Data.List
import PlutusTx.Plugin ()
import PlutusTx.Prelude qualified as PlutusTx

-- | A very crude deterministic generator for 'ScriptContext's with size
-- approximately proportional to the input integer.
mkScriptContext :: Int -> ScriptContext
mkScriptContext :: Integer -> ScriptContext
mkScriptContext i =
ScriptContext
(mkTxInfo i)
(Redeemer (PlutusTx.toBuiltinData (1 :: Integer)))
(SpendingScript (TxOutRef (TxId "") 0) Nothing)


mkTxInfo :: Int -> TxInfo
mkTxInfo :: Integer -> TxInfo
mkTxInfo i = TxInfo {
txInfoInputs=mempty,
txInfoReferenceInputs=mempty,
txInfoOutputs=fmap mkTxOut [1..i],
txInfoOutputs=Data.List.map mkTxOut (Data.List.fromSOP ([1..i] :: [Integer])),
txInfoFee=10000,
txInfoMint=mempty,
txInfoTxCerts=mempty,
Expand All @@ -46,15 +47,15 @@ mkTxInfo i = TxInfo {
txInfoTreasuryDonation=Nothing
}

mkTxOut :: Int -> TxOut
mkTxOut :: Integer -> TxOut
mkTxOut i = TxOut {
txOutAddress=pubKeyHashAddress (PubKeyHash ""),
txOutValue=mkValue i,
txOutDatum=NoOutputDatum,
txOutReferenceScript=Nothing
}

mkValue :: Int -> Value
mkValue :: Integer -> Value
mkValue i = assetClassValue (assetClass adaSymbol adaToken) (fromIntegral i)

-- This example decodes the script context (which is O(size-of-context) work), and then also
Expand All @@ -69,7 +70,7 @@ checkScriptContext1 d =
let !sc = PlutusTx.unsafeFromBuiltinData d
ScriptContext txi _ _ = sc
in
if PlutusTx.length (txInfoOutputs txi) `PlutusTx.modInteger` 2 PlutusTx.== 0
if Data.List.length (txInfoOutputs txi) `PlutusTx.modInteger` 2 PlutusTx.== 0
then ()
else PlutusTx.traceError "Odd number of outputs"

Expand Down
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
({cpu: 2600203951081
| mem: 2600000766517})
({cpu: 2100037450901
| mem: 2100000152637})
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
({cpu: 1000064498009
| mem: 1000000246421})
({cpu: 500029175573
| mem: 500000111005})
Loading