Skip to content
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

[3.1] Update cleos to return an error code on failed trx processing #199

Merged
merged 6 commits into from
Sep 22, 2022
Merged
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
Expand Up @@ -441,7 +441,7 @@ class read_only {
get_scheduled_transactions_result get_scheduled_transactions( const get_scheduled_transactions_params& params ) const;
struct compute_transaction_results {
chain::transaction_id_type transaction_id;
fc::variant processed;
fc::variant processed; // "processed" is expected JSON for trxs in cleos
};

struct compute_transaction_params {
Expand Down Expand Up @@ -666,7 +666,7 @@ class read_write {
using push_transaction_params = fc::variant_object;
struct push_transaction_results {
chain::transaction_id_type transaction_id;
fc::variant processed;
fc::variant processed; // "processed" is expected JSON for trxs in cleos
};
void push_transaction(const push_transaction_params& params, chain::plugin_interface::next_function<push_transaction_results> next);

Expand Down
41 changes: 36 additions & 5 deletions programs/cleos/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,7 @@ bool print_request = false;
bool print_response = false;
bool no_auto_keosd = false;
bool verbose = false;
int return_code = 0;

uint8_t tx_max_cpu_usage = 0;
uint32_t tx_max_net_usage = 0;
Expand Down Expand Up @@ -538,6 +539,32 @@ void print_action_tree( const fc::variant& action ) {
}
}

int get_return_code( const fc::variant& result ) {
// if a trx with a processed, then check to see if it failed execution for return value
int r = 0;
if (result.is_object() && result.get_object().contains("processed")) {
const auto& processed = result["processed"];
if( processed.is_object() && processed.get_object().contains( "except" ) ) {
const auto& except = processed["except"];
if( except.is_object() ) {
try {
auto soft_except = except.as<fc::exception>();
auto code = soft_except.code();
if( code > std::numeric_limits<int>::max() ) {
r = 1;
} else {
r = static_cast<int>( code );
}
if( r == 0 ) r = 1;
} catch( ... ) {
r = 1;
}
}
}
}
return r;
}

void print_result( const fc::variant& result ) { try {
if (result.is_object() && result.get_object().contains("processed")) {
const auto& processed = result["processed"];
Expand Down Expand Up @@ -600,6 +627,7 @@ void send_actions(std::vector<chain::action>&& actions, packed_transaction::comp
out << jsonstr;
out.close();
}
return_code = get_return_code( result );
if( tx_print_json ) {
if (jsonstr.length() == 0) {
jsonstr = fc::json::to_pretty_string( result );
Expand All @@ -624,6 +652,7 @@ void send_transaction( signed_transaction& trx, packed_transaction::compression_
out << jsonstr;
out.close();
}
return_code = get_return_code( result );
if( tx_print_json ) {
if (jsonstr.length() == 0) {
jsonstr = fc::json::to_pretty_string( result );
Expand Down Expand Up @@ -4136,14 +4165,16 @@ int main( int argc, char** argv ) {
}
return 1;
} catch ( const std::bad_alloc& ) {
elog("bad alloc");
elog("bad alloc");
return 1;
} catch( const boost::interprocess::bad_alloc& ) {
elog("bad alloc");
elog("bad alloc");
return 1;
} catch (const fc::exception& e) {
return handle_error(e);
return handle_error(e);
} catch (const std::exception& e) {
return handle_error(fc::std_exception_wrapper::from_current_exception(e));
return handle_error(fc::std_exception_wrapper::from_current_exception(e));
}

return 0;
return return_code;
}
2 changes: 1 addition & 1 deletion tests/Node.py
Original file line number Diff line number Diff line change
Expand Up @@ -776,7 +776,7 @@ def publishContract(self, account, contractDir, wasmFile, abiFile, waitForTransB
if shouldFail:
if trans["processed"]["except"] != None:
retMap={}
retMap["returncode"]=0
retMap["returncode"]=1
retMap["cmd"]=cmd
retMap["output"]=bytes(str(trans),'utf-8')
return retMap
Expand Down
5 changes: 3 additions & 2 deletions tests/prod_preactivation_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,9 @@
Print("publish a new bios contract %s should fails because env.is_feature_activated unresolveable" % (contractDir))
retMap = node0.publishContract("eosio", contractDir, wasmFile, abiFile, True, shouldFail=True)

if retMap["output"].decode("utf-8").find("unresolveable") < 0:
errorExit("bios contract not result in expected unresolveable error")
outPut = retMap["output"].decode("utf-8")
if outPut.find("unresolveable") < 0:
errorExit(f"bios contract not result in expected unresolveable error: {outPut}")

secwait = 30
Print("Wait for node 1 to produce...")
Expand Down
3 changes: 2 additions & 1 deletion tests/testUtils.py
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,8 @@ def checkDelayedOutput(popen, cmd, ignoreError=False):
Utils.checkOutputFileWrite(start, cmd, output, error)
if popen.returncode != 0 and not ignoreError:
Utils.Print("ERROR: %s" % error)
raise subprocess.CalledProcessError(returncode=popen.returncode, cmd=cmd, output=error)
# for now just include both stderr and stdout in output, python 3.5+ supports both a stdout and stderr attributes
Copy link
Member

Choose a reason for hiding this comment

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

what is the Python 3.5+ comment here getting at -- even Ubuntu 18 is using 3.6 so if there is a 3.5+ feature we should be able to use it

Copy link
Member Author

Choose a reason for hiding this comment

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

stderr was being placed in output. Python 3.5+ has both an output and a stderr. The better fix is to put stdout into output and stderr into stderr. However, output is used in 20+ places in many tests. I didn't want to make that big of a change in release/3.1. In main I plan to sperate out the stdout and stderr and update the 20+ usages of the CalledProcessError.

raise subprocess.CalledProcessError(returncode=popen.returncode, cmd=cmd, output=error+bytes(" ", 'utf-8')+output)
return output.decode("utf-8")

@staticmethod
Expand Down