dash/src/util/skip_set.h
Konstantin Akimov 8a0e681cea
feat!: add an implementation of DIP 0027 Credit Asset Locks (#5026)
## Issue being fixed or feature implemented
This is an implementation of DIP0027 "Credit Asset Locks".
It's a mechanism to fluidly exchange between Dash and credits.

## What was done?
This pull request includes:
      - Asset Lock transaction
      - Asset Unlock transaction (withdrawal)
      - Credit Pool in coinbase
      - Unit tests for Asset Lock/Unlock tx
      - New functional test `feature_asset_locks.py`

RPC: currently locked amount (credit pool) is available through rpc call
`getblock`.

## How Has This Been Tested?
There added new unit tests for basic checks of transaction validity
(asset lock/unlock).
Also added new functional test "feature_asset_locks.py" that cover
typical cases, but not all corner cases yet.

## Breaking Changes
This feature should be activated as hard-fork because:
- It adds 2 new special transaction and one of them [asset unlock tx]
requires update consensus rulels
 - It adds new data in coinbase tx (credit pool)

## Checklist:
- [x] I have performed a self-review of my own code
- [x] I have commented my code, particularly in hard-to-understand areas
- [x] I have added or updated relevant unit/integration/functional/e2e
tests
- [ ] I have made corresponding changes to the documentation
**To release DIP 0027**
- [x] I have assigned this pull request to a milestone

---------

Co-authored-by: UdjinM6 <UdjinM6@users.noreply.github.com>
2023-07-24 11:39:38 -05:00

53 lines
1.5 KiB
C++

// 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_SKIP_SET_H
#define BITCOIN_UTIL_SKIP_SET_H
#include <serialize.h>
#include <unordered_set>
// This data structure keeps efficiently all indexes and have a strict limit for used memory
// So far as CCreditPool is built only in direction from parent block to child
// there's no need to remove elements from CSkipSet ever, only add them
class CSkipSet {
private:
std::unordered_set<uint64_t> skipped;
uint64_t current_max{0};
size_t capacity_limit;
public:
explicit CSkipSet(size_t capacity_limit = 10'000) :
capacity_limit(capacity_limit)
{}
/**
* `Add` returns true if element has been added correctly and false if
* capacity is depleted.
*
* `Add` should not be called if the element has been already added.
* Use `Contains` to check if the element is here
* Adding existing value will cause an exception
*/
[[nodiscard]] bool Add(uint64_t value);
bool CanBeAdded(uint64_t value) const;
bool Contains(uint64_t value) const;
size_t Size() const {
return current_max - skipped.size();
}
size_t Capacity() const {
return skipped.size();
}
SERIALIZE_METHODS(CSkipSet, obj)
{
READWRITE(obj.current_max);
READWRITE(obj.skipped);
}
};
#endif // BITCOIN_UTIL_SKIP_SET_H