Skip to content

Commit 874faa0

Browse files
committed
AddressLookupTable: deserializeOwned
1 parent bb94ea6 commit 874faa0

File tree

2 files changed

+14
-4
lines changed

2 files changed

+14
-4
lines changed

src/replay/resolve_lookup.zig

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -300,10 +300,10 @@ fn getLookupTable(
300300
const account_bytes = buf[0..account.data.len()];
301301
account.data.readAll(account_bytes);
302302

303-
var table = AddressLookupTable.deserialize(account_bytes) catch {
303+
const table = AddressLookupTable.deserializeOwned(allocator, account_bytes) catch |err| {
304+
if (err == error.OutOfMemory) return err;
304305
return error.InvalidAddressLookupTableData;
305306
};
306-
table.addresses = try allocator.dupe(Pubkey, table.addresses);
307307

308308
// NOTE: deactivated lookup tables are allowed to be used,
309309
// according to agave's implementation. see here, where agave

src/runtime/program/address_lookup_table/state.zig

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,10 +103,10 @@ pub const AddressLookupTable = struct {
103103

104104
// [agave] https://github.com/anza-xyz/agave/blob/d300f3733f45d64a3b6b9fdb5a1157f378e181c2/sdk/program/src/address_lookup_table/state.rs#L224
105105
/// NOTE: This AddressLookupTable's 'addresses' slice will point to inside 'data' - consider
106-
/// if you need to clone the buffer.
106+
/// if you need to clone the buffer (see deserializeOwned).
107107
pub fn deserialize(
108108
data: []const u8,
109-
) (error{OutOfMemory} || InstructionError)!AddressLookupTable {
109+
) error{ UninitializedAccount, InvalidAccountData }!AddressLookupTable {
110110
const noalloc = sig.utils.allocators.failing.allocator(.{});
111111
const state = sig.bincode.readFromSlice(noalloc, ProgramState, data, .{}) catch
112112
return error.InvalidAccountData;
@@ -124,4 +124,14 @@ pub const AddressLookupTable = struct {
124124
.addresses = addresses,
125125
};
126126
}
127+
128+
/// Deserializes an AddressLookupTable, coping .addresses into a new buffer.
129+
pub fn deserializeOwned(
130+
allocator: std.mem.Allocator,
131+
data: []const u8,
132+
) error{ OutOfMemory, UninitializedAccount, InvalidAccountData }!AddressLookupTable {
133+
var table = try AddressLookupTable.deserialize(data);
134+
table.addresses = try allocator.dupe(Pubkey, table.addresses);
135+
return table;
136+
}
127137
};

0 commit comments

Comments
 (0)