Skip to content

Commit d4736bc

Browse files
Merge #1854
1854: assign correct network discrimination to withdrawals r=KtorZ a=KtorZ # Issue Number <!-- Put here a reference to the issue this PR relates to and which requirements it tackles --> #1821 # Overview <!-- Detail in a few bullet points the work accomplished in this PR --> - [ ] I have assigned correct network discrimination to withdrawals. This was hard-coded to mainnet, for some reasons. # Comments <!-- Additional comments or screenshots to attach if any --> <!-- Don't forget to: ✓ Self-review your changes to make sure nothing unexpected slipped through ✓ Assign yourself to the PR ✓ Assign one or several reviewer(s) ✓ Once created, link this PR to its corresponding ticket ✓ Assign the PR to a corresponding milestone ✓ Acknowledge any changes required to the Wiki --> Co-authored-by: KtorZ <matthias.benkort@gmail.com>
2 parents 8078cf0 + 1f3f854 commit d4736bc

File tree

6 files changed

+74
-35
lines changed

6 files changed

+74
-35
lines changed

lib/core/src/Cardano/Wallet/Primitive/AddressDerivation.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -472,7 +472,7 @@ instance MonadRandom ((->) (Passphrase "salt")) where
472472
-------------------------------------------------------------------------------}
473473

474474
-- | Available network options.
475-
data NetworkDiscriminant = Mainnet | Testnet Nat
475+
data NetworkDiscriminant = Mainnet | Testnet Nat deriving Typeable
476476

477477
class NetworkDiscriminantVal (n :: NetworkDiscriminant) where
478478
networkDiscriminantVal :: Text

lib/shelley/src/Cardano/Wallet/Shelley.hs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,8 @@ import System.Exit
153153
( ExitCode (..) )
154154
import System.IOManager
155155
( withIOManager )
156+
import Type.Reflection
157+
( Typeable )
156158

157159
import qualified Cardano.Pool.DB.Sqlite as Pool
158160
import qualified Cardano.Wallet.Api.Server as Server
@@ -172,6 +174,7 @@ data SomeNetworkDiscriminant where
172174
, DelegationAddress n ShelleyKey
173175
, DecodeAddress n
174176
, EncodeAddress n
177+
, Typeable n
175178
)
176179
=> Proxy n
177180
-> SomeNetworkDiscriminant

lib/shelley/src/Cardano/Wallet/Shelley/Compatibility.hs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ module Cardano.Wallet.Shelley.Compatibility
7777
, fromTip
7878
, fromTip'
7979
, fromPParams
80+
, fromNetworkDiscriminant
8081

8182
-- * Internal Conversions
8283
, decentralizationLevelFromPParams
@@ -132,12 +133,16 @@ import Data.Map.Strict
132133
( Map )
133134
import Data.Maybe
134135
( fromMaybe, isJust, mapMaybe )
136+
import Data.Proxy
137+
( Proxy )
135138
import Data.Quantity
136139
( Percentage, Quantity (..), mkPercentage )
137140
import Data.Text
138141
( Text )
139142
import Data.Text.Class
140143
( TextDecodingError (..) )
144+
import Data.Type.Equality
145+
( testEquality )
141146
import Data.Word
142147
( Word16, Word32, Word64 )
143148
import Fmt
@@ -175,6 +180,8 @@ import Ouroboros.Network.Point
175180
( WithOrigin (..) )
176181
import Shelley.Spec.Ledger.BaseTypes
177182
( strictMaybeToMaybe, urlToText )
183+
import Type.Reflection
184+
( Typeable, typeRep )
178185

179186
import qualified Cardano.Api as Cardano
180187
import qualified Cardano.Byron.Codec.Cbor as CBOR
@@ -741,6 +748,15 @@ fromUnitInterval x =
741748
, show x
742749
]
743750

751+
fromNetworkDiscriminant
752+
:: forall (n :: NetworkDiscriminant). (Typeable n)
753+
=> Proxy n
754+
-> SL.Network
755+
fromNetworkDiscriminant _ =
756+
case testEquality (typeRep @n) (typeRep @'Mainnet) of
757+
Just{} -> SL.Mainnet
758+
Nothing -> SL.Testnet
759+
744760
-- NOTE: Arguably breaks naming conventions. Perhaps fromCardanoSignedTx instead
745761
toSealed :: SL.Tx TPraosStandardCrypto -> (W.Tx, W.SealedTx)
746762
toSealed tx =

lib/shelley/src/Cardano/Wallet/Shelley/Transaction.hs

Lines changed: 36 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ import Cardano.Wallet.Primitive.Types
7373
import Cardano.Wallet.Shelley.Compatibility
7474
( Shelley
7575
, TPraosStandardCrypto
76+
, fromNetworkDiscriminant
7677
, toCardanoLovelace
7778
, toCardanoTxIn
7879
, toCardanoTxOut
@@ -111,6 +112,8 @@ import Ouroboros.Consensus.Shelley.Protocol.Crypto
111112
( Crypto (..) )
112113
import Ouroboros.Network.Block
113114
( SlotNo )
115+
import Type.Reflection
116+
( Typeable )
114117

115118
import qualified Cardano.Api as Cardano
116119
import qualified Cardano.Byron.Codec.Cbor as CBOR
@@ -157,17 +160,19 @@ emptyTxPayload :: Crypto c => TxPayload c
157160
emptyTxPayload = TxPayload mempty mempty
158161

159162
mkTx
160-
:: WalletKey k
161-
=> TxPayload TPraosStandardCrypto
163+
:: forall (n :: NetworkDiscriminant) k. (Typeable n, WalletKey k)
164+
=> Proxy n
165+
-> TxPayload TPraosStandardCrypto
162166
-> SlotNo
163167
-- ^ Time to Live
164168
-> (k 'AddressK XPrv, Passphrase "encryption")
165169
-- ^ Reward account
166170
-> (Address -> Maybe (k 'AddressK XPrv, Passphrase "encryption"))
167171
-> CoinSelection
168172
-> Either ErrMkTx (Tx, SealedTx)
169-
mkTx (TxPayload certs mkExtraWits) timeToLive (rewardAcnt, pwdAcnt) keyFrom cs = do
173+
mkTx proxy (TxPayload certs mkExtraWits) timeToLive (rewardAcnt, pwdAcnt) keyFrom cs = do
170174
let withdrawals = mkWithdrawals
175+
proxy
171176
(toChimericAccountRaw . getRawKey . publicKey $ rewardAcnt)
172177
(withdrawal cs)
173178

@@ -193,24 +198,25 @@ newTransactionLayer
193198
:: forall (n :: NetworkDiscriminant) k t.
194199
( t ~ IO Shelley
195200
, WalletKey k
201+
, Typeable n
196202
)
197203
=> Proxy n
198204
-> ProtocolMagic
199205
-> EpochLength
200206
-> TransactionLayer t k
201-
newTransactionLayer _proxy _protocolMagic epochLength = TransactionLayer
202-
{ mkStdTx = \acc ks tip -> mkTx emptyTxPayload (defaultTTL epochLength tip) acc ks
207+
newTransactionLayer proxy _protocolMagic epochLength = TransactionLayer
208+
{ mkStdTx = \acc ks tip ->
209+
mkTx proxy emptyTxPayload (defaultTTL epochLength tip) acc ks
203210
, initDelegationSelection = _initDelegationSelection
204211
, mkDelegationJoinTx = _mkDelegationJoinTx
205212
, mkDelegationQuitTx = _mkDelegationQuitTx
206213
, decodeSignedTx = _decodeSignedTx
207-
, minimumFee = _minimumFee
208-
, estimateMaxNumberOfInputs = _estimateMaxNumberOfInputs
214+
, minimumFee = _minimumFee proxy
215+
, estimateMaxNumberOfInputs = _estimateMaxNumberOfInputs proxy
209216
, validateSelection = const $ return ()
210217
, allowUnbalancedTx = True
211218
}
212219
where
213-
214220
_initDelegationSelection
215221
:: FeePolicy
216222
-- Current fee policy
@@ -254,7 +260,7 @@ newTransactionLayer _proxy _protocolMagic epochLength = TransactionLayer
254260

255261
let payload = TxPayload certs mkWits
256262
let ttl = defaultTTL epochLength tip
257-
mkTx payload ttl acc keyFrom cs
263+
mkTx proxy payload ttl acc keyFrom cs
258264

259265
_mkDelegationQuitTx
260266
:: (k 'AddressK XPrv, Passphrase "encryption")
@@ -277,15 +283,17 @@ newTransactionLayer _proxy _protocolMagic epochLength = TransactionLayer
277283

278284
let payload = TxPayload certs mkWits
279285
let ttl = defaultTTL epochLength tip
280-
mkTx payload ttl acc keyFrom cs
286+
mkTx proxy payload ttl acc keyFrom cs
281287

282288
_estimateMaxNumberOfInputs
283-
:: Quantity "byte" Word16
289+
:: forall (n :: NetworkDiscriminant). Typeable n
290+
=> Proxy n
291+
-> Quantity "byte" Word16
284292
-- ^ Transaction max size in bytes
285293
-> Word8
286294
-- ^ Number of outputs in transaction
287295
-> Word8
288-
_estimateMaxNumberOfInputs (Quantity maxSize) nOuts =
296+
_estimateMaxNumberOfInputs proxy (Quantity maxSize) nOuts =
289297
fromIntegral $ bisect (lowerBound, upperBound)
290298
where
291299
bisect (!inf, !sup)
@@ -306,7 +314,7 @@ _estimateMaxNumberOfInputs (Quantity maxSize) nOuts =
306314

307315
isTooBig nInps = size > fromIntegral maxSize
308316
where
309-
size = computeTxSize Nothing sel
317+
size = computeTxSize proxy Nothing sel
310318
sel = dummyCoinSel nInps (fromIntegral nOuts)
311319

312320
dummyCoinSel :: Int -> Int -> CoinSelection
@@ -346,12 +354,14 @@ _decodeSignedTx bytes = do
346354
Left $ ErrDecodeSignedTxWrongPayload (Cardano.renderApiError apiErr)
347355

348356
_minimumFee
349-
:: FeePolicy
357+
:: forall (n :: NetworkDiscriminant). Typeable n
358+
=> Proxy (n :: NetworkDiscriminant)
359+
-> FeePolicy
350360
-> Maybe DelegationAction
351361
-> CoinSelection
352362
-> Fee
353-
_minimumFee policy action cs =
354-
computeFee $ computeTxSize action cs
363+
_minimumFee proxy policy action cs =
364+
computeFee $ computeTxSize proxy action cs
355365
where
356366
computeFee :: Integer -> Fee
357367
computeFee size =
@@ -360,10 +370,12 @@ _minimumFee policy action cs =
360370
LinearFee (Quantity a) (Quantity b) _unused = policy
361371

362372
computeTxSize
363-
:: Maybe DelegationAction
373+
:: forall (n :: NetworkDiscriminant). Typeable n
374+
=> Proxy (n :: NetworkDiscriminant)
375+
-> Maybe DelegationAction
364376
-> CoinSelection
365377
-> Integer
366-
computeTxSize action cs =
378+
computeTxSize proxy action cs =
367379
SL.txsize $ SL.Tx unsigned wits metadata
368380
where
369381
metadata = SL.SNothing
@@ -397,6 +409,7 @@ computeTxSize action cs =
397409
dummyKeyHashRaw = BS.pack (replicate 28 0)
398410

399411
withdrawals = mkWithdrawals
412+
proxy
400413
(ChimericAccount dummyKeyHashRaw)
401414
(withdrawal cs)
402415

@@ -459,20 +472,21 @@ mkUnsignedTx ttl cs withdrawals certs =
459472
unsigned
460473

461474
mkWithdrawals
462-
:: ChimericAccount
475+
:: forall (n :: NetworkDiscriminant). (Typeable n)
476+
=> Proxy n
477+
-> ChimericAccount
463478
-> Word64
464479
-> Map (SL.RewardAcnt TPraosStandardCrypto) SL.Coin
465-
mkWithdrawals (ChimericAccount keyHash) amount
480+
mkWithdrawals proxy (ChimericAccount keyHash) amount
466481
| amount == 0 = mempty
467482
| otherwise = Map.fromList
468-
[ ( SL.RewardAcnt SL.Mainnet keyHashObj
483+
[ ( SL.RewardAcnt (fromNetworkDiscriminant proxy) keyHashObj
469484
, SL.Coin $ fromIntegral amount
470485
)
471486
]
472487
where
473488
keyHashObj = SL.KeyHashObj $ SL.KeyHash $ Hash.UnsafeHash keyHash
474489

475-
476490
-- TODO: The SlotId-SlotNo conversion based on epoch length would not
477491
-- work if the epoch length changed in a hard fork.
478492

lib/shelley/test/integration/Main.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,7 @@ mkFeeEstimator policy = \case
266266
mempty { inputs = inps, change = outs }
267267

268268
computeFee selection action =
269-
fromIntegral $ getFee $ _minimumFee policy action selection
269+
fromIntegral $ getFee $ _minimumFee (Proxy @'Mainnet) policy action selection
270270

271271
{-------------------------------------------------------------------------------
272272
Logging

lib/shelley/test/unit/Cardano/Wallet/Shelley/TransactionSpec.hs

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -113,14 +113,15 @@ spec = do
113113
prop "roundtrip" prop_decodeSignedTxRoundtrip
114114

115115
describe "estimateMaxNumberOfInputs" $ do
116+
let proxy = Proxy @'Mainnet
116117
it "order of magnitude, nOuts = 1" $
117-
_estimateMaxNumberOfInputs (Quantity 4096) 1 `shouldBe` 27
118+
_estimateMaxNumberOfInputs proxy (Quantity 4096) 1 `shouldBe` 27
118119
it "order of magnitude, nOuts = 10" $
119-
_estimateMaxNumberOfInputs (Quantity 4096) 10 `shouldBe` 19
120+
_estimateMaxNumberOfInputs proxy (Quantity 4096) 10 `shouldBe` 19
120121
it "order of magnitude, nOuts = 20" $
121-
_estimateMaxNumberOfInputs (Quantity 4096) 20 `shouldBe` 10
122+
_estimateMaxNumberOfInputs proxy (Quantity 4096) 20 `shouldBe` 10
122123
it "order of magnitude, nOuts = 30" $
123-
_estimateMaxNumberOfInputs (Quantity 4096) 30 `shouldBe` 1
124+
_estimateMaxNumberOfInputs proxy (Quantity 4096) 30 `shouldBe` 1
124125

125126
prop "more outputs ==> less inputs" prop_moreOutputsMeansLessInputs
126127
prop "less outputs ==> more inputs" prop_lessOutputsMeansMoreInputs
@@ -187,9 +188,11 @@ prop_moreOutputsMeansLessInputs
187188
-> Property
188189
prop_moreOutputsMeansLessInputs size nOuts = withMaxSuccess 1000 $
189190
nOuts < maxBound ==>
190-
_estimateMaxNumberOfInputs size nOuts
191+
_estimateMaxNumberOfInputs proxy size nOuts
191192
>=
192-
_estimateMaxNumberOfInputs size (nOuts + 1)
193+
_estimateMaxNumberOfInputs proxy size (nOuts + 1)
194+
where
195+
proxy = Proxy @'Mainnet
193196

194197
-- | REducing the number of outputs increases the number of inputs.
195198
prop_lessOutputsMeansMoreInputs
@@ -198,10 +201,11 @@ prop_lessOutputsMeansMoreInputs
198201
-> Property
199202
prop_lessOutputsMeansMoreInputs size nOuts = withMaxSuccess 1000 $
200203
nOuts > minBound ==>
201-
_estimateMaxNumberOfInputs size (nOuts - 1)
204+
_estimateMaxNumberOfInputs proxy size (nOuts - 1)
202205
>=
203-
_estimateMaxNumberOfInputs size nOuts
204-
206+
_estimateMaxNumberOfInputs proxy size nOuts
207+
where
208+
proxy = Proxy @'Mainnet
205209

206210
-- | Increasing the max size automatically increased the number of inputs
207211
prop_biggerMaxSizeMeansMoreInputs
@@ -210,9 +214,11 @@ prop_biggerMaxSizeMeansMoreInputs
210214
-> Property
211215
prop_biggerMaxSizeMeansMoreInputs (Quantity size) nOuts = withMaxSuccess 1000 $
212216
size < maxBound `div` 2 ==>
213-
_estimateMaxNumberOfInputs (Quantity size) nOuts
217+
_estimateMaxNumberOfInputs proxy (Quantity size) nOuts
214218
<=
215-
_estimateMaxNumberOfInputs (Quantity (size * 2)) nOuts
219+
_estimateMaxNumberOfInputs proxy (Quantity (size * 2)) nOuts
220+
where
221+
proxy = Proxy @'Mainnet
216222

217223
testCoinSelOpts :: CoinSelectionOptions ()
218224
testCoinSelOpts = coinSelOpts testTxLayer (Quantity 4096)

0 commit comments

Comments
 (0)