Skip to content

Commit

Permalink
add new builtins to encoding/decoding
Browse files Browse the repository at this point in the history
  • Loading branch information
imalsogreg committed Dec 13, 2023
1 parent ce21070 commit 280322c
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 56 deletions.
8 changes: 3 additions & 5 deletions pact-core-tests/Pact/Core/Gen/Serialise.hs
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ schemaGen = do
typeGen :: Gen Type
typeGen = Gen.recursive Gen.choice
[ TyPrim <$> tyPrimGen
, TyModRef <$> moduleNameGen
, TyModRef <$> Gen.set (Range.linear 0 10) moduleNameGen
]
[ TyList <$> typeGen
, TyObject <$> schemaGen
Expand Down Expand Up @@ -311,11 +311,9 @@ defTableGen i = do

stepGen :: Gen b -> Gen i -> Gen (Step Name Type b i)
stepGen b i = Gen.choice
[ Step <$> termGen b i <*> mt
, StepWithRollback <$> termGen b i <*> termGen b i <*> mt
[ Step <$> termGen b i
, StepWithRollback <$> termGen b i <*> termGen b i
]
where
mt = Gen.maybe (Gen.list (Range.linear 0 16) (termGen b i))

defPactGen :: Gen b -> Gen i -> Gen (DefPact Name Type b i)
defPactGen b i = do
Expand Down
8 changes: 4 additions & 4 deletions pact-core-tests/Pact/Core/Test/ReplTests.hs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import Test.Tasty.HUnit
import Control.Monad(when)
import Data.IORef
import Data.Default
import Data.Text(Text)
import Data.Foldable(traverse_)
import System.Directory
import System.FilePath
Expand Down Expand Up @@ -38,6 +37,7 @@ import Pact.Core.Serialise.CBOR_V1 (encodeModuleData_TESTING, decodeModuleData_T
tests :: IO TestTree
tests = do
files <- replTestFiles
pure $ testGroup "ReplTests"
[ testGroup "in-memory db" (runFileReplTest <$> files)
, testGroup "sqlite db" (runFileReplTestSqlite <$> files)
]
Expand All @@ -52,18 +52,18 @@ replTestFiles = do
runFileReplTest :: TestName -> TestTree
runFileReplTest file = testCase file $ do
pdb <- mockPactDb serialiseRepl
B.readFile (replTestDir </> file) >>= runReplTest pdb file
T.readFile (replTestDir </> file) >>= runReplTest pdb file

runFileReplTestSqlite :: TestName -> TestTree
runFileReplTestSqlite file = testCase file $ do
ctnt <- B.readFile (replTestDir </> file)
ctnt <- T.readFile (replTestDir </> file)
let enc = serialisePact{ _encodeModuleData = encodeModuleData_TESTING
, _decodeModuleData = fmap LegacyDocument . decodeModuleData_TESTING
}
withSqlitePactDb enc ":memory:" $ \pdb -> do
runReplTest pdb file ctnt

runReplTest :: PactDb ReplRawBuiltin SpanInfo -> FilePath -> ByteString -> Assertion
runReplTest :: PactDb ReplRawBuiltin SpanInfo -> FilePath -> T.Text -> Assertion
runReplTest pdb file src = do
gasRef <- newIORef (Gas 0)
gasLog <- newIORef Nothing
Expand Down
114 changes: 67 additions & 47 deletions pact-core/Pact/Core/Serialise/CBOR_V1.hs
Original file line number Diff line number Diff line change
Expand Up @@ -550,9 +550,6 @@ instance Serialise RawBuiltin where
RawRound -> encodeWord 19
RawCeiling -> encodeWord 20
RawFloor -> encodeWord 21
RawRoundPrec -> encodeWord 22
RawCeilingPrec -> encodeWord 23
RawFloorPrec -> encodeWord 24
RawExp -> encodeWord 22
RawLn -> encodeWord 23
RawSqrt -> encodeWord 24
Expand Down Expand Up @@ -594,7 +591,6 @@ instance Serialise RawBuiltin where
RawB64Decode -> encodeWord 60
RawStrToList -> encodeWord 61
RawYield -> encodeWord 62
RawYieldToChain -> encodeWord 63
RawResume -> encodeWord 63
RawBind -> encodeWord 64
RawRequireCapability -> encodeWord 65
Expand Down Expand Up @@ -642,14 +638,26 @@ instance Serialise RawBuiltin where
RawNamespace -> encodeWord 107
RawDefineNamespace -> encodeWord 108
RawDescribeNamespace -> encodeWord 109
RawChainData -> encodeWord 100
RawIsCharset -> encodeWord 100
RawCreatePrincipal -> encodeWord 110
RawIsPrincipal -> encodeWord 111
RawTypeOfPrincipal -> encodeWord 112
RawValidatePrincipal -> encodeWord 113
RawCreateDefPactGuard -> encodeWord 114

RawRoundPrec -> encodeWord 125
RawCeilingPrec -> encodeWord 126
RawFloorPrec -> encodeWord 127
RawYieldToChain -> encodeWord 115
RawChainData -> encodeWord 116
RawIsCharset -> encodeWord 117
RawPactId -> encodeWord 118
RawZkPairingCheck -> encodeWord 119
RawZKScalarMult -> encodeWord 120
RawZkPointAdd -> encodeWord 121
RawPoseidonHashHackachain -> encodeWord 122
RawTypeOf -> encodeWord 123
RawDec -> encodeWord 124

decode = decodeWord >>= \case
0 -> pure RawAdd
1 -> pure RawSub
Expand All @@ -673,47 +681,44 @@ instance Serialise RawBuiltin where
19 -> pure RawRound
20 -> pure RawCeiling
21 -> pure RawFloor
22 -> pure RawRoundPrec
23 -> pure RawCeilingPrec
24 -> pure RawFloorPrec
25 -> pure RawExp
26 -> pure RawLn
27 -> pure RawSqrt
28 -> pure RawLogBase
29 -> pure RawLength
30 -> pure RawTake
31 -> pure RawDrop
32 -> pure RawConcat
33 -> pure RawReverse
34 -> pure RawContains
35 -> pure RawSort
36 -> pure RawSortObject
37 -> pure RawRemove
38 -> pure RawMod
39 -> pure RawMap
40 -> pure RawFilter
41 -> pure RawZip
42 -> pure RawIntToStr
43 -> pure RawStrToInt
44 -> pure RawStrToIntBase
45 -> pure RawFold
46 -> pure RawDistinct
47 -> pure RawFormat
48 -> pure RawEnumerate
49 -> pure RawEnumerateStepN
50 -> pure RawShow
51 -> pure RawReadMsg
52 -> pure RawReadMsgDefault
53 -> pure RawReadInteger
54 -> pure RawReadDecimal
55 -> pure RawReadString
56 -> pure RawReadKeyset
57 -> pure RawEnforceGuard
58 -> pure RawEnforceKeyset
59 -> pure RawKeysetRefGuard
60 -> pure RawAt
61 -> pure RawMakeList
62 -> pure RawB64Encode
22 -> pure RawExp
23 -> pure RawLn
24 -> pure RawSqrt
25 -> pure RawLogBase
26 -> pure RawLength
27 -> pure RawTake
28 -> pure RawDrop
29 -> pure RawConcat
30 -> pure RawReverse
31 -> pure RawContains
32 -> pure RawSort
33 -> pure RawSortObject
34 -> pure RawRemove
35 -> pure RawMod
36 -> pure RawMap
37 -> pure RawFilter
38 -> pure RawZip
39 -> pure RawIntToStr
40 -> pure RawStrToInt
41 -> pure RawStrToIntBase
42 -> pure RawFold
43 -> pure RawDistinct
44 -> pure RawFormat
45 -> pure RawEnumerate
46 -> pure RawEnumerateStepN
47 -> pure RawShow
48 -> pure RawReadMsg
49 -> pure RawReadMsgDefault
50 -> pure RawReadInteger
51 -> pure RawReadDecimal
52 -> pure RawReadString
53 -> pure RawReadKeyset
54 -> pure RawEnforceGuard
55 -> pure RawEnforceKeyset
56 -> pure RawKeysetRefGuard
57 -> pure RawAt
58 -> pure RawMakeList
59 -> pure RawB64Encode
60 -> pure RawB64Decode
61 -> pure RawStrToList
62 -> pure RawYield
Expand Down Expand Up @@ -769,6 +774,21 @@ instance Serialise RawBuiltin where
112 -> pure RawTypeOfPrincipal
113 -> pure RawValidatePrincipal
114 -> pure RawCreateDefPactGuard

115 -> pure RawYieldToChain
116 -> pure RawChainData
117 -> pure RawIsCharset
118 -> pure RawPactId
119 -> pure RawZkPairingCheck
120 -> pure RawZKScalarMult
121 -> pure RawZkPointAdd
122 -> pure RawPoseidonHashHackachain
123 -> pure RawTypeOf
124 -> pure RawDec

125 -> pure RawRoundPrec
126 -> pure RawCeilingPrec
127 -> pure RawFloorPrec
_ -> fail "unexpeced decoding"


Expand Down

0 comments on commit 280322c

Please sign in to comment.