From 3bc884a6f81967952c93770dabb0285352463389 Mon Sep 17 00:00:00 2001 From: TannrA Date: Wed, 8 Mar 2023 20:37:34 -0500 Subject: [PATCH] fix push size calc --- src/lib.rs | 2 ++ src/parser.rs | 35 ++++++++++++++++++++++++++++++++--- 2 files changed, 34 insertions(+), 3 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 427f5de..55da6c5 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -11,6 +11,8 @@ pub mod state; pub mod traits; pub mod storage; pub mod parser; + + use instruction::*; use smt::*; use stack::*; diff --git a/src/parser.rs b/src/parser.rs index 5c2af74..ab12aad 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -41,11 +41,26 @@ fn is_push(b: u8) -> bool { } fn push_size(b: u8) -> u8 { - b - 0x60 + if b > 0x60 { + b - 0x60 + 1 + } else { + 0 + } + } +impl Instruction { + pub fn from_byte(value: u8) -> Self { + value.into() + + } + // Has to handle when it's a push or dup, otherwise easy 1-1 conversion + pub fn from_slice(bytes: &[u8]) -> Vec { + todo!() + } +} impl From for Instruction { fn from(value: u8) -> Self { @@ -191,8 +206,22 @@ impl From for Instruction { 0xfe => Instruction::Invalid, 0xff => Instruction::SelfDestruct, _ => Instruction::Invalid, - }; + } - todo!() + } +} + +#[test] +fn is_push_works() { + for b in (0x60_u8..0x7f) { + assert!(is_push(b)); + } + + for b in (0x00_u8 .. 0x5f) { + assert!(!is_push(b)); + } + + for b in (0x80..0xff_u8) { + assert!(!is_push(b)); } } \ No newline at end of file