Skip to content

Commit

Permalink
Apply 80 cols limit to Wallet (with reason)
Browse files Browse the repository at this point in the history
  • Loading branch information
florentc committed Feb 6, 2023
1 parent d4a68af commit e606284
Showing 1 changed file with 46 additions and 26 deletions.
72 changes: 46 additions & 26 deletions cooked-validators/src/Cooked/Wallet.hs
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,15 @@ wallet j
| j > 0 && j <= 10 = let i = j - 1 in knownWallets !! i
| otherwise = Pl.fromWalletNumber (Pl.WalletNumber $ fromIntegral j)

-- | Retrieves the id of the known wallet that corresponds to a public key hash, if any.
-- | Retrieves the id of the known wallet that corresponds to a public key
-- hash, if any.
--
-- @walletPKHashToId (walletPKHash (wallet 3)) == Just 3@
walletPKHashToId :: Pl.PubKeyHash -> Maybe Int
walletPKHashToId = flip Map.lookup walletPKHashToIdMap
where
walletPKHashToIdMap = Map.fromList . flip zip [1 ..] . map walletPKHash $ knownWallets
walletPKHashToIdMap =
Map.fromList . flip zip [1 ..] . map walletPKHash $ knownWallets

-- | Retrieves a wallet publik key (PK)
walletPK :: Wallet -> Pl.PubKey
Expand Down Expand Up @@ -85,21 +87,22 @@ walletAddress w =
walletSK :: Pl.MockWallet -> PrivateKey
walletSK = Pl.unPaymentPrivateKey . Pl.paymentPrivateKey

-- FIXME Massive hack to be able to open a MockPrivateKey; this is needed because
-- the constructor and accessor to MockPrivateKey are not exported. Hence,
-- we make an isomorphic datatype, unsafeCoerce to this datatype then extract
-- whatever we need from it.
-- FIXME Massive hack to be able to open a MockPrivateKey; this is needed
-- because the constructor and accessor to MockPrivateKey are not exported.
-- Hence, we make an isomorphic datatype, unsafeCoerce to this datatype then
-- extract whatever we need from it.
newtype HACK = HACK {unHACK :: Cardano.XPrv}

-- | Retrieves a wallet's private staking key (secret key SK), if any
walletStakingSK :: Wallet -> Maybe PrivateKey
walletStakingSK = fmap hackUnMockPrivateKey . Pl.mwStakeKey
where
-- Don't use this; its a hack and will be deprecated once we have time
-- to make a PR into plutus exporting the things we need. If you use this anyway,
-- make sure that you only apply it to @MockPrivateKey@; the function is polymorphic
-- because @MockPrivateKey@ is not exported either; having a dedicated function makes
-- it easy to test that this works: check the @Cooked.WalletSpec@ test module.
-- Don't use this; its a hack and will be deprecated once we have time to
-- make a PR into plutus exporting the things we need. If you use this
-- anyway, make sure that you only apply it to @MockPrivateKey@; the
-- function is polymorphic because @MockPrivateKey@ is not exported either;
-- having a dedicated function makes it easy to test that this works: check
-- the @Cooked.WalletSpec@ test module.
hackUnMockPrivateKey :: a -> Cardano.XPrv
hackUnMockPrivateKey = unHACK . unsafeCoerce

Expand All @@ -115,11 +118,11 @@ txAddSignature w = Pl.addSignature' (walletSK w)
-- the underlying plutus definitions to make it easer when we have
-- to plug our own, if we ever have the need

-- | Describes the initial distribution of /UTxOs/ per wallet. This is important since
-- transaction validation must specify a /collateral/, hence, wallets must posses more
-- than one UTxO to begin with in order to execute a transaction and have some collateral
-- option. The @txCollateral@ is transfered to the node operator in case the transaction
-- fails to validate.
-- | Describes the initial distribution of /UTxOs/ per wallet. This is
-- important since transaction validation must specify a /collateral/, hence,
-- wallets must posses more than one UTxO to begin with in order to execute a
-- transaction and have some collateral option. The @txCollateral@ is
-- transfered to the node operator in case the transaction fails to validate.
--
-- An initial distribution defined by:
--
Expand All @@ -131,23 +134,31 @@ txAddSignature w = Pl.addSignature' (walletSK w)
-- > , (wallet 3 , [Pl.lovelaceValueOf 10000000 <> permanentValue "XYZ" 10])
-- > ]
--
-- Specifies a starting state where @wallet 1@ contains two /UTxOs/, one with 42 Ada
-- and one with 2 Ada and one "TOK" token; @wallet 2@ contains a single /UTxO/ with 10 Ada and
-- @wallet 3@ has 10 Ada and a permanent value. Check #quickvalues for more on quick
-- and permanent values. (Remember: 1 Ada = 1000000 Lovelace)
-- Specifies a starting state where @wallet 1@ contains two /UTxOs/, one with
-- 42 Ada and one with 2 Ada and one "TOK" token; @wallet 2@ contains a single
-- /UTxO/ with 10 Ada and @wallet 3@ has 10 Ada and a permanent value. Check
-- #quickvalues for more on quick and permanent values. (Remember: 1 Ada =
-- 1000000 Lovelace)
--
-- Check the corresponding @Default InitialDistribution@ instance for the default value.
newtype InitialDistribution = InitialDistribution {unInitialDistribution :: Map Wallet [Pl.Value]}
-- Check the corresponding @Default InitialDistribution@ instance for the
-- default value.
newtype InitialDistribution = InitialDistribution
{ unInitialDistribution :: Map Wallet [Pl.Value]
}
deriving (Eq, Show)

instance Semigroup InitialDistribution where
(InitialDistribution i) <> (InitialDistribution j) = InitialDistribution $ Map.unionWith (<>) i j
(InitialDistribution i) <> (InitialDistribution j) =
InitialDistribution $ Map.unionWith (<>) i j

instance Monoid InitialDistribution where
mempty = InitialDistribution Map.empty

instance Default InitialDistribution where
def = InitialDistribution $ Map.fromList $ zip knownWallets (repeat $ replicate 10 defLovelace)
def =
InitialDistribution $
Map.fromList $
zip knownWallets (repeat $ replicate 10 defLovelace)
where
defLovelace = Pl.lovelaceValueOf 100_000_000

Expand Down Expand Up @@ -177,13 +188,22 @@ initialTxFor initDist =
toPlTxOut :: Pl.ToData a => Pl.Address -> Pl.Value -> Maybe a -> Pl.TxOut
toPlTxOut addr value datum = toPlTxOut' addr value datum'
where
datum' = maybe Pl2.NoOutputDatum (Pl2.OutputDatumHash . Pl.datumHash . Pl.Datum . Pl.toBuiltinData) datum
datum' =
maybe
Pl2.NoOutputDatum
( Pl2.OutputDatumHash
. Pl.datumHash
. Pl.Datum
. Pl.toBuiltinData
)
datum

toPlTxOut' :: Pl.Address -> Pl.Value -> Pl2.OutputDatum -> Pl.TxOut
toPlTxOut' addr value datum = Pl.TxOut $ toCardanoTxOut' addr value datum

toCardanoTxOut' :: Pl.Address -> Pl.Value -> Pl2.OutputDatum -> Cardano.TxOut Cardano.CtxTx Cardano.BabbageEra
toCardanoTxOut' addr value datum = Cardano.TxOut cAddr cValue cDatum Cardano.ReferenceScriptNone
toCardanoTxOut' addr value datum =
Cardano.TxOut cAddr cValue cDatum Cardano.ReferenceScriptNone
where
fromRight' x = case x of
Left err -> error $ show err
Expand Down

0 comments on commit e606284

Please sign in to comment.