Skip to content

Commit

Permalink
disabled opcode implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
supreme2580 committed Sep 17, 2024
1 parent fa5329f commit 7522ee5
Showing 1 changed file with 24 additions and 0 deletions.
24 changes: 24 additions & 0 deletions src/script/engine.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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),
Expand All @@ -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),
Expand Down Expand Up @@ -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" {
Expand Down Expand Up @@ -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());
}

0 comments on commit 7522ee5

Please sign in to comment.