mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-08-19 10:22:23 +00:00
p2p/enode: store local seq by ID
This commit is contained in:
parent
eba7a41a34
commit
47d29f2c9b
3 changed files with 14 additions and 6 deletions
|
|
@ -63,10 +63,10 @@ func NewLocalNode(db *DB, key *ecdsa.PrivateKey) *LocalNode {
|
|||
id: PubkeyToIDV4(&key.PublicKey),
|
||||
db: db,
|
||||
key: key,
|
||||
seq: db.LocalSeq(),
|
||||
udpTrack: netutil.NewIPTracker(iptrackWindow, iptrackContactWindow, iptrackMinStatements),
|
||||
entries: make(map[string]enr.Entry),
|
||||
}
|
||||
ln.seq = db.localSeq(ln.id)
|
||||
ln.invalidate()
|
||||
return ln
|
||||
}
|
||||
|
|
@ -242,5 +242,5 @@ func (ln *LocalNode) sign() {
|
|||
|
||||
func (ln *LocalNode) bumpSeq() {
|
||||
ln.seq++
|
||||
ln.db.storeLocalSeq(ln.seq)
|
||||
ln.db.storeLocalSeq(ln.id, ln.seq)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -65,4 +65,12 @@ func TestLocalNodeSeqPersist(t *testing.T) {
|
|||
if s := ln2.Node().Seq(); s != 3 {
|
||||
t.Fatalf("wrong seq %d on new instance, want 3", s)
|
||||
}
|
||||
|
||||
// Create a new instance with a different node key on the same database.
|
||||
// This should reset the sequence number.
|
||||
key, _ := crypto.GenerateKey()
|
||||
ln3 := NewLocalNode(db, key)
|
||||
if s := ln3.Node().Seq(); s != 1 {
|
||||
t.Fatalf("wrong seq %d on instance with changed key, want 1", s)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -328,13 +328,13 @@ func (db *DB) UpdateFindFails(id ID, fails int) error {
|
|||
}
|
||||
|
||||
// LocalSeq retrieves the local record sequence counter.
|
||||
func (db *DB) LocalSeq() uint64 {
|
||||
return db.fetchUint64([]byte(dbLocalSeq))
|
||||
func (db *DB) localSeq(id ID) uint64 {
|
||||
return db.fetchUint64(makeKey(id, dbLocalSeq))
|
||||
}
|
||||
|
||||
// storeLocalSeq stores the local record sequence counter.
|
||||
func (db *DB) storeLocalSeq(n uint64) {
|
||||
db.storeUint64([]byte(dbLocalSeq), n)
|
||||
func (db *DB) storeLocalSeq(id ID, n uint64) {
|
||||
db.storeUint64(makeKey(id, dbLocalSeq), n)
|
||||
}
|
||||
|
||||
// QuerySeeds retrieves random nodes to be used as potential seed nodes
|
||||
|
|
|
|||
Loading…
Reference in a new issue