Skip to content

Commit

Permalink
timelock golden tests
Browse files Browse the repository at this point in the history
  • Loading branch information
paweljakubas committed Jan 26, 2021
1 parent 9371e22 commit 724e839
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 47 deletions.
3 changes: 0 additions & 3 deletions lib/core/src/Cardano/Wallet/DB/Sqlite.hs
Expand Up @@ -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
Expand Down
Expand Up @@ -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
Expand Down
18 changes: 1 addition & 17 deletions lib/core/src/Cardano/Wallet/Primitive/Scripts.hs
Expand Up @@ -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
Expand All @@ -44,23 +44,17 @@ 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
( catMaybes )

import qualified Data.Map.Strict as Map

deriving instance Ord ScriptHash

isShared
:: (k ~ ShelleyKey, SoftDerivation k)
=> Script
Expand Down Expand Up @@ -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
82 changes: 62 additions & 20 deletions lib/shelley/test/unit/Cardano/Wallet/Shelley/CompatibilitySpec.hs
Expand Up @@ -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
Expand Down Expand Up @@ -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 =
Expand Down Expand Up @@ -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
Expand Down
12 changes: 7 additions & 5 deletions stack.yaml
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 724e839

Please sign in to comment.