Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move Redeemer type to Write.Tx.Redeemers module #4047

Merged
merged 1 commit into from
Jul 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -500,8 +500,6 @@ import Cardano.Wallet.Primitive.Types.Credentials
( ClearCredentials, RootCredentials (..) )
import Cardano.Wallet.Primitive.Types.Hash
( Hash (..) )
import Cardano.Wallet.Primitive.Types.Redeemer
( Redeemer (..) )
import Cardano.Wallet.Primitive.Types.TokenBundle
( TokenBundle (..) )
import Cardano.Wallet.Primitive.Types.TokenMap
Expand Down Expand Up @@ -558,7 +556,7 @@ import Cardano.Wallet.Unsafe
import Cardano.Wallet.Write.Tx
( AnyRecentEra (..) )
import Cardano.Wallet.Write.Tx.Balance
( UTxOAssumptions (..) )
( Redeemer (..), UTxOAssumptions (..) )
import Control.Arrow
( second, (&&&) )
import Control.DeepSeq
Expand Down
1 change: 0 additions & 1 deletion lib/wallet/cardano-wallet.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,6 @@ library
Cardano.Wallet.Primitive.Types.MinimumUTxO
Cardano.Wallet.Primitive.Types.MinimumUTxO.Gen
Cardano.Wallet.Primitive.Types.ProtocolMagic
Cardano.Wallet.Primitive.Types.Redeemer
Cardano.Wallet.Primitive.Types.RewardAccount
Cardano.Wallet.Primitive.Types.RewardAccount.Gen
Cardano.Wallet.Primitive.Types.StateDeltaSeq
Expand Down
50 changes: 0 additions & 50 deletions lib/wallet/src/Cardano/Wallet/Primitive/Types/Redeemer.hs

This file was deleted.

26 changes: 0 additions & 26 deletions lib/wallet/src/Cardano/Wallet/Shelley/Compatibility.hs
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,6 @@ module Cardano.Wallet.Shelley.Compatibility
, toLedgerExUnits
, fromCardanoAddress
, toSystemStart
, toScriptPurpose
, fromShelleyTxIn
, toCostModelsAsArray
, toCardanoPolicyId
Expand Down Expand Up @@ -379,7 +378,6 @@ import qualified Cardano.Ledger.Allegra as Allegra
import qualified Cardano.Ledger.Alonzo as Alonzo
import qualified Cardano.Ledger.Alonzo.Language as Alonzo
import qualified Cardano.Ledger.Alonzo.Scripts as Alonzo
import qualified Cardano.Ledger.Alonzo.Tx as Alonzo
import qualified Cardano.Ledger.Alonzo.TxSeq as Alonzo
import qualified Cardano.Ledger.Api as Ledger
import qualified Cardano.Ledger.Babbage as Babbage
Expand All @@ -390,8 +388,6 @@ import qualified Cardano.Ledger.Credential as SL
import qualified Cardano.Ledger.Crypto as SL
import qualified Cardano.Ledger.Keys as Ledger
import qualified Cardano.Ledger.Mary as Mary
import qualified Cardano.Ledger.Mary.Value as SL
import qualified Cardano.Ledger.SafeHash as SafeHash
import qualified Cardano.Ledger.Shelley as Shelley
import qualified Cardano.Ledger.Shelley.API as SL
import qualified Cardano.Ledger.Shelley.API as SLAPI
Expand All @@ -402,7 +398,6 @@ import qualified Cardano.Wallet.Primitive.Types.Address as W
import qualified Cardano.Wallet.Primitive.Types.Coin as Coin
import qualified Cardano.Wallet.Primitive.Types.Coin as W
import qualified Cardano.Wallet.Primitive.Types.Hash as W
import qualified Cardano.Wallet.Primitive.Types.Redeemer as W
import qualified Cardano.Wallet.Primitive.Types.RewardAccount as W
import qualified Cardano.Wallet.Primitive.Types.TokenBundle as TokenBundle
import qualified Cardano.Wallet.Primitive.Types.TokenPolicy as W
Expand Down Expand Up @@ -1354,34 +1349,13 @@ fromUnitInterval x =
toSystemStart :: W.StartTime -> SystemStart
toSystemStart (W.StartTime t) = SystemStart t

toScriptPurpose :: W.Redeemer -> Alonzo.ScriptPurpose StandardCrypto
toScriptPurpose = \case
W.RedeemerSpending _ txin ->
Alonzo.Spending (toTxIn txin)
W.RedeemerMinting _ pid ->
Alonzo.Minting (toPolicyID pid)
W.RedeemerRewarding _ (Cardano.StakeAddress ntwrk acct) ->
Alonzo.Rewarding (SL.RewardAcnt ntwrk acct)

toCardanoTxId :: W.Hash "Tx" -> Cardano.TxId
toCardanoTxId (W.Hash h) = Cardano.TxId $ UnsafeHash $ toShort h

toCardanoTxIn :: W.TxIn -> Cardano.TxIn
toCardanoTxIn (W.TxIn tid ix) =
Cardano.TxIn (toCardanoTxId tid) (Cardano.TxIx (fromIntegral ix))

toTxIn :: SL.Crypto crypto => W.TxIn -> SL.TxIn crypto
toTxIn (W.TxIn tid ix) =
SL.TxIn (toTxId tid) (SL.mkTxIxPartial $ fromIntegral ix)

toTxId :: Crypto.HashAlgorithm (SL.HASH crypto) => W.Hash "Tx" -> SL.TxId crypto
toTxId (W.Hash h) =
(SL.TxId (SafeHash.unsafeMakeSafeHash $ UnsafeHash $ toShort h))

toPolicyID :: SL.Crypto crypto => W.TokenPolicyId -> SL.PolicyID crypto
toPolicyID (W.UnsafeTokenPolicyId (W.Hash bytes)) =
SL.PolicyID (SL.ScriptHash (unsafeHashFromBytes bytes))

toCardanoStakeCredential :: W.RewardAccount -> Cardano.StakeCredential
toCardanoStakeCredential = \case
W.FromKeyHash bs ->
Expand Down
3 changes: 3 additions & 0 deletions lib/wallet/src/Cardano/Wallet/Write/Tx.hs
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,9 @@ module Cardano.Wallet.Write.Tx
, modifyLedgerBody
, emptyTx

-- * TxId
, Ledger.TxId

-- * TxOut
, Core.TxOut
, BabbageTxOut (..)
Expand Down
5 changes: 2 additions & 3 deletions lib/wallet/src/Cardano/Wallet/Write/Tx/Balance.hs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ module Cardano.Wallet.Write.Tx.Balance

-- * Partial transactions
, PartialTx (..)
, Redeemer (..)

-- * UTxO assumptions
, UTxOAssumptions (..)
Expand Down Expand Up @@ -105,8 +106,6 @@ import Cardano.Tx.Balance.Internal.CoinSelection
)
import Cardano.Wallet.Primitive.Types
( TokenBundleMaxSize (TokenBundleMaxSize) )
import Cardano.Wallet.Primitive.Types.Redeemer
( Redeemer )
import Cardano.Wallet.Primitive.Types.TokenBundle
( TokenBundle (..) )
import Cardano.Wallet.Primitive.Types.Tx
Expand Down Expand Up @@ -147,7 +146,7 @@ import Cardano.Wallet.Write.Tx
, withConstraints
)
import Cardano.Wallet.Write.Tx.Redeemers
( ErrAssignRedeemers (..), assignScriptRedeemers )
( ErrAssignRedeemers (..), Redeemer (..), assignScriptRedeemers )
import Cardano.Wallet.Write.Tx.Sign
( estimateKeyWitnessCount, estimateSignedTxSize )
import Cardano.Wallet.Write.Tx.SizeEstimation
Expand Down
73 changes: 68 additions & 5 deletions lib/wallet/src/Cardano/Wallet/Write/Tx/Redeemers.hs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TupleSections #-}
{-# LANGUAGE TypeApplications #-}
Expand All @@ -15,22 +17,29 @@
module Cardano.Wallet.Write.Tx.Redeemers
( assignScriptRedeemers
, ErrAssignRedeemers (..)
, Redeemer (..)
) where

import Prelude

import Cardano.Api
( StakeAddress, serialiseToBech32 )
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should have the cardano-api imports qualified as Cardano instead.

import Cardano.Crypto.Hash.Class
( Hash )
import Cardano.Ledger.Alonzo.TxInfo
( TranslationError )
import Cardano.Ledger.Api
( Tx, bodyTxL, rdmrsTxWitsL, scriptIntegrityHashTxBodyL, witsTxL )
import Cardano.Ledger.Mary.Value
( PolicyID (..) )
import Cardano.Ledger.Shelley.API
( StrictMaybe (..) )
( ScriptHash (..), StrictMaybe (..) )
import Cardano.Slotting.EpochInfo
( EpochInfo, hoistEpochInfo )
import Cardano.Wallet.Primitive.Types.Redeemer
( Redeemer, redeemerData )
import Cardano.Wallet.Shelley.Compatibility
( toScriptPurpose )
import Cardano.Wallet.Primitive.Types.TokenPolicy
( TokenPolicyId )
import Cardano.Wallet.Shelley.Compatibility.Ledger
( Convert (..) )
import Cardano.Wallet.Write.Tx
( IsRecentEra (recentEra)
, PParams
Expand Down Expand Up @@ -59,6 +68,8 @@ import Control.Monad.Trans.State.Strict
( StateT (..), execStateT, get, modify', put )
import Data.Bifunctor
( bimap )
import Data.ByteString
( ByteString )
import Data.Function
( (&) )
import Data.Generics.Internal.VL.Lens
Expand All @@ -67,17 +78,25 @@ import Data.Generics.Labels
()
import Data.Map.Strict
( Map, (!) )
import Data.Maybe
( fromMaybe )
import Fmt
( Buildable (..) )
import GHC.Generics
( Generic )

import qualified Cardano.Api as Cardano
import qualified Cardano.Api.Shelley as Cardano
import qualified Cardano.Crypto.Hash as Crypto
import qualified Cardano.Ledger.Alonzo.PlutusScriptApi as Alonzo
import qualified Cardano.Ledger.Alonzo.Scripts as Alonzo
import qualified Cardano.Ledger.Alonzo.Scripts.Data as Alonzo
import qualified Cardano.Ledger.Alonzo.Tx as Alonzo
import qualified Cardano.Ledger.Alonzo.TxWits as Alonzo
import qualified Cardano.Ledger.Api as Ledger
import qualified Cardano.Wallet.Primitive.Types.Hash as W
import qualified Cardano.Wallet.Primitive.Types.TokenPolicy as W
import qualified Cardano.Wallet.Primitive.Types.Tx.TxIn as W
import qualified Data.ByteString.Lazy as BL
import qualified Data.Map as Map
import qualified Data.Map.Merge.Strict as Map
Expand Down Expand Up @@ -223,6 +242,50 @@ assignScriptRedeemers pparams timeTranslation utxo redeemers tx =
, Just l <- [Alonzo.language script]
]

--
-- The 'Redeemer' type
-- TODO: Move back to the wallet and/or retire
Copy link
Member Author

@Anviking Anviking Jul 21, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👆 We should (probably) work with the redeemers a part of the partial tx CBOR instead. In that case we'd need to add logic for updating the tx without invalidating the existing ix-based pointers.

--

data Redeemer
= RedeemerSpending ByteString W.TxIn
| RedeemerMinting ByteString TokenPolicyId
| RedeemerRewarding ByteString StakeAddress
deriving (Eq, Generic, Show)

instance Buildable Redeemer where
build = \case
RedeemerSpending _ input ->
"spending(" <> build input <> ")"
RedeemerMinting _ pid ->
"minting(" <> build pid <> ")"
RedeemerRewarding _ addr ->
"rewarding(" <> build (serialiseToBech32 addr) <> ")"

redeemerData :: Redeemer -> ByteString
redeemerData = \case
RedeemerSpending bytes _ -> bytes
RedeemerMinting bytes _ -> bytes
RedeemerRewarding bytes _ -> bytes

toScriptPurpose :: Redeemer -> Alonzo.ScriptPurpose StandardCrypto
toScriptPurpose = \case
RedeemerSpending _ txin ->
Alonzo.Spending (toLedger txin)
RedeemerMinting _ pid ->
Alonzo.Minting (toPolicyID pid)
RedeemerRewarding _ (Cardano.StakeAddress ntwrk acct) ->
Alonzo.Rewarding (Ledger.RewardAcnt ntwrk acct)

toPolicyID :: W.TokenPolicyId -> PolicyID StandardCrypto
toPolicyID (W.UnsafeTokenPolicyId (W.Hash bytes)) =
PolicyID (ScriptHash (unsafeHashFromBytes bytes))
where
unsafeHashFromBytes :: Crypto.HashAlgorithm h => ByteString -> Hash h a
unsafeHashFromBytes =
fromMaybe (error "unsafeHashFromBytes: wrong length")
. Crypto.hashFromBytes

--------------------------------------------------------------------------------
-- Utils
--------------------------------------------------------------------------------
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -179,8 +179,6 @@ import Cardano.Wallet.Primitive.Types.MinimumUTxO.Gen
( testParameter_coinsPerUTxOByte_Babbage
, testParameter_coinsPerUTxOWord_Alonzo
)
import Cardano.Wallet.Primitive.Types.Redeemer
( Redeemer (..) )
import Cardano.Wallet.Primitive.Types.TokenBundle
( AssetId, TokenBundle )
import Cardano.Wallet.Primitive.Types.TokenBundle.Gen
Expand Down Expand Up @@ -265,6 +263,7 @@ import Cardano.Wallet.Write.Tx.Balance
, ErrSelectAssets (..)
, ErrUpdateSealedTx (..)
, PartialTx (..)
, Redeemer (..)
, TxFeeAndChange (..)
, TxFeeUpdate (..)
, TxUpdate (..)
Expand Down
Loading