2019-10-04 15:21:24 +00:00
|
|
|
// Copyright 2018 The go-ethereum Authors
|
2016-06-14 16:17:44 +00:00
|
|
|
// This file is part of the go-ethereum library.
|
|
|
|
//
|
|
|
|
// The go-ethereum library is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Lesser General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// The go-ethereum library is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU Lesser General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Lesser General Public License
|
|
|
|
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
2018-11-14 07:03:58 +00:00
|
|
|
package enode
|
2016-06-14 16:17:44 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"crypto/rand"
|
|
|
|
"encoding/binary"
|
2018-11-14 07:03:58 +00:00
|
|
|
"fmt"
|
2019-03-14 06:13:05 +00:00
|
|
|
"net"
|
2016-06-14 16:17:44 +00:00
|
|
|
"os"
|
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/rlp"
|
|
|
|
"github.com/syndtr/goleveldb/leveldb"
|
|
|
|
"github.com/syndtr/goleveldb/leveldb/errors"
|
|
|
|
"github.com/syndtr/goleveldb/leveldb/iterator"
|
|
|
|
"github.com/syndtr/goleveldb/leveldb/opt"
|
|
|
|
"github.com/syndtr/goleveldb/leveldb/storage"
|
|
|
|
"github.com/syndtr/goleveldb/leveldb/util"
|
|
|
|
)
|
|
|
|
|
2018-12-19 10:02:07 +00:00
|
|
|
// Keys in the node database.
|
|
|
|
const (
|
2019-03-14 06:13:05 +00:00
|
|
|
dbVersionKey = "version" // Version of the database to flush if changes
|
|
|
|
dbNodePrefix = "n:" // Identifier to prefix node entries with
|
|
|
|
dbLocalPrefix = "local:"
|
|
|
|
dbDiscoverRoot = "v4"
|
2021-06-28 06:53:50 +00:00
|
|
|
dbDiscv5Root = "v5"
|
2019-03-14 06:13:05 +00:00
|
|
|
|
|
|
|
// These fields are stored per ID and IP, the full key is "n:<ID>:v4:<IP>:findfail".
|
|
|
|
// Use nodeItemKey to create those keys.
|
|
|
|
dbNodeFindFails = "findfail"
|
|
|
|
dbNodePing = "lastping"
|
|
|
|
dbNodePong = "lastpong"
|
|
|
|
dbNodeSeq = "seq"
|
|
|
|
|
|
|
|
// Local information is keyed by ID only, the full key is "local:<ID>:seq".
|
|
|
|
// Use localItemKey to create those keys.
|
|
|
|
dbLocalSeq = "seq"
|
2018-12-19 10:02:07 +00:00
|
|
|
)
|
|
|
|
|
2019-03-14 06:13:05 +00:00
|
|
|
const (
|
2018-12-19 10:02:07 +00:00
|
|
|
dbNodeExpiration = 24 * time.Hour // Time after which an unseen node should be dropped.
|
|
|
|
dbCleanupCycle = time.Hour // Time period for running the expiration task.
|
2019-10-04 15:21:24 +00:00
|
|
|
dbVersion = 9
|
2016-06-14 16:17:44 +00:00
|
|
|
)
|
|
|
|
|
2021-06-28 06:53:50 +00:00
|
|
|
var (
|
|
|
|
errInvalidIP = errors.New("invalid IP")
|
|
|
|
)
|
|
|
|
|
2019-03-14 06:13:05 +00:00
|
|
|
var zeroIP = make(net.IP, 16)
|
|
|
|
|
2018-11-14 07:03:58 +00:00
|
|
|
// DB is the node database, storing previously seen nodes and any collected metadata about
|
|
|
|
// them for QoS purposes.
|
|
|
|
type DB struct {
|
2016-06-14 16:17:44 +00:00
|
|
|
lvl *leveldb.DB // Interface to the database itself
|
|
|
|
runner sync.Once // Ensures we can start at most one expirer
|
|
|
|
quit chan struct{} // Channel to signal the expiring thread to stop
|
|
|
|
}
|
|
|
|
|
2018-11-14 07:03:58 +00:00
|
|
|
// OpenDB opens a node database for storing and retrieving infos about known peers in the
|
|
|
|
// network. If no path is given an in-memory, temporary database is constructed.
|
|
|
|
func OpenDB(path string) (*DB, error) {
|
2016-06-14 16:17:44 +00:00
|
|
|
if path == "" {
|
2018-11-14 07:03:58 +00:00
|
|
|
return newMemoryDB()
|
2016-06-14 16:17:44 +00:00
|
|
|
}
|
2018-11-14 07:03:58 +00:00
|
|
|
return newPersistentDB(path)
|
2016-06-14 16:17:44 +00:00
|
|
|
}
|
|
|
|
|
2018-11-14 07:03:58 +00:00
|
|
|
// newMemoryNodeDB creates a new in-memory node database without a persistent backend.
|
|
|
|
func newMemoryDB() (*DB, error) {
|
2016-06-14 16:17:44 +00:00
|
|
|
db, err := leveldb.Open(storage.NewMemStorage(), nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2018-11-14 07:03:58 +00:00
|
|
|
return &DB{lvl: db, quit: make(chan struct{})}, nil
|
2016-06-14 16:17:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// newPersistentNodeDB creates/opens a leveldb backed persistent node database,
|
|
|
|
// also flushing its contents in case of a version mismatch.
|
2018-11-14 07:03:58 +00:00
|
|
|
func newPersistentDB(path string) (*DB, error) {
|
2016-06-14 16:17:44 +00:00
|
|
|
opts := &opt.Options{OpenFilesCacheCapacity: 5}
|
|
|
|
db, err := leveldb.OpenFile(path, opts)
|
|
|
|
if _, iscorrupted := err.(*errors.ErrCorrupted); iscorrupted {
|
|
|
|
db, err = leveldb.RecoverFile(path, nil)
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
// The nodes contained in the cache correspond to a certain protocol version.
|
|
|
|
// Flush all nodes if the version doesn't match.
|
|
|
|
currentVer := make([]byte, binary.MaxVarintLen64)
|
2018-12-19 10:02:07 +00:00
|
|
|
currentVer = currentVer[:binary.PutVarint(currentVer, int64(dbVersion))]
|
2016-06-14 16:17:44 +00:00
|
|
|
|
2018-12-19 10:02:07 +00:00
|
|
|
blob, err := db.Get([]byte(dbVersionKey), nil)
|
2016-06-14 16:17:44 +00:00
|
|
|
switch err {
|
|
|
|
case leveldb.ErrNotFound:
|
|
|
|
// Version not found (i.e. empty cache), insert it
|
2018-12-19 10:02:07 +00:00
|
|
|
if err := db.Put([]byte(dbVersionKey), currentVer, nil); err != nil {
|
2016-06-14 16:17:44 +00:00
|
|
|
db.Close()
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
case nil:
|
|
|
|
// Version present, flush if different
|
|
|
|
if !bytes.Equal(blob, currentVer) {
|
|
|
|
db.Close()
|
|
|
|
if err = os.RemoveAll(path); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2018-11-14 07:03:58 +00:00
|
|
|
return newPersistentDB(path)
|
2016-06-14 16:17:44 +00:00
|
|
|
}
|
|
|
|
}
|
2018-11-14 07:03:58 +00:00
|
|
|
return &DB{lvl: db, quit: make(chan struct{})}, nil
|
2016-06-14 16:17:44 +00:00
|
|
|
}
|
|
|
|
|
2019-03-14 06:13:05 +00:00
|
|
|
// nodeKey returns the database key for a node record.
|
|
|
|
func nodeKey(id ID) []byte {
|
|
|
|
key := append([]byte(dbNodePrefix), id[:]...)
|
|
|
|
key = append(key, ':')
|
|
|
|
key = append(key, dbDiscoverRoot...)
|
|
|
|
return key
|
2016-06-14 16:17:44 +00:00
|
|
|
}
|
|
|
|
|
2019-03-14 06:13:05 +00:00
|
|
|
// splitNodeKey returns the node ID of a key created by nodeKey.
|
|
|
|
func splitNodeKey(key []byte) (id ID, rest []byte) {
|
|
|
|
if !bytes.HasPrefix(key, []byte(dbNodePrefix)) {
|
|
|
|
return ID{}, nil
|
2016-06-14 16:17:44 +00:00
|
|
|
}
|
2019-03-14 06:13:05 +00:00
|
|
|
item := key[len(dbNodePrefix):]
|
2016-06-14 16:17:44 +00:00
|
|
|
copy(id[:], item[:len(id)])
|
2019-03-14 06:13:05 +00:00
|
|
|
return id, item[len(id)+1:]
|
|
|
|
}
|
2016-06-14 16:17:44 +00:00
|
|
|
|
2019-03-14 06:13:05 +00:00
|
|
|
// nodeItemKey returns the database key for a node metadata field.
|
|
|
|
func nodeItemKey(id ID, ip net.IP, field string) []byte {
|
|
|
|
ip16 := ip.To16()
|
|
|
|
if ip16 == nil {
|
|
|
|
panic(fmt.Errorf("invalid IP (length %d)", len(ip)))
|
|
|
|
}
|
|
|
|
return bytes.Join([][]byte{nodeKey(id), ip16, []byte(field)}, []byte{':'})
|
|
|
|
}
|
|
|
|
|
|
|
|
// splitNodeItemKey returns the components of a key created by nodeItemKey.
|
|
|
|
func splitNodeItemKey(key []byte) (id ID, ip net.IP, field string) {
|
|
|
|
id, key = splitNodeKey(key)
|
|
|
|
// Skip discover root.
|
|
|
|
if string(key) == dbDiscoverRoot {
|
|
|
|
return id, nil, ""
|
|
|
|
}
|
|
|
|
key = key[len(dbDiscoverRoot)+1:]
|
|
|
|
// Split out the IP.
|
2021-06-28 06:53:50 +00:00
|
|
|
ip = key[:16]
|
2019-03-14 06:13:05 +00:00
|
|
|
if ip4 := ip.To4(); ip4 != nil {
|
|
|
|
ip = ip4
|
|
|
|
}
|
|
|
|
key = key[16+1:]
|
|
|
|
// Field is the remainder of key.
|
|
|
|
field = string(key)
|
|
|
|
return id, ip, field
|
|
|
|
}
|
|
|
|
|
2021-06-28 06:53:50 +00:00
|
|
|
func v5Key(id ID, ip net.IP, field string) []byte {
|
|
|
|
return bytes.Join([][]byte{
|
|
|
|
[]byte(dbNodePrefix),
|
|
|
|
id[:],
|
|
|
|
[]byte(dbDiscv5Root),
|
|
|
|
ip.To16(),
|
|
|
|
[]byte(field),
|
|
|
|
}, []byte{':'})
|
|
|
|
}
|
|
|
|
|
2019-03-14 06:13:05 +00:00
|
|
|
// localItemKey returns the key of a local node item.
|
|
|
|
func localItemKey(id ID, field string) []byte {
|
|
|
|
key := append([]byte(dbLocalPrefix), id[:]...)
|
|
|
|
key = append(key, ':')
|
|
|
|
key = append(key, field...)
|
|
|
|
return key
|
2016-06-14 16:17:44 +00:00
|
|
|
}
|
|
|
|
|
2018-12-19 10:02:07 +00:00
|
|
|
// fetchInt64 retrieves an integer associated with a particular key.
|
2018-11-14 07:03:58 +00:00
|
|
|
func (db *DB) fetchInt64(key []byte) int64 {
|
2016-06-14 16:17:44 +00:00
|
|
|
blob, err := db.lvl.Get(key, nil)
|
|
|
|
if err != nil {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
val, read := binary.Varint(blob)
|
|
|
|
if read <= 0 {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
return val
|
|
|
|
}
|
|
|
|
|
2018-12-19 10:02:07 +00:00
|
|
|
// storeInt64 stores an integer in the given key.
|
2018-11-14 07:03:58 +00:00
|
|
|
func (db *DB) storeInt64(key []byte, n int64) error {
|
2016-06-14 16:17:44 +00:00
|
|
|
blob := make([]byte, binary.MaxVarintLen64)
|
|
|
|
blob = blob[:binary.PutVarint(blob, n)]
|
2018-12-19 10:02:07 +00:00
|
|
|
return db.lvl.Put(key, blob, nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
// fetchUint64 retrieves an integer associated with a particular key.
|
|
|
|
func (db *DB) fetchUint64(key []byte) uint64 {
|
|
|
|
blob, err := db.lvl.Get(key, nil)
|
|
|
|
if err != nil {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
val, _ := binary.Uvarint(blob)
|
|
|
|
return val
|
|
|
|
}
|
2016-06-14 16:17:44 +00:00
|
|
|
|
2018-12-19 10:02:07 +00:00
|
|
|
// storeUint64 stores an integer in the given key.
|
|
|
|
func (db *DB) storeUint64(key []byte, n uint64) error {
|
|
|
|
blob := make([]byte, binary.MaxVarintLen64)
|
|
|
|
blob = blob[:binary.PutUvarint(blob, n)]
|
2016-06-14 16:17:44 +00:00
|
|
|
return db.lvl.Put(key, blob, nil)
|
|
|
|
}
|
|
|
|
|
2018-11-14 07:03:58 +00:00
|
|
|
// Node retrieves a node with a given id from the database.
|
|
|
|
func (db *DB) Node(id ID) *Node {
|
2019-03-14 06:13:05 +00:00
|
|
|
blob, err := db.lvl.Get(nodeKey(id), nil)
|
2016-06-14 16:17:44 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
2018-11-14 07:03:58 +00:00
|
|
|
return mustDecodeNode(id[:], blob)
|
|
|
|
}
|
|
|
|
|
|
|
|
func mustDecodeNode(id, data []byte) *Node {
|
2016-06-14 16:17:44 +00:00
|
|
|
node := new(Node)
|
2018-11-14 07:03:58 +00:00
|
|
|
if err := rlp.DecodeBytes(data, &node.r); err != nil {
|
|
|
|
panic(fmt.Errorf("p2p/enode: can't decode node %x in DB: %v", id, err))
|
2016-06-14 16:17:44 +00:00
|
|
|
}
|
2018-11-14 07:03:58 +00:00
|
|
|
// Restore node id cache.
|
|
|
|
copy(node.id[:], id)
|
2016-06-14 16:17:44 +00:00
|
|
|
return node
|
|
|
|
}
|
|
|
|
|
2018-11-14 07:03:58 +00:00
|
|
|
// UpdateNode inserts - potentially overwriting - a node into the peer database.
|
|
|
|
func (db *DB) UpdateNode(node *Node) error {
|
2018-12-19 10:02:07 +00:00
|
|
|
if node.Seq() < db.NodeSeq(node.ID()) {
|
|
|
|
return nil
|
|
|
|
}
|
2018-11-14 07:03:58 +00:00
|
|
|
blob, err := rlp.EncodeToBytes(&node.r)
|
2016-06-14 16:17:44 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-03-14 06:13:05 +00:00
|
|
|
if err := db.lvl.Put(nodeKey(node.ID()), blob, nil); err != nil {
|
2018-12-19 10:02:07 +00:00
|
|
|
return err
|
|
|
|
}
|
2019-03-14 06:13:05 +00:00
|
|
|
return db.storeUint64(nodeItemKey(node.ID(), zeroIP, dbNodeSeq), node.Seq())
|
2018-12-19 10:02:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NodeSeq returns the stored record sequence number of the given node.
|
|
|
|
func (db *DB) NodeSeq(id ID) uint64 {
|
2019-03-14 06:13:05 +00:00
|
|
|
return db.fetchUint64(nodeItemKey(id, zeroIP, dbNodeSeq))
|
2018-12-19 10:02:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Resolve returns the stored record of the node if it has a larger sequence
|
|
|
|
// number than n.
|
|
|
|
func (db *DB) Resolve(n *Node) *Node {
|
|
|
|
if n.Seq() > db.NodeSeq(n.ID()) {
|
|
|
|
return n
|
|
|
|
}
|
|
|
|
return db.Node(n.ID())
|
2016-06-14 16:17:44 +00:00
|
|
|
}
|
|
|
|
|
2019-03-14 06:13:05 +00:00
|
|
|
// DeleteNode deletes all information associated with a node.
|
|
|
|
func (db *DB) DeleteNode(id ID) {
|
|
|
|
deleteRange(db.lvl, nodeKey(id))
|
|
|
|
}
|
|
|
|
|
|
|
|
func deleteRange(db *leveldb.DB, prefix []byte) {
|
|
|
|
it := db.NewIterator(util.BytesPrefix(prefix), nil)
|
|
|
|
defer it.Release()
|
|
|
|
for it.Next() {
|
|
|
|
db.Delete(it.Key(), nil)
|
2016-06-14 16:17:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ensureExpirer is a small helper method ensuring that the data expiration
|
|
|
|
// mechanism is running. If the expiration goroutine is already running, this
|
|
|
|
// method simply returns.
|
|
|
|
//
|
|
|
|
// The goal is to start the data evacuation only after the network successfully
|
|
|
|
// bootstrapped itself (to prevent dumping potentially useful seed nodes). Since
|
|
|
|
// it would require significant overhead to exactly trace the first successful
|
|
|
|
// convergence, it's simpler to "ensure" the correct state when an appropriate
|
|
|
|
// condition occurs (i.e. a successful bonding), and discard further events.
|
2018-11-14 07:03:58 +00:00
|
|
|
func (db *DB) ensureExpirer() {
|
2016-06-14 16:17:44 +00:00
|
|
|
db.runner.Do(func() { go db.expirer() })
|
|
|
|
}
|
|
|
|
|
|
|
|
// expirer should be started in a go routine, and is responsible for looping ad
|
|
|
|
// infinitum and dropping stale data from the database.
|
2018-11-14 07:03:58 +00:00
|
|
|
func (db *DB) expirer() {
|
2018-12-19 10:02:07 +00:00
|
|
|
tick := time.NewTicker(dbCleanupCycle)
|
2018-02-27 10:39:30 +00:00
|
|
|
defer tick.Stop()
|
2016-06-14 16:17:44 +00:00
|
|
|
for {
|
|
|
|
select {
|
2018-02-27 10:39:30 +00:00
|
|
|
case <-tick.C:
|
2019-03-14 06:13:05 +00:00
|
|
|
db.expireNodes()
|
2016-06-14 16:17:44 +00:00
|
|
|
case <-db.quit:
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// expireNodes iterates over the database and deletes all nodes that have not
|
2019-03-14 06:13:05 +00:00
|
|
|
// been seen (i.e. received a pong from) for some time.
|
|
|
|
func (db *DB) expireNodes() {
|
|
|
|
it := db.lvl.NewIterator(util.BytesPrefix([]byte(dbNodePrefix)), nil)
|
2016-06-14 16:17:44 +00:00
|
|
|
defer it.Release()
|
2019-03-14 06:13:05 +00:00
|
|
|
if !it.Next() {
|
|
|
|
return
|
|
|
|
}
|
2016-06-14 16:17:44 +00:00
|
|
|
|
2019-03-14 06:13:05 +00:00
|
|
|
var (
|
|
|
|
threshold = time.Now().Add(-dbNodeExpiration).Unix()
|
|
|
|
youngestPong int64
|
|
|
|
atEnd = false
|
|
|
|
)
|
|
|
|
for !atEnd {
|
|
|
|
id, ip, field := splitNodeItemKey(it.Key())
|
|
|
|
if field == dbNodePong {
|
|
|
|
time, _ := binary.Varint(it.Value())
|
|
|
|
if time > youngestPong {
|
|
|
|
youngestPong = time
|
|
|
|
}
|
|
|
|
if time < threshold {
|
|
|
|
// Last pong from this IP older than threshold, remove fields belonging to it.
|
|
|
|
deleteRange(db.lvl, nodeItemKey(id, ip, ""))
|
|
|
|
}
|
2016-06-14 16:17:44 +00:00
|
|
|
}
|
2019-03-14 06:13:05 +00:00
|
|
|
atEnd = !it.Next()
|
|
|
|
nextID, _ := splitNodeKey(it.Key())
|
|
|
|
if atEnd || nextID != id {
|
|
|
|
// We've moved beyond the last entry of the current ID.
|
|
|
|
// Remove everything if there was no recent enough pong.
|
|
|
|
if youngestPong > 0 && youngestPong < threshold {
|
|
|
|
deleteRange(db.lvl, nodeKey(id))
|
|
|
|
}
|
|
|
|
youngestPong = 0
|
2016-06-14 16:17:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-14 07:03:58 +00:00
|
|
|
// LastPingReceived retrieves the time of the last ping packet received from
|
|
|
|
// a remote node.
|
2019-03-14 06:13:05 +00:00
|
|
|
func (db *DB) LastPingReceived(id ID, ip net.IP) time.Time {
|
2021-06-28 06:53:50 +00:00
|
|
|
if ip = ip.To16(); ip == nil {
|
|
|
|
return time.Time{}
|
|
|
|
}
|
2019-03-14 06:13:05 +00:00
|
|
|
return time.Unix(db.fetchInt64(nodeItemKey(id, ip, dbNodePing)), 0)
|
2016-06-14 16:17:44 +00:00
|
|
|
}
|
|
|
|
|
2018-11-14 07:03:58 +00:00
|
|
|
// UpdateLastPingReceived updates the last time we tried contacting a remote node.
|
2019-03-14 06:13:05 +00:00
|
|
|
func (db *DB) UpdateLastPingReceived(id ID, ip net.IP, instance time.Time) error {
|
2021-06-28 06:53:50 +00:00
|
|
|
if ip = ip.To16(); ip == nil {
|
|
|
|
return errInvalidIP
|
|
|
|
}
|
2019-03-14 06:13:05 +00:00
|
|
|
return db.storeInt64(nodeItemKey(id, ip, dbNodePing), instance.Unix())
|
2016-06-14 16:17:44 +00:00
|
|
|
}
|
|
|
|
|
2018-11-14 07:03:58 +00:00
|
|
|
// LastPongReceived retrieves the time of the last successful pong from remote node.
|
2019-03-14 06:13:05 +00:00
|
|
|
func (db *DB) LastPongReceived(id ID, ip net.IP) time.Time {
|
2021-06-28 06:53:50 +00:00
|
|
|
if ip = ip.To16(); ip == nil {
|
|
|
|
return time.Time{}
|
|
|
|
}
|
2018-11-14 07:03:58 +00:00
|
|
|
// Launch expirer
|
|
|
|
db.ensureExpirer()
|
2019-03-14 06:13:05 +00:00
|
|
|
return time.Unix(db.fetchInt64(nodeItemKey(id, ip, dbNodePong)), 0)
|
2016-06-14 16:17:44 +00:00
|
|
|
}
|
|
|
|
|
2018-11-14 07:03:58 +00:00
|
|
|
// UpdateLastPongReceived updates the last pong time of a node.
|
2019-03-14 06:13:05 +00:00
|
|
|
func (db *DB) UpdateLastPongReceived(id ID, ip net.IP, instance time.Time) error {
|
2021-06-28 06:53:50 +00:00
|
|
|
if ip = ip.To16(); ip == nil {
|
|
|
|
return errInvalidIP
|
|
|
|
}
|
2019-03-14 06:13:05 +00:00
|
|
|
return db.storeInt64(nodeItemKey(id, ip, dbNodePong), instance.Unix())
|
2016-06-14 16:17:44 +00:00
|
|
|
}
|
|
|
|
|
2018-11-14 07:03:58 +00:00
|
|
|
// FindFails retrieves the number of findnode failures since bonding.
|
2019-03-14 06:13:05 +00:00
|
|
|
func (db *DB) FindFails(id ID, ip net.IP) int {
|
2021-06-28 06:53:50 +00:00
|
|
|
if ip = ip.To16(); ip == nil {
|
|
|
|
return 0
|
|
|
|
}
|
2019-03-14 06:13:05 +00:00
|
|
|
return int(db.fetchInt64(nodeItemKey(id, ip, dbNodeFindFails)))
|
2016-06-14 16:17:44 +00:00
|
|
|
}
|
|
|
|
|
2018-11-14 07:03:58 +00:00
|
|
|
// UpdateFindFails updates the number of findnode failures since bonding.
|
2019-03-14 06:13:05 +00:00
|
|
|
func (db *DB) UpdateFindFails(id ID, ip net.IP, fails int) error {
|
2021-06-28 06:53:50 +00:00
|
|
|
if ip = ip.To16(); ip == nil {
|
|
|
|
return errInvalidIP
|
|
|
|
}
|
2019-03-14 06:13:05 +00:00
|
|
|
return db.storeInt64(nodeItemKey(id, ip, dbNodeFindFails), int64(fails))
|
2018-12-19 10:02:07 +00:00
|
|
|
}
|
|
|
|
|
2021-06-28 06:53:50 +00:00
|
|
|
// FindFailsV5 retrieves the discv5 findnode failure counter.
|
|
|
|
func (db *DB) FindFailsV5(id ID, ip net.IP) int {
|
|
|
|
if ip = ip.To16(); ip == nil {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
return int(db.fetchInt64(v5Key(id, ip, dbNodeFindFails)))
|
|
|
|
}
|
|
|
|
|
|
|
|
// UpdateFindFailsV5 stores the discv5 findnode failure counter.
|
|
|
|
func (db *DB) UpdateFindFailsV5(id ID, ip net.IP, fails int) error {
|
|
|
|
if ip = ip.To16(); ip == nil {
|
|
|
|
return errInvalidIP
|
|
|
|
}
|
|
|
|
return db.storeInt64(v5Key(id, ip, dbNodeFindFails), int64(fails))
|
|
|
|
}
|
|
|
|
|
2018-12-19 10:02:07 +00:00
|
|
|
// LocalSeq retrieves the local record sequence counter.
|
|
|
|
func (db *DB) localSeq(id ID) uint64 {
|
2019-10-04 15:21:24 +00:00
|
|
|
return db.fetchUint64(localItemKey(id, dbLocalSeq))
|
2018-12-19 10:02:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// storeLocalSeq stores the local record sequence counter.
|
|
|
|
func (db *DB) storeLocalSeq(id ID, n uint64) {
|
2019-10-04 15:21:24 +00:00
|
|
|
db.storeUint64(localItemKey(id, dbLocalSeq), n)
|
2016-06-14 16:17:44 +00:00
|
|
|
}
|
|
|
|
|
2018-11-14 07:03:58 +00:00
|
|
|
// QuerySeeds retrieves random nodes to be used as potential seed nodes
|
2016-06-14 16:17:44 +00:00
|
|
|
// for bootstrapping.
|
2018-11-14 07:03:58 +00:00
|
|
|
func (db *DB) QuerySeeds(n int, maxAge time.Duration) []*Node {
|
2016-06-14 16:17:44 +00:00
|
|
|
var (
|
|
|
|
now = time.Now()
|
|
|
|
nodes = make([]*Node, 0, n)
|
|
|
|
it = db.lvl.NewIterator(nil, nil)
|
2018-11-14 07:03:58 +00:00
|
|
|
id ID
|
2016-06-14 16:17:44 +00:00
|
|
|
)
|
|
|
|
defer it.Release()
|
|
|
|
|
|
|
|
seek:
|
|
|
|
for seeks := 0; len(nodes) < n && seeks < n*5; seeks++ {
|
|
|
|
// Seek to a random entry. The first byte is incremented by a
|
|
|
|
// random amount each time in order to increase the likelihood
|
|
|
|
// of hitting all existing nodes in very small databases.
|
|
|
|
ctr := id[0]
|
|
|
|
rand.Read(id[:])
|
|
|
|
id[0] = ctr + id[0]%16
|
2019-03-14 06:13:05 +00:00
|
|
|
it.Seek(nodeKey(id))
|
2016-06-14 16:17:44 +00:00
|
|
|
|
|
|
|
n := nextNode(it)
|
|
|
|
if n == nil {
|
|
|
|
id[0] = 0
|
|
|
|
continue seek // iterator exhausted
|
|
|
|
}
|
2019-03-14 06:13:05 +00:00
|
|
|
if now.Sub(db.LastPongReceived(n.ID(), n.IP())) > maxAge {
|
2016-06-14 16:17:44 +00:00
|
|
|
continue seek
|
|
|
|
}
|
|
|
|
for i := range nodes {
|
2018-11-14 07:03:58 +00:00
|
|
|
if nodes[i].ID() == n.ID() {
|
2016-06-14 16:17:44 +00:00
|
|
|
continue seek // duplicate
|
|
|
|
}
|
|
|
|
}
|
|
|
|
nodes = append(nodes, n)
|
|
|
|
}
|
|
|
|
return nodes
|
|
|
|
}
|
|
|
|
|
|
|
|
// reads the next node record from the iterator, skipping over other
|
|
|
|
// database entries.
|
|
|
|
func nextNode(it iterator.Iterator) *Node {
|
|
|
|
for end := false; !end; end = !it.Next() {
|
2019-03-14 06:13:05 +00:00
|
|
|
id, rest := splitNodeKey(it.Key())
|
|
|
|
if string(rest) != dbDiscoverRoot {
|
2016-06-14 16:17:44 +00:00
|
|
|
continue
|
|
|
|
}
|
2018-11-14 07:03:58 +00:00
|
|
|
return mustDecodeNode(id[:], it.Value())
|
2016-06-14 16:17:44 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// close flushes and closes the database files.
|
2018-11-14 07:03:58 +00:00
|
|
|
func (db *DB) Close() {
|
2016-06-14 16:17:44 +00:00
|
|
|
close(db.quit)
|
|
|
|
db.lvl.Close()
|
|
|
|
}
|