Skip to content

Commit

Permalink
OP_TOALTSTACK OP_FROMALTSTACK opcodes
Browse files Browse the repository at this point in the history
  • Loading branch information
KhairallahA committed Sep 19, 2024
1 parent 69798f2 commit 23d9a24
Showing 1 changed file with 40 additions and 0 deletions.
40 changes: 40 additions & 0 deletions src/script/engine.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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;

Expand Down

0 comments on commit 23d9a24

Please sign in to comment.