-
Notifications
You must be signed in to change notification settings - Fork 792
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4482 from simpago/rep-weight-table
rep_weights part 1 - Add data store for representative weights
- Loading branch information
Showing
19 changed files
with
378 additions
and
8 deletions.
There are no files selected for viewing
This file contains 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 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,75 @@ | ||
#include <nano/lib/numbers.hpp> | ||
#include <nano/store/component.hpp> | ||
#include <nano/store/rep_weight.hpp> | ||
#include <nano/test_common/make_store.hpp> | ||
|
||
#include <gtest/gtest.h> | ||
|
||
#include <atomic> | ||
#include <iostream> | ||
|
||
TEST (rep_weight_store, empty) | ||
{ | ||
auto store = nano::test::make_store (); | ||
ASSERT_TRUE (!store->init_error ()); | ||
auto txn{ store->tx_begin_read () }; | ||
ASSERT_EQ (0, store->rep_weight.count (txn)); | ||
} | ||
|
||
TEST (rep_weight_store, add_item) | ||
{ | ||
auto store = nano::test::make_store (); | ||
ASSERT_TRUE (!store->init_error ()); | ||
auto txn{ store->tx_begin_write () }; | ||
|
||
nano::account representative{ 123 }; | ||
nano::uint128_t weight{ 456 }; | ||
store->rep_weight.put (txn, representative, weight); | ||
|
||
ASSERT_EQ (1, store->rep_weight.count (txn)); | ||
ASSERT_EQ (weight, store->rep_weight.get (txn, representative)); | ||
} | ||
|
||
TEST (rep_weight_store, del) | ||
{ | ||
auto store = nano::test::make_store (); | ||
ASSERT_TRUE (!store->init_error ()); | ||
auto txn{ store->tx_begin_write () }; | ||
|
||
store->rep_weight.put (txn, 1, 100); | ||
store->rep_weight.put (txn, 2, 200); | ||
store->rep_weight.put (txn, 3, 300); | ||
|
||
store->rep_weight.del (txn, 2); | ||
|
||
ASSERT_EQ (2, store->rep_weight.count (txn)); | ||
ASSERT_EQ (0, store->rep_weight.get (txn, 200)); | ||
} | ||
|
||
TEST (rep_weight_store, for_each_par) | ||
{ | ||
auto store = nano::test::make_store (); | ||
ASSERT_TRUE (!store->init_error ()); | ||
{ | ||
auto txn{ store->tx_begin_write () }; | ||
for (auto i = 0; i < 50; ++i) | ||
{ | ||
store->rep_weight.put (txn, i, 100); | ||
} | ||
} | ||
|
||
std::atomic_size_t rep_total{ 0 }; | ||
std::atomic_size_t weight_total{ 0 }; | ||
|
||
store->rep_weight.for_each_par ( | ||
[&rep_total, &weight_total] (auto const &, auto i, auto n) { | ||
for (; i != n; ++i) | ||
{ | ||
rep_total.fetch_add (static_cast<std::size_t> (i->first.number ())); | ||
weight_total.fetch_add (static_cast<std::size_t> (i->second.number ())); | ||
} | ||
}); | ||
|
||
ASSERT_EQ (1225, rep_total.load ()); | ||
ASSERT_EQ (50 * 100, weight_total.load ()); | ||
} |
This file contains 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 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 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 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 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 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 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,68 @@ | ||
#include <nano/lib/numbers.hpp> | ||
#include <nano/secure/parallel_traversal.hpp> | ||
#include <nano/store/lmdb/lmdb.hpp> | ||
#include <nano/store/lmdb/rep_weight.hpp> | ||
|
||
#include <iostream> | ||
#include <stdexcept> | ||
|
||
nano::store::lmdb::rep_weight::rep_weight (nano::store::lmdb::component & store_a) : | ||
store{ store_a } | ||
{ | ||
} | ||
|
||
uint64_t nano::store::lmdb::rep_weight::count (store::transaction const & txn_a) | ||
{ | ||
return store.count (txn_a, tables::rep_weights); | ||
} | ||
|
||
nano::uint128_t nano::store::lmdb::rep_weight::get (store::transaction const & txn_a, nano::account const & representative_a) | ||
{ | ||
nano::store::lmdb::db_val value; | ||
auto status = store.get (txn_a, tables::rep_weights, representative_a, value); | ||
release_assert (store.success (status) || store.not_found (status)); | ||
nano::uint128_t weight{ 0 }; | ||
if (store.success (status)) | ||
{ | ||
nano::uint128_union weight_union{ value }; | ||
weight = weight_union.number (); | ||
} | ||
return weight; | ||
} | ||
|
||
void nano::store::lmdb::rep_weight::put (store::write_transaction const & txn_a, nano::account const & representative_a, nano::uint128_t const & weight_a) | ||
{ | ||
nano::uint128_union weight{ weight_a }; | ||
auto status = store.put (txn_a, tables::rep_weights, representative_a, weight); | ||
store.release_assert_success (status); | ||
} | ||
|
||
void nano::store::lmdb::rep_weight::del (store::write_transaction const & txn_a, nano::account const & representative_a) | ||
{ | ||
auto status = store.del (txn_a, tables::rep_weights, representative_a); | ||
store.release_assert_success (status); | ||
} | ||
|
||
nano::store::iterator<nano::account, nano::uint128_union> nano::store::lmdb::rep_weight::begin (store::transaction const & transaction_a, nano::account const & representative_a) const | ||
{ | ||
return store.make_iterator<nano::account, nano::uint128_union> (transaction_a, tables::rep_weights, representative_a); | ||
} | ||
|
||
nano::store::iterator<nano::account, nano::uint128_union> nano::store::lmdb::rep_weight::begin (store::transaction const & transaction_a) const | ||
{ | ||
return store.make_iterator<nano::account, nano::uint128_union> (transaction_a, tables::rep_weights); | ||
} | ||
|
||
nano::store::iterator<nano::account, nano::uint128_union> nano::store::lmdb::rep_weight::end () const | ||
{ | ||
return nano::store::iterator<nano::account, nano::uint128_union> (nullptr); | ||
} | ||
|
||
void nano::store::lmdb::rep_weight::for_each_par (std::function<void (store::read_transaction const &, store::iterator<nano::account, nano::uint128_union>, store::iterator<nano::account, nano::uint128_union>)> const & action_a) const | ||
{ | ||
parallel_traversal<nano::uint256_t> ( | ||
[&action_a, this] (nano::uint256_t const & start, nano::uint256_t const & end, bool const is_last) { | ||
auto transaction (this->store.tx_begin_read ()); | ||
action_a (transaction, this->begin (transaction, start), !is_last ? this->begin (transaction, end) : this->end ()); | ||
}); | ||
} |
This file contains 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,34 @@ | ||
#pragma once | ||
|
||
#include <nano/store/rep_weight.hpp> | ||
|
||
#include <lmdb/libraries/liblmdb/lmdb.h> | ||
|
||
namespace nano::store::lmdb | ||
{ | ||
class component; | ||
|
||
class rep_weight : public nano::store::rep_weight | ||
{ | ||
private: | ||
nano::store::lmdb::component & store; | ||
|
||
public: | ||
explicit rep_weight (nano::store::lmdb::component & store_a); | ||
|
||
uint64_t count (store::transaction const & txn) override; | ||
nano::uint128_t get (store::transaction const & txn_a, nano::account const & representative_a) override; | ||
void put (store::write_transaction const & txn_a, nano::account const & representative_a, nano::uint128_t const & weight_a) override; | ||
void del (store::write_transaction const &, nano::account const & representative_a) override; | ||
store::iterator<nano::account, nano::uint128_union> begin (store::transaction const & transaction_a, nano::account const & representative_a) const override; | ||
store::iterator<nano::account, nano::uint128_union> begin (store::transaction const & transaction_a) const override; | ||
store::iterator<nano::account, nano::uint128_union> end () const override; | ||
void for_each_par (std::function<void (store::read_transaction const &, store::iterator<nano::account, nano::uint128_union>, store::iterator<nano::account, nano::uint128_union>)> const & action_a) const override; | ||
|
||
/** | ||
* Representative weights | ||
* nano::account -> uint128_t | ||
*/ | ||
MDB_dbi rep_weights_handle{ 0 }; | ||
}; | ||
} |
This file contains 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,32 @@ | ||
#pragma once | ||
|
||
#include <nano/lib/numbers.hpp> | ||
#include <nano/store/component.hpp> | ||
#include <nano/store/iterator.hpp> | ||
|
||
#include <cstdint> | ||
#include <functional> | ||
|
||
namespace nano | ||
{ | ||
// class account; | ||
} | ||
namespace nano::store | ||
{ | ||
/** | ||
* A lookup table of all representatives and their vote weight | ||
*/ | ||
class rep_weight | ||
{ | ||
public: | ||
virtual ~rep_weight (){}; | ||
virtual uint64_t count (store::transaction const & txn_a) = 0; | ||
virtual nano::uint128_t get (store::transaction const & txn_a, nano::account const & representative_a) = 0; | ||
virtual void put (store::write_transaction const & txn_a, nano::account const & representative_a, nano::uint128_t const & weight_a) = 0; | ||
virtual void del (store::write_transaction const &, nano::account const & representative_a) = 0; | ||
virtual store::iterator<nano::account, nano::uint128_union> begin (store::transaction const & transaction_a, nano::account const & representative_a) const = 0; | ||
virtual store::iterator<nano::account, nano::uint128_union> begin (store::transaction const & transaction_a) const = 0; | ||
virtual store::iterator<nano::account, nano::uint128_union> end () const = 0; | ||
virtual void for_each_par (std::function<void (store::read_transaction const &, store::iterator<nano::account, nano::uint128_union>, store::iterator<nano::account, nano::uint128_union>)> const & action_a) const = 0; | ||
}; | ||
} |
Oops, something went wrong.