Skip to content

Commit ccdf443

Browse files
Use Read.ChainPoint in Cardano.Wallet.Deposit.Read
1 parent 9e91ac5 commit ccdf443

File tree

7 files changed

+78
-41
lines changed

7 files changed

+78
-41
lines changed

lib/customer-deposit-wallet/customer-deposit-wallet.cabal

+1
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,7 @@ test-suite unit
156156
, base
157157
, bytestring
158158
, cardano-crypto
159+
, cardano-wallet-read
159160
, cardano-wallet-test-utils
160161
, customer-deposit-wallet
161162
, customer-deposit-wallet:customer-deposit-wallet-http

lib/customer-deposit-wallet/data/swagger.json

+7
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,13 @@
1515
},
1616
{
1717
"properties": {
18+
"header_hash": {
19+
"description": "Hash (Blake2b_256) of a block header.",
20+
"format": "hex",
21+
"maxLength": 64,
22+
"minLength": 64,
23+
"type": "string"
24+
},
1825
"slot_no": {
1926
"maximum": 1.8446744073709551615e19,
2027
"minimum": 0,

lib/customer-deposit-wallet/http/Cardano/Wallet/Deposit/HTTP/Types/JSON.hs

+20-18
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
{-# LANGUAGE DerivingVia #-}
22
{-# LANGUAGE FlexibleInstances #-}
3+
{-# LANGUAGE NamedFieldPuns #-}
34
{-# LANGUAGE StandaloneDeriving #-}
45
{-# LANGUAGE TypeApplications #-}
56

@@ -37,8 +38,6 @@ import Cardano.Wallet.Deposit.Pure
3738
import Cardano.Wallet.Deposit.Read
3839
( Address
3940
, ChainPoint (..)
40-
, fromSlot
41-
, toSlot
4241
)
4342
import Control.Applicative
4443
( (<|>)
@@ -80,17 +79,12 @@ import Data.Text.Class
8079
, ToText (..)
8180
, getTextDecodingError
8281
)
83-
import Data.Word
84-
( Word64
85-
)
86-
import Numeric.Natural
87-
( Natural
88-
)
8982
import Servant
9083
( FromHttpApiData (..)
9184
)
9285

9386
import qualified Cardano.Wallet.Read as Read
87+
import qualified Cardano.Wallet.Read.Hash as Hash
9488
import qualified Data.Text as T
9589
import qualified Data.Text.Encoding as T
9690

@@ -192,21 +186,29 @@ instance ToSchema (ApiT CustomerList) where
192186
customerListSchema
193187

194188
instance ToJSON (ApiT ChainPoint) where
195-
toJSON (ApiT Origin) = "genesis"
196-
toJSON (ApiT (At sl)) = object
197-
[ "slot_no" .= toJSON (fromIntegral @Natural @Word64 $ fromSlot sl)
189+
toJSON (ApiT Read.GenesisPoint) = "genesis"
190+
toJSON (ApiT (Read.BlockPoint{slotNo,headerHash})) = object
191+
[ "slot_no" .=
192+
Read.unSlotNo slotNo
193+
, "header_hash" .=
194+
Hash.hashToTextAsHex headerHash
198195
]
199196

200197
instance FromJSON (ApiT ChainPoint) where
201198
parseJSON payload = parseOrigin payload <|> parseSlot payload
202199
where
203-
parseOrigin = withText "genesis" $ \txt ->
204-
if txt == "genesis" then
205-
pure $ ApiT Origin
206-
else
207-
fail "'origin' is expected."
208-
parseSlot = withObject "slot no" $ \obj ->
209-
ApiT . At . toSlot <$> obj .: "slot_no"
200+
parseOrigin = withText "genesis" $ \txt ->
201+
if txt == "genesis"
202+
then pure $ ApiT Read.GenesisPoint
203+
else fail "'genesis' is expected."
204+
parseSlot = withObject "slot_no" $ \obj -> do
205+
slotNo <- Read.SlotNo <$> obj .: "slot_no"
206+
headerHashText <- obj .: "header_hash"
207+
headerHash <-
208+
case Hash.hashFromTextAsHex headerHashText of
209+
Nothing -> fail "invalid 'header_hash'"
210+
Just hash -> pure hash
211+
pure $ ApiT Read.BlockPoint{slotNo,headerHash}
210212

211213
instance ToSchema (ApiT ChainPoint) where
212214
declareNamedSchema _ = do

lib/customer-deposit-wallet/http/Cardano/Wallet/Deposit/HTTP/Types/OpenAPI.hs

+18-2
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,9 @@ import Data.OpenApi
3939
, HasInfo (..)
4040
, HasItems (..)
4141
, HasLicense (license)
42+
, HasMaxLength (..)
4243
, HasMaximum (..)
44+
, HasMinLength (..)
4345
, HasMinimum (..)
4446
, HasName (..)
4547
, HasOneOf (..)
@@ -217,12 +219,26 @@ chainPointAtSlotSchema =
217219
mempty
218220
& type_ ?~ OpenApiObject
219221
& properties
220-
.~ [ ("slot_no", Inline slotSchema)
221-
]
222+
.~ [ ("slot_no", Inline slotSchema)
223+
, ("header_hash", Inline headerHashSchema)
224+
]
222225

223226
slotSchema :: Schema
224227
slotSchema =
225228
mempty
226229
& type_ ?~ OpenApiInteger
227230
& minimum_ ?~ 0
228231
& maximum_ ?~ fromIntegral (maxBound :: Word64)
232+
233+
headerHashSchema :: Schema
234+
headerHashSchema =
235+
blake2b_256Schema
236+
& description ?~ "Hash (Blake2b_256) of a block header."
237+
238+
blake2b_256Schema :: Schema
239+
blake2b_256Schema =
240+
mempty
241+
& type_ ?~ OpenApiString
242+
& format ?~ "hex"
243+
& minLength ?~ (2*32)
244+
& maxLength ?~ (2*32)

lib/customer-deposit-wallet/src/Cardano/Wallet/Deposit/IO/Network/Mock.hs

+14-4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
{-# LANGUAGE DuplicateRecordFields #-}
12
{-|
23
Copyright: © 2024 Cardano Foundation
34
License: Apache-2.0
@@ -93,10 +94,19 @@ newNetworkEnvMock = do
9394
}
9495

9596
genesis :: Read.ChainPoint
96-
genesis = Read.Origin
97+
genesis = Read.GenesisPoint
9798

9899
getBlockPoint :: Read.Block -> Read.ChainPoint
99-
getBlockPoint = Read.At . Read.slotNo . Read.blockHeaderBody . Read.blockHeader
100+
getBlockPoint block =
101+
Read.BlockPoint
102+
{ Read.slotNo = slot
103+
, Read.headerHash =
104+
Read.mockRawHeaderHash
105+
$ fromIntegral $ fromEnum slot
106+
}
107+
where
108+
bhBody = Read.blockHeaderBody $ Read.blockHeader block
109+
slot = Read.slotNo bhBody
100110

101111
mkNextBlock :: Read.ChainPoint -> [Tx Conway] -> Read.Block
102112
mkNextBlock tipOld txs =
@@ -114,5 +124,5 @@ mkNextBlock tipOld txs =
114124
}
115125
where
116126
slotNext = case tipOld of
117-
Read.Origin -> 1
118-
Read.At n -> succ n
127+
Read.GenesisPoint -> 1
128+
Read.BlockPoint{slotNo = n} -> succ n

lib/customer-deposit-wallet/src/Cardano/Wallet/Deposit/Read.hs

+2-8
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,7 @@
88
module Cardano.Wallet.Deposit.Read
99
( Network (..)
1010
, Read.SlotNo
11-
, toSlot
12-
, fromSlot
13-
, ChainPoint (..)
11+
, Read.ChainPoint (..)
1412

1513
, Address
1614
, KeyHash
@@ -34,6 +32,7 @@ module Cardano.Wallet.Deposit.Read
3432
, BlockNo
3533
, Block (..)
3634
, BHeader (..)
35+
, Read.mockRawHeaderHash
3736
, BHBody (..)
3837

3938
, GenesisData
@@ -78,11 +77,6 @@ import qualified Data.ByteString.Short as SBS
7877
------------------------------------------------------------------------------}
7978
data Network = Testnet | Mainnet
8079

81-
data ChainPoint
82-
= Origin
83-
| At Read.SlotNo
84-
deriving (Eq, Ord, Show)
85-
8680
-- | Synonym for readability.
8781
-- The ledger specifications define @Addr@.
8882
-- Byron addresses are represented by @Addr_bootstrap@.

lib/customer-deposit-wallet/test/unit/Cardano/Wallet/Deposit/HTTP/JSON/JSONSpec.hs

+16-9
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ import Cardano.Wallet.Deposit.Pure
3030
)
3131
import Cardano.Wallet.Deposit.Read
3232
( mkEnterpriseAddress
33-
, toSlot
3433
)
3534
import Data.Aeson
3635
( FromJSON (..)
@@ -50,9 +49,6 @@ import Data.OpenApi
5049
import Data.Word
5150
( Word64
5251
)
53-
import Numeric.Natural
54-
( Natural
55-
)
5652
import Test.Hspec
5753
( Expectation
5854
, Spec
@@ -68,14 +64,16 @@ import Test.QuickCheck
6864
, arbitrarySizedBoundedIntegral
6965
, chooseInt
7066
, counterexample
67+
, elements
7168
, forAll
72-
, oneof
69+
, frequency
7370
, property
7471
, shrinkIntegral
7572
, vectorOf
7673
, (===)
7774
)
7875

76+
import qualified Cardano.Wallet.Read as Read
7977
import qualified Data.ByteString as BS
8078
import qualified Data.ByteString.Lazy.Char8 as BL
8179
import qualified Data.List as L
@@ -155,10 +153,19 @@ genApiTCustomerList = do
155153
pure $ ApiT $ uniqueAddr $ uniqueCustomer vectors
156154

157155
genApiTChainPoint :: Gen (ApiT ChainPoint)
158-
genApiTChainPoint = oneof
159-
[ pure . ApiT $ Origin
160-
, ApiT . At . toSlot . fromIntegral @Word64 @Natural <$> arbitrary
161-
]
156+
genApiTChainPoint = ApiT <$> genChainPoint
157+
158+
genChainPoint :: Gen Read.ChainPoint
159+
genChainPoint = frequency
160+
[ ( 1, pure Read.GenesisPoint) -- "common" but not "very common"
161+
, (40, Read.BlockPoint <$> genReadSlotNo <*> genHeaderHash)
162+
]
163+
where
164+
genReadSlotNo = Read.SlotNo . fromIntegral <$> (arbitrary :: Gen Word64)
165+
genHeaderHash = elements mockHashes
166+
167+
mockHashes :: [Read.RawHeaderHash]
168+
mockHashes = map Read.mockRawHeaderHash [0..2]
162169

163170
instance Arbitrary (ApiT Address) where
164171
arbitrary = genApiTAddress

0 commit comments

Comments
 (0)