Skip to content

Commit

Permalink
extend Tx to account for witnesses
Browse files Browse the repository at this point in the history
  • Loading branch information
paweljakubas committed Oct 12, 2021
1 parent 79b58b3 commit 6858754
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 7 deletions.
2 changes: 1 addition & 1 deletion lib/core/src/Cardano/Wallet/Api/Server.hs
Expand Up @@ -2306,7 +2306,7 @@ balanceTransaction ctx genChange (ApiT wid) body = do
)

extractFromTx tx =
let (Tx _id _fee _coll _inps outs wdrlMap meta _vldt) = decodeTx tl tx
let (Tx _id _fee _coll _inps outs wdrlMap meta _vldt _) = decodeTx tl tx
-- TODO: Find a better abstraction that can cover this case.
wdrl = WithdrawalSelf
(error $ "WithdrawalSelf: reward-account should never been use "
Expand Down
6 changes: 6 additions & 0 deletions lib/core/src/Cardano/Wallet/Primitive/Types/Tx.hs
@@ -1,4 +1,5 @@
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE DeriveAnyClass #-}
{-# LANGUAGE DeriveFunctor #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE DerivingVia #-}
Expand Down Expand Up @@ -253,6 +254,10 @@ data Tx = Tx
-- passed validation. This is added by the block creator when
-- constructing the block. May be 'Nothing' for pre-Alonzo and pending
-- transactions.

, witnesses
:: ![ByteString]
-- ^ Witnesses cbored included in this transaction
} deriving (Show, Generic, Ord, Eq)

instance NFData Tx
Expand Down Expand Up @@ -742,6 +747,7 @@ fromTransactionInfo info = Tx
, withdrawals = txInfoWithdrawals info
, metadata = txInfoMetadata info
, scriptValidity = txInfoScriptValidity info
, witnesses = []
}
where
drop3rd :: (a, b, c) -> (a, b)
Expand Down
17 changes: 11 additions & 6 deletions lib/core/src/Cardano/Wallet/Primitive/Types/Tx/Gen.hs
Expand Up @@ -47,6 +47,8 @@ import Cardano.Wallet.Primitive.Types.Tx
( Tx (..), TxIn (..), TxMetadata (..), TxOut (..), TxScriptValidity (..) )
import Control.Monad
( replicateM )
import Data.ByteString
( ByteString )
import Data.Either
( fromRight )
import Data.Map.Strict
Expand Down Expand Up @@ -77,7 +79,7 @@ import Test.QuickCheck.Extra
( genFunction
, genMapWith
, genSized2With
, liftShrink7
, liftShrink8
, shrinkInterleaved
, shrinkMapWith
)
Expand All @@ -104,6 +106,7 @@ data TxWithoutId = TxWithoutId
, metadata :: !(Maybe TxMetadata)
, withdrawals :: !(Map RewardAccount Coin)
, scriptValidity :: !(Maybe TxScriptValidity)
, witnesses :: ![ByteString]
}
deriving (Eq, Ord, Show)

Expand All @@ -116,17 +119,19 @@ genTxWithoutId = TxWithoutId
<*> liftArbitrary genNestedTxMetadata
<*> genMapWith genRewardAccount genCoinPositive
<*> liftArbitrary genTxScriptValidity
<*> pure []

shrinkTxWithoutId :: TxWithoutId -> [TxWithoutId]
shrinkTxWithoutId =
shrinkMapBy tupleToTxWithoutId txWithoutIdToTuple $ liftShrink7
shrinkMapBy tupleToTxWithoutId txWithoutIdToTuple $ liftShrink8
(liftShrink shrinkCoinPositive)
(shrinkList (liftShrink2 shrinkTxIn shrinkCoinPositive))
(shrinkList (liftShrink2 shrinkTxIn shrinkCoinPositive))
(shrinkList shrinkTxOut)
(liftShrink shrinkTxMetadata)
(shrinkMapWith shrinkRewardAccount shrinkCoinPositive)
(liftShrink shrinkTxScriptValidity)
(const [])

txWithoutIdToTx :: TxWithoutId -> Tx
txWithoutIdToTx tx@TxWithoutId {..} = Tx {txId = mockHash tx, ..}
Expand All @@ -135,12 +140,12 @@ txToTxWithoutId :: Tx -> TxWithoutId
txToTxWithoutId Tx {..} = TxWithoutId {..}

txWithoutIdToTuple :: TxWithoutId -> _
txWithoutIdToTuple (TxWithoutId a1 a2 a3 a4 a5 a6 a7) =
(a1, a2, a3, a4, a5, a6, a7)
txWithoutIdToTuple (TxWithoutId a1 a2 a3 a4 a5 a6 a7 a8) =
(a1, a2, a3, a4, a5, a6, a7, a8)

tupleToTxWithoutId :: _ -> TxWithoutId
tupleToTxWithoutId (a1, a2, a3, a4, a5, a6, a7) =
(TxWithoutId a1 a2 a3 a4 a5 a6 a7)
tupleToTxWithoutId (a1, a2, a3, a4, a5, a6, a7, a8) =
(TxWithoutId a1 a2 a3 a4 a5 a6 a7 a8)

genTxScriptValidity :: Gen TxScriptValidity
genTxScriptValidity = genericArbitrary
Expand Down

0 comments on commit 6858754

Please sign in to comment.