Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

OP_ROLL #104

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions src/script/engine.zig
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ pub const Engine = struct {
Opcode.OP_CHECKSIG => try self.opCheckSig(),
Opcode.OP_NIP => try self.opNip(),
Opcode.OP_OVER => try self.opOver(),
Opcode.OP_ROLL => try self.opRoll(),
Opcode.OP_SWAP => try self.opSwap(),
Opcode.OP_TUCK => try self.opTuck(),
Opcode.OP_SIZE => try self.opSize(),
Expand Down Expand Up @@ -379,6 +380,16 @@ pub const Engine = struct {
try self.stack.pushByteArray(value);
}

/// OP_ROLL: Pop the top stack element as N. Move the Nth stack element to the top.
///
/// # Returns
/// - `EngineError`: If an error occurs during execution
fn opRoll(self: *Engine) !void {
const n = try self.stack.popInt();
const value = try self.stack.removeAt(@intCast(n));
try self.stack.pushElement(value);
}

/// OP_SWAP: The top two items on the stack are swapped.
///
/// /// # Returns
Expand Down Expand Up @@ -719,6 +730,28 @@ test "Script execution OP_1 OP_2 OP_3 OP_OVER" {
try std.testing.expectEqual(3, element1);
}

test "Script execution OP_1 OP_2 OP_3 OP_2 OP_ROLL" {
const allocator = std.testing.allocator;

// Simple script: OP_1 OP_2 OP_3 OP_2 OP_ROLL
const script_bytes = [_]u8{ Opcode.OP_1.toBytes(), Opcode.OP_2.toBytes(), Opcode.OP_3.toBytes(), Opcode.OP_2.toBytes(), Opcode.OP_ROLL.toBytes() };
const script = Script.init(&script_bytes);

var engine = Engine.init(allocator, script, .{});
defer engine.deinit();

try engine.execute();
try std.testing.expectEqual(3, engine.stack.len());

const element0 = try engine.stack.peekInt(0);
const element1 = try engine.stack.peekInt(1);
const element2 = try engine.stack.peekInt(2);

try std.testing.expectEqual(1, element0);
try std.testing.expectEqual(3, element1);
try std.testing.expectEqual(2, element2);
}

test "Script execution OP_1 OP_2 OP_3 OP_SWAP" {
const allocator = std.testing.allocator;

Expand Down
19 changes: 19 additions & 0 deletions src/script/stack.zig
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,25 @@ pub const Stack = struct {
return std.mem.readVarInt(i64, elem, native_endian);
}

/// Removes an item from the stack at the specified index
///
/// # Arguments
/// - `index`: The index of the item to remove (0 is the top of the stack)
///
/// # Returns
/// - `StackError` if the index is out of bounds
/// - `[]u8`: The removed item
pub fn removeAt(self: *Stack, index: usize) StackError![]u8 {
oxlime marked this conversation as resolved.
Show resolved Hide resolved
if (index >= self.items.items.len) {
return StackError.StackUnderflow;
}

const actualIndex = self.items.items.len - 1 - index;

// Use orderedRemove to get the item
return self.items.orderedRemove(actualIndex);
}

/// Get the number of items in the stack
///
/// # Returns
Expand Down