Skip to content

p2p: refactor to use time.Now().UnixMilli() in golang std lib #32402

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 0 additions & 9 deletions p2p/enode/localnode.go
Original file line number Diff line number Diff line change
Expand Up @@ -305,12 +305,3 @@ func (ln *LocalNode) bumpSeq() {
ln.seq++
ln.db.storeLocalSeq(ln.id, ln.seq)
}

// nowMilliseconds gives the current timestamp at millisecond precision.
func nowMilliseconds() uint64 {
ns := time.Now().UnixNano()
if ns < 0 {
return 0
}
return uint64(ns / 1000 / 1000)
}
3 changes: 2 additions & 1 deletion p2p/enode/localnode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"net"
"net/netip"
"testing"
"time"

"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/p2p/enr"
Expand Down Expand Up @@ -53,7 +54,7 @@ func TestLocalNode(t *testing.T) {

// This test checks that the sequence number is persisted between restarts.
func TestLocalNodeSeqPersist(t *testing.T) {
timestamp := nowMilliseconds()
timestamp := uint64(time.Now().UnixMilli())

ln, db := newLocalNodeForTesting()
defer db.Close()
Expand Down
2 changes: 1 addition & 1 deletion p2p/enode/nodedb.go
Original file line number Diff line number Diff line change
Expand Up @@ -434,7 +434,7 @@ func (db *DB) localSeq(id ID) uint64 {
if seq := db.fetchUint64(localItemKey(id, dbLocalSeq)); seq > 0 {
return seq
}
return nowMilliseconds()
return uint64(time.Now().UnixMilli())
}

// storeLocalSeq stores the local record sequence counter.
Expand Down