Skip to content

Commit

Permalink
Merge pull request #199 from AntelopeIO/GH-139-cleos-return-codes-3.1
Browse files Browse the repository at this point in the history
[3.1] Update cleos to return an error code on failed trx processing
  • Loading branch information
heifner authored Sep 22, 2022
2 parents b7f969d + ced3887 commit 508ff97
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 11 deletions.
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
raise subprocess.CalledProcessError(returncode=popen.returncode, cmd=cmd, output=error+bytes(" ", 'utf-8')+output)
return output.decode("utf-8")

@staticmethod
Expand Down

0 comments on commit 508ff97

Please sign in to comment.