Skip to content

Commit

Permalink
evm: eth_sendRawTransaction implementation (#1913)
Browse files Browse the repository at this point in the history
* Move ffi files to folder, rename ffi_exports, add cxx header file

* eth_sendRawTransaction implementation
  • Loading branch information
shohamc1 authored Apr 14, 2023
1 parent b63a29e commit dc0b505
Show file tree
Hide file tree
Showing 9 changed files with 1,193 additions and 15 deletions.
2 changes: 1 addition & 1 deletion lib/ain-cpp-imports/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ fn main() {
lib_path.to_str().unwrap()
);
println!(
"cargo:rerun-if-changed={}/masternodes/ffi_exports.h",
"cargo:rerun-if-changed={}/ffi/ffiexports.h",
lib_path.to_str().unwrap()
);
}
8 changes: 7 additions & 1 deletion lib/ain-cpp-imports/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@ use std::error::Error;
#[cxx::bridge]
mod ffi {
unsafe extern "C++" {
include!("masternodes/ffi_exports.h");
include!("ffi/ffiexports.h");

fn getChainId() -> u64;
fn isMining() -> bool;
fn publishEthTransaction(data: Vec<u8>) -> bool;
}
}

Expand All @@ -19,3 +20,8 @@ pub fn is_mining() -> Result<bool, Box<dyn Error>> {
let is_mining = ffi::isMining();
Ok(is_mining)
}

pub fn publish_eth_transaction(data: Vec<u8>) -> Result<bool, Box<dyn Error>> {
let publish = ffi::publishEthTransaction(data);
Ok(publish)
}
24 changes: 23 additions & 1 deletion lib/ain-grpc/src/rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@ use crate::codegen::rpc::{
EthGetBlockTransactionCountByNumberResult, EthGetCodeInput, EthGetCodeResult,
EthGetStorageAtInput, EthGetStorageAtResult, EthGetTransactionByBlockHashAndIndexInput,
EthGetTransactionByBlockNumberAndIndexInput, EthGetTransactionByHashInput, EthMiningResult,
EthTransactionInfo,
EthSendRawTransactionInput, EthSendRawTransactionResult, EthTransactionInfo,
},
EthService,
};
use ain_cpp_imports::publish_eth_transaction;
use ain_evm::handler::Handlers;
use primitive_types::{H256, U256};
use std::convert::Into;
Expand Down Expand Up @@ -85,6 +86,11 @@ pub trait EthServiceApi {
handler: Arc<Handlers>,
input: EthGetStorageAtInput,
) -> Result<EthGetStorageAtResult, jsonrpsee_core::Error>;

fn Eth_SendRawTransaction(
handler: Arc<Handlers>,
input: EthSendRawTransactionInput,
) -> Result<EthSendRawTransactionResult, jsonrpsee_core::Error>;
}

#[allow(non_snake_case)]
Expand Down Expand Up @@ -307,4 +313,20 @@ impl EthServiceApi for EthService {
value: format!("{:#x}", value),
})
}

fn Eth_SendRawTransaction(
_handler: Arc<Handlers>,
input: EthSendRawTransactionInput,
) -> Result<EthSendRawTransactionResult, jsonrpsee_core::Error> {
let EthSendRawTransactionInput { transaction } = input;

let hex = hex::decode(transaction).expect("Invalid transaction");
let stat = publish_eth_transaction(hex).unwrap();

println!("{stat}");

Ok(EthSendRawTransactionResult {
hash: format!("{stat}"),
})
}
}
2 changes: 2 additions & 0 deletions lib/proto/rpc/eth.proto
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,6 @@ service eth {
rpc Eth_GetCode(types.EthGetCodeInput) returns (types.EthGetCodeResult);

rpc Eth_GetStorageAt(types.EthGetStorageAtInput) returns (types.EthGetStorageAtResult);

rpc Eth_SendRawTransaction(types.EthSendRawTransactionInput) returns (types.EthSendRawTransactionResult);
}
4 changes: 2 additions & 2 deletions src/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@ DEFI_CORE_H = \
dbwrapper.h \
limitedmap.h \
logging.h \
ffi/ffiexports.h \
masternodes/accounts.h \
masternodes/accountshistory.h \
masternodes/anchors.h \
Expand All @@ -164,7 +165,6 @@ DEFI_CORE_H = \
masternodes/communityaccounttypes.h \
masternodes/errors.h \
masternodes/evm.h \
masternodes/ffi_exports.h \
masternodes/factory.h \
masternodes/govvariables/attributes.h \
masternodes/govvariables/icx_takerfee_per_btc.h \
Expand Down Expand Up @@ -392,11 +392,11 @@ libdefi_server_a_SOURCES = \
interfaces/chain.cpp \
init.cpp \
dbwrapper.cpp \
ffi/ffiexports.cpp \
masternodes/accounts.cpp \
masternodes/accountshistory.cpp \
masternodes/anchors.cpp \
masternodes/auctionhistory.cpp \
masternodes/ffi_exports.cpp \
masternodes/govvariables/attributes.cpp \
masternodes/govvariables/icx_takerfee_per_btc.cpp \
masternodes/govvariables/loan_daily_reward.cpp \
Expand Down
Loading

0 comments on commit dc0b505

Please sign in to comment.