-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
16 changed files
with
618 additions
and
90 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
const std = @import("std"); | ||
const sig = @import("../lib.zig"); | ||
|
||
const Instant = std.time.Instant; | ||
|
||
const Hash = sig.core.Hash; | ||
const VersionedTransaction = sig.core.VersionedTransaction; | ||
|
||
/// Analogous to Entry [TODO] | ||
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: 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.ArrayList(VersionedTransaction), | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
pub const poh = @import("poh.zig"); | ||
pub const entry = @import("entry.zig"); | ||
|
||
pub const Poh = poh.Poh; | ||
pub const Entry = entry.Entry; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
const std = @import("std"); | ||
const sig = @import("../lib.zig"); | ||
|
||
const Instant = std.time.Instant; | ||
|
||
const Hash = sig.core.Hash; | ||
|
||
const hashValue = sig.core.hash.hashValue; | ||
const hashValues = sig.core.hash.hashValues; | ||
|
||
pub const Poh = struct { | ||
hash: Hash, | ||
num_hashes: u64, | ||
hashes_per_tick: u64, | ||
remaining_hashes: u64, | ||
tick_number: u64, | ||
slot_start_time: Instant, | ||
|
||
pub fn init(hash: Hash, hashes_per_tick: u64, tick_number: u64) !Poh { | ||
return .{ | ||
.hash = hash, | ||
.num_hashes = 0, | ||
.hashes_per_tick = hashes_per_tick, | ||
.remaining_hashes = hashes_per_tick, | ||
.tick_number = tick_number, | ||
.slot_start_time = try Instant.now(), | ||
}; | ||
} | ||
|
||
/// Repeatedly rehash until tick is required or max number of hashes completed | ||
/// Returns true if a tick is required | ||
pub fn increment(self: *Poh, max_num_hashes: u64) bool { | ||
const num_hashes = @min(self.remaining_hashes - 1, max_num_hashes); | ||
for (num_hashes) |_| self.hash = hashValue(self.hash.bytes); | ||
self.num_hashes += num_hashes; | ||
self.remaining_hashes -= num_hashes; | ||
return self.remaining_hashes == 1; | ||
} | ||
|
||
/// Incorporate a mixin hash into the Poh chain | ||
/// Return null if a tick is required | ||
pub fn record(self: *Poh, mixin: Hash) ?PohEntry { | ||
if (self.remaining_hashes == 1) return null; | ||
self.hash = hashValues([_][]const u8{ self.hash.bytes, mixin.bytes }); | ||
const num_hashes = self.num_hashes + 1; | ||
self.num_hashes = 0; | ||
self.remaining_hashes -= 1; | ||
return .{ | ||
.num_hashes = num_hashes, | ||
.hash = self.hash, | ||
}; | ||
} | ||
|
||
/// Emit PohEntry and prepare Poh for next tick | ||
/// Return null if their are still remaining hashes | ||
pub fn tick(self: *const Poh) ?PohEntry { | ||
self.hash = hashValue(self.hash.bytes); | ||
self.num_hashes += 1; | ||
self.remaining_hashes -= 1; | ||
if (self.remaining_hashes != 0) return null; | ||
|
||
const num_hashes = self.num_hashes; | ||
self.remaining_hashes = self.hashes_per_tick; | ||
self.num_hashes = 0; | ||
self.tick_number += 1; | ||
|
||
return .{ | ||
.num_hashes = num_hashes, | ||
.hash = self.hash, | ||
}; | ||
} | ||
}; | ||
|
||
pub const PohEntry = struct { | ||
num_hashes: u64, | ||
hash: Hash, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
pub const service = @import("service.zig"); | ||
|
||
pub const StandardBroadcast = @import("standard_broadcast.zig"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
const std = @import("std"); | ||
const sig = @import("../../lib.zig"); | ||
|
||
const AtomicBool = std.atomic.Value(bool); | ||
const KeyPair = std.crypto.sign.Ed25519.KeyPair; | ||
const Instant = std.time.Instant; | ||
|
||
const Duration = sig.time.Duration; | ||
const Channel = sig.sync.Channel; | ||
const BlockstoreReader = sig.ledger.BlockstoreReader; | ||
const BlockstoreWriter = sig.ledger.BlockstoreWriter; | ||
const ShredInserter = sig.ledger.insert_shred.ShredInserter; | ||
const Shred = sig.ledger.shred.Shred; | ||
const Hash = sig.core.Hash; | ||
const ReedSolomonCache = sig.ledger.shredder.ReedSolomonCache; | ||
const Slot = sig.core.Slot; | ||
const PohEntry = sig.poh.Entry; | ||
|
||
// MISSING DATA STRUCTURES | ||
const Bank = struct {}; | ||
const UdpSocket = struct {}; | ||
const Blockstore = struct {}; | ||
const BankForks = struct {}; | ||
const WorkingBankEntry = struct { | ||
bank: Bank, | ||
entry: PohEntry, | ||
last_tick_height: u64, | ||
}; | ||
|
||
/// Runs the broadcast service using a broadcaster implementation | ||
/// The broadcaster must implement receive, transmit, and record functions | ||
pub fn runBroadcastService( | ||
allocator: std.mem.Allocator, | ||
broadcaster: anytype, | ||
sockets: std.ArrayList(UdpSocket), | ||
keypair: KeyPair, | ||
receiver: Channel(WorkingBankEntry), | ||
retransmit_receiver: Channel(Slot), | ||
blockstore: Blockstore, | ||
bank_forks: BankForks, | ||
exit: *AtomicBool, | ||
) !void { | ||
const socket_sender, const socket_receiver = Channel(std.ArrayList(Shred)).init(allocator, 100); | ||
const blockstore_sender, const blockstore_receiver = Channel(std.ArrayList(Shred)).init(allocator, 100); | ||
|
||
const broadcast_handle = try std.Thread.spawn( | ||
.{}, | ||
broadcaster.receive, | ||
.{ | ||
keypair, | ||
blockstore, | ||
receiver, | ||
socket_sender, | ||
blockstore_sender, | ||
exit, | ||
}, | ||
); | ||
|
||
var transmit_handles = try std.ArrayList(std.Thread).init(allocator); | ||
for (sockets.items) |socket| { | ||
try transmit_handles.append(try std.Thread.spawn( | ||
.{}, | ||
broadcaster.transmit, | ||
.{ | ||
&socket_receiver, | ||
&bank_forks, | ||
&socket, | ||
exit, | ||
}, | ||
)); | ||
} | ||
|
||
const record_handle = try std.Thread.spawn( | ||
.{}, | ||
broadcaster.record, | ||
.{ | ||
&blockstore_receiver, | ||
&blockstore, | ||
exit, | ||
}, | ||
); | ||
|
||
const retransmit_handle = try std.Thread.spawn( | ||
.{}, | ||
retransmit, | ||
.{ | ||
&blockstore, | ||
&retransmit_receiver, | ||
&socket_sender, | ||
exit, | ||
}, | ||
); | ||
|
||
broadcast_handle.join(); | ||
for (transmit_handles) |h| h.join(); | ||
record_handle.join(); | ||
retransmit_handle.join(); | ||
} | ||
|
||
fn retransmit( | ||
blockstore: *Blockstore, | ||
retransmit_receiver: *Channel(Slot), | ||
socket_sender: *Channel(std.ArrayList(Shred)), | ||
exit: *AtomicBool, | ||
) !void { | ||
_ = blockstore; | ||
_ = retransmit_receiver; | ||
_ = socket_sender; | ||
_ = exit; | ||
} |
Oops, something went wrong.