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
Changes from 2 commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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) | ||
|
@@ -358,13 +359,15 @@ namespace | |
|
||
|
||
std::string | ||
pad_str_to_mod_64(const std::string ¶meter) | ||
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; | ||
} | ||
|
||
|
||
|
@@ -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); | ||
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) | ||
|
@@ -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)}; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) | ||
{ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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