Skip to content

Commit

Permalink
Merge pull request #217 from kadena-io/jose/fix-signer-codec
Browse files Browse the repository at this point in the history
Integration: Fix verifier codec
  • Loading branch information
jmcardon authored Sep 11, 2024
2 parents 8f050f9 + 2cae64a commit b5e3dae
Show file tree
Hide file tree
Showing 24 changed files with 634 additions and 533 deletions.
6 changes: 6 additions & 0 deletions .github/workflows/applications.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,12 @@ jobs:
- name: Checkout repository
uses: actions/checkout@v4

# Needed for a certain test
- name: Download chain9 test file
run: |
mkdir -p pact-tests/legacy-db-regression
curl -L https://chainweb-chain-db.s3.amazonaws.com/test-objects/pact-v1-chain-9.sqlite \
-o pact-tests/legacy-db-regression/pact-v1-chain-9.sqlite
# Haskell Setup
- name: Set permissions for .ghcup (ubuntu)
if: startsWith(matrix.os, 'ubuntu-')
Expand Down
8 changes: 4 additions & 4 deletions .github/workflows/nix.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,15 @@ jobs:

- name: Build and cache artifacts
run: |
echo Building the project and its devShell
nix build .#check --log-lines 500 --show-trace --accept-flake-config
#echo Building the project and its devShell
#nix build .#check --log-lines 500 --show-trace --accept-flake-config
echo Build the bundle
nix build .#pact-binary-bundle --log-lines 500 --show-trace --out-link pact-binary-bundle --accept-flake-config
tar -zcvf pact-binary-bundle.${{ matrix.os }}.tar.gz $(readlink pact-binary-bundle)
echo Build the recursive output
nix build .#recursive.allDerivations --log-lines 500 --show-trace --accept-flake-config
#echo Build the recursive output
#nix build .#recursive.allDerivations --log-lines 500 --show-trace --accept-flake-config
- name: Publish the bundle
uses: actions/upload-artifact@v4
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,4 @@ cabal.project.local*
.envrc
*.sqlite
.vscode
*.DS_Store
67 changes: 67 additions & 0 deletions pact-tests/Pact/Core/Test/JSONRoundtripTests.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE ExistentialQuantification #-}

module Pact.Core.Test.JSONRoundtripTests(tests) where

import qualified Pact.JSON.Encode as J

import Pact.Core.StableEncoding
import Pact.Core.Gen

import Data.Typeable
import Hedgehog
import Test.Tasty
import Test.Tasty.Hedgehog
import qualified Data.Aeson as JD

data StableEncodingCase
= forall a. (JD.FromJSON (StableEncoding a), J.Encode (StableEncoding a), Typeable a, Eq a, Show a) => StableEncodingCase (Gen a)

data EncodingCase
= forall a. (JD.FromJSON a, J.Encode a, Typeable a, Eq a, Show a) => EncodingCase (Gen a)


testJSONRoundtrip :: EncodingCase -> TestTree
testJSONRoundtrip (EncodingCase gen) =
testProperty testName $ property $ do
v <- forAll gen
JD.decodeStrict (J.encodeStrict v) === Just v
where
testName = "JSON roundtrips for: " <> show (typeNameOfGen gen)

typeNameOfGen :: Typeable a => Gen a -> String
typeNameOfGen a = show (typeRep (proxyFromGen a))
where
proxyFromGen :: Gen a -> Proxy a
proxyFromGen _ = Proxy

testStableEncodingRoundtrip :: StableEncodingCase -> TestTree
testStableEncodingRoundtrip (StableEncodingCase gen) = do
let testName = "JSON roundtrips for StableEncoding: " <> show (typeNameOfGen gen)
testProperty testName $ property $ do
v <- forAll gen
JD.decodeStrict (J.encodeStrict (StableEncoding v)) === Just (StableEncoding v)


tests :: TestTree
tests = testGroup "JSON Roundtrips" $ stableEncodings ++ jsonRoundtrips
where
stableEncodings = fmap testStableEncodingRoundtrip $
[ StableEncodingCase namespaceNameGen
, StableEncodingCase moduleNameGen
, StableEncodingCase qualifiedNameGen
, StableEncodingCase pactValueGen
, StableEncodingCase (guardGen qualifiedNameGen)
, StableEncodingCase keySetNameGen
, StableEncodingCase keySetGen
, StableEncodingCase fullyQualifiedNameGen
, StableEncodingCase (capTokenGen qualifiedNameGen pactValueGen)
, StableEncodingCase publicMetaGen
, StableEncodingCase publicDataGen
, StableEncodingCase rowDataGen
, StableEncodingCase defPactExecGen
, StableEncodingCase namespaceGen
]
jsonRoundtrips = fmap testJSONRoundtrip $
[ EncodingCase signerGen
]
164 changes: 164 additions & 0 deletions pact-tests/Pact/Core/Test/LegacyDBRegression.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
{-# LANGUAGE InstanceSigs #-}
{-# LANGUAGE ExistentialQuantification #-}

module Pact.Core.Test.LegacyDBRegression
( tests )
where

import Control.Exception.Safe
import Control.Lens
import Control.Applicative
import Control.Monad
import Data.Default
import Data.Text(Text)
import Test.Tasty
import Test.Tasty.HUnit
import System.FilePath
import System.Directory
import qualified Database.SQLite3 as SQL
import qualified Network.HTTP.Simple as Http
import qualified Data.ByteString as B
import qualified Data.Text as T

import Pact.Core.Persistence
import Pact.Core.Builtin
import Pact.Core.Info
import Pact.Core.Names
import Pact.Core.Persistence.SQLite
import Pact.Core.Serialise

import qualified Data.Char as Char
import qualified Text.Megaparsec as MP
import qualified Text.Megaparsec.Char as MP
import Data.IORef


dbFolder :: FilePath
dbFolder = "pact-tests" </> "legacy-db-regression"

dbFile :: FilePath
dbFile = "pact-v1-chain-9.sqlite"

dbFilePath :: FilePath
dbFilePath = dbFolder </> dbFile

data SomeDomain
= forall k v. Show k => SomeDomain (Domain k v CoreBuiltin SpanInfo)

-- Copy pasted from Pact.Core.Names
-- exporting this causes a compliation error in Pact.Core.Principals
identParser :: Parser Text
identParser = do
c1 <- MP.letterChar <|> MP.oneOf specials
rest <- MP.takeWhileP Nothing (\c -> Char.isLetter c || Char.isDigit c || elem c specials)
pure (T.cons c1 rest)
where
specials :: String
specials = "%#+-_&$@<>=^?*!|/~"

-- Copy pasted from Pact.Core.Names
-- exporting this causes a compliation error in Pact.Core.Principals
moduleNameParser :: Parser ModuleName
moduleNameParser = do
p <- identParser
MP.try (go p <|> pure (ModuleName p Nothing))
where
go ns = do
_ <- MP.char '.'
p1 <- identParser
pure (ModuleName p1 (Just (NamespaceName ns)))

type Parser = MP.Parsec () Text

-- | Hacky way of parsing a user table
parseUserTable :: Text -> Maybe TableName
parseUserTable s =
case reverse (T.splitOn "_" s) of
identRaw:tbl ->
let tbl' = T.intercalate "_" (reverse tbl)
in case (,) <$> MP.parseMaybe moduleNameParser tbl' <*> MP.parseMaybe identParser identRaw of
Just (mn, ident) -> Just (TableName ident mn)
_ -> Nothing
_ -> Nothing

-- Note: It's an IO PactDb because `withResource` from tasty has a really
-- annoying signature
runTableDecodeRegression :: HasCallStack => IO (PactDb CoreBuiltin SpanInfo) -> SomeDomain -> TestTree
runTableDecodeRegression pdbIO (SomeDomain domain) = testCase testName $ do
pdb <- pdbIO
keys <- ignoreGas def $ _pdbKeys pdb domain
forM_ keys $ \k -> do
v <- ignoreGas def $ _pdbRead pdb domain k
let msg = "Decode failed for table " <> T.unpack (renderDomain domain) <> " at key " <> show k
assertBool msg $ has _Just v
where
testName = "Running regression for table: " <> T.unpack (renderDomain domain)

data DBHarness
= DBHarness
{ _dbhDB :: SQL.Database
, _dbhPactDb :: PactDb CoreBuiltin SpanInfo
, _dbhStmtCache :: IORef StmtCache
}

withStmt :: SQL.Database -> Text -> (SQL.Statement -> IO a) -> IO a
withStmt conn sql = bracket (SQL.prepare conn sql) SQL.finalize

withDb :: (SQL.Database -> IO c) -> IO c
withDb act =
bracket (unsafeCreateSqlitePactDb serialisePact_raw_spaninfo (T.pack dbFilePath))
(\(_, db, c) -> unsafeCloseSqlitePactDb db c)
(\(_, db, _) -> act db)

tests :: IO TestTree
tests = do
downloadRegressionDb
userTables <- withDb getUserTables
let allTables =
[ SomeDomain DKeySets
, SomeDomain DDefPacts
, SomeDomain DModules
, SomeDomain DNamespaces
] ++ [SomeDomain (DUserTables t) | u <- userTables, Just t <- [parseUserTable u]]
pure $ withResource acquireDbHarness releaseDbHarness $ \pdbio ->
testGroup "Legacy PactDb Regression" $
runTableDecodeRegression (_dbhPactDb <$> pdbio) <$> allTables
where
acquireDbHarness :: IO DBHarness
acquireDbHarness = do
(pdb, db, cache) <- unsafeCreateSqlitePactDb serialisePact_raw_spaninfo (T.pack dbFilePath)
pure (DBHarness db pdb cache )

releaseDbHarness :: DBHarness -> IO ()
releaseDbHarness harness =
unsafeCloseSqlitePactDb (_dbhDB harness) (_dbhStmtCache harness)


getUserTables :: SQL.Database -> IO [Text]
getUserTables con = do
withStmt con qry (go [])
where
qry = "select name from sqlite_master where type='table'"
go acc stmt = SQL.step stmt >>= \case
SQL.Done -> pure acc
SQL.Row -> do
[SQL.SQLText tbl] <- SQL.columns stmt
go (tbl: acc) stmt


-- Function to download a file as a ByteString and save it to a file
downloadFile :: String -> FilePath -> IO ()
downloadFile url destination = do
let request = Http.parseRequest_ url
response <- Http.httpBS request
let body = Http.getResponseBody response -- Get the response as a ByteString
B.writeFile destination body -- Write the ByteString to a file

downloadRegressionDb :: IO ()
downloadRegressionDb = do
fileExists <- doesFileExist dbFilePath
unless fileExists $ do
createDirectoryIfMissing True dbFolder
downloadFile "https://chainweb-chain-db.s3.amazonaws.com/test-objects/pact-v1-chain-9.sqlite" dbFilePath


2 changes: 1 addition & 1 deletion pact-tests/Pact/Core/Test/PersistenceTests.hs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ testsWithSerial :: (Show i, Eq b, Eq i, Default i, IsBuiltin b)
-> Gen i
-> [TestTree]
testsWithSerial serial builtins b i =
[ testProperty "KeySet" $ keysetPersistRoundtrip serial builtins (keySetGen undefined)
[ testProperty "KeySet" $ keysetPersistRoundtrip serial builtins keySetGen
-- ^ keySetGen does not use its first argument now. We will pass a real argument
-- once custom keyset predicate functions are supported.
, testProperty "ModuleData" $ moduleDataRoundtrip serial builtins b i
Expand Down
2 changes: 1 addition & 1 deletion pact-tests/Pact/Core/Test/SerialiseTests.hs
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ serialiseModule = property $ do

serialiseKeySet :: Property
serialiseKeySet = property $ do
ks <- forAll (keySetGen qualifiedNameGen)
ks <- forAll keySetGen
let
encoded = _encodeKeySet serialisePact ks
case _decodeKeySet serialisePact encoded of
Expand Down
6 changes: 6 additions & 0 deletions pact-tests/PactCoreTests.hs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ module Main where

import Test.Tasty


import qualified Pact.Core.Test.CommandTests as CommandTests
import qualified Pact.Core.Test.ReplTests as ReplTests
import qualified Pact.Core.Test.LexerParserTests as LexerParserTests
Expand All @@ -19,6 +20,8 @@ import qualified Pact.Core.Test.ConTagGolden as ConTagGoldenTests
import qualified Pact.Core.Test.DocsTests as DocsTests
import qualified Pact.Core.Test.PrincipalTests as PrincipalTests
import qualified Pact.Core.Test.SignatureSchemeTests as SignatureSchemeTests
import qualified Pact.Core.Test.JSONRoundtripTests as JSONRoundtripTests
import qualified Pact.Core.Test.LegacyDBRegression as LegacyDbRegression

main :: IO ()
main = do
Expand All @@ -27,6 +30,7 @@ main = do
legacyTests <- LegacySerialiseTests.tests
commandTests <- CommandTests.tests
docsTests <- DocsTests.tests
legacyDbRegression <- LegacyDbRegression.tests
defaultMain $ testGroup "pactTests"
[ replTests
, LexerTests.tests
Expand All @@ -45,5 +49,7 @@ main = do
, docsTests
, PrincipalTests.tests
, SignatureSchemeTests.tests
, JSONRoundtripTests.tests
, legacyDbRegression
]

26 changes: 5 additions & 21 deletions pact-tng.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,6 @@ library
Pact.Core.Repl
Pact.Core.SizeOf
Pact.Core.StackFrame
Pact.Core.Legacy.LegacyPactValue
Pact.Core.Legacy.LegacyCodec
Pact.Core.Verifiers
Pact.Core.Interpreter
Expand Down Expand Up @@ -362,13 +361,6 @@ executable profile-tx
, terminal-progress-bar
, neat-interpolation

other-modules:
-- Pact.Core.GasModel.BuiltinsGas
-- Pact.Core.GasModel.InterpreterGas
-- Pact.Core.GasModel.ContractBench
-- Pact.Core.GasModel.Serialization
-- Pact.Core.GasModel.Utils

ghc-options: -Wall -threaded -rtsopts -Wincomplete-record-updates -Wincomplete-uni-patterns -Wredundant-constraints
ghc-prof-options: -fprof-auto -fprof-auto-calls
default-language: Haskell2010
Expand Down Expand Up @@ -514,6 +506,7 @@ test-suite core-tests
, pact-tng
, prettyprinter
, vector
, megaparsec
, tasty
, tasty-hunit
, tasty-hedgehog
Expand All @@ -530,6 +523,8 @@ test-suite core-tests
, lsp-test >= 0.17
, lsp-types
, safe-exceptions
, http-conduit
, direct-sqlite
other-modules:
, Pact.Core.Test.CommandTests
, Pact.Core.Test.ReplTests
Expand All @@ -547,25 +542,14 @@ test-suite core-tests
, Pact.Core.Test.PrincipalTests
, Pact.Core.Test.ConTagGolden
, Pact.Core.Test.DocsTests
, Pact.Core.Test.JSONRoundtripTests
, Pact.Core.Test.LegacyDBRegression
, Paths_pact_tng
, Pact.Core.Test.SignatureSchemeTests
if (flag(with-crypto))
build-depends: pact-tng:pact-crypto


-- tools
executable legacyPactDbCheck
import: pact-common
hs-source-dirs: tools

main-is: LegacyPactDbCheck.hs

build-depends:
, pact-tng
ghc-options: -Wall -threaded -rtsopts -Wincomplete-record-updates -Wincomplete-uni-patterns -Wredundant-constraints
ghc-prof-options: -fprof-auto -fprof-auto-calls
default-language: Haskell2010

-- -- tools
-- executable pact-server
-- import: pact-common
Expand Down
2 changes: 1 addition & 1 deletion pact/Pact/Core/ChainData.hs
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ data PublicData = PublicData
, _pdPrevBlockHash :: !Text
-- ^ block hash of preceding block
}
deriving (Show, Generic)
deriving (Show, Eq, Generic)
makeLenses ''PublicData

instance Default PublicData where
Expand Down
Loading

0 comments on commit b5e3dae

Please sign in to comment.