-
Notifications
You must be signed in to change notification settings - Fork 305
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
Add rust binding client #527
Open
CaptainVincent
wants to merge
9
commits into
ethereum:master
Choose a base branch
from
second-state:rust-binding-client
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
b3d6ab2
rust: cherry-pick b0c40df4f3ced6cac21bd45328334ba1ecd924a6
CaptainVincent 20285d3
rust: add evmc client binding
CaptainVincent e35812f
license: consistent with official used
CaptainVincent f726610
rust: reorganize alias types
CaptainVincent 5b2cc1a
rust: make evmc-client thread safety with HostContext
CaptainVincent 5b2b4f2
rust: use evmc c loader replace pure rust version
CaptainVincent 737a683
rust: refactor avoid hostcontext with lifetime issue
CaptainVincent e54e3e5
rust: call add salt supported
CaptainVincent e5e955a
rust: use git clone evmc source before build and link loader
CaptainVincent File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
/target | ||
**/*.rs.bk | ||
/Cargo.lock |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
# EVMC: Ethereum Client-VM Connector API. | ||
# Copyright 2019 The EVMC Authors. | ||
# Licensed under the Apache License, Version 2.0. | ||
|
||
[package] | ||
name = "evmc-client" | ||
version = "7.4.0" | ||
authors = ["Zigang Lin <vincent@secondstate.io>"] | ||
license = "Apache-2.0" | ||
repository = "https://github.com/ethereum/evmc" | ||
description = "Bindings to EVMC (Client/Host specific)" | ||
edition = "2018" | ||
build = "build.rs" | ||
|
||
[dependencies] | ||
evmc-sys = { path = "../evmc-sys" } | ||
evmc-vm = { path = "../evmc-vm" } | ||
enum_primitive = "0.1.1" | ||
num = "0.3" | ||
|
||
[build-dependencies] | ||
cmake = "0.1.44" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
// Copyright (C) 2020 Second State. | ||
// This file is part of EVMC-Client. | ||
|
||
// EVMC-Client is free software: you can redistribute it and/or modify | ||
// it under the terms of the GNU Affero General Public License as | ||
// published by the Free Software Foundation, either version 3 of the | ||
// License, or (at your option) any later version. | ||
|
||
// EVMC-Client is distributed in the hope that it will be useful, | ||
// but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
// GNU Affero General Public License for more details. | ||
|
||
// You should have received a copy of the GNU Affero General Public License | ||
// along with this program. If not, see <https://www.gnu.org/licenses/>. | ||
|
||
use std::env; | ||
use std::path::{Path, PathBuf}; | ||
use std::process::Command; | ||
extern crate cmake; | ||
use cmake::Config; | ||
|
||
macro_rules! get(($name:expr) => (ok!(env::var($name)))); | ||
macro_rules! ok(($expression:expr) => ($expression.unwrap())); | ||
|
||
const REPOSITORY: &'static str = "https://github.com/second-state/evmc.git"; | ||
const TAG: &'static str = "v7.4.0-rust-evmc-client-rc.2"; | ||
|
||
fn run<F>(name: &str, mut configure: F) | ||
where | ||
F: FnMut(&mut Command) -> &mut Command, | ||
{ | ||
let mut command = Command::new(name); | ||
let configured = configure(&mut command); | ||
if !ok!(configured.status()).success() { | ||
panic!("failed to execute {:?}", configured); | ||
} | ||
} | ||
|
||
fn build_from_src() { | ||
let source = PathBuf::from(&get!("CARGO_MANIFEST_DIR")).join(format!("target/evmc-{}", TAG)); | ||
if !Path::new(&source.join(".git")).exists() { | ||
run("git", |command| { | ||
command | ||
.arg("clone") | ||
.arg(format!("--branch={}", TAG)) | ||
.arg("--recursive") | ||
.arg(REPOSITORY) | ||
.arg(&source) | ||
}); | ||
} | ||
|
||
let dst = Config::new(source).build(); | ||
let evmc_path = Path::new(&dst).join("build/lib/loader"); | ||
println!("cargo:rustc-link-search=native={}", evmc_path.display()); | ||
println!("cargo:rustc-link-lib=static=evmc-loader"); | ||
} | ||
|
||
fn main() { | ||
build_from_src(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,237 @@ | ||
/* EVMC: Ethereum Client-VM Connector API. | ||
* Copyright 2019 The EVMC Authors. | ||
* Licensed under the Apache License, Version 2.0. | ||
*/ | ||
|
||
use crate::types::*; | ||
use evmc_sys as ffi; | ||
use std::mem; | ||
|
||
#[repr(C)] | ||
pub(crate) struct ExtendedContext<'a> { | ||
pub hctx: &'a mut dyn HostContext, | ||
} | ||
|
||
pub trait HostContext { | ||
fn account_exists(&mut self, addr: &Address) -> bool; | ||
fn get_storage(&mut self, addr: &Address, key: &Bytes32) -> Bytes32; | ||
fn set_storage(&mut self, addr: &Address, key: &Bytes32, value: &Bytes32) -> StorageStatus; | ||
fn get_balance(&mut self, addr: &Address) -> Bytes32; | ||
fn get_code_size(&mut self, addr: &Address) -> usize; | ||
fn get_code_hash(&mut self, addr: &Address) -> Bytes32; | ||
fn copy_code( | ||
&mut self, | ||
addr: &Address, | ||
offset: &usize, | ||
buffer_data: &*mut u8, | ||
buffer_size: &usize, | ||
) -> usize; | ||
fn selfdestruct(&mut self, addr: &Address, beneficiary: &Address); | ||
fn get_tx_context(&mut self) -> (Bytes32, Address, Address, i64, i64, i64, Bytes32, Bytes32); | ||
fn get_block_hash(&mut self, number: i64) -> Bytes32; | ||
fn emit_log(&mut self, addr: &Address, topics: &Vec<Bytes32>, data: &Bytes); | ||
fn call( | ||
&mut self, | ||
kind: MessageKind, | ||
destination: &Address, | ||
sender: &Address, | ||
value: &Bytes32, | ||
input: &Bytes, | ||
gas: i64, | ||
depth: i32, | ||
is_static: bool, | ||
salt: &Bytes32, | ||
) -> (Vec<u8>, i64, Address, StatusCode); | ||
} | ||
|
||
pub(crate) fn get_evmc_host_interface() -> ffi::evmc_host_interface { | ||
ffi::evmc_host_interface { | ||
account_exists: Some(account_exists), | ||
get_storage: Some(get_storage), | ||
set_storage: Some(set_storage), | ||
get_balance: Some(get_balance), | ||
get_code_size: Some(get_code_size), | ||
get_code_hash: Some(get_code_hash), | ||
copy_code: Some(copy_code), | ||
selfdestruct: Some(selfdestruct), | ||
call: Some(call), | ||
get_tx_context: Some(get_tx_context), | ||
get_block_hash: Some(get_block_hash), | ||
emit_log: Some(emit_log), | ||
} | ||
} | ||
|
||
unsafe extern "C" fn account_exists( | ||
context: *mut ffi::evmc_host_context, | ||
address: *const ffi::evmc_address, | ||
) -> bool { | ||
return (*(context as *mut ExtendedContext)) | ||
.hctx | ||
.account_exists(&(*address).bytes); | ||
} | ||
|
||
unsafe extern "C" fn get_storage( | ||
context: *mut ffi::evmc_host_context, | ||
address: *const ffi::evmc_address, | ||
key: *const ffi::evmc_bytes32, | ||
) -> ffi::evmc_bytes32 { | ||
return ffi::evmc_bytes32 { | ||
bytes: (*(context as *mut ExtendedContext)) | ||
.hctx | ||
.get_storage(&(*address).bytes, &(*key).bytes), | ||
}; | ||
} | ||
|
||
unsafe extern "C" fn set_storage( | ||
context: *mut ffi::evmc_host_context, | ||
address: *const ffi::evmc_address, | ||
key: *const ffi::evmc_bytes32, | ||
value: *const ffi::evmc_bytes32, | ||
) -> ffi::evmc_storage_status { | ||
return (*(context as *mut ExtendedContext)).hctx.set_storage( | ||
&(*address).bytes, | ||
&(*key).bytes, | ||
&(*value).bytes, | ||
); | ||
} | ||
|
||
unsafe extern "C" fn get_balance( | ||
context: *mut ffi::evmc_host_context, | ||
address: *const ffi::evmc_address, | ||
) -> ffi::evmc_uint256be { | ||
return ffi::evmc_uint256be { | ||
bytes: (*(context as *mut ExtendedContext)) | ||
.hctx | ||
.get_balance(&(*address).bytes), | ||
}; | ||
} | ||
|
||
unsafe extern "C" fn get_code_size( | ||
context: *mut ffi::evmc_host_context, | ||
address: *const ffi::evmc_address, | ||
) -> usize { | ||
return (*(context as *mut ExtendedContext)) | ||
.hctx | ||
.get_code_size(&(*address).bytes); | ||
} | ||
|
||
unsafe extern "C" fn get_code_hash( | ||
context: *mut ffi::evmc_host_context, | ||
address: *const ffi::evmc_address, | ||
) -> ffi::evmc_bytes32 { | ||
return ffi::evmc_bytes32 { | ||
bytes: (*(context as *mut ExtendedContext)) | ||
.hctx | ||
.get_code_hash(&(*address).bytes), | ||
}; | ||
} | ||
|
||
unsafe extern "C" fn copy_code( | ||
context: *mut ffi::evmc_host_context, | ||
address: *const ffi::evmc_address, | ||
code_offset: usize, | ||
buffer_data: *mut u8, | ||
buffer_size: usize, | ||
) -> usize { | ||
return (*(context as *mut ExtendedContext)).hctx.copy_code( | ||
&(*address).bytes, | ||
&code_offset, | ||
&buffer_data, | ||
&buffer_size, | ||
); | ||
} | ||
|
||
unsafe extern "C" fn selfdestruct( | ||
context: *mut ffi::evmc_host_context, | ||
address: *const ffi::evmc_address, | ||
beneficiary: *const ffi::evmc_address, | ||
) { | ||
(*(context as *mut ExtendedContext)) | ||
.hctx | ||
.selfdestruct(&(*address).bytes, &(*beneficiary).bytes) | ||
} | ||
|
||
unsafe extern "C" fn get_tx_context(context: *mut ffi::evmc_host_context) -> ffi::evmc_tx_context { | ||
let (gas_price, origin, coinbase, number, timestamp, gas_limit, difficulty, chain_id) = | ||
(*(context as *mut ExtendedContext)).hctx.get_tx_context(); | ||
return ffi::evmc_tx_context { | ||
tx_gas_price: evmc_sys::evmc_bytes32 { bytes: gas_price }, | ||
tx_origin: evmc_sys::evmc_address { bytes: origin }, | ||
block_coinbase: evmc_sys::evmc_address { bytes: coinbase }, | ||
block_number: number, | ||
block_timestamp: timestamp, | ||
block_gas_limit: gas_limit, | ||
block_difficulty: evmc_sys::evmc_bytes32 { bytes: difficulty }, | ||
chain_id: evmc_sys::evmc_bytes32 { bytes: chain_id }, | ||
}; | ||
} | ||
|
||
unsafe extern "C" fn get_block_hash( | ||
context: *mut ffi::evmc_host_context, | ||
number: i64, | ||
) -> ffi::evmc_bytes32 { | ||
return ffi::evmc_bytes32 { | ||
bytes: (*(context as *mut ExtendedContext)) | ||
.hctx | ||
.get_block_hash(number), | ||
}; | ||
} | ||
|
||
unsafe extern "C" fn emit_log( | ||
context: *mut ffi::evmc_host_context, | ||
address: *const ffi::evmc_address, | ||
data: *const u8, | ||
data_size: usize, | ||
topics: *const ffi::evmc_bytes32, | ||
topics_count: usize, | ||
) { | ||
let ts = &std::slice::from_raw_parts(topics, topics_count) | ||
.iter() | ||
.map(|topic| topic.bytes) | ||
.collect::<Vec<_>>(); | ||
(*(context as *mut ExtendedContext)).hctx.emit_log( | ||
&(*address).bytes, | ||
&ts, | ||
&std::slice::from_raw_parts(data, data_size), | ||
); | ||
} | ||
|
||
unsafe extern "C" fn release(result: *const ffi::evmc_result) { | ||
drop(std::slice::from_raw_parts( | ||
(*result).output_data, | ||
(*result).output_size, | ||
)); | ||
} | ||
|
||
pub unsafe extern "C" fn call( | ||
context: *mut ffi::evmc_host_context, | ||
msg: *const ffi::evmc_message, | ||
) -> ffi::evmc_result { | ||
let msg = *msg; | ||
let (output, gas_left, create_address, status_code) = | ||
(*(context as *mut ExtendedContext)).hctx.call( | ||
msg.kind, | ||
&msg.destination.bytes, | ||
&msg.sender.bytes, | ||
&msg.value.bytes, | ||
&std::slice::from_raw_parts(msg.input_data, msg.input_size), | ||
msg.gas, | ||
msg.depth, | ||
msg.flags != 0, | ||
&msg.create2_salt.bytes, | ||
); | ||
let ptr = output.as_ptr(); | ||
let len = output.len(); | ||
mem::forget(output); | ||
return ffi::evmc_result { | ||
status_code: status_code, | ||
gas_left: gas_left, | ||
output_data: ptr, | ||
output_size: len, | ||
release: Some(release), | ||
create_address: ffi::evmc_address { | ||
bytes: create_address, | ||
}, | ||
padding: [0u8; 4], | ||
}; | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This entire function could be reduced to
hctx.call().into()
is usingExecutionResult
fromevmc-vm/lib.rs
because that implements the translation and memory handling already.