New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Let DB.readTxHistory
take SortOrder
and Range SlotId
#588
Conversation
2f8cab3
to
c93d2f1
Compare
deriving (Eq, Show) | ||
|
||
-- Represents the range [start, end] | ||
data Filter a = Filter |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should add some clearer comment here
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah! This si quite similar to what I proposed here: #586 (comment). I like it :)
68e0db7
to
56cd71b
Compare
deriving (Eq, Show) | ||
|
||
-- Represents the range [start, end] | ||
data Filter a = Filter |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah! This si quite similar to what I proposed here: #586 (comment). I like it :)
44c8611
to
b08b92a
Compare
lib/core/src/Cardano/Wallet.hs
Outdated
liftIO $ assemble tip <$> DB.readTxHistory db (PrimaryKey wid) | ||
liftIO $ assemble tip | ||
<$> DB.readTxHistory db (PrimaryKey wid) | ||
defaultTxSortOrder noFilter |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
TODO: _listTransactions
should take sort and filter parameters as well.
DB.readTxHistory
is well-tested through the StateMachine
tests. Not sure we need to do anything in particular on the WalletLayer level 🤔
@@ -326,6 +330,23 @@ instance ToText SortOrder where | |||
instance FromText SortOrder where | |||
fromText = fromTextToBoundedEnum SnakeLowerCase | |||
|
|||
defaultTxSortOrder :: SortOrder | |||
defaultTxSortOrder = Descending |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just wondering, why is it better to have Descending
as a default?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hm indeed… this is currently not used from the API as a "defaultTxSortOrder".
It is currently used for:
- Ordering the DB read for pending transactions when reading a wallet checkpoint. This appears pointless as it will be put in a
Set (Tx, TxMeta)
later. https://github.com/input-output-hk/cardano-wallet/blob/315fec4d4cb56114e1fd59eedd420ea963e3cf29/lib/core/src/Cardano/Wallet/DB/Sqlite.hs#L312 DBSpec
which just needs an arbitrary sort-order.SqliteFileModeSpec
which just needs an arbitrary sort-order https://github.com/input-output-hk/cardano-wallet/blob/315fec4d4cb56114e1fd59eedd420ea963e3cf29/lib/core/test/unit/Cardano/Wallet/DB/SqliteFileModeSpec.hs#L137listTransactions
inWalletLayer
, which is temporary
Maybe we should remove this from Primitive.Types
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Removed in #592 .
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If this Descending
value is used as a default somewhere (be it in the API or elsewhere), then I'd much rather see it defined as a named constant than as a magic value embedded in the functions that use it.
See: #592 (review)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
listTransactions in WalletLayer, which is temporary
What do you mean "temporary" 😅 ?
I think the rationale is: the ordering should come from the caller of the wallet layer. So, starting from the WalletLayer
, the sort order shouldn't be Maybe
but a plain value (because a default at this stage would be totally arbitrary).
At the API-level however, we decide of a default ordering for which Descending
makes sense (most recent results first).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What do you mean "temporary" 😅 ?
😄 listTransactions
used a hard-coded Descending
order instead of its argument.
With a default value in listTransactions
we do need to refer to Descending
, which I didn't think about.
the sort order shouldn't be Maybe but a plain value (because a default at this stage would be totally arbitrary).
Yes. Unless one argues that defaulting to Descending
shouldn't be unique to the API. Since you seem positive, I think I'll go with it though.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looking good
DB.readTxHistory
take SortDirection
and Filter SlotId
DB.readTxHistory
take SortOrder
and Range SlotId
0bd9baa
to
315fec4
Compare
@@ -326,6 +330,23 @@ instance ToText SortOrder where | |||
instance FromText SortOrder where | |||
fromText = fromTextToBoundedEnum SnakeLowerCase | |||
|
|||
defaultTxSortOrder :: SortOrder | |||
defaultTxSortOrder = Descending |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If this Descending
value is used as a default somewhere (be it in the API or elsewhere), then I'd much rather see it defined as a named constant than as a magic value embedded in the functions that use it.
See: #592 (review)
315fec4
to
13eab2e
Compare
Squashed fixups diff --git a/lib/core/src/Cardano/Wallet/Primitive/Types.hs b/lib/core/src/Cardano/Wallet/Primitive/Types.hs
index 8a037865..b2a438ec 100644
--- a/lib/core/src/Cardano/Wallet/Primitive/Types.hs
+++ b/lib/core/src/Cardano/Wallet/Primitive/Types.hs
@@ -344,7 +344,7 @@ defaultTxSortOrder = Descending
--
-- - [start, end] Range (Just start) (Just end) \x -> x >= start && x <= end
-- - [start,∞) Range (Just start) Nothing \x -> x >= start
--- - (-∞,end) Range Nothing (Just end) \x -> x <= end
+-- - (-∞,end] Range Nothing (Just end) \x -> x <= end
-- - (-∞,∞) Range Nothing Nothing \_x -> True
data Range a = Range
{ rStart :: Maybe a
diff --git a/lib/core/test/unit/Cardano/Wallet/DB/SqliteFileModeSpec.hs b/lib/core/test/unit/Cardano/Wallet/DB/SqliteFileModeSpec.hs
index e2effbf4..dfeeca56 100644
--- a/lib/core/test/unit/Cardano/Wallet/DB/SqliteFileModeSpec.hs
+++ b/lib/core/test/unit/Cardano/Wallet/DB/SqliteFileModeSpec.hs
@@ -133,9 +133,11 @@ spec = do
unsafeRunExceptT $ createWallet db testWid testCp testMetadata
unsafeRunExceptT $ putTxHistory db testWid (Map.fromList testTxs)
destroyDBLayer ctx
- testOpeningCleaning f
+ testOpeningCleaning
+ f
(\db' -> readTxHistory db' testWid defaultTxSortOrder wholeRange)
- testTxs mempty
+ testTxs
+ mempty
it "put and read checkpoint" $ \f -> do
(ctx, db) <- newDBLayer' (Just f)
diff --git a/lib/core/test/unit/Cardano/Wallet/DBSpec.hs b/lib/core/test/unit/Cardano/Wallet/DBSpec.hs
index d0a02055..3d774062 100644
--- a/lib/core/test/unit/Cardano/Wallet/DBSpec.hs
+++ b/lib/core/test/unit/Cardano/Wallet/DBSpec.hs
@@ -199,7 +199,9 @@ type TxHistory = [(Hash "Tx", (Tx, TxMeta))]
filterTxHistory :: SortOrder -> Range SlotId -> TxHistory -> TxHistory
filterTxHistory order range =
filter (isWithinRange range . slotId . snd . snd)
- . (if order == Ascending then reverse else id)
+ . (case order of
+ Ascending -> reverse
+ Descending -> id)
. sortBySlot
. sortByTxId
where |
4aa664c
to
e23c506
Compare
|
(maybe True (x <=) high) | ||
|
||
-- NOTE: We could imagine replacing 'Range (Just 3) Nothing' with something | ||
-- more magic like: `(Including 3) ... NoBound` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I considered this. Maybe superfluous comment…
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
with something more magic like
😬 .... We gotta find a use-case for this. Otherwise, it just introduces more code to maintain and more complexity without justification ^.^
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A couple more comments:
genSortOrder
can reusearbitraryBoundedEnum
.- The
UTCTime
instance fromquickcheck-instances
can be replaced withTest.Utils.Time.genUniformTime
, as this gives a wider range ofUTCTime
values (rather than those with dates that are clustered around 1864).
EDIT: I've added a couple of commits to address the above comments.
@@ -526,6 +537,21 @@ generator (Model _ wids) = Just $ frequency $ fmap (fmap At) <$> concat | |||
genPrivKey :: Gen MPrivKey | |||
genPrivKey = elements ["pk1", "pk2", "pk3"] | |||
|
|||
genSortOrder :: Gen SortOrder | |||
genSortOrder = QC.elements [Ascending, Descending] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This can be replaced with arbitraryBoundedEnum
from Test.QuickCheck.Arbitrary
:
arbitraryBoundedEnum :: (Bounded a, Enum a) => Gen a
, choose | ||
, elements | ||
, property | ||
, withMaxSuccess | ||
, (==>) | ||
) | ||
import Test.QuickCheck.Instances.Time | ||
() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would recommend replacing this with Test.Utils.Time.genUniformTime
to get a wider range of UTCTime
values. The instance supplied by the package above generates times that are clustered around 1864:
@@ -512,7 +523,7 @@ generator (Model _ wids) = Just $ frequency $ fmap (fmap At) <$> concat | |||
, (5, PutWalletMeta <$> genId' <*> arbitrary) | |||
, (5, ReadWalletMeta <$> genId') | |||
, (5, PutTxHistory <$> genId' <*> fmap unGenTxHistory arbitrary) | |||
, (5, ReadTxHistory <$> genId') | |||
, (5, ReadTxHistory <$> genId' <*> genSortOrder <*> genRange) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The genSortOrder
here can be replaced with arbitraryBoundedEnum
.
e23c506
to
4f00014
Compare
I made DB.readTxHistory take and apply sortOrder and range filtering with implementations in Sqlite, MVar and Sqlite's StateMachine tests. Squashed from: Ensure readTxHistory tests pass with `Descending noFilter` [WIP] Make tests concider sorting and filtering Do actual Sqlite sorting and filtering Apply suggestions from code review Introduce defaultTxSortDirection Temporary overflow fix to make StateMachine tests pass Rename Filter to Range` Fixup: more polish
This is slightly akward. But I opted for "slightly awkward" instead of "the wrong solution".
a99a8c9
to
820b63f
Compare
Issue Number
#466
Overview
SortDirection
andFilter a
(for imposing>=
/<=
requirements) to coreDB.readTxHistory
take and applysortDir
andfilter
with implementations inSqlite
,MVar
and Sqlite's StateMachine tests.Test.QuickCheck.Instances.Time
withUniformTime
from moduleTest.Utils.Time
.arbitraryBoundedEnum
to avoid repeating the definition ofSortOrder
.Comments