-
Notifications
You must be signed in to change notification settings - Fork 33
cleos convert un/pack_hex #1578
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
Conversation
|
Nice addition! I've been using to abieos tools to do the same. |
Any missing types I should add? |
No. Could be extended to pull contract specific ABI and unpack that. For example |
|
as is no problems, LGTM to merge as is if you want, but fwiw personally I like the feature set of abieos' tool better. I like that I must specify a type and that built in types work (e.g. I can specify And of course I think we should do a pack_hex if doing this. |
Add support for specifing abi of un/pack_hex
| std::cerr << std::endl; | ||
| } | ||
| }); | ||
|
|
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.
I'd encourage us to seek a way to eliminate the repetitiveness above. A first thought might be to
using try_types = std::tuple<block_state_legacy, signed_block, transaction_trace, etc>;
template <typename... Types>
bool try_each_type(const std::tuple<Types...>&, auto&& func) {
return [&]<std::size_t... Idxs>(std::index_sequence<Idxs...>) {
return (func.template operator()<std::tuple_element_t<Idxs, std::tuple<Types...>>>() || ...);
}(std::make_index_sequence<sizeof...(Types)>{});
}
bool success = try_each_type(try_types{}, [&]<typename Type>() {
try {
auto v = fc::raw::unpack<Type>( packed_blob );
std::cout << fc::json::to_pretty_string(v) << std::endl;
return true;
} catch(...) {}
return false;
});though this only works for the automatic case.
For the non-automatic case, maybe you could instantiate an abi_serializer and then load in these types with add_specialized_unpack_pack(). So it'd be something like
template <typename... Types>
void for_each_type(const std::tuple<Types...>&, auto&& func) {
[&]<std::size_t... Idxs>(std::index_sequence<Idxs...>) {
(func.template operator()<std::tuple_element_t<Idxs, std::tuple<Types...>>>(), ...);
}(std::make_index_sequence<sizeof...(Types)>{});
}
for_each_type(try_types{}, [&]<typename Type>() {
std::string fully_qualified_name = boost::core::demangle(typeid(Type).name());
//XXX strip off everything before last :: here, we don't have something that does it already I guess?
my_abi_serializer.add_specialized_unpack_pack(stripped_name, pack_unpack<Type>());
});Disclaimer: just improvising -- code above may not work or be ideal.
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.
That's pretty cool. I was looking into doing it with boost mp11, but using std::tuple as a list of types, and the demangle to get the string representation, is genius.
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.
programs/cleos/main.cpp
Outdated
| } catch (...) {} | ||
| } else { | ||
| EOS_ASSERT( abi_file.empty(), misc_exception, "--type required if --abi-file specified"); | ||
| } |
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.
Could remove that } and remove the test for !success && type.empty() at line 3049.
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.
Add new
cleosconvertfunctionality ofunpack_hexandpack_hexI have created a one-off exec one too many times and decided to just add this functionality to
cleos. It is handy for decoding the deep-mind output that is hex encoded.