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

View file

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

View file

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