Skip to content

Commit 0ef6110

Browse files
committed
Deserialise; failing test
1 parent 7aee3aa commit 0ef6110

File tree

5 files changed

+76
-25
lines changed

5 files changed

+76
-25
lines changed

.github/workflows/ci-compiled-scripts.yaml

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ jobs:
2121
2222
- name: Install system dependencies
2323
uses: input-output-hk/actions/base@latest
24+
25+
- name: Install nix
26+
uses: cachix/install-nix-action@v27
27+
2428

2529
- uses: haskell-actions/setup@v2
2630
id: cabal-setup
@@ -39,14 +43,13 @@ jobs:
3943
- name: build & test
4044
run: |
4145
cabal update
42-
cabal build -j all --enable-tests
43-
cabal test all
46+
cabal build -j all
4447
- name: check compiled scripts are consistent
4548
# git diff --quiet implies --exit-code
4649
run: |
4750
cabal run export-smart-tokens ./generated/scripts/unapplied
4851
cabal run export-smart-tokens ./generated/scripts/preview 08a8d0bb8717839931b0a594f7c28b0a3b7c78f6e9172e977e250eab7637d879.0 '"addr_test1qq986m3uel86pl674mkzneqtycyg7csrdgdxj6uf7v7kd857kquweuh5kmrj28zs8czrwkl692jm67vna2rf7xtafhpqk3hecm"'
4952
cabal run export-smart-tokens ./generated/scripts/mainnet b1977c1eb33590ca1311384ab68cd36209832213ad4483feb8a1b7cb64828946.0 '"addr_test1qq986m3uel86pl674mkzneqtycyg7csrdgdxj6uf7v7kd857kquweuh5kmrj28zs8czrwkl692jm67vna2rf7xtafhpqk3hecm"'
5053
cabal run write-openapi-schema -- generated/openapi/schema.json
51-
nix develop --command bash -c "aiken build src/aiken-example/aiken --out ./generated/aiken/aiken-scripts.json"
54+
nix develop --command bash -c "aiken build src/aiken-example/aiken --out ./src/aiken-example/haskell/data/aiken-scripts.json"
5255
git diff --quiet

src/aiken-example/haskell/aiken-example.cabal

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ bug-reports: https://github.com/input-output-hk/wsc-poc
1313
description:
1414
Please see the README on GitHub at <https://github.com/input-output-hk/wst-poc>
1515

16+
data-files: data/aiken-scripts.json
17+
1618
common lang
1719
default-language: Haskell2010
1820
default-extensions:
@@ -47,6 +49,9 @@ common lang
4749
-Wincomplete-uni-patterns -Wincomplete-record-updates
4850
-Wredundant-constraints -Widentities
4951

52+
other-modules: Paths_aiken_example
53+
autogen-modules: Paths_aiken_example
54+
5055
library
5156
import: lang
5257
exposed-modules: Wst.Aiken.Blueprint
@@ -66,5 +71,9 @@ test-suite wst-aiken-test
6671
main-is: Spec.hs
6772
other-modules: Wst.Aiken.Test
6873
build-depends:
69-
, base >=4.14.0
74+
, aiken-example
75+
, base >=4.14.0
76+
, cardano-api
77+
, containers
7078
, tasty
79+
, tasty-hunit

src/aiken-example/haskell/lib/Wst/Aiken/Blueprint.hs

Lines changed: 28 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import Data.ByteString.Lazy qualified as BSL
2525
import Data.Map (Map)
2626
import Data.Map qualified as Map
2727
import Data.Proxy (Proxy (..))
28+
import Data.String (IsString (..))
2829
import Data.Text (Text)
2930
import Data.Text qualified as T
3031
import Data.Text.Encoding qualified as TE
@@ -41,32 +42,39 @@ instance ToJSON BlueprintScriptVersion where
4142
C.PlutusScriptV3 -> toJSON @String "v3"
4243

4344
instance FromJSON BlueprintScriptVersion where
44-
parseJSON = fmap (fmap BlueprintScriptVersion) $ Aeson.withText "BlueprintScriptVersion" $ \x -> case T.unpack x of
45-
"v1" -> pure (C.AnyPlutusScriptVersion C.PlutusScriptV1)
46-
"v2" -> pure (C.AnyPlutusScriptVersion C.PlutusScriptV2)
47-
"v3" -> pure (C.AnyPlutusScriptVersion C.PlutusScriptV3)
48-
v -> fail $ "Unexpected plutus script version: " <> v
45+
parseJSON = fmap (fmap BlueprintScriptVersion) $ Aeson.withText "BlueprintScriptVersion" $ \x -> case T.unpack x of
46+
"v1" -> pure (C.AnyPlutusScriptVersion C.PlutusScriptV1)
47+
"v2" -> pure (C.AnyPlutusScriptVersion C.PlutusScriptV2)
48+
"v3" -> pure (C.AnyPlutusScriptVersion C.PlutusScriptV3)
49+
v -> fail $ "Unexpected plutus script version: " <> v
4950

5051
data Blueprint = Blueprint
5152
{ preamble :: Preamble
52-
, validators :: [BlueprintValidator]
53+
, validators :: Map BlueprintKey ScriptInAnyLang
5354
}
5455
deriving stock (Eq, Show, Generic)
55-
deriving anyclass (FromJSON)
56+
57+
instance FromJSON Blueprint where
58+
parseJSON = withObject "Blueprint" $ \obj ->
59+
let mkb p v = Blueprint p <$> deserialise p v in
60+
(mkb
61+
<$> obj .: "preamble"
62+
<*> obj .: "validators")
63+
>>= either fail pure
5664

5765
data BlueprintValidator = BlueprintValidator
58-
{ title :: BlueprintKey
59-
, compiledCode :: Text
60-
, hash :: Text
61-
}
62-
deriving stock (Eq, Show, Generic)
63-
deriving anyclass (ToJSON, FromJSON)
66+
{ title :: BlueprintKey
67+
, compiledCode :: Text
68+
, hash :: Text
69+
}
70+
deriving stock (Eq, Show, Generic)
71+
deriving anyclass (ToJSON, FromJSON)
6472

6573
data Preamble = Preamble
66-
{ description :: Text
67-
, plutusVersion :: BlueprintScriptVersion
68-
}
69-
deriving stock (Eq, Show, Generic)
74+
{ description :: Text
75+
, plutusVersion :: BlueprintScriptVersion
76+
}
77+
deriving stock (Eq, Show, Generic)
7078

7179
instance FromJSON Preamble where
7280
parseJSON = withObject "Preamble" $ \obj ->
@@ -75,13 +83,13 @@ instance FromJSON Preamble where
7583
<*> obj .: "plutusVersion"
7684

7785
newtype BlueprintKey = BlueprintKey {unBlueprintKey :: Text}
78-
deriving newtype (Eq, Ord, Show, ToJSON, FromJSON)
86+
deriving newtype (Eq, Ord, Show, ToJSON, FromJSON, IsString)
7987

8088
loadFromFile :: FilePath -> IO (Either String Blueprint)
8189
loadFromFile fp = Aeson.eitherDecode . BSL.fromStrict <$> BS.readFile fp
8290

83-
deserialise :: Blueprint -> Either String (Map BlueprintKey ScriptInAnyLang)
84-
deserialise Blueprint{preamble = Preamble{plutusVersion = BlueprintScriptVersion v}, validators} =
91+
deserialise :: Preamble -> [BlueprintValidator] -> Either String (Map BlueprintKey ScriptInAnyLang)
92+
deserialise Preamble{plutusVersion = BlueprintScriptVersion v} validators =
8593
Map.fromList <$> traverse (deserialiseScript v) validators
8694

8795
deserialiseScript :: AnyPlutusScriptVersion -> BlueprintValidator -> Either String (BlueprintKey, ScriptInAnyLang)
Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,41 @@
1+
{-# LANGUAGE NamedFieldPuns #-}
2+
{-# LANGUAGE OverloadedStrings #-}
13
module Wst.Aiken.Test(
24
tests
35
) where
46

7+
import Cardano.Api qualified as C
8+
import Data.Functor (void)
9+
import Data.Map qualified as Map
10+
import Data.Maybe (isJust)
11+
import Paths_aiken_example qualified as Pkg
512
import Test.Tasty (TestTree, testGroup)
13+
import Test.Tasty.HUnit (Assertion, assertBool, assertEqual, testCase)
14+
import Wst.Aiken.Blueprint (Blueprint (..))
15+
import Wst.Aiken.Blueprint qualified as Blueprint
616

717
tests :: TestTree
818
tests = testGroup "unit tests"
9-
[
19+
[ testCase "load blueprint" loadBlueprint
20+
, testCase "deserialise script" deserialiseScript
1021
]
22+
23+
loadBlueprint :: Assertion
24+
loadBlueprint = do
25+
void loadExample
26+
27+
deserialiseScript :: Assertion
28+
deserialiseScript = do
29+
Blueprint{validators} <- loadExample
30+
maybe (fail "Expected script named 'placeholder.placeholder.mint'") pure (Map.lookup "placeholder.placeholder.mint" validators)
31+
>>= \case
32+
(C.ScriptInAnyLang (C.PlutusScriptLanguage C.PlutusScriptV3) script) -> do
33+
let hsh = C.hashScript script
34+
assertEqual "Script hash" "cc068514c844ed3f6c6d0f131b20cda83dbd50f340242b5740d0f81f" hsh
35+
_ -> fail "Unexpected script language"
36+
37+
loadExample :: IO Blueprint
38+
loadExample = do
39+
Pkg.getDataFileName "data/aiken-scripts.json"
40+
>>= Blueprint.loadFromFile
41+
>>= either fail pure

0 commit comments

Comments
 (0)