From 7522ee5ba23f39884649af8d39d3cc20d58a6d47 Mon Sep 17 00:00:00 2001 From: supreme2580 Date: Tue, 17 Sep 2024 15:51:02 +0100 Subject: [PATCH] disabled opcode implementation --- src/script/engine.zig | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/src/script/engine.zig b/src/script/engine.zig index b0a5afd..4b73401 100644 --- a/src/script/engine.zig +++ b/src/script/engine.zig @@ -16,6 +16,8 @@ pub const EngineError = error{ EarlyReturn, /// Encountered an unknown opcode UnknownOpcode, + /// Encountered a disabled opcode + DisabledOpcode, } || StackError; /// Engine is the virtual machine that executes Bitcoin scripts @@ -119,6 +121,8 @@ pub const Engine = struct { 0x74 => self.opDepth(), 0x75 => try self.opDrop(), 0x76 => try self.opDup(), + 0x7e...0x86 => try self.opDisabled(), + 0x8d...0x8e => try self.opDisabled(), 0x87 => try self.opEqual(), 0x88 => try self.opEqualVerify(), 0x8b => try arithmetic.op1Add(self), @@ -129,6 +133,7 @@ pub const Engine = struct { 0x92 => try arithmetic.op0NotEqual(self), 0x93 => try arithmetic.opAdd(self), 0x94 => try arithmetic.opSub(self), + 0x95...0x99 => try self.opDisabled(), 0x9a => try arithmetic.opBoolAnd(self), 0x9b => try arithmetic.opBoolOr(self), 0x9c => try arithmetic.opNumEqual(self), @@ -407,6 +412,11 @@ pub const Engine = struct { // Assume signature is valid for now try self.stack.pushByteArray(&[_]u8{1}); } + + fn opDisabled(self: *Engine) !void { + std.debug.print("Attempt to execute disabled opcode: 0x{x:0>2}\n", .{self.script.data[self.pc]}); + return error.DisabledOpcode; + } }; test "Script execution - OP_1 OP_1 OP_EQUAL" { @@ -583,3 +593,17 @@ test "Script execution - OP_1 OP_2 OP_DROP" { // Ensure the stack is empty after popping the result try std.testing.expectEqualSlices(u8, &[_]u8{1}, element0); } + +test "Script execution - OP_DISABLED" { + const allocator = std.testing.allocator; + + // Simple script to run a disabled opcode + const script_bytes = [_]u8{0x95}; + const script = Script.init(&script_bytes); + + var engine = Engine.init(allocator, script, .{}); + defer engine.deinit(); + + // Expect an error when running a disabled opcode + try std.testing.expectError(error.DisabledOpcode, engine.opDisabled()); +}