Skip to content

Commit

Permalink
Merge #3551
Browse files Browse the repository at this point in the history
3551: [ADP-2328] add cbor field to TxRelation r=paolino a=paolino

<!--
Detail in a few bullet points the work accomplished in this PR.

Before you submit, don't forget to:

* Make sure the GitHub PR fields are correct:
   ✓ Set a good Title for your PR.
   ✓ Assign yourself to the PR.
   ✓ Assign one or more reviewer(s).
   ✓ Link to a Jira issue, and/or other GitHub issues or PRs.
   ✓ In the PR description delete any empty sections
     and all text commented in <!--, so that this text does not appear
     in merge commit messages.

* Don't waste reviewers' time:
   ✓ If it's a draft, select the Create Draft PR option.
   ✓ Self-review your changes to make sure nothing unexpected slipped through.

* Try to make your intent clear:
   ✓ Write a good Description that explains what this PR is meant to do.
   ✓ Jira will detect and link to this PR once created, but you can also
     link this PR in the description of the corresponding Jira ticket.
   ✓ Highlight what Testing you have done.
   ✓ Acknowledge any changes required to the Documentation.
-->


- expose a prism to convert from database CBOR records to `TxCBOR` datatype
- add `cbor` field to `TxRelation`

### Comments

<!-- Additional comments, links, or screenshots to attach, if any. -->

### Issue Number

<!-- Reference the Jira/GitHub issue that this PR relates to, and which requirements it tackles.
  Note: Jira issues of the form ADP- will be auto-linked. -->
ADP-2328 

Co-authored-by: paolo veronelli <paolo.veronelli@gmail.com>
  • Loading branch information
iohk-bors[bot] and paolino committed Oct 29, 2022
2 parents 675f55f + a3019b3 commit c18f64d
Show file tree
Hide file tree
Showing 13 changed files with 106 additions and 424 deletions.
6 changes: 0 additions & 6 deletions lib/wallet/cardano-wallet.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -236,17 +236,13 @@ library
Cardano.Wallet.DB.Sqlite.Schema
Cardano.Wallet.DB.Sqlite.Stores
Cardano.Wallet.DB.Sqlite.Types
Cardano.Wallet.DB.Store.CBOR.Model
Cardano.Wallet.DB.Store.CBOR.Store
Cardano.Wallet.DB.Store.Checkpoints
Cardano.Wallet.DB.Store.Meta.Model
Cardano.Wallet.DB.Store.Meta.Store
Cardano.Wallet.DB.Store.Submissions.Model
Cardano.Wallet.DB.Store.Submissions.Store
Cardano.Wallet.DB.Store.Transactions.Model
Cardano.Wallet.DB.Store.Transactions.Store
Cardano.Wallet.DB.Store.TransactionsWithCBOR.Model
Cardano.Wallet.DB.Store.TransactionsWithCBOR.Store
Cardano.Wallet.DB.Store.Wallets.Model
Cardano.Wallet.DB.Store.Wallets.Store
Cardano.Wallet.DB.WalletState
Expand Down Expand Up @@ -855,8 +851,6 @@ test-suite unit
Cardano.Wallet.DB.Sqlite.StoresSpec
Cardano.Wallet.DB.Sqlite.TypesSpec
Cardano.Wallet.DB.StateMachine
Cardano.Wallet.DB.Store.CBOR.ModelSpec
Cardano.Wallet.DB.Store.CBOR.StoreSpec
Cardano.Wallet.DB.Store.Meta.ModelSpec
Cardano.Wallet.DB.Store.Meta.StoreSpec
Cardano.Wallet.DB.Store.Submissions.ModelSpec
Expand Down
7 changes: 1 addition & 6 deletions lib/wallet/src/Cardano/Wallet/DB/Layer.hs
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,6 @@ import Cardano.Wallet.DB.Sqlite.Schema
)
import Cardano.Wallet.DB.Sqlite.Types
( BlockId (..), TxId (..) )
import Cardano.Wallet.DB.Store.CBOR.Model
( TxCBORSet (..) )
import Cardano.Wallet.DB.Store.Checkpoints
( PersistAddressBook (..), blockHeaderFromEntity, mkStoreWallets )
import Cardano.Wallet.DB.Store.Meta.Model
Expand All @@ -115,8 +113,6 @@ import Cardano.Wallet.DB.Store.Submissions.Model
( TxLocalSubmissionHistory (..) )
import Cardano.Wallet.DB.Store.Transactions.Model
( TxSet (..), decorateTxIns, withdrawals )
import Cardano.Wallet.DB.Store.TransactionsWithCBOR.Model
( TxSetWithCBOR (..) )
import Cardano.Wallet.DB.Store.Wallets.Model
( DeltaWalletsMetaWithSubmissions (..)
, TxWalletsHistory
Expand Down Expand Up @@ -1054,7 +1050,7 @@ selectTxHistory
-> TxWalletsHistory
-> m [W.TransactionInfo]
selectTxHistory tip ti wid minWithdrawal order whichMeta
(TxSetWithCBOR txSet (TxCBORSet txCBORSet), wmetas) = do
(txSet, wmetas) = do
tinfos <- sequence $ do
(TxMetaHistory metas, _) <- maybeToList $ Map.lookup wid wmetas
meta <- toList metas
Expand All @@ -1070,7 +1066,6 @@ selectTxHistory tip ti wid minWithdrawal order whichMeta
ti tip
transaction
decoration
(Map.lookup (txMetaTxId meta) txCBORSet)
meta
pure $ sortTx tinfos
where
Expand Down
45 changes: 0 additions & 45 deletions lib/wallet/src/Cardano/Wallet/DB/Store/CBOR/Model.hs

This file was deleted.

103 changes: 0 additions & 103 deletions lib/wallet/src/Cardano/Wallet/DB/Store/CBOR/Store.hs

This file was deleted.

53 changes: 46 additions & 7 deletions lib/wallet/src/Cardano/Wallet/DB/Store/Transactions/Model.hs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE NamedFieldPuns #-}
{-# LANGUAGE OverloadedLabels #-}
{-# LANGUAGE RecordWildCards #-}
{-# LANGUAGE TupleSections #-}
{-# LANGUAGE TypeFamilies #-}

{- |
Expand Down Expand Up @@ -35,12 +37,14 @@ module Cardano.Wallet.DB.Store.Transactions.Model
-- * Type conversions to wallet types
, fromTxOut
, fromTxCollateralOut
, txCBORPrism
) where

import Prelude

import Cardano.Wallet.DB.Sqlite.Schema
( TxCollateral (..)
( CBOR (..)
, TxCollateral (..)
, TxCollateralOut (..)
, TxCollateralOutToken (..)
, TxIn (..)
Expand All @@ -49,7 +53,7 @@ import Cardano.Wallet.DB.Sqlite.Schema
, TxWithdrawal (..)
)
import Cardano.Wallet.DB.Sqlite.Types
( TxId (TxId) )
( TxId (..) )
import Cardano.Wallet.Primitive.Types.RewardAccount
( RewardAccount )
import Cardano.Wallet.Primitive.Types.TokenMap
Expand All @@ -58,34 +62,48 @@ import Cardano.Wallet.Primitive.Types.TokenPolicy
( TokenName, TokenPolicyId )
import Cardano.Wallet.Primitive.Types.TokenQuantity
( TokenQuantity )
import Cardano.Wallet.Primitive.Types.Tx.Tx
( Tx (txCBOR) )
import Cardano.Wallet.Read.Eras.EraValue
( eraValueSerialize )
import Cardano.Wallet.Read.Tx.CBOR
( TxCBOR )
import Control.Applicative
( (<|>) )
import Control.Arrow
( (&&&) )
( (&&&), (***) )
import Control.Monad
( guard )
import Data.Bifunctor
( bimap )
import Data.ByteString
( ByteString )
import Data.ByteString.Lazy.Char8
( fromStrict, toStrict )
import Data.Delta
( Delta (..) )
import Data.Foldable
( fold )
import Data.Generics.Internal.VL
( view, (^.) )
( Iso', Prism, fromIso, iso, match, prism, view, (^.) )
import Data.List
( find, sortOn )
import Data.Map.Strict
( Map )
import Data.Maybe
( catMaybes )
import Data.Word
( Word32 )
( Word16, Word32 )
import Fmt
( Buildable (build) )
( Buildable (..) )
import GHC.Generics
( Generic )

import qualified Cardano.Wallet.Primitive.Types.Coin as W
import qualified Cardano.Wallet.Primitive.Types.TokenBundle as TokenBundle
import qualified Cardano.Wallet.Primitive.Types.Tx as W
import qualified Cardano.Wallet.Primitive.Types.Tx.Tx as W
import qualified Data.ByteString.Lazy as BL
import qualified Data.Generics.Internal.VL as L
import qualified Data.Map.Strict as Map

{- | A low level definition of a transaction covering all transaction content
Expand All @@ -102,6 +120,7 @@ data TxRelation =
, outs :: [(TxOut, [TxOutToken])]
, collateralOuts :: Maybe (TxCollateralOut, [TxCollateralOutToken])
, withdrawals :: [TxWithdrawal]
, cbor :: Maybe CBOR
}
deriving ( Generic, Eq, Show )

Expand Down Expand Up @@ -247,6 +266,7 @@ mkTxRelation tx =
, collateralOuts = mkTxCollateralOut tid <$> W.collateralOutput tx
, withdrawals =
fmap (mkTxWithdrawal tid) $ Map.toList $ W.withdrawals tx
, cbor = fst . L.build txCBORPrism . (tid,) <$> txCBOR tx
}
where
tid = TxId $ tx ^. #txId
Expand Down Expand Up @@ -351,3 +371,22 @@ decorateTxIns (TxSet relations) TxRelation{ins,collateralIns} =
let collateralOutputIndex = toEnum $ length (outs tx)
guard $ index == collateralOutputIndex -- Babbage leder spec
pure $ fromTxCollateralOut out


type TxCBORRaw = (BL.ByteString, Int)

i :: Iso' (BL.ByteString, Int) (ByteString, Word16)
i = iso (toStrict *** fromIntegral) (fromStrict *** fromIntegral)

toTxCBOR :: (TxId, TxCBOR) -> (CBOR, TxCBORRaw)
toTxCBOR (id', tx) =
let r = L.build eraValueSerialize tx
in (uncurry (CBOR id') $ r ^. i, r)

fromTxCBOR :: CBOR -> Either (CBOR, TxCBORRaw ) (TxId, TxCBOR)
fromTxCBOR s@CBOR {..} = bimap (s ,) (cborTxId ,) $
match eraValueSerialize $ (cborTxCBOR, cborTxEra) ^. fromIso i


txCBORPrism :: Prism CBOR (CBOR, TxCBORRaw) (TxId, TxCBOR) (TxId, TxCBOR)
txCBORPrism = prism toTxCBOR fromTxCBOR

0 comments on commit c18f64d

Please sign in to comment.