KEP-1552 Get Peer Info creates correct ESR request data #341
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -358,13 +358,16 @@ 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; | ||
static const size_t REQUIRED_SIZE_MULTIPLE{64}; | ||
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; | ||
} | ||
|
||
|
||
|
@@ -401,35 +404,45 @@ 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) | ||
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. fix coding standard |
||
{ | ||
static const off_t PARAMETER_OFFSET{64}; | ||
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. move to unnamed namespace 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. All statics moved to anonymous namespace where appropriate |
||
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}; | ||
static const auto GET_PEER_INFO_SIGNATURE{PEER_INFO_ABI["signature"].asString()}; | ||
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. namespace please 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. Done |
||
|
||
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) | ||
{ | ||
|
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.
please stop using statics... move to unnamed namespaces.
You incur a performance hit for every call of a function.
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.
ok