Skip to content

Commit

Permalink
Merge pull request #23 from danhper/fix-bytes-encoding
Browse files Browse the repository at this point in the history
Fix bytes encoding
  • Loading branch information
danhper authored Sep 23, 2024
2 parents d3f342e + 031d98f commit 4c0b9bc
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 22 deletions.
29 changes: 8 additions & 21 deletions src/interpreter/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ use solang_parser::pt as parser;
use super::{
builtins::{INSTANCE_METHODS, STATIC_METHODS},
functions::{ContractFunction, Function},
utils::to_fixed_bytes,
Value,
};

Expand Down Expand Up @@ -479,30 +480,17 @@ impl Type {
let num = U256::from_be_slice(v.as_slice());
Ok(Value::Uint(num, *bits_num))
}
(Type::FixBytes(target_bytes_num), Value::FixBytes(bytes, _)) => {
let mut new_bytes = bytes.to_vec();
if *target_bytes_num < 32 {
let to_fill = 32 - *target_bytes_num;
let filler = vec![0; to_fill];
new_bytes[..to_fill].copy_from_slice(&filler);
}
new_bytes[..bytes.0.len()].copy_from_slice(&bytes.0);
Ok(Value::FixBytes(
B256::from_slice(&new_bytes),
*target_bytes_num,
))
(Type::FixBytes(size), Value::FixBytes(bytes, _)) => {
Ok(Value::FixBytes(to_fixed_bytes(&bytes.0, *size)?, *size))
}
(Type::Transaction, Value::FixBytes(v, 32)) => Ok(Value::Transaction(*v)),
(Type::Bytes, Value::Str(v)) => Ok(Value::Bytes(v.as_bytes().to_vec())),
(type_ @ Type::FixBytes(_), Value::Str(_)) => type_.cast(&Type::Bytes.cast(value)?),
(Type::Bytes, Value::FixBytes(v, _)) => Ok(Value::Bytes(v.0.to_vec())),
(Type::FixBytes(size), Value::Bytes(v)) => {
let mut new_bytes = v.clone();
if new_bytes.len() > *size {
new_bytes = new_bytes[new_bytes.len() - *size..].to_vec();
}
new_bytes.resize(32, 0);
Ok(Value::FixBytes(B256::from_slice(&new_bytes), *size))
(Type::Bytes, Value::FixBytes(v, s)) => {
Ok(Value::Bytes(v.0[v.0.len() - *s..].to_vec()))
}
(Type::FixBytes(size), Value::Bytes(bytes)) => {
Ok(Value::FixBytes(to_fixed_bytes(bytes, *size)?, *size))
}
(Type::NamedTuple(name, types_), Value::Tuple(values)) => {
let mut new_values = IndexMap::new();
Expand Down Expand Up @@ -689,7 +677,6 @@ mod tests {
let b256_value =
Value::from_hex("0x00000000000000000000000000000000000000000000000000000000281dd5af")
.unwrap();
println!("{:?}", b256_value);
let b4_value = Value::from_hex("0x281dd5af").unwrap();
assert_eq!(Type::FixBytes(4).cast(&b256_value).unwrap(), b4_value);
assert_eq!(Type::FixBytes(32).cast(&b4_value).unwrap(), b256_value);
Expand Down
25 changes: 24 additions & 1 deletion src/interpreter/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use std::str::FromStr;
use alloy::{
dyn_abi::{EventExt, JsonAbiExt},
json_abi::Event,
primitives::{FixedBytes, U256},
primitives::{FixedBytes, B256, U256},
rpc::types::{Log, TransactionReceipt},
};

Expand Down Expand Up @@ -138,6 +138,13 @@ pub fn receipt_to_value(env: &Env, receipt: TransactionReceipt) -> Result<Value>
Ok(Value::from_receipt(receipt, transformed_logs))
}

pub fn to_fixed_bytes(bytes: &[u8], size: usize) -> Result<B256> {
let mut new_bytes = vec![0; 32];
let new_size = bytes.len().min(size);
new_bytes[32 - new_size..].copy_from_slice(&bytes[bytes.len() - new_size..]);
Ok(B256::from_slice(&new_bytes))
}

#[cfg(test)]
mod tests {
use alloy::{
Expand Down Expand Up @@ -208,6 +215,22 @@ mod tests {
);
}

#[test]
fn test_to_fixed_bytes() {
assert_eq!(
to_fixed_bytes(&[18, 52], 2).unwrap(),
B256::from(U256::from(4660).to_be_bytes())
);
assert_eq!(
to_fixed_bytes(&[18, 52], 4).unwrap(),
B256::from(U256::from(4660).to_be_bytes())
);
assert_eq!(
to_fixed_bytes(&[18, 52], 1).unwrap(),
B256::from(U256::from(52).to_be_bytes())
);
}

#[test]
fn test_decode_event() {
let event = Event {
Expand Down

0 comments on commit 4c0b9bc

Please sign in to comment.