@@ -186,7 +186,9 @@ eskToHdRootId = HdRootId . InDb . Core.makePubKeyAddressBoot . Core.encToPublic
186
186
HD wallets
187
187
-------------------------------------------------------------------------------}
188
188
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
190
192
-- of 'Ae2tdPwUPEZ18ZjTLnLVr9CEvUEUX4eW1LBHbxxxJgxdAYHrDeSCSbCxrvx', but is,
191
193
-- in a sense, a special breed as it's derived from the 'PublicKey' (derived
192
194
-- from some BIP-39 mnemonics, typically) and which does not depend from any
@@ -197,6 +199,13 @@ eskToHdRootId = HdRootId . InDb . Core.makePubKeyAddressBoot . Core.encToPublic
197
199
-- just a Text) it's possible to call 'decodeTextAddress' to grab a valid
198
200
-- 'Core.Address', and then transform this into a 'Kernel.WalletId' type
199
201
-- 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.
200
209
newtype HdRootId = HdRootId { getHdRootId :: InDb Core. Address }
201
210
deriving (Eq , Ord )
202
211
@@ -211,7 +220,14 @@ data HdAccountId = HdAccountId {
211
220
_hdAccountIdParent :: HdRootId
212
221
, _hdAccountIdIx :: HdAccountIx
213
222
}
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)
215
231
216
232
instance Arbitrary HdAccountId where
217
233
arbitrary = HdAccountId <$> arbitrary <*> arbitrary
@@ -221,7 +237,14 @@ data HdAddressId = HdAddressId {
221
237
_hdAddressIdParent :: HdAccountId
222
238
, _hdAddressIdIx :: HdAddressIx
223
239
}
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)
225
248
226
249
instance Arbitrary HdAddressId where
227
250
arbitrary = HdAddressId <$> arbitrary <*> arbitrary
0 commit comments