Finish fork choice rule base procs

This commit is contained in:
mratsim 2018-09-05 12:03:52 +02:00
parent 3d76e238e7
commit e049d9e106
2 changed files with 34 additions and 5 deletions

View File

@ -13,7 +13,7 @@
import
# Stdlib
tables, deques, strutils, endians, strformat,
times,
times, sequtils,
# Nimble packages
nimcrypto,
# Local imports
@ -27,7 +27,7 @@ method on_receive(self: Node, obj: BlockOrSig, reprocess = false) {.base.} =
###########################################################
proc broadcast(self: Node, x: Block) =
proc broadcast(self: Node, x: BlockOrSig) =
if self.sleepy and self.timestamp != DurationZero:
return
self.network.broadcast(self, x)
@ -250,3 +250,19 @@ proc tick(self: Node) =
self.broadcast(
initBlock(self.blocks[self.main_chain[^1]], slot, self.id)
)
self.last_made_block = slot
# Make a sig?
if slot > self.last_made_sig and (slot mod EPOCH_LENGTH) == self.id mod EPOCH_LENGTH:
var sig_from = self.main_chain.high
while sig_from > 0 and self.blocks[self.main_chain[sig_from]].slot >= slot - EPOCH_LENGTH:
dec sig_from
let sig = initSig(self.id, self.get_sig_targets(slot), slot, self.timestamp)
self.log &"Sig: {self.id} {sig.slot} {sig.targets.mapIt(($it)[0 ..< 4])}"
self.broadcast sig
self.last_made_sig = slot
# process time queue
var first = self.timequeue[0]
while self.timequeue.len > 0 and first.min_timestamp <= self.timestamp:
self.timequeue.delete(0) # This is expensive, but we can't use a queue due to random insertions in add_to_timequeue
self.on_receive(first, reprocess = true)
first = self.timequeue[0]

View File

@ -116,7 +116,7 @@ type
proposer*: int64 # the validator that creates a block
targets*: seq[MDigest[256]] # the hash of blocks proposed
slot*: int32 # slot number
timestamp*: int64 # ts in the ref implementation
timestamp*: Duration # ts in the ref implementation
hash*: MDigest[384] # The signature (BLS12-384)
Node* = ref object
@ -138,5 +138,18 @@ type
sleepy*: bool
careless*: bool
first_round*: bool
last_made_block*: int64
last_made_sig*: int64
last_made_block*: int32
last_made_sig*: int32
proc initSig*(
proposer: int32,
targets: seq[MDigest[256]],
slot: int32,
ts: Duration): Sig =
new result
result.proposer = proposer
result.targets = targets
result.slot = slot
result.timestamp = ts
for val in result.hash.data.mitems:
val = rand(0.byte .. 7.byte)