Skip to content

Commit

Permalink
common, p2p: use AbsTime.Add instead of conversion (ethereum#25417)
Browse files Browse the repository at this point in the history
  • Loading branch information
gzliudan committed Dec 19, 2024
1 parent 19d6c08 commit d2b2585
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 12 deletions.
4 changes: 2 additions & 2 deletions common/mclock/simclock.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func (s *Simulated) Run(d time.Duration) {
s.mu.Lock()
s.init()

end := s.now + AbsTime(d)
end := s.now.Add(d)
var do []func()
for len(s.scheduled) > 0 && s.scheduled[0].at <= end {
ev := heap.Pop(&s.scheduled).(*simTimer)
Expand Down Expand Up @@ -134,7 +134,7 @@ func (s *Simulated) AfterFunc(d time.Duration, fn func()) Timer {
func (s *Simulated) schedule(d time.Duration, fn func()) *simTimer {
s.init()

at := s.now + AbsTime(d)
at := s.now.Add(d)
ev := &simTimer{do: fn, at: at, s: s}
heap.Push(&s.scheduled, ev)
s.cond.Broadcast()
Expand Down
4 changes: 2 additions & 2 deletions common/prque/lazyqueue.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,13 +88,13 @@ func (q *LazyQueue) Refresh() {

// refresh re-evaluates items in the older queue and swaps the two queues
func (q *LazyQueue) refresh(now mclock.AbsTime) {
q.maxUntil = now + mclock.AbsTime(q.period)
q.maxUntil = now.Add(q.period)
for q.queue[0].Len() != 0 {
q.Push(heap.Pop(q.queue[0]).(*item).value)
}
q.queue[0], q.queue[1] = q.queue[1], q.queue[0]
q.indexOffset = 1 - q.indexOffset
q.maxUntil += mclock.AbsTime(q.period)
q.maxUntil = q.maxUntil.Add(q.period)
}

// Push adds an item to the queue
Expand Down
8 changes: 4 additions & 4 deletions p2p/discv5/ticket.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ func pongToTicket(localTime mclock.AbsTime, topics []Topic, node *Node, p *ingre
}
// Convert wait periods to local absolute time.
for i, wp := range wps {
t.regTime[i] = localTime + mclock.AbsTime(time.Second*time.Duration(wp))
t.regTime[i] = localTime.Add(time.Second * time.Duration(wp))
}
return t, nil
}
Expand Down Expand Up @@ -349,7 +349,7 @@ func (s *ticketStore) nextFilteredTicket() (*ticketRef, time.Duration) {
}
log.Trace("Found discovery ticket to register", "node", ticket.t.node, "serial", ticket.t.serial, "wait", wait)

regTime := now + mclock.AbsTime(wait)
regTime := now.Add(wait)
topic := ticket.t.topics[ticket.idx]
if s.tickets[topic] != nil && regTime >= s.tickets[topic].nextReg {
return ticket, wait
Expand Down Expand Up @@ -614,7 +614,7 @@ func (s *ticketStore) cleanupTopicQueries(now mclock.AbsTime) {
delete(s.queriesSent, n)
}
}
s.nextTopicQueryCleanup = now + mclock.AbsTime(topicQueryTimeout)
s.nextTopicQueryCleanup = now.Add(topicQueryTimeout)
}

func (s *ticketStore) gotTopicNodes(from *Node, hash common.Hash, nodes []rpcNode) (timeout bool) {
Expand All @@ -625,7 +625,7 @@ func (s *ticketStore) gotTopicNodes(from *Node, hash common.Hash, nodes []rpcNod
return true
}
q, ok := qq[hash]
if !ok || now > q.sent+mclock.AbsTime(topicQueryTimeout) {
if !ok || now > q.sent.Add(topicQueryTimeout) {
return true
}
inside := float64(0)
Expand Down
8 changes: 4 additions & 4 deletions p2p/discv5/topic.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ func (t *topicTable) addEntry(node *Node, topic Topic) {
topic: topic,
fifoIdx: fifoIdx,
node: node,
expire: tm + mclock.AbsTime(fallbackRegistrationExpiry),
expire: tm.Add(fallbackRegistrationExpiry),
}
if printTestImgLogs {
fmt.Printf("*+ %d %v %016x %016x\n", tm/1000000, topic, t.self.sha[:8], node.sha[:8])
Expand Down Expand Up @@ -251,7 +251,7 @@ func (t *topicTable) useTicket(node *Node, serialNo uint32, topics []Topic, idx
}
if serialNo != n.lastUsedTicket {
n.lastUsedTicket = serialNo
n.noRegUntil = tm + mclock.AbsTime(noRegTimeout())
n.noRegUntil = tm.Add(noRegTimeout())
t.storeTicketCounters(node)
}

Expand All @@ -263,7 +263,7 @@ func (t *topicTable) useTicket(node *Node, serialNo uint32, topics []Topic, idx
t.addEntry(node, topics[idx])
} else {
// if there is an active entry, don't move to the front of the FIFO but prolong expire time
e.expire = tm + mclock.AbsTime(fallbackRegistrationExpiry)
e.expire = tm.Add(fallbackRegistrationExpiry)
}
return true
}
Expand Down Expand Up @@ -293,7 +293,7 @@ func (topictab *topicTable) getTicket(node *Node, topics []Topic) *ticket {
waitPeriod = minWaitPeriod
}

t.regTime[i] = now + mclock.AbsTime(waitPeriod)
t.regTime[i] = now.Add(waitPeriod)
}
return t
}
Expand Down

0 comments on commit d2b2585

Please sign in to comment.