Skip to content
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
53 changes: 52 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion examples/runtime-sdk/rofl-oracle-sgx/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ rofl-utils = { path = "../../../rofl-utils" }
# Third-party dependencies.
anyhow = "1.0"
async-trait = "0.1.77"
ethabi = { version = "18.0.0", default-features = false, features = ["std"] }
solabi = "0.3.0"
tokio = { version = "1.38", features = ["rt", "rt-multi-thread"] }
serde_json = "1.0"

Expand Down
11 changes: 5 additions & 6 deletions examples/runtime-sdk/rofl-oracle-sgx/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ use rofl_app_core::prelude::*;
/// Address where the oracle contract is deployed.
const ORACLE_CONTRACT_ADDRESS: &str = "0x5FbDB2315678afecb367f032d93F642f64180aa3"; // TODO: Replace with your contract address.

/// Type of the submitObservation function inside the contract.
const SUBMIT_OBSERVATION: solabi::FunctionEncoder<(u128,), (bool,)> =
solabi::FunctionEncoder::new(solabi::selector!("submitObservation(uint128)"));

struct OracleApp;

#[async_trait]
Expand Down Expand Up @@ -54,12 +58,7 @@ impl OracleApp {
module_evm::types::Call {
address: ORACLE_CONTRACT_ADDRESS.parse().unwrap(),
value: 0.into(),
data: [
ethabi::short_signature("submitObservation", &[ethabi::ParamType::Uint(128)])
.to_vec(),
ethabi::encode(&[ethabi::Token::Uint(observation.into())]),
]
.concat(),
data: SUBMIT_OBSERVATION.encode_params(&(observation,)),
},
);
tx.set_fee_gas(200_000);
Expand Down
2 changes: 1 addition & 1 deletion examples/runtime-sdk/rofl-oracle-tdx-raw/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ module-evm = { path = "../../../runtime-sdk/modules/evm", package = "oasis-runti
# Third-party dependencies.
anyhow = "1.0"
async-trait = "0.1.77"
ethabi = { version = "18.0.0", default-features = false, features = ["std"] }
solabi = "0.3.0"
tokio = { version = "1.38", features = ["full"] }
reqwest = { version = "0.12", features = ["json"] }
serde_json = "1.0"
11 changes: 5 additions & 6 deletions examples/runtime-sdk/rofl-oracle-tdx-raw/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ use rofl_app_core::prelude::*;
/// Address where the oracle contract is deployed.
const ORACLE_CONTRACT_ADDRESS: &str = "0x5FbDB2315678afecb367f032d93F642f64180aa3"; // TODO: Replace with your contract address.

/// Type of the submitObservation function inside the contract.
const SUBMIT_OBSERVATION: solabi::FunctionEncoder<(u128,), (bool,)> =
solabi::FunctionEncoder::new(solabi::selector!("submitObservation(uint128)"));

struct OracleApp;

#[async_trait]
Expand Down Expand Up @@ -49,12 +53,7 @@ impl OracleApp {
module_evm::types::Call {
address: ORACLE_CONTRACT_ADDRESS.parse().unwrap(),
value: 0.into(),
data: [
ethabi::short_signature("submitObservation", &[ethabi::ParamType::Uint(128)])
.to_vec(),
ethabi::encode(&[ethabi::Token::Uint(observation.into())]),
]
.concat(),
data: SUBMIT_OBSERVATION.encode_params(&(observation,)),
},
);
tx.set_fee_gas(200_000);
Expand Down
34 changes: 22 additions & 12 deletions runtime-sdk-macros/src/evm_derive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,20 +121,21 @@ fn generate_method_call(attr: &EvmMethod, sig: &Signature) -> Option<TokenStream
let mut arg_decls: Vec<TokenStream> = Vec::new();
for (i, evm_ty) in sig.args.iter().enumerate() {
let arg_name = format_ident!("arg_{}", i);
let field_index = syn::Index::from(i);
call_args.push(arg_name.clone());
match evm_ty.as_str() {
"address" => {
decoder_list.push(quote!(::ethabi::ParamType::Address));
decoder_list.push(quote!(::solabi::Address));
arg_decls.push(quote! {
let #arg_name = decoded_args[#i].clone().into_address().unwrap();
let #arg_name = ::primitive_types::H160(decoded_args.#field_index.0);
});
}
"uint256" => {
decoder_list.push(quote!(::ethabi::ParamType::Uint(256)));
decoder_list.push(quote!(::solabi::U256));
let temp_name = format_ident!("{}_uint", arg_name);
arg_decls.push(quote! {
let #temp_name = decoded_args[#i].clone().into_uint().unwrap();
if #temp_name.bits() > (u128::BITS as usize) {
let #temp_name = decoded_args.#field_index.clone();
if *#temp_name.high() != 0 {
return Some(Err(::evm::executor::stack::PrecompileFailure::Error {
exit_status: ::evm::ExitError::Other("integer overflow".into()),
}));
Expand All @@ -152,8 +153,17 @@ fn generate_method_call(attr: &EvmMethod, sig: &Signature) -> Option<TokenStream
}
}
let method_name = sig.name.clone();
let decode_type = if decoder_list.is_empty() {
quote!(())
} else if decoder_list.len() == 1 {
let single_type = &decoder_list[0];
quote!((#single_type,))
} else {
quote!((#(#decoder_list,)*))
};

Some(quote! { {
let decoded_args = match ::ethabi::decode(&[#(#decoder_list),*], &handle.input()[#SELECTOR_LENGTH..]) {
let decoded_args: #decode_type = match ::solabi::decode(&handle.input()[#SELECTOR_LENGTH..]) {
Err(e) => return Some(Err(::evm::executor::stack::PrecompileFailure::Error {
exit_status: ::evm::ExitError::Other("invalid argument".into()),
})),
Expand Down Expand Up @@ -329,7 +339,7 @@ pub fn derive_evm_event(input: DeriveInput) -> TokenStream {
let topics: Vec<TokenStream> = fields.iter().filter_map(|f| {
if f.indexed.is_present() {
let name = f.ident.as_ref().unwrap();
Some(quote! { ::primitive_types::H256::from_slice(::ethabi::encode(&[self.#name.clone()]).as_slice()) })
Some(quote! { ::primitive_types::H256::from_slice(::solabi::encode(&[self.#name.clone()]).as_slice()) })
} else {
None
}
Expand All @@ -350,7 +360,7 @@ pub fn derive_evm_event(input: DeriveInput) -> TokenStream {
quote! { vec![] }
} else {
quote! {
::ethabi::encode(&[
::solabi::encode(&[
#(#data ,)*
])
}
Expand Down Expand Up @@ -408,19 +418,19 @@ pub fn derive_evm_error(input: DeriveInput) -> TokenStream {
"address" => {
// The tuple field for this should be a primitive_types::H160.
abi_tokens.push(quote! {
::ethabi::Token::Address(*#field_ident)
solabi::Address((*#field_ident).into())
});
}
"uint256" => {
// The tuple field for this should be a u128.
abi_tokens.push(quote! {
::ethabi::Token::Uint((*#field_ident).into())
solabi::U256::new(*#field_ident as u128)
});
}
"string" => {
// The tuple field for this should be anything that has a to_string() method.
abi_tokens.push(quote! {
::ethabi::Token::String(#field_ident.to_string())
#field_ident.to_string()
});
}
ty => {
Expand All @@ -439,7 +449,7 @@ pub fn derive_evm_error(input: DeriveInput) -> TokenStream {
} else {
(
quote! { Self::#ident(#(#match_fields,)*) },
quote! { output.extend(::ethabi::encode(&[#(#abi_tokens,)*]).as_slice()); },
quote! { output.extend(::solabi::encode(&(#(#abi_tokens,)*)).as_slice()); },
)
};
variant_encoders.push(quote! {
Expand Down
1 change: 1 addition & 0 deletions runtime-sdk/modules/evm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ hmac = "0.12.1"
rand_core = { version = "0.6.4", default-features = false }

# Ethereum.
solabi = "0.3.0"
ethabi = { version = "18.0.0", default-features = false, features = ["std"] }
ethereum = "0.15"
evm = { git = "https://github.com/oasisprotocol/evm", tag = "v0.39.1-oasis" }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ use std::{fs, path};
#[cfg(fuzzing)]
use honggfuzz::fuzz;

use ethabi::Token;
use primitive_types::H160;

fn gen_calldata() -> Box<dyn Iterator<Item = Vec<u8>>> {
Expand All @@ -13,10 +12,10 @@ fn gen_calldata() -> Box<dyn Iterator<Item = Vec<u8>>> {
let arg = &[0xd5, 0x2b, 0xce, 0x59];

let mut arg_input = arg.to_vec();
arg_input.append(&mut ethabi::encode(&[
Token::Address(H160::zero()),
Token::Uint(42.into()),
]));
arg_input.append(&mut solabi::encode(&(
solabi::Address(H160::zero().into()),
solabi::U256::from(42_u128),
)));

Box::new(vec![direct.to_vec(), arg_input].into_iter())
}
Expand Down
58 changes: 24 additions & 34 deletions runtime-sdk/modules/evm/fuzz/precompile_corpus.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ use std::{fs, path};
#[cfg(fuzzing)]
use honggfuzz::fuzz;

use ethabi::Token;

use oasis_runtime_sdk_evm::precompile::testing::read_test_cases;

fn gen_test_vectors(fixture: &str) -> Box<dyn Iterator<Item = Vec<u8>>> {
Expand All @@ -16,61 +14,53 @@ fn gen_test_vectors(fixture: &str) -> Box<dyn Iterator<Item = Vec<u8>>> {
)
}

fn gen_ethabi(tokens: &[Token]) -> Box<dyn Iterator<Item = Vec<u8>>> {
Box::new(vec![ethabi::encode(tokens)].into_iter())
}

fn gen_x25519() -> Box<dyn Iterator<Item = Vec<u8>>> {
let key = b"this must be the excelentest key";
let nonce = b"complete noncence, and too long.";
let plaintext = b"0123456789";
let ad = b"additional data";

gen_ethabi(&[
Token::FixedBytes(key.to_vec()),
Token::FixedBytes(nonce.to_vec()),
Token::Bytes(plaintext.to_vec()),
Token::Bytes(ad.to_vec()),
])
Box::new(
vec![solabi::encode(&(
key.to_vec(),
nonce.to_vec(),
plaintext.to_vec(),
ad.to_vec(),
))]
.into_iter(),
)
}

fn gen_random_bytes() -> Box<dyn Iterator<Item = Vec<u8>>> {
Box::new(
(0..32).map(|bytes| {
ethabi::encode(&[Token::Uint(bytes.into()), Token::Bytes(vec![0xbe, 0xef])])
}),
)
Box::new((0..32).map(|bytes| solabi::encode(&(bytes, vec![0xbe, 0xef]))))
}

fn gen_keygen() -> Box<dyn Iterator<Item = Vec<u8>>> {
Box::new((0..10).map(|signature_type| {
ethabi::encode(&[
Token::Uint(signature_type.into()),
Token::Bytes(b"01234567890123456789012345678901".to_vec()),
])
solabi::encode(&(signature_type, b"01234567890123456789012345678901".to_vec()))
}))
}

fn gen_sign() -> Box<dyn Iterator<Item = Vec<u8>>> {
Box::new((0..10).map(|signature_type| {
ethabi::encode(&[
Token::Uint(signature_type.into()),
Token::Bytes(b"01234567890123456789012345678901".to_vec()),
Token::Bytes(b"test context".to_vec()),
Token::Bytes(b"test message".to_vec()),
])
solabi::encode(&(
signature_type,
b"01234567890123456789012345678901",
b"test context",
b"test message",
))
}))
}

fn gen_verify() -> Box<dyn Iterator<Item = Vec<u8>>> {
Box::new((0..10).map(|signature_type| {
ethabi::encode(&[
Token::Uint(signature_type.into()),
Token::Bytes(b"01234567890123456789012345678901".to_vec()),
Token::Bytes(b"test context".to_vec()),
Token::Bytes(b"test message".to_vec()),
Token::Bytes(b"01234567890123456789012345678901".to_vec()),
])
solabi::encode(&(
signature_type,
b"01234567890123456789012345678901",
b"test context",
b"test message",
b"01234567890123456789012345678901",
))
}))
}

Expand Down
Loading
Loading