Fork choice compile and run 🔥

This commit is contained in:
mratsim 2018-09-12 15:41:49 +02:00
parent acf322ea7b
commit a42a0ad851
4 changed files with 14 additions and 9 deletions

View File

@ -27,7 +27,7 @@ proc broadcast(self: Node, x: BlockOrSig) =
proc log(self: Node, words: string, lvl = 3, all = false) =
if (self.id == 0 or all) and lvl >= 2:
echo self.id, words
echo self.id, " - ", words
func add_to_timequeue(self: Node, obj: Block) =
var i = 0
@ -122,7 +122,7 @@ method on_receive(self: Node, blck: Block, reprocess = false) =
self.add_to_timequeue(blck)
return
# Add the block
self.log "Processing beacon block &" % blck.hash.data[0 .. ^4].toHex(false)
self.log "Processing beacon block " & blck.hash.data[0 .. ^4].toHex(false)
self.blocks[blck.hash] = blck
# Is the block building on the head? Then add it to the head!
if blck.parent_hash == self.main_chain[^1] or self.careless:
@ -254,5 +254,5 @@ proc tick*(self: Node) =
self.last_made_sig = slot
# process time queue
while self.timequeue.len > 0 and self.timequeue[0].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(self.timequeue[0], reprocess = true)
self.timequeue.delete(0) # This is expensive, but we can't use a queue due to random insertions in add_to_timequeue

View File

@ -25,9 +25,7 @@ for i in 0'i32 ..< NOTARIES:
)
net.generate_peers()
for i in 0 ..< 100000:
net.tick()
net.run(steps = 100000)
for n in net.agents:
echo &"Local timestamp: {n.timestamp:>.1}, timequeue len {n.timequeue.len}"

View File

@ -176,6 +176,7 @@ proc newNode*(
result.scores = newTable[MDigest[256], int]()
result.scores_at_height = newTable[array[36, byte], int]()
result.sigs = newTable[MDigest[384], Sig]()
result.justified = newTable[MDigest[256], bool]()
###########################################################
# Forward declarations

View File

@ -40,16 +40,22 @@ proc generate_peers*(self: NetworkSimulator, num_peers = 5) =
proc tick*(self: NetworkSimulator) =
if self.time in self.objqueue:
for ro in self.objqueue[self.time]:
let (recipient, obj) = ro
# on_receive, calls broadcast which will enqueue new BlockOrSig in objqueue
# so we can't for loop like in EF research repo (modifying length is not allowed)
var ros: seq[tuple[recipient: Node, obj: BlockOrSig]]
shallowCopy(ros, self.objqueue[self.time])
var i = 0
while i < ros.len:
let (recipient, obj) = ros[i]
if rand(1.0) < self.reliability:
recipient.on_receive(obj)
inc i
self.objqueue.del self.time
for a in self.agents:
a.tick()
self.time += initDuration(seconds = 1)
proc run(self: NetworkSimulator, steps: int) =
proc run*(self: NetworkSimulator, steps: int) =
for i in 0 ..< steps:
self.tick()