Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Specify whether bootstrap pull should start at block hash or account #4018

Merged
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
8 changes: 8 additions & 0 deletions nano/core_test/bootstrap_server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +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::hash_type::account;

request.payload = request_payload;
request.update_header ();
Expand Down Expand Up @@ -217,6 +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::hash_type::block;

request.payload = request_payload;
request.update_header ();
Expand Down Expand Up @@ -263,6 +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::hash_type::block;

request.payload = request_payload;
request.update_header ();
Expand Down Expand Up @@ -303,6 +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::hash_type::block;

request.payload = request_payload;
request.update_header ();
Expand Down Expand Up @@ -343,6 +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::hash_type::block;

request.payload = request_payload;
request.update_header ();
Expand Down Expand Up @@ -387,6 +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::hash_type::account;

request.payload = request_payload;
request.update_header ();
Expand Down Expand Up @@ -444,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 @@ -491,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
45 changes: 28 additions & 17 deletions nano/node/bootstrap/bootstrap_server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -174,23 +174,26 @@ nano::asc_pull_ack nano::bootstrap_server::process (nano::transaction const & tr
{
const std::size_t count = std::min (static_cast<std::size_t> (request.count), max_blocks);

// `start` can represent either account or block hash
if (store.block.exists (transaction, request.start.as_block_hash ()))
switch (request.start_type)
{
return prepare_response (transaction, id, request.start.as_block_hash (), count);
}
if (store.account.exists (transaction, request.start.as_account ()))
{
auto info = store.account.get (transaction, request.start.as_account ());
if (info)
case asc_pull_req::hash_type::block:
{
// Start from open block if pulling by account
return prepare_response (transaction, id, info->open_block, count);
if (store.block.exists (transaction, request.start.as_block_hash ()))
{
return prepare_response (transaction, id, request.start.as_block_hash (), count);
}
}
else
break;
case asc_pull_req::hash_type::account:
{
debug_assert (false, "account exists but cannot be retrieved");
auto info = store.account.get (transaction, request.start.as_account ());
if (info)
{
// Start from open block if pulling by account
return prepare_response (transaction, id, info->open_block, count);
}
}
break;
}

// Neither block nor account found, send empty response to indicate that
Expand Down Expand Up @@ -258,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
4 changes: 4 additions & 0 deletions nano/node/messages.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1747,12 +1747,14 @@ void nano::asc_pull_req::blocks_payload::serialize (nano::stream & stream) const
{
nano::write (stream, start);
nano::write (stream, count);
nano::write (stream, start_type);
}

void nano::asc_pull_req::blocks_payload::deserialize (nano::stream & stream)
{
nano::read (stream, start);
nano::read (stream, count);
nano::read (stream, start_type);
}

/*
Expand All @@ -1762,11 +1764,13 @@ void nano::asc_pull_req::blocks_payload::deserialize (nano::stream & stream)
void nano::asc_pull_req::account_info_payload::serialize (stream & stream) const
{
nano::write (stream, target);
nano::write (stream, target_type);
}

void nano::asc_pull_req::account_info_payload::deserialize (stream & stream)
{
nano::read (stream, target);
nano::read (stream, target_type);
}

/*
Expand Down
8 changes: 8 additions & 0 deletions nano/node/messages.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -414,6 +414,12 @@ class asc_pull_req final : public message
bool verify_consistency () const;

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

class blocks_payload
{
public:
Expand All @@ -423,6 +429,7 @@ class asc_pull_req final : public message
public:
nano::hash_or_account start{ 0 };
uint8_t count{ 0 };
asc_pull_req::hash_type start_type{ 0 };
};

class account_info_payload
Expand All @@ -433,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