Skip to content

Commit

Permalink
remove tmppubkey and remove cached_str in pubkey
Browse files Browse the repository at this point in the history
  • Loading branch information
0xNineteen committed Nov 30, 2023
1 parent 04ede14 commit eed2eec
Show file tree
Hide file tree
Showing 10 changed files with 48 additions and 82 deletions.
2 changes: 1 addition & 1 deletion src/cmd/cmd.zig
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ fn gossip(_: []const []const u8) !void {
std.debug.print("gossip port: {d}\n", .{gossip_port});

// setup contact info
var my_pubkey = Pubkey.fromPublicKey(&my_keypair.public_key, false);
var my_pubkey = Pubkey.fromPublicKey(&my_keypair.public_key);
var contact_info = LegacyContactInfo.default(my_pubkey);
contact_info.shred_version = 0; // TODO: double check
contact_info.gossip = gossip_address;
Expand Down
30 changes: 2 additions & 28 deletions src/core/append_vec.zig
Original file line number Diff line number Diff line change
Expand Up @@ -15,43 +15,17 @@ const AppendVecInfo = @import("./snapshot_fields.zig").AppendVecInfo;

const base58 = @import("base58-zig");

pub const TmpPubkey = struct {
data: [32]u8,
// note: need to remove cached string to have correct ptr casting

pub fn toString(self: *const TmpPubkey) error{EncodingError}![44]u8 {
var dest: [44]u8 = undefined;
@memset(&dest, 0);

const encoder = base58.Encoder.init(.{});
var written = encoder.encode(&self.data, &dest) catch return error.EncodingError;
if (written > 44) {
std.debug.panic("written is > 44, written: {}, dest: {any}, bytes: {any}", .{ written, dest, self.data });
}
return dest;
}

pub fn format(self: @This(), comptime _: []const u8, _: std.fmt.FormatOptions, writer: anytype) std.os.WriteError!void {
const str = self.toString() catch unreachable;
return writer.print("{s}", .{str});
}

pub fn isDefault(self: *const TmpPubkey) bool {
return std.mem.eql(u8, &self.data, &[_]u8{0} ** 32);
}
};

// metadata which is stored inside an AppendVec
pub const AppendVecStoreInfo = struct {
write_version_obsolete: u64,
data_len: u64,
pubkey: TmpPubkey,
pubkey: Pubkey,
};

pub const AppendVecInnerAccountInfo = struct {
lamports: u64,
rent_epoch: Epoch,
owner: TmpPubkey,
owner: Pubkey,
executable: bool,
};

Expand Down
28 changes: 10 additions & 18 deletions src/core/pubkey.zig
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ const decoder = base58.Decoder.init(.{});

pub const Pubkey = struct {
data: [32]u8,
cached_str: ?[44]u8 = null,

const Self = @This();

Expand Down Expand Up @@ -38,16 +37,11 @@ pub const Pubkey = struct {
/// set this option to true and it will not decode & cache the encoded value. This can be helpful in
/// scenarios where you plan to only use the bytes and want to save on expensive base58 encoding.
///
pub fn fromBytes(bytes: []const u8, opts: struct { skip_encoding: bool = false }) !Self {
pub fn fromBytes(bytes: []const u8) !Self {
if (bytes.len != 32) {
return Error.InvalidBytesLength;
}
if (opts.skip_encoding) {
return Self{ .data = bytes[0..32].*, .cached_str = null };
} else {
var dest = base58_encode(bytes) catch @panic("could not encode pubkey");
return Self{ .data = bytes[0..32].*, .cached_str = dest };
}
return Self{ .data = bytes[0..32].* };
}

pub fn base58_encode(bytes: []const u8) error{EncodingError}![44]u8 {
Expand All @@ -61,11 +55,7 @@ pub const Pubkey = struct {
}

pub fn string(self: *const Self) [44]u8 {
if (self.cached_str) |str| {
return str;
} else {
return base58_encode(&self.data) catch @panic("could not encode pubkey");
}
return Self.base58_encode(&self.data) catch @panic("could not encode pubkey");
}

/// ***random*** generates a random pubkey. Optionally set `skip_encoding` to skip expensive base58 encoding.
Expand All @@ -79,13 +69,13 @@ pub const Pubkey = struct {
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
};
if (options.skip_encoding) {
return Self{ .data = bytes[0..32].*, .cached_str = null };
return Self{ .data = bytes[0..32].* };
}
var written = encoder.encode(&bytes, &dest) catch @panic("could not encode pubkey");
if (written > 44) {
std.debug.panic("written is > 44, written: {}, dest: {any}, bytes: {any}", .{ written, dest, bytes });
}
return Self{ .data = bytes[0..32].*, .cached_str = dest[0..44].* };
return Self{ .data = bytes[0..32].* };
}

pub fn default() Self {
Expand All @@ -96,15 +86,17 @@ pub const Pubkey = struct {
return std.mem.eql(u8, &self.data, &other.data);
}

pub fn fromPublicKey(public_key: *const Ed25519.PublicKey, skip_bs58_encoding: bool) Self {
return Self.fromBytes(public_key.bytes[0..], .{ .skip_encoding = skip_bs58_encoding }) catch unreachable;
pub fn fromPublicKey(public_key: *const Ed25519.PublicKey) Self {
return Self.fromBytes(&public_key.bytes) catch unreachable;
}

pub fn format(self: @This(), comptime _: []const u8, _: std.fmt.FormatOptions, writer: anytype) std.os.WriteError!void {
return writer.print("{s}", .{self.string()});
}

pub const @"!bincode-config:cached_str" = bincode.FieldConfig(?[44]u8){ .skip = true };
pub fn isDefault(self: *const Self) bool {
return std.mem.eql(u8, &self.data, &[_]u8{0} ** 32);
}
};

const Error = error{ InvalidBytesLength, InvalidEncodedLength, InvalidEncodedValue };
10 changes: 5 additions & 5 deletions src/gossip/crds.zig
Original file line number Diff line number Diff line change
Expand Up @@ -824,7 +824,7 @@ pub const SnapshotHashes = struct {

test "gossip.crds: test sig verify duplicateShreds" {
var keypair = try KeyPair.create([_]u8{1} ** 32);
var pubkey = Pubkey.fromPublicKey(&keypair.public_key, true);
var pubkey = Pubkey.fromPublicKey(&keypair.public_key);
var rng = std.rand.DefaultPrng.init(0);
var data = DuplicateShred.random(rng.random());
data.from = pubkey;
Expand All @@ -848,7 +848,7 @@ test "gossip.crds: test CrdsValue label() and id() methods" {
var kp_bytes = [_]u8{1} ** 32;
var kp = try KeyPair.create(kp_bytes);
const pk = kp.public_key;
var id = Pubkey.fromPublicKey(&pk, true);
var id = Pubkey.fromPublicKey(&pk);

var legacy_contact_info = LegacyContactInfo.default(id);
legacy_contact_info.wallclock = 0;
Expand All @@ -865,7 +865,7 @@ test "gossip.crds: pubkey matches rust" {
var kp_bytes = [_]u8{1} ** 32;
const kp = try KeyPair.create(kp_bytes);
const pk = kp.public_key;
const id = Pubkey.fromPublicKey(&pk, true);
const id = Pubkey.fromPublicKey(&pk);

const rust_bytes = [_]u8{ 138, 136, 227, 221, 116, 9, 241, 149, 253, 82, 219, 45, 60, 186, 93, 114, 202, 103, 9, 191, 29, 148, 18, 27, 243, 116, 136, 1, 180, 15, 111, 92 };
var buf = [_]u8{0} ** 1024;
Expand All @@ -880,7 +880,7 @@ test "gossip.crds: contact info serialization matches rust" {
var kp_bytes = [_]u8{1} ** 32;
const kp = try KeyPair.create(kp_bytes);
const pk = kp.public_key;
const id = Pubkey.fromPublicKey(&pk, true);
const id = Pubkey.fromPublicKey(&pk);

const gossip_addr = SocketAddr.initIpv4(.{ 127, 0, 0, 1 }, 1234);

Expand All @@ -900,7 +900,7 @@ test "gossip.crds: crds data serialization matches rust" {
var kp_bytes = [_]u8{1} ** 32;
const kp = try KeyPair.create(kp_bytes);
const pk = kp.public_key;
const id = Pubkey.fromPublicKey(&pk, true);
const id = Pubkey.fromPublicKey(&pk);

const gossip_addr = SocketAddr.initIpv4(.{ 127, 0, 0, 1 }, 1234);

Expand Down
4 changes: 2 additions & 2 deletions src/gossip/crds_table.zig
Original file line number Diff line number Diff line change
Expand Up @@ -839,7 +839,7 @@ test "gossip.crds_table: insert and get votes" {
var kp_bytes = [_]u8{1} ** 32;
const kp = try KeyPair.create(kp_bytes);
const pk = kp.public_key;
var id = Pubkey.fromPublicKey(&pk, true);
var id = Pubkey.fromPublicKey(&pk);

var vote = crds.Vote{ .from = id, .transaction = Transaction.default(), .wallclock = 10 };
var crds_value = try CrdsValue.initSigned(CrdsData{
Expand Down Expand Up @@ -878,7 +878,7 @@ test "gossip.crds_table: insert and get votes" {

test "gossip.crds_table: insert and get contact_info" {
const kp = try KeyPair.create([_]u8{1} ** 32);
var id = Pubkey.fromPublicKey(&kp.public_key, true);
var id = Pubkey.fromPublicKey(&kp.public_key);

var legacy_contact_info = crds.LegacyContactInfo.default(id);
var crds_value = try CrdsValue.initSigned(CrdsData{
Expand Down
10 changes: 5 additions & 5 deletions src/gossip/fuzz.zig
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ pub fn randomPongPacket(rng: std.rand.Random, keypair: *const KeyPair, to_addr:

pub fn randomCrdsValue(rng: std.rand.Random, maybe_should_pass_sig_verification: ?bool) !CrdsValue {
var keypair = try KeyPair.create(null);
var pubkey = Pubkey.fromPublicKey(&keypair.public_key, false);
var pubkey = Pubkey.fromPublicKey(&keypair.public_key);

// will have random id
var value = try CrdsValue.random(rng, &keypair);
Expand All @@ -117,7 +117,7 @@ pub fn randomPushMessage(rng: std.rand.Random, keypair: *const KeyPair, to_addr:
const allocator = std.heap.page_allocator;
const packets = try crds_values_to_packets(
allocator,
&Pubkey.fromPublicKey(&keypair.public_key, false),
&Pubkey.fromPublicKey(&keypair.public_key),
&crds_values,
&to_addr,
ChunkType.PushMessage,
Expand All @@ -137,7 +137,7 @@ pub fn randomPullResponse(rng: std.rand.Random, keypair: *const KeyPair, to_addr
const allocator = std.heap.page_allocator;
const packets = try crds_values_to_packets(
allocator,
&Pubkey.fromPublicKey(&keypair.public_key, false),
&Pubkey.fromPublicKey(&keypair.public_key),
&crds_values,
&to_addr,
ChunkType.PullResponse,
Expand Down Expand Up @@ -226,7 +226,7 @@ pub fn main() !void {
var exit = AtomicBool.init(false);

// setup contact info
var my_pubkey = Pubkey.fromPublicKey(&my_keypair.public_key, false);
var my_pubkey = Pubkey.fromPublicKey(&my_keypair.public_key);
var contact_info = LegacyContactInfo.default(my_pubkey);
contact_info.shred_version = 0;
contact_info.gossip = gossip_address;
Expand All @@ -253,7 +253,7 @@ pub fn main() !void {
var fuzz_keypair = try KeyPair.create(null);
var fuzz_address = SocketAddr.initIpv4(.{ 127, 0, 0, 1 }, 9998);

var fuzz_pubkey = Pubkey.fromPublicKey(&fuzz_keypair.public_key, false);
var fuzz_pubkey = Pubkey.fromPublicKey(&fuzz_keypair.public_key);
var fuzz_contact_info = LegacyContactInfo.default(fuzz_pubkey);
fuzz_contact_info.shred_version = 19;
fuzz_contact_info.gossip = fuzz_address;
Expand Down
28 changes: 14 additions & 14 deletions src/gossip/gossip_service.zig
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ pub const GossipService = struct {
var crds_table = try CrdsTable.init(allocator);
errdefer crds_table.deinit();
var crds_table_rw = RwMux(CrdsTable).init(crds_table);
var my_pubkey = Pubkey.fromPublicKey(&my_keypair.public_key, false);
var my_pubkey = Pubkey.fromPublicKey(&my_keypair.public_key);
var my_shred_version = my_contact_info.shred_version;
var active_set = ActiveSet.init(allocator);

Expand Down Expand Up @@ -1080,7 +1080,7 @@ pub const GossipService = struct {

const now = get_wallclock_ms();
var buf: [PACKET_DATA_SIZE]u8 = undefined;
const my_pubkey = Pubkey.fromPublicKey(&self.my_keypair.public_key, true);
const my_pubkey = Pubkey.fromPublicKey(&self.my_keypair.public_key);

for (failed_origins.keys(), 0..) |origin, i| {
origin_buf[origin_count] = origin;
Expand Down Expand Up @@ -1382,7 +1382,7 @@ test "gossip.gossip_service: tests handle_prune_messages" {
const allocator = std.testing.allocator;
var exit = AtomicBool.init(false);
var my_keypair = try KeyPair.create([_]u8{1} ** 32);
var my_pubkey = Pubkey.fromPublicKey(&my_keypair.public_key, true);
var my_pubkey = Pubkey.fromPublicKey(&my_keypair.public_key);

var contact_info = crds.LegacyContactInfo.default(my_pubkey);
contact_info.gossip = SocketAddr.initIpv4(.{ 127, 0, 0, 1 }, 0);
Expand Down Expand Up @@ -1450,7 +1450,7 @@ test "gossip.gossip_service: tests handle_pull_response" {
var rng = std.rand.DefaultPrng.init(91);
var exit = AtomicBool.init(false);
var my_keypair = try KeyPair.create([_]u8{1} ** 32);
var my_pubkey = Pubkey.fromPublicKey(&my_keypair.public_key, true);
var my_pubkey = Pubkey.fromPublicKey(&my_keypair.public_key);

var contact_info = crds.LegacyContactInfo.default(my_pubkey);
contact_info.gossip = SocketAddr.initIpv4(.{ 127, 0, 0, 1 }, 0);
Expand Down Expand Up @@ -1503,7 +1503,7 @@ test "gossip.gossip_service: tests handle_pull_request" {
var rng = std.rand.DefaultPrng.init(91);
var exit = AtomicBool.init(false);
var my_keypair = try KeyPair.create([_]u8{1} ** 32);
var my_pubkey = Pubkey.fromPublicKey(&my_keypair.public_key, true);
var my_pubkey = Pubkey.fromPublicKey(&my_keypair.public_key);

var contact_info = crds.LegacyContactInfo.default(my_pubkey);
contact_info.gossip = SocketAddr.initIpv4(.{ 127, 0, 0, 1 }, 0);
Expand Down Expand Up @@ -1587,7 +1587,7 @@ test "gossip.gossip_service: test build prune messages and handle_push_msgs" {
var rng = std.rand.DefaultPrng.init(91);
var exit = AtomicBool.init(false);
var my_keypair = try KeyPair.create([_]u8{1} ** 32);
var my_pubkey = Pubkey.fromPublicKey(&my_keypair.public_key, true);
var my_pubkey = Pubkey.fromPublicKey(&my_keypair.public_key);

var contact_info = crds.LegacyContactInfo.default(my_pubkey);
contact_info.gossip = SocketAddr.initIpv4(.{ 127, 0, 0, 1 }, 0);
Expand Down Expand Up @@ -1660,7 +1660,7 @@ test "gossip.gossip_service: test build_pull_requests" {
var rng = std.rand.DefaultPrng.init(91);
var exit = AtomicBool.init(false);
var my_keypair = try KeyPair.create([_]u8{1} ** 32);
var my_pubkey = Pubkey.fromPublicKey(&my_keypair.public_key, true);
var my_pubkey = Pubkey.fromPublicKey(&my_keypair.public_key);

var contact_info = crds.LegacyContactInfo.default(my_pubkey);
contact_info.gossip = SocketAddr.initIpv4(.{ 127, 0, 0, 1 }, 0);
Expand Down Expand Up @@ -1704,7 +1704,7 @@ test "gossip.gossip_service: test build_push_messages" {
var rng = std.rand.DefaultPrng.init(91);
var exit = AtomicBool.init(false);
var my_keypair = try KeyPair.create([_]u8{1} ** 32);
var my_pubkey = Pubkey.fromPublicKey(&my_keypair.public_key, true);
var my_pubkey = Pubkey.fromPublicKey(&my_keypair.public_key);

var contact_info = crds.LegacyContactInfo.default(my_pubkey);
contact_info.gossip = SocketAddr.initIpv4(.{ 127, 0, 0, 1 }, 0);
Expand Down Expand Up @@ -1736,7 +1736,7 @@ test "gossip.gossip_service: test build_push_messages" {
lg.unlock();

var keypair = try KeyPair.create([_]u8{1} ** 32);
// var id = Pubkey.fromPublicKey(&keypair.public_key, false);
// var id = Pubkey.fromPublicKey(&keypair.public_key);
var value = try CrdsValue.random(rng.random(), &keypair);

// set the active set
Expand Down Expand Up @@ -1775,7 +1775,7 @@ test "gossip.gossip_service: test packet verification" {
const allocator = std.testing.allocator;
var exit = AtomicBool.init(false);
var keypair = try KeyPair.create([_]u8{1} ** 32);
var id = Pubkey.fromPublicKey(&keypair.public_key, true);
var id = Pubkey.fromPublicKey(&keypair.public_key);

var contact_info = crds.LegacyContactInfo.default(id);
contact_info.gossip = SocketAddr.initIpv4(.{ 127, 0, 0, 1 }, 0);
Expand Down Expand Up @@ -1850,7 +1850,7 @@ test "gossip.gossip_service: test packet verification" {

// send it with a CrdsValue which hash a slice
{
var rand_pubkey = Pubkey.fromPublicKey(&rand_keypair.public_key, true);
var rand_pubkey = Pubkey.fromPublicKey(&rand_keypair.public_key);
var dshred = crds.DuplicateShred.random(rng.random());
var chunk: [32]u8 = .{1} ** 32;
dshred.chunk = &chunk;
Expand Down Expand Up @@ -1903,7 +1903,7 @@ test "gossip.gossip_service: process contact_info push packet" {
const allocator = std.testing.allocator;
var exit = AtomicBool.init(false);
var my_keypair = try KeyPair.create([_]u8{1} ** 32);
var my_pubkey = Pubkey.fromPublicKey(&my_keypair.public_key, true);
var my_pubkey = Pubkey.fromPublicKey(&my_keypair.public_key);

var contact_info = crds.LegacyContactInfo.default(my_pubkey);
contact_info.gossip = SocketAddr.initIpv4(.{ 127, 0, 0, 1 }, 0);
Expand All @@ -1926,7 +1926,7 @@ test "gossip.gossip_service: process contact_info push packet" {
var responder_channel = gossip_service.packet_outgoing_channel;

var kp = try KeyPair.create(null);
var pk = Pubkey.fromPublicKey(&kp.public_key, false);
var pk = Pubkey.fromPublicKey(&kp.public_key);

var packet_handle = try Thread.spawn(
.{},
Expand Down Expand Up @@ -2037,7 +2037,7 @@ pub const BenchmarkMessageProcessing = struct {
var keypair = try KeyPair.create(null);
var address = SocketAddr.initIpv4(.{ 127, 0, 0, 1 }, 0);

var pubkey = Pubkey.fromPublicKey(&keypair.public_key, false);
var pubkey = Pubkey.fromPublicKey(&keypair.public_key);
var contact_info = crds.LegacyContactInfo.default(pubkey);
contact_info.shred_version = 19;
contact_info.gossip = address;
Expand Down
6 changes: 3 additions & 3 deletions src/gossip/ping_pong.zig
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ pub const Ping = struct {
pub fn init(token: [PING_TOKEN_SIZE]u8, keypair: *const KeyPair) !Self {
const sig = try keypair.sign(&token, null);
var self = Self{
.from = Pubkey.fromPublicKey(&keypair.public_key, true),
.from = Pubkey.fromPublicKey(&keypair.public_key),
.token = token,
.signature = Signature.init(sig.toBytes()),
};
Expand All @@ -52,7 +52,7 @@ pub const Ping = struct {
var sig = keypair.sign(&token, null) catch unreachable; // TODO: do we need noise?

return Self{
.from = Pubkey.fromPublicKey(&keypair.public_key, true),
.from = Pubkey.fromPublicKey(&keypair.public_key),
.token = token,
.signature = Signature.init(sig.toBytes()),
};
Expand All @@ -78,7 +78,7 @@ pub const Pong = struct {
const sig = keypair.sign(&hash.data, null) catch return error.SignatureError;

return Self{
.from = Pubkey.fromPublicKey(&keypair.public_key, true),
.from = Pubkey.fromPublicKey(&keypair.public_key),
.hash = hash,
.signature = Signature.init(sig.toBytes()),
};
Expand Down
Loading

0 comments on commit eed2eec

Please sign in to comment.