Skip to content

Commit

Permalink
Specify account info request target type
Browse files Browse the repository at this point in the history
  • Loading branch information
pwojcikdev committed Dec 7, 2022
1 parent c66da2c commit 3e143ac
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 21 deletions.
14 changes: 8 additions & 6 deletions nano/core_test/bootstrap_server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ TEST (bootstrap_server, serve_account_blocks)
nano::asc_pull_req::blocks_payload request_payload;
request_payload.start = first_account;
request_payload.count = nano::bootstrap_server::max_blocks;
request_payload.start_type = nano::asc_pull_req::blocks_payload::type::account;
request_payload.start_type = nano::asc_pull_req::hash_type::account;

request.payload = request_payload;
request.update_header ();
Expand Down Expand Up @@ -218,7 +218,7 @@ TEST (bootstrap_server, serve_hash)
nano::asc_pull_req::blocks_payload request_payload;
request_payload.start = blocks.front ()->hash ();
request_payload.count = nano::bootstrap_server::max_blocks;
request_payload.start_type = nano::asc_pull_req::blocks_payload::type::block;
request_payload.start_type = nano::asc_pull_req::hash_type::block;

request.payload = request_payload;
request.update_header ();
Expand Down Expand Up @@ -265,7 +265,7 @@ TEST (bootstrap_server, serve_hash_one)
nano::asc_pull_req::blocks_payload request_payload;
request_payload.start = blocks.front ()->hash ();
request_payload.count = 1;
request_payload.start_type = nano::asc_pull_req::blocks_payload::type::block;
request_payload.start_type = nano::asc_pull_req::hash_type::block;

request.payload = request_payload;
request.update_header ();
Expand Down Expand Up @@ -306,7 +306,7 @@ TEST (bootstrap_server, serve_end_of_chain)
nano::asc_pull_req::blocks_payload request_payload;
request_payload.start = blocks.back ()->hash ();
request_payload.count = nano::bootstrap_server::max_blocks;
request_payload.start_type = nano::asc_pull_req::blocks_payload::type::block;
request_payload.start_type = nano::asc_pull_req::hash_type::block;

request.payload = request_payload;
request.update_header ();
Expand Down Expand Up @@ -347,7 +347,7 @@ TEST (bootstrap_server, serve_missing)
nano::asc_pull_req::blocks_payload request_payload;
request_payload.start = nano::test::random_hash ();
request_payload.count = nano::bootstrap_server::max_blocks;
request_payload.start_type = nano::asc_pull_req::blocks_payload::type::block;
request_payload.start_type = nano::asc_pull_req::hash_type::block;

request.payload = request_payload;
request.update_header ();
Expand Down Expand Up @@ -392,7 +392,7 @@ TEST (bootstrap_server, serve_multiple)
nano::asc_pull_req::blocks_payload request_payload;
request_payload.start = account;
request_payload.count = nano::bootstrap_server::max_blocks;
request_payload.start_type = nano::asc_pull_req::blocks_payload::type::account;
request_payload.start_type = nano::asc_pull_req::hash_type::account;

request.payload = request_payload;
request.update_header ();
Expand Down Expand Up @@ -450,6 +450,7 @@ TEST (bootstrap_server, serve_account_info)

nano::asc_pull_req::account_info_payload request_payload;
request_payload.target = account;
request_payload.target_type = nano::asc_pull_req::hash_type::account;

request.payload = request_payload;
request.update_header ();
Expand Down Expand Up @@ -497,6 +498,7 @@ TEST (bootstrap_server, serve_account_info_missing)

nano::asc_pull_req::account_info_payload request_payload;
request_payload.target = nano::test::random_account ();
request_payload.target_type = nano::asc_pull_req::hash_type::account;

request.payload = request_payload;
request.update_header ();
Expand Down
22 changes: 15 additions & 7 deletions nano/node/bootstrap/bootstrap_server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -176,15 +176,15 @@ nano::asc_pull_ack nano::bootstrap_server::process (nano::transaction const & tr

switch (request.start_type)
{
case asc_pull_req::blocks_payload::type::block:
case asc_pull_req::hash_type::block:
{
if (store.block.exists (transaction, request.start.as_block_hash ()))
{
return prepare_response (transaction, id, request.start.as_block_hash (), count);
}
}
break;
case asc_pull_req::blocks_payload::type::account:
case asc_pull_req::hash_type::account:
{
auto info = store.account.get (transaction, request.start.as_account ());
if (info)
Expand Down Expand Up @@ -261,13 +261,21 @@ nano::asc_pull_ack nano::bootstrap_server::process (const nano::transaction & tr
response.id = id;
response.type = nano::asc_pull_type::account_info;

auto target = request.target.as_account ();
// Try to lookup account assuming target is block hash
if (auto account_from_hash = ledger.account_safe (transaction, request.target.as_block_hash ()); !account_from_hash.is_zero ())
nano::account target{ 0 };
switch (request.target_type)
{
target = account_from_hash;
case asc_pull_req::hash_type::account:
{
target = request.target.as_account ();
}
break;
case asc_pull_req::hash_type::block:
{
// Try to lookup account assuming target is block hash
target = ledger.account_safe (transaction, request.target.as_block_hash ());
}
break;
}
// Otherwise assume target is an actual account

nano::asc_pull_ack::account_info_payload response_payload{};
response_payload.account = target;
Expand Down
16 changes: 8 additions & 8 deletions nano/node/messages.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -414,23 +414,22 @@ class asc_pull_req final : public message
bool verify_consistency () const;

public: // Payload definitions
class blocks_payload
enum class hash_type : uint8_t
{
public:
enum class type : uint8_t
{
block = 1,
account = 2,
};
account = 0,
block = 1,
};

class blocks_payload
{
public:
void serialize (nano::stream &) const;
void deserialize (nano::stream &);

public:
nano::hash_or_account start{ 0 };
uint8_t count{ 0 };
type start_type{ 0 };
asc_pull_req::hash_type start_type{ 0 };
};

class account_info_payload
Expand All @@ -441,6 +440,7 @@ class asc_pull_req final : public message

public:
nano::hash_or_account target{ 0 };
asc_pull_req::hash_type target_type{ 0 };
};

public: // Payload
Expand Down

0 comments on commit 3e143ac

Please # to comment.