Skip to content

Commit

Permalink
stack,vm: use stack push_data()
Browse files Browse the repository at this point in the history
  • Loading branch information
cdump committed Sep 26, 2024
1 parent a328dc5 commit b2885c8
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 13 deletions.
2 changes: 2 additions & 0 deletions src/evm/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ pub const VAL_32_B: [u8; 32] = VAL_32.to_be_bytes();
pub const VAL_256: U256 = uint!(256_U256);

pub const VAL_1024: U256 = uint!(1024_U256);
pub const VAL_1024_B: [u8; 32] = VAL_1024.to_be_bytes();

pub const VAL_131072: U256 = uint!(131072_U256);

pub const VAL_1M: U256 = uint!(1000000_U256);
pub const VAL_1M_B: [u8; 32] = VAL_1M.to_be_bytes();
8 changes: 6 additions & 2 deletions src/evm/stack.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,13 +80,17 @@ impl<T: Clone> Stack<T> {
}
}

pub fn push_uint(&mut self, val: U256) {
pub fn push_data(&mut self, data: [u8; 32]) {
self.data.push(Element {
data: val.to_be_bytes(),
data,
label: None,
});
}

pub fn push_uint(&mut self, val: U256) {
self.push_data(val.to_be_bytes());
}

pub fn pop_uint(&mut self) -> Result<U256> {
match self.data.pop() {
Some(v) => Ok(v.into()),
Expand Down
22 changes: 11 additions & 11 deletions src/evm/vm.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use super::{calldata::CallData, element::Element, memory::Memory, op, stack::Stack, U256};
use super::{VAL_0_B, VAL_1, VAL_1024, VAL_1M, VAL_1_B, VAL_256, VAL_32};
use super::{VAL_0_B, VAL_1, VAL_1024_B, VAL_1M_B, VAL_1_B, VAL_256, VAL_32};
use std::{error, fmt};

#[derive(Debug)]
Expand Down Expand Up @@ -299,7 +299,7 @@ where
let mut ret = StepResult::new(op, 30);
ret.fa = Some(self.stack.pop()?); // offset
ret.sa = Some(self.stack.pop()?); // size
self.stack.push_uint(VAL_1);
self.stack.push_data(VAL_1_B);
Ok(ret)
}

Expand All @@ -316,13 +316,13 @@ where
| op::BASEFEE
| op::BLOBBASEFEE
| op::GASPRICE => {
self.stack.push_uint(U256::ZERO);
self.stack.push_data(VAL_0_B);
Ok(StepResult::new(op, 2))
}

op::BALANCE => {
self.stack.pop()?;
self.stack.push_uint(U256::ZERO);
self.stack.push_data(VAL_0_B);
Ok(StepResult::new(op, 100))
}

Expand Down Expand Up @@ -385,12 +385,12 @@ where

op::EXTCODESIZE | op::EXTCODEHASH => {
self.stack.pop()?;
self.stack.push_uint(VAL_1);
self.stack.push_data(VAL_1_B);
Ok(StepResult::new(op, 100))
}

op::RETURNDATASIZE => {
self.stack.push_uint(VAL_1024);
self.stack.push_data(VAL_1024_B);
Ok(StepResult::new(op, 2))
}

Expand All @@ -409,12 +409,12 @@ where

op::BLOCKHASH => {
self.stack.pop()?;
self.stack.push_uint(VAL_1);
self.stack.push_data(VAL_1_B);
Ok(StepResult::new(op, 20))
}

op::SELFBALANCE => {
self.stack.push_uint(U256::ZERO);
self.stack.push_data(VAL_0_B);
Ok(StepResult::new(op, 5))
}

Expand Down Expand Up @@ -457,7 +457,7 @@ where
op::SLOAD => {
let mut ret = StepResult::new(op, 100);
ret.fa = Some(self.stack.pop()?); // slot
self.stack.push_uint(U256::ZERO);
self.stack.push_data(VAL_0_B);
Ok(ret)
}

Expand All @@ -469,7 +469,7 @@ where
}

op::GAS => {
self.stack.push_uint(VAL_1M);
self.stack.push_data(VAL_1M_B);
Ok(StepResult::new(op, 2))
}

Expand All @@ -488,7 +488,7 @@ where
if op == op::CREATE2 {
self.stack.pop()?;
}
self.stack.push_uint(U256::ZERO);
self.stack.push_data(VAL_0_B);
Ok(StepResult::new(op, 32000))
}

Expand Down

0 comments on commit b2885c8

Please sign in to comment.