Skip to content

Commit

Permalink
decoder: update decoder on superchain client
Browse files Browse the repository at this point in the history
  • Loading branch information
Raiden1411 committed Aug 6, 2024
1 parent cce9c6d commit 231c77f
Show file tree
Hide file tree
Showing 2 changed files with 112 additions and 11 deletions.
120 changes: 112 additions & 8 deletions src/clients/optimism/clients/L1PubClient.zig
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ pub fn L1Client(comptime client_type: Clients) type {
defer data.deinit();

const decoded = try decoder.decodeAbiParameter(struct { outputRoot: Hash, timestamp: u128, l2BlockNumber: u128 }, self.allocator, data.response, .{});
defer decoded.denit();
defer decoded.deinit();

const l2_output = decoded.result;

Expand Down Expand Up @@ -311,16 +311,12 @@ pub fn L1Client(comptime client_type: Clients) type {
} }, .{});
defer data.deinit();

const proven = try decoder.decodeAbiParameterLeaky(struct { outputRoot: Hash, timestamp: u128, l2OutputIndex: u128 }, self.allocator, data.response, .{});
const proven = try decoder.decodeAbiParameterLeaky(ProvenWithdrawal, self.allocator, data.response, .{});

if (proven.timestamp == 0)
return error.InvalidWithdrawalHash;

return .{
.outputRoot = proven.outputRoot,
.timestamp = proven.timestamp,
.l2OutputIndex = proven.l2OutputIndex,
};
return proven;
}
/// Gets the amount of time to wait in ms until the next output is posted.
///
Expand Down Expand Up @@ -486,7 +482,7 @@ pub fn L1Client(comptime client_type: Clients) type {
.to = decoded_logs[2],
.version = decoded_logs[3],
// Needs to be duped because the arena owns this memory.
.opaqueData = try self.allocator.dupe(u8, decoded[0]),
.opaqueData = try self.allocator.dupe(u8, decoded.result),
.logIndex = log_event.logIndex.?,
.blockHash = log_event.blockHash.?,
});
Expand Down Expand Up @@ -601,3 +597,111 @@ pub fn L1Client(comptime client_type: Clients) type {
}
};
}

test "GetL2HashFromL1DepositInfo" {
const uri = try std.Uri.parse("http://localhost:8545/");

var op: L1Client(.http) = undefined;
defer op.deinit();

try op.init(.{ .uri = uri, .allocator = testing.allocator }, null);

const messages = try op.getL2HashesForDepositTransaction(try utils.hashToBytes("0x33faeeee9c6d5e19edcdfc003f329c6652f05502ffbf3218d9093b92589a42c4"));
defer testing.allocator.free(messages);

try testing.expectEqualSlices(u8, &try utils.hashToBytes("0xed88afbd3f126180bd5488c2212cd033c51a6f9b1765249bdb738dcac1d0cb41"), &messages[0]);
}

test "GetL2Output" {
const uri = try std.Uri.parse("http://localhost:8545/");

var op: L1Client(.http) = undefined;
defer op.deinit();

try op.init(.{ .uri = uri, .allocator = testing.allocator }, null);

const l2_output = try op.getL2Output(2725977);

try testing.expectEqual(l2_output.timestamp, 1686075935);
try testing.expectEqual(l2_output.outputIndex, 0);
try testing.expectEqual(l2_output.l2BlockNumber, 105236863);
}

test "getSecondsToFinalize" {
const uri = try std.Uri.parse("http://localhost:8545/");

var op: L1Client(.http) = undefined;
defer op.deinit();

try op.init(.{ .uri = uri, .allocator = testing.allocator }, .{ .portalAddress = try utils.addressToBytes("0x49048044D57e1C92A77f79988d21Fa8fAF74E97e") });

const seconds = try op.getSecondsToFinalize(try utils.hashToBytes("0xEC0AD491512F4EDC603C2DD7B9371A0B18D4889A23E74692101BA4C6DC9B5709"));
try testing.expectEqual(seconds, 0);
}

test "GetSecondsToNextL2Output" {
const uri = try std.Uri.parse("http://localhost:8545/");

var op: L1Client(.http) = undefined;
defer op.deinit();

try op.init(.{ .uri = uri, .allocator = testing.allocator }, null);

const block = try op.getLatestProposedL2BlockNumber();
const seconds = try op.getSecondsToNextL2Output(block);
try testing.expectEqual(seconds, 3600);
}

test "GetTransactionDepositEvents" {
const uri = try std.Uri.parse("http://localhost:8545/");

var op: L1Client(.http) = undefined;
defer op.deinit();

try op.init(.{ .uri = uri, .allocator = testing.allocator }, null);

const deposit_events = try op.getTransactionDepositEvents(try utils.hashToBytes("0xe94031c3174788c3fee7216465c50bb2b72e7a1963f5af807b3768da10827f5c"));
defer {
for (deposit_events) |event| testing.allocator.free(event.opaqueData);
testing.allocator.free(deposit_events);
}

try testing.expect(deposit_events.len != 0);
try testing.expectEqual(deposit_events[0].to, try utils.addressToBytes("0xbc3ed6B537f2980e66f396Fe14210A56ba3f72C4"));
}

test "GetProvenWithdrawals" {
const uri = try std.Uri.parse("http://localhost:8545/");

var op: L1Client(.http) = undefined;
defer op.deinit();

try op.init(.{ .uri = uri, .allocator = testing.allocator }, .{ .portalAddress = try utils.addressToBytes("0x49048044D57e1C92A77f79988d21Fa8fAF74E97e") });

const proven = try op.getProvenWithdrawals(try utils.hashToBytes("0xEC0AD491512F4EDC603C2DD7B9371A0B18D4889A23E74692101BA4C6DC9B5709"));

try testing.expectEqual(proven.l2OutputIndex, 1490);
}

test "GetFinalizedWithdrawals" {
const uri = try std.Uri.parse("http://localhost:8545/");

var op: L1Client(.http) = undefined;
defer op.deinit();

try op.init(.{ .uri = uri, .allocator = testing.allocator }, .{ .portalAddress = try utils.addressToBytes("0x49048044D57e1C92A77f79988d21Fa8fAF74E97e") });

const finalized = try op.getFinalizedWithdrawals(try utils.hashToBytes("0xEC0AD491512F4EDC603C2DD7B9371A0B18D4889A23E74692101BA4C6DC9B5709"));
try testing.expect(finalized);
}

test "Errors" {
const uri = try std.Uri.parse("http://localhost:8545/");

var op: L1Client(.http) = undefined;
defer op.deinit();

try op.init(.{ .uri = uri, .allocator = testing.allocator }, null);
try testing.expectError(error.InvalidBlockNumber, op.getSecondsToNextL2Output(1));
try testing.expectError(error.InvalidWithdrawalHash, op.getSecondsToFinalize(try utils.hashToBytes("0xe94031c3174788c3fee7216465c50bb2b72e7a1963f5af807b3768da10827f5c")));
}
3 changes: 0 additions & 3 deletions src/root.zig
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,6 @@ pub const Signer = @import("crypto/Signer.zig");
pub const WsServerContext = @import("tests/clients/ws_server.zig").WsContext;

test {
// _ = @import("clients/ens/ens.zig");
// _ = @import("clients/optimism/clients/L1PubClient.zig");
// _ = @import("clients/optimism/clients/L2PubClient.zig");
_ = @import("abi/abi_parameter.zig");
_ = @import("abi/abi.zig");
_ = @import("abi/eip712.zig");
Expand Down

0 comments on commit 231c77f

Please sign in to comment.