From c650e6c799c1b320c1ac39466113644f38e3aa8a Mon Sep 17 00:00:00 2001 From: Sebastian Roa <115311276+Roaring30s@users.noreply.github.com> Date: Fri, 20 Oct 2023 17:05:43 -0400 Subject: [PATCH 01/25] 0.3.26 --- js/napi/npm/darwin-arm64/package.json | 2 +- js/napi/npm/darwin-x64/package.json | 2 +- js/napi/npm/linux-x64-gnu/package.json | 2 +- js/napi/npm/win32-x64-msvc/package.json | 2 +- js/napi/package.json | 2 +- js/package.json | 2 +- 6 files changed, 6 insertions(+), 6 deletions(-) diff --git a/js/napi/npm/darwin-arm64/package.json b/js/napi/npm/darwin-arm64/package.json index 09eaa84..c1144c9 100644 --- a/js/napi/npm/darwin-arm64/package.json +++ b/js/napi/npm/darwin-arm64/package.json @@ -1,6 +1,6 @@ { "name": "@three-em/node-darwin-arm64", - "version": "0.3.25", + "version": "0.3.26", "os": [ "darwin" ], diff --git a/js/napi/npm/darwin-x64/package.json b/js/napi/npm/darwin-x64/package.json index 3696cde..eda1182 100644 --- a/js/napi/npm/darwin-x64/package.json +++ b/js/napi/npm/darwin-x64/package.json @@ -1,6 +1,6 @@ { "name": "@three-em/node-darwin-x64", - "version": "0.3.25", + "version": "0.3.26", "os": [ "darwin" ], diff --git a/js/napi/npm/linux-x64-gnu/package.json b/js/napi/npm/linux-x64-gnu/package.json index 8e6c55a..d17b2f3 100644 --- a/js/napi/npm/linux-x64-gnu/package.json +++ b/js/napi/npm/linux-x64-gnu/package.json @@ -1,6 +1,6 @@ { "name": "@three-em/node-linux-x64-gnu", - "version": "0.3.25", + "version": "0.3.26", "os": [ "linux" ], diff --git a/js/napi/npm/win32-x64-msvc/package.json b/js/napi/npm/win32-x64-msvc/package.json index 20d5d82..1756e51 100644 --- a/js/napi/npm/win32-x64-msvc/package.json +++ b/js/napi/npm/win32-x64-msvc/package.json @@ -1,6 +1,6 @@ { "name": "@three-em/node-win32-x64-msvc", - "version": "0.3.25", + "version": "0.3.26", "os": [ "win32" ], diff --git a/js/napi/package.json b/js/napi/package.json index f8db87b..cc4bf10 100644 --- a/js/napi/package.json +++ b/js/napi/package.json @@ -1,6 +1,6 @@ { "name": "@three-em/node", - "version": "0.3.25", + "version": "0.3.26", "author": "Divy Srivastava ", "license": "MIT", "napi": { diff --git a/js/package.json b/js/package.json index d3ed074..c52be0a 100644 --- a/js/package.json +++ b/js/package.json @@ -1,7 +1,7 @@ { "name": "@three-em/js", "type": "module", - "version": "0.3.19", + "version": "0.3.20", "main": "index.js", "license": "MIT" } From fcb10b193909b05bed8ce015f406187c88c70d50 Mon Sep 17 00:00:00 2001 From: Sebastian Roa <115311276+Roaring30s@users.noreply.github.com> Date: Sat, 21 Oct 2023 12:10:06 -0400 Subject: [PATCH 02/25] :mag: Experiment Sub Opcodes with Base 16 --- crates/evm/lib.rs | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/crates/evm/lib.rs b/crates/evm/lib.rs index a69a614..dfb9eaf 100644 --- a/crates/evm/lib.rs +++ b/crates/evm/lib.rs @@ -1,4 +1,5 @@ pub use primitive_types::U256; +pub use primitive_types::H128; use tiny_keccak::Hasher; use tiny_keccak::Keccak; @@ -362,7 +363,11 @@ impl<'a> Machine<'a> { Instruction::Sub => { let lhs = self.stack.pop(); let rhs = self.stack.pop(); - self.stack.push(lhs - rhs); + let mlhs = lhs.as_u128(); + let mrhs = rhs.as_u128(); + let difference = mlhs.overflowing_sub(mrhs); + println!("{:#?}", difference); + self.stack.push(lhs); } Instruction::Mul => { let lhs = self.stack.pop(); @@ -1306,7 +1311,7 @@ mod tests { #[test] fn test_erc_constructor() { //60015f60026003 - let bytes = hex!("608060405234801561000f575f80fd5b5060405161015a38038061015a83398181016040528101906100319190610074565b805f819055505061009f565b5f80fd5b5f819050919050565b61005381610041565b811461005d575f80fd5b50565b5f8151905061006e8161004a565b92915050565b5f602082840312156100895761008861003d565b5b5f61009684828501610060565b91505092915050565b60af806100ab5f395ff3fe6080604052348015600e575f80fd5b50600436106026575f3560e01c8063eea32eb214602a575b5f80fd5b60306044565b604051603b91906062565b60405180910390f35b5f8054905090565b5f819050919050565b605c81604c565b82525050565b5f60208201905060735f8301846055565b9291505056fea2646970667358221220e172226e595342e8a3f2ef9078c5da77614a34292db622e83940893b7979708764736f6c634300081400330000000000000000000000000000000000000000000000000000000000000003"); + let bytes = hex!("6003600203"); let mut machine = Machine::new(test_cost_fn); let status = machine.execute(&bytes, Default::default()); //assert_eq!(status, ExecutionState::Ok); From de0373e72b342a1e62250a806d27efe570fd7e81 Mon Sep 17 00:00:00 2001 From: Sebastian Roa <115311276+Roaring30s@users.noreply.github.com> Date: Tue, 24 Oct 2023 09:06:02 -0400 Subject: [PATCH 03/25] :wrench: Fix CodeCopy Opcode --- crates/evm/lib.rs | 41 ++++++++++++++++++++++++++++------------- 1 file changed, 28 insertions(+), 13 deletions(-) diff --git a/crates/evm/lib.rs b/crates/evm/lib.rs index dfb9eaf..dc461a7 100644 --- a/crates/evm/lib.rs +++ b/crates/evm/lib.rs @@ -352,7 +352,8 @@ impl<'a> Machine<'a> { pc += 1; - //println!("STACK: {:?}", self.stack); + println!("Instruction: {:#?}", inst); + println!("Counter: {:#?}", pc); match inst { Instruction::Stop => {} Instruction::Add => { @@ -363,11 +364,11 @@ impl<'a> Machine<'a> { Instruction::Sub => { let lhs = self.stack.pop(); let rhs = self.stack.pop(); - let mlhs = lhs.as_u128(); - let mrhs = rhs.as_u128(); - let difference = mlhs.overflowing_sub(mrhs); - println!("{:#?}", difference); - self.stack.push(lhs); + let test1: u16 = lhs.as_u64() as u16; + let test2: u16 = rhs.as_u64() as u16; + println!("CHECK: {:#?}", test1.overflowing_sub(test2).0); + let difference = U256::from(test1.overflowing_sub(test2).0); + self.stack.push(difference); } Instruction::Mul => { let lhs = self.stack.pop(); @@ -695,13 +696,26 @@ impl<'a> Machine<'a> { let mem_offset = self.stack.pop().low_u64() as usize; let code_offset = self.stack.pop(); let len = self.stack.pop().low_u64() as usize; + //println!("Dest Offset: {:#?}", mem_offset); + //println!("Offset: {:#?}", code_offset); + //println!("Size: {:#?}", len); if code_offset > usize::max_value().into() { dbg!("CODECOPY: offset too large"); } let code_offset = code_offset.low_u64() as usize; - - //if code_offset < self.data.len() { - let code = &bytecode[code_offset..code_offset + len]; + + let code: &[u8]; + // Needed for testing parts of bytecode to avoid out of bound errors in &bytecode[code_offset..code_offset + len] + let mut temp_vec: Vec = vec![]; + + if code_offset + len <= bytecode.len() { + code = &bytecode[code_offset..code_offset + len]; + } else { + // Used for byte by byte testing + temp_vec.extend(std::iter::repeat(0).take(len - code_offset)); + code = &temp_vec; + } + println!("length of new code {:#?}", &code.len()); //ATTENTION: Later investigate if code is being injected into memory correctly // I dont think the below code is correct. if self.memory.len() < mem_offset + 32 { @@ -712,9 +726,10 @@ impl<'a> Machine<'a> { let current_size = code.len() + mem_offset; let remainder = current_size % 32; let padding = if remainder == 0 { 0 } else { 32 - remainder }; - + // Resize self.memory.resize(current_size + padding + 32, 0); + //Calculate new space of zeroes for i in 0..=code.len() - 1 { if i > code.len() { @@ -1310,8 +1325,8 @@ mod tests { */ #[test] fn test_erc_constructor() { - //60015f60026003 - let bytes = hex!("6003600203"); + //608060405234801562000010575f80fd5b506040516200166f3803806200166f3839 + let bytes = hex!("608060405234801562000010575f80fd5b506040516200166f3803806200166f3839818101604052810190620000369190620003cb565b6040518060400160405280600481526020017f476f6c64000000000000000000000000000000000000000000000000000000008152506040518060400160405280600381526020017f474c4400000000000000000000000000000000000000000000000000000000008152508160039081620000b3919062000656565b508060049081620000c5919062000656565b505050620000da3382620000e160201b60201c565b5062000866565b5f73ffffffffffffffffffffffffffffffffffffffff168273ffffffffffffffffffffffffffffffffffffffff160362000154575f6040517fec442f050000000000000000000000000000000000000000000000000000000081526004016200014b91906200077d565b60405180910390fd5b620001675f83836200016b60201b60201c565b5050565b5f73ffffffffffffffffffffffffffffffffffffffff168373ffffffffffffffffffffffffffffffffffffffff1603620001bf578060025f828254620001b29190620007c5565b9250508190555062000290565b5f805f8573ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020019081526020015f20549050818110156200024b578381836040517fe450d38c000000000000000000000000000000000000000000000000000000008152600401620002429392919062000810565b60405180910390fd5b8181035f808673ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020019081526020015f2081905550505b5f73ffffffffffffffffffffffffffffffffffffffff168273ffffffffffffffffffffffffffffffffffffffff1603620002d9578060025f828254039250508190555062000323565b805f808473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020019081526020015f205f82825401925050819055505b8173ffffffffffffffffffffffffffffffffffffffff168373ffffffffffffffffffffffffffffffffffffffff167fddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef836040516200038291906200084b565b60405180910390a3505050565b5f80fd5b5f819050919050565b620003a78162000393565b8114620003b2575f80fd5b50565b5f81519050620003c5816200039c565b92915050565b5f60208284031215620003e357620003e26200038f565b5b5f620003f284828501620003b5565b91505092915050565b5f81519050919050565b7f4e487b71000000000000000000000000000000000000000000000000000000005f52604160045260245ffd5b7f4e487b71000000000000000000000000000000000000000000000000000000005f52602260045260245ffd5b5f60028204905060018216806200047757607f821691505b6020821081036200048d576200048c62000432565b5b50919050565b5f819050815f5260205f209050919050565b5f6020601f8301049050919050565b5f82821b905092915050565b5f60088302620004f17fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff82620004b4565b620004fd8683620004b4565b95508019841693508086168417925050509392505050565b5f819050919050565b5f6200053e62000538620005328462000393565b62000515565b62000393565b9050919050565b5f819050919050565b62000559836200051e565b62000571620005688262000545565b848454620004c0565b825550505050565b5f90565b6200058762000579565b620005948184846200054e565b505050565b5b81811015620005bb57620005af5f826200057d565b6001810190506200059a565b5050565b601f8211156200060a57620005d48162000493565b620005df84620004a5565b81016020851015620005ef578190505b62000607620005fe85620004a5565b83018262000599565b50505b505050565b5f82821c905092915050565b5f6200062c5f19846008026200060f565b1980831691505092915050565b5f6200064683836200061b565b9150826002028217905092915050565b6200066182620003fb565b67ffffffffffffffff8111156200067d576200067c62000405565b5b6200068982546200045f565b62000696828285620005bf565b5f60209050601f831160018114620006cc575f8415620006b7578287015190505b620006c3858262000639565b86555062000732565b601f198416620006dc8662000493565b5f5b828110156200070557848901518255600182019150602085019450602081019050620006de565b8683101562000725578489015162000721601f8916826200061b565b8355505b6001600288020188555050505b505050505050565b5f73ffffffffffffffffffffffffffffffffffffffff82169050919050565b5f62000765826200073a565b9050919050565b620007778162000759565b82525050565b5f602082019050620007925f8301846200076c565b92915050565b7f4e487b71000000000000000000000000000000000000000000000000000000005f52601160045260245ffd5b5f620007d18262000393565b9150620007de8362000393565b9250828201905080821115620007f957620007f862000798565b5b92915050565b6200080a8162000393565b82525050565b5f606082019050620008255f8301866200076c565b620008346020830185620007ff565b620008436040830184620007ff565b949350505050565b5f602082019050620008605f830184620007ff565b92915050565b610dfb80620008745f395ff3fe608060405234801561000f575f80fd5b5060043610610091575f3560e01c8063313ce56711610064578063313ce5671461013157806370a082311461014f57806395d89b411461017f578063a9059cbb1461019d578063dd62ed3e146101cd57610091565b806306fdde0314610095578063095ea7b3146100b357806318160ddd146100e357806323b872dd14610101575b5f80fd5b61009d6101fd565b6040516100aa9190610a74565b60405180910390f35b6100cd60048036038101906100c89190610b25565b61028d565b6040516100da9190610b7d565b60405180910390f35b6100eb6102af565b6040516100f89190610ba5565b60405180910390f35b61011b60048036038101906101169190610bbe565b6102b8565b6040516101289190610b7d565b60405180910390f35b6101396102e6565b6040516101469190610c29565b60405180910390f35b61016960048036038101906101649190610c42565b6102ee565b6040516101769190610ba5565b60405180910390f35b610187610333565b6040516101949190610a74565b60405180910390f35b6101b760048036038101906101b29190610b25565b6103c3565b6040516101c49190610b7d565b60405180910390f35b6101e760048036038101906101e29190610c6d565b6103e5565b6040516101f49190610ba5565b60405180910390f35b60606003805461020c90610cd8565b80601f016020809104026020016040519081016040528092919081815260200182805461023890610cd8565b80156102835780601f1061025a57610100808354040283529160200191610283565b820191905f5260205f20905b81548152906001019060200180831161026657829003601f168201915b5050505050905090565b5f80610297610467565b90506102a481858561046e565b600191505092915050565b5f600254905090565b5f806102c2610467565b90506102cf858285610480565b6102da858585610512565b60019150509392505050565b5f6012905090565b5f805f8373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020019081526020015f20549050919050565b60606004805461034290610cd8565b80601f016020809104026020016040519081016040528092919081815260200182805461036e90610cd8565b80156103b95780601f10610390576101008083540402835291602001916103b9565b820191905f5260205f20905b81548152906001019060200180831161039c57829003601f168201915b5050505050905090565b5f806103cd610467565b90506103da818585610512565b600191505092915050565b5f60015f8473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020019081526020015f205f8373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020019081526020015f2054905092915050565b5f33905090565b61047b8383836001610602565b505050565b5f61048b84846103e5565b90507fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff811461050c57818110156104fd578281836040517ffb8f41b20000000000000000000000000000000000000000000000000000000081526004016104f493929190610d17565b60405180910390fd5b61050b84848484035f610602565b5b50505050565b5f73ffffffffffffffffffffffffffffffffffffffff168373ffffffffffffffffffffffffffffffffffffffff1603610582575f6040517f96c6fd1e0000000000000000000000000000000000000000000000000000000081526004016105799190610d4c565b60405180910390fd5b5f73ffffffffffffffffffffffffffffffffffffffff168273ffffffffffffffffffffffffffffffffffffffff16036105f2575f6040517fec442f050000000000000000000000000000000000000000000000000000000081526004016105e99190610d4c565b60405180910390fd5b6105fd8383836107d1565b505050565b5f73ffffffffffffffffffffffffffffffffffffffff168473ffffffffffffffffffffffffffffffffffffffff1603610672575f6040517fe602df050000000000000000000000000000000000000000000000000000000081526004016106699190610d4c565b60405180910390fd5b5f73ffffffffffffffffffffffffffffffffffffffff168373ffffffffffffffffffffffffffffffffffffffff16036106e2575f6040517f94280d620000000000000000000000000000000000000000000000000000000081526004016106d99190610d4c565b60405180910390fd5b8160015f8673ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020019081526020015f205f8573ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020019081526020015f208190555080156107cb578273ffffffffffffffffffffffffffffffffffffffff168473ffffffffffffffffffffffffffffffffffffffff167f8c5be1e5ebec7d5bd14f71427d1e84f3dd0314c0f7b2291e5b200ac8c7c3b925846040516107c29190610ba5565b60405180910390a35b50505050565b5f73ffffffffffffffffffffffffffffffffffffffff168373ffffffffffffffffffffffffffffffffffffffff1603610821578060025f8282546108159190610d92565b925050819055506108ef565b5f805f8573ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020019081526020015f20549050818110156108aa578381836040517fe450d38c0000000000000000000000000000000000000000000000000000000081526004016108a193929190610d17565b60405180910390fd5b8181035f808673ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020019081526020015f2081905550505b5f73ffffffffffffffffffffffffffffffffffffffff168273ffffffffffffffffffffffffffffffffffffffff1603610936578060025f8282540392505081905550610980565b805f808473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020019081526020015f205f82825401925050819055505b8173ffffffffffffffffffffffffffffffffffffffff168373ffffffffffffffffffffffffffffffffffffffff167fddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef836040516109dd9190610ba5565b60405180910390a3505050565b5f81519050919050565b5f82825260208201905092915050565b5f5b83811015610a21578082015181840152602081019050610a06565b5f8484015250505050565b5f601f19601f8301169050919050565b5f610a46826109ea565b610a5081856109f4565b9350610a60818560208601610a04565b610a6981610a2c565b840191505092915050565b5f6020820190508181035f830152610a8c8184610a3c565b905092915050565b5f80fd5b5f73ffffffffffffffffffffffffffffffffffffffff82169050919050565b5f610ac182610a98565b9050919050565b610ad181610ab7565b8114610adb575f80fd5b50565b5f81359050610aec81610ac8565b92915050565b5f819050919050565b610b0481610af2565b8114610b0e575f80fd5b50565b5f81359050610b1f81610afb565b92915050565b5f8060408385031215610b3b57610b3a610a94565b5b5f610b4885828601610ade565b9250506020610b5985828601610b11565b9150509250929050565b5f8115159050919050565b610b7781610b63565b82525050565b5f602082019050610b905f830184610b6e565b92915050565b610b9f81610af2565b82525050565b5f602082019050610bb85f830184610b96565b92915050565b5f805f60608486031215610bd557610bd4610a94565b5b5f610be286828701610ade565b9350506020610bf386828701610ade565b9250506040610c0486828701610b11565b9150509250925092565b5f60ff82169050919050565b610c2381610c0e565b82525050565b5f602082019050610c3c5f830184610c1a565b92915050565b5f60208284031215610c5757610c56610a94565b5b5f610c6484828501610ade565b91505092915050565b5f8060408385031215610c8357610c82610a94565b5b5f610c9085828601610ade565b9250506020610ca185828601610ade565b9150509250929050565b7f4e487b71000000000000000000000000000000000000000000000000000000005f52602260045260245ffd5b5f6002820490506001821680610cef57607f821691505b602082108103610d0257610d01610cab565b5b50919050565b610d1181610ab7565b82525050565b5f606082019050610d2a5f830186610d08565b610d376020830185610b96565b610d446040830184610b96565b949350505050565b5f602082019050610d5f5f830184610d08565b92915050565b7f4e487b71000000000000000000000000000000000000000000000000000000005f52601160045260245ffd5b5f610d9c82610af2565b9150610da783610af2565b9250828201905080821115610dbf57610dbe610d65565b5b9291505056fea2646970667358221220c765511a5dee70c383f8d47c87f6800974809284b01b55a5db8021bfbe6319ea64736f6c6343000814003300000000000000000000000000000000000000000000000000000000000004e2"); let mut machine = Machine::new(test_cost_fn); let status = machine.execute(&bytes, Default::default()); //assert_eq!(status, ExecutionState::Ok); @@ -1319,7 +1334,7 @@ mod tests { println!("LOOK DOWN"); println!("Result: {:#?}", machine.result); println!("Storage: {:#?}", machine.storage); - println!("Memory: {:#?}", machine.memory); + println!("Memory: {:#?}", machine.memory.len()); println!("Stack: {:#?}", machine.stack); } /* From dfc9643ec01789414bfe3690eeeb117c08ba0fc7 Mon Sep 17 00:00:00 2001 From: Sebastian Roa <115311276+Roaring30s@users.noreply.github.com> Date: Tue, 24 Oct 2023 09:28:44 -0400 Subject: [PATCH 04/25] :hammer: Add SHL Opcode --- crates/evm/lib.rs | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/crates/evm/lib.rs b/crates/evm/lib.rs index dc461a7..e7993ee 100644 --- a/crates/evm/lib.rs +++ b/crates/evm/lib.rs @@ -562,6 +562,18 @@ impl<'a> Machine<'a> { self.stack.push(U256::zero()); } } + + Instruction::Shl => { + let rhs = self.stack.pop(); + let lhs = self.stack.pop(); + + if rhs < U256::from(256) { + self.stack.push(lhs << rhs); + } else { + self.stack.push(U256::zero()); + } + } + Instruction::Eq => { let lhs = self.stack.pop(); let rhs = self.stack.pop(); @@ -1326,7 +1338,7 @@ mod tests { #[test] fn test_erc_constructor() { //608060405234801562000010575f80fd5b506040516200166f3803806200166f3839 - let bytes = hex!("608060405234801562000010575f80fd5b506040516200166f3803806200166f3839818101604052810190620000369190620003cb565b6040518060400160405280600481526020017f476f6c64000000000000000000000000000000000000000000000000000000008152506040518060400160405280600381526020017f474c4400000000000000000000000000000000000000000000000000000000008152508160039081620000b3919062000656565b508060049081620000c5919062000656565b505050620000da3382620000e160201b60201c565b5062000866565b5f73ffffffffffffffffffffffffffffffffffffffff168273ffffffffffffffffffffffffffffffffffffffff160362000154575f6040517fec442f050000000000000000000000000000000000000000000000000000000081526004016200014b91906200077d565b60405180910390fd5b620001675f83836200016b60201b60201c565b5050565b5f73ffffffffffffffffffffffffffffffffffffffff168373ffffffffffffffffffffffffffffffffffffffff1603620001bf578060025f828254620001b29190620007c5565b9250508190555062000290565b5f805f8573ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020019081526020015f20549050818110156200024b578381836040517fe450d38c000000000000000000000000000000000000000000000000000000008152600401620002429392919062000810565b60405180910390fd5b8181035f808673ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020019081526020015f2081905550505b5f73ffffffffffffffffffffffffffffffffffffffff168273ffffffffffffffffffffffffffffffffffffffff1603620002d9578060025f828254039250508190555062000323565b805f808473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020019081526020015f205f82825401925050819055505b8173ffffffffffffffffffffffffffffffffffffffff168373ffffffffffffffffffffffffffffffffffffffff167fddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef836040516200038291906200084b565b60405180910390a3505050565b5f80fd5b5f819050919050565b620003a78162000393565b8114620003b2575f80fd5b50565b5f81519050620003c5816200039c565b92915050565b5f60208284031215620003e357620003e26200038f565b5b5f620003f284828501620003b5565b91505092915050565b5f81519050919050565b7f4e487b71000000000000000000000000000000000000000000000000000000005f52604160045260245ffd5b7f4e487b71000000000000000000000000000000000000000000000000000000005f52602260045260245ffd5b5f60028204905060018216806200047757607f821691505b6020821081036200048d576200048c62000432565b5b50919050565b5f819050815f5260205f209050919050565b5f6020601f8301049050919050565b5f82821b905092915050565b5f60088302620004f17fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff82620004b4565b620004fd8683620004b4565b95508019841693508086168417925050509392505050565b5f819050919050565b5f6200053e62000538620005328462000393565b62000515565b62000393565b9050919050565b5f819050919050565b62000559836200051e565b62000571620005688262000545565b848454620004c0565b825550505050565b5f90565b6200058762000579565b620005948184846200054e565b505050565b5b81811015620005bb57620005af5f826200057d565b6001810190506200059a565b5050565b601f8211156200060a57620005d48162000493565b620005df84620004a5565b81016020851015620005ef578190505b62000607620005fe85620004a5565b83018262000599565b50505b505050565b5f82821c905092915050565b5f6200062c5f19846008026200060f565b1980831691505092915050565b5f6200064683836200061b565b9150826002028217905092915050565b6200066182620003fb565b67ffffffffffffffff8111156200067d576200067c62000405565b5b6200068982546200045f565b62000696828285620005bf565b5f60209050601f831160018114620006cc575f8415620006b7578287015190505b620006c3858262000639565b86555062000732565b601f198416620006dc8662000493565b5f5b828110156200070557848901518255600182019150602085019450602081019050620006de565b8683101562000725578489015162000721601f8916826200061b565b8355505b6001600288020188555050505b505050505050565b5f73ffffffffffffffffffffffffffffffffffffffff82169050919050565b5f62000765826200073a565b9050919050565b620007778162000759565b82525050565b5f602082019050620007925f8301846200076c565b92915050565b7f4e487b71000000000000000000000000000000000000000000000000000000005f52601160045260245ffd5b5f620007d18262000393565b9150620007de8362000393565b9250828201905080821115620007f957620007f862000798565b5b92915050565b6200080a8162000393565b82525050565b5f606082019050620008255f8301866200076c565b620008346020830185620007ff565b620008436040830184620007ff565b949350505050565b5f602082019050620008605f830184620007ff565b92915050565b610dfb80620008745f395ff3fe608060405234801561000f575f80fd5b5060043610610091575f3560e01c8063313ce56711610064578063313ce5671461013157806370a082311461014f57806395d89b411461017f578063a9059cbb1461019d578063dd62ed3e146101cd57610091565b806306fdde0314610095578063095ea7b3146100b357806318160ddd146100e357806323b872dd14610101575b5f80fd5b61009d6101fd565b6040516100aa9190610a74565b60405180910390f35b6100cd60048036038101906100c89190610b25565b61028d565b6040516100da9190610b7d565b60405180910390f35b6100eb6102af565b6040516100f89190610ba5565b60405180910390f35b61011b60048036038101906101169190610bbe565b6102b8565b6040516101289190610b7d565b60405180910390f35b6101396102e6565b6040516101469190610c29565b60405180910390f35b61016960048036038101906101649190610c42565b6102ee565b6040516101769190610ba5565b60405180910390f35b610187610333565b6040516101949190610a74565b60405180910390f35b6101b760048036038101906101b29190610b25565b6103c3565b6040516101c49190610b7d565b60405180910390f35b6101e760048036038101906101e29190610c6d565b6103e5565b6040516101f49190610ba5565b60405180910390f35b60606003805461020c90610cd8565b80601f016020809104026020016040519081016040528092919081815260200182805461023890610cd8565b80156102835780601f1061025a57610100808354040283529160200191610283565b820191905f5260205f20905b81548152906001019060200180831161026657829003601f168201915b5050505050905090565b5f80610297610467565b90506102a481858561046e565b600191505092915050565b5f600254905090565b5f806102c2610467565b90506102cf858285610480565b6102da858585610512565b60019150509392505050565b5f6012905090565b5f805f8373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020019081526020015f20549050919050565b60606004805461034290610cd8565b80601f016020809104026020016040519081016040528092919081815260200182805461036e90610cd8565b80156103b95780601f10610390576101008083540402835291602001916103b9565b820191905f5260205f20905b81548152906001019060200180831161039c57829003601f168201915b5050505050905090565b5f806103cd610467565b90506103da818585610512565b600191505092915050565b5f60015f8473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020019081526020015f205f8373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020019081526020015f2054905092915050565b5f33905090565b61047b8383836001610602565b505050565b5f61048b84846103e5565b90507fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff811461050c57818110156104fd578281836040517ffb8f41b20000000000000000000000000000000000000000000000000000000081526004016104f493929190610d17565b60405180910390fd5b61050b84848484035f610602565b5b50505050565b5f73ffffffffffffffffffffffffffffffffffffffff168373ffffffffffffffffffffffffffffffffffffffff1603610582575f6040517f96c6fd1e0000000000000000000000000000000000000000000000000000000081526004016105799190610d4c565b60405180910390fd5b5f73ffffffffffffffffffffffffffffffffffffffff168273ffffffffffffffffffffffffffffffffffffffff16036105f2575f6040517fec442f050000000000000000000000000000000000000000000000000000000081526004016105e99190610d4c565b60405180910390fd5b6105fd8383836107d1565b505050565b5f73ffffffffffffffffffffffffffffffffffffffff168473ffffffffffffffffffffffffffffffffffffffff1603610672575f6040517fe602df050000000000000000000000000000000000000000000000000000000081526004016106699190610d4c565b60405180910390fd5b5f73ffffffffffffffffffffffffffffffffffffffff168373ffffffffffffffffffffffffffffffffffffffff16036106e2575f6040517f94280d620000000000000000000000000000000000000000000000000000000081526004016106d99190610d4c565b60405180910390fd5b8160015f8673ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020019081526020015f205f8573ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020019081526020015f208190555080156107cb578273ffffffffffffffffffffffffffffffffffffffff168473ffffffffffffffffffffffffffffffffffffffff167f8c5be1e5ebec7d5bd14f71427d1e84f3dd0314c0f7b2291e5b200ac8c7c3b925846040516107c29190610ba5565b60405180910390a35b50505050565b5f73ffffffffffffffffffffffffffffffffffffffff168373ffffffffffffffffffffffffffffffffffffffff1603610821578060025f8282546108159190610d92565b925050819055506108ef565b5f805f8573ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020019081526020015f20549050818110156108aa578381836040517fe450d38c0000000000000000000000000000000000000000000000000000000081526004016108a193929190610d17565b60405180910390fd5b8181035f808673ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020019081526020015f2081905550505b5f73ffffffffffffffffffffffffffffffffffffffff168273ffffffffffffffffffffffffffffffffffffffff1603610936578060025f8282540392505081905550610980565b805f808473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020019081526020015f205f82825401925050819055505b8173ffffffffffffffffffffffffffffffffffffffff168373ffffffffffffffffffffffffffffffffffffffff167fddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef836040516109dd9190610ba5565b60405180910390a3505050565b5f81519050919050565b5f82825260208201905092915050565b5f5b83811015610a21578082015181840152602081019050610a06565b5f8484015250505050565b5f601f19601f8301169050919050565b5f610a46826109ea565b610a5081856109f4565b9350610a60818560208601610a04565b610a6981610a2c565b840191505092915050565b5f6020820190508181035f830152610a8c8184610a3c565b905092915050565b5f80fd5b5f73ffffffffffffffffffffffffffffffffffffffff82169050919050565b5f610ac182610a98565b9050919050565b610ad181610ab7565b8114610adb575f80fd5b50565b5f81359050610aec81610ac8565b92915050565b5f819050919050565b610b0481610af2565b8114610b0e575f80fd5b50565b5f81359050610b1f81610afb565b92915050565b5f8060408385031215610b3b57610b3a610a94565b5b5f610b4885828601610ade565b9250506020610b5985828601610b11565b9150509250929050565b5f8115159050919050565b610b7781610b63565b82525050565b5f602082019050610b905f830184610b6e565b92915050565b610b9f81610af2565b82525050565b5f602082019050610bb85f830184610b96565b92915050565b5f805f60608486031215610bd557610bd4610a94565b5b5f610be286828701610ade565b9350506020610bf386828701610ade565b9250506040610c0486828701610b11565b9150509250925092565b5f60ff82169050919050565b610c2381610c0e565b82525050565b5f602082019050610c3c5f830184610c1a565b92915050565b5f60208284031215610c5757610c56610a94565b5b5f610c6484828501610ade565b91505092915050565b5f8060408385031215610c8357610c82610a94565b5b5f610c9085828601610ade565b9250506020610ca185828601610ade565b9150509250929050565b7f4e487b71000000000000000000000000000000000000000000000000000000005f52602260045260245ffd5b5f6002820490506001821680610cef57607f821691505b602082108103610d0257610d01610cab565b5b50919050565b610d1181610ab7565b82525050565b5f606082019050610d2a5f830186610d08565b610d376020830185610b96565b610d446040830184610b96565b949350505050565b5f602082019050610d5f5f830184610d08565b92915050565b7f4e487b71000000000000000000000000000000000000000000000000000000005f52601160045260245ffd5b5f610d9c82610af2565b9150610da783610af2565b9250828201905080821115610dbf57610dbe610d65565b5b9291505056fea2646970667358221220c765511a5dee70c383f8d47c87f6800974809284b01b55a5db8021bfbe6319ea64736f6c6343000814003300000000000000000000000000000000000000000000000000000000000004e2"); + let bytes = hex!("600360011b"); let mut machine = Machine::new(test_cost_fn); let status = machine.execute(&bytes, Default::default()); //assert_eq!(status, ExecutionState::Ok); From 63f6e218e42fa81183ca84f0a819754cbcb59f92 Mon Sep 17 00:00:00 2001 From: Sebastian Roa <115311276+Roaring30s@users.noreply.github.com> Date: Tue, 24 Oct 2023 12:03:21 -0400 Subject: [PATCH 05/25] :hammer: Add peek_step Function to Machine --- crates/evm/lib.rs | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/crates/evm/lib.rs b/crates/evm/lib.rs index e7993ee..4e6b4f9 100644 --- a/crates/evm/lib.rs +++ b/crates/evm/lib.rs @@ -223,6 +223,10 @@ impl Stack { self.data[self.data.len() - 1] } + pub fn peek_step(&self, step: usize) -> U256 { + self.data[self.data.len() - step] + } + pub fn swap(&mut self, index: usize) { let ptr = self.data.len() - 1; @@ -704,10 +708,11 @@ impl<'a> Machine<'a> { self.stack.push(U256::from(len)); } Instruction::CodeCopy => { - // NOTE about codecopy: We need to determine what sets initial memory size & thru out init & runtime bytecode - let mem_offset = self.stack.pop().low_u64() as usize; - let code_offset = self.stack.pop(); - let len = self.stack.pop().low_u64() as usize; + let mem_offset = self.stack.peek_step(1).low_u64() as usize; + let code_offset = self.stack.peek_step(2); + let len = self.stack.peek_step(3).low_u64() as usize; + println!("code offset: {:#?}", code_offset); + println!("len: {:#?}", len); //println!("Dest Offset: {:#?}", mem_offset); //println!("Offset: {:#?}", code_offset); //println!("Size: {:#?}", len); @@ -1337,8 +1342,7 @@ mod tests { */ #[test] fn test_erc_constructor() { - //608060405234801562000010575f80fd5b506040516200166f3803806200166f3839 - let bytes = hex!("600360011b"); + let bytes = hex!("608060405234801562000010575f80fd5b506040516200166f3803806200166f3839"); let mut machine = Machine::new(test_cost_fn); let status = machine.execute(&bytes, Default::default()); //assert_eq!(status, ExecutionState::Ok); From dc1a110659cd01a64e7b47bb3d003f19cdbe5329 Mon Sep 17 00:00:00 2001 From: Sebastian Roa <115311276+Roaring30s@users.noreply.github.com> Date: Tue, 24 Oct 2023 12:15:18 -0400 Subject: [PATCH 06/25] :wrench: Remove CodeCopy Opcode Pop --- crates/evm/lib.rs | 3 --- 1 file changed, 3 deletions(-) diff --git a/crates/evm/lib.rs b/crates/evm/lib.rs index 4e6b4f9..b203a68 100644 --- a/crates/evm/lib.rs +++ b/crates/evm/lib.rs @@ -711,8 +711,6 @@ impl<'a> Machine<'a> { let mem_offset = self.stack.peek_step(1).low_u64() as usize; let code_offset = self.stack.peek_step(2); let len = self.stack.peek_step(3).low_u64() as usize; - println!("code offset: {:#?}", code_offset); - println!("len: {:#?}", len); //println!("Dest Offset: {:#?}", mem_offset); //println!("Offset: {:#?}", code_offset); //println!("Size: {:#?}", len); @@ -755,7 +753,6 @@ impl<'a> Machine<'a> { self.memory[mem_offset + i] = code[i]; } } - //} } Instruction::ExtCodeSize => { // Fetch the `Contract-Src` from Arweave for the contract. From 6f25e7fb5739bd7513839695dd0930d48cd7edf4 Mon Sep 17 00:00:00 2001 From: Sebastian Roa <115311276+Roaring30s@users.noreply.github.com> Date: Tue, 24 Oct 2023 12:46:04 -0400 Subject: [PATCH 07/25] :wrench: Fix NOT Opcode --- crates/evm/lib.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/crates/evm/lib.rs b/crates/evm/lib.rs index b203a68..a4cd47c 100644 --- a/crates/evm/lib.rs +++ b/crates/evm/lib.rs @@ -370,7 +370,6 @@ impl<'a> Machine<'a> { let rhs = self.stack.pop(); let test1: u16 = lhs.as_u64() as u16; let test2: u16 = rhs.as_u64() as u16; - println!("CHECK: {:#?}", test1.overflowing_sub(test2).0); let difference = U256::from(test1.overflowing_sub(test2).0); self.stack.push(difference); } @@ -617,8 +616,9 @@ impl<'a> Machine<'a> { } Instruction::Not => { let val = self.stack.pop(); - - self.stack.push(!val); + let val16: u16 = val.as_u64() as u16; + let not_val16 = U256::from(!val16); + self.stack.push(not_val16); } Instruction::Byte => { let rhs = self.stack.pop(); @@ -1339,7 +1339,7 @@ mod tests { */ #[test] fn test_erc_constructor() { - let bytes = hex!("608060405234801562000010575f80fd5b506040516200166f3803806200166f3839"); + let bytes = hex!("600119"); let mut machine = Machine::new(test_cost_fn); let status = machine.execute(&bytes, Default::default()); //assert_eq!(status, ExecutionState::Ok); From a5acbd796bce0f998793733441dc8d51ec7f4451 Mon Sep 17 00:00:00 2001 From: Sebastian Roa <115311276+Roaring30s@users.noreply.github.com> Date: Tue, 24 Oct 2023 18:05:26 -0400 Subject: [PATCH 08/25] :wrench: Fix ADD and MUL Opcodes --- crates/evm/lib.rs | 32 ++++++++++++++++++++++++-------- 1 file changed, 24 insertions(+), 8 deletions(-) diff --git a/crates/evm/lib.rs b/crates/evm/lib.rs index a4cd47c..62b63c5 100644 --- a/crates/evm/lib.rs +++ b/crates/evm/lib.rs @@ -1,3 +1,5 @@ +use std::collections::HashMap; + pub use primitive_types::U256; pub use primitive_types::H128; use tiny_keccak::Hasher; @@ -343,6 +345,7 @@ impl<'a> Machine<'a> { ) -> ExecutionState { let mut pc = 0; let len = bytecode.len(); + let mut instruction_vec: Vec = Vec::new(); while pc < len { let opcode = bytecode[pc]; let inst = match Instruction::try_from(opcode) { @@ -356,27 +359,34 @@ impl<'a> Machine<'a> { pc += 1; - println!("Instruction: {:#?}", inst); - println!("Counter: {:#?}", pc); + println!("'{:#?}',", inst); + + //println!("Counter: {:#?}", pc); match inst { Instruction::Stop => {} Instruction::Add => { let lhs = self.stack.pop(); let rhs = self.stack.pop(); - self.stack.push(lhs + rhs); + let lhs16: u16 = lhs.as_u64() as u16; + let rhs16: u16 = rhs.as_u64() as u16; + let sum = U256::from(lhs16.overflowing_add(rhs16).0); + self.stack.push(sum); } Instruction::Sub => { let lhs = self.stack.pop(); let rhs = self.stack.pop(); - let test1: u16 = lhs.as_u64() as u16; - let test2: u16 = rhs.as_u64() as u16; - let difference = U256::from(test1.overflowing_sub(test2).0); + let lhs16: u16 = lhs.as_u64() as u16; + let rhs16: u16 = rhs.as_u64() as u16; + let difference = U256::from(lhs16.overflowing_sub(rhs16).0); self.stack.push(difference); } Instruction::Mul => { let lhs = self.stack.pop(); let rhs = self.stack.pop(); - self.stack.push(lhs * rhs); + let lhs16: u16 = lhs.as_u64() as u16; + let rhs16: u16 = rhs.as_u64() as u16; + let product = U256::from(lhs16.overflowing_mul(rhs16).0); + self.stack.push(product); } Instruction::Div => { let lhs = self.stack.pop(); @@ -1051,6 +1061,8 @@ impl<'a> Machine<'a> { } } + + #[cfg(test)] mod tests { use crate::storage::Storage; @@ -1339,7 +1351,7 @@ mod tests { */ #[test] fn test_erc_constructor() { - let bytes = hex!("600119"); + let bytes = hex!("61ffff600102"); let mut machine = Machine::new(test_cost_fn); let status = machine.execute(&bytes, Default::default()); //assert_eq!(status, ExecutionState::Ok); @@ -1397,3 +1409,7 @@ mod tests { } */ } +/* +Yield integer overflow error to u64 +60017fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff01 +*/ \ No newline at end of file From c2aa68f8b345743e9ba47b3a2c4e80551791ea98 Mon Sep 17 00:00:00 2001 From: Sebastian Roa <115311276+Roaring30s@users.noreply.github.com> Date: Tue, 24 Oct 2023 18:10:28 -0400 Subject: [PATCH 09/25] :wrench: Fix DIV Opcode --- crates/evm/lib.rs | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/crates/evm/lib.rs b/crates/evm/lib.rs index 62b63c5..3ea1bfb 100644 --- a/crates/evm/lib.rs +++ b/crates/evm/lib.rs @@ -391,12 +391,14 @@ impl<'a> Machine<'a> { Instruction::Div => { let lhs = self.stack.pop(); let rhs = self.stack.pop(); - + let lhs16: u16 = lhs.as_u64() as u16; + let rhs16: u16 = rhs.as_u64() as u16; + let quotient = U256::from(lhs16.overflowing_div(rhs16).0); if rhs == U256::zero() { return ExecutionState::Abort(AbortError::DivZero); } - self.stack.push(lhs / rhs); + self.stack.push(quotient); } Instruction::SDiv => { let dividend = to_signed(self.stack.pop()); @@ -1351,7 +1353,7 @@ mod tests { */ #[test] fn test_erc_constructor() { - let bytes = hex!("61ffff600102"); + let bytes = hex!("6002600404"); let mut machine = Machine::new(test_cost_fn); let status = machine.execute(&bytes, Default::default()); //assert_eq!(status, ExecutionState::Ok); From 12e491e28571caff1f7041facf08b1eb6fe6d48d Mon Sep 17 00:00:00 2001 From: Sebastian Roa <115311276+Roaring30s@users.noreply.github.com> Date: Tue, 24 Oct 2023 19:38:15 -0400 Subject: [PATCH 10/25] :mag: Inspect CALLER opcode --- crates/evm/lib.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/crates/evm/lib.rs b/crates/evm/lib.rs index 3ea1bfb..191d1f6 100644 --- a/crates/evm/lib.rs +++ b/crates/evm/lib.rs @@ -673,7 +673,7 @@ impl<'a> Machine<'a> { } Instruction::Caller => { // TODO: caller - self.stack.push(U256::zero()); + self.stack.push(U256::from("03")); } Instruction::CallValue => { self.stack.push(self.state); @@ -1353,7 +1353,7 @@ mod tests { */ #[test] fn test_erc_constructor() { - let bytes = hex!("6002600404"); + let bytes = hex!("33"); let mut machine = Machine::new(test_cost_fn); let status = machine.execute(&bytes, Default::default()); //assert_eq!(status, ExecutionState::Ok); From e15f1c913958925189703948b2c74ae9f40b38bb Mon Sep 17 00:00:00 2001 From: Darwin Date: Wed, 25 Oct 2023 16:02:18 +0200 Subject: [PATCH 11/25] docs: add opcodes --- docs/opcodes.md | 50 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 docs/opcodes.md diff --git a/docs/opcodes.md b/docs/opcodes.md new file mode 100644 index 0000000..b7d96e5 --- /dev/null +++ b/docs/opcodes.md @@ -0,0 +1,50 @@ + +## List of the supported and tested 3EM EVM opcodes: + +| opcodes | status | +|:---------:|:--------:| +| `CallValue` | ✅ | +| `Dup1` | ✅ | +| `IsZero` | ✅ | +| `Push3` | ✅ | +| `JumpI` | ✅ | +| `JumpDest` | ✅ | +| `Pop` | ✅ | +| `Push1` | ✅ | +| `MLoad` | ✅ | +| `CodeSize` | ✅ | +| `Sub` | ✅ | +| `CodeCopy` | ✅ | +| `Dup2` | ✅ | +| `Add` | ✅ | +| `MStore` | ✅ | +| `Swap1` | ✅ | +| `Swap2` | ✅ | +| `Jump` | ✅ | +| `Push0` | ✅ | +| `Dup3` | ✅ | +| `Dup5` | ✅ | +| `SLt` | ✅ | +| `Dup6` | ✅ | +| `Eq` | ✅ | +| `Swap3` | ✅ | +| `Push32` | ✅ | +| `Push8` | ✅ | +| `Gt` | ✅ | +| `SLoad` | ✅ | +| `Div` | ✅ | +| `And` | ✅ | +| `Lt` | ✅ | +| `Dup4` | ✅ | +| `Dup8` | ✅ | +| `Not` | ✅ | +| `Mul` | ✅ | +| `Shr` | ✅ | +| `Or` | ✅ | +| `Dup7` | ✅ | +| `SStore` | ✅ | +| `Caller` | ✅ | +| `Shl` | ✅ | +| `Push20` | ✅ | +| `Revert` | ✅ | +| `Mload` | ✅ | From ee48719afc3525fb293f77867f6b02ab2b83894f Mon Sep 17 00:00:00 2001 From: Sebastian Roa <115311276+Roaring30s@users.noreply.github.com> Date: Thu, 26 Oct 2023 10:23:35 -0400 Subject: [PATCH 12/25] :hammer: Add get_window_data function --- crates/evm/lib.rs | 50 +++++++++++++++++++++++++++++++++++------------ 1 file changed, 37 insertions(+), 13 deletions(-) diff --git a/crates/evm/lib.rs b/crates/evm/lib.rs index 191d1f6..d169988 100644 --- a/crates/evm/lib.rs +++ b/crates/evm/lib.rs @@ -1,5 +1,3 @@ -use std::collections::HashMap; - pub use primitive_types::U256; pub use primitive_types::H128; use tiny_keccak::Hasher; @@ -38,6 +36,21 @@ fn to_signed(value: U256) -> U256 { } } +fn get_window_data(data: &Vec, window_size: usize, offset: usize) -> Vec { + let start_index = offset % data.len(); + let end_index = start_index + window_size; + + // Ensure the end index doesn't go beyond the length of the data + let end_index = if end_index > data.len() { + data.len() + } else { + end_index + }; + + // Return the data within the specified window + data[start_index..end_index].to_vec() +} + repr_u8! { // EVM instructions #[repr(u8)] @@ -345,7 +358,8 @@ impl<'a> Machine<'a> { ) -> ExecutionState { let mut pc = 0; let len = bytecode.len(); - let mut instruction_vec: Vec = Vec::new(); + let mut counter = 0; + while pc < len { let opcode = bytecode[pc]; let inst = match Instruction::try_from(opcode) { @@ -358,10 +372,17 @@ impl<'a> Machine<'a> { let cost = (self.cost_fn)(&inst); pc += 1; + counter += 1; + /* + if counter == 576 { + break; + } + */ + println!("{:#?}", inst); - println!("'{:#?}',", inst); - - //println!("Counter: {:#?}", pc); + println!("Position: {:#?}", pc); + println!("Counter: {:#?}", counter); + println!("==================="); match inst { Instruction::Stop => {} Instruction::Add => { @@ -673,7 +694,7 @@ impl<'a> Machine<'a> { } Instruction::Caller => { // TODO: caller - self.stack.push(U256::from("03")); + self.stack.push(U256::zero()); } Instruction::CallValue => { self.stack.push(self.state); @@ -723,9 +744,6 @@ impl<'a> Machine<'a> { let mem_offset = self.stack.peek_step(1).low_u64() as usize; let code_offset = self.stack.peek_step(2); let len = self.stack.peek_step(3).low_u64() as usize; - //println!("Dest Offset: {:#?}", mem_offset); - //println!("Offset: {:#?}", code_offset); - //println!("Size: {:#?}", len); if code_offset > usize::max_value().into() { dbg!("CODECOPY: offset too large"); } @@ -821,14 +839,20 @@ impl<'a> Machine<'a> { if offset > usize::max_value().into() { dbg!("MLOAD: offset too large"); } + let len = offset.low_u64() as usize; + let mut data = vec![0u8; 32]; + let d1 = get_window_data(&self.memory, 32, len); + println!("{:#?}", d1); + /* for (idx, mem_ptr) in (0..len).zip(len..len + 32) { data[idx] = self.memory[mem_ptr]; } - + println!("altered data: {:#?}", data.as_slice()); self.stack.push(U256::from(data.as_slice())); + */ } Instruction::MStore => { let offset = self.stack.pop(); @@ -1353,7 +1377,7 @@ mod tests { */ #[test] fn test_erc_constructor() { - let bytes = hex!("33"); + let bytes = hex!("7f00000000000000000000000000000000000000000000000000000000000000ff600052600151"); let mut machine = Machine::new(test_cost_fn); let status = machine.execute(&bytes, Default::default()); //assert_eq!(status, ExecutionState::Ok); @@ -1361,7 +1385,7 @@ mod tests { println!("LOOK DOWN"); println!("Result: {:#?}", machine.result); println!("Storage: {:#?}", machine.storage); - println!("Memory: {:#?}", machine.memory.len()); + println!("Memory: {:#?}", machine.memory); println!("Stack: {:#?}", machine.stack); } /* From 319c8928297702559bc05ba7565ef7b3f7f34f9f Mon Sep 17 00:00:00 2001 From: Sebastian Roa <115311276+Roaring30s@users.noreply.github.com> Date: Thu, 26 Oct 2023 11:50:25 -0400 Subject: [PATCH 13/25] :hammer: Add Padding to Memory --- crates/evm/lib.rs | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/crates/evm/lib.rs b/crates/evm/lib.rs index d169988..fcdf087 100644 --- a/crates/evm/lib.rs +++ b/crates/evm/lib.rs @@ -843,9 +843,24 @@ impl<'a> Machine<'a> { let len = offset.low_u64() as usize; let mut data = vec![0u8; 32]; - - let d1 = get_window_data(&self.memory, 32, len); - println!("{:#?}", d1); + println!("offset: {:#?}", len); + + // Calcuate bytes to add to memory based on offset + let num_memory_rows = self.memory.len() / 32; + let offset_needed_rows = ((len + 32) as f64 / 32.0).ceil() as usize; + let rows_to_add = offset_needed_rows - num_memory_rows; + println!("num_memory_rows {:#?}", num_memory_rows); + println!("offset_needed_rows {:#?}", offset_needed_rows); + println!("rows_to_add {:#?}", rows_to_add); + + if rows_to_add > 0 { + for _ in 0..=rows_to_add - 1 { + self.memory.extend(std::iter::repeat(0).take(32)); + } + } + //let d1 = get_window_data(&self.memory, 32, len); + //println!("function res: {:#?}", d1); + /* for (idx, mem_ptr) in (0..len).zip(len..len + 32) { data[idx] = self.memory[mem_ptr]; @@ -1377,7 +1392,7 @@ mod tests { */ #[test] fn test_erc_constructor() { - let bytes = hex!("7f00000000000000000000000000000000000000000000000000000000000000ff600052600151"); + let bytes = hex!("7f00000000000000000000000000000000000000000000000000000000000000ff600052602251"); let mut machine = Machine::new(test_cost_fn); let status = machine.execute(&bytes, Default::default()); //assert_eq!(status, ExecutionState::Ok); From 84a24b49db0ad412f02ea14c5a3eef6555308ef2 Mon Sep 17 00:00:00 2001 From: Sebastian Roa <115311276+Roaring30s@users.noreply.github.com> Date: Thu, 26 Oct 2023 14:54:57 -0400 Subject: [PATCH 14/25] :hammer: Add filter_left_zeros Function --- crates/evm/lib.rs | 30 +++++++++++++++++++++++------- 1 file changed, 23 insertions(+), 7 deletions(-) diff --git a/crates/evm/lib.rs b/crates/evm/lib.rs index fcdf087..2e3d506 100644 --- a/crates/evm/lib.rs +++ b/crates/evm/lib.rs @@ -51,6 +51,23 @@ fn get_window_data(data: &Vec, window_size: usize, offset: usize) -> Vec data[start_index..end_index].to_vec() } +fn filter_left_zeros(data: Vec) -> Vec { + let mut result = Vec::new(); + let mut found_non_zero = false; + + for &value in &data { + if value > 0 { + found_non_zero = true; + } + + if found_non_zero || value > 0 { + result.push(value); + } + } + + result +} + repr_u8! { // EVM instructions #[repr(u8)] @@ -849,17 +866,16 @@ impl<'a> Machine<'a> { let num_memory_rows = self.memory.len() / 32; let offset_needed_rows = ((len + 32) as f64 / 32.0).ceil() as usize; let rows_to_add = offset_needed_rows - num_memory_rows; - println!("num_memory_rows {:#?}", num_memory_rows); - println!("offset_needed_rows {:#?}", offset_needed_rows); - println!("rows_to_add {:#?}", rows_to_add); - + if rows_to_add > 0 { for _ in 0..=rows_to_add - 1 { self.memory.extend(std::iter::repeat(0).take(32)); } } - //let d1 = get_window_data(&self.memory, 32, len); - //println!("function res: {:#?}", d1); + + let word = get_window_data(&self.memory, 32, len); + let filtered_word = filter_left_zeros(word); + println!("filtered word: {:#?}", filtered_word); /* for (idx, mem_ptr) in (0..len).zip(len..len + 32) { @@ -1392,7 +1408,7 @@ mod tests { */ #[test] fn test_erc_constructor() { - let bytes = hex!("7f00000000000000000000000000000000000000000000000000000000000000ff600052602251"); + let bytes = hex!("7f00000000000000000000000000000000000000000000000000000000000000ff600052600351"); let mut machine = Machine::new(test_cost_fn); let status = machine.execute(&bytes, Default::default()); //assert_eq!(status, ExecutionState::Ok); From 8ae1092332ff8ea448ac926f8e22e41291080bc1 Mon Sep 17 00:00:00 2001 From: Sebastian Roa <115311276+Roaring30s@users.noreply.github.com> Date: Thu, 26 Oct 2023 17:03:58 -0400 Subject: [PATCH 15/25] :wrench: Fix MLOAD Opcode --- crates/evm/lib.rs | 21 ++++++++------------- 1 file changed, 8 insertions(+), 13 deletions(-) diff --git a/crates/evm/lib.rs b/crates/evm/lib.rs index 2e3d506..796dee6 100644 --- a/crates/evm/lib.rs +++ b/crates/evm/lib.rs @@ -859,9 +859,6 @@ impl<'a> Machine<'a> { let len = offset.low_u64() as usize; - let mut data = vec![0u8; 32]; - println!("offset: {:#?}", len); - // Calcuate bytes to add to memory based on offset let num_memory_rows = self.memory.len() / 32; let offset_needed_rows = ((len + 32) as f64 / 32.0).ceil() as usize; @@ -875,15 +872,12 @@ impl<'a> Machine<'a> { let word = get_window_data(&self.memory, 32, len); let filtered_word = filter_left_zeros(word); - println!("filtered word: {:#?}", filtered_word); - - /* - for (idx, mem_ptr) in (0..len).zip(len..len + 32) { - data[idx] = self.memory[mem_ptr]; - } - println!("altered data: {:#?}", data.as_slice()); - self.stack.push(U256::from(data.as_slice())); - */ + let filtered_hex: Vec = filtered_word.iter().map(|u256| format!("{:02x}", u256)).collect(); + let joined_filtered: String = filtered_hex.into_iter().map(|byte| byte.to_string()).collect(); + let word_u256 = U256::from_str_radix(joined_filtered.as_str(), 16).unwrap(); + + self.stack.push(U256::from(word_u256)); + } Instruction::MStore => { let offset = self.stack.pop(); @@ -1408,7 +1402,7 @@ mod tests { */ #[test] fn test_erc_constructor() { - let bytes = hex!("7f00000000000000000000000000000000000000000000000000000000000000ff600052600351"); + let bytes = hex!("7f00000000000000000000000000000000000000000000000000000000000011ff6000527f00000000000000000000000000000000000000000000000000000000002200ff602052601f51"); let mut machine = Machine::new(test_cost_fn); let status = machine.execute(&bytes, Default::default()); //assert_eq!(status, ExecutionState::Ok); @@ -1418,6 +1412,7 @@ mod tests { println!("Storage: {:#?}", machine.storage); println!("Memory: {:#?}", machine.memory); println!("Stack: {:#?}", machine.stack); + println!("Test: {:#?}", U256::from("ff00000000000000000000000000000000000000000000000000000000002200")); } /* #[test] From 7fa480f033c3555a1bf9392b6629e8ef7df759d9 Mon Sep 17 00:00:00 2001 From: Sebastian Roa <115311276+Roaring30s@users.noreply.github.com> Date: Thu, 26 Oct 2023 22:10:16 -0400 Subject: [PATCH 16/25] :hammer: Add Test --- crates/evm/lib.rs | 27 ++++++++++++++++++++------- 1 file changed, 20 insertions(+), 7 deletions(-) diff --git a/crates/evm/lib.rs b/crates/evm/lib.rs index 796dee6..b5440ad 100644 --- a/crates/evm/lib.rs +++ b/crates/evm/lib.rs @@ -399,6 +399,7 @@ impl<'a> Machine<'a> { println!("Position: {:#?}", pc); println!("Counter: {:#?}", counter); + println!("==================="); match inst { Instruction::Stop => {} @@ -413,8 +414,15 @@ impl<'a> Machine<'a> { Instruction::Sub => { let lhs = self.stack.pop(); let rhs = self.stack.pop(); - let lhs16: u16 = lhs.as_u64() as u16; - let rhs16: u16 = rhs.as_u64() as u16; + + if counter == 534 { + println!("STACK: {:#?}", self.stack); + println!("TEST: {:#?}", U256::from("be862ad9abfe6f22bcb087716c7d89a26051f74c")); + } + + let lhs16: u16 = lhs.as_u64() as u16; // culprits causing an issue + let rhs16: u16 = rhs.as_u64() as u16; // Should wallet be converted to base16? We need to see what obj truth offers wallet conversion + let difference = U256::from(lhs16.overflowing_sub(rhs16).0); self.stack.push(difference); } @@ -711,7 +719,7 @@ impl<'a> Machine<'a> { } Instruction::Caller => { // TODO: caller - self.stack.push(U256::zero()); + self.stack.push(U256::from("320F23780c98f1cbA153dA685e67c4F02aC78bd1")); } Instruction::CallValue => { self.stack.push(self.state); @@ -862,7 +870,12 @@ impl<'a> Machine<'a> { // Calcuate bytes to add to memory based on offset let num_memory_rows = self.memory.len() / 32; let offset_needed_rows = ((len + 32) as f64 / 32.0).ceil() as usize; - let rows_to_add = offset_needed_rows - num_memory_rows; + println!("OFFSET: {:#?}", len); + println!("num_memory_rows: {:#?}", num_memory_rows); + println!("offset_needed_row {:#?}", offset_needed_rows); + + let rows_to_add = offset_needed_rows as i32 - num_memory_rows as i32; + println!("rows_to_add {:#?}", rows_to_add); if rows_to_add > 0 { for _ in 0..=rows_to_add - 1 { @@ -875,7 +888,7 @@ impl<'a> Machine<'a> { let filtered_hex: Vec = filtered_word.iter().map(|u256| format!("{:02x}", u256)).collect(); let joined_filtered: String = filtered_hex.into_iter().map(|byte| byte.to_string()).collect(); let word_u256 = U256::from_str_radix(joined_filtered.as_str(), 16).unwrap(); - + println!("HEXXY {:#?}", joined_filtered); self.stack.push(U256::from(word_u256)); } @@ -1402,7 +1415,7 @@ mod tests { */ #[test] fn test_erc_constructor() { - let bytes = hex!("7f00000000000000000000000000000000000000000000000000000000000011ff6000527f00000000000000000000000000000000000000000000000000000000002200ff602052601f51"); + let bytes = hex!("608060405234801561000f575f80fd5b506101438061001d5f395ff3fe608060405234801561000f575f80fd5b5060043610610034575f3560e01c80632e64cec1146100385780636057361d14610056575b5f80fd5b610040610072565b60405161004d919061009b565b60405180910390f35b610070600480360381019061006b91906100e2565b61007a565b005b5f8054905090565b805f8190555050565b5f819050919050565b61009581610083565b82525050565b5f6020820190506100ae5f83018461008c565b92915050565b5f80fd5b6100c181610083565b81146100cb575f80fd5b50565b5f813590506100dc816100b8565b92915050565b5f602082840312156100f7576100f66100b4565b5b5f610104848285016100ce565b9150509291505056fea2646970667358221220b5c3075f2f2034d039a227fac6dd314b052ffb2b3da52c7b6f5bc374d528ed3664736f6c63430008140033"); let mut machine = Machine::new(test_cost_fn); let status = machine.execute(&bytes, Default::default()); //assert_eq!(status, ExecutionState::Ok); @@ -1412,7 +1425,7 @@ mod tests { println!("Storage: {:#?}", machine.storage); println!("Memory: {:#?}", machine.memory); println!("Stack: {:#?}", machine.stack); - println!("Test: {:#?}", U256::from("ff00000000000000000000000000000000000000000000000000000000002200")); + } /* #[test] From 3dd532d87fa9ce98517b34c8f2679c42f405622d Mon Sep 17 00:00:00 2001 From: Sebastian Roa <115311276+Roaring30s@users.noreply.github.com> Date: Fri, 27 Oct 2023 14:32:17 -0400 Subject: [PATCH 17/25] :wrench: Fix CODECOPY Opcode --- crates/evm/lib.rs | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/crates/evm/lib.rs b/crates/evm/lib.rs index b5440ad..e810ce6 100644 --- a/crates/evm/lib.rs +++ b/crates/evm/lib.rs @@ -391,13 +391,14 @@ impl<'a> Machine<'a> { pc += 1; counter += 1; /* - if counter == 576 { + if counter == 16 { break; } */ + println!("{:#?}", inst); - println!("Position: {:#?}", pc); + println!("Position: {:#?}", pc-1); println!("Counter: {:#?}", counter); println!("==================="); @@ -766,9 +767,9 @@ impl<'a> Machine<'a> { self.stack.push(U256::from(len)); } Instruction::CodeCopy => { - let mem_offset = self.stack.peek_step(1).low_u64() as usize; - let code_offset = self.stack.peek_step(2); - let len = self.stack.peek_step(3).low_u64() as usize; + let mem_offset = self.stack.pop().low_u64() as usize; + let code_offset = self.stack.pop(); + let len = self.stack.pop().low_u64() as usize; if code_offset > usize::max_value().into() { dbg!("CODECOPY: offset too large"); } @@ -1088,6 +1089,7 @@ impl<'a> Machine<'a> { .copy_from_slice(&evm.result); } Instruction::Return => { + let offset = self.stack.pop(); if offset > usize::max_value().into() { @@ -1425,6 +1427,14 @@ mod tests { println!("Storage: {:#?}", machine.storage); println!("Memory: {:#?}", machine.memory); println!("Stack: {:#?}", machine.stack); + + /* + let test_wallet = U256::from("be862ad9abfe6f22bcb087716c7d89a26051f74b"); + let convert_wallet: u16 = test_wallet.as_u64() as u16; + println!("Wallet to u256 {:#?}", test_wallet); + println!("Wallet to u16 {:#?}", convert_wallet); + */ + } /* From ced6d7f2fe342acfdf6b78073cb85902952551ca Mon Sep 17 00:00:00 2001 From: Sebastian Roa <115311276+Roaring30s@users.noreply.github.com> Date: Sat, 28 Oct 2023 00:08:59 -0400 Subject: [PATCH 18/25] :wrench: Fix COPYCODE Opcode Memory Allocation --- crates/evm/lib.rs | 45 ++++++++++++--------------------------------- 1 file changed, 12 insertions(+), 33 deletions(-) diff --git a/crates/evm/lib.rs b/crates/evm/lib.rs index e810ce6..9d34485 100644 --- a/crates/evm/lib.rs +++ b/crates/evm/lib.rs @@ -390,11 +390,11 @@ impl<'a> Machine<'a> { pc += 1; counter += 1; - /* - if counter == 16 { + /* + if counter == 20 { break; } - */ + */ println!("{:#?}", inst); @@ -416,11 +416,6 @@ impl<'a> Machine<'a> { let lhs = self.stack.pop(); let rhs = self.stack.pop(); - if counter == 534 { - println!("STACK: {:#?}", self.stack); - println!("TEST: {:#?}", U256::from("be862ad9abfe6f22bcb087716c7d89a26051f74c")); - } - let lhs16: u16 = lhs.as_u64() as u16; // culprits causing an issue let rhs16: u16 = rhs.as_u64() as u16; // Should wallet be converted to base16? We need to see what obj truth offers wallet conversion @@ -720,7 +715,7 @@ impl<'a> Machine<'a> { } Instruction::Caller => { // TODO: caller - self.stack.push(U256::from("320F23780c98f1cbA153dA685e67c4F02aC78bd1")); + self.stack.push(U256::zero()); } Instruction::CallValue => { self.stack.push(self.state); @@ -778,36 +773,20 @@ impl<'a> Machine<'a> { let code: &[u8]; // Needed for testing parts of bytecode to avoid out of bound errors in &bytecode[code_offset..code_offset + len] let mut temp_vec: Vec = vec![]; + println!("mem_offset: {:#?}", mem_offset); + println!("code_offset: {:#?}", code_offset); + println!("len: {:#?}", len); + println!("Memory Size: {:#?}", self.memory.len()); - if code_offset + len <= bytecode.len() { - code = &bytecode[code_offset..code_offset + len]; - } else { - // Used for byte by byte testing - temp_vec.extend(std::iter::repeat(0).take(len - code_offset)); - code = &temp_vec; - } - println!("length of new code {:#?}", &code.len()); - //ATTENTION: Later investigate if code is being injected into memory correctly - // I dont think the below code is correct. - if self.memory.len() < mem_offset + 32 { - self.memory.resize(mem_offset + 32, 0); + if self.memory.len() < (mem_offset + len) { + self.memory.extend(std::iter::repeat(0).take(mem_offset+len - self.memory.len())); } - - // Calculate padding for resizing - let current_size = code.len() + mem_offset; - let remainder = current_size % 32; - let padding = if remainder == 0 { 0 } else { 32 - remainder }; - - // Resize - self.memory.resize(current_size + padding + 32, 0); + code = &bytecode[code_offset..code_offset + len]; + println!("Grabbed Code: {:#?}", code); //Calculate new space of zeroes for i in 0..=code.len() - 1 { - if i > code.len() { - self.memory[mem_offset + i] = 0; - } else { self.memory[mem_offset + i] = code[i]; - } } } Instruction::ExtCodeSize => { From 32f083aa941d5ab65ca4a6d620bf745465bd579d Mon Sep 17 00:00:00 2001 From: Sebastian Roa <115311276+Roaring30s@users.noreply.github.com> Date: Tue, 28 Nov 2023 21:26:21 -0500 Subject: [PATCH 19/25] :hammer: Add EXM SHA256 Function --- js/napi/src/lib.rs | 40 +++++++++++++++++++++++++++++++++++++++- 1 file changed, 39 insertions(+), 1 deletion(-) diff --git a/js/napi/src/lib.rs b/js/napi/src/lib.rs index 96c9f2d..2f67d90 100644 --- a/js/napi/src/lib.rs +++ b/js/napi/src/lib.rs @@ -757,7 +757,45 @@ mod tests { //println!("{}", contract.result); assert_eq!(contract.result, "[\"Yangtze\",\"Amazon\"]"); } - + + #[tokio::test] + pub async fn simulate_sha256() { + let contract_source_bytes = + include_bytes!("../../../testdata/contracts/sha256.js"); + let contract_source_vec = contract_source_bytes.to_vec(); + let execution_context: SimulateExecutionContext = + SimulateExecutionContext { + contract_id: String::new(), + interactions: vec![SimulateInput { + id: String::from("abcd"), + owner: String::from("210392sdaspd-asdm-asd_sa0d1293-lc"), + quantity: String::from("12301"), + reward: String::from("12931293"), + target: None, + tags: vec![], + block: None, + input: serde_json::json!({ + "act": "1", + }) + .to_string(), + }], + contract_init_state: Some(r#"{}"#.into()), + maybe_config: None, + maybe_cache: Some(false), + maybe_bundled_contract: None, + maybe_settings: None, + maybe_exm_context: Some(r#"{"requests": {}, "kv": {}, "initiated":[]}"#.into()), + maybe_contract_source: Some(ContractSource { + contract_src: contract_source_vec.into(), + contract_type: SimulateContractType::JAVASCRIPT, + }), + }; + + let contract = simulate_contract(execution_context).await.unwrap(); + const hello_hash: &str = "2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824"; + assert_eq!(contract.state[0], hello_hash); + } + #[tokio::test] pub async fn simulate_kv_get() { let contract_source_bytes = From e046651045ba6fe7bd2ec7e1f134172f05e19048 Mon Sep 17 00:00:00 2001 From: Sebastian Roa <115311276+Roaring30s@users.noreply.github.com> Date: Tue, 28 Nov 2023 21:30:30 -0500 Subject: [PATCH 20/25] :wrench: Employ Cargo Fmt --- crates/evm/lib.rs | 122 ++++++++++++++++++++--------------- crates/exm/base.js | 12 ++++ testdata/contracts/sha256.js | 8 +++ 3 files changed, 91 insertions(+), 51 deletions(-) create mode 100644 testdata/contracts/sha256.js diff --git a/crates/evm/lib.rs b/crates/evm/lib.rs index 9d34485..6cf97b2 100644 --- a/crates/evm/lib.rs +++ b/crates/evm/lib.rs @@ -1,5 +1,5 @@ -pub use primitive_types::U256; pub use primitive_types::H128; +pub use primitive_types::U256; use tiny_keccak::Hasher; use tiny_keccak::Keccak; @@ -36,15 +36,19 @@ fn to_signed(value: U256) -> U256 { } } -fn get_window_data(data: &Vec, window_size: usize, offset: usize) -> Vec { +fn get_window_data( + data: &Vec, + window_size: usize, + offset: usize, +) -> Vec { let start_index = offset % data.len(); let end_index = start_index + window_size; // Ensure the end index doesn't go beyond the length of the data let end_index = if end_index > data.len() { - data.len() + data.len() } else { - end_index + end_index }; // Return the data within the specified window @@ -56,13 +60,13 @@ fn filter_left_zeros(data: Vec) -> Vec { let mut found_non_zero = false; for &value in &data { - if value > 0 { - found_non_zero = true; - } + if value > 0 { + found_non_zero = true; + } - if found_non_zero || value > 0 { - result.push(value); - } + if found_non_zero || value > 0 { + result.push(value); + } } result @@ -390,15 +394,19 @@ impl<'a> Machine<'a> { pc += 1; counter += 1; - /* - if counter == 20 { - break; + + /* + Error taking place at 114 + PUSH32 is not being injected correctly + */ + if counter == 114 { + break; } - */ - + println!("{:#?}", inst); + println!("OPCODE: {:#?}", opcode); - println!("Position: {:#?}", pc-1); + println!("Position: {:#?}", pc - 1); println!("Counter: {:#?}", counter); println!("==================="); @@ -417,7 +425,7 @@ impl<'a> Machine<'a> { let rhs = self.stack.pop(); let lhs16: u16 = lhs.as_u64() as u16; // culprits causing an issue - let rhs16: u16 = rhs.as_u64() as u16; // Should wallet be converted to base16? We need to see what obj truth offers wallet conversion + let rhs16: u16 = rhs.as_u64() as u16; // Should wallet be converted to base16? We need to see what obj truth offers wallet conversion let difference = U256::from(lhs16.overflowing_sub(rhs16).0); self.stack.push(difference); @@ -769,24 +777,26 @@ impl<'a> Machine<'a> { dbg!("CODECOPY: offset too large"); } let code_offset = code_offset.low_u64() as usize; - + let code: &[u8]; // Needed for testing parts of bytecode to avoid out of bound errors in &bytecode[code_offset..code_offset + len] - let mut temp_vec: Vec = vec![]; - println!("mem_offset: {:#?}", mem_offset); - println!("code_offset: {:#?}", code_offset); - println!("len: {:#?}", len); - println!("Memory Size: {:#?}", self.memory.len()); - + let mut temp_vec: Vec = vec![]; + //println!("mem_offset: {:#?}", mem_offset); + //println!("code_offset: {:#?}", code_offset); + //println!("len: {:#?}", len); + //println!("Memory Size: {:#?}", self.memory.len()); + if self.memory.len() < (mem_offset + len) { - self.memory.extend(std::iter::repeat(0).take(mem_offset+len - self.memory.len())); + self.memory.extend( + std::iter::repeat(0).take(mem_offset + len - self.memory.len()), + ); } code = &bytecode[code_offset..code_offset + len]; - println!("Grabbed Code: {:#?}", code); - + //println!("Grabbed Code: {:#?}", code); + //Calculate new space of zeroes for i in 0..=code.len() - 1 { - self.memory[mem_offset + i] = code[i]; + self.memory[mem_offset + i] = code[i]; } } Instruction::ExtCodeSize => { @@ -844,19 +854,19 @@ impl<'a> Machine<'a> { if offset > usize::max_value().into() { dbg!("MLOAD: offset too large"); } - + let len = offset.low_u64() as usize; - + // Calcuate bytes to add to memory based on offset let num_memory_rows = self.memory.len() / 32; let offset_needed_rows = ((len + 32) as f64 / 32.0).ceil() as usize; - println!("OFFSET: {:#?}", len); - println!("num_memory_rows: {:#?}", num_memory_rows); - println!("offset_needed_row {:#?}", offset_needed_rows); - + //println!("OFFSET: {:#?}", len); + //println!("num_memory_rows: {:#?}", num_memory_rows); + //println!("offset_needed_row {:#?}", offset_needed_rows); + let rows_to_add = offset_needed_rows as i32 - num_memory_rows as i32; - println!("rows_to_add {:#?}", rows_to_add); - + //println!("rows_to_add {:#?}", rows_to_add); + if rows_to_add > 0 { for _ in 0..=rows_to_add - 1 { self.memory.extend(std::iter::repeat(0).take(32)); @@ -865,12 +875,17 @@ impl<'a> Machine<'a> { let word = get_window_data(&self.memory, 32, len); let filtered_word = filter_left_zeros(word); - let filtered_hex: Vec = filtered_word.iter().map(|u256| format!("{:02x}", u256)).collect(); - let joined_filtered: String = filtered_hex.into_iter().map(|byte| byte.to_string()).collect(); - let word_u256 = U256::from_str_radix(joined_filtered.as_str(), 16).unwrap(); - println!("HEXXY {:#?}", joined_filtered); + let filtered_hex: Vec = filtered_word + .iter() + .map(|u256| format!("{:02x}", u256)) + .collect(); + let joined_filtered: String = filtered_hex + .into_iter() + .map(|byte| byte.to_string()) + .collect(); + let word_u256 = + U256::from_str_radix(joined_filtered.as_str(), 16).unwrap(); self.stack.push(U256::from(word_u256)); - } Instruction::MStore => { let offset = self.stack.pop(); @@ -973,6 +988,7 @@ impl<'a> Machine<'a> { | Instruction::Push32 => { let value_size = (opcode - 0x60 + 1) as usize; let value = &bytecode[pc..pc + value_size]; + println!("VALUE ADDED {:#?} :", value); pc += value_size; self.stack.push(U256::from(value)); } @@ -1068,7 +1084,6 @@ impl<'a> Machine<'a> { .copy_from_slice(&evm.result); } Instruction::Return => { - let offset = self.stack.pop(); if offset > usize::max_value().into() { @@ -1106,8 +1121,6 @@ impl<'a> Machine<'a> { } } - - #[cfg(test)] mod tests { use crate::storage::Storage; @@ -1396,25 +1409,24 @@ mod tests { */ #[test] fn test_erc_constructor() { - let bytes = hex!("608060405234801561000f575f80fd5b506101438061001d5f395ff3fe608060405234801561000f575f80fd5b5060043610610034575f3560e01c80632e64cec1146100385780636057361d14610056575b5f80fd5b610040610072565b60405161004d919061009b565b60405180910390f35b610070600480360381019061006b91906100e2565b61007a565b005b5f8054905090565b805f8190555050565b5f819050919050565b61009581610083565b82525050565b5f6020820190506100ae5f83018461008c565b92915050565b5f80fd5b6100c181610083565b81146100cb575f80fd5b50565b5f813590506100dc816100b8565b92915050565b5f602082840312156100f7576100f66100b4565b5b5f610104848285016100ce565b9150509291505056fea2646970667358221220b5c3075f2f2034d039a227fac6dd314b052ffb2b3da52c7b6f5bc374d528ed3664736f6c63430008140033"); + //let bytes = hex!("608060405234801562000010575f80fd5b506040516200166f3803806200166f8339818101604052810190620000369190620003cb565b6040518060400160405280600481526020017f476f6c64000000000000000000000000000000000000000000000000000000008152506040518060400160405280600381526020017f474c4400000000000000000000000000000000000000000000000000000000008152508160039081620000b3919062000656565b508060049081620000c5919062000656565b505050620000da3382620000e160201b60201c565b5062000866565b5f73ffffffffffffffffffffffffffffffffffffffff168273ffffffffffffffffffffffffffffffffffffffff160362000154575f6040517fec442f050000000000000000000000000000000000000000000000000000000081526004016200014b91906200077d565b60405180910390fd5b620001675f83836200016b60201b60201c565b5050565b5f73ffffffffffffffffffffffffffffffffffffffff168373ffffffffffffffffffffffffffffffffffffffff1603620001bf578060025f828254620001b29190620007c5565b9250508190555062000290565b5f805f8573ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020019081526020015f20549050818110156200024b578381836040517fe450d38c000000000000000000000000000000000000000000000000000000008152600401620002429392919062000810565b60405180910390fd5b8181035f808673ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020019081526020015f2081905550505b5f73ffffffffffffffffffffffffffffffffffffffff168273ffffffffffffffffffffffffffffffffffffffff1603620002d9578060025f828254039250508190555062000323565b805f808473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020019081526020015f205f82825401925050819055505b8173ffffffffffffffffffffffffffffffffffffffff168373ffffffffffffffffffffffffffffffffffffffff167fddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef836040516200038291906200084b565b60405180910390a3505050565b5f80fd5b5f819050919050565b620003a78162000393565b8114620003b2575f80fd5b50565b5f81519050620003c5816200039c565b92915050565b5f60208284031215620003e357620003e26200038f565b5b5f620003f284828501620003b5565b91505092915050565b5f81519050919050565b7f4e487b71000000000000000000000000000000000000000000000000000000005f52604160045260245ffd5b7f4e487b71000000000000000000000000000000000000000000000000000000005f52602260045260245ffd5b5f60028204905060018216806200047757607f821691505b6020821081036200048d576200048c62000432565b5b50919050565b5f819050815f5260205f209050919050565b5f6020601f8301049050919050565b5f82821b905092915050565b5f60088302620004f17fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff82620004b4565b620004fd8683620004b4565b95508019841693508086168417925050509392505050565b5f819050919050565b5f6200053e62000538620005328462000393565b62000515565b62000393565b9050919050565b5f819050919050565b62000559836200051e565b62000571620005688262000545565b848454620004c0565b825550505050565b5f90565b6200058762000579565b620005948184846200054e565b505050565b5b81811015620005bb57620005af5f826200057d565b6001810190506200059a565b5050565b601f8211156200060a57620005d48162000493565b620005df84620004a5565b81016020851015620005ef578190505b62000607620005fe85620004a5565b83018262000599565b50505b505050565b5f82821c905092915050565b5f6200062c5f19846008026200060f565b1980831691505092915050565b5f6200064683836200061b565b9150826002028217905092915050565b6200066182620003fb565b67ffffffffffffffff8111156200067d576200067c62000405565b5b6200068982546200045f565b62000696828285620005bf565b5f60209050601f831160018114620006cc575f8415620006b7578287015190505b620006c3858262000639565b86555062000732565b601f198416620006dc8662000493565b5f5b828110156200070557848901518255600182019150602085019450602081019050620006de565b8683101562000725578489015162000721601f8916826200061b565b8355505b6001600288020188555050505b505050505050565b5f73ffffffffffffffffffffffffffffffffffffffff82169050919050565b5f62000765826200073a565b9050919050565b620007778162000759565b82525050565b5f602082019050620007925f8301846200076c565b92915050565b7f4e487b71000000000000000000000000000000000000000000000000000000005f52601160045260245ffd5b5f620007d18262000393565b9150620007de8362000393565b9250828201905080821115620007f957620007f862000798565b5b92915050565b6200080a8162000393565b82525050565b5f606082019050620008255f8301866200076c565b620008346020830185620007ff565b620008436040830184620007ff565b949350505050565b5f602082019050620008605f830184620007ff565b92915050565b610dfb80620008745f395ff3fe608060405234801561000f575f80fd5b5060043610610091575f3560e01c8063313ce56711610064578063313ce5671461013157806370a082311461014f57806395d89b411461017f578063a9059cbb1461019d578063dd62ed3e146101cd57610091565b806306fdde0314610095578063095ea7b3146100b357806318160ddd146100e357806323b872dd14610101575b5f80fd5b61009d6101fd565b6040516100aa9190610a74565b60405180910390f35b6100cd60048036038101906100c89190610b25565b61028d565b6040516100da9190610b7d565b60405180910390f35b6100eb6102af565b6040516100f89190610ba5565b60405180910390f35b61011b60048036038101906101169190610bbe565b6102b8565b6040516101289190610b7d565b60405180910390f35b6101396102e6565b6040516101469190610c29565b60405180910390f35b61016960048036038101906101649190610c42565b6102ee565b6040516101769190610ba5565b60405180910390f35b610187610333565b6040516101949190610a74565b60405180910390f35b6101b760048036038101906101b29190610b25565b6103c3565b6040516101c49190610b7d565b60405180910390f35b6101e760048036038101906101e29190610c6d565b6103e5565b6040516101f49190610ba5565b60405180910390f35b60606003805461020c90610cd8565b80601f016020809104026020016040519081016040528092919081815260200182805461023890610cd8565b80156102835780601f1061025a57610100808354040283529160200191610283565b820191905f5260205f20905b81548152906001019060200180831161026657829003601f168201915b5050505050905090565b5f80610297610467565b90506102a481858561046e565b600191505092915050565b5f600254905090565b5f806102c2610467565b90506102cf858285610480565b6102da858585610512565b60019150509392505050565b5f6012905090565b5f805f8373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020019081526020015f20549050919050565b60606004805461034290610cd8565b80601f016020809104026020016040519081016040528092919081815260200182805461036e90610cd8565b80156103b95780601f10610390576101008083540402835291602001916103b9565b820191905f5260205f20905b81548152906001019060200180831161039c57829003601f168201915b5050505050905090565b5f806103cd610467565b90506103da818585610512565b600191505092915050565b5f60015f8473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020019081526020015f205f8373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020019081526020015f2054905092915050565b5f33905090565b61047b8383836001610602565b505050565b5f61048b84846103e5565b90507fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff811461050c57818110156104fd578281836040517ffb8f41b20000000000000000000000000000000000000000000000000000000081526004016104f493929190610d17565b60405180910390fd5b61050b84848484035f610602565b5b50505050565b5f73ffffffffffffffffffffffffffffffffffffffff168373ffffffffffffffffffffffffffffffffffffffff1603610582575f6040517f96c6fd1e0000000000000000000000000000000000000000000000000000000081526004016105799190610d4c565b60405180910390fd5b5f73ffffffffffffffffffffffffffffffffffffffff168273ffffffffffffffffffffffffffffffffffffffff16036105f2575f6040517fec442f050000000000000000000000000000000000000000000000000000000081526004016105e99190610d4c565b60405180910390fd5b6105fd8383836107d1565b505050565b5f73ffffffffffffffffffffffffffffffffffffffff168473ffffffffffffffffffffffffffffffffffffffff1603610672575f6040517fe602df050000000000000000000000000000000000000000000000000000000081526004016106699190610d4c565b60405180910390fd5b5f73ffffffffffffffffffffffffffffffffffffffff168373ffffffffffffffffffffffffffffffffffffffff16036106e2575f6040517f94280d620000000000000000000000000000000000000000000000000000000081526004016106d99190610d4c565b60405180910390fd5b8160015f8673ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020019081526020015f205f8573ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020019081526020015f208190555080156107cb578273ffffffffffffffffffffffffffffffffffffffff168473ffffffffffffffffffffffffffffffffffffffff167f8c5be1e5ebec7d5bd14f71427d1e84f3dd0314c0f7b2291e5b200ac8c7c3b925846040516107c29190610ba5565b60405180910390a35b50505050565b5f73ffffffffffffffffffffffffffffffffffffffff168373ffffffffffffffffffffffffffffffffffffffff1603610821578060025f8282546108159190610d92565b925050819055506108ef565b5f805f8573ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020019081526020015f20549050818110156108aa578381836040517fe450d38c0000000000000000000000000000000000000000000000000000000081526004016108a193929190610d17565b60405180910390fd5b8181035f808673ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020019081526020015f2081905550505b5f73ffffffffffffffffffffffffffffffffffffffff168273ffffffffffffffffffffffffffffffffffffffff1603610936578060025f8282540392505081905550610980565b805f808473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020019081526020015f205f82825401925050819055505b8173ffffffffffffffffffffffffffffffffffffffff168373ffffffffffffffffffffffffffffffffffffffff167fddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef836040516109dd9190610ba5565b60405180910390a3505050565b5f81519050919050565b5f82825260208201905092915050565b5f5b83811015610a21578082015181840152602081019050610a06565b5f8484015250505050565b5f601f19601f8301169050919050565b5f610a46826109ea565b610a5081856109f4565b9350610a60818560208601610a04565b610a6981610a2c565b840191505092915050565b5f6020820190508181035f830152610a8c8184610a3c565b905092915050565b5f80fd5b5f73ffffffffffffffffffffffffffffffffffffffff82169050919050565b5f610ac182610a98565b9050919050565b610ad181610ab7565b8114610adb575f80fd5b50565b5f81359050610aec81610ac8565b92915050565b5f819050919050565b610b0481610af2565b8114610b0e575f80fd5b50565b5f81359050610b1f81610afb565b92915050565b5f8060408385031215610b3b57610b3a610a94565b5b5f610b4885828601610ade565b9250506020610b5985828601610b11565b9150509250929050565b5f8115159050919050565b610b7781610b63565b82525050565b5f602082019050610b905f830184610b6e565b92915050565b610b9f81610af2565b82525050565b5f602082019050610bb85f830184610b96565b92915050565b5f805f60608486031215610bd557610bd4610a94565b5b5f610be286828701610ade565b9350506020610bf386828701610ade565b9250506040610c0486828701610b11565b9150509250925092565b5f60ff82169050919050565b610c2381610c0e565b82525050565b5f602082019050610c3c5f830184610c1a565b92915050565b5f60208284031215610c5757610c56610a94565b5b5f610c6484828501610ade565b91505092915050565b5f8060408385031215610c8357610c82610a94565b5b5f610c9085828601610ade565b9250506020610ca185828601610ade565b9150509250929050565b7f4e487b71000000000000000000000000000000000000000000000000000000005f52602260045260245ffd5b5f6002820490506001821680610cef57607f821691505b602082108103610d0257610d01610cab565b5b50919050565b610d1181610ab7565b82525050565b5f606082019050610d2a5f830186610d08565b610d376020830185610b96565b610d446040830184610b96565b949350505050565b5f602082019050610d5f5f830184610d08565b92915050565b7f4e487b71000000000000000000000000000000000000000000000000000000005f52601160045260245ffd5b5f610d9c82610af2565b9150610da783610af2565b9250828201905080821115610dbf57610dbe610d65565b5b9291505056fea26469706673582212209e44fcf9f1633c4ef31f016a68dcf977b7606fd376618fb0b59f05773c82a5f864736f6c63430008160033000000000000000000000000000000000000000000000000000000000000007b"); + let bytes = hex!("73ffffffffffffffffffffffffffffffffffffffff"); let mut machine = Machine::new(test_cost_fn); let status = machine.execute(&bytes, Default::default()); //assert_eq!(status, ExecutionState::Ok); - println!("LOOK DOWN"); - println!("Result: {:#?}", machine.result); + println!("EVM INFO"); + //println!("Result: {:#?}", machine.result); println!("Storage: {:#?}", machine.storage); println!("Memory: {:#?}", machine.memory); println!("Stack: {:#?}", machine.stack); - /* + /* let test_wallet = U256::from("be862ad9abfe6f22bcb087716c7d89a26051f74b"); let convert_wallet: u16 = test_wallet.as_u64() as u16; println!("Wallet to u256 {:#?}", test_wallet); println!("Wallet to u16 {:#?}", convert_wallet); */ - - } /* #[test] @@ -1466,4 +1478,12 @@ mod tests { /* Yield integer overflow error to u64 60017fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff01 -*/ \ No newline at end of file +*/ + +/* +476f6c6400000000000000000000000000000000000000000000000000000000 is the PUSH32 (7F) value + +result in decimal: + +40343534588991024208220822200420820688602844086 +*/ diff --git a/crates/exm/base.js b/crates/exm/base.js index 946c2ff..43c3211 100644 --- a/crates/exm/base.js +++ b/crates/exm/base.js @@ -249,6 +249,18 @@ }); } + async stringToSHA256(input) { + const encoder = new TextEncoder(); + const data = encoder.encode(input); + + const hashBuffer = await crypto.subtle.digest('SHA-256', data); + + const hashArray = Array.from(new Uint8Array(hashBuffer)); + const hashHex = hashArray.map(byte => byte.toString(16).padStart(2, '0')).join(''); + + return hashHex; + } + } const ExmSymbol = Symbol('exm'); diff --git a/testdata/contracts/sha256.js b/testdata/contracts/sha256.js new file mode 100644 index 0000000..21a2ea1 --- /dev/null +++ b/testdata/contracts/sha256.js @@ -0,0 +1,8 @@ +export async function handle(state, action) { + try { + const someSha = await EXM.stringToSHA256("hello"); + return { state: [someSha] }; + } catch(e) { + return { state: e.toString() } + } +} From bc3d5d1c17284984fe067d9da45e6cc03598e4ac Mon Sep 17 00:00:00 2001 From: Sebastian Roa <115311276+Roaring30s@users.noreply.github.com> Date: Tue, 28 Nov 2023 22:53:15 -0500 Subject: [PATCH 21/25] :wrench: Retest --- js/napi/src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/js/napi/src/lib.rs b/js/napi/src/lib.rs index 2f67d90..b7c097b 100644 --- a/js/napi/src/lib.rs +++ b/js/napi/src/lib.rs @@ -800,7 +800,7 @@ mod tests { pub async fn simulate_kv_get() { let contract_source_bytes = include_bytes!("../../../testdata/contracts/getKv.js"); - + let contract_source_vec = contract_source_bytes.to_vec(); let execution_context: SimulateExecutionContext = SimulateExecutionContext { From 457f349a053e38ee4b9278c06fc69d73f3b26870 Mon Sep 17 00:00:00 2001 From: Sebastian Roa <115311276+Roaring30s@users.noreply.github.com> Date: Mon, 11 Dec 2023 11:29:31 -0500 Subject: [PATCH 22/25] :hammer: Create SmartWeave convertString Function --- js/napi/src/lib.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/js/napi/src/lib.rs b/js/napi/src/lib.rs index b7c097b..ecbfe38 100644 --- a/js/napi/src/lib.rs +++ b/js/napi/src/lib.rs @@ -753,8 +753,6 @@ mod tests { }; let contract = simulate_contract(execution_context).await.unwrap(); - //println!("{}", contract.exm_context); - //println!("{}", contract.result); assert_eq!(contract.result, "[\"Yangtze\",\"Amazon\"]"); } @@ -793,7 +791,9 @@ mod tests { let contract = simulate_contract(execution_context).await.unwrap(); const hello_hash: &str = "2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824"; - assert_eq!(contract.state[0], hello_hash); + println!("{:#?}", contract.state); + println!("{:#?}", contract.state[0]); + //assert_eq!(contract.state[0], hello_hash); } #[tokio::test] From 74e156d2ef45fad1781e804e0c40cc5111c865d4 Mon Sep 17 00:00:00 2001 From: Sebastian Roa <115311276+Roaring30s@users.noreply.github.com> Date: Mon, 11 Dec 2023 11:53:59 -0500 Subject: [PATCH 23/25] :hammer: Build convertTo Function for Smartweave --- crates/evm/lib.rs | 12 ++++-------- crates/exm/base.js | 23 ----------------------- crates/smartweave/smartweave.js | 15 +++++++++++++++ testdata/contracts/sha256.js | 2 +- 4 files changed, 20 insertions(+), 32 deletions(-) diff --git a/crates/evm/lib.rs b/crates/evm/lib.rs index 6cf97b2..13cddae 100644 --- a/crates/evm/lib.rs +++ b/crates/evm/lib.rs @@ -394,12 +394,8 @@ impl<'a> Machine<'a> { pc += 1; counter += 1; - - /* - Error taking place at 114 - PUSH32 is not being injected correctly - */ - if counter == 114 { + + if counter == 400 { break; } @@ -1409,8 +1405,8 @@ mod tests { */ #[test] fn test_erc_constructor() { - //let bytes = hex!("608060405234801562000010575f80fd5b506040516200166f3803806200166f8339818101604052810190620000369190620003cb565b6040518060400160405280600481526020017f476f6c64000000000000000000000000000000000000000000000000000000008152506040518060400160405280600381526020017f474c4400000000000000000000000000000000000000000000000000000000008152508160039081620000b3919062000656565b508060049081620000c5919062000656565b505050620000da3382620000e160201b60201c565b5062000866565b5f73ffffffffffffffffffffffffffffffffffffffff168273ffffffffffffffffffffffffffffffffffffffff160362000154575f6040517fec442f050000000000000000000000000000000000000000000000000000000081526004016200014b91906200077d565b60405180910390fd5b620001675f83836200016b60201b60201c565b5050565b5f73ffffffffffffffffffffffffffffffffffffffff168373ffffffffffffffffffffffffffffffffffffffff1603620001bf578060025f828254620001b29190620007c5565b9250508190555062000290565b5f805f8573ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020019081526020015f20549050818110156200024b578381836040517fe450d38c000000000000000000000000000000000000000000000000000000008152600401620002429392919062000810565b60405180910390fd5b8181035f808673ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020019081526020015f2081905550505b5f73ffffffffffffffffffffffffffffffffffffffff168273ffffffffffffffffffffffffffffffffffffffff1603620002d9578060025f828254039250508190555062000323565b805f808473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020019081526020015f205f82825401925050819055505b8173ffffffffffffffffffffffffffffffffffffffff168373ffffffffffffffffffffffffffffffffffffffff167fddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef836040516200038291906200084b565b60405180910390a3505050565b5f80fd5b5f819050919050565b620003a78162000393565b8114620003b2575f80fd5b50565b5f81519050620003c5816200039c565b92915050565b5f60208284031215620003e357620003e26200038f565b5b5f620003f284828501620003b5565b91505092915050565b5f81519050919050565b7f4e487b71000000000000000000000000000000000000000000000000000000005f52604160045260245ffd5b7f4e487b71000000000000000000000000000000000000000000000000000000005f52602260045260245ffd5b5f60028204905060018216806200047757607f821691505b6020821081036200048d576200048c62000432565b5b50919050565b5f819050815f5260205f209050919050565b5f6020601f8301049050919050565b5f82821b905092915050565b5f60088302620004f17fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff82620004b4565b620004fd8683620004b4565b95508019841693508086168417925050509392505050565b5f819050919050565b5f6200053e62000538620005328462000393565b62000515565b62000393565b9050919050565b5f819050919050565b62000559836200051e565b62000571620005688262000545565b848454620004c0565b825550505050565b5f90565b6200058762000579565b620005948184846200054e565b505050565b5b81811015620005bb57620005af5f826200057d565b6001810190506200059a565b5050565b601f8211156200060a57620005d48162000493565b620005df84620004a5565b81016020851015620005ef578190505b62000607620005fe85620004a5565b83018262000599565b50505b505050565b5f82821c905092915050565b5f6200062c5f19846008026200060f565b1980831691505092915050565b5f6200064683836200061b565b9150826002028217905092915050565b6200066182620003fb565b67ffffffffffffffff8111156200067d576200067c62000405565b5b6200068982546200045f565b62000696828285620005bf565b5f60209050601f831160018114620006cc575f8415620006b7578287015190505b620006c3858262000639565b86555062000732565b601f198416620006dc8662000493565b5f5b828110156200070557848901518255600182019150602085019450602081019050620006de565b8683101562000725578489015162000721601f8916826200061b565b8355505b6001600288020188555050505b505050505050565b5f73ffffffffffffffffffffffffffffffffffffffff82169050919050565b5f62000765826200073a565b9050919050565b620007778162000759565b82525050565b5f602082019050620007925f8301846200076c565b92915050565b7f4e487b71000000000000000000000000000000000000000000000000000000005f52601160045260245ffd5b5f620007d18262000393565b9150620007de8362000393565b9250828201905080821115620007f957620007f862000798565b5b92915050565b6200080a8162000393565b82525050565b5f606082019050620008255f8301866200076c565b620008346020830185620007ff565b620008436040830184620007ff565b949350505050565b5f602082019050620008605f830184620007ff565b92915050565b610dfb80620008745f395ff3fe608060405234801561000f575f80fd5b5060043610610091575f3560e01c8063313ce56711610064578063313ce5671461013157806370a082311461014f57806395d89b411461017f578063a9059cbb1461019d578063dd62ed3e146101cd57610091565b806306fdde0314610095578063095ea7b3146100b357806318160ddd146100e357806323b872dd14610101575b5f80fd5b61009d6101fd565b6040516100aa9190610a74565b60405180910390f35b6100cd60048036038101906100c89190610b25565b61028d565b6040516100da9190610b7d565b60405180910390f35b6100eb6102af565b6040516100f89190610ba5565b60405180910390f35b61011b60048036038101906101169190610bbe565b6102b8565b6040516101289190610b7d565b60405180910390f35b6101396102e6565b6040516101469190610c29565b60405180910390f35b61016960048036038101906101649190610c42565b6102ee565b6040516101769190610ba5565b60405180910390f35b610187610333565b6040516101949190610a74565b60405180910390f35b6101b760048036038101906101b29190610b25565b6103c3565b6040516101c49190610b7d565b60405180910390f35b6101e760048036038101906101e29190610c6d565b6103e5565b6040516101f49190610ba5565b60405180910390f35b60606003805461020c90610cd8565b80601f016020809104026020016040519081016040528092919081815260200182805461023890610cd8565b80156102835780601f1061025a57610100808354040283529160200191610283565b820191905f5260205f20905b81548152906001019060200180831161026657829003601f168201915b5050505050905090565b5f80610297610467565b90506102a481858561046e565b600191505092915050565b5f600254905090565b5f806102c2610467565b90506102cf858285610480565b6102da858585610512565b60019150509392505050565b5f6012905090565b5f805f8373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020019081526020015f20549050919050565b60606004805461034290610cd8565b80601f016020809104026020016040519081016040528092919081815260200182805461036e90610cd8565b80156103b95780601f10610390576101008083540402835291602001916103b9565b820191905f5260205f20905b81548152906001019060200180831161039c57829003601f168201915b5050505050905090565b5f806103cd610467565b90506103da818585610512565b600191505092915050565b5f60015f8473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020019081526020015f205f8373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020019081526020015f2054905092915050565b5f33905090565b61047b8383836001610602565b505050565b5f61048b84846103e5565b90507fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff811461050c57818110156104fd578281836040517ffb8f41b20000000000000000000000000000000000000000000000000000000081526004016104f493929190610d17565b60405180910390fd5b61050b84848484035f610602565b5b50505050565b5f73ffffffffffffffffffffffffffffffffffffffff168373ffffffffffffffffffffffffffffffffffffffff1603610582575f6040517f96c6fd1e0000000000000000000000000000000000000000000000000000000081526004016105799190610d4c565b60405180910390fd5b5f73ffffffffffffffffffffffffffffffffffffffff168273ffffffffffffffffffffffffffffffffffffffff16036105f2575f6040517fec442f050000000000000000000000000000000000000000000000000000000081526004016105e99190610d4c565b60405180910390fd5b6105fd8383836107d1565b505050565b5f73ffffffffffffffffffffffffffffffffffffffff168473ffffffffffffffffffffffffffffffffffffffff1603610672575f6040517fe602df050000000000000000000000000000000000000000000000000000000081526004016106699190610d4c565b60405180910390fd5b5f73ffffffffffffffffffffffffffffffffffffffff168373ffffffffffffffffffffffffffffffffffffffff16036106e2575f6040517f94280d620000000000000000000000000000000000000000000000000000000081526004016106d99190610d4c565b60405180910390fd5b8160015f8673ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020019081526020015f205f8573ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020019081526020015f208190555080156107cb578273ffffffffffffffffffffffffffffffffffffffff168473ffffffffffffffffffffffffffffffffffffffff167f8c5be1e5ebec7d5bd14f71427d1e84f3dd0314c0f7b2291e5b200ac8c7c3b925846040516107c29190610ba5565b60405180910390a35b50505050565b5f73ffffffffffffffffffffffffffffffffffffffff168373ffffffffffffffffffffffffffffffffffffffff1603610821578060025f8282546108159190610d92565b925050819055506108ef565b5f805f8573ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020019081526020015f20549050818110156108aa578381836040517fe450d38c0000000000000000000000000000000000000000000000000000000081526004016108a193929190610d17565b60405180910390fd5b8181035f808673ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020019081526020015f2081905550505b5f73ffffffffffffffffffffffffffffffffffffffff168273ffffffffffffffffffffffffffffffffffffffff1603610936578060025f8282540392505081905550610980565b805f808473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020019081526020015f205f82825401925050819055505b8173ffffffffffffffffffffffffffffffffffffffff168373ffffffffffffffffffffffffffffffffffffffff167fddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef836040516109dd9190610ba5565b60405180910390a3505050565b5f81519050919050565b5f82825260208201905092915050565b5f5b83811015610a21578082015181840152602081019050610a06565b5f8484015250505050565b5f601f19601f8301169050919050565b5f610a46826109ea565b610a5081856109f4565b9350610a60818560208601610a04565b610a6981610a2c565b840191505092915050565b5f6020820190508181035f830152610a8c8184610a3c565b905092915050565b5f80fd5b5f73ffffffffffffffffffffffffffffffffffffffff82169050919050565b5f610ac182610a98565b9050919050565b610ad181610ab7565b8114610adb575f80fd5b50565b5f81359050610aec81610ac8565b92915050565b5f819050919050565b610b0481610af2565b8114610b0e575f80fd5b50565b5f81359050610b1f81610afb565b92915050565b5f8060408385031215610b3b57610b3a610a94565b5b5f610b4885828601610ade565b9250506020610b5985828601610b11565b9150509250929050565b5f8115159050919050565b610b7781610b63565b82525050565b5f602082019050610b905f830184610b6e565b92915050565b610b9f81610af2565b82525050565b5f602082019050610bb85f830184610b96565b92915050565b5f805f60608486031215610bd557610bd4610a94565b5b5f610be286828701610ade565b9350506020610bf386828701610ade565b9250506040610c0486828701610b11565b9150509250925092565b5f60ff82169050919050565b610c2381610c0e565b82525050565b5f602082019050610c3c5f830184610c1a565b92915050565b5f60208284031215610c5757610c56610a94565b5b5f610c6484828501610ade565b91505092915050565b5f8060408385031215610c8357610c82610a94565b5b5f610c9085828601610ade565b9250506020610ca185828601610ade565b9150509250929050565b7f4e487b71000000000000000000000000000000000000000000000000000000005f52602260045260245ffd5b5f6002820490506001821680610cef57607f821691505b602082108103610d0257610d01610cab565b5b50919050565b610d1181610ab7565b82525050565b5f606082019050610d2a5f830186610d08565b610d376020830185610b96565b610d446040830184610b96565b949350505050565b5f602082019050610d5f5f830184610d08565b92915050565b7f4e487b71000000000000000000000000000000000000000000000000000000005f52601160045260245ffd5b5f610d9c82610af2565b9150610da783610af2565b9250828201905080821115610dbf57610dbe610d65565b5b9291505056fea26469706673582212209e44fcf9f1633c4ef31f016a68dcf977b7606fd376618fb0b59f05773c82a5f864736f6c63430008160033000000000000000000000000000000000000000000000000000000000000007b"); - let bytes = hex!("73ffffffffffffffffffffffffffffffffffffffff"); + let bytes = hex!("608060405234801562000010575f80fd5b506040516200166f3803806200166f8339818101604052810190620000369190620003cb565b6040518060400160405280600481526020017f476f6c64000000000000000000000000000000000000000000000000000000008152506040518060400160405280600381526020017f474c4400000000000000000000000000000000000000000000000000000000008152508160039081620000b3919062000656565b508060049081620000c5919062000656565b505050620000da3382620000e160201b60201c565b5062000866565b5f73ffffffffffffffffffffffffffffffffffffffff168273ffffffffffffffffffffffffffffffffffffffff160362000154575f6040517fec442f050000000000000000000000000000000000000000000000000000000081526004016200014b91906200077d565b60405180910390fd5b620001675f83836200016b60201b60201c565b5050565b5f73ffffffffffffffffffffffffffffffffffffffff168373ffffffffffffffffffffffffffffffffffffffff1603620001bf578060025f828254620001b29190620007c5565b9250508190555062000290565b5f805f8573ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020019081526020015f20549050818110156200024b578381836040517fe450d38c000000000000000000000000000000000000000000000000000000008152600401620002429392919062000810565b60405180910390fd5b8181035f808673ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020019081526020015f2081905550505b5f73ffffffffffffffffffffffffffffffffffffffff168273ffffffffffffffffffffffffffffffffffffffff1603620002d9578060025f828254039250508190555062000323565b805f808473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020019081526020015f205f82825401925050819055505b8173ffffffffffffffffffffffffffffffffffffffff168373ffffffffffffffffffffffffffffffffffffffff167fddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef836040516200038291906200084b565b60405180910390a3505050565b5f80fd5b5f819050919050565b620003a78162000393565b8114620003b2575f80fd5b50565b5f81519050620003c5816200039c565b92915050565b5f60208284031215620003e357620003e26200038f565b5b5f620003f284828501620003b5565b91505092915050565b5f81519050919050565b7f4e487b71000000000000000000000000000000000000000000000000000000005f52604160045260245ffd5b7f4e487b71000000000000000000000000000000000000000000000000000000005f52602260045260245ffd5b5f60028204905060018216806200047757607f821691505b6020821081036200048d576200048c62000432565b5b50919050565b5f819050815f5260205f209050919050565b5f6020601f8301049050919050565b5f82821b905092915050565b5f60088302620004f17fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff82620004b4565b620004fd8683620004b4565b95508019841693508086168417925050509392505050565b5f819050919050565b5f6200053e62000538620005328462000393565b62000515565b62000393565b9050919050565b5f819050919050565b62000559836200051e565b62000571620005688262000545565b848454620004c0565b825550505050565b5f90565b6200058762000579565b620005948184846200054e565b505050565b5b81811015620005bb57620005af5f826200057d565b6001810190506200059a565b5050565b601f8211156200060a57620005d48162000493565b620005df84620004a5565b81016020851015620005ef578190505b62000607620005fe85620004a5565b83018262000599565b50505b505050565b5f82821c905092915050565b5f6200062c5f19846008026200060f565b1980831691505092915050565b5f6200064683836200061b565b9150826002028217905092915050565b6200066182620003fb565b67ffffffffffffffff8111156200067d576200067c62000405565b5b6200068982546200045f565b62000696828285620005bf565b5f60209050601f831160018114620006cc575f8415620006b7578287015190505b620006c3858262000639565b86555062000732565b601f198416620006dc8662000493565b5f5b828110156200070557848901518255600182019150602085019450602081019050620006de565b8683101562000725578489015162000721601f8916826200061b565b8355505b6001600288020188555050505b505050505050565b5f73ffffffffffffffffffffffffffffffffffffffff82169050919050565b5f62000765826200073a565b9050919050565b620007778162000759565b82525050565b5f602082019050620007925f8301846200076c565b92915050565b7f4e487b71000000000000000000000000000000000000000000000000000000005f52601160045260245ffd5b5f620007d18262000393565b9150620007de8362000393565b9250828201905080821115620007f957620007f862000798565b5b92915050565b6200080a8162000393565b82525050565b5f606082019050620008255f8301866200076c565b620008346020830185620007ff565b620008436040830184620007ff565b949350505050565b5f602082019050620008605f830184620007ff565b92915050565b610dfb80620008745f395ff3fe608060405234801561000f575f80fd5b5060043610610091575f3560e01c8063313ce56711610064578063313ce5671461013157806370a082311461014f57806395d89b411461017f578063a9059cbb1461019d578063dd62ed3e146101cd57610091565b806306fdde0314610095578063095ea7b3146100b357806318160ddd146100e357806323b872dd14610101575b5f80fd5b61009d6101fd565b6040516100aa9190610a74565b60405180910390f35b6100cd60048036038101906100c89190610b25565b61028d565b6040516100da9190610b7d565b60405180910390f35b6100eb6102af565b6040516100f89190610ba5565b60405180910390f35b61011b60048036038101906101169190610bbe565b6102b8565b6040516101289190610b7d565b60405180910390f35b6101396102e6565b6040516101469190610c29565b60405180910390f35b61016960048036038101906101649190610c42565b6102ee565b6040516101769190610ba5565b60405180910390f35b610187610333565b6040516101949190610a74565b60405180910390f35b6101b760048036038101906101b29190610b25565b6103c3565b6040516101c49190610b7d565b60405180910390f35b6101e760048036038101906101e29190610c6d565b6103e5565b6040516101f49190610ba5565b60405180910390f35b60606003805461020c90610cd8565b80601f016020809104026020016040519081016040528092919081815260200182805461023890610cd8565b80156102835780601f1061025a57610100808354040283529160200191610283565b820191905f5260205f20905b81548152906001019060200180831161026657829003601f168201915b5050505050905090565b5f80610297610467565b90506102a481858561046e565b600191505092915050565b5f600254905090565b5f806102c2610467565b90506102cf858285610480565b6102da858585610512565b60019150509392505050565b5f6012905090565b5f805f8373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020019081526020015f20549050919050565b60606004805461034290610cd8565b80601f016020809104026020016040519081016040528092919081815260200182805461036e90610cd8565b80156103b95780601f10610390576101008083540402835291602001916103b9565b820191905f5260205f20905b81548152906001019060200180831161039c57829003601f168201915b5050505050905090565b5f806103cd610467565b90506103da818585610512565b600191505092915050565b5f60015f8473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020019081526020015f205f8373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020019081526020015f2054905092915050565b5f33905090565b61047b8383836001610602565b505050565b5f61048b84846103e5565b90507fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff811461050c57818110156104fd578281836040517ffb8f41b20000000000000000000000000000000000000000000000000000000081526004016104f493929190610d17565b60405180910390fd5b61050b84848484035f610602565b5b50505050565b5f73ffffffffffffffffffffffffffffffffffffffff168373ffffffffffffffffffffffffffffffffffffffff1603610582575f6040517f96c6fd1e0000000000000000000000000000000000000000000000000000000081526004016105799190610d4c565b60405180910390fd5b5f73ffffffffffffffffffffffffffffffffffffffff168273ffffffffffffffffffffffffffffffffffffffff16036105f2575f6040517fec442f050000000000000000000000000000000000000000000000000000000081526004016105e99190610d4c565b60405180910390fd5b6105fd8383836107d1565b505050565b5f73ffffffffffffffffffffffffffffffffffffffff168473ffffffffffffffffffffffffffffffffffffffff1603610672575f6040517fe602df050000000000000000000000000000000000000000000000000000000081526004016106699190610d4c565b60405180910390fd5b5f73ffffffffffffffffffffffffffffffffffffffff168373ffffffffffffffffffffffffffffffffffffffff16036106e2575f6040517f94280d620000000000000000000000000000000000000000000000000000000081526004016106d99190610d4c565b60405180910390fd5b8160015f8673ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020019081526020015f205f8573ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020019081526020015f208190555080156107cb578273ffffffffffffffffffffffffffffffffffffffff168473ffffffffffffffffffffffffffffffffffffffff167f8c5be1e5ebec7d5bd14f71427d1e84f3dd0314c0f7b2291e5b200ac8c7c3b925846040516107c29190610ba5565b60405180910390a35b50505050565b5f73ffffffffffffffffffffffffffffffffffffffff168373ffffffffffffffffffffffffffffffffffffffff1603610821578060025f8282546108159190610d92565b925050819055506108ef565b5f805f8573ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020019081526020015f20549050818110156108aa578381836040517fe450d38c0000000000000000000000000000000000000000000000000000000081526004016108a193929190610d17565b60405180910390fd5b8181035f808673ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020019081526020015f2081905550505b5f73ffffffffffffffffffffffffffffffffffffffff168273ffffffffffffffffffffffffffffffffffffffff1603610936578060025f8282540392505081905550610980565b805f808473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020019081526020015f205f82825401925050819055505b8173ffffffffffffffffffffffffffffffffffffffff168373ffffffffffffffffffffffffffffffffffffffff167fddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef836040516109dd9190610ba5565b60405180910390a3505050565b5f81519050919050565b5f82825260208201905092915050565b5f5b83811015610a21578082015181840152602081019050610a06565b5f8484015250505050565b5f601f19601f8301169050919050565b5f610a46826109ea565b610a5081856109f4565b9350610a60818560208601610a04565b610a6981610a2c565b840191505092915050565b5f6020820190508181035f830152610a8c8184610a3c565b905092915050565b5f80fd5b5f73ffffffffffffffffffffffffffffffffffffffff82169050919050565b5f610ac182610a98565b9050919050565b610ad181610ab7565b8114610adb575f80fd5b50565b5f81359050610aec81610ac8565b92915050565b5f819050919050565b610b0481610af2565b8114610b0e575f80fd5b50565b5f81359050610b1f81610afb565b92915050565b5f8060408385031215610b3b57610b3a610a94565b5b5f610b4885828601610ade565b9250506020610b5985828601610b11565b9150509250929050565b5f8115159050919050565b610b7781610b63565b82525050565b5f602082019050610b905f830184610b6e565b92915050565b610b9f81610af2565b82525050565b5f602082019050610bb85f830184610b96565b92915050565b5f805f60608486031215610bd557610bd4610a94565b5b5f610be286828701610ade565b9350506020610bf386828701610ade565b9250506040610c0486828701610b11565b9150509250925092565b5f60ff82169050919050565b610c2381610c0e565b82525050565b5f602082019050610c3c5f830184610c1a565b92915050565b5f60208284031215610c5757610c56610a94565b5b5f610c6484828501610ade565b91505092915050565b5f8060408385031215610c8357610c82610a94565b5b5f610c9085828601610ade565b9250506020610ca185828601610ade565b9150509250929050565b7f4e487b71000000000000000000000000000000000000000000000000000000005f52602260045260245ffd5b5f6002820490506001821680610cef57607f821691505b602082108103610d0257610d01610cab565b5b50919050565b610d1181610ab7565b82525050565b5f606082019050610d2a5f830186610d08565b610d376020830185610b96565b610d446040830184610b96565b949350505050565b5f602082019050610d5f5f830184610d08565b92915050565b7f4e487b71000000000000000000000000000000000000000000000000000000005f52601160045260245ffd5b5f610d9c82610af2565b9150610da783610af2565b9250828201905080821115610dbf57610dbe610d65565b5b9291505056fea26469706673582212209e44fcf9f1633c4ef31f016a68dcf977b7606fd376618fb0b59f05773c82a5f864736f6c63430008160033000000000000000000000000000000000000000000000000000000000000007b"); + //let bytes = hex!("7f476f6c6400000000000000000000000000000000000000000000000000000000"); let mut machine = Machine::new(test_cost_fn); let status = machine.execute(&bytes, Default::default()); //assert_eq!(status, ExecutionState::Ok); diff --git a/crates/exm/base.js b/crates/exm/base.js index 43c3211..9abc12a 100644 --- a/crates/exm/base.js +++ b/crates/exm/base.js @@ -238,29 +238,6 @@ testDelKv() { return this.kv; } - - async sha256(buffer) { - return subtle.digest('SHA-256', buffer).then((hashBuffer) => { - const hashArray = Array.from(new Uint8Array(hashBuffer)); - const hashHex = hashArray - .map((bytes) => bytes.toString(16).padStart(2, '0')) - .join(''); - return hashHex; - }); - } - - async stringToSHA256(input) { - const encoder = new TextEncoder(); - const data = encoder.encode(input); - - const hashBuffer = await crypto.subtle.digest('SHA-256', data); - - const hashArray = Array.from(new Uint8Array(hashBuffer)); - const hashHex = hashArray.map(byte => byte.toString(16).padStart(2, '0')).join(''); - - return hashHex; - } - } const ExmSymbol = Symbol('exm'); diff --git a/crates/smartweave/smartweave.js b/crates/smartweave/smartweave.js index 1e3870a..1739097 100644 --- a/crates/smartweave/smartweave.js +++ b/crates/smartweave/smartweave.js @@ -367,6 +367,21 @@ }; } + get convertString() { + return { + async toSHA256(input) { + const encoder = new TextEncoder(); + const data = encoder.encode(input); + const hashBuffer = await SmartWeave.arweave.crypto.hash(data); + const hashArray = Array.from(hashBuffer); + const hashHex = hashArray.map(byte => byte.toString(16).padStart(2, '0')).join(''); + + return hashHex; + + } + } + } + get unsafeClient() { const txGetData = async (txId, opts) => { try { diff --git a/testdata/contracts/sha256.js b/testdata/contracts/sha256.js index 21a2ea1..a252277 100644 --- a/testdata/contracts/sha256.js +++ b/testdata/contracts/sha256.js @@ -1,6 +1,6 @@ export async function handle(state, action) { try { - const someSha = await EXM.stringToSHA256("hello"); + const someSha = await SmartWeave.convertString.toSHA256("hello"); return { state: [someSha] }; } catch(e) { return { state: e.toString() } From 8dde2125ddbcda0f25a3bb47553074c56b1f2885 Mon Sep 17 00:00:00 2001 From: Sebastian Roa <115311276+Roaring30s@users.noreply.github.com> Date: Mon, 11 Dec 2023 12:29:21 -0500 Subject: [PATCH 24/25] :mag: Inspect CI Test --- crates/exm/base.js | 9 +++++++++ crates/smartweave/smartweave.js | 4 ++-- js/napi/src/lib.rs | 5 ++--- 3 files changed, 13 insertions(+), 5 deletions(-) diff --git a/crates/exm/base.js b/crates/exm/base.js index 9abc12a..9a75cc4 100644 --- a/crates/exm/base.js +++ b/crates/exm/base.js @@ -238,6 +238,15 @@ testDelKv() { return this.kv; } + async sha256(buffer) { + return subtle.digest('SHA-256', buffer).then((hashBuffer) => { + const hashArray = Array.from(new Uint8Array(hashBuffer)); + const hashHex = hashArray + .map((bytes) => bytes.toString(16).padStart(2, '0')) + .join(''); + return hashHex; + }); + } } const ExmSymbol = Symbol('exm'); diff --git a/crates/smartweave/smartweave.js b/crates/smartweave/smartweave.js index 1739097..6151d41 100644 --- a/crates/smartweave/smartweave.js +++ b/crates/smartweave/smartweave.js @@ -372,12 +372,12 @@ async toSHA256(input) { const encoder = new TextEncoder(); const data = encoder.encode(input); - const hashBuffer = await SmartWeave.arweave.crypto.hash(data); + const hashBuffer = await globalThis.SmartWeave.arweave.crypto.hash(data); const hashArray = Array.from(hashBuffer); const hashHex = hashArray.map(byte => byte.toString(16).padStart(2, '0')).join(''); return hashHex; - + } } } diff --git a/js/napi/src/lib.rs b/js/napi/src/lib.rs index ecbfe38..822ff84 100644 --- a/js/napi/src/lib.rs +++ b/js/napi/src/lib.rs @@ -554,6 +554,7 @@ mod tests { let contract_result = contract.state; let str_state = contract_result.to_string(); + //println!("STATUS::::: {:#?}", str_state.contains("wearemintingyes")); assert!(str_state.contains("wearemintingyes")); } @@ -791,9 +792,7 @@ mod tests { let contract = simulate_contract(execution_context).await.unwrap(); const hello_hash: &str = "2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824"; - println!("{:#?}", contract.state); - println!("{:#?}", contract.state[0]); - //assert_eq!(contract.state[0], hello_hash); + assert_eq!(contract.state[0], hello_hash); } #[tokio::test] From d87fa0d3cd7f6c92633de824bda873e2f3f22425 Mon Sep 17 00:00:00 2001 From: Sebastian Roa <115311276+Roaring30s@users.noreply.github.com> Date: Mon, 11 Dec 2023 13:37:56 -0500 Subject: [PATCH 25/25] :hammer: Run Cargo FMT --- crates/evm/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/evm/lib.rs b/crates/evm/lib.rs index 13cddae..c50c0c7 100644 --- a/crates/evm/lib.rs +++ b/crates/evm/lib.rs @@ -394,7 +394,7 @@ impl<'a> Machine<'a> { pc += 1; counter += 1; - + if counter == 400 { break; }