forked from bitcoin/bitcoin
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
feat!: improves Credit Pool to protect from cumulative increasing amount of gaps #5520
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
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
d0c096c
feat: add an implementation of new data structure CRangesSet
knst c83dd49
refactor: follow-up changes in unit tests for CSkipList (following to…
knst 15bca84
feat!: replaced CSkipList to CRangesSet in credit pool
knst aeef1a6
cleanup: remove dead code - SkipSet is not used anywhere
knst File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,98 @@ | ||
| // Copyright (c) 2023 The Dash Core developers | ||
| // Distributed under the MIT software license, see the accompanying | ||
| // file COPYING or http://www.opensource.org/licenses/mit-license.php. | ||
|
|
||
| #include <util/ranges_set.h> | ||
|
|
||
| CRangesSet::Range::Range() : CRangesSet::Range::Range(0, 0) {} | ||
|
|
||
| CRangesSet::Range::Range(uint64_t begin, uint64_t end) : | ||
| begin(begin), | ||
| end(end) | ||
| { | ||
| } | ||
|
|
||
| bool CRangesSet::Add(uint64_t value) | ||
| { | ||
| if (Contains(value)) return false; | ||
|
|
||
| // If element is not in CRangesSet, we need to add a new range [value, value + 1) | ||
| // But this operation can cause 2 operation of merge (total 3 cases): | ||
| // - if there're exist ranges [x, value) and [value + 1, y) than | ||
| // all 3 of them should be merged in one range [x, y) | ||
| // - if there's exist a range [x, value) - we need to replace it to new range [x, value + 1) | ||
| // - if there's exist a range [value + 1, y) - we need to replace it to new range [value, y) | ||
| Range new_range{value, value + 1}; | ||
| auto it = ranges.lower_bound({value, value}); | ||
| if (it != ranges.begin()) { | ||
| auto prev = it; | ||
| --prev; | ||
|
|
||
| if (prev->end == value) { | ||
| new_range.begin = prev->begin; | ||
| it = ranges.erase(prev); | ||
| } | ||
| } | ||
| const auto next = it; | ||
| if (next != ranges.end()) { | ||
| if (next->begin == value + 1) { | ||
| new_range.end = next->end; | ||
| it = ranges.erase(next); | ||
| } | ||
| } | ||
| const auto ret = ranges.insert(new_range); | ||
| assert(ret.second); | ||
| return true; | ||
| } | ||
|
|
||
| bool CRangesSet::Remove(uint64_t value) | ||
| { | ||
| if (!Contains(value)) return false; | ||
|
|
||
| // If element is in CRangesSet, there's possible 2 cases: | ||
| // - value in the range that is lower-bound (if begin of that range equals to `value`) | ||
| // - value in the previous range (otherwise) | ||
| auto it = ranges.lower_bound({value, value}); | ||
|
|
||
| if (it == ranges.end() || it->begin != value) { | ||
| assert(it != ranges.begin()); | ||
| --it; | ||
| } | ||
|
|
||
| const Range current_range = *it; | ||
| ranges.erase(it); | ||
| if (current_range.begin != value) { | ||
| const auto ret = ranges.insert({current_range.begin, value}); | ||
| assert(ret.second); | ||
| } | ||
| if (value + 1 != current_range.end) { | ||
| const auto ret = ranges.insert({value + 1, current_range.end}); | ||
| assert(ret.second); | ||
| } | ||
| return true; | ||
| } | ||
|
|
||
| bool CRangesSet::IsEmpty() const noexcept | ||
| { | ||
| return ranges.empty(); | ||
| } | ||
|
|
||
| size_t CRangesSet::Size() const noexcept | ||
| { | ||
| size_t result{0}; | ||
| for (auto i : ranges) { | ||
| result += i.end - i.begin; | ||
| } | ||
| return result; | ||
| } | ||
|
|
||
| bool CRangesSet::Contains(uint64_t value) const noexcept | ||
| { | ||
| const auto it = ranges.lower_bound({value, value}); | ||
| if (it != ranges.end() && it->begin == value) return true; | ||
| if (it == ranges.begin()) return false; | ||
| auto prev = it; | ||
| --prev; | ||
| return prev->begin <= value && prev->end > value; | ||
| } | ||
|
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,84 @@ | ||
| // Copyright (c) 2023 The Dash Core developers | ||
| // Distributed under the MIT software license, see the accompanying | ||
| // file COPYING or http://www.opensource.org/licenses/mit-license.php. | ||
|
|
||
| #ifndef BITCOIN_UTIL_RANGES_SET_H | ||
| #define BITCOIN_UTIL_RANGES_SET_H | ||
|
|
||
| #include <hash.h> | ||
| #include <saltedhasher.h> | ||
| #include <serialize.h> | ||
|
|
||
| #include <set> | ||
|
|
||
| /** | ||
| * The CRangesSet is a datastructure that keeps efficiently numbers as set of | ||
| * continuous ranges of numbers. | ||
| * CRangesSet let's to keep elements with gaps of any size while CSkipList has | ||
| * limited capacity (total size of all gaps) | ||
| * | ||
| * The CRangesSet provides transaction guarantees: element can be added or | ||
| * removed and data structure will be consistent. For case if any of these | ||
| * operation failed (out-memory for example), the `assert` will be called to | ||
| * terminate program. | ||
| */ | ||
| class CRangesSet | ||
| { | ||
| // internal datastructure, doesn't have a reason to be publicly available | ||
| struct Range | ||
| { | ||
| uint64_t begin; | ||
| uint64_t end; | ||
| Range(); | ||
| Range(uint64_t begin, uint64_t end); | ||
| bool operator<(const Range& other) const | ||
| { | ||
| if (begin != other.begin) return begin < other.begin; | ||
| return end < other.end; | ||
| } | ||
|
|
||
| SERIALIZE_METHODS(Range, obj) | ||
| { | ||
| READWRITE(obj.begin); | ||
| READWRITE(obj.end); | ||
| } | ||
| }; | ||
|
|
||
| std::set<Range> ranges; | ||
|
|
||
| public: | ||
| /** | ||
| * this function adds `value` to the datastructure. | ||
| * it returns true if `add` succeed | ||
| */ | ||
| [[nodiscard]] bool Add(uint64_t value); | ||
|
|
||
| /** | ||
| * this function returns true if `value` exists in the datastructure | ||
| */ | ||
| [[nodiscard]] bool Contains(uint64_t value) const noexcept; | ||
|
|
||
| /** | ||
| * this function removes `value` from the datastructure. | ||
| * it returns `false` if element didn't existed or removing failed by any reason | ||
| */ | ||
| [[nodiscard]] bool Remove(uint64_t value); | ||
|
|
||
| /** | ||
| * Size() works with complexity O(N) times, avoid calling it without a good reason | ||
| * Instead prefer to use IsEmpty() | ||
| */ | ||
| [[nodiscard]] size_t Size() const noexcept; | ||
|
|
||
| /** | ||
| * IsEmpty() returns true if there's no any elements added | ||
| */ | ||
| [[nodiscard]] bool IsEmpty() const noexcept; | ||
|
|
||
| SERIALIZE_METHODS(CRangesSet, obj) | ||
| { | ||
| READWRITE(obj.ranges); | ||
| } | ||
| }; | ||
|
|
||
| #endif // BITCOIN_UTIL_RANGES_SET_H |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.