From 23d9a243e6c8bbca4bd6425b7b0a7a74c54411d5 Mon Sep 17 00:00:00 2001 From: KhairallahA Date: Thu, 19 Sep 2024 23:21:38 +0300 Subject: [PATCH 1/2] OP_TOALTSTACK OP_FROMALTSTACK opcodes --- src/script/engine.zig | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/src/script/engine.zig b/src/script/engine.zig index be605e0..a176af4 100644 --- a/src/script/engine.zig +++ b/src/script/engine.zig @@ -108,6 +108,8 @@ pub const Engine = struct { Opcode.OP_NOP => try self.opNop(), Opcode.OP_VERIFY => try self.opVerify(), Opcode.OP_RETURN => try self.opReturn(), + Opcode.OP_TOALTSTACK => try self.opToAltStack(), + Opcode.OP_FROMALTSTACK => try self.opFromAltStack(), Opcode.OP_2DROP => try self.op2Drop(), Opcode.OP_2DUP => try self.op2Dup(), Opcode.OP_3DUP => try self.op3Dup(), @@ -265,6 +267,24 @@ pub const Engine = struct { return error.EarlyReturn; } + /// OP_TOALTSTACK: Puts the value onto the top of the alt stack, and removes it from the main stack. + /// + /// # Returns + /// - `EngineError`: If an error occurs during execution + fn opToAltStack(self: *Engine) !void { + const value = try self.stack.pop(); + try self.alt_stack.pushElement(value); + } + + /// OP_FROMALTSTACK: Puts the value onto the top of the main stack, and removes it from the alt stack. + /// + /// # Returns + /// - `EngineError`: If an error occurs during execution + fn opFromAltStack(self: *Engine) !void { + const value = try self.alt_stack.pop(); + try self.stack.pushElement(value); + } + /// OP_2DROP: Drops top 2 stack items /// /// # Returns @@ -524,6 +544,26 @@ test "Script execution - OP_RETURN" { } } +test "Script execution - OP_TOALTSTACK OP_FROMALTSTACK" { + const allocator = std.testing.allocator; + + // Simple script: OP_1 OP_TOALTSTACK OP_FROMALTSTACK + const script_bytes = [_]u8{ + Opcode.OP_1.toBytes(), + Opcode.OP_TOALTSTACK.toBytes(), + Opcode.OP_FROMALTSTACK.toBytes(), + }; + const script = Script.init(&script_bytes); + + var engine = Engine.init(allocator, script, .{}); + defer engine.deinit(); + + try engine.execute(); + + try std.testing.expectEqual(1, engine.stack.len()); + try std.testing.expectEqual(0, engine.alt_stack.len()); +} + test "Script execution - OP_1 OP_1 OP_1 OP_2Drop" { const allocator = std.testing.allocator; From 71cda2709f2a4fcb7fa0bb18f5541e1f2d4e841d Mon Sep 17 00:00:00 2001 From: KhairallahA Date: Fri, 20 Sep 2024 14:35:14 +0300 Subject: [PATCH 2/2] Update requests completed --- src/script/engine.zig | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/script/engine.zig b/src/script/engine.zig index a176af4..636724c 100644 --- a/src/script/engine.zig +++ b/src/script/engine.zig @@ -271,7 +271,7 @@ pub const Engine = struct { /// /// # Returns /// - `EngineError`: If an error occurs during execution - fn opToAltStack(self: *Engine) !void { + fn opToAltStack(self: *Engine) EngineError!void { const value = try self.stack.pop(); try self.alt_stack.pushElement(value); } @@ -280,7 +280,7 @@ pub const Engine = struct { /// /// # Returns /// - `EngineError`: If an error occurs during execution - fn opFromAltStack(self: *Engine) !void { + fn opFromAltStack(self: *Engine) EngineError!void { const value = try self.alt_stack.pop(); try self.stack.pushElement(value); } @@ -550,6 +550,8 @@ test "Script execution - OP_TOALTSTACK OP_FROMALTSTACK" { // Simple script: OP_1 OP_TOALTSTACK OP_FROMALTSTACK const script_bytes = [_]u8{ Opcode.OP_1.toBytes(), + Opcode.OP_2.toBytes(), + Opcode.OP_TOALTSTACK.toBytes(), Opcode.OP_TOALTSTACK.toBytes(), Opcode.OP_FROMALTSTACK.toBytes(), }; @@ -560,8 +562,8 @@ test "Script execution - OP_TOALTSTACK OP_FROMALTSTACK" { try engine.execute(); - try std.testing.expectEqual(1, engine.stack.len()); - try std.testing.expectEqual(0, engine.alt_stack.len()); + try std.testing.expectEqual(1, try engine.stack.peekInt(0)); + try std.testing.expectEqual(2, try engine.alt_stack.peekInt(0)); } test "Script execution - OP_1 OP_1 OP_1 OP_2Drop" {