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),
|
id: PubkeyToIDV4(&key.PublicKey),
|
||||||
db: db,
|
db: db,
|
||||||
key: key,
|
key: key,
|
||||||
seq: db.LocalSeq(),
|
|
||||||
udpTrack: netutil.NewIPTracker(iptrackWindow, iptrackContactWindow, iptrackMinStatements),
|
udpTrack: netutil.NewIPTracker(iptrackWindow, iptrackContactWindow, iptrackMinStatements),
|
||||||
entries: make(map[string]enr.Entry),
|
entries: make(map[string]enr.Entry),
|
||||||
}
|
}
|
||||||
|
ln.seq = db.localSeq(ln.id)
|
||||||
ln.invalidate()
|
ln.invalidate()
|
||||||
return ln
|
return ln
|
||||||
}
|
}
|
||||||
|
|
@ -242,5 +242,5 @@ func (ln *LocalNode) sign() {
|
||||||
|
|
||||||
func (ln *LocalNode) bumpSeq() {
|
func (ln *LocalNode) bumpSeq() {
|
||||||
ln.seq++
|
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 {
|
if s := ln2.Node().Seq(); s != 3 {
|
||||||
t.Fatalf("wrong seq %d on new instance, want 3", s)
|
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.
|
// LocalSeq retrieves the local record sequence counter.
|
||||||
func (db *DB) LocalSeq() uint64 {
|
func (db *DB) localSeq(id ID) uint64 {
|
||||||
return db.fetchUint64([]byte(dbLocalSeq))
|
return db.fetchUint64(makeKey(id, dbLocalSeq))
|
||||||
}
|
}
|
||||||
|
|
||||||
// storeLocalSeq stores the local record sequence counter.
|
// storeLocalSeq stores the local record sequence counter.
|
||||||
func (db *DB) storeLocalSeq(n uint64) {
|
func (db *DB) storeLocalSeq(id ID, n uint64) {
|
||||||
db.storeUint64([]byte(dbLocalSeq), n)
|
db.storeUint64(makeKey(id, dbLocalSeq), n)
|
||||||
}
|
}
|
||||||
|
|
||||||
// QuerySeeds retrieves random nodes to be used as potential seed nodes
|
// QuerySeeds retrieves random nodes to be used as potential seed nodes
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue