Skip to content

Commit

Permalink
cmd/bootnode, eth, p2p, p2p/discover: clean up the seeder and mesh in…
Browse files Browse the repository at this point in the history
…to eth.
  • Loading branch information
karalabe committed Apr 24, 2015
1 parent 971702e commit 6def110
Show file tree
Hide file tree
Showing 9 changed files with 168 additions and 144 deletions.
2 changes: 1 addition & 1 deletion cmd/bootnode/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ func main() {
}
}

if _, err := discover.ListenUDP(nodeKey, *listenAddr, natm, ""); err != nil {
if _, err := discover.ListenUDP(nodeKey, *listenAddr, natm, nil); err != nil {
log.Fatal(err)
}
select {}
Expand Down
13 changes: 10 additions & 3 deletions eth/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,8 @@ type Ethereum struct {
blockDb common.Database // Block chain database
stateDb common.Database // State changes database
extraDb common.Database // Extra database (txs, etc)
seedDb *discover.Cache // Peer database seeding the bootstrap

// Closed when databases are flushed and closed
databasesClosed chan bool

Expand Down Expand Up @@ -179,7 +181,10 @@ func New(config *Config) (*Ethereum, error) {
if err != nil {
return nil, err
}
seedDbPath := path.Join(config.DataDir, "seeds")
seedDb, err := discover.NewPersistentCache(path.Join(config.DataDir, "seeds"))
if err != nil {
return nil, err
}

// Perform database sanity checks
d, _ := blockDb.Get([]byte("ProtocolVersion"))
Expand Down Expand Up @@ -207,6 +212,7 @@ func New(config *Config) (*Ethereum, error) {
blockDb: blockDb,
stateDb: stateDb,
extraDb: extraDb,
seedDb: seedDb,
eventMux: &event.TypeMux{},
accountManager: config.AccountManager,
DataDir: config.DataDir,
Expand Down Expand Up @@ -244,7 +250,7 @@ func New(config *Config) (*Ethereum, error) {
NAT: config.NAT,
NoDial: !config.Dial,
BootstrapNodes: config.parseBootNodes(),
SeedCache: seedDbPath,
SeedCache: seedDb,
}
if len(config.Port) > 0 {
eth.net.ListenAddr = ":" + config.Port
Expand Down Expand Up @@ -423,6 +429,7 @@ done:
}
}

s.seedDb.Close()
s.blockDb.Close()
s.stateDb.Close()
s.extraDb.Close()
Expand Down Expand Up @@ -450,7 +457,7 @@ func (self *Ethereum) SuggestPeer(nodeURL string) error {
}

func (s *Ethereum) Stop() {
s.txSub.Unsubscribe() // quits txBroadcastLoop
s.txSub.Unsubscribe() // quits txBroadcastLoop

s.protocolManager.Stop()
s.txPool.Stop()
Expand Down
134 changes: 134 additions & 0 deletions p2p/discover/cache.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
// Contains the discovery cache, storing previously seen nodes to act as seed
// servers during bootstrapping the network.

package discover

import (
"bytes"
"encoding/binary"
"net"
"os"

"github.com/ethereum/go-ethereum/rlp"
"github.com/syndtr/goleveldb/leveldb"
"github.com/syndtr/goleveldb/leveldb/storage"
)

// Cache stores all nodes we know about.
type Cache struct {
db *leveldb.DB
}

// Cache version to allow dumping old data if it changes.
var cacheVersionKey = []byte("pv")

// NewMemoryCache creates a new in-memory peer cache without a persistent backend.
func NewMemoryCache() (*Cache, error) {
db, err := leveldb.Open(storage.NewMemStorage(), nil)
if err != nil {
return nil, err
}
return &Cache{db: db}, nil
}

// NewPersistentCache creates/opens a leveldb backed persistent peer cache, also
// flushing its contents in case of a version mismatch.
func NewPersistentCache(path string) (*Cache, error) {
// Try to open the cache, recovering any corruption
db, err := leveldb.OpenFile(path, nil)
if _, iscorrupted := err.(leveldb.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)
currentVer = currentVer[:binary.PutVarint(currentVer, Version)]

blob, err := db.Get(cacheVersionKey, nil)
switch err {
case leveldb.ErrNotFound:
// Version not found (i.e. empty cache), insert it
err = db.Put(cacheVersionKey, currentVer, nil)

case nil:
// Version present, flush if different
if !bytes.Equal(blob, currentVer) {
db.Close()
if err = os.RemoveAll(path); err != nil {
return nil, err
}
return NewPersistentCache(path)
}
}
// Clean up in case of an error
if err != nil {
db.Close()
return nil, err
}
return &Cache{db: db}, nil
}

// get retrieves a node with a given id from the seed da
func (c *Cache) get(id NodeID) *Node {
blob, err := c.db.Get(id[:], nil)
if err != nil {
return nil
}
node := new(Node)
if err := rlp.DecodeBytes(blob, node); err != nil {
return nil
}
return node
}

// list retrieves a batch of nodes from the database.
func (c *Cache) list(n int) []*Node {
it := c.db.NewIterator(nil, nil)
defer it.Release()

nodes := make([]*Node, 0, n)
for i := 0; i < n && it.Next(); i++ {
var id NodeID
copy(id[:], it.Key())

if node := c.get(id); node != nil {
nodes = append(nodes, node)
}
}
return nodes
}

// update inserts - potentially overwriting - a node in the seed database.
func (c *Cache) update(node *Node) error {
blob, err := rlp.EncodeToBytes(node)
if err != nil {
return err
}
return c.db.Put(node.ID[:], blob, nil)
}

// add inserts a new node into the seed database.
func (c *Cache) add(id NodeID, addr *net.UDPAddr, tcpPort uint16) *Node {
node := &Node{
ID: id,
IP: addr.IP,
DiscPort: addr.Port,
TCPPort: int(tcpPort),
}
c.update(node)

return node
}

// delete removes a node from the database.
func (c *Cache) delete(id NodeID) error {
return c.db.Delete(id[:], nil)
}

// Close flushes and closes the database files.
func (c *Cache) Close() {
c.db.Close()
}
114 changes: 0 additions & 114 deletions p2p/discover/node.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
package discover

import (
"bytes"
"crypto/ecdsa"
"crypto/elliptic"
"encoding/binary"
"encoding/hex"
"errors"
"fmt"
Expand All @@ -13,16 +11,12 @@ import (
"math/rand"
"net"
"net/url"
"os"
"strconv"
"strings"

"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/crypto/secp256k1"
"github.com/ethereum/go-ethereum/rlp"
"github.com/syndtr/goleveldb/leveldb"
"github.com/syndtr/goleveldb/leveldb/opt"
"github.com/syndtr/goleveldb/leveldb/storage"
)

const nodeIDBits = 512
Expand Down Expand Up @@ -310,111 +304,3 @@ func randomID(a NodeID, n int) (b NodeID) {
}
return b
}

// nodeDB stores all nodes we know about.
type nodeDB struct {
ldb *leveldb.DB
}

var dbVersionKey = []byte("pv")

// Opens the backing LevelDB. If path is "", we use an in-memory database.
func newNodeDB(path string, version int64) (db *nodeDB, err error) {
db = new(nodeDB)
opts := new(opt.Options)
if path == "" {
db.ldb, err = leveldb.Open(storage.NewMemStorage(), opts)
} else {
db.ldb, err = openNodeDB(path, opts, version)
}
return db, err
}

// openNodeDB opens a persistent seed cache, flushing old versions.
func openNodeDB(path string, opts *opt.Options, version int64) (*leveldb.DB, error) {
ldb, err := leveldb.OpenFile(path, opts)
if _, iscorrupted := err.(leveldb.ErrCorrupted); iscorrupted {
ldb, err = leveldb.RecoverFile(path, opts)
}
if err != nil {
return nil, err
}
// The nodes contained in the database correspond to a certain
// protocol version. Flush all nodes if the DB version doesn't match.
// There is no need to do this for memory databases because they
// won't ever be used with a different protocol version.
shouldVal := make([]byte, binary.MaxVarintLen64)
shouldVal = shouldVal[:binary.PutVarint(shouldVal, version)]
val, err := ldb.Get(dbVersionKey, nil)
if err == leveldb.ErrNotFound {
err = ldb.Put(dbVersionKey, shouldVal, nil)
} else if err == nil && !bytes.Equal(val, shouldVal) {
// Delete and start over.
ldb.Close()
if err = os.RemoveAll(path); err != nil {
return nil, err
}
return openNodeDB(path, opts, version)
}
if err != nil {
ldb.Close()
ldb = nil
}
return ldb, err
}

// get retrieves a node with a given id from the seed da
func (db *nodeDB) get(id NodeID) *Node {
v, err := db.ldb.Get(id[:], nil)
if err != nil {
return nil
}
n := new(Node)
if err := rlp.DecodeBytes(v, n); err != nil {
return nil
}
return n
}

// list retrieves a batch of nodes from the database.
func (db *nodeDB) list(n int) []*Node {
it := db.ldb.NewIterator(nil, nil)
defer it.Release()

nodes := make([]*Node, 0, n)
for i := 0; i < n && it.Next(); i++ {
var id NodeID
copy(id[:], it.Key())

if node := db.get(id); node != nil {
nodes = append(nodes, node)
}
}
return nodes
}

// update inserts - potentially overwriting - a node in the seed database.
func (db *nodeDB) update(n *Node) error {
v, err := rlp.EncodeToBytes(n)
if err != nil {
return err
}
return db.ldb.Put(n.ID[:], v, nil)
}

// add inserts a new node into the seed database.
func (db *nodeDB) add(id NodeID, addr *net.UDPAddr, tcpPort uint16) *Node {
n := &Node{ID: id, IP: addr.IP, DiscPort: addr.Port, TCPPort: int(tcpPort)}
db.update(n)
return n
}

// delete removes a node from the database.
func (db *nodeDB) delete(id NodeID) error {
return db.ldb.Delete(id[:], nil)
}

// close flushes and closes the database files.
func (db *nodeDB) close() {
db.ldb.Close()
}
Loading

0 comments on commit 6def110

Please sign in to comment.