diff --git a/beacon_chain/fork_choice_rule/fork_choice_rule.nim b/beacon_chain/fork_choice_rule/fork_choice_rule.nim index 96e152312..127fd3516 100644 --- a/beacon_chain/fork_choice_rule/fork_choice_rule.nim +++ b/beacon_chain/fork_choice_rule/fork_choice_rule.nim @@ -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 diff --git a/beacon_chain/fork_choice_rule/fork_choice_test.nim b/beacon_chain/fork_choice_rule/fork_choice_test.nim index 6af92eaf8..0a7145847 100644 --- a/beacon_chain/fork_choice_rule/fork_choice_test.nim +++ b/beacon_chain/fork_choice_rule/fork_choice_test.nim @@ -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}" diff --git a/beacon_chain/fork_choice_rule/fork_choice_types.nim b/beacon_chain/fork_choice_rule/fork_choice_types.nim index 4ec11e9d4..4fcb748a7 100644 --- a/beacon_chain/fork_choice_rule/fork_choice_types.nim +++ b/beacon_chain/fork_choice_rule/fork_choice_types.nim @@ -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 diff --git a/beacon_chain/fork_choice_rule/networksim.nim b/beacon_chain/fork_choice_rule/networksim.nim index 47142e524..80c4f6e1f 100644 --- a/beacon_chain/fork_choice_rule/networksim.nim +++ b/beacon_chain/fork_choice_rule/networksim.nim @@ -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()