Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Gossip: CRDS Table implementation #6

Merged
merged 23 commits into from
Jul 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions src/core/hash.zig
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@ const Sha256 = std.crypto.hash.sha2.Sha256;

pub const HASH_SIZE: usize = 32;

pub const CompareResult = enum {
Greater,
Less,
Equal,
};

pub const Hash = struct {
data: [HASH_SIZE]u8,

Expand All @@ -15,4 +21,15 @@ pub const Hash = struct {
Sha256.hash(bytes, &hash.data, .{});
return hash;
}

pub fn cmp(a: *const Self, b: *const Self) CompareResult {
for (0..HASH_SIZE) |i| {
if (a.data[i] > b.data[i]) {
return CompareResult.Greater;
} else if (a.data[i] < b.data[i]) {
return CompareResult.Less;
}
}
return CompareResult.Equal;
}
};
26 changes: 26 additions & 0 deletions src/core/transaction.zig
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,14 @@ pub const Transaction = struct {
message: Message,

pub const @"!bincode-config:signatures" = shortvec_config;

// used in tests
pub fn default() Transaction {
return Transaction{
.signatures = &[_]Signature{},
.message = Message.default(),
};
}
};

pub const Message = struct {
Expand All @@ -19,6 +27,19 @@ pub const Message = struct {

pub const @"!bincode-config:account_keys" = shortvec_config;
pub const @"!bincode-config:instructions" = shortvec_config;

pub fn default() Message {
return Message{
.header = MessageHeader{
.num_required_signatures = 0,
.num_readonly_signed_accounts = 0,
.num_readonly_unsigned_accounts = 0,
},
.account_keys = &[_]Pubkey{},
.recent_blockhash = Hash.generateSha256Hash(&[_]u8{0}),
.instructions = &[_]CompiledInstruction{},
};
}
};

pub const MessageHeader = struct {
Expand Down Expand Up @@ -48,3 +69,8 @@ pub const CompiledInstruction = struct {
pub const @"!bincode-config:accounts" = shortvec_config;
pub const @"!bincode-config:data" = shortvec_config;
};

test "core.transaction: tmp" {
const msg = Message.default();
try std.testing.expect(msg.account_keys.len == 0);
}
172 changes: 155 additions & 17 deletions src/gossip/crds.zig
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
const std = @import("std");
const Pubkey = @import("../core/pubkey.zig").Pubkey;
const SocketAddr = @import("net.zig").SocketAddr;
const Tuple = std.meta.Tuple;
const Hash = @import("../core/hash.zig").Hash;
Expand All @@ -9,26 +8,11 @@ const Slot = @import("../core/slot.zig").Slot;
const Option = @import("../option.zig").Option;
const ContactInfo = @import("node.zig").ContactInfo;
const bincode = @import("bincode-zig");
const AutoArrayHashMap = std.AutoArrayHashMap;
const ArrayList = std.ArrayList;
const ArrayListConfig = @import("../utils/arraylist.zig").ArrayListConfig;
const Bloom = @import("../bloom/bloom.zig").Bloom;
const KeyPair = std.crypto.sign.Ed25519.KeyPair;

/// Cluster Replicated Data Store
pub const Crds = struct {
store: AutoArrayHashMap(CrdsValueLabel, CrdsVersionedValue),

const Self = @This();

pub fn init(allocator: std.mem.Allocator) Self {
return Self{ .store = AutoArrayHashMap(CrdsValueLabel, CrdsVersionedValue).init(allocator) };
}

pub fn deinit(self: *Self) void {
self.store.deinit();
}
};
const Pubkey = @import("../core/pubkey.zig").Pubkey;

pub const CrdsFilter = struct {
filter: Bloom,
Expand All @@ -51,6 +35,7 @@ pub const CrdsFilter = struct {
};

pub const CrdsVersionedValue = struct {
ordinal: u64,
value: CrdsValue,
local_timestamp: u64,
value_hash: Hash,
Expand Down Expand Up @@ -92,6 +77,129 @@ pub const CrdsValue = struct {
var msg = try bincode.writeToSlice(buf[0..], self.data, bincode.Params.standard);
return self.signature.verify(pubkey, msg);
}

pub fn id(self: *const Self) Pubkey {
return switch (self.data) {
.LegacyContactInfo => |*v| {
return v.id;
},
.Vote => |*v| {
return v[1].from;
},
.LowestSlot => |*v| {
return v[1].from;
},
.LegacySnapshotHashes => |*v| {
return v.from;
},
.AccountsHashes => |*v| {
return v.from;
},
.EpochSlots => |*v| {
return v[1].from;
},
.LegacyVersion => |*v| {
return v.from;
},
.Version => |*v| {
return v.from;
},
.NodeInstance => |*v| {
return v.from;
},
.DuplicateShred => |*v| {
return v[1].from;
},
.SnapshotHashes => |*v| {
return v.from;
},
.ContactInfo => |*v| {
return v.pubkey;
},
};
}

pub fn wallclock(self: *const Self) u64 {
return switch (self.data) {
.LegacyContactInfo => |*v| {
return v.wallclock;
},
.Vote => |*v| {
return v[1].wallclock;
},
.LowestSlot => |*v| {
return v[1].wallclock;
},
.LegacySnapshotHashes => |*v| {
return v.wallclock;
},
.AccountsHashes => |*v| {
return v.wallclock;
},
.EpochSlots => |*v| {
return v[1].wallclock;
},
.LegacyVersion => |*v| {
return v.wallclock;
},
.Version => |*v| {
return v.wallclock;
},
.NodeInstance => |*v| {
return v.wallclock;
},
.DuplicateShred => |*v| {
return v[1].wallclock;
},
.SnapshotHashes => |*v| {
return v.wallclock;
},
.ContactInfo => |*v| {
return v.wallclock;
},
};
}

pub fn label(self: *const Self) CrdsValueLabel {
return switch (self.data) {
.LegacyContactInfo => {
return CrdsValueLabel{ .LegacyContactInfo = self.id() };
},
.Vote => |*v| {
return CrdsValueLabel{ .Vote = .{ v[0], self.id() } };
},
.LowestSlot => {
return CrdsValueLabel{ .LowestSlot = self.id() };
},
.LegacySnapshotHashes => {
return CrdsValueLabel{ .LegacySnapshotHashes = self.id() };
},
.AccountsHashes => {
return CrdsValueLabel{ .AccountsHashes = self.id() };
},
.EpochSlots => |*v| {
return CrdsValueLabel{ .EpochSlots = .{ v[0], self.id() } };
},
.LegacyVersion => {
return CrdsValueLabel{ .LegacyVersion = self.id() };
},
.Version => {
return CrdsValueLabel{ .Version = self.id() };
},
.NodeInstance => {
return CrdsValueLabel{ .NodeInstance = self.id() };
},
.DuplicateShred => |*v| {
return CrdsValueLabel{ .DuplicateShred = .{ v[0], self.id() } };
},
.SnapshotHashes => {
return CrdsValueLabel{ .SnapshotHashes = self.id() };
},
.ContactInfo => {
return CrdsValueLabel{ .ContactInfo = self.id() };
},
};
}
};

pub const LegacyContactInfo = struct {
Expand Down Expand Up @@ -331,6 +439,36 @@ pub const SnapshotHashes = struct {
wallclock: u64,
};

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);
const unspecified_addr = SocketAddr.unspecified();
var legacy_contact_info = LegacyContactInfo{
.id = id,
.gossip = unspecified_addr,
.tvu = unspecified_addr,
.tvu_forwards = unspecified_addr,
.repair = unspecified_addr,
.tpu = unspecified_addr,
.tpu_forwards = unspecified_addr,
.tpu_vote = unspecified_addr,
.rpc = unspecified_addr,
.rpc_pubsub = unspecified_addr,
.serve_repair = unspecified_addr,
.wallclock = 0,
.shred_version = 0,
};

var crds_value = try CrdsValue.initSigned(CrdsData{
.LegacyContactInfo = legacy_contact_info,
}, kp);

try std.testing.expect(crds_value.id().equals(&id));
try std.testing.expect(crds_value.label().LegacyContactInfo.equals(&id));
}

test "gossip.crds: default crds filter matches rust bytes" {
const rust_bytes = [_]u8{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 255, 255, 255, 255, 255, 255, 255, 255, 0, 0, 0, 0 };
var filter = CrdsFilter.init(std.testing.allocator);
Expand Down
Loading
Loading