Skip to content

Commit

Permalink
support evm version: Shanghai! (#85)
Browse files Browse the repository at this point in the history
* chore: tmp

* feat: support push0

* feat: supoort shanghai

* support evm version: shanghai
* Supplant DIFFICULTY opcode with PREVRANDAO
* fix test

* chore: bump to 0.4.0

* chore: downgrade cita_trie

* chore: bump to 0.4.1

* fix: push0 lookup failed

* chore: bump to 0.4.2
  • Loading branch information
Pencil-Yao authored Oct 26, 2023
1 parent 27fb444 commit bd1cdcd
Show file tree
Hide file tree
Showing 17 changed files with 222 additions and 183 deletions.
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "cita-vm"
version = "0.3.4"
version = "0.4.2"
authors = ["Cryptape Technologies <[email protected]>"]
edition = "2021"
description = "CITA VM"
Expand All @@ -13,7 +13,7 @@ byteorder = "1.0"
cita_trie = "4.0"
env_logger = "0.10"
ethereum-types = "0.14"
hashbrown = { version = "0.13", features = ["rayon"] }
hashbrown = { version = "0.14", features = ["rayon"] }
hasher = { version="0.1" }
hex = "0.4"
secp256k1 = { package = "libsecp256k1", version = "0.7" }
Expand Down
13 changes: 7 additions & 6 deletions examples/simplestorage.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use std::cell::RefCell;
use std::str::FromStr;
use std::sync::Arc;

use ethereum_types::{Address, U256};
Expand All @@ -16,13 +17,13 @@ fn main() {
99c66a25d59f0aa78f7ebc40748fa1d1fbc335d8d780f284841b30e0365acd9\
60029";
state.new_contract(
&Address::from("0xBd770416a3345F91E4B34576cb804a576fa48EB1"),
&Address::from_str("0xBd770416a3345F91E4B34576cb804a576fa48EB1").unwrap(),
U256::from(10),
U256::from(1),
hex::decode(code).unwrap(),
);
state.new_contract(
&Address::from("0x1000000000000000000000000000000000000000"),
&Address::from_str("0x1000000000000000000000000000000000000000").unwrap(),
U256::from(1_000_000_000_000_000u64),
U256::from(1),
vec![],
Expand All @@ -34,8 +35,8 @@ fn main() {
let config = cita_vm::Config::default();

let tx = cita_vm::Transaction {
from: Address::from("0x1000000000000000000000000000000000000000"),
to: Some(Address::from("0xBd770416a3345F91E4B34576cb804a576fa48EB1")),
from: Address::from_str("0x1000000000000000000000000000000000000000").unwrap(),
to: Some(Address::from_str("0xBd770416a3345F91E4B34576cb804a576fa48EB1").unwrap()),
value: U256::from(0),
nonce: U256::from(1),
gas_limit: 80000,
Expand All @@ -53,8 +54,8 @@ fn main() {
println!("return={:?}", r);

let tx = cita_vm::Transaction {
from: Address::from("0x1000000000000000000000000000000000000000"),
to: Some(Address::from("0xBd770416a3345F91E4B34576cb804a576fa48EB1")),
from: Address::from_str("0x1000000000000000000000000000000000000000").unwrap(),
to: Some(Address::from_str("0xBd770416a3345F91E4B34576cb804a576fa48EB1").unwrap()),
value: U256::from(0),
nonce: U256::from(2),
gas_limit: 80000,
Expand Down
28 changes: 14 additions & 14 deletions src/err.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,20 +26,20 @@ impl error::Error for Error {}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
Error::Evm(e) => return write!(f, "{}", e),
Error::Secp256k1(e) => return write!(f, "{:?}", e),
Error::State(e) => return write!(f, "{}", e),
Error::IO(e) => return write!(f, "{:?}", e),
Error::Str(e) => return write!(f, "{:?}", e),
Error::NotEnoughBaseGas => return write!(f, "NotEnoughBaseGas"),
Error::NotEnoughBalance => return write!(f, "NotEnoughBalance"),
Error::InvalidNonce => return write!(f, "InvalidNonce"),
Error::ContractAlreadyExist => return write!(f, "ContractAlreadyExist"),
Error::ExccedMaxCodeSize => return write!(f, "ExccedMaxCodeSize"),
Error::ExccedMaxBlockGasLimit => return write!(f, "ExccedMaxBlockGasLimit"),
Error::ExccedMaxCallDepth => return write!(f, "ExccedMaxCallDepth"),
Error::CreateInStaticCall => return write!(f, "CreateInStaticCall"),
};
Error::Evm(e) => write!(f, "{}", e),
Error::Secp256k1(e) => write!(f, "{:?}", e),
Error::State(e) => write!(f, "{}", e),
Error::IO(e) => write!(f, "{:?}", e),
Error::Str(e) => write!(f, "{:?}", e),
Error::NotEnoughBaseGas => write!(f, "NotEnoughBaseGas"),
Error::NotEnoughBalance => write!(f, "NotEnoughBalance"),
Error::InvalidNonce => write!(f, "InvalidNonce"),
Error::ContractAlreadyExist => write!(f, "ContractAlreadyExist"),
Error::ExccedMaxCodeSize => write!(f, "ExccedMaxCodeSize"),
Error::ExccedMaxBlockGasLimit => write!(f, "ExccedMaxBlockGasLimit"),
Error::ExccedMaxCallDepth => write!(f, "ExccedMaxCallDepth"),
Error::CreateInStaticCall => write!(f, "CreateInStaticCall"),
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/evm/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ pub fn rpad(slice: Vec<u8>, n: usize) -> Vec<u8> {
let mut padded: Vec<u8> = Vec::with_capacity(n);
let mut part1 = slice;
padded.append(&mut part1);
let mut part2 = vec![0; n as usize - slice_len];
let mut part2 = vec![0; n - slice_len];
padded.append(&mut part2);
padded
}
Expand Down
26 changes: 13 additions & 13 deletions src/evm/err.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,18 @@ impl error::Error for Error {}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
Error::OutOfBounds => return write!(f, "OutOfBounds"),
Error::OutOfGas => return write!(f, "OutOfGas"),
Error::OutOfStack => return write!(f, "OutOfStack"),
Error::OutOfCode => return write!(f, "OutOfCode"),
Error::OutOfData => return write!(f, "OutOfData"),
Error::MutableCallInStaticContext => return write!(f, "MutableCallInStaticContext"),
Error::InvalidOpcode => return write!(f, "InvalidOpcode"),
Error::CallError => return write!(f, "CallError"),
Error::ExccedMaxCodeSize => return write!(f, "ExccedMaxCodeSize"),
Error::InvalidJumpDestination => return write!(f, "InvalidJumpDestination"),
Error::Internal(err) => return write!(f, "Internal error {}", err),
Error::StackUnderflow => return write!(f, "StackUnderflow"),
};
Error::OutOfBounds => write!(f, "OutOfBounds"),
Error::OutOfGas => write!(f, "OutOfGas"),
Error::OutOfStack => write!(f, "OutOfStack"),
Error::OutOfCode => write!(f, "OutOfCode"),
Error::OutOfData => write!(f, "OutOfData"),
Error::MutableCallInStaticContext => write!(f, "MutableCallInStaticContext"),
Error::InvalidOpcode => write!(f, "InvalidOpcode"),
Error::CallError => write!(f, "CallError"),
Error::ExccedMaxCodeSize => write!(f, "ExccedMaxCodeSize"),
Error::InvalidJumpDestination => write!(f, "InvalidJumpDestination"),
Error::Internal(err) => write!(f, "Internal error {}", err),
Error::StackUnderflow => write!(f, "StackUnderflow"),
}
}
}
2 changes: 1 addition & 1 deletion src/evm/extmock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ impl ext::DataProvider for DataProviderMock {
let mut it = interpreter::Interpreter::new(
interpreter::Context::default(),
interpreter::InterpreterConf::default(),
Box::new(DataProviderMock::default()),
Box::<DataProviderMock>::default(),
params,
);
let data_provider = DataProviderMock {
Expand Down
21 changes: 15 additions & 6 deletions src/evm/interpreter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -663,7 +663,7 @@ impl Interpreter {
let shift = shift.as_u32() as usize;
let mut shifted = value >> shift;
if sign {
shifted = shifted | (U256::max_value() << (256 - shift));
shifted |= U256::max_value() << (256 - shift);
}
shifted
};
Expand Down Expand Up @@ -788,7 +788,7 @@ impl Interpreter {
opcodes::OpCode::NUMBER => {
self.stack.push(self.context.number);
}
opcodes::OpCode::DIFFICULTY => {
opcodes::OpCode::PREVRANDAO => {
self.stack.push(self.context.difficulty);
}
opcodes::OpCode::GASLIMIT => {
Expand Down Expand Up @@ -851,6 +851,9 @@ impl Interpreter {
self.stack.push(U256::from(self.gas));
}
opcodes::OpCode::JUMPDEST => {}
opcodes::OpCode::PUSH0 => {
self.stack.push(U256::zero());
}
opcodes::OpCode::PUSH1
| opcodes::OpCode::PUSH2
| opcodes::OpCode::PUSH3
Expand Down Expand Up @@ -1771,10 +1774,16 @@ mod tests {
let mut it = default_interpreter();
it.cfg.eip1283 = true;
assert_eq!(it.gas, it.context.gas_limit);
it.data_provider
.set_storage_origin(&it.params.contract.code_address, H256::zero(), H256::from(origin));
it.data_provider
.set_storage(&it.params.contract.code_address, H256::zero(), H256::from(origin));
it.data_provider.set_storage_origin(
&it.params.contract.code_address,
H256::zero(),
H256::from_low_u64_be(origin),
);
it.data_provider.set_storage(
&it.params.contract.code_address,
H256::zero(),
H256::from_low_u64_be(origin),
);
it.params.contract.code_data = hex::decode(code).unwrap();
it.run().unwrap();
assert_eq!(it.gas, it.context.gas_limit - use_gas);
Expand Down
21 changes: 14 additions & 7 deletions src/evm/opcodes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ pub enum OpCode {
COINBASE = 0x41,
TIMESTAMP = 0x42,
NUMBER = 0x43,
DIFFICULTY = 0x44,
PREVRANDAO = 0x44,
GASLIMIT = 0x45,
CHAINID = 0x46,
SELFBALANCE = 0x47,
Expand All @@ -66,6 +66,7 @@ pub enum OpCode {
MSIZE = 0x59,
GAS = 0x5a,
JUMPDEST = 0x5b,
PUSH0 = 0x5f,
PUSH1 = 0x60,
PUSH2 = 0x61,
PUSH3 = 0x62,
Expand Down Expand Up @@ -196,7 +197,7 @@ impl fmt::Display for OpCode {
OpCode::COINBASE => write!(f, "COINBASE"),
OpCode::TIMESTAMP => write!(f, "TIMESTAMP"),
OpCode::NUMBER => write!(f, "NUMBER"),
OpCode::DIFFICULTY => write!(f, "DIFFICULTY"),
OpCode::PREVRANDAO => write!(f, "PREVRANDAO"),
OpCode::GASLIMIT => write!(f, "GASLIMIT"),
OpCode::CHAINID => write!(f, "CHAINID"),
OpCode::SELFBALANCE => write!(f, "SELFBALANCE"),
Expand All @@ -213,6 +214,7 @@ impl fmt::Display for OpCode {
OpCode::MSIZE => write!(f, "MSIZE"),
OpCode::GAS => write!(f, "GAS"),
OpCode::JUMPDEST => write!(f, "JUMPDEST"),
OpCode::PUSH0 => write!(f, "PUSH0"),
OpCode::PUSH1 => write!(f, "PUSH1"),
OpCode::PUSH2 => write!(f, "PUSH2"),
OpCode::PUSH3 => write!(f, "PUSH3"),
Expand Down Expand Up @@ -372,7 +374,7 @@ impl OpCode {
0x41 => Some(OpCode::COINBASE),
0x42 => Some(OpCode::TIMESTAMP),
0x43 => Some(OpCode::NUMBER),
0x44 => Some(OpCode::DIFFICULTY),
0x44 => Some(OpCode::PREVRANDAO),
0x45 => Some(OpCode::GASLIMIT),
0x46 => Some(OpCode::CHAINID),
0x47 => Some(OpCode::SELFBALANCE),
Expand All @@ -389,6 +391,7 @@ impl OpCode {
0x59 => Some(OpCode::MSIZE),
0x5a => Some(OpCode::GAS),
0x5b => Some(OpCode::JUMPDEST),
0x5f => Some(OpCode::PUSH0),
0x60 => Some(OpCode::PUSH1),
0x61 => Some(OpCode::PUSH2),
0x62 => Some(OpCode::PUSH3),
Expand Down Expand Up @@ -520,7 +523,7 @@ impl OpCode {
OpCode::COINBASE => GasPriceTier::Base,
OpCode::TIMESTAMP => GasPriceTier::Base,
OpCode::NUMBER => GasPriceTier::Base,
OpCode::DIFFICULTY => GasPriceTier::Base,
OpCode::PREVRANDAO => GasPriceTier::Base,
OpCode::GASLIMIT => GasPriceTier::Base,
OpCode::CHAINID => GasPriceTier::VeryLow,
OpCode::SELFBALANCE => GasPriceTier::Low,
Expand All @@ -537,6 +540,7 @@ impl OpCode {
OpCode::MSIZE => GasPriceTier::Base,
OpCode::GAS => GasPriceTier::Base,
OpCode::JUMPDEST => GasPriceTier::Special,
OpCode::PUSH0 => GasPriceTier::Base,
OpCode::PUSH1 => GasPriceTier::VeryLow,
OpCode::PUSH2 => GasPriceTier::VeryLow,
OpCode::PUSH3 => GasPriceTier::VeryLow,
Expand Down Expand Up @@ -667,7 +671,7 @@ impl OpCode {
OpCode::COINBASE => 0,
OpCode::TIMESTAMP => 0,
OpCode::NUMBER => 0,
OpCode::DIFFICULTY => 0,
OpCode::PREVRANDAO => 0,
OpCode::GASLIMIT => 0,
OpCode::CHAINID => 0,
OpCode::SELFBALANCE => 0,
Expand All @@ -684,6 +688,7 @@ impl OpCode {
OpCode::MSIZE => 0,
OpCode::GAS => 0,
OpCode::JUMPDEST => 0,
OpCode::PUSH0 => 0,
OpCode::PUSH1 => 0,
OpCode::PUSH2 => 0,
OpCode::PUSH3 => 0,
Expand Down Expand Up @@ -814,7 +819,7 @@ impl OpCode {
OpCode::COINBASE => 1,
OpCode::TIMESTAMP => 1,
OpCode::NUMBER => 1,
OpCode::DIFFICULTY => 1,
OpCode::PREVRANDAO => 1,
OpCode::GASLIMIT => 1,
OpCode::CHAINID => 1,
OpCode::SELFBALANCE => 1,
Expand All @@ -831,6 +836,7 @@ impl OpCode {
OpCode::MSIZE => 1,
OpCode::GAS => 1,
OpCode::JUMPDEST => 0,
OpCode::PUSH0 => 1,
OpCode::PUSH1 => 1,
OpCode::PUSH2 => 1,
OpCode::PUSH3 => 1,
Expand Down Expand Up @@ -961,7 +967,7 @@ impl OpCode {
OpCode::COINBASE => false,
OpCode::TIMESTAMP => false,
OpCode::NUMBER => false,
OpCode::DIFFICULTY => false,
OpCode::PREVRANDAO => false,
OpCode::GASLIMIT => false,
OpCode::CHAINID => false,
OpCode::SELFBALANCE => false,
Expand All @@ -978,6 +984,7 @@ impl OpCode {
OpCode::MSIZE => false,
OpCode::GAS => false,
OpCode::JUMPDEST => false,
OpCode::PUSH0 => false,
OpCode::PUSH1 => false,
OpCode::PUSH2 => false,
OpCode::PUSH3 => false,
Expand Down
Loading

0 comments on commit bd1cdcd

Please sign in to comment.