p2p/enode: store local seq by ID

This commit is contained in:
Felix Lange 2018-10-01 17:13:48 +02:00
parent eba7a41a34
commit 47d29f2c9b
3 changed files with 14 additions and 6 deletions

View file

@ -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)
} }

View file

@ -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)
}
} }

View file

@ -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