Skip to content

Commit

Permalink
create dumb database to test migration to new submissions
Browse files Browse the repository at this point in the history
  • Loading branch information
paolino committed Jan 31, 2023
1 parent e0fcd91 commit cad1887
Show file tree
Hide file tree
Showing 3 changed files with 188 additions and 0 deletions.
2 changes: 2 additions & 0 deletions lib/wallet/cardano-wallet.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ library
, lifted-async
, math-functions
, memory
, mmorph
, monad-control
, monad-logger
, MonadRandom
Expand Down Expand Up @@ -226,6 +227,7 @@ library
Cardano.Wallet.DB.Pure.Implementation
Cardano.Wallet.DB.Pure.Layer
Cardano.Wallet.DB.Sqlite.Migration
Cardano.Wallet.DB.Sqlite.Migration.NewSubmissionStore
Cardano.Wallet.DB.Sqlite.Schema
Cardano.Wallet.DB.Sqlite.Stores
Cardano.Wallet.DB.Sqlite.Types
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,185 @@
{-# LANGUAGE BlockArguments #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE OverloadedLists #-}
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE TypeFamilies #-}
{-# OPTIONS_GHC -Wno-orphans #-}

module Cardano.Wallet.DB.Sqlite.Migration.NewSubmissionStore
( createDatabase) where

import Prelude

import Cardano.Wallet.DB.Sqlite.Schema
( LocalTxSubmission (..)
, TxCollateral (..)
, TxCollateralOut (..)
, TxCollateralOutToken (..)
, TxIn (..)
, TxMeta (..)
, TxOut (..)
, TxOutToken (TxOutToken)
, TxWithdrawal (TxWithdrawal)
, Wallet (..)
, migrateAll
)
import Cardano.Wallet.DB.Sqlite.Types
( BlockId (..), TxId (..) )
import Cardano.Wallet.DB.Store.Meta.Model
( TxMetaHistory (TxMetaHistory) )
import Cardano.Wallet.DB.Store.Submissions.Model
( TxLocalSubmissionHistory (TxLocalSubmissionHistory) )
import Cardano.Wallet.DB.Store.Transactions.Model
( TxRelation (TxRelation), TxSet (TxSet) )
import Cardano.Wallet.DB.Store.Wallets.Model
( MetasAndSubmissionsHistory, TxWalletsHistory )
import Cardano.Wallet.DB.Store.Wallets.Store
( mkStoreTxWalletsHistory )
import Cardano.Wallet.Primitive.Types.RewardAccount
( RewardAccount (..) )
import Cardano.Wallet.Primitive.Types.TokenPolicy
( TokenName (..), TokenPolicyId (..) )
import Cardano.Wallet.Primitive.Types.TokenQuantity
( TokenQuantity (..) )
import Cardano.Wallet.Primitive.Types.Tx.SealedTx
( mockSealedTx )
import Control.Monad.Logger
( runStderrLoggingT )
import Control.Monad.Morph
( MFunctor (hoist) )
import Control.Monad.Trans
( MonadIO (liftIO), MonadTrans (lift) )
import Control.Monad.Writer
( MonadWriter (tell), execWriter )
import Crypto.Hash
( hash )
import Data.ByteString.Char8
( ByteString )
import Data.DBVar
( Store (..) )
import Data.Map.Strict
( Map )
import Data.Time
( getCurrentTime )
import Database.Persist.Sql
( SqlPersistT, insert_, runMigration )
import Database.Persist.Sqlite
( runSqlite )

import qualified Cardano.Wallet.Primitive.Types as W
import qualified Cardano.Wallet.Primitive.Types.Address as W
import qualified Cardano.Wallet.Primitive.Types.Coin as W
import qualified Cardano.Wallet.Primitive.Types.Hash as W
import qualified Cardano.Wallet.Primitive.Types.Tx.TxMeta as W
( Direction (Outgoing), TxStatus (..) )

createDatabase :: IO ()
createDatabase = runStderrLoggingT $ runSqlite "" $ do
runMigration migrateAll
liftIO mkWalletEntity >>= insert_
hoist (lift . lift . lift) $ storeAWallet

mkWalletEntity :: IO Wallet
mkWalletEntity = do
ct <- getCurrentTime
pure $ Wallet
{ walId = wid "a"
, walName = "test"
, walCreationTime = ct
, walPassphraseLastUpdatedAt = Nothing
, walPassphraseScheme = Nothing
, walGenesisHash = BlockId $ W.Hash ""
, walGenesisStart = ct
}

storeAWallet :: SqlPersistT IO ()
storeAWallet = writeS mkStoreTxWalletsHistory history

history :: TxWalletsHistory
history = (txSet, txMetaAndSubmissions)
where
txSet :: TxSet
txSet = TxSet $
execWriter $ do
mkRelation "0" dumbTxRelation
mkRelation "1" dumbTxRelation
mkRelation "2" dumbTxRelation
txMetaAndSubmissions :: Map W.WalletId MetasAndSubmissionsHistory
txMetaAndSubmissions =
[ ( wid',
(txMetaHistory, txLocalSubmissionHistory)
)
]
where
txMetaHistory = TxMetaHistory $
execWriter $ do
mkRelation "0" $ dumbMetaRelation W.InLedger
mkRelation "1" $ dumbMetaRelation W.Pending
mkRelation "2" $ dumbMetaRelation W.Expired
txLocalSubmissionHistory = TxLocalSubmissionHistory $
execWriter $ do
mkRelation "1" $ dumbLocalSubmissionRelation wid'
mkRelation "2" $ dumbLocalSubmissionRelation wid'
wid' = wid "a"

mkTxId :: ByteString -> TxId
mkTxId = TxId . W.Hash

mkRelation :: MonadWriter (Map TxId r) m
=> ByteString -> (TxId -> r) -> m ()
mkRelation txId r = tell ([(txId', r txId')] )
where txId' = mkTxId txId

wid :: ByteString -> W.WalletId
wid = W.WalletId . hash @ByteString

novalue :: W.Coin
novalue = W.Coin 0

dumbTxRelation :: TxId -> TxRelation
dumbTxRelation id' =
TxRelation
[TxIn id' 0 (mkTxId "1") 0 novalue]
[TxCollateral id' 0 (mkTxId "1") 0 novalue]
[ ( TxOut id' 0 (W.Address "addr") novalue
, [TxOutToken id' 0
(UnsafeTokenPolicyId (W.Hash "0"))
(UnsafeTokenName "token")
(TokenQuantity 42)
]
)
]
(Just ( TxCollateralOut id' (W.Address "addr") novalue
, [TxCollateralOutToken id'
(UnsafeTokenPolicyId (W.Hash "0"))
(UnsafeTokenName "token")
(TokenQuantity 42)
]
)
)
[ TxWithdrawal id' novalue ( RewardAccount "rw") ]
Nothing

dumbMetaRelation :: W.TxStatus -> TxId -> TxMeta
dumbMetaRelation status id' =
TxMeta
id'
(wid "a")
status
W.Outgoing
1000
50
novalue
Nothing
Nothing
Nothing
Nothing

dumbLocalSubmissionRelation :: W.WalletId -> TxId -> LocalTxSubmission
dumbLocalSubmissionRelation wid' id' =
LocalTxSubmission
id'
wid'
999
(mockSealedTx "")
1 change: 1 addition & 0 deletions lib/wallet/src/Cardano/Wallet/DB/Store/Wallets/Model.hs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ module Cardano.Wallet.DB.Store.Wallets.Model
, DeltaWalletsMetaWithSubmissions (..)
, TxWalletsHistory
, walletsLinkedTransactions
, MetasAndSubmissionsHistory
) where

import Prelude
Expand Down

0 comments on commit cad1887

Please sign in to comment.