Skip to content

Commit

Permalink
chore(deps): bump alloy
Browse files Browse the repository at this point in the history
  • Loading branch information
DaniPopes committed Sep 4, 2024
1 parent 99af7ef commit 2e88cd6
Show file tree
Hide file tree
Showing 8 changed files with 65 additions and 68 deletions.
40 changes: 20 additions & 20 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 6 additions & 10 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,19 @@ readme = "README.md"
repository = "https://github.com/cdump/evmole"
exclude = ["/javascript", "/python", "/benchmark", "/.github"]

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
ruint = "1.12.3"
hex = { version = "0.4.3", optional = true }
alloy-primitives = "0.8"
alloy-dyn-abi = "0.8"

pyo3 = { version = "0.22.2", features = ["extension-module"], optional = true }
wasm-bindgen = { version = "0.2", optional = true }
alloy-dyn-abi = {version = "0.7"}

[dev-dependencies]
hex = "0.4.3"

[features]
python = ["dep:pyo3"]
javascript = ["dep:wasm-bindgen"]

# for dev
trace = []
python = ["dep:hex", "dep:pyo3"]
javascript = ["dep:hex", "dep:wasm-bindgen"]

[lib]
crate-type = ["cdylib", "lib"]
9 changes: 5 additions & 4 deletions src/arguments.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,11 @@ use std::{
cmp::max,
collections::{BTreeMap, BTreeSet},
};
use alloy_primitives::uint;

const VAL_2: U256 = ruint::uint!(2_U256);
const VAL_31_B: [u8; 32] = ruint::uint!(31_U256).to_be_bytes();
const VAL_131072_B: [u8; 32] = ruint::uint!(131072_U256).to_be_bytes();
const VAL_2: U256 = uint!(2_U256);
const VAL_31_B: [u8; 32] = uint!(31_U256).to_be_bytes();
const VAL_131072_B: [u8; 32] = uint!(131072_U256).to_be_bytes();

#[derive(Clone, Debug, PartialEq, Eq, Hash)]
struct Val {
Expand Down Expand Up @@ -681,8 +682,8 @@ pub fn function_arguments(code: &[u8], selector: &Selector, gas_limit: u32) -> S
#[cfg(test)]
mod test {
use crate::function_selectors;

use super::function_arguments;
use alloy_primitives::hex;

#[test]
fn test_code_offset_buffer() {
Expand Down
16 changes: 9 additions & 7 deletions src/evm.rs
Original file line number Diff line number Diff line change
@@ -1,25 +1,27 @@
use alloy_primitives::uint;

pub mod memory;
pub mod op;
pub mod stack;
pub mod vm;

pub use ruint::aliases::U256;
pub use alloy_primitives::U256;

pub const VAL_0_B: [u8; 32] = U256::ZERO.to_be_bytes();

pub const VAL_1: U256 = ruint::uint!(1_U256);
pub const VAL_1: U256 = uint!(1_U256);
pub const VAL_1_B: [u8; 32] = VAL_1.to_be_bytes();

pub const VAL_4: U256 = ruint::uint!(4_U256);
pub const VAL_4: U256 = uint!(4_U256);

pub const VAL_32: U256 = ruint::uint!(32_U256);
pub const VAL_32: U256 = uint!(32_U256);
pub const VAL_32_B: [u8; 32] = VAL_32.to_be_bytes();

pub const VAL_256: U256 = ruint::uint!(256_U256);
pub const VAL_256: U256 = uint!(256_U256);

pub const VAL_1024: U256 = ruint::uint!(1024_U256);
pub const VAL_1024: U256 = uint!(1024_U256);

pub const VAL_1M: U256 = ruint::uint!(1000000_U256);
pub const VAL_1M: U256 = uint!(1000000_U256);

#[derive(Clone)]
pub struct Element<T> {
Expand Down
19 changes: 9 additions & 10 deletions src/interface_js.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,13 @@
use hex::FromHex;
use alloy_primitives::hex::{self, FromHex};
use wasm_bindgen::prelude::*;

fn strip_hex_prefix(s: &str) -> &str {
s.strip_prefix("0x").unwrap_or(s)
}

fn decode_hex_code(input: &str) -> Result<Vec<u8>, JsError> {
hex::decode(strip_hex_prefix(input))
.map_err(|e| JsError::new(&format!("Failed to decode code hex input: {}", e)))
hex::decode(input).map_err(|e| JsError::new(&format!("Failed to decode code hex input: {e}")))
}

fn decode_hex_selector(input: &str) -> Result<[u8; 4], JsError> {
<[u8; 4]>::from_hex(strip_hex_prefix(input))
.map_err(|e| JsError::new(&format!("Failed to decode selector hex input: {}", e)))
<[u8; 4]>::from_hex(input)
.map_err(|e| JsError::new(&format!("Failed to decode selector hex input: {e}")))
}

/// Extracts function selectors from the given bytecode.
Expand Down Expand Up @@ -50,7 +45,11 @@ pub fn function_arguments(code: &str, selector: &str, gas_limit: u32) -> Result<
/// @param {number} gas_limit - Maximum allowed gas usage; set to `0` to use defaults
/// @returns {string} `payable` | `nonpayable` | `view` | `pure`
#[wasm_bindgen(js_name = functionStateMutability, skip_jsdoc)]
pub fn function_state_mutability(code: &str, selector: &str, gas_limit: u32) -> Result<String, JsError> {
pub fn function_state_mutability(
code: &str,
selector: &str,
gas_limit: u32,
) -> Result<String, JsError> {
let c = decode_hex_code(code)?;
let s = decode_hex_selector(selector)?;
Ok(crate::state_mutability::function_state_mutability(&c, &s, gas_limit).to_string())
Expand Down
19 changes: 7 additions & 12 deletions src/interface_py.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
use std::borrow::Cow;

use alloy_primitives::hex;
use pyo3::exceptions::{PyTypeError, PyValueError};
use pyo3::prelude::*;
use pyo3::types::{PyBytes, PyString};
use std::borrow::Cow;

fn input_to_bytes<'a>(code: &'a Bound<'a, PyAny>) -> PyResult<Cow<'a, [u8]>> {
if let Ok(s) = code.downcast::<PyString>() {
let str_slice = s
.to_str()
.map_err(|e| PyValueError::new_err(e.to_string()))?;

let v = hex::decode(strip_hex_prefix(str_slice))
let v = hex::decode(str_slice)
.map_err(|e| PyValueError::new_err(format!("failed to parse hex: {}", e)))?;
Ok(Cow::Owned(v))
} else if let Ok(b) = code.downcast::<PyBytes>() {
Expand All @@ -22,10 +22,6 @@ fn input_to_bytes<'a>(code: &'a Bound<'a, PyAny>) -> PyResult<Cow<'a, [u8]>> {
}
}

fn strip_hex_prefix(s: &str) -> &str {
s.strip_prefix("0x").unwrap_or(s)
}

/// Extracts function selectors from the given bytecode.
///
/// Args:
Expand Down Expand Up @@ -101,11 +97,10 @@ fn function_state_mutability(
<[u8; 4]>::try_from(selectors_ref).expect("len checked above")
};

Ok(crate::state_mutability::function_state_mutability(
&code_bytes,
&sel,
gas_limit,
).to_string())
Ok(
crate::state_mutability::function_state_mutability(&code_bytes, &sel, gas_limit)
.to_string(),
)
}

#[pymodule]
Expand Down
9 changes: 6 additions & 3 deletions src/selectors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use crate::evm::{
Element, U256, VAL_0_B, VAL_1_B,
};
use crate::Selector;

use alloy_primitives::uint;
use std::collections::BTreeSet;

#[derive(Clone, Debug, PartialEq, Eq, Hash)]
Expand All @@ -14,7 +14,7 @@ enum Label {
MulSig,
}

const VAL_FFFFFFFF_B: [u8; 32] = ruint::uint!(0xffffffff_U256).to_be_bytes();
const VAL_FFFFFFFF_B: [u8; 32] = uint!(0xffffffff_U256).to_be_bytes();

fn analyze(
vm: &mut Vm<Label>,
Expand Down Expand Up @@ -161,7 +161,10 @@ pub fn function_selectors(code: &[u8], gas_limit: u32) -> Vec<Selector> {
let vm = Vm::<Label>::new(
code,
Element {
data: [0xaa, 0xbb, 0xcc, 0xdd, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
data: [
0xaa, 0xbb, 0xcc, 0xdd, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
],
label: Some(Label::CallData),
},
);
Expand Down
5 changes: 3 additions & 2 deletions src/state_mutability.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use crate::{
utils::execute_until_function_start,
Selector,
};
use alloy_primitives::uint;

const fn create_opcode_lookup_table<const N: usize>(ops: [op::OpCode; N]) -> [bool; 256] {
let mut res = [false; 256];
Expand Down Expand Up @@ -96,7 +97,7 @@ fn analyze_payable(mut vm: Vm<Label>, gas_limit: u32, call_value: u32) -> (bool,
StepResult{op: op::CALLDATASIZE, ..} =>
{
if let Ok(s) = vm.stack.peek_mut() {
s.data = ruint::uint!(131072_U256).to_be_bytes();
s.data = uint!(131072_U256).to_be_bytes();
} else {
break; // error
}
Expand Down Expand Up @@ -161,7 +162,7 @@ fn analyze_view_pure_internal(
match ret.op {
op::CALLDATASIZE => {
if let Ok(s) = vm.stack.peek_mut() {
s.data = ruint::uint!(131072_U256).to_be_bytes();
s.data = uint!(131072_U256).to_be_bytes();
} else {
break; // error
}
Expand Down

0 comments on commit 2e88cd6

Please sign in to comment.