Skip to content

Commit

Permalink
Return reason for failure (#2012)
Browse files Browse the repository at this point in the history
Co-authored-by: Peter Bushnell <[email protected]>
  • Loading branch information
Jouzo and Bushstar authored May 26, 2023
1 parent b1ad7e5 commit 9d9a25c
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 32 deletions.
2 changes: 1 addition & 1 deletion lib/ain-cpp-imports/src/bridge.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ pub mod ffi {

fn getChainId() -> u64;
fn isMining() -> bool;
fn publishEthTransaction(data: Vec<u8>) -> bool;
fn publishEthTransaction(data: Vec<u8>) -> String;
fn getAccounts() -> Vec<String>;
fn getDatadir() -> String;
fn getDifficulty(_block_hash: [u8; 32]) -> u32;
Expand Down
4 changes: 2 additions & 2 deletions lib/ain-cpp-imports/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ mod ffi {
pub fn isMining() -> bool {
unimplemented!("{}", UNIMPL_MSG)
}
pub fn publishEthTransaction(_data: Vec<u8>) -> bool {
pub fn publishEthTransaction(_data: Vec<u8>) -> String {
unimplemented!("{}", UNIMPL_MSG)
}
pub fn getAccounts() -> Vec<String> {
Expand Down Expand Up @@ -55,7 +55,7 @@ pub fn is_mining() -> Result<bool, Box<dyn Error>> {
Ok(is_mining)
}

pub fn publish_eth_transaction(data: Vec<u8>) -> Result<bool, Box<dyn Error>> {
pub fn publish_eth_transaction(data: Vec<u8>) -> Result<String, Box<dyn Error>> {
let publish = ffi::publishEthTransaction(data);
Ok(publish)
}
Expand Down
49 changes: 25 additions & 24 deletions lib/ain-grpc/src/rpc/eth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -554,30 +554,31 @@ impl MetachainRPCServer for MetachainRPCModule {
hex::decode(raw_tx).map_err(|e| Error::Custom(format!("Eror decoding TX {e:?}")))?;

match ain_cpp_imports::publish_eth_transaction(hex) {
Ok(true) => {
let signed_tx = SignedTx::try_from(raw_tx)
.map_err(|e| Error::Custom(format!("TX error {e:?}")))?;

debug!(target:"rpc",
"[send_raw_transaction] signed_tx sender : {:#x}",
signed_tx.sender
);
debug!(target:"rpc",
"[send_raw_transaction] signed_tx nonce : {:#x}",
signed_tx.nonce()
);
debug!(target:"rpc",
"[send_raw_transaction] transaction hash : {:#x}",
signed_tx.transaction.hash()
);

Ok(format!("{:#x}", signed_tx.transaction.hash()))
}
Ok(false) => {
debug!(target:"rpc","[send_raw_transaction] Could not publish raw transaction: {tx}");
Err(Error::Custom(format!(
"Could not publish raw transaction: {tx}"
)))
Ok(res_string) => {
if res_string.is_empty() {
let signed_tx = SignedTx::try_from(raw_tx)
.map_err(|e| Error::Custom(format!("TX error {e:?}")))?;

debug!(target:"rpc",
"[send_raw_transaction] signed_tx sender : {:#x}",
signed_tx.sender
);
debug!(target:"rpc",
"[send_raw_transaction] signed_tx nonce : {:#x}",
signed_tx.nonce()
);
debug!(target:"rpc",
"[send_raw_transaction] transaction hash : {:#x}",
signed_tx.transaction.hash()
);

Ok(format!("{:#x}", signed_tx.transaction.hash()))
} else {
debug!(target:"rpc","[send_raw_transaction] Could not publish raw transaction: {tx}");
Err(Error::Custom(format!(
"Could not publish raw transaction: {tx} resaon: {res_string}"
)))
}
}
Err(e) => {
debug!(target:"rpc","[send_raw_transaction] Error publishing TX {e:?}");
Expand Down
8 changes: 4 additions & 4 deletions src/ffi/ffiexports.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ bool isMining() {
return gArgs.GetBoolArg("-gen", false);
}

bool publishEthTransaction(rust::Vec<uint8_t> rawTransaction) {
rust::string publishEthTransaction(rust::Vec<uint8_t> rawTransaction) {
std::vector<uint8_t> evmTx(rawTransaction.size());
std::copy(rawTransaction.begin(), rawTransaction.end(), evmTx.begin());
CDataStream metadata(DfTxMarker, SER_NETWORK, PROTOCOL_VERSION);
Expand Down Expand Up @@ -44,11 +44,11 @@ bool publishEthTransaction(rust::Vec<uint8_t> rawTransaction) {
try {
execTestTx(CTransaction(rawTx), targetHeight, optAuthTx);
send(MakeTransactionRef(std::move(rawTx)), optAuthTx)->GetHash().ToString();
} catch (...) {
return false;
} catch (std::runtime_error& e) {
return e.what();
}

return true;
return {};
}

rust::vec<rust::string> getAccounts() {
Expand Down
2 changes: 1 addition & 1 deletion src/ffi/ffiexports.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

uint64_t getChainId();
bool isMining();
bool publishEthTransaction(rust::Vec<uint8_t> rawTransaction);
rust::string publishEthTransaction(rust::Vec<uint8_t> rawTransaction);
rust::vec<rust::string> getAccounts();
rust::string getDatadir();
uint32_t getDifficulty(std::array<uint8_t, 32> blockHash);
Expand Down

0 comments on commit 9d9a25c

Please sign in to comment.