From 231c77f270658a2b2aaefaf430dee2f10a76699e Mon Sep 17 00:00:00 2001 From: Raiden1411 <67233402+Raiden1411@users.noreply.github.com> Date: Tue, 6 Aug 2024 07:08:11 +0100 Subject: [PATCH] decoder: update decoder on superchain client --- src/clients/optimism/clients/L1PubClient.zig | 120 +++++++++++++++++-- src/root.zig | 3 - 2 files changed, 112 insertions(+), 11 deletions(-) diff --git a/src/clients/optimism/clients/L1PubClient.zig b/src/clients/optimism/clients/L1PubClient.zig index 99efbe28..194b5765 100644 --- a/src/clients/optimism/clients/L1PubClient.zig +++ b/src/clients/optimism/clients/L1PubClient.zig @@ -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; @@ -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. /// @@ -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.?, }); @@ -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"))); +} diff --git a/src/root.zig b/src/root.zig index 64f58b22..48fef3c2 100644 --- a/src/root.zig +++ b/src/root.zig @@ -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");