Skip to content

Commit

Permalink
Add era parameter to mock Block type
Browse files Browse the repository at this point in the history
  • Loading branch information
HeinrichApfelmus committed Oct 1, 2024
1 parent c49d27f commit 99d15f2
Show file tree
Hide file tree
Showing 8 changed files with 40 additions and 17 deletions.
5 changes: 3 additions & 2 deletions lib/customer-deposit-wallet/src/Cardano/Wallet/Deposit/IO.hs
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ data WalletBootEnv m = WalletBootEnv
-- ^ Logger for the wallet.
, genesisData :: Read.GenesisData
-- ^ Genesis data for the wallet.
, networkEnv :: Network.NetworkEnv m Read.Block
, networkEnv :: Network.NetworkEnv m (Read.EraValue Read.Block)
-- ^ Network environment for the wallet.
}

Expand Down Expand Up @@ -221,7 +221,8 @@ getCustomerHistories
getCustomerHistories a w =
Wallet.getCustomerHistories a <$> readWalletState w

rollForward :: WalletInstance -> NonEmpty Read.Block -> tip -> IO ()
rollForward
:: WalletInstance -> NonEmpty (Read.EraValue Read.Block) -> tip -> IO ()
rollForward w blocks _nodeTip =
onWalletState w
$ Delta.update
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ import qualified Cardano.Wallet.Deposit.Write as Write
------------------------------------------------------------------------------}
newNetworkEnvMock
:: (MonadDelay m, MonadSTM m)
=> m (NetworkEnv m Read.Block)
=> m (NetworkEnv m (Read.Block Read.Conway))
newNetworkEnvMock = do
mchain <- newTVarIO []
mtip <- newTVarIO Read.GenesisPoint
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE NamedFieldPuns #-}
module Cardano.Wallet.Deposit.IO.Network.Type
( NetworkEnv (..)
, mapBlock
, ChainFollower (..)
) where

import Prelude

import Cardano.Wallet.Network
( ChainFollower (..)
, mapChainFollower
)
import Control.Tracer
( Tracer
Expand Down Expand Up @@ -45,6 +48,17 @@ data NetworkEnv m block = NetworkEnv

}

mapBlock
:: Functor m
=> (block1 -> block2)
-> NetworkEnv m block1
-> NetworkEnv m block2
mapBlock f NetworkEnv{chainSync,postTx} = NetworkEnv
{ chainSync = \tr follower ->
chainSync tr (mapChainFollower id id id (fmap f) follower)
, postTx = postTx
}

{-------------------------------------------------------------------------------
Errors
-------------------------------------------------------------------------------}
Expand Down
11 changes: 7 additions & 4 deletions lib/customer-deposit-wallet/src/Cardano/Wallet/Deposit/Pure.hs
Original file line number Diff line number Diff line change
Expand Up @@ -175,11 +175,13 @@ fromXPubAndGenesis xpub knownCustomerCount _ =
getWalletTip :: WalletState -> Read.ChainPoint
getWalletTip = error "getWalletTip"

rollForwardMany :: NonEmpty Read.Block -> WalletState -> WalletState
rollForwardMany
:: NonEmpty (Read.EraValue Read.Block) -> WalletState -> WalletState
rollForwardMany blocks w = foldl' (flip rollForwardOne) w blocks

rollForwardOne :: Read.Block -> WalletState -> WalletState
rollForwardOne block w =
rollForwardOne
:: Read.EraValue Read.Block -> WalletState -> WalletState
rollForwardOne (Read.EraValue block) w =
w
{ utxoHistory = rollForwardUTxO isOurs block (utxoHistory w)
, submissions = Delta.apply (Sbm.rollForward block) (submissions w)
Expand All @@ -189,7 +191,8 @@ rollForwardOne block w =
isOurs = Address.isOurs (addresses w)

rollForwardUTxO
:: (Address -> Bool) -> Read.Block -> UTxOHistory -> UTxOHistory
:: Read.IsEra era
=> (Address -> Bool) -> Read.Block era -> UTxOHistory -> UTxOHistory
rollForwardUTxO isOurs block u =
UTxOHistory.appendBlock slot deltaUTxO u
where
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@ availableUTxO u pending =
--
-- Returns both a delta and the new value.
applyBlock
:: IsOurs Read.Address -> Read.Block -> UTxO -> (DeltaUTxO, UTxO)
:: Read.IsEra era
=> IsOurs Read.Address -> Read.Block era -> UTxO -> (DeltaUTxO, UTxO)
applyBlock isOurs block u0 =
(DeltaUTxO.concat $ reverse dus, u1)
where
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ add = undefined
listInSubmission :: TxSubmissions -> Set Read.Tx
listInSubmission = undefined

rollForward :: Read.Block -> DeltaTxSubmissions
rollForward :: Read.Block era -> DeltaTxSubmissions
rollForward block = [ Sbm.RollForward tip txs ]
where
tip = undefined block
Expand Down
15 changes: 9 additions & 6 deletions lib/customer-deposit-wallet/src/Cardano/Wallet/Deposit/Read.hs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@
--
-- TODO: Match this up with the @Read@ hierarchy.
module Cardano.Wallet.Deposit.Read
( Network (..)
( Read.IsEra
, Read.EraValue (..)
, Read.Conway

, Network (..)
, Read.SlotNo
, Read.ChainPoint (..)
, Slot
Expand Down Expand Up @@ -129,11 +133,10 @@ type TxWitness = ()
type BlockNo = Natural

-- type Block = O.CardanoBlock O.StandardCrypto
data Block = Block
data Block era = Block
{ blockHeader :: BHeader
, transactions :: [Read.Tx Read.Conway]
, transactions :: [Read.Tx era]
}
deriving (Eq, Show)

data BHeader = BHeader
{ blockHeaderBody :: BHBody
Expand All @@ -154,7 +157,7 @@ data BHBody = BHBody
type HashHeader = Read.RawHeaderHash
type HashBBody = ()

getChainPoint :: Block -> Read.ChainPoint
getChainPoint :: Read.IsEra era => Block era -> Read.ChainPoint
getChainPoint block =
Read.BlockPoint
{ Read.slotNo = slot
Expand All @@ -167,7 +170,7 @@ getChainPoint block =
slot = slotNo bhBody

-- | Create a new block from a sequence of transaction.
mockNextBlock :: Read.ChainPoint -> [Read.Tx Read.Conway] -> Block
mockNextBlock :: Read.ChainPoint -> [Read.Tx Read.Conway] -> Block Read.Conway
mockNextBlock old txs =
Block
{ blockHeader = BHeader
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import Cardano.Wallet.Deposit.IO.Network.Mock
)
import Cardano.Wallet.Deposit.IO.Network.Type
( NetworkEnv (..)
, mapBlock
)
import Cardano.Wallet.Deposit.Pure
( BIP32Path
Expand Down Expand Up @@ -68,14 +69,14 @@ assert False = error "Assertion failed!"
-- | Environment for scenarios.
data ScenarioEnv = ScenarioEnv
{ genesisData :: Read.GenesisData
, networkEnv :: NetworkEnv IO Read.Block
, networkEnv :: NetworkEnv IO (Read.EraValue Read.Block)
, faucet :: Faucet
}

-- | Acquire and release a mock environment for a blockchain
withScenarioEnvMock :: (ScenarioEnv -> IO a) -> IO a
withScenarioEnvMock action = do
networkEnv <- newNetworkEnvMock
networkEnv <- mapBlock Read.EraValue <$> newNetworkEnvMock
action
$ ScenarioEnv
{ genesisData = error "TODO: Mock Genesis Data"
Expand Down

0 comments on commit 99d15f2

Please sign in to comment.