Skip to content
This repository has been archived by the owner on Aug 2, 2022. It is now read-only.

cleos remove wasm to wast conversion 📦 #10816

Merged
merged 3 commits into from
Oct 19, 2021
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion libraries/chain/include/eosio/chain/wast_to_wasm.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ namespace eosio { namespace chain {

std::vector<uint8_t> wast_to_wasm( const std::string& wast );
std::string wasm_to_wast( const std::vector<uint8_t>& wasm, bool strip_names );
std::string wasm_to_wast( const uint8_t* data, uint64_t size, bool strip_names );
std::string wasm_to_wast( const uint8_t* data, uint64_t size, bool strip_names, bool check_limits = true );
Copy link
Contributor

Choose a reason for hiding this comment

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

can all the wasm_to_wast stuff be ripped out?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes. It is not used, so might as well.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Removed.

Copy link
Contributor

Choose a reason for hiding this comment

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

it's probably possible to purge more, down in the libraries/wasm-jit/Source/WAST (and include dir). That would be highly desirable, but maybe not critical at the moment

Copy link
Contributor

Choose a reason for hiding this comment

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

ah, hm, can't remove all too much in there since unit tests since use wast


} } /// eosio::chain
18 changes: 14 additions & 4 deletions libraries/chain/wast_to_wasm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include <sstream>
#include <iomanip>
#include <fc/exception/exception.hpp>
#include <fc/scoped_exit.hpp>
#include <eosio/chain/exceptions.hpp>

namespace eosio { namespace chain {
Expand Down Expand Up @@ -62,16 +63,25 @@ namespace eosio { namespace chain {
return wasm_to_wast( wasm.data(), wasm.size(), strip_names );
} /// wasm_to_wast

std::string wasm_to_wast( const uint8_t* data, uint64_t size, bool strip_names )
{ try {
std::string wasm_to_wast( const uint8_t* data, uint64_t size, bool strip_names, bool check_limits )
{
try {
auto reset_check_limits = fc::make_scoped_exit([old_value=WASM::check_limits](){
WASM::check_limits = old_value;
});
WASM::check_limits = check_limits;
IR::Module module;
Serialization::MemoryInputStream stream((const U8*)data,size);
WASM::serialize(stream,module);
if(strip_names)
module.userSections.clear();
// Print the module to WAST.
return WAST::print(module);
} FC_CAPTURE_AND_RETHROW() } /// wasm_to_wast

} catch(const Serialization::FatalSerializationException& e) {
EOS_ASSERT( false, wasm_exception, "error converting to wast: ${msg}", ("msg",e.message) );
} catch(const IR::ValidationException& e) {
EOS_ASSERT( false, wasm_exception, "error converting to wast: ${msg}", ("msg",e.message) );
} FC_CAPTURE_AND_RETHROW()
}

} } // eosio::chain
26 changes: 8 additions & 18 deletions programs/cleos/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,6 @@ Usage: ./cleos create account [OPTIONS] creator name OwnerKey ActiveKey

#include <eosio/chain/name.hpp>
#include <eosio/chain/config.hpp>
#include <eosio/chain/wast_to_wasm.hpp>
#include <eosio/chain/trace.hpp>
#include <eosio/chain_plugin/chain_plugin.hpp>
#include <eosio/chain/contract_types.hpp>
Expand Down Expand Up @@ -2754,14 +2753,14 @@ int main( int argc, char** argv ) {
// get code
string codeFilename;
string abiFilename;
bool code_as_wasm = false;
bool code_as_wasm = true;
auto getCode = get->add_subcommand("code", localized("Retrieve the code and ABI for an account"));
getCode->add_option("name", accountName, localized("The name of the account whose code should be retrieved"))->required();
getCode->add_option("-c,--code",codeFilename, localized("The name of the file to save the contract .wast/wasm to") );
getCode->add_option("-c,--code",codeFilename, localized("The name of the file to save the contract wasm to") );
getCode->add_option("-a,--abi",abiFilename, localized("The name of the file to save the contract .abi to") );
getCode->add_flag("--wasm", code_as_wasm, localized("Save contract as wasm"));
getCode->add_flag("--wasm", code_as_wasm, localized("Save contract as wasm (ignored, default)"));
getCode->callback([&] {
string code_hash, wasm, wast, abi;
string code_hash, wasm, abi;
try {
const auto result = call(get_raw_code_and_abi_func, fc::mutable_variant_object("account_name", accountName));
const std::vector<char> wasm_v = result["wasm"].as_blob().data;
Expand All @@ -2773,8 +2772,6 @@ int main( int argc, char** argv ) {
code_hash = (string)hash;

wasm = string(wasm_v.begin(), wasm_v.end());
if(!code_as_wasm && wasm_v.size())
wast = wasm_to_wast((const uint8_t*)wasm_v.data(), wasm_v.size(), false);

abi_def abi_d;
if(abi_serializer::to_abi(abi_v, abi_d))
Expand All @@ -2784,25 +2781,18 @@ int main( int argc, char** argv ) {
//see if this is an old nodeos that doesn't support get_raw_code_and_abi
const auto old_result = call(get_code_func, fc::mutable_variant_object("account_name", accountName)("code_as_wasm",code_as_wasm));
code_hash = old_result["code_hash"].as_string();
if(code_as_wasm) {
wasm = old_result["wasm"].as_string();
std::cout << localized("Warning: communicating to older ${n} which returns malformed binary wasm", ("n", node_executable_name)) << std::endl;
}
else
wast = old_result["wast"].as_string();
wasm = old_result["wasm"].as_string();
std::cout << localized("Warning: communicating to older ${n} which returns malformed binary wasm", ("n", node_executable_name)) << std::endl;
abi = fc::json::to_pretty_string(old_result["abi"]);
}

std::cout << localized("code hash: ${code_hash}", ("code_hash", code_hash)) << std::endl;

if( codeFilename.size() ){
std::cout << localized("saving ${type} to ${codeFilename}", ("type", (code_as_wasm ? "wasm" : "wast"))("codeFilename", codeFilename)) << std::endl;
std::cout << localized("saving wasm to ${codeFilename}", ("codeFilename", codeFilename)) << std::endl;

std::ofstream out( codeFilename.c_str() );
if(code_as_wasm)
out << wasm;
else
out << wast;
out << wasm;
}
if( abiFilename.size() ) {
std::cout << localized("saving abi to ${abiFilename}", ("abiFilename", abiFilename)) << std::endl;
Expand Down