Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions csnode/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ add_library(csnode
include/csnode/compressor.hpp
include/csnode/configholder.hpp
include/csnode/multiwallets.hpp
include/csnode/statedigest.hpp
include/csnode/sendcachedata.hpp
include/csnode/eventreport.hpp
include/csnode/idatastream.hpp
Expand Down Expand Up @@ -107,6 +108,7 @@ add_library(csnode
src/roundpackage.cpp
src/configholder.cpp
src/multiwallets.cpp
src/statedigest.cpp
src/sendcachedata.cpp
src/eventreport.cpp
src/poolcache.cpp
Expand Down
8 changes: 8 additions & 0 deletions csnode/include/csnode/multiwallets.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include <boost/multi_index/ordered_index.hpp>
#include <boost/multi_index/hashed_index.hpp>

#include <csnode/statedigest.hpp>
#include <csnode/walletscache.hpp>

using namespace boost::multi_index;
Expand Down Expand Up @@ -57,6 +58,10 @@ class MultiWallets {
}

csdb::Amount checkWallets();

// order-independent digest of every wallet's balance-relevant state (ECMH, see statedigest.hpp);
// computed in full on the first call, then kept up to date on every wallet update
MultisetHash::Digest stateDigest() const;
void iterate(std::function<bool(const PublicKey& key, const InternalData& data)> func);

protected:
Expand Down Expand Up @@ -111,6 +116,9 @@ class MultiWallets {

mutable std::mutex mutex_;
Container indexes_;

mutable MultisetHash digest_;
mutable bool digestReady_ = false;
};
}

Expand Down
4 changes: 4 additions & 0 deletions csnode/include/csnode/node.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -385,6 +385,10 @@ public slots:
// request current trusted nodes for block with specific sequence
void sendBlockRequestToConfidants(cs::Sequence sequence);
void processSpecialInfo(const csdb::Pool& pool);
// logs the wallet state digest every kStateDigestInterval blocks, comparable between nodes
void logStateDigest(const csdb::Pool& pool);
void logStateDigest(cs::Sequence sequence);
static constexpr cs::Sequence kStateDigestInterval = 1000;
void checkConsensusSettings(cs::Sequence seq, std::string& msg);

void validateBlock(const csdb::Pool& block, bool* shouldStop);
Expand Down
46 changes: 46 additions & 0 deletions csnode/include/csnode/statedigest.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#ifndef STATEDIGEST_HPP
#define STATEDIGEST_HPP

#include <array>
#include <cstdint>

#include <csnode/walletscache.hpp>
#include <lib/system/common.hpp>

namespace cs {

// Elliptic-curve multiset hash (ECMH) over ristretto255: an order-independent digest of a set of
// items, updated in O(1) per added or removed item. Two nodes holding the same set get the same
// digest regardless of the order in which the items were added.
class MultisetHash {
public:
using Digest = std::array<uint8_t, 32>;
using Element = std::array<uint8_t, 32>;

MultisetHash();

// the group element an item contributes; callers may keep it to remove the item later
static Element toElement(const cs::Bytes& item);

void add(const cs::Bytes& item);
void remove(const cs::Bytes& item);
void add(const Element& element);
void remove(const Element& element);
void reset();

const Digest& digest() const {
return sum_;
}

private:
Digest sum_;
};

// Canonical serialisation of the balance-relevant state of a wallet: key, balance, delegated amount
// and delegations in both directions, sorted. Empty for a wallet without such state, so that empty
// records do not change the digest.
cs::Bytes serializeWalletState(const WalletsCache::WalletData& wallet);

} // namespace cs

#endif // STATEDIGEST_HPP
4 changes: 4 additions & 0 deletions csnode/include/csnode/walletscache.hpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#ifndef WALLETS_CACHE_HPP
#define WALLETS_CACHE_HPP

#include <array>
#include <list>
#include <memory>
#include <unordered_map>
Expand Down Expand Up @@ -56,6 +57,9 @@ class WalletsCache {
#ifdef MONITOR_NODE
uint64_t createTime_ = 0;
#endif
// ECMH element of this wallet's state, kept by MultiWallets for the state digest (not persisted)
std::array<uint8_t, 32> stateElement_{};
bool hasStateElement_ = false;
};

struct TrustedData {
Expand Down
5 changes: 5 additions & 0 deletions csnode/include/csnode/walletscache_serializer.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#ifndef WALLETS_CACHE_SERIALIZER_HPP
#define WALLETS_CACHE_SERIALIZER_HPP
#include <array>
#include <filesystem>

#include <boost/serialization/serialization.hpp>
Expand Down Expand Up @@ -161,6 +162,10 @@ class WalletsCache_Serializer {
#ifdef MONITOR_NODE
uint64_t createTime_;
#endif
// keep the layout identical to WalletsCache::WalletData (wallets_ aliases its container);
// the state-digest element is rebuilt at start and never serialised
std::array<uint8_t, 32> stateElement_{};
bool hasStateElement_ = false;
};
#ifdef MONITOR_NODE
class TrustedData {
Expand Down
45 changes: 43 additions & 2 deletions csnode/src/multiwallets.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,12 +69,53 @@ void cs::MultiWallets::onWalletCacheUpdated(const cs::WalletsCache::WalletData&
<< ", delegated: " << data.delegated_.to_string();
}

InternalData stored = data;
stored.hasStateElement_ = false;
if (digestReady_) {
const auto state = cs::serializeWalletState(stored);
if (!state.empty()) {
stored.stateElement_ = cs::MultisetHash::toElement(state);
stored.hasStateElement_ = true;
}
}

if (auto iter = byKey.find(data.key_); iter != byKey.end()) {
byKey.replace(iter, data);
// the stored element, not the stored record: delegation maps are shared and already updated in place
if (digestReady_ && iter->hasStateElement_) {
digest_.remove(iter->stateElement_);
}
byKey.replace(iter, stored);
}
else {
indexes_.insert(data);
indexes_.insert(stored);
}

if (stored.hasStateElement_) {
digest_.add(stored.stateElement_);
}
}

cs::MultisetHash::Digest cs::MultiWallets::stateDigest() const {
cs::Lock lock(mutex_);

if (!digestReady_) {
digest_.reset();
auto& byKey = indexes_.get<Tags::ByPublicKey>();
for (auto it = byKey.begin(); it != byKey.end(); ++it) {
const auto state = cs::serializeWalletState(*it);
const bool hasState = !state.empty();
const auto element = hasState ? cs::MultisetHash::toElement(state) : cs::MultisetHash::Element{};
if (hasState) {
digest_.add(element);
}
// the element is not a key of any index, so updating it in place keeps the container valid
const_cast<InternalData&>(*it).stateElement_ = element;
const_cast<InternalData&>(*it).hasStateElement_ = hasState;
}
digestReady_ = true;
}

return digest_.digest();
}

void cs::MultiWallets::iterate(std::function<bool(const PublicKey& key, const InternalData& data)> func) {
Expand Down
16 changes: 16 additions & 0 deletions csnode/src/node.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ Node::Node(cs::config::Observer& observer)
cs::Connector::connect(&blockChain_.tryToStoreBlockEvent, this, &Node::deepBlockValidation);
cs::Connector::connect(&blockChain_.stopNode, this, &Node::stop);
cs::Connector::connect(&blockChain_.storeBlockEvent, this, &Node::processSpecialInfo);
cs::Connector::connect(&blockChain_.storeBlockEvent, this, static_cast<void (Node::*)(const csdb::Pool&)>(&Node::logStateDigest));
cs::Connector::connect(&blockChain_.uncertainBlock, this, &Node::sendBlockRequestToConfidants);
cs::Connector::connect(&blockChain_.orderNecessaryBlock, this, &Node::sendNecessaryBlockRequest);
cs::Connector::connect(&stat_.accountInitiationRequest, this, &Node::accountInitiationRequest);
Expand Down Expand Up @@ -248,6 +249,8 @@ bool Node::init() {
}

cslog() << "Blockchain is ready, contains " << WithDelimiters(stat_.totalTransactions()) << " transactions";
// full digest once here, before taking part in the network; incremental from now on
logStateDigest(blockChain_.getLastSeq());

#ifdef NODE_API
api_->run();
Expand Down Expand Up @@ -4300,6 +4303,19 @@ void Node::checkConsensusSettings(cs::Sequence seq, std::string& msg){
saveConsensusSettingsToChain();
}

void Node::logStateDigest(const csdb::Pool& pool) {
if (pool.sequence() % kStateDigestInterval == 0) {
logStateDigest(pool.sequence());
}
}

void Node::logStateDigest(cs::Sequence sequence) {
const auto& wallets = blockChain_.multiWallets();
const auto digest = wallets.stateDigest();
cslog() << "STATE DIGEST #" << sequence << " " << cs::Utils::byteStreamToHex(digest.data(), digest.size())
<< " wallets " << wallets.size();
}

void Node::validateBlock(const csdb::Pool& block, bool* shouldStop) {
// user-cancel is honoured via BlockChain::init's OpenCallback; *shouldStop here means DataIntegrityError
if (!blockValidator_->validateBlock(block,
Expand Down
128 changes: 128 additions & 0 deletions csnode/src/statedigest.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
#include <csnode/statedigest.hpp>

#include <algorithm>
#include <tuple>
#include <vector>

#include <sodium.h>

#include <csnode/nodecore.hpp>

namespace {
static_assert(crypto_core_ristretto255_BYTES == 32, "ristretto255 element size");

void putU8(cs::Bytes& out, uint8_t value) {
out.push_back(value);
}

void putU32(cs::Bytes& out, uint32_t value) {
for (int i = 0; i < 4; ++i) {
out.push_back(static_cast<uint8_t>(value >> (8 * i)));
}
}

void putU64(cs::Bytes& out, uint64_t value) {
for (int i = 0; i < 8; ++i) {
out.push_back(static_cast<uint8_t>(value >> (8 * i)));
}
}

void putAmount(cs::Bytes& out, const csdb::Amount& amount) {
putU32(out, static_cast<uint32_t>(amount.integral()));
putU64(out, amount.fraction());
}

void putKey(cs::Bytes& out, const cs::PublicKey& key) {
out.insert(out.end(), key.begin(), key.end());
}

using Delegations = std::shared_ptr<std::map<cs::PublicKey, std::vector<cs::TimeMoney>>>;

// std::map keeps the counterparts sorted by key; the entries of each counterpart are sorted here,
// since their order depends on how blocks were applied and reverted
void putDelegations(cs::Bytes& out, const Delegations& delegations) {
if (!delegations) {
putU32(out, 0);
return;
}

putU32(out, static_cast<uint32_t>(delegations->size()));
for (const auto& [counterpart, entries] : *delegations) {
auto sorted = entries;
std::sort(sorted.begin(), sorted.end(), [](const cs::TimeMoney& lhs, const cs::TimeMoney& rhs) {
return std::make_tuple(lhs.time, lhs.initialTime, lhs.amount.integral(), lhs.amount.fraction(), static_cast<uint8_t>(lhs.coeff)) <
std::make_tuple(rhs.time, rhs.initialTime, rhs.amount.integral(), rhs.amount.fraction(), static_cast<uint8_t>(rhs.coeff));
});

putKey(out, counterpart);
putU32(out, static_cast<uint32_t>(sorted.size()));
for (const auto& entry : sorted) {
putU64(out, entry.initialTime);
putU64(out, entry.time);
putAmount(out, entry.amount);
putU8(out, static_cast<uint8_t>(entry.coeff));
}
}
}

bool hasEntries(const Delegations& delegations) {
if (!delegations) {
return false;
}
return std::any_of(delegations->begin(), delegations->end(), [](const auto& item) { return !item.second.empty(); });
}
} // namespace

namespace cs {

MultisetHash::MultisetHash() {
reset();
}

MultisetHash::Element MultisetHash::toElement(const cs::Bytes& item) {
std::array<uint8_t, crypto_core_ristretto255_HASHBYTES> hash;
crypto_generichash(hash.data(), hash.size(), item.data(), item.size(), nullptr, 0);

Element element;
crypto_core_ristretto255_from_hash(element.data(), hash.data());
return element;
}

void MultisetHash::add(const cs::Bytes& item) {
add(toElement(item));
}

void MultisetHash::remove(const cs::Bytes& item) {
remove(toElement(item));
}

void MultisetHash::add(const Element& element) {
crypto_core_ristretto255_add(sum_.data(), sum_.data(), element.data());
}

void MultisetHash::remove(const Element& element) {
crypto_core_ristretto255_sub(sum_.data(), sum_.data(), element.data());
}

void MultisetHash::reset() {
sum_.fill(0); // the identity element of ristretto255
}

cs::Bytes serializeWalletState(const WalletsCache::WalletData& wallet) {
const bool hasState = wallet.balance_ != csdb::Amount{0} || wallet.delegated_ != csdb::Amount{0} ||
hasEntries(wallet.delegateSources_) || hasEntries(wallet.delegateTargets_);
if (!hasState) {
return {};
}

cs::Bytes out;
putU8(out, 1); // format version
putKey(out, wallet.key_);
putAmount(out, wallet.balance_);
putAmount(out, wallet.delegated_);
putDelegations(out, wallet.delegateSources_);
putDelegations(out, wallet.delegateTargets_);
return out;
}

} // namespace cs
1 change: 1 addition & 0 deletions csnode/src/walletscache_serializer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ void WalletsCache_Serializer::bind(WalletsCache& wCache) {
smartPayableTransactions_ =
reinterpret_cast<decltype(smartPayableTransactions_)>(&wCache.smartPayableTransactions_);
canceledSmarts_ = reinterpret_cast<decltype(canceledSmarts_)>(&wCache.canceledSmarts_);
static_assert(sizeof(WalletData) == sizeof(WalletsCache::WalletData), "WalletData mirror must match WalletsCache::WalletData");
wallets_ = reinterpret_cast<decltype(wallets_)>(&wCache.multiWallets_->indexes_);
#ifdef MONITOR_NODE
trusted_info_ = reinterpret_cast<decltype(trusted_info_)>(&wCache.trusted_info_);
Expand Down
Loading
Loading