This repository was archived by the owner on Apr 8, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 166
Expand file tree
/
Copy pathlib.rs
More file actions
80 lines (63 loc) · 2.3 KB
/
lib.rs
File metadata and controls
80 lines (63 loc) · 2.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
// EVMC: Ethereum Client-VM Connector API.
// Copyright 2019 The EVMC Authors.
// Licensed under the Apache License, Version 2.0.
use core::str::FromStr;
use evmc_declare::evmc_declare_vm;
use evmc_vm::*;
#[evmc_declare_vm("ExampleRustVM", "evm, precompiles", "12.0.0-alpha.0")]
pub struct ExampleRustVM {
verbosity: i8,
}
impl EvmcVm for ExampleRustVM {
fn init() -> Self {
Self { verbosity: 0 }
}
fn set_option(&mut self, key: &str, value: &str) -> Result<(), SetOptionError> {
if key == "verbose" {
if value.is_empty() {
return Err(SetOptionError::InvalidValue);
}
self.verbosity = i8::from_str(value).map_err(|_| SetOptionError::InvalidValue)?;
if self.verbosity > 9 {
return Err(SetOptionError::InvalidValue);
}
return Ok(());
}
Err(SetOptionError::InvalidKey)
}
fn execute<'a>(
&self,
_revision: Revision,
message: &'a ExecutionMessage,
_context: Option<&'a mut ExecutionContext<'a>>,
) -> ExecutionResult {
if _context.is_none() {
return ExecutionResult::failure();
}
let _context = _context.unwrap();
if self.verbosity > 0 {
println!("execution started");
}
if message.kind() != MessageKind::EVMC_CALL {
return ExecutionResult::failure();
}
if message.code().unwrap().is_empty() {
return ExecutionResult::failure();
}
let tx_context = *_context.get_tx_context();
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 != *message.code().unwrap() {
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.recipient(), &storage_key, &storage_value);
let ret = format!("{}", block_number).into_bytes();
ExecutionResult::success(message.gas() / 2, 0, Some(&ret))
}
}