Switch to proper time datatypes

This commit is contained in:
mratsim 2018-09-04 17:27:53 +02:00
parent 1cb28aecc8
commit 0561bf70b7
3 changed files with 37 additions and 13 deletions

View File

@ -13,6 +13,7 @@
import
# Stdlib
tables, deques, strutils, endians, strformat,
times,
# Nimble packages
nimcrypto,
# Local imports
@ -27,7 +28,7 @@ method on_receive(self: Node, obj: BlockOrSig, reprocess = false) {.base.} =
###########################################################
proc broadcast(self: Node, x: Block) =
if self.sleepy and self.timestamp != 0:
if self.sleepy and self.timestamp != Duration():
return
self.network.broadcast(self, x)
self.on_receive(x)
@ -191,7 +192,8 @@ method on_receive(self: Node, sig: Sig, reprocess = false) =
# considered finalized
var finalize = true
for slot2 in countdown(slot-1, max(slot - EPOCH_LENGTH * 1, 0) - 1):
for slot2 in countdown(slot-1, max(slot - EPOCH_LENGTH * 1, 0)):
# Note the max(...)-1 in spec is unneeded, Nim ranges are inclusive
if slot2 < self.blocks[c2].slot:
c2 = self.blocks[c2].parent_hash
@ -223,3 +225,19 @@ method on_receive(self: Node, sig: Sig, reprocess = false) =
# Rebroadcast
self.network.broadcast(self, sig)
func get_sig_targets(self: Node, start_slot: int32): seq[MDigest[256]] =
# Get the portion of the main chain that is within the last EPOCH_LENGTH
# slots, once again duplicating the parent in cases where the parent and
# child's slots are not consecutive
result = @[]
var i = self.main_chain.high
for slot in countdown(start_slot-1, max(start_slot - EPOCH_LENGTH, 0)):
# Note the max(...)-1 in spec is unneeded, Nim ranges are inclusive
if slot < self.blocks[self.main_chain[i]].slot:
dec i
result.add self.main_chain[i]
for i, x in result:
doAssert self.blocks[x].slot <= start_slot - 1 - i
doAssert result.len == min(EPOCH_LENGTH, start_slot)

View File

@ -11,12 +11,12 @@
# Note that implementation is not updated to the latest v2.1 yet
import
tables, deques, strutils, hashes, # Stdlib
tables, deques, strutils, hashes, times, # Stdlib
nimcrypto # Nimble packages
const
NOTARIES* = 100 # Committee size in Casper v2.1
SLOT_SIZE* = 6 # Slot duration in Casper v2.1
SLOT_SIZE* = initDuration(seconds = 6) # Slot duration in Casper v2.1
EPOCH_LENGTH* = 25 # Cycle length in Casper v2.
# TODO, clear up if reference semantics are needed
@ -43,7 +43,7 @@ type
slot*: int64
##########################################
func min_timestamp*(self: Block): int64 =
func min_timestamp*(self: Block): Duration =
SLOT_SIZE * self.slot
let Genesis* = Block()
@ -71,14 +71,20 @@ method hash*(x: SigHash): Hash =
## Allow usage of Sighash in tables
x.raw.hash
func hash*(x: Duration): Hash =
## Allow usage of Duration in tables
# Due to rpivate fields, we use pointer + length as a hack:
# https://github.com/nim-lang/Nim/issues/8857
result = hashData(x.unsafeAddr, x.sizeof)
#########################################
type
NetworkSimulator* = ref object
agents*: seq[int]
latency_distribution_sample*: proc (): int
time*: int64
objqueue*: TableRef[int64, seq[(Node, BlockOrSig)]]
latency_distribution_sample*: proc (): Duration
time*: Duration
objqueue*: TableRef[Duration, seq[(Node, BlockOrSig)]]
peers*: TableRef[int, seq[Node]]
reliability*: float
@ -101,7 +107,7 @@ type
scores_at_height*: TableRef[array[36, byte], int] # Should be slot not height in v2.1
justified*: TableRef[MDigest[256], bool]
finalized*: TableRef[MDigest[256], bool]
timestamp*: int64
timestamp*: Duration
id*: int
network*: NetworkSimulator
used_parents*: TableRef[MDigest[256], Node]

View File

@ -10,7 +10,7 @@
# Part of Casper+Sharding chain v2.1: https://notes.ethereum.org/SCIg8AH5SA-O4C1G1LYZHQ#
import
tables,
tables, times,
./fork_choice_types
func broadcast*(self: NetworkSimulator, sender: Node, obj: BlockOrSig) =