Skip to content

Commit

Permalink
Merge pull request #2604 from OffchainLabs/inputs-wiring
Browse files Browse the repository at this point in the history
Validation Inputs wiring
  • Loading branch information
tsahee authored Sep 30, 2024
2 parents 4a93769 + ea47d46 commit 8197890
Show file tree
Hide file tree
Showing 26 changed files with 885 additions and 466 deletions.
78 changes: 7 additions & 71 deletions arbitrator/Cargo.lock

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

101 changes: 101 additions & 0 deletions arbitrator/arbutil/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use std::{
borrow::Borrow,
fmt,
ops::{Deref, DerefMut},
str::FromStr,
};

// These values must be kept in sync with `arbutil/preimage_type.go`,
Expand Down Expand Up @@ -83,6 +84,32 @@ impl From<usize> for Bytes32 {
}
}

impl FromStr for Bytes32 {
type Err = &'static str;

fn from_str(s: &str) -> Result<Self, Self::Err> {
// Remove the "0x" prefix if present
let s = s.strip_prefix("0x").unwrap_or(s);

// Pad with leading zeros if the string is shorter than 64 characters (32 bytes)
let padded = format!("{:0>64}", s);

// Decode the hex string using the hex crate
let decoded_bytes = hex::decode(padded).map_err(|_| "Invalid hex string")?;

// Ensure the decoded bytes is exactly 32 bytes
if decoded_bytes.len() != 32 {
return Err("Hex string too long for Bytes32");
}

// Create a 32-byte array and fill it with the decoded bytes.
let mut b = [0u8; 32];
b.copy_from_slice(&decoded_bytes);

Ok(Bytes32(b))
}
}

impl TryFrom<&[u8]> for Bytes32 {
type Error = std::array::TryFromSliceError;

Expand Down Expand Up @@ -249,3 +276,77 @@ impl From<GenericBytes20> for Bytes20 {
<[u8; 20]>::from(x).into()
}
}

#[cfg(test)]
mod test {
use super::*;

#[test]
fn test_bytes32() {
let b = Bytes32::from(0x12345678u32);
let expected = [
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,
0x12, 0x34, 0x56, 0x78,
];
assert_eq!(b, Bytes32(expected));
}

#[test]
fn test_from_str_short() {
// Short hex string
let b = Bytes32::from_str("0x12345678").unwrap();
let expected = [
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,
0x12, 0x34, 0x56, 0x78,
];
assert_eq!(b, Bytes32(expected));
}

#[test]
fn test_from_str_very_short() {
// Short hex string
let b = Bytes32::from_str("0x1").unwrap();
let expected = [
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, 0,
0, 0, 0x1,
];
assert_eq!(b, Bytes32(expected));
}

#[test]
fn test_from_str_no_prefix() {
// Short hex string
let b = Bytes32::from_str("12345678").unwrap();
let expected = [
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,
0x12, 0x34, 0x56, 0x78,
];
assert_eq!(b, Bytes32(expected));
}

#[test]
fn test_from_str_full() {
// Full-length hex string
let b =
Bytes32::from_str("0x0000000000000000000000000000000000000000000000000000000012345678")
.unwrap();
let expected = [
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,
0x12, 0x34, 0x56, 0x78,
];
assert_eq!(b, Bytes32(expected));
}

#[test]
fn test_from_str_invalid_non_hex() {
let s = "0x123g5678"; // Invalid character 'g'
assert!(Bytes32::from_str(s).is_err());
}

#[test]
fn test_from_str_too_big() {
let s =
"0123456789ABCDEF0123456789ABCDEF01234567890123456789ABCDEF01234567890123456789ABCDEF0"; // 65 characters
assert!(Bytes32::from_str(s).is_err());
}
}
5 changes: 0 additions & 5 deletions arbitrator/bench/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,6 @@ name = "bench"
version = "0.1.0"
edition = "2021"

[lib]
name = "bench"
path = "src/lib.rs"

[[bin]]
name = "benchbin"
path = "src/bin.rs"
Expand All @@ -20,7 +16,6 @@ clap = { version = "4.4.8", features = ["derive"] }
gperftools = { version = "0.2.0", optional = true }
serde = { version = "1.0.130", features = ["derive", "rc"] }
serde_json = "1.0.67"
serde_with = { version = "3.8.1", features = ["base64"] }

[features]
counters = []
Expand Down
6 changes: 3 additions & 3 deletions arbitrator/bench/src/bin.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use std::{path::PathBuf, time::Duration};

use bench::prepare::*;
use clap::Parser;
use eyre::bail;

Expand All @@ -10,11 +9,12 @@ use gperftools::profiler::PROFILER;
#[cfg(feature = "heapprof")]
use gperftools::heap_profiler::HEAP_PROFILER;

use prover::machine::MachineStatus;

#[cfg(feature = "counters")]
use prover::{machine, memory, merkle};

use prover::machine::MachineStatus;
use prover::prepare::prepare_machine;

#[derive(Parser, Debug)]
#[command(author, version, about, long_about = None)]
struct Args {
Expand Down
2 changes: 0 additions & 2 deletions arbitrator/bench/src/lib.rs

This file was deleted.

76 changes: 0 additions & 76 deletions arbitrator/bench/src/parse_input.rs

This file was deleted.

Loading

0 comments on commit 8197890

Please sign in to comment.