Skip to content
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

Fix seek in TrieDB iterator #13

Merged
merged 2 commits into from
Aug 1, 2018
Merged

Fix seek in TrieDB iterator #13

merged 2 commits into from
Aug 1, 2018

Conversation

ngotchac
Copy link
Contributor

It seems to me that the behavior of seek on a TrieDB iterator was broken, looking at the tests.
Seeking to "A" which is in the Trie, calling next should return "A" and not the next value.
This was particularly broken when in a trie with [16, 8] and [16, 16], seeking to [16, 0] would start the iterator at [16, 16].

@ordian
Copy link
Member

ordian commented Jul 27, 2018

Looking at the TrieIterator docs, it says >:

/// Position the iterator on the first element with key > `key`

So, I'm not sure whether it was a typo in the TrieDBIterator docs or the actual bug.

@ngotchac
Copy link
Contributor Author

Hm, yeah there are two contradicting comments... IMO >= makes the most sense.

@pepyakin
Copy link
Contributor

pepyakin commented Jul 27, 2018

Nice find @ngotchac ! I think that not a bug, since the comment found by @ordian and tests are good evidences of otherwise, and this is more of a documentation issue.

And I agree with you that >= makes more sense. If you don't need an element that stricly equals to the one that passed into seek as an argument then you can freely skip it. On the other hand, you can't just go back one item with this iterator.

UPDATE

To clarify: there is actually code in polkadot that relies on >=

/// Position the iterator on the first element with key >= `key`

The use-case is to delete a range of entries keys of which share the given common prefix. E.g. the trie contains the following keys:

cat
dog
dogger
doggest
shark

If I deleted keys with prefix dog I would expected that all dog, dogger and the doggest would have been deleted. But with > the value with the key dog would have been skipped.

@ngotchac
Copy link
Contributor Author

ngotchac commented Jul 27, 2018

I fully agree with you @pepyakin ! And actually the current implementation is still broken with > running my example described above.
(I updated the lib.rs comment to reflect that)

Copy link
Contributor

@dvdplm dvdplm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree that >= is the more natural logic, but I am worried about breakage in code relying on the == behavior. Did you review other code in parity-ethereum that calls seek? If yes, lgtm.

@ngotchac
Copy link
Contributor Author

So the only uses of seek in parity-ethereum are in list_accounts and list_storage methods of ethcore/src/client/client.rs, which are only used in execute_export_state in parity/blockchain.rs. Updating to the new implementation is trivial, but I'm not sure how to make atomic PRs :/

Copy link
Member

@ordian ordian left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, although I don't fully understand the implementation.

@debris debris merged commit 0045887 into master Aug 1, 2018
@debris debris deleted the ng-fix-triedb-seek branch August 1, 2018 08:05
dvdplm pushed a commit that referenced this pull request Aug 3, 2018
Lower hex and couple of cleanups
sorpaas pushed a commit that referenced this pull request Jan 24, 2019
Lower hex and couple of cleanups
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants