From 45fdef0717ed4351014a2c7080a8c7fe49aeb5c7 Mon Sep 17 00:00:00 2001 From: Pawel Jakubas Date: Thu, 17 Dec 2020 17:31:04 +0100 Subject: [PATCH] timelock golden tests --- lib/core/src/Cardano/Wallet/DB/Sqlite.hs | 3 - .../Primitive/AddressDiscovery/Sequential.hs | 2 - .../src/Cardano/Wallet/Primitive/Scripts.hs | 18 +--- .../Wallet/Shelley/CompatibilitySpec.hs | 82 ++++++++++++++----- stack.yaml | 12 +-- 5 files changed, 70 insertions(+), 47 deletions(-) diff --git a/lib/core/src/Cardano/Wallet/DB/Sqlite.hs b/lib/core/src/Cardano/Wallet/DB/Sqlite.hs index 2c95d14621e..6601fd83f7a 100644 --- a/lib/core/src/Cardano/Wallet/DB/Sqlite.hs +++ b/lib/core/src/Cardano/Wallet/DB/Sqlite.hs @@ -2246,9 +2246,6 @@ insertScriptPool wid sl pool = do toDB (scriptHash, verKeyIxs) = zipWith (SeqStateScriptHash wid sl scriptHash . getIndex) verKeyIxs [0..] -instance Ord ScriptHash where - compare (ScriptHash sh1) (ScriptHash sh2) = compare sh1 sh2 - selectScriptPool :: forall k . W.WalletId -> W.SlotNo diff --git a/lib/core/src/Cardano/Wallet/Primitive/AddressDiscovery/Sequential.hs b/lib/core/src/Cardano/Wallet/Primitive/AddressDiscovery/Sequential.hs index 870d2626e1c..43231452a00 100644 --- a/lib/core/src/Cardano/Wallet/Primitive/AddressDiscovery/Sequential.hs +++ b/lib/core/src/Cardano/Wallet/Primitive/AddressDiscovery/Sequential.hs @@ -308,8 +308,6 @@ instance ((PersistPublicKey (key 'AccountK))) xpubF = hexF $ serializeXPub acct acctF = prefixF 8 xpubF <> "..." <> suffixF 8 xpubF -deriving instance Ord KeyHash - lookupKeyHash :: KeyHash -> VerificationKeyPool k diff --git a/lib/core/src/Cardano/Wallet/Primitive/Scripts.hs b/lib/core/src/Cardano/Wallet/Primitive/Scripts.hs index 25ca0c00899..2203e9aaf08 100644 --- a/lib/core/src/Cardano/Wallet/Primitive/Scripts.hs +++ b/lib/core/src/Cardano/Wallet/Primitive/Scripts.hs @@ -28,7 +28,7 @@ module Cardano.Wallet.Primitive.Scripts import Prelude import Cardano.Address.Script - ( KeyHash (..), Script (..), ScriptHash (..), toScriptHash ) + ( KeyHash (..), Script (..), ScriptHash (..), foldScript, toScriptHash ) import Cardano.Crypto.Wallet ( XPub ) import Cardano.Wallet.Primitive.AddressDerivation @@ -44,14 +44,10 @@ import Cardano.Wallet.Primitive.AddressDiscovery.Sequential ) import Control.Arrow ( first ) -import Control.Monad - ( foldM ) import Control.Monad.Trans.State.Strict ( runState, state ) import Data.Function ( (&) ) -import Data.Functor.Identity - ( Identity (..) ) import Data.Map.Strict ( Map ) import Data.Maybe @@ -59,8 +55,6 @@ import Data.Maybe import qualified Data.Map.Strict as Map -deriving instance Ord ScriptHash - isShared :: (k ~ ShelleyKey, SoftDerivation k) => Script @@ -97,13 +91,3 @@ insertIf predicate k v = if predicate v then Map.insert k v else id retrieveAllVerKeyHashes :: Script -> [KeyHash] retrieveAllVerKeyHashes = foldScript (:) [] - -foldScript :: (KeyHash -> b -> b) -> b -> Script -> b -foldScript fn zero = \case - RequireSignatureOf k -> fn k zero - RequireAllOf xs -> foldMScripts xs - RequireAnyOf xs -> foldMScripts xs - RequireSomeOf _ xs -> foldMScripts xs - where - foldMScripts = - runIdentity . foldM (\acc -> Identity . foldScript fn acc) zero diff --git a/lib/shelley/test/unit/Cardano/Wallet/Shelley/CompatibilitySpec.hs b/lib/shelley/test/unit/Cardano/Wallet/Shelley/CompatibilitySpec.hs index d5ee0490037..ebfe9196325 100644 --- a/lib/shelley/test/unit/Cardano/Wallet/Shelley/CompatibilitySpec.hs +++ b/lib/shelley/test/unit/Cardano/Wallet/Shelley/CompatibilitySpec.hs @@ -104,7 +104,7 @@ import Ouroboros.Network.Block import Test.Hspec ( Spec, describe, it, shouldBe, shouldSatisfy ) import Test.Hspec.Core.Spec - ( SpecM ) + ( SpecM, SpecWith ) import Test.Hspec.QuickCheck ( prop ) import Test.QuickCheck @@ -317,29 +317,41 @@ spec = do it title $ inspectAddress addr `shouldSatisfy` predicate describe "Script hashes for diffent versions" $ do - testScripts Cardano.SimpleScriptV1 - testScripts Cardano.SimpleScriptV2 - -testScripts + testScriptsAllLangs Cardano.SimpleScriptV1 + testScriptsAllLangs Cardano.SimpleScriptV2 + testScriptsTimelockLang + +toKeyHash :: Text -> Script +toKeyHash txt = case fromBase16 (T.encodeUtf8 txt) of + Right bs -> case keyHashFromBytes bs of + Just kh -> RequireSignatureOf kh + Nothing -> error "Hash key not valid" + Left _ -> error "Hash key not valid" + +toPaymentHash :: Text -> Cardano.SimpleScript lang +toPaymentHash txt = + case Cardano.deserialiseFromRawBytesHex (Cardano.AsHash Cardano.AsPaymentKey) (T.encodeUtf8 txt) of + Just payKeyHash -> Cardano.RequireSignature payKeyHash + Nothing -> error "Hash key not valid" + +checkScriptHashes + :: String + -> Script + -> Cardano.Script lang + -> SpecWith () +checkScriptHashes title adrestiaScript nodeScript = it title $ + (unScriptHash $ toScriptHash adrestiaScript) `shouldBe` + (Cardano.serialiseToRawBytes $ Cardano.hashScript nodeScript) + +testScriptsAllLangs :: forall lang . Cardano.SimpleScriptVersion lang -> SpecM () () -testScripts version = do +testScriptsAllLangs version = do let hashKeyTxt1 = "deeae4e895d8d57378125ed4fd540f9bf245d59f7936a504379cfc1e" let hashKeyTxt2 = "60a3bf69aa748f9934b64357d9f1ca202f1a768aaf57263aedca8d5f" let hashKeyTxt3 = "ffcbb72393215007d9a0aa02b7430080409cd8c053fd4f5b4d905053" let hashKeyTxt4 = "96834025cdca063ce9c32dfae6bc6a3e47f8da07ee4fb8e1a3901559" - let toKeyHash txt = case fromBase16 (T.encodeUtf8 txt) of - Right bs -> case keyHashFromBytes bs of - Just kh -> RequireSignatureOf kh - Nothing -> error "Hash key not valid" - Left _ -> error "Hash key not valid" - - let toPaymentHash txt = - case Cardano.deserialiseFromRawBytesHex (Cardano.AsHash Cardano.AsPaymentKey) (T.encodeUtf8 txt) of - Just payKeyHash -> Cardano.RequireSignature payKeyHash - Nothing -> error "Hash key not valid" - let toSimpleScript = Cardano.SimpleScript version let matrix = @@ -423,9 +435,39 @@ testScripts version = do ] forM_ matrix $ \(title, adrestiaScript, nodeScript) -> - it title $ do - (unScriptHash $ toScriptHash adrestiaScript) `shouldBe` - (Cardano.serialiseToRawBytes $ Cardano.hashScript nodeScript) + checkScriptHashes title adrestiaScript nodeScript + +testScriptsTimelockLang :: SpecM () () +testScriptsTimelockLang = do + let hashKeyTxt1 = "deeae4e895d8d57378125ed4fd540f9bf245d59f7936a504379cfc1e" + let hashKeyTxt2 = "60a3bf69aa748f9934b64357d9f1ca202f1a768aaf57263aedca8d5f" + + let toSimpleScript = Cardano.SimpleScript Cardano.SimpleScriptV2 + + let matrix = + [ ( "SimpleScriptV2 StartSlot" + , RequireAllOf [toKeyHash hashKeyTxt1, StartSlot 120] + , toSimpleScript $ + Cardano.RequireAllOf [toPaymentHash hashKeyTxt1, Cardano.RequireTimeAfter Cardano.TimeLocksInSimpleScriptV2 (SlotNo 120)] + ) + , ( "SimpleScriptV2 ExpireSlot" + , RequireAllOf [toKeyHash hashKeyTxt1, ExpireSlot 120] + , toSimpleScript $ + Cardano.RequireAllOf [toPaymentHash hashKeyTxt1, Cardano.RequireTimeBefore Cardano.TimeLocksInSimpleScriptV2 (SlotNo 120)] + ) + , ( "SimpleScriptV2 ExpireSlot and StartSlot" + , RequireAllOf [StartSlot 120, ExpireSlot 150, RequireAnyOf [toKeyHash hashKeyTxt1, toKeyHash hashKeyTxt2]] + , toSimpleScript $ + Cardano.RequireAllOf + [ Cardano.RequireTimeAfter Cardano.TimeLocksInSimpleScriptV2 (SlotNo 120) + , Cardano.RequireTimeBefore Cardano.TimeLocksInSimpleScriptV2 (SlotNo 150) + , Cardano.RequireAnyOf [toPaymentHash hashKeyTxt1, toPaymentHash hashKeyTxt2 ] + ] + ) + ] + + forM_ matrix $ \(title, adrestiaScript, nodeScript) -> + checkScriptHashes title adrestiaScript nodeScript instance Arbitrary (Hash "Genesis") where arbitrary = Hash . BS.pack <$> vector 32 diff --git a/stack.yaml b/stack.yaml index 1a01d539cce..97ccd17dd5c 100644 --- a/stack.yaml +++ b/stack.yaml @@ -15,6 +15,8 @@ packages: - lib/test-utils - lib/shelley - lib/strict-non-empty-containers +- ../cardano-addresses/command-line +- ../cardano-addresses/core extra-deps: # Miscellaneous @@ -66,11 +68,11 @@ extra-deps: - persistent-template # cardano-addresses-3.1.0 -- git: https://github.com/input-output-hk/cardano-addresses - commit: 9c38189c3e0e011a305ddcd1219e50a0731e99ea - subdirs: - - command-line - - core +#- git: https://github.com/input-output-hk/cardano-addresses +# commit: 9c38189c3e0e011a305ddcd1219e50a0731e99ea +# subdirs: +# - command-line +# - core # cardano-transactions master branch - git: https://github.com/input-output-hk/cardano-transactions