Skip to content
This repository was archived by the owner on Aug 18, 2020. It is now read-only.

Commit 4246051

Browse files
authored
Merge pull request #3436 from input-output-hk/bugfix/cbr-325-efficient-ord-for-ids
[CBR-325] Compare parent IDs last
2 parents 27ab50c + 9cbe05f commit 4246051

File tree

1 file changed

+26
-3
lines changed

1 file changed

+26
-3
lines changed

wallet-new/src/Cardano/Wallet/Kernel/DB/HdWallet.hs

+26-3
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,9 @@ eskToHdRootId = HdRootId . InDb . Core.makePubKeyAddressBoot . Core.encToPublic
186186
HD wallets
187187
-------------------------------------------------------------------------------}
188188

189-
-- | HD wallet root ID. Conceptually, this is just an 'Address' in the form
189+
-- | HD wallet root ID.
190+
--
191+
-- Conceptually, this is just an 'Address' in the form
190192
-- of 'Ae2tdPwUPEZ18ZjTLnLVr9CEvUEUX4eW1LBHbxxxJgxdAYHrDeSCSbCxrvx', but is,
191193
-- in a sense, a special breed as it's derived from the 'PublicKey' (derived
192194
-- from some BIP-39 mnemonics, typically) and which does not depend from any
@@ -197,6 +199,13 @@ eskToHdRootId = HdRootId . InDb . Core.makePubKeyAddressBoot . Core.encToPublic
197199
-- just a Text) it's possible to call 'decodeTextAddress' to grab a valid
198200
-- 'Core.Address', and then transform this into a 'Kernel.WalletId' type
199201
-- easily.
202+
--
203+
-- NOTE: Comparing 'HdRootId' is a potentially expensive computation, as it
204+
-- implies comparing large addresses. Use with care.
205+
--
206+
-- TODO: It would be better not to have the address here, and just use an 'Int'
207+
-- as a primary key. This however is a slightly larger refactoring we don't
208+
-- currently have time for.
200209
newtype HdRootId = HdRootId { getHdRootId :: InDb Core.Address }
201210
deriving (Eq, Ord)
202211

@@ -211,7 +220,14 @@ data HdAccountId = HdAccountId {
211220
_hdAccountIdParent :: HdRootId
212221
, _hdAccountIdIx :: HdAccountIx
213222
}
214-
deriving (Eq, Ord)
223+
deriving (Eq)
224+
225+
-- | We make sure to compare the account index first to avoid doing an
226+
-- unnecessary comparison of the root ID
227+
instance Ord HdAccountId where
228+
compare a b =
229+
compare (_hdAccountIdIx a) (_hdAccountIdIx b)
230+
<> compare (_hdAccountIdParent a) (_hdAccountIdParent b)
215231

216232
instance Arbitrary HdAccountId where
217233
arbitrary = HdAccountId <$> arbitrary <*> arbitrary
@@ -221,7 +237,14 @@ data HdAddressId = HdAddressId {
221237
_hdAddressIdParent :: HdAccountId
222238
, _hdAddressIdIx :: HdAddressIx
223239
}
224-
deriving (Eq, Ord)
240+
deriving (Eq)
241+
242+
-- | We make sure to compare the address index first to avoid doing an
243+
-- unnecessary comparison of the account ID
244+
instance Ord HdAddressId where
245+
compare a b =
246+
compare (_hdAddressIdIx a) (_hdAddressIdIx b)
247+
<> compare (_hdAddressIdParent a) (_hdAddressIdParent b)
225248

226249
instance Arbitrary HdAddressId where
227250
arbitrary = HdAddressId <$> arbitrary <*> arbitrary

0 commit comments

Comments
 (0)