Skip to content

Commit

Permalink
[ADP-3344] Move mockNextBlock to Cardano.Wallet.Deposit.Read (#4778)
Browse files Browse the repository at this point in the history
This pull request moves functionality that is useful for creating mock
`Block`s for testing from `Cardano.Wallet.Deposit.IO.Network.Mock` to
`Cardano.Wallet.Deposit.Read`. In this way, the functionality can be
used for unit testing the modules `Cardano.Wallet.Deposit.Pure` as well.

### Issue Number

ADP-3344
  • Loading branch information
HeinrichApfelmus authored Sep 19, 2024
2 parents d215d9e + f28d652 commit bfdcdd3
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 57 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,6 @@ import Cardano.Wallet.Deposit.IO.Network.Type
import Cardano.Wallet.Network
( ChainFollower (..)
)
import Cardano.Wallet.Read
( Conway
, Tx
)
import Control.Concurrent.Class.MonadSTM
( MonadSTM
, atomically
Expand Down Expand Up @@ -55,11 +51,11 @@ newNetworkEnvMock
=> m (NetworkEnv m Read.Block)
newNetworkEnvMock = do
mchain <- newTVarIO []
mtip <- newTVarIO genesis
mtip <- newTVarIO Read.GenesisPoint
mfollowers <- newTVarIO []

let registerAndUpdate follower = do
_ <- rollBackward follower genesis
_ <- rollBackward follower Read.GenesisPoint
(chain, tip) <- atomically $ do
modifyTVar mfollowers (follower:)
(,) <$> readTVar mchain <*> readTVar mtip
Expand All @@ -70,8 +66,8 @@ newNetworkEnvMock = do
let forgeBlock tx = atomically $ do
tipOld <- readTVar mtip
let txRead = Write.toConwayTx (Write.mockTxId tipOld) tx
blockNew = mkNextBlock tipOld [txRead]
tipNew = getBlockPoint blockNew
blockNew = Read.mockNextBlock tipOld [txRead]
tipNew = Read.getChainPoint blockNew
writeTVar mtip tipNew
modifyTVar mchain (blockNew:)
pure (blockNew, tipNew)
Expand All @@ -92,37 +88,3 @@ newNetworkEnvMock = do
threadDelay 100
pure $ Right ()
}

genesis :: Read.ChainPoint
genesis = Read.GenesisPoint

getBlockPoint :: Read.Block -> Read.ChainPoint
getBlockPoint block =
Read.BlockPoint
{ Read.slotNo = slot
, Read.headerHash =
Read.mockRawHeaderHash
$ fromIntegral $ fromEnum slot
}
where
bhBody = Read.blockHeaderBody $ Read.blockHeader block
slot = Read.slotNo bhBody

mkNextBlock :: Read.ChainPoint -> [Tx Conway] -> Read.Block
mkNextBlock tipOld txs =
Read.Block
{ Read.blockHeader = Read.BHeader
{ Read.blockHeaderBody = Read.BHBody
{ Read.prev = Nothing
, Read.blockno = toEnum $ fromEnum slotNext
, Read.slotNo = slotNext
, Read.bhash = ()
}
, Read.blockHeaderSignature = ()
}
, Read.transactions = txs
}
where
slotNext = case tipOld of
Read.GenesisPoint -> 1
Read.BlockPoint{slotNo = n} -> succ n
62 changes: 47 additions & 15 deletions lib/customer-deposit-wallet/src/Cardano/Wallet/Deposit/Read.hs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
{-# LANGUAGE BinaryLiterals #-}
{-# LANGUAGE DuplicateRecordFields #-}
{-# LANGUAGE NamedFieldPuns #-}

-- | Indirection module that re-exports types
-- used for reading data from the blockchain,
Expand Down Expand Up @@ -31,6 +33,8 @@ module Cardano.Wallet.Deposit.Read

, BlockNo
, Block (..)
, getChainPoint
, mockNextBlock
, BHeader (..)
, Read.mockRawHeaderHash
, BHBody (..)
Expand All @@ -40,7 +44,6 @@ module Cardano.Wallet.Deposit.Read

-- * Dummy Values useful for testing
, dummyAddress
, dummyBHeader
) where

import Prelude
Expand Down Expand Up @@ -112,6 +115,9 @@ type TxBody = ()

type TxWitness = ()

{-----------------------------------------------------------------------------
Block
------------------------------------------------------------------------------}
type BlockNo = Natural

-- type Block = O.CardanoBlock O.StandardCrypto
Expand All @@ -127,12 +133,6 @@ data BHeader = BHeader
}
deriving (Eq, Ord, Show)

dummyBHeader :: BHeader
dummyBHeader = BHeader
{ blockHeaderBody = dummyBHBody
, blockHeaderSignature = ()
}

type Sig = ()

data BHBody = BHBody
Expand All @@ -143,16 +143,48 @@ data BHBody = BHBody
}
deriving (Eq, Ord, Show)

type HashHeader = ()
type HashHeader = Read.RawHeaderHash
type HashBBody = ()

dummyBHBody :: BHBody
dummyBHBody = BHBody
{ prev = Nothing
, blockno = 128
, slotNo = Read.SlotNo 42
, bhash = ()
}
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 old txs =
Block
{ blockHeader = BHeader
{ blockHeaderBody = BHBody
{ prev
, blockno
, slotNo
, bhash = ()
}
, blockHeaderSignature = ()
}
, transactions = txs
}
where
blockno = toEnum $ fromEnum slotNo
slotNo = case old of
Read.GenesisPoint -> 0
Read.BlockPoint{slotNo = n} -> succ n
prev = case old of
Read.GenesisPoint -> Nothing
Read.BlockPoint{headerHash} -> Just headerHash

{-----------------------------------------------------------------------------
Genesis
------------------------------------------------------------------------------}

-- GenesisData is not part of the ledger specification proper
type GenesisData = Byron.GenesisData
Expand Down

0 comments on commit bfdcdd3

Please sign in to comment.