Skip to content

Commit

Permalink
New accounts are 'pushed' to worker threads (#102)
Browse files Browse the repository at this point in the history
  • Loading branch information
vtnerd committed Apr 8, 2024
1 parent f300bff commit 80604e8
Show file tree
Hide file tree
Showing 21 changed files with 742 additions and 49 deletions.
2 changes: 1 addition & 1 deletion src/admin_main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
#include "rpc/admin.h"
#include "span.h" // monero/contrib/epee/include
#include "string_tools.h" // monero/contrib/epee/include
#include "wire/crypto.h"
#include "wire/adapted/crypto.h"
#include "wire/filters.h"
#include "wire/json/write.h"
#include "wire/wrapper/array.h"
Expand Down
58 changes: 58 additions & 0 deletions src/db/account.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@
#include "common/expect.h"
#include "db/data.h"
#include "db/string.h"
#include "wire/adapted/crypto.h"
#include "wire/adapted/pair.h"
#include "wire/msgpack.h"
#include "wire/vector.h"
#include "wire/wrapper/trusted_array.h"

namespace lws
{
Expand All @@ -50,6 +55,10 @@ namespace lws

struct account::internal
{
internal()
: address(), id(db::account_id::invalid), pubs{}, view_key{}
{}

explicit internal(db::account const& source)
: address(db::address_string(source.address)), id(source.id), pubs(source.address), view_key()
{
Expand All @@ -66,6 +75,23 @@ namespace lws
);
}

void read_bytes(wire::msgpack_reader& source)
{ map(source, *this); }

void write_bytes(wire::msgpack_writer& dest) const
{ map(dest, *this); }

template<typename F, typename T>
static void map(F& format, T& self)
{
wire::object(format,
WIRE_FIELD_ID(0, address),
WIRE_FIELD_ID(1, id),
WIRE_FIELD_ID(2, pubs),
WIRE_FIELD_ID(3, view_key)
);
}

std::string address;
db::account_id id;
db::account_address pubs;
Expand All @@ -87,6 +113,23 @@ namespace lws
MONERO_THROW(::common_error::kInvalidArgument, "using moved from account");
}

template<typename F, typename T, typename U>
void account::map(F& format, T& self, U& immutable)
{
wire::object(format,
wire::field<0>("immutable_", std::ref(immutable)),
wire::optional_field<1>("spendable_", wire::trusted_array(std::ref(self.spendable_))),
wire::optional_field<2>("pubs_", wire::trusted_array(std::ref(self.pubs_))),
wire::optional_field<3>("spends_", wire::trusted_array(std::ref(self.spends_))),
wire::optional_field<4>("outputs_", wire::trusted_array(std::ref(self.outputs_))),
WIRE_FIELD_ID(5, height_)
);
}

account::account() noexcept
: immutable_(nullptr), spendable_(), pubs_(), spends_(), outputs_(), height_(db::block_id(0))
{}

account::account(db::account const& source, std::vector<std::pair<db::output_id, db::address_index>> spendable, std::vector<crypto::public_key> pubs)
: account(std::make_shared<internal>(source), source.scan_height, std::move(spendable), std::move(pubs))
{
Expand All @@ -97,6 +140,21 @@ namespace lws
account::~account() noexcept
{}

void account::read_bytes(::wire::msgpack_reader& source)
{
auto immutable = std::make_shared<internal>();
map(source, *this, *immutable);
immutable_ = std::move(immutable);
std::sort(spendable_.begin(), spendable_.end());
std::sort(pubs_.begin(), pubs_.end(), sort_pubs{});
}

void account::write_bytes(::wire::msgpack_writer& dest) const
{
null_check();
map(dest, *this, *immutable_);
}

account account::clone() const
{
account result{immutable_, height_, spendable_, pubs_};
Expand Down
14 changes: 14 additions & 0 deletions src/db/account.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@
#include "fwd.h"
#include "db/data.h"
#include "db/fwd.h"
#include "wire/fwd.h"
#include "wire/msgpack/fwd.h"

namespace lws
{
Expand All @@ -54,8 +56,14 @@ namespace lws
explicit account(std::shared_ptr<const internal> immutable, db::block_id height, std::vector<std::pair<db::output_id, db::address_index>> spendable, std::vector<crypto::public_key> pubs) noexcept;
void null_check() const;

template<typename F, typename T, typename U>
static void map(F& format, T& self, U& immutable);

public:

//! Construct an "invalid" account (for de-serialization)
account() noexcept;

//! Construct an account from `source` and current `spendable` outputs.
explicit account(db::account const& source, std::vector<std::pair<db::output_id, db::address_index>> spendable, std::vector<crypto::public_key> pubs);

Expand All @@ -71,6 +79,12 @@ namespace lws
account& operator=(const account&) = delete;
account& operator=(account&&) = default;

//! Read into `this` from `source`.
void read_bytes(::wire::msgpack_reader& source);

//! Write to `dest` from `this`.
void write_bytes(::wire::msgpack_writer& dest) const;

//! \return A copy of `this`.
account clone() const;

Expand Down
78 changes: 64 additions & 14 deletions src/db/data.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2018, The Monero Project
// Copyright (c) 2018-2024, The Monero Project
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without modification, are
Expand Down Expand Up @@ -36,7 +36,7 @@
#include "ringct/rctTypes.h" // monero/src
#include "wire.h"
#include "wire/adapted/array.h"
#include "wire/crypto.h"
#include "wire/adapted/crypto.h"
#include "wire/json/write.h"
#include "wire/msgpack.h"
#include "wire/uuid.h"
Expand All @@ -54,7 +54,7 @@ namespace db
template<typename F, typename T>
void map_output_id(F& format, T& self)
{
wire::object(format, WIRE_FIELD(high), WIRE_FIELD(low));
wire::object(format, WIRE_FIELD_ID(0, high), WIRE_FIELD_ID(1, low));
}
}
WIRE_DEFINE_OBJECT(output_id, map_output_id);
Expand All @@ -72,7 +72,7 @@ namespace db
template<typename F, typename T>
void map_account_address(F& format, T& self)
{
wire::object(format, WIRE_FIELD(spend_public), WIRE_FIELD(view_public));
wire::object(format, WIRE_FIELD_ID(0, spend_public), WIRE_FIELD_ID(1, view_public));
}
}
WIRE_DEFINE_OBJECT(account_address, map_account_address);
Expand Down Expand Up @@ -250,6 +250,55 @@ namespace db
}
WIRE_DEFINE_OBJECT(transaction_link, map_transaction_link);

void read_bytes(wire::reader& source, output& self)
{
bool coinbase = false;
boost::optional<rct::key> rct;
boost::optional<std::vector<std::uint8_t>> payment_id;

wire::object(source,
wire::optional_field<0>("id", wire::defaulted(std::ref(self.spend_meta.id), output_id::txpool())),
wire::optional_field<1>("block", wire::defaulted(std::ref(self.link.height), block_id::txpool)),
wire::field<2>("index", std::ref(self.spend_meta.index)),
wire::field<3>("amount", std::ref(self.spend_meta.amount)),
wire::field<4>("timestamp", std::ref(self.timestamp)),
wire::field<5>("tx_hash", std::ref(self.link.tx_hash)),
wire::field<6>("tx_prefix_hash", std::ref(self.tx_prefix_hash)),
wire::field<7>("tx_public", std::ref(self.spend_meta.tx_public)),
wire::optional_field<8>("rct_mask", std::ref(rct)),
wire::optional_field<9>("payment_id", std::ref(payment_id)),
wire::field<10>("unlock_time", std::ref(self.unlock_time)),
wire::field<11>("mixin_count", std::ref(self.spend_meta.mixin_count)),
wire::field<12>("coinbase", std::ref(coinbase)),
wire::field<13>("fee", std::ref(self.fee)),
wire::field<14>("recipient", std::ref(self.recipient)),
wire::field<15>("pub", std::ref(self.pub))
);

std::uint8_t pay_length = 0;
if (payment_id)
pay_length = payment_id->size();

if (pay_length && pay_length != 8 && pay_length != 32)
WIRE_DLOG_THROW(wire::error::schema::binary, "Unexpected binary size");
if (pay_length == 8)
std::memcpy(std::addressof(self.payment_id.short_), payment_id->data(), 8);
if (pay_length == 32)
std::memcpy(std::addressof(self.payment_id.long_), payment_id->data(), 32);

if (rct)
std::memcpy(std::addressof(self.ringct_mask), std::addressof(*rct), sizeof(self.ringct_mask));
else
std::memset(std::addressof(self.ringct_mask), 0, sizeof(self.ringct_mask));

extra flags{};
if (coinbase)
flags = lws::db::coinbase_output;
if (rct)
flags = extra(lws::db::ringct_output | flags);
self.extra = db::pack(flags, pay_length);
}

void write_bytes(wire::writer& dest, const output& self)
{
const std::pair<db::extra, std::uint8_t> unpacked =
Expand Down Expand Up @@ -286,7 +335,8 @@ namespace db
wire::field<11>("mixin_count", self.spend_meta.mixin_count),
wire::field<12>("coinbase", coinbase),
wire::field<13>("fee", self.fee),
wire::field<14>("recipient", self.recipient)
wire::field<14>("recipient", self.recipient),
wire::field<15>("pub", std::cref(self.pub))
);
}

Expand All @@ -296,15 +346,15 @@ namespace db
void map_spend(F& format, T1& self, T2& payment_id)
{
wire::object(format,
wire::field("height", std::ref(self.link.height)),
wire::field("tx_hash", std::ref(self.link.tx_hash)),
WIRE_FIELD(image),
WIRE_FIELD(source),
WIRE_FIELD(timestamp),
WIRE_FIELD(unlock_time),
WIRE_FIELD(mixin_count),
wire::optional_field("payment_id", std::ref(payment_id)),
WIRE_FIELD(sender)
wire::field<0>("height", std::ref(self.link.height)),
wire::field<1>("tx_hash", std::ref(self.link.tx_hash)),
WIRE_FIELD_ID(2, image),
WIRE_FIELD_ID(3, source),
WIRE_FIELD_ID(4, timestamp),
WIRE_FIELD_ID(5, unlock_time),
WIRE_FIELD_ID(6, mixin_count),
wire::optional_field<7>("payment_id", std::ref(payment_id)),
WIRE_FIELD_ID(8, sender)
);
}
}
Expand Down
1 change: 1 addition & 0 deletions src/db/data.h
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,7 @@ namespace db
sizeof(output) == 8 + 32 + (8 * 3) + (4 * 2) + 32 + (8 * 2) + (32 * 3) + 7 + 1 + 32 + 8 + 2 * 4,
"padding in output"
);
void read_bytes(wire::reader&, output&);
void write_bytes(wire::writer&, const output&);

//! Information about a possible spend of a received `output`.
Expand Down
2 changes: 1 addition & 1 deletion src/rest_server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@
#include "util/gamma_picker.h"
#include "util/random_outputs.h"
#include "util/source_location.h"
#include "wire/crypto.h"
#include "wire/adapted/crypto.h"
#include "wire/json.h"

namespace lws
Expand Down
2 changes: 1 addition & 1 deletion src/rpc/admin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
#include "error.h"
#include "span.h" // monero/contrib/epee/include
#include "wire.h"
#include "wire/crypto.h"
#include "wire/adapted/crypto.h"
#include "wire/error.h"
#include "wire/json/write.h"
#include "wire/traits.h"
Expand Down
Loading

0 comments on commit 80604e8

Please sign in to comment.