Skip to content

Commit

Permalink
[ADP-3344] Use Read.Block instead of mock Block type in Deposit W…
Browse files Browse the repository at this point in the history
…allet (#4802)

This pull request replaces the mock `Block` type in the Deposit Wallet
with the real thing, `Read.Block`.

### Comments

* After this pull request, we can connect the Deposit Wallet to a local
cluster.
* In order to connect to one of the public test networks, we need to
implement the `Testnet` tag in `Address`.

### Issue number

ADP-3344
  • Loading branch information
HeinrichApfelmus authored Oct 1, 2024
2 parents c49d27f + 9b2fd9e commit 66e819c
Show file tree
Hide file tree
Showing 8 changed files with 55 additions and 82 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
13 changes: 8 additions & 5 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,12 +191,13 @@ 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
(deltaUTxO,_) = Balance.applyBlock isOurs block (UTxOHistory.getUTxO u)
slot = Read.slotNo . Read.blockHeaderBody $ Read.blockHeader block
slot = Read.getEraSlotNo $ Read.getEraBHeader block

rollBackward
:: Read.ChainPoint
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,14 @@ 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
(dus, u1) =
mapAccumL' (applyTx isOurs) u0
$ Read.transactions block
$ Read.getEraTransactions block

{-----------------------------------------------------------------------------
Helpers
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
91 changes: 22 additions & 69 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 All @@ -34,13 +38,13 @@ module Cardano.Wallet.Deposit.Read
, TxBody
, TxWitness

, BlockNo
, Block (..)
, getChainPoint
, Read.Block
, Read.getChainPoint
, Read.getEraBHeader
, Read.getEraSlotNo
, Read.getEraTransactions
, mockNextBlock
, BHeader (..)
, Read.mockRawHeaderHash
, BHBody (..)

, GenesisData
, GenesisHash
Expand All @@ -51,6 +55,12 @@ module Cardano.Wallet.Deposit.Read

import Prelude

import Cardano.Wallet.Read.Block.Gen
( mkBlockEra
)
import Cardano.Wallet.Read.Block.Gen.BlockParameters
( BlockParameters (..)
)
import Cardano.Wallet.Read.Chain
( Slot
, WithOrigin (..)
Expand All @@ -71,16 +81,12 @@ import Data.Maybe
import Data.Word
( Word8
)
import Numeric.Natural
( Natural
)

import qualified Cardano.Chain.Genesis as Byron
import qualified Cardano.Wallet.Read as Read
import qualified Data.ByteString as BS
import qualified Data.ByteString.Char8 as B8
import qualified Data.ByteString.Short as SBS
-- import qualified Ouroboros.Consensus.Cardano.Block as O

{-----------------------------------------------------------------------------
Type definitions
Expand Down Expand Up @@ -126,69 +132,16 @@ type TxWitness = ()
{-----------------------------------------------------------------------------
Block
------------------------------------------------------------------------------}
type BlockNo = Natural

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

data BHeader = BHeader
{ blockHeaderBody :: BHBody
, blockHeaderSignature :: Sig
}
deriving (Eq, Ord, Show)

type Sig = ()

data BHBody = BHBody
{ prev :: Maybe HashHeader
, blockno :: BlockNo
, slotNo :: Read.SlotNo
, bhash :: HashBBody
}
deriving (Eq, Ord, Show)

type HashHeader = Read.RawHeaderHash
type HashBBody = ()

getChainPoint :: Block -> Read.ChainPoint
getChainPoint block =
Read.BlockPoint
{ Read.slotNo = slot
, Read.headerHash =
Read.mockRawHeaderHash
$ fromIntegral $ fromEnum slot
}
where
bhBody = blockHeaderBody $ blockHeader 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] -> Read.Block Read.Conway
mockNextBlock old txs =
Block
{ blockHeader = BHeader
{ blockHeaderBody = BHBody
{ prev
, blockno
, slotNo
, bhash = ()
}
, blockHeaderSignature = ()
}
, transactions = txs
}
mkBlockEra BlockParameters{slotNumber,blockNumber,txs}
where
blockno = toEnum $ fromEnum slotNo
slotNo = case old of
Read.GenesisPoint -> 0
blockNumber = Read.BlockNo $ Read.unSlotNo slotNumber
slotNumber = case old of
Read.GenesisPoint -> Read.SlotNo 0
Read.BlockPoint{slotNo = n} -> succ n
prev = case old of
Read.GenesisPoint -> Nothing
Read.BlockPoint{headerHash} -> Just headerHash

{-----------------------------------------------------------------------------
Genesis
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 66e819c

Please sign in to comment.