Skip to content

Conversation

@heifner
Copy link
Contributor

@heifner heifner commented Jun 4, 2025

Add new cleos convert functionality of unpack_hex and pack_hex

I 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.

@ericpassmore
Copy link
Contributor

Nice addition! I've been using to abieos tools to do the same.

@heifner heifner added the OCI Work exclusive to OCI team label Jun 4, 2025
@heifner
Copy link
Contributor Author

heifner commented Jun 4, 2025

Nice addition! I've been using to abieos tools to do the same.

Any missing types I should add?

@ericpassmore
Copy link
Contributor

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 action_results.

@heifner heifner added this to the Spring v1.3.0-rc1 milestone Jun 4, 2025
@spoonincode
Copy link
Contributor

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 bool[] or public_key? and it works), and that I can use an abi file to supplement the built in types.

And of course I think we should do a pack_hex if doing this.

@heifner heifner marked this pull request as draft June 5, 2025 20:02
@heifner heifner changed the title cleos convert unpack_hex cleos convert un/pack_hex Jun 9, 2025
heifner added 2 commits June 9, 2025 13:59
Add support for specifing abi of un/pack_hex
std::cerr << std::endl;
}
});

Copy link
Contributor

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.

Copy link
Contributor

@greg7mdp greg7mdp Jun 12, 2025

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@heifner heifner marked this pull request as ready for review June 12, 2025 15:51
@heifner heifner linked an issue Jun 12, 2025 that may be closed by this pull request
} catch (...) {}
} else {
EOS_ASSERT( abi_file.empty(), misc_exception, "--type required if --abi-file specified");
}
Copy link
Contributor

@greg7mdp greg7mdp Jun 12, 2025

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@heifner heifner requested a review from spoonincode June 16, 2025 15:45
@heifner heifner merged commit 6fea27b into main Jun 24, 2025
69 of 71 checks passed
@heifner heifner deleted the cleos-unpack_hex branch June 24, 2025 19:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

OCI Work exclusive to OCI team

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Feature: cleos convert unpack_hex

6 participants