/
Types.hs
172 lines (155 loc) · 5.18 KB
/
Types.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE TypeApplications #-}
module Cardano.Wallet.DummyTarget.Primitive.Types
( -- * Dummy values
block0
, dummyNetworkParameters
, dummyGenesisParameters
, dummySlottingParameters
, dummyTimeInterpreter
, genesisHash
, mockHash
, mkTxId
, mkTx
-- * Mocks
, dummyNetworkLayer
) where
import Prelude
import Cardano.Wallet.Network
( NetworkLayer (..) )
import Cardano.Wallet.Primitive.Slotting
( TimeInterpreter, hoistTimeInterpreter, mkSingleEraInterpreter )
import Cardano.Wallet.Primitive.Types
( ActiveSlotCoefficient (..)
, Block (..)
, BlockHeader (..)
, EpochLength (..)
, FeePolicy (..)
, GenesisParameters (..)
, NetworkParameters (..)
, ProtocolParameters (..)
, SlotLength (..)
, SlotNo (..)
, SlottingParameters (..)
, StartTime (..)
, TxParameters (..)
, emptyEraInfo
)
import Cardano.Wallet.Primitive.Types.Coin
( Coin (..) )
import Cardano.Wallet.Primitive.Types.Hash
( Hash (..) )
import Cardano.Wallet.Primitive.Types.RewardAccount
( RewardAccount (..) )
import Cardano.Wallet.Primitive.Types.Tx
( Tx (..), TxIn (..), TxMetadata (..), TxOut (..) )
import Crypto.Hash
( Blake2b_256, hash )
import Data.ByteString
( ByteString )
import Data.Coerce
( coerce )
import Data.Functor.Identity
( Identity (..) )
import Data.Map.Strict
( Map )
import Data.Quantity
( Quantity (..) )
import Data.Time.Clock.POSIX
( posixSecondsToUTCTime )
import qualified Data.ByteArray as BA
import qualified Data.ByteString.Char8 as B8
genesisHash :: Hash "Genesis"
genesisHash = Hash (B8.replicate 32 '0')
block0 :: Block
block0 = Block
{ header = BlockHeader
{ slotNo = SlotNo 0
, blockHeight = Quantity 0
, headerHash = mockHash $ SlotNo 0
, parentHeaderHash = coerce genesisHash
}
, transactions = []
, delegations = []
}
dummyGenesisParameters :: GenesisParameters
dummyGenesisParameters = GenesisParameters
{ getGenesisBlockHash = genesisHash
, getGenesisBlockDate = StartTime $ posixSecondsToUTCTime 0
}
dummySlottingParameters :: SlottingParameters
dummySlottingParameters = SlottingParameters
{ getSlotLength = SlotLength 1
, getEpochLength = EpochLength 21600
, getActiveSlotCoefficient = ActiveSlotCoefficient 1
, getSecurityParameter = Quantity 2160
}
dummyTimeInterpreter :: Monad m => TimeInterpreter m
dummyTimeInterpreter = hoistTimeInterpreter (pure . runIdentity)
$ mkSingleEraInterpreter
(getGenesisBlockDate dummyGenesisParameters)
dummySlottingParameters
dummyTxParameters :: TxParameters
dummyTxParameters = TxParameters
{ getFeePolicy = LinearFee (Quantity 14) (Quantity 42)
, getTxMaxSize = Quantity 8192
}
dummyNetworkParameters :: NetworkParameters
dummyNetworkParameters = NetworkParameters
{ genesisParameters = dummyGenesisParameters
, slottingParameters = dummySlottingParameters
, protocolParameters = dummyProtocolParameters
}
dummyProtocolParameters :: ProtocolParameters
dummyProtocolParameters = ProtocolParameters
{ decentralizationLevel = minBound
, txParameters = dummyTxParameters
, desiredNumberOfStakePools = 100
, minimumUTxOvalue = Coin 0
, stakeKeyDeposit = Coin 0
, eras = emptyEraInfo
}
-- | Construct a @Tx@, computing its hash using the dummy @mkTxId@.
mkTx
:: Maybe Coin
-> [(TxIn, Coin)]
-> [TxOut]
-> Map RewardAccount Coin
-> Maybe TxMetadata
-> Tx
mkTx fees ins outs wdrls md =
Tx (mkTxId ins outs wdrls md) fees ins outs wdrls md
-- | txId calculation for testing purposes.
mkTxId
:: [(TxIn, Coin)]
-> [TxOut]
-> Map RewardAccount Coin
-> Maybe TxMetadata -> Hash "Tx"
mkTxId ins outs wdrls md = mockHash (ins, outs, wdrls, md)
-- | Construct a good-enough hash for testing
mockHash :: Show a => a -> Hash whatever
mockHash = Hash . blake2b256 . B8.pack . show
where
blake2b256 :: ByteString -> ByteString
blake2b256 =
BA.convert . hash @_ @Blake2b_256
dummyNetworkLayer :: NetworkLayer m a
dummyNetworkLayer = NetworkLayer
{ nextBlocks = error "nextBlocks: not implemented"
, initCursor = error "initCursor: not implemented"
, destroyCursor = error "destroyCursor: not implemented"
, cursorSlotNo = error "cursorSlotNo: not implemented"
, currentNodeEra = error "currentNodeEra: not implemented"
, currentNodeTip = error "currentNodeTip: not implemented"
, watchNodeTip = error "watchNodeTip: not implemented"
, currentProtocolParameters = error "currentProtocolParameters: not implemented"
, currentSlottingParameters = error "currentSlottingParameters: not implemented"
, postTx = error "postTx: not implemented"
, stakeDistribution = error "stakeDistribution: not implemented"
, getCachedRewardAccountBalance = error "getRewardCachedAccountBalance: not implemented"
, fetchRewardAccountBalances = error "fetchRewardAccountBalances: not implemented"
, timeInterpreter = error "timeInterpreter: not implemented"
, syncProgress = error "syncProgress: not implemented"
}