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

feat(storage) : add transient storage for cancun #292

Merged
merged 24 commits into from
Dec 3, 2024
Merged
Show file tree
Hide file tree
Changes from 8 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
4 changes: 2 additions & 2 deletions codegen/src/asm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
//! TODO: refactor this module with Result as outputs. (issue-21)

use crate::{Buffer, Error, Result};
use opcodes::{for_each_shanghai_operator, OpCode as _, ShangHai as OpCode};
use opcodes::{for_each_cancun_operator, Cancun as OpCode, OpCode as _};

/// Low level assembler implementation for EVM.
#[derive(Default, Clone, Debug)]
Expand Down Expand Up @@ -141,5 +141,5 @@ macro_rules! impl_opcodes {

/// Basic instruction implementations
impl Assembler {
for_each_shanghai_operator!(impl_opcodes);
for_each_cancun_operator!(impl_opcodes);
}
2 changes: 1 addition & 1 deletion codegen/src/masm/cmp.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Comparison Instructions

use crate::{MacroAssembler, Result};
use opcodes::ShangHai as OpCode;
use opcodes::Cancun as OpCode;

impl MacroAssembler {
/// Greater than or equal comparison.
Expand Down
2 changes: 1 addition & 1 deletion codegen/src/visitor/call.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use crate::{
Error, Function, Result,
};
use anyhow::anyhow;
use opcodes::ShangHai as OpCode;
use opcodes::Cancun as OpCode;

impl Function {
/// The call indirect instruction calls a function indirectly
Expand Down
24 changes: 17 additions & 7 deletions codegen/src/wasm/host.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
use crate::{Error, Result};
use anyhow::anyhow;
use core::str::FromStr;
use opcodes::{OpCode as _, ShangHai as OpCode};
use opcodes::{Cancun as OpCode, OpCode as _};

/// EVM built-in function.
#[derive(Clone, Copy, Debug, PartialOrd, Ord, PartialEq, Eq)]
Expand All @@ -12,6 +12,8 @@ pub enum HostFunc {
Evm(OpCode),
/// No operations, this only covers `push_$ty` at the moment.
NoOp,
/// Transient storage operations
TransientOp(OpCode),
malik672 marked this conversation as resolved.
Show resolved Hide resolved
// Zinkc helper functions
//
/// Emit ABI to the compiler.
Expand Down Expand Up @@ -39,6 +41,7 @@ impl HostFunc {
pub fn stack_out(&self) -> u8 {
match self {
Self::Evm(op) => op.stack_out() as u8,
Self::TransientOp(op) => op.stack_out() as u8,
malik672 marked this conversation as resolved.
Show resolved Hide resolved
_ => 0,
}
}
Expand All @@ -53,19 +56,26 @@ impl TryFrom<(&str, &str)> for HostFunc {
("asm", name) => {
if name.starts_with("sload") {
Ok(Self::Evm(OpCode::SLOAD))
} else if name.starts_with("tload") {
Ok(Self::TransientOp(OpCode::TLOAD))
malik672 marked this conversation as resolved.
Show resolved Hide resolved
} else if name.starts_with("revert") {
let count = name.trim_start_matches("revert");

// TODO: use anyhow instead of Error
Ok(Self::Revert(count.parse().map_err(|e| anyhow!("{e}"))?))
} else {
Ok(Self::NoOp)
}
}
("evm", name) => Ok(Self::Evm(OpCode::from_str(name).map_err(|_| {
tracing::error!("Failed to load host function: {:?}", import);
Error::HostFuncNotFound(module.into(), name.into())
})?)),
("evm", name) => match name {
"tstore" => Ok(Self::TransientOp(OpCode::TSTORE)),
"tload" => Ok(Self::TransientOp(OpCode::TLOAD)),
"mcopy" => Ok(Self::TransientOp(OpCode::MCOPY)),
"blobhash" => Ok(Self::TransientOp(OpCode::BLOBHASH)),
"blobbasefee" => Ok(Self::TransientOp(OpCode::BLOBBASEFEE)),
_ => Ok(Self::Evm(OpCode::from_str(name).map_err(|_| {
tracing::error!("Failed to load host function: {:?}", import);
Error::HostFuncNotFound(module.into(), name.into())
})?)),
},
("zinkc", "emit_abi") => Ok(Self::EmitABI),
("zinkc", "address_eq") => Ok(Self::Evm(OpCode::EQ)),
("zinkc", "u256_add") => Ok(Self::Evm(OpCode::ADD)),
Expand Down
92 changes: 92 additions & 0 deletions evm/opcodes/src/cancun.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
//! Instructions for Cancun.

use crate::{opcodes, Group, OpCode, Upgrade};

opcodes! {
Cancun,
(0x00, STOP, 0, 0, 0, "Halts execution.", Frontier, StopArithmetic),
(0x01, ADD, 3, 2, 1, "Addition operation.", Frontier, StopArithmetic),
(0x02, MUL, 5, 2, 1, "Multiplication operation.", Frontier, StopArithmetic),
(0x03, SUB, 3, 2, 1, "Subtraction operation.", Frontier, StopArithmetic),
(0x04, DIV, 5, 2, 1, "Integer division operation.", Frontier, StopArithmetic),
(0x05, SDIV, 5, 2, 1, "Signed integer division operation (truncated).", Frontier, StopArithmetic),
(0x06, MOD, 5, 2, 1, "Modulo remainder operation.", Frontier, StopArithmetic),
(0x07, SMOD, 5, 2, 1, "Signed modulo remainder operation.", Frontier, StopArithmetic),
(0x08, ADDMOD, 8, 3, 1, "Modulo addition operation.", Frontier, StopArithmetic),
(0x09, MULMOD, 8, 3, 1, "Modulo multiplication operation.", Frontier, StopArithmetic),
(0x0a, EXP, 10, 2, 1, "Exponential operation.", Frontier, StopArithmetic),
(0x0b, SIGNEXTEND, 5, 2, 1, "Extend length of two's complement signed integer.", Frontier, StopArithmetic),
(0x10, LT, 3, 2, 1, "Less-than comparison.", Frontier, ComparisonBitwiseLogic),
(0x11, GT, 3, 2, 1, "Greater-than comparison.", Frontier, ComparisonBitwiseLogic),
(0x12, SLT, 3, 2, 1, "Signed less-than comparison.", Frontier, ComparisonBitwiseLogic),
(0x13, SGT, 3, 2, 1, "Signed greater-than comparison.", Frontier, ComparisonBitwiseLogic),
(0x14, EQ, 3, 2, 1, "Equality comparison.", Frontier, ComparisonBitwiseLogic),
(0x15, ISZERO, 3, 1, 1, "Simple not operator.", Frontier, ComparisonBitwiseLogic),
(0x16, AND, 3, 2, 1, "Bitwise AND operation.", Frontier, ComparisonBitwiseLogic),
(0x17, OR, 3, 2, 1, "Bitwise OR operation.", Frontier, ComparisonBitwiseLogic),
(0x18, XOR, 3, 2, 1, "Bitwise XOR operation.", Frontier, ComparisonBitwiseLogic),
(0x19, NOT, 3, 1, 1, "Bitwise NOT operation.", Frontier, ComparisonBitwiseLogic),
(0x1a, BYTE, 3, 2, 1, "Retrieve single byte from word.", Frontier, ComparisonBitwiseLogic),
(0x1b, SHL, 3, 2, 1, "Left shift operation", Constantinople, ComparisonBitwiseLogic),
(0x1c, SHR, 3, 2, 1, "Logical right shift operation", Constantinople, ComparisonBitwiseLogic),
(0x1d, SAR, 3, 2, 1, "Arithmetic (signed) right shift operation", Constantinople, ComparisonBitwiseLogic),
(0x20, KECCAK256, 30, 2, 1, "Compute Keccak-256 hash.", Frontier, StopArithmetic),
(0x30, ADDRESS, 2, 0, 1, "Get address of currently executing account.", Frontier, EnvironmentalInformation),
(0x31, BALANCE, 20, 1, 1, "Get balance of the given account.", Frontier, EnvironmentalInformation),
(0x32, ORIGIN, 2, 0, 1, "Get execution origination address.", Frontier, EnvironmentalInformation),
(0x33, CALLER, 2, 0, 1, "Get caller address.", Frontier, EnvironmentalInformation),
(0x34, CALLVALUE, 2, 0, 1, "Get deposited value by the instruction/transaction responsible for this execution.", Frontier, EnvironmentalInformation),
(0x35, CALLDATALOAD, 3, 1, 1, "Get input data of current environment.", Frontier, EnvironmentalInformation),
(0x36, CALLDATASIZE, 2, 0, 1, "Get size of input data in current environment.", Frontier, EnvironmentalInformation),
(0x37, CALLDATACOPY, 3, 3, 0, "Copy input data in current environment to memory.", Frontier, EnvironmentalInformation),
(0x38, CODESIZE, 2, 0, 1, "Get size of code running in current environment.", Frontier, EnvironmentalInformation),
(0x39, CODECOPY, 3, 3, 0, "Copy code running in current environment to memory.", Frontier, EnvironmentalInformation),
(0x3a, GASPRICE, 2, 0, 1, "Get price of gas in current environment", Frontier, EnvironmentalInformation),
(0x3b, EXTCODESIZE, 20, 1, 1, "Get size of an account's code.", Frontier, EnvironmentalInformation),
(0x3c, EXTCODECOPY, 20, 4, 0, "Copy an account's code to memory.", Frontier, EnvironmentalInformation),
(0x3d, RETURNDATASIZE, 2, 0, 1, "Get size of output data from the previous call from the current environment.", Byzantium, EnvironmentalInformation),
(0x3e, RETURNDATACOPY, 3, 3, 0, "Copy output data from the previous call to memory.", Byzantium, EnvironmentalInformation),
(0x3f, TSTORE, 100, 2, 0, "Store word in transient storage.", Cancun, EnvironmentalInformation),
(0x40, TLOAD, 100, 1, 1, "Load word from transient storage.", Cancun, EnvironmentalInformation),
malik672 marked this conversation as resolved.
Show resolved Hide resolved
(0x41, COINBASE, 2, 0, 1, "Get the block's beneficiary address.", Frontier, BlockInformation),
(0x42, TIMESTAMP, 2, 0, 1, "Get the block's timestamp.", Frontier, BlockInformation),
(0x43, NUMBER, 2, 0, 1, "Get the block's number.", Frontier, BlockInformation),
(0x44, DIFFICULTY, 2, 0, 1, "Get the block's difficulty.", Frontier, BlockInformation),
(0x45, GASLIMIT, 2, 0, 1, "Get the block's gas limit.", Frontier, BlockInformation),
(0x46, CHAINID, 2, 0, 1, "Get the chain ID.", Istanbul, BlockInformation),
(0x47, SELFBALANCE, 5, 0, 1, "Get balance of currently executing account.", Istanbul, BlockInformation),
(0x48, BASEFEE, 2, 0, 1, "Get the base fee.", London, BlockInformation),
(0x49, BLOBHASH, 3, 1, 1, "Get versioned hash at index.", Cancun, BlockInformation),
(0x4a, BLOBBASEFEE, 2, 0, 1, "Get the current blob base fee.", Cancun, BlockInformation),
(0x50, POP, 2, 1, 0, "Remove item from stack.", Frontier, StackMemoryStorageFlow),
(0x51, MLOAD, 3, 1, 1, "Load word from memory.", Frontier, StackMemoryStorageFlow),
(0x52, MSTORE, 3, 2, 0, "Save word to memory.", Frontier, StackMemoryStorageFlow),
(0x53, MSTORE8, 3, 2, 0, "Save byte to memory.", Frontier, StackMemoryStorageFlow),
(0x54, SLOAD, 50, 1, 1, "Load word from storage.", Frontier, StackMemoryStorageFlow),
(0x55, SSTORE, 0, 2, 0, "Save word to storage.", Frontier, StackMemoryStorageFlow),
(0x56, JUMP, 8, 1, 0, "Alter the program counter.", Frontier, StackMemoryStorageFlow),
(0x57, JUMPI, 10, 2, 0, "Conditionally alter the program counter.", Frontier, StackMemoryStorageFlow),
(0x58, PC, 2, 0, 1, "Get the value of the program counter prior to the increment.", Frontier, StackMemoryStorageFlow),
(0x59, MSIZE, 2, 0, 1, "Get the size of active memory in bytes.", Frontier, StackMemoryStorageFlow),
(0x5a, GAS, 2, 0, 1, "Get the amount of available gas.", Frontier, StackMemoryStorageFlow),
(0x5b, JUMPDEST, 1, 0, 0, "Mark a valid destination for jumps.", Frontier, StackMemoryStorageFlow),
malik672 marked this conversation as resolved.
Show resolved Hide resolved
(0x5f, PUSH0, 2, 0, 1, "Place 0 byte item on stack.", Shanghai, Push),
// PUSH1 through SWAP16 remain unchanged from Shanghai
// Including all PUSH, DUP, and SWAP operations (0x60-0x9f)
(0xa0, LOG0, 375, 2, 0, "Append log record with no topics.", Frontier, Logging),
(0xa1, LOG1, 750, 3, 0, "Append log record with one topic.", Frontier, Logging),
(0xa2, LOG2, 1125, 4, 0, "Append log record with two topics.", Frontier, Logging),
(0xa3, LOG3, 1500, 5, 0, "Append log record with three topics.", Frontier, Logging),
(0xa4, LOG4, 1875, 6, 0, "Append log record with four topics.", Frontier, Logging),
(0xf0, CREATE, 32000, 3, 1, "Create a new account with associated code.", Frontier, System),
(0xf1, CALL, 100, 7, 1, "Message-call into an account.", Frontier, System),
(0xf2, CALLCODE, 100, 7, 1, "Message-call into this account with alternative account's code.", Frontier, System),
(0xf3, RETURN, 0, 2, 0, "Halt execution returning output data.", Frontier, System),
(0xf4, DELEGATECALL, 40, 6, 1, "Message-call with an alternative account's code, persisting the current context.", Frontier, System),
(0xf5, CREATE2, 32000, 4, 1, "Create a new account with associated code at a specified address.", Constantinople, System),
(0xfa, STATICCALL, 40, 6, 1, "Static message-call into an account.", Byzantium, System),
(0xfb, MCOPY, 3, 3, 0, "Copy memory to memory.", Cancun, System),
(0xfd, REVERT, 0, 2, 0, "Stop execution and revert state changes, without consuming all gas and providing a reason.", Byzantium, System),
(0xfe, INVALID, 0, 0, 0, "Designated invalid instruction.", Frontier, System),
(0xff, SELFDESTRUCT, 5000, 1, 0, "Halt execution and register account for later deletion.", Frontier, System)
}
4 changes: 4 additions & 0 deletions evm/opcodes/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
//! Ethereum virtual machine opcode
#![deny(missing_docs)]

mod cancun;
mod shanghai;

pub use cancun::Cancun;
pub use shanghai::ShangHai;

/// Ethereum virtual machine opcode generator.
Expand Down Expand Up @@ -185,6 +187,8 @@ pub enum Upgrade {
London,
/// Shanghai
Shanghai,
/// Cancun
Cancun,
}

/// Ethereum virtual machine opcode.
Expand Down
65 changes: 65 additions & 0 deletions examples/transient_storage.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
//! Transient Storage example.
#![cfg_attr(target_arch = "wasm32", no_std)]
#![cfg_attr(target_arch = "wasm32", no_main)]

extern crate zink;

use zink::storage::TransientStorage;

/// Temporary counter with value type `i32` that resets after each transaction
#[zink::transient_storage(i32)]
malik672 marked this conversation as resolved.
Show resolved Hide resolved
pub struct TempCounter;

/// Set value to the transient storage.
#[zink::external]
pub fn set_temp(value: i32) {
TempCounter::set(value);
}

/// Get value from the transient storage.
#[zink::external]
pub fn get_temp() -> i32 {
TempCounter::get()
}
malik672 marked this conversation as resolved.
Show resolved Hide resolved

#[cfg(not(target_arch = "wasm32"))]
fn main() {}

#[test]
fn transient_value() -> anyhow::Result<()> {
use zint::{Bytes32, Contract, U256};

let mut contract = Contract::search("transient_storage")?.compile()?;
let value: i32 = 42;

// First transaction: set value
{
let info = contract.execute(&[b"set_temp(int32)".to_vec(), value.to_bytes32().to_vec()])?;
assert!(info.ret.is_empty());
assert_eq!(
info.transient_storage
.get(&U256::from_le_bytes(TempCounter::STORAGE_KEY)),
Some(&U256::from(value))
);
// Verify regular storage is untouched
assert_eq!(
info.storage
.get(&U256::from_le_bytes(TempCounter::STORAGE_KEY)),
None
);
}

// Second transaction: value should be cleared
{
let info = contract.execute(&[b"get_temp()".to_vec()])?;
assert_eq!(info.ret, 0.to_bytes32());
// Verify transient storage was cleared
assert_eq!(
info.transient_storage
.get(&U256::from_le_bytes(TempCounter::STORAGE_KEY)),
None
);
}

Ok(())
}
Loading
Loading