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 -> main] Update cleos to return an error code on failed trx processing #212

Merged
merged 10 commits into from
Sep 22, 2022
Original file line number Diff line number Diff line change
Expand Up @@ -479,7 +479,7 @@ class read_only {
get_scheduled_transactions_result get_scheduled_transactions( const get_scheduled_transactions_params& params, const fc::time_point& deadline ) 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 @@ -738,7 +738,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
40 changes: 35 additions & 5 deletions programs/cleos/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,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 @@ -622,6 +623,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 @@ -685,6 +712,7 @@ void send_actions(std::vector<chain::action>&& actions, const std::vector<public
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 @@ -4436,14 +4464,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/TestHarness/Node.py
Original file line number Diff line number Diff line change
Expand Up @@ -802,7 +802,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
3 changes: 2 additions & 1 deletion tests/TestHarness/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 Author

Choose a reason for hiding this comment

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

I plan to address this comment in a follow-on PR. Plan on moving stdout to output and stderr to stderr. Then update all uses to use stderr instead of output; except for the one existing use that the stdout was added for.

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

@staticmethod
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 @@ -107,8 +107,9 @@
Print("publish a new bios contract %s should fails because env.is_feature_activated unresolveable" % (contractDir))
retMap = node0.publishContract(cluster.eosioAccount, 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