Skip to content
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

#187 code copy elimination #15

Merged
merged 2 commits into from
May 24, 2021
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
40 changes: 40 additions & 0 deletions core/src/code.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
use alloc::vec::Vec;
use core::ops::Deref;
use primitive_types::H160;

/// Contract code
#[derive(Clone, Debug)]
#[cfg_attr(feature = "with-codec", derive(codec::Encode, codec::Decode))]
#[cfg_attr(feature = "with-serde", derive(serde::Serialize, serde::Deserialize))]
pub enum Code {
/// Account reference
AccountRef {
/// pointer to the code data
#[cfg_attr(feature = "with-serde", serde(skip))]
#[cfg_attr(feature = "with-serde", serde(default = "core::ptr::null"))]
ptr: *const u8,

/// code size
#[cfg_attr(feature = "with-serde", serde(skip))]
len: usize,

/// code account
account: H160,
},
/// Owned code
Vec {
/// Code storage
#[cfg_attr(feature = "with-serde", serde(with = "serde_bytes"))]
code: Vec<u8>,
},
}

impl Deref for Code {
type Target = [u8];
fn deref(&self) -> &[u8] {
match self {
Code::AccountRef { ptr, len, .. } => unsafe { core::slice::from_raw_parts(*ptr, *len) },
Code::Vec { code } => &code[..],
}
}
}
11 changes: 8 additions & 3 deletions core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
extern crate core;
extern crate alloc;

mod code;
mod memory;
mod stack;
mod valids;
Expand All @@ -16,6 +17,7 @@ mod error;
mod eval;
mod utils;

pub use crate::code::Code;
pub use crate::memory::Memory;
pub use crate::stack::Stack;
pub use crate::valids::Valids;
Expand All @@ -34,8 +36,7 @@ pub struct Machine {
#[cfg_attr(feature = "with-serde", serde(with = "serde_bytes"))]
data: Vec<u8>,
/// Program code.
#[cfg_attr(feature = "with-serde", serde(with = "serde_bytes"))]
code: Vec<u8>,
code: Code,
/// Program counter.
position: Result<usize, ExitReason>,
/// Return value.
Expand All @@ -57,10 +58,14 @@ impl Machine {
pub fn memory(&self) -> &Memory { &self.memory }
/// Mutable reference of machine memory.
pub fn memory_mut(&mut self) -> &mut Memory { &mut self.memory }
/// Reference of machine code.
pub fn code(&self) -> &Code { &self.code }
/// Mutable reference of machine code.
pub fn code_mut(&mut self) -> &mut Code { &mut self.code }

/// Create a new machine with given code and data.
pub fn new(
code: Vec<u8>,
code: Code,
data: Vec<u8>,
stack_limit: usize,
memory_limit: usize
Expand Down
4 changes: 2 additions & 2 deletions runtime/src/handler.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use alloc::vec::Vec;
use primitive_types::{H160, H256, U256};
use crate::{Capture, Stack, ExitError, Opcode,
CreateScheme, Context, Machine, ExitReason};
CreateScheme, Context, Machine, ExitReason, Code};

/// Transfer from source to target, with given value.
#[derive(Clone, Debug)]
Expand Down Expand Up @@ -35,7 +35,7 @@ pub trait Handler {
/// Get code hash of address.
fn code_hash(&self, address: H160) -> H256;
/// Get code of address.
fn code(&self, address: H160) -> Vec<u8>;
fn code(&self, address: H160) -> Code;
/// Get storage value of address at index.
fn storage(&self, address: H160, index: U256) -> U256;
/// Get original storage value of address at index.
Expand Down
12 changes: 11 additions & 1 deletion runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ pub struct Runtime<'config> {
impl<'config> Runtime<'config> {
/// Create a new runtime with given code and data.
pub fn new(
code: Vec<u8>,
code: Code,
data: Vec<u8>,
context: Context,
config: &'config Config,
Expand All @@ -106,6 +106,16 @@ impl<'config> Runtime<'config> {
}
}

/// Restore code data
pub fn finalize_restore<H: Handler>(&mut self, handler: &H) {
let code_account = match self.machine.code() {
Code::AccountRef{ptr: _, len: _, account} => *account,
_ => return
};

*self.machine.code_mut() = handler.code(code_account);
}

/// Get return data
pub fn return_data(&self) -> &Vec<u8> {
&self.return_data_buffer
Expand Down
6 changes: 3 additions & 3 deletions src/backend/memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use primitive_types::{H160, H256, U256};
use sha3::{Digest, Keccak256};
use super::{Basic, Backend, ApplyBackend, Apply, Log};
use evm_runtime::CreateScheme;
use crate::{Capture, Transfer, ExitReason};
use crate::{Capture, Transfer, ExitReason, Code};

/// Vivinity value of a memory backend.
#[derive(Clone, Debug, Eq, PartialEq)]
Expand Down Expand Up @@ -114,8 +114,8 @@ impl<'vicinity> Backend for MemoryBackend<'vicinity> {
self.state.get(&address).map(|v| v.code.len()).unwrap_or(0)
}

fn code(&self, address: H160) -> Vec<u8> {
self.state.get(&address).map(|v| v.code.clone()).unwrap_or_default()
fn code(&self, address: H160) -> Code {
Code::Vec{ code: self.state.get(&address).map(|v| v.code.clone()).unwrap_or_default() }
}

fn storage(&self, address: H160, index: U256) -> U256 {
Expand Down
4 changes: 2 additions & 2 deletions src/backend/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use alloc::vec::Vec;
use core::convert::Infallible;
use primitive_types::{H160, H256, U256};
use evm_runtime::CreateScheme;
use crate::{Capture, Transfer, ExitReason};
use crate::{Capture, Transfer, ExitReason, Code};

/// Basic account information.
#[derive(Clone, Eq, PartialEq, Debug, Default)]
Expand Down Expand Up @@ -95,7 +95,7 @@ pub trait Backend {
/// Get account code size.
fn code_size(&self, address: H160) -> usize;
/// Get account code.
fn code(&self, address: H160) -> Vec<u8>;
fn code(&self, address: H160) -> Code;
/// Get storage value of address at index.
fn storage(&self, address: H160, index: U256) -> U256;

Expand Down
10 changes: 5 additions & 5 deletions src/executor/stack.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use alloc::vec::Vec;
use alloc::collections::{BTreeMap, BTreeSet};
use primitive_types::{U256, H256, H160};
use crate::{ExitError, Stack, Opcode, Capture, Handler, Transfer,
Context, CreateScheme, Runtime, ExitReason, ExitSucceed, Config};
Context, CreateScheme, Runtime, ExitReason, ExitSucceed, Config, Code};
use crate::backend::{Log, Basic, Apply, Backend};
//use crate::gasometer::{self, Gasometer};

Expand Down Expand Up @@ -394,7 +394,7 @@ impl<'backend, 'config, B: Backend> StackExecutor<'backend, 'config, B> {
}
} else {
let code = substate.backend.code(address);
substate.account_mut(address).code = Some(code.clone());
substate.account_mut(address).code = Some((&code[..]).into());

if code.len() != 0 {
let _ = self.merge_fail(substate);
Expand Down Expand Up @@ -434,7 +434,7 @@ impl<'backend, 'config, B: Backend> StackExecutor<'backend, 'config, B> {
}

let mut runtime = Runtime::new(
init_code,
Code::Vec{ code: init_code },
Vec::new(),
context,
self.config,
Expand Down Expand Up @@ -666,9 +666,9 @@ impl<'backend, 'config, B: Backend> Handler for StackExecutor<'backend, 'config,
value
}

fn code(&self, address: H160) -> Vec<u8> {
fn code(&self, address: H160) -> Code {
self.state.get(&address).and_then(|v| {
v.code.clone()
v.code.clone().map(|code| Code::Vec{ code })
}).unwrap_or(self.backend.code(address))
}

Expand Down