Merge pull request #391 from ethereum/rust-example-expand

rust: implement the save_return_block_number example in example-rust-vm
This commit is contained in:
Alex Beregszaszi 2019-08-08 21:19:19 +01:00 committed by GitHub
commit 598432e5e5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 34 additions and 6 deletions

View File

@ -199,6 +199,9 @@ jobs:
name: Test with evmc-vmtester
command: |
~/build/test/evmc-vmtester target/debug/libexamplerustvm.so
- run:
name: Test with evmc-example
command: |
~/build/examples/evmc-example target/debug/libexamplerustvm.so
bindings-rust-asan-combined:
@ -227,6 +230,9 @@ jobs:
name: Test with evmc-vmtester
command: |
~/build/test/evmc-vmtester target/x86_64-unknown-linux-gnu/debug/libexamplerustvm.so
- run:
name: Test with evmc-example
command: |
~/build/examples/evmc-example target/x86_64-unknown-linux-gnu/debug/libexamplerustvm.so
workflows:

View File

@ -21,12 +21,33 @@ impl EvmcVm for ExampleRustVM {
message: &ExecutionMessage,
_context: &mut ExecutionContext,
) -> ExecutionResult {
let is_create = message.kind() == evmc_sys::evmc_call_kind::EVMC_CREATE;
if is_create {
ExecutionResult::failure()
} else {
ExecutionResult::success(66, Some(&[0xc0, 0xff, 0xee]))
if message.kind() != evmc_sys::evmc_call_kind::EVMC_CALL {
return ExecutionResult::failure();
}
if _code.len() == 0 {
return ExecutionResult::failure();
}
let tx_context = _context.get_tx_context().clone();
let save_return_block_number: Vec<u8> = vec![
0x43, 0x60, 0x00, 0x55, 0x43, 0x60, 0x00, 0x52, 0x59, 0x60, 0x00, 0xf3,
];
if save_return_block_number != _code {
return ExecutionResult::failure();
}
assert!(tx_context.block_number <= 255);
let block_number = tx_context.block_number as u8;
let storage_key = Bytes32::default();
let mut storage_value = Bytes32::default();
storage_value.bytes[31] = block_number;
_context.set_storage(&message.destination(), &storage_key, &storage_value);
let ret = format!("{}", block_number).into_bytes();
ExecutionResult::success(message.gas() / 2, Some(&ret))
}
}

View File

@ -50,6 +50,7 @@ int main(int argc, char* argv[])
tx_context.block_gas_limit = gas * 2;
struct evmc_context* ctx = example_host_create_context(tx_context);
struct evmc_message msg;
msg.kind = EVMC_CALL;
msg.sender = addr;
msg.destination = addr;
msg.value = value;