Skip to content

Commit

Permalink
Fix cache issues in prodvote action
Browse files Browse the repository at this point in the history
  • Loading branch information
harrywong committed Feb 23, 2019
1 parent da2829a commit 882ca71
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 19 deletions.
28 changes: 16 additions & 12 deletions libraries/chain/include/evt/chain/contracts/evt_contract_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -263,10 +263,10 @@ get_db_prefix<token_def>(const token_def& v) {
EVT_THROW2(EXCEPTION, FORMAT, __VA_ARGS__); \
}

#define READ_DB_TOKEN_NO_THROW(TYPE, PREFIX, KEY, VPTR) \
{ \
using vtype = typename decltype(VPTR)::element_type; \
VPTR = tokendb_cache.template read_token<vtype>(TYPE, PREFIX, KEY, false /* no throw */); \
#define READ_DB_TOKEN_NO_THROW(TYPE, PREFIX, KEY, VPTR) \
{ \
using vtype = typename decltype(VPTR)::element_type; \
VPTR = tokendb_cache.template read_token<vtype>(TYPE, PREFIX, KEY, true /* no throw */); \
}

#define MAKE_PROPERTY(AMOUNT, SYM) \
Expand Down Expand Up @@ -1557,16 +1557,20 @@ EVT_ACTION_IMPL_BEGIN(prodvote) {
READ_DB_TOKEN_NO_THROW(token_type::prodvote, std::nullopt, pvact.key, map);

if(map == nullptr) {
map.reset(new flat_map<public_key_type, int64_t>());
}
auto newmap = flat_map<public_key_type, int64_t>();
newmap.emplace(*pkey, pvact.value);

auto it = map->emplace(*pkey, pvact.value);
if(it.second == false) {
// existed
it.first->second = pvact.value;
map = tokendb_cache.put_token<std::add_rvalue_reference_t<decltype(newmap)>, true>(
token_type::prodvote, action_op::put, std::nullopt, pvact.key, std::move(newmap));
}
else {
auto it = map->emplace(*pkey, pvact.value);
if(it.second == false) {
// existed
it.first->second = pvact.value;
}
tokendb_cache.put_token(token_type::prodvote, action_op::put, std::nullopt, pvact.key, *map);
}

tokendb_cache.put_token(token_type::prodvote, action_op::put, std::nullopt, pvact.key, *map);

auto is_prod = [&](auto& pk) {
for(auto& p : sche.producers) {
Expand Down
27 changes: 20 additions & 7 deletions libraries/chain/include/evt/chain/token_database_cache.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -101,9 +101,9 @@ class token_database_cache {
return nullptr;
}

template<typename T, typename U = std::decay_t<T>>
void
put_token(token_type type, action_op op, const std::optional<name128>& domain, const name128& key, T&& data) {
template<typename T, bool RtnPTR = false, typename U = std::decay_t<T>>
std::conditional_t<RtnPTR, std::unique_ptr<U, cache_deleter<U>>, void>
put_token(token_type type, action_op op, const std::optional<name128>& domain, const name128& key, T&& data) {
static_assert(std::is_class_v<U>, "Underlying of T should be a class type");
using entry_t = cache_entry<U>;

Expand All @@ -122,13 +122,26 @@ class token_database_cache {

if(h != nullptr) {
// if there's already cache item, no need to insert new one
return;
if constexpr(!RtnPTR) {
return;
}
else {
return nullptr;
}
}

auto entry = new entry_t(std::forward<T>(data));
auto s = cache_->Insert(k, (void*)entry, v.size(),
[](auto& ck, auto cv) { delete (cache_entry<U>*)cv; }, nullptr /* handle */);
FC_ASSERT(s == rocksdb::Status::OK());
if constexpr(!RtnPTR) {
auto s = cache_->Insert(k, (void*)entry, v.size(),
[](auto& ck, auto cv) { delete (cache_entry<U>*)cv; }, nullptr /* handle */);
FC_ASSERT(s == rocksdb::Status::OK());
}
else {
auto s = cache_->Insert(k, (void*)entry, v.size(),
[](auto& ck, auto cv) { delete (cache_entry<U>*)cv; }, &h);
FC_ASSERT(s == rocksdb::Status::OK());
return std::unique_ptr<U, cache_deleter<U>>(&entry->data, cache_deleter<U>(this, h));
}
}

private:
Expand Down

0 comments on commit 882ca71

Please sign in to comment.