Skip to content

Commit

Permalink
refactor(svm): refactor transaction implementation (#513)
Browse files Browse the repository at this point in the history
* Transaction refactor

* replace transaction builder

* Removed CheckedReader

* remove nullability from address lookups

* Address PR comments

* implement serialize

* restore transaction unit tests

* missed test case

* address comments

* Address PR comments

* fix type issue

* Extract signable component into message

* add clone test
  • Loading branch information
yewman authored Feb 12, 2025
1 parent 3502333 commit 805b122
Show file tree
Hide file tree
Showing 11 changed files with 566 additions and 764 deletions.
40 changes: 21 additions & 19 deletions docs/docusaurus/docs/usage/rpc-api-client.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ From a string:
const Pubkey = @import("sig").core.Pubkey;
fn main() !void {
const pubkey = try Pubkey.fromString("4rL4RCWHz3iNCdCaveD8KcHfV9YWGsqSHFPo7X2zBNwa");
const pubkey = try Pubkey.parseBase58String("4rL4RCWHz3iNCdCaveD8KcHfV9YWGsqSHFPo7X2zBNwa");
}
```

Expand Down Expand Up @@ -89,7 +91,7 @@ pub fn main() !void {
var client = try rpc.Client.init(allocator, .{ .http_endpoint = HTTP_ENDPOINT });
defer client.deinit();
const pubkey = try Pubkey.fromString("4rL4RCWHz3iNCdCaveD8KcHfV9YWGsqSHFPo7X2zBNwa");
const pubkey = try Pubkey.parseBase58String("4rL4RCWHz3iNCdCaveD8KcHfV9YWGsqSHFPo7X2zBNwa");
var resp = try client.getAccountInfo(pubkey, .{ .encoding = .Base64 });
defer resp.deinit();
Expand Down Expand Up @@ -128,7 +130,7 @@ const Pubkey = sig.core.Pubkey;
const allocator = std.heap.page_allocator;
pub fn main() !void {
const pubkey = try Pubkey.fromString("4rL4RCWHz3iNCdCaveD8KcHfV9YWGsqSHFPo7X2zBNwa");
const pubkey = try Pubkey.parseBase58String("4rL4RCWHz3iNCdCaveD8KcHfV9YWGsqSHFPo7X2zBNwa");
var resp = try client.getBalance(pubkey);
defer resp.deinit();
Expand Down Expand Up @@ -958,10 +960,10 @@ pub fn main() !void {
defer client.deinit();
var accounts = [2]Pubkey{
try Pubkey.fromString(
try Pubkey.parseBase58String(
"6dmNQ5jwLeLk5REvio1JcMshcbvkYMwy26sJ8pbkvStu",
) ,
try Pubkey.fromString(
try Pubkey.parseBase58String(
"BGsqMegLpV6n6Ve146sSX2dTjUMj3M92HnU8BbNRMhF2",
),
};
Expand Down Expand Up @@ -1280,10 +1282,10 @@ pub fn main() !void {
defer client.deinit();
var accounts2 = [2]Pubkey{
try Pubkey.fromString(
try Pubkey.parseBase58String(
"4rL4RCWHz3iNCdCaveD8KcHfV9YWGsqSHFPo7X2zBNwa",
),
try Pubkey.fromString(
try Pubkey.parseBase58String(
"BGsqMegLpV6n6Ve146sSX2dTjUMj3M92HnU8BbNRMhF2",
),
};
Expand Down Expand Up @@ -1344,7 +1346,7 @@ pub fn main() !void {
var filters = [1]Filter{.{ .memcmp = .{ .offset = 0, .bytes = "EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v" } }};
var resp = try client.getProgramAccounts(
try Pubkey.fromString("TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA"),
try Pubkey.parseBase58String("TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA"),
.{ .filters = &filters },
);
defer resp.deinit();
Expand Down Expand Up @@ -1480,7 +1482,7 @@ pub fn main() !void {
var client = try rpc.Client.init(allocator, .{ .http_endpoint = HTTP_ENDPOINT });
defer client.deinit();
var resp = try client.getSignaturesForAddress(try Pubkey.fromString("4rL4RCWHz3iNCdCaveD8KcHfV9YWGsqSHFPo7X2zBNwa"), .{ .limit = 10 });
var resp = try client.getSignaturesForAddress(try Pubkey.parseBase58String("4rL4RCWHz3iNCdCaveD8KcHfV9YWGsqSHFPo7X2zBNwa"), .{ .limit = 10 });
defer resp.deinit();
if (resp.err()) |err| {
Expand Down Expand Up @@ -1670,7 +1672,7 @@ pub fn main() !void {
var client = try rpc.Client.init(allocator, .{ .http_endpoint = HTTP_ENDPOINT });
defer client.deinit();
var resp = try client.getStakeActivation(try Pubkey.fromString(
var resp = try client.getStakeActivation(try Pubkey.parseBase58String(
"CWrKSEDYhj6VHGocZowq2BUncKESqD7rdLTSrsoasTjU",
), .{});
defer resp.deinit();
Expand Down Expand Up @@ -1817,7 +1819,7 @@ pub fn main() !void {
var client = try rpc.Client.init(allocator, .{ .http_endpoint = HTTP_ENDPOINT });
defer client.deinit();
var pubkey = try Pubkey.fromString(
var pubkey = try Pubkey.parseBase58String(
"6A5NHCj1yF6urc9wZNe6Bcjj4LVszQNj5DwAWG97yzMu",
);
var resp = try client.getTokenAccountBalance(pubkey, .{});
Expand Down Expand Up @@ -1876,10 +1878,10 @@ pub fn main() !void {
var client = try rpc.Client.init(allocator, .{ .http_endpoint = HTTP_ENDPOINT });
defer client.deinit();
var programPubkey = try Pubkey.fromString(
var programPubkey = try Pubkey.parseBase58String(
"TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA",
);
var pubkey = try Pubkey.fromString(
var pubkey = try Pubkey.parseBase58String(
"CTz5UMLQm2SRWHzQnU62Pi4yJqbNGjgRBHqqp6oDHfF7",
);
var resp = try client.getTokenAccountsByDelegate(pubkey, .{ .programId = programPubkey }, .{});
Expand Down Expand Up @@ -1938,10 +1940,10 @@ pub fn main() !void {
var client = try rpc.Client.init(allocator, .{ .http_endpoint = HTTP_ENDPOINT });
defer client.deinit();
var mintPubkey = try Pubkey.fromString(
var mintPubkey = try Pubkey.parseBase58String(
"EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v",
);
var pubkey = try Pubkey.fromString(
var pubkey = try Pubkey.parseBase58String(
"CTz5UMLQm2SRWHzQnU62Pi4yJqbNGjgRBHqqp6oDHfF7",
);
var resp = try client.getTokenAccountsByOwner(pubkey, .{ .mint = mintPubkey }, .{});
Expand Down Expand Up @@ -1992,7 +1994,7 @@ pub fn main() !void {
var client = try rpc.Client.init(allocator, .{ .http_endpoint = HTTP_ENDPOINT });
defer client.deinit();
var mintPubkey = try Pubkey.fromString(
var mintPubkey = try Pubkey.parseBase58String(
"EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v",
);
var resp = try client.getTokenLargestAccounts(mintPubkey, .{});
Expand Down Expand Up @@ -2043,7 +2045,7 @@ pub fn main() !void {
var client = try rpc.Client.init(allocator, .{ .http_endpoint = HTTP_ENDPOINT });
defer client.deinit();
var mintPubkey = try Pubkey.fromString(
var mintPubkey = try Pubkey.parseBase58String(
"EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v",
);
var resp = try client.getTokenSupply(mintPubkey, .{});
Expand Down Expand Up @@ -2237,7 +2239,7 @@ pub fn main() !void {
var client = try rpc.Client.init(allocator, .{ .http_endpoint = HTTP_ENDPOINT });
defer client.deinit();
var votePubkey = try Pubkey.fromString(
var votePubkey = try Pubkey.parseBase58String(
"CertusDeBmqN8ZawdkxK5kFGMwBXdudvWHYwtNgNhvLu",
);
var resp = try client.getVoteAccounts(.{ .votePubkey = votePubkey });
Expand Down Expand Up @@ -2376,7 +2378,7 @@ pub fn main() !void {
var client = try rpc.Client.init(allocator, .{ .http_endpoint = HTTP_ENDPOINT });
defer client.deinit();
var pubkey = try Pubkey.fromString(
var pubkey = try Pubkey.parseBase58String(
"Bvg7GuhqwNmV2JVyeZjhAcTPFqPktfmq25VBaZipozda",
);
var resp = try client.requestAirdrop(pubkey, 10000, .{});
Expand Down
2 changes: 1 addition & 1 deletion src/bincode/benchmarks.zig
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ pub const BenchmarkEntry = struct {
const actual_struct = try sig.bincode.readFromSlice(
allocator,
Entry,
&test_entry.bincode_serialized_bytes,
&test_entry.as_bytes,
.{},
);
defer actual_struct.deinit(allocator);
Expand Down
28 changes: 14 additions & 14 deletions src/core/entry.zig
Original file line number Diff line number Diff line change
@@ -1,53 +1,53 @@
pub const std = @import("std");
pub const sig = @import("../sig.zig");
pub const core = @import("lib.zig");

const Hash = sig.core.hash.Hash;
const Transaction = sig.core.transaction.Transaction;

pub const Entry = struct {
/// The number of hashes since the previous Entry ID.
num_hashes: u64,

/// The SHA-256 hash `num_hashes` after the previous Entry ID.
hash: core.Hash,
hash: Hash,

/// An unordered list of transactions that were observed before the Entry ID was
/// generated. They may have been observed before a previous Entry ID but were
/// pushed back into this list to ensure deterministic interpretation of the ledger.
transactions: std.ArrayListUnmanaged(core.VersionedTransaction),
transactions: std.ArrayListUnmanaged(Transaction),

pub fn isTick(self: Entry) bool {
return self.transactions.items.len == 0;
}

pub fn deinit(self: Entry, allocator: std.mem.Allocator) void {
for (self.transactions.items) |tx| {
tx.deinit(allocator);
}
for (self.transactions.items) |tx| tx.deinit(allocator);
allocator.free(self.transactions.allocatedSlice());
}
};

test "Entry serialization and deserialization" {
const entry = test_entry.as_struct;
try sig.bincode.testRoundTrip(entry, &test_entry.bincode_serialized_bytes);
try sig.bincode.testRoundTrip(entry, &test_entry.as_bytes);
}

pub const test_entry = struct {
var txns = [_]Transaction{
sig.core.transaction.transaction_v0_example.as_struct,
sig.core.transaction.transaction_v0_example.as_struct,
};

pub const as_struct = Entry{
.num_hashes = 149218308,
.hash = core.Hash
.hash = sig.core.Hash
.parseBase58String("G8T3smgLc4XavAtxScD3u4FTAqPtwbFCEJKwJbfoECcd") catch unreachable,
.transactions = .{
.items = txns[0..2],
.capacity = 2,
},
};

var txns = [_]core.VersionedTransaction{
core.transaction.test_v0_transaction.as_struct,
core.transaction.test_v0_transaction.as_struct,
};

pub const bincode_serialized_bytes = [_]u8{
pub const as_bytes = [_]u8{
4, 228, 228, 8, 0, 0, 0, 0, 224, 199, 210, 235, 148, 143, 98, 241, 248, 45,
140, 115, 214, 164, 132, 17, 95, 89, 221, 166, 5, 158, 5, 121, 181, 80, 48, 103,
173, 21, 40, 70, 2, 0, 0, 0, 0, 0, 0, 0, 2, 81, 7, 106, 50, 99,
Expand Down
8 changes: 1 addition & 7 deletions src/core/lib.zig
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,9 @@ pub const Nonce = shred.Nonce;
pub const Pubkey = pubkey.Pubkey;
pub const ShredVersion = shred.ShredVersion;
pub const Signature = signature.Signature;
pub const Transaction = transaction.Transaction;

pub const Epoch = time.Epoch;
pub const Slot = time.Slot;

pub const CompiledInstruction = transaction.CompiledInstruction;
pub const Message = transaction.Message;
pub const MessageHeader = transaction.MessageHeader;
pub const Transaction = transaction.Transaction;
pub const VersionedTransaction = transaction.VersionedTransaction;
pub const V0Message = transaction.V0Message;

pub const Cluster = enum { mainnet, testnet, devnet, localnet };
Loading

0 comments on commit 805b122

Please sign in to comment.