Skip to content
This repository has been archived by the owner on Mar 3, 2020. It is now read-only.

KEP-1552 Get Peer Info creates correct ESR request data #341

Merged
merged 3 commits into from Jul 3, 2019
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
66 changes: 39 additions & 27 deletions utils/esr_peer_info.cpp
Expand Up @@ -91,7 +91,8 @@ namespace
)"};

const size_t ESR_RESPONSE_LINE_LENGTH{64};

const size_t REQUIRED_SIZE_MULTIPLE{64};
const off_t PARAMETER_OFFSET{64};

void
trim_right_nulls(std::string& s)
Expand Down Expand Up @@ -358,13 +359,15 @@ namespace


std::string
pad_str_to_mod_64(const std::string &parameter)
pad_str_to_mod_64(std::string parameter)
{
const size_t REQUIRED_MOD{64};
const size_t PADDING_REQUIRED = (REQUIRED_MOD - parameter.size() % REQUIRED_MOD);
std::string result{parameter};
result.insert(result.size(), PADDING_REQUIRED, '0');
return result;
const size_t REMAINDER{parameter.size() % REQUIRED_SIZE_MULTIPLE};
if (REMAINDER)
{
const size_t padding_required = REQUIRED_SIZE_MULTIPLE - REMAINDER;
parameter.insert(parameter.size(), padding_required, '0');
}
return parameter;
}


Expand All @@ -391,8 +394,8 @@ namespace
const std::string
data_string_for_get_peers(const std::string &swarm_id)
{
static const auto NODE_LIST_ABI = str_to_json(GET_NODE_LIST_ABI);
static const auto GET_PEERS_ADDRESS{NODE_LIST_ABI["signature"].asCString() + 2}; // 0x46e76d8b -> 46e76d8b
const auto NODE_LIST_ABI = str_to_json(GET_NODE_LIST_ABI);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you were better as a static here... why don;t you add a json version to the consts instead of converting it

const auto GET_PEERS_ADDRESS{NODE_LIST_ABI["signature"].asCString() + 2}; // 0x46e76d8b -> 46e76d8b

return std::string{"0x"
+ pad_str_to_mod_64(GET_PEERS_ADDRESS)
Expand All @@ -401,35 +404,44 @@ namespace
+ pad_str_to_mod_64(string_to_hex(swarm_id))// hexified swarm id
};
}
}


namespace bzn::utils::esr
{
// TODO: replace this with a function that uses the ABI to create the request data
// data_string_for_get_peer_info has been moved out of the anonymous namespace to make it possible to
// unit test directly.
const std::string
data_string_for_get_peer_info(const std::string &swarm_id, const std::string &peer_id)
data_string_for_get_peer_info(const std::string& swarm_id, const std::string& peer_id)
{
static const auto PEER_INFO_ABI{str_to_json(GET_PEER_INFO_ABI)};
static const auto GET_PEER_INFO_SIGNATURE{PEER_INFO_ABI["signature"].asCString() + 2};
const auto PEER_INFO_ABI{str_to_json(GET_PEER_INFO_ABI)};
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same as above

const auto GET_PEER_INFO_SIGNATURE{PEER_INFO_ABI["signature"].asString()};

const std::string PARAMS
{
size_type_to_hex(swarm_id.size(), 64) // size of swarm id string (pre hexification)
+ pad_str_to_mod_64(string_to_hex(swarm_id)) // parameter 1 - swarm id
+ size_type_to_hex(peer_id.size(), 64) // size of peer id (pre hexification)
+ pad_str_to_mod_64(string_to_hex(peer_id)) // parameter 2 - peer id
};
const std::string SWARM_ID_PARAMETER {
size_type_to_hex(swarm_id.size(), 64) // size of variable parameter
+ pad_str_to_mod_64(string_to_hex(swarm_id)) // swarm id parameter
};

return std::string{"0x"
+ pad_str_to_mod_64(GET_PEER_INFO_SIGNATURE)
+ pad_str_to_mod_64("00000040") // first param type?
+ size_type_to_hex(PARAMS.size() / 2) // size of params blob in bytes, padded to 8 chars
+ PARAMS
const std::string PEER_ID_PARAMETER {
size_type_to_hex(peer_id.size(), 64)
+ pad_str_to_mod_64(string_to_hex(peer_id))
};

const std::string PREAMBLE {
GET_PEER_INFO_SIGNATURE
+ size_type_to_hex( PARAMETER_OFFSET, 64)
+ size_type_to_hex( PARAMETER_OFFSET + SWARM_ID_PARAMETER.size() / 2, 64)
};

return std::string{
PREAMBLE
+ SWARM_ID_PARAMETER
+ PEER_ID_PARAMETER
};
}
}


namespace bzn::utils::esr
{
std::vector<std::string>
get_peer_ids(const bzn::uuid_t& swarm_id, const std::string& esr_address, const std::string& url)
{
Expand Down
95 changes: 86 additions & 9 deletions utils/test/utils_test.cpp
Expand Up @@ -410,6 +410,21 @@ TEST(util_test, test_that_esr_returns_peers_list)
}


TEST(util_test, test_that_esr_returns_peers_list_with_deletions)
{
// There is a swarm in the ESR called BluzelleSwarm4. It contins 2 nodes, there were originally 4, the 2nd and 4th
// node have been deleted which will leave two 0 length strings.
const std::string CONTRACT{"D5B3d7C061F817ab05aF9Fab3b61EEe036e4f4fc"};
const std::string SWARM_ID{"BluzelleSwarm4"};
const std::vector<bzn::uuid_t> ACCEPTED_IDS{"MFYwEAYHKoZIzj0CAQYFK4EEAAoDQgAEvsJhAghU+bWyVjiSg5VMHJKLqs2NGKGNmWTkl3zU8syKIPo+CEuXey7YAAS7pMOFErkjpMyi4FEWnG2ysuN1Pg==", "MFYwEAYHKoZIzj0CAQYFK4EEAAoDQgAEbrgU0EQ2UHRG5UFFfcDIaqfarezPvT1uRJAA++8mRc3FgK3DYeTrOdVVopCBLA+EaNiJdkLDObvRkkFny6185g=="};

const auto peer_ids = bzn::utils::esr::get_peer_ids( SWARM_ID, CONTRACT, bzn::utils::ROPSTEN_URL);

EXPECT_EQ(ACCEPTED_IDS.size(), peer_ids.size());
EXPECT_EQ(ACCEPTED_IDS, peer_ids);
}


TEST(util_test, test_that_esr_fails_nicely)
{
const std::string ESR_CONTRACT{"3a38a7ed11431975fa4a5403a246850479e7b930"};
Expand Down Expand Up @@ -454,16 +469,78 @@ TEST(util_test, test_that_esr_returns_peer_info_with_large_node_name)
}


TEST(util_test, test_that_esr_returns_peers_list_with_deletions)
TEST(util_test, test_that_live_esr_returns_peer_info)
{
// There is a swarm in the ESR called BluzelleSwarm4. It contins 2 nodes, there were originally 4, the 2nd and 4th
// node have been deleted which will leave two 0 length strings.
const std::string CONTRACT{"D5B3d7C061F817ab05aF9Fab3b61EEe036e4f4fc"};
const std::string SWARM_ID{"BluzelleSwarm4"};
const std::vector<bzn::uuid_t> ACCEPTED_IDS{"MFYwEAYHKoZIzj0CAQYFK4EEAAoDQgAEvsJhAghU+bWyVjiSg5VMHJKLqs2NGKGNmWTkl3zU8syKIPo+CEuXey7YAAS7pMOFErkjpMyi4FEWnG2ysuN1Pg==", "MFYwEAYHKoZIzj0CAQYFK4EEAAoDQgAEbrgU0EQ2UHRG5UFFfcDIaqfarezPvT1uRJAA++8mRc3FgK3DYeTrOdVVopCBLA+EaNiJdkLDObvRkkFny6185g=="};
const std::string ESR_CONTRACT{"f039E760a4E97b1E50689ea6572DD74a46359aD9"};
const std::string ESR_URL{bzn::utils::ROPSTEN_URL+ "/uvek7IebbbHoP8Bb9NkV"};

const auto peer_ids = bzn::utils::esr::get_peer_ids( SWARM_ID, CONTRACT, bzn::utils::ROPSTEN_URL);
{
const std::string SWARM_ID{"testnet-1"};
const std::string NODE_ID{"MFYwEAYHKoZIzj0CAQYFK4EEAAoDQgAEpVjlefytrB8r4bz8+CDf9z66biQBe5v/hJ2P5/+HdfM3zAee7aWkAzkHBVjNSEym8/7ifpmKLXmm9oB4w7ISpg=="};
const auto peer_info = bzn::utils::esr::get_peer_info(SWARM_ID, NODE_ID, ESR_CONTRACT, ESR_URL);

EXPECT_EQ(peer_info.uuid, NODE_ID);
EXPECT_EQ(peer_info.host, "54.162.31.254");
EXPECT_EQ(peer_info.port, 51010);
EXPECT_EQ(peer_info.name, "node_0_testnet-1");
}
}

EXPECT_EQ(ACCEPTED_IDS.size(), peer_ids.size());
EXPECT_EQ(ACCEPTED_IDS, peer_ids);

TEST(util_test, test_that_live_esr_returns_second_peer_info)
{
const std::string ESR_CONTRACT{"f039E760a4E97b1E50689ea6572DD74a46359aD9"};
const std::string ESR_URL{bzn::utils::ROPSTEN_URL + "/uvek7IebbbHoP8Bb9NkV"};
{
const std::string SWARM_ID{"testnet-1"};
const std::string NODE_ID{
"MFYwEAYHKoZIzj0CAQYFK4EEAAoDQgAE1/Hdxl9+ey34mbYfRtgY44o+Q2jtXU+6QLUDvDGLvYqBMkZu1oKYo91MePry4GRNrQ4BPJP2QLRzZ6dtBwn83w=="};
const auto peer_info = bzn::utils::esr::get_peer_info(SWARM_ID, NODE_ID, ESR_CONTRACT, ESR_URL);

EXPECT_EQ(peer_info.uuid, NODE_ID);
EXPECT_EQ(peer_info.host, "13.57.199.69");
EXPECT_EQ(peer_info.port, 51010);
EXPECT_EQ(peer_info.name, "node_0_testnet-1");
}
}



namespace bzn::utils::esr
{
const std::string data_string_for_get_peer_info(const std::string &swarm_id, const std::string &peer_id);
}



TEST(util_test, test_request_data_validity)
{
std::map<std::pair<std::string,std::string>,std::string> accepted_data
{
{{"debug-swarm","nodeuuid111111111111111111111111"}, "0xcc8575cb00000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000000080000000000000000000000000000000000000000000000000000000000000000b64656275672d737761726d00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000206e6f646575756964313131313131313131313131313131313131313131313131"},
{{"debug-swarm", "nodeuuid1111111111111111111111111"}, "0xcc8575cb00000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000000080000000000000000000000000000000000000000000000000000000000000000b64656275672d737761726d00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000216e6f6465757569643131313131313131313131313131313131313131313131313100000000000000000000000000000000000000000000000000000000000000"},
{{"debug-swarm", "nodeuuid111111111111111111111111111111111111111111111111111111111"},"0xcc8575cb00000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000000080000000000000000000000000000000000000000000000000000000000000000b64656275672d737761726d00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000416e6f64657575696431313131313131313131313131313131313131313131313131313131313131313131313131313131313131313131313131313131313131313100000000000000000000000000000000000000000000000000000000000000"},
{{"debug-swarm","nodeuuid11111111111111111111111111111111111111111111111111111111111111111111111111111111111111111"},"0xcc8575cb00000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000000080000000000000000000000000000000000000000000000000000000000000000b64656275672d737761726d00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000616e6f646575756964313131313131313131313131313131313131313131313131313131313131313131313131313131313131313131313131313131313131313131313131313131313131313131313131313131313131313131313131313131313100000000000000000000000000000000000000000000000000000000000000"},
{{"debug-swarm1111111111111111111111","nodeuuid111111111111111111111111"},"0xcc8575cb000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000a0000000000000000000000000000000000000000000000000000000000000002164656275672d737761726d313131313131313131313131313131313131313131310000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000206e6f646575756964313131313131313131313131313131313131313131313131"},
{{"debug-swarm1111111111111111111111","nodeuuid1111111111111111111111111"},"0xcc8575cb000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000a0000000000000000000000000000000000000000000000000000000000000002164656275672d737761726d313131313131313131313131313131313131313131310000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000216e6f6465757569643131313131313131313131313131313131313131313131313100000000000000000000000000000000000000000000000000000000000000"},
{{"debug-swarm1111111111111111111111","nodeuuid111111111111111111111111111111111111111111111111111111111"},"0xcc8575cb000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000a0000000000000000000000000000000000000000000000000000000000000002164656275672d737761726d313131313131313131313131313131313131313131310000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000416e6f64657575696431313131313131313131313131313131313131313131313131313131313131313131313131313131313131313131313131313131313131313100000000000000000000000000000000000000000000000000000000000000"},
{{"debug-swarm1111111111111111111111","nodeuuid11111111111111111111111111111111111111111111111111111111111111111111111111111111111111111"},"0xcc8575cb000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000a0000000000000000000000000000000000000000000000000000000000000002164656275672d737761726d313131313131313131313131313131313131313131310000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000616e6f646575756964313131313131313131313131313131313131313131313131313131313131313131313131313131313131313131313131313131313131313131313131313131313131313131313131313131313131313131313131313131313100000000000000000000000000000000000000000000000000000000000000"},
{{"debug-swarm111111111111111111111111111111111111111111111111111111","nodeuuid111111111111111111111111"},"0xcc8575cb000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000c0000000000000000000000000000000000000000000000000000000000000004164656275672d737761726d3131313131313131313131313131313131313131313131313131313131313131313131313131313131313131313131313131313131310000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000206e6f646575756964313131313131313131313131313131313131313131313131"},
{{"debug-swarm111111111111111111111111111111111111111111111111111111","nodeuuid1111111111111111111111111"},"0xcc8575cb000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000c0000000000000000000000000000000000000000000000000000000000000004164656275672d737761726d3131313131313131313131313131313131313131313131313131313131313131313131313131313131313131313131313131313131310000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000216e6f6465757569643131313131313131313131313131313131313131313131313100000000000000000000000000000000000000000000000000000000000000"},
{{"debug-swarm111111111111111111111111111111111111111111111111111111","nodeuuid111111111111111111111111111111111111111111111111111111111"},"0xcc8575cb000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000c0000000000000000000000000000000000000000000000000000000000000004164656275672d737761726d3131313131313131313131313131313131313131313131313131313131313131313131313131313131313131313131313131313131310000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000416e6f64657575696431313131313131313131313131313131313131313131313131313131313131313131313131313131313131313131313131313131313131313100000000000000000000000000000000000000000000000000000000000000"},
{{"debug-swarm11111111111111111111111111111111111111111111111111111111111111111111111111111111111111","nodeuuid111111111111111111111111"},"0xcc8575cb000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000e0000000000000000000000000000000000000000000000000000000000000006164656275672d737761726d31313131313131313131313131313131313131313131313131313131313131313131313131313131313131313131313131313131313131313131313131313131313131313131313131313131313131313131313131310000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000206e6f646575756964313131313131313131313131313131313131313131313131"},
{{"debug-swarm11111111111111111111111111111111111111111111111111111111111111111111111111111111111111","nodeuuid1111111111111111111111111"},"0xcc8575cb000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000e0000000000000000000000000000000000000000000000000000000000000006164656275672d737761726d31313131313131313131313131313131313131313131313131313131313131313131313131313131313131313131313131313131313131313131313131313131313131313131313131313131313131313131313131310000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000216e6f6465757569643131313131313131313131313131313131313131313131313100000000000000000000000000000000000000000000000000000000000000"},
{{"debug-swarm11111111111111111111111111111111111111111111111111111111111111111111111111111111111111","nodeuuid111111111111111111111111111111111111111111111111111111111"},"0xcc8575cb000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000e0000000000000000000000000000000000000000000000000000000000000006164656275672d737761726d31313131313131313131313131313131313131313131313131313131313131313131313131313131313131313131313131313131313131313131313131313131313131313131313131313131313131313131313131310000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000416e6f64657575696431313131313131313131313131313131313131313131313131313131313131313131313131313131313131313131313131313131313131313100000000000000000000000000000000000000000000000000000000000000"},
{{"debug-swarm11111111111111111111111111111111111111111111111111111111111111111111111111111111111111","nodeuuid11111111111111111111111111111111111111111111111111111111111111111111111111111111111111111"},"0xcc8575cb000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000e0000000000000000000000000000000000000000000000000000000000000006164656275672d737761726d31313131313131313131313131313131313131313131313131313131313131313131313131313131313131313131313131313131313131313131313131313131313131313131313131313131313131313131313131310000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000616e6f646575756964313131313131313131313131313131313131313131313131313131313131313131313131313131313131313131313131313131313131313131313131313131313131313131313131313131313131313131313131313131313100000000000000000000000000000000000000000000000000000000000000"},
};

for(const auto& info : accepted_data)
{
const auto& SWARM_ID{info.first.first};
const auto& PEER_ID{info.first.second};
const auto& ACCEPTED_REQUEST{info.second};
const auto& ACTUAL_REQUEST{bzn::utils::esr::data_string_for_get_peer_info(SWARM_ID,PEER_ID)};
EXPECT_EQ(ACCEPTED_REQUEST.size(), ACTUAL_REQUEST.size());
EXPECT_STRCASEEQ(ACCEPTED_REQUEST.c_str(), ACTUAL_REQUEST.c_str());
}
}