LNS: bitwise operators for extra_field, removing uneeded KV_*OPT

This commit is contained in:
Doyle 2020-03-06 13:05:43 +11:00
parent 00befd0605
commit 2125af28d4
3 changed files with 26 additions and 39 deletions

View File

@ -67,10 +67,11 @@ namespace lns
{
enum struct extra_field : uint8_t
{
owner = 1 << 0,
backup_owner = 1 << 1,
signature = 1 << 2,
encrypted_value = 1 << 3,
none = 0,
owner = 1 << 0,
backup_owner = 1 << 1,
signature = 1 << 2,
encrypted_value = 1 << 3,
// Bit Masks
updatable_fields = (extra_field::owner | extra_field::backup_owner | extra_field::encrypted_value),
@ -78,7 +79,12 @@ enum struct extra_field : uint8_t
buy = (extra_field::buy_no_backup | extra_field::backup_owner),
all = (extra_field::updatable_fields | extra_field::signature),
};
};
constexpr inline extra_field operator|(extra_field a, extra_field b) { return static_cast<extra_field>(static_cast<uint8_t>(a) | static_cast<uint8_t>(b)); }
constexpr inline extra_field operator&(extra_field a, extra_field b) { return static_cast<extra_field>(static_cast<uint8_t>(a) & static_cast<uint8_t>(b)); }
constexpr inline extra_field& operator|=(extra_field& a, extra_field b) { return a = a | b; }
constexpr inline extra_field& operator&=(extra_field& a, extra_field b) { return a = a & b; }
}
namespace service_nodes {
enum class new_state : uint16_t
@ -412,9 +418,8 @@ namespace cryptonote
crypto::generic_signature signature = {};
std::string encrypted_value; // binary format of the name->value mapping
void set_field (lns::extra_field bit) { fields = static_cast<lns::extra_field>(static_cast<uint8_t>(fields) | static_cast<uint8_t>(bit)); }
bool field_is_set (lns::extra_field bit) const { return (static_cast<uint8_t>(fields) & static_cast<uint8_t>(bit)) == static_cast<uint8_t>(bit); }
bool field_any_set(lns::extra_field bit) const { return (static_cast<uint8_t>(fields) & static_cast<uint8_t>(bit)) > 0; }
bool field_is_set (lns::extra_field bit) const { return (fields & bit) == bit; }
bool field_any_set(lns::extra_field bit) const { return (fields & bit) != lns::extra_field::none; }
bool is_updating() const { return field_is_set(lns::extra_field::signature) && field_any_set(lns::extra_field::updatable_fields); }
bool is_buying() const { return (fields == lns::extra_field::buy || fields == lns::extra_field::buy_no_backup); }
@ -449,23 +454,23 @@ namespace cryptonote
result.signature = signature;
result.type = type;
result.name_hash = name_hash;
result.set_field(lns::extra_field::signature);
result.fields |= lns::extra_field::signature;
if (encrypted_value.size())
{
result.set_field(lns::extra_field::encrypted_value);
result.fields |= lns::extra_field::encrypted_value;
result.encrypted_value = std::string(reinterpret_cast<char const *>(encrypted_value.data()), encrypted_value.size());
}
if (owner)
{
result.set_field(lns::extra_field::owner);
result.fields |= lns::extra_field::owner;
result.owner = *owner;
}
if (backup_owner)
{
result.set_field(lns::extra_field::backup_owner);
result.fields |= lns::extra_field::backup_owner;
result.backup_owner = *backup_owner;
}

View File

@ -1221,9 +1221,9 @@ static bool add_lns_entry(lns::name_system_db &lns_db, uint64_t height, cryptono
stream << R"(UPDATE "mappings" SET )";
for (size_t i = 0; i < column_count; i++)
{
if (i) stream << ", ";
auto column_type = columns[i];
stream << "\"" << mapping_record_column_string(column_type) << "\" = ?";
if (column_type != columns[column_count - 1]) stream << ", ";
}
columns[column_count++] = mapping_record_column::type;
@ -1355,26 +1355,7 @@ struct replay_lns_tx
static std::vector<replay_lns_tx> find_lns_txs_to_replay(cryptonote::Blockchain const &blockchain, lns::mapping_record const &mapping, uint64_t blockchain_height)
{
/*
-----------------------------------------------------------------------------------------------
Detach Logic: Simple Case
-----------------------------------------------------------------------------------------------
LNS Buy @ Height 100: LNS Record={field1=a1, field2=b1, field3=c1}
LNS Update @ Height 200: LNS Record={field1=a2 }
LNS Update @ Height 300: LNS Record={ field2=b2 }
LNS Update @ Height 400: LNS Record={ field3=c2}
Blockchain detaches to height 301, the target LNS record now looks like
{field1=a2, field2=b2, field3=c1}
Our current LNS record looks like
{field1=a2, field2=b2, field3=c3}
To rebuild our record, find the closest LNS Update that is earlier than the
detach height. If we run out of transactions to run back to, then the LNS
entry is just deleted.
-----------------------------------------------------------------------------------------------
Detach Logic: Advance Case
Detach Logic
-----------------------------------------------------------------------------------------------
LNS Buy @ Height 100: LNS Record={field1=a1, field2=b1, field3=c1}
LNS Update @ Height 200: LNS Record={field1=a2 }
@ -1398,8 +1379,9 @@ static std::vector<replay_lns_tx> find_lns_txs_to_replay(cryptonote::Blockchain
a state representative of pre-detach height.
i.e. Go back to the closest LNS record to the detach height, at height 300.
Next, iterate back until all LNS fields have been updated at a point in
time before the detach height (i.e. height 200 with field=a2).
Next, iterate back until all LNS fields have been touched at a point in
time before the detach height (i.e. height 200 with field=a2). Replay the
transactions.
*/
std::vector<replay_lns_tx> result;

View File

@ -1493,9 +1493,9 @@ bool loki_name_system_invalid_tx_extra_params::generate(std::vector<test_event_e
std::string name = "my_lns_name";
cryptonote::tx_extra_loki_name_system valid_data = {};
valid_data.set_field(lns::extra_field::buy_no_backup);
valid_data.owner = miner_key.key;
valid_data.type = lns::mapping_type::wallet;
valid_data.fields |= lns::extra_field::buy_no_backup;
valid_data.owner = miner_key.key;
valid_data.type = lns::mapping_type::wallet;
valid_data.encrypted_value = helper_encrypt_lns_value(name, miner_key.wallet_value).to_string();
valid_data.name_hash = lns::name_to_hash(name);
@ -1902,7 +1902,7 @@ bool loki_name_system_name_value_max_lengths::generate(std::vector<test_event_en
lns_keys_t miner_key = make_lns_keys(miner);
cryptonote::tx_extra_loki_name_system data = {};
data.set_field(lns::extra_field::buy_no_backup);
data.fields |= lns::extra_field::buy_no_backup;
data.owner = miner_key.key;
// Wallet