From: HAOYUatHZ Date: Mon, 12 Aug 2019 10:31:47 +0000 (+0800) Subject: clean X-Git-Url: http://git.osdn.net/view?a=commitdiff_plain;h=ecce89f830e4a14a5c87ff36c04ba4f92d9f77a5;hp=bcedca75279eaa7021b347f7fef162f0d9a451fd;p=bytom%2Fvapor.git clean --- diff --git a/crypto/ed25519/ed25519.go b/crypto/ed25519/ed25519.go index 530b08b3..aa22b5b9 100644 --- a/crypto/ed25519/ed25519.go +++ b/crypto/ed25519/ed25519.go @@ -36,6 +36,10 @@ type PublicKey []byte // PrivateKey is the type of Ed25519 private keys. It implements crypto.Signer. type PrivateKey []byte +func (pub PublicKey) String() string { + return hex.EncodeToString(pub) +} + // Public returns the PublicKey corresponding to priv. func (priv PrivateKey) Public() PublicKey { publicKey := make([]byte, PublicKeySize) diff --git a/docs/precog/config_example.json b/docs/precog/config_example.json index eb2b6c16..fa4ed404 100644 --- a/docs/precog/config_example.json +++ b/docs/precog/config_example.json @@ -12,35 +12,5 @@ }, "check_frequency_seconds" : 1, "bootstrap_nodes" : [ - { - "alias" : "matpool", - "public_key" : "0f8669abbd3cc0a167156188e428f940088d5b2f36bb3449df71d2bdc5e077814ea3f68628eef279ed435f51ee26cff00f8bd28fabfd500bedb2a9e369f5c825", - "host": "vapornode.matpool.io", - "port": 56656 - }, - { - "alias" : "seed1", - "public_key" : "1f8669abbd3cc0a167156188e428f940088d5b2f36bb3449df71d2bdc5e077814ea3f68628eef279ed435f51ee26cff00f8bd28fabfd500bedb2a9e369f5c825", - "host": "47.103.79.68", - "port": 56656 - }, - { - "alias" : "seed2", - "public_key" : "2f8669abbd3cc0a167156188e428f940088d5b2f36bb3449df71d2bdc5e077814ea3f68628eef279ed435f51ee26cff00f8bd28fabfd500bedb2a9e369f5c825", - "host": "47.103.13.86", - "port": 56656 - }, - { - "alias" : "seed3", - "public_key" : "3f8669abbd3cc0a167156188e428f940088d5b2f36bb3449df71d2bdc5e077814ea3f68628eef279ed435f51ee26cff00f8bd28fabfd500bedb2a9e369f5c825", - "host": "47.102.193.119", - "port": 56656 - }, - { - "alias" : "seed4", - "public_key" : "4f8669abbd3cc0a167156188e428f940088d5b2f36bb3449df71d2bdc5e077814ea3f68628eef279ed435f51ee26cff00f8bd28fabfd500bedb2a9e369f5c825", - "host": "47.103.17.22", - "port": 56656 - } ] } \ No newline at end of file diff --git a/docs/precog/sql_dump/precog_schema.sql b/docs/precog/sql_dump/precog_schema.sql index 64bc852e..36d52632 100644 --- a/docs/precog/sql_dump/precog_schema.sql +++ b/docs/precog/sql_dump/precog_schema.sql @@ -18,7 +18,8 @@ USE `precog`; CREATE TABLE `nodes` ( `id` int(11) NOT NULL AUTO_INCREMENT, `alias` varchar(128) NOT NULL DEFAULT '', - `public_key` char(128) NOT NULL DEFAULT '', + `xpub` char(128) NOT NULL DEFAULT '', + `public_key` char(64) NOT NULL DEFAULT '', `host` varchar(128) NOT NULL DEFAULT '', `port` smallint unsigned NOT NULL DEFAULT '0', `best_height` int(11) DEFAULT '0', @@ -26,8 +27,7 @@ CREATE TABLE `nodes` ( `created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, `updated_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, PRIMARY KEY (`id`), - UNIQUE KEY `public_key` (`public_key`), - UNIQUE KEY `host_port` (`host`,`port`) + UNIQUE KEY `public_key` (`public_key`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8; LOCK TABLES `nodes` WRITE; diff --git a/toolbar/precog/config/config.go b/toolbar/precog/config/config.go index 753bac3d..6dc69b60 100644 --- a/toolbar/precog/config/config.go +++ b/toolbar/precog/config/config.go @@ -48,10 +48,11 @@ type Policy struct { } type Node struct { - Alias string `json:"alias"` - PublicKey chainkd.XPub `json:"public_key"` - Host string `json:"host"` - Port uint16 `json:"port"` + Alias string `json:"alias"` + XPub *chainkd.XPub `json:"xpub"` + PublicKey string `json:"public_key"` + Host string `json:"host"` + Port uint16 `json:"port"` } type API struct { diff --git a/toolbar/precog/database/orm/node.go b/toolbar/precog/database/orm/node.go index 20882d9a..151ad565 100644 --- a/toolbar/precog/database/orm/node.go +++ b/toolbar/precog/database/orm/node.go @@ -6,6 +6,7 @@ import ( type Node struct { Alias string + Xpub string PublicKey string Host string Port uint16 diff --git a/toolbar/precog/monitor/monitor.go b/toolbar/precog/monitor/monitor.go index a77ae0f4..f4555d0c 100644 --- a/toolbar/precog/monitor/monitor.go +++ b/toolbar/precog/monitor/monitor.go @@ -2,10 +2,11 @@ package monitor import ( // "encoding/binary" + // "encoding/hex" // "io/ioutil" "fmt" "os" - "strings" + // "strings" "time" "github.com/jinzhu/gorm" @@ -13,6 +14,7 @@ import ( // dbm "github.com/vapor/database/leveldb" vaporCfg "github.com/vapor/config" + // "github.com/vapor/crypto/ed25519/chainkd" "github.com/vapor/p2p" // conn "github.com/vapor/p2p/connection" // "github.com/vapor/consensus" @@ -57,9 +59,13 @@ func NewMonitor(cfg *config.Config, db *gorm.DB) *monitor { func (m *monitor) Run() { defer os.RemoveAll(m.nodeCfg.DBPath) - m.updateBootstrapNodes() + for _, node := range m.cfg.Nodes { + m.upSertNode(&node) + } + go m.discovery() go m.collectDiscv() + ticker := time.NewTicker(time.Duration(m.cfg.CheckFreqSeconds) * time.Second) for ; true; <-ticker.C { // TODO: lock? @@ -68,28 +74,31 @@ func (m *monitor) Run() { } // create or update: https://github.com/jinzhu/gorm/issues/1307 -func (m *monitor) updateBootstrapNodes() { - var seeds []string - for _, node := range m.cfg.Nodes { - ormNode := &orm.Node{ - PublicKey: node.PublicKey.String(), - Alias: node.Alias, - Host: node.Host, - Port: node.Port, - } - seeds = append(seeds, fmt.Sprintf("%s:%d", node.Host, node.Port)) - - if err := m.db.Where(&orm.Node{PublicKey: ormNode.PublicKey}). - Assign(&orm.Node{ - Alias: node.Alias, - Host: node.Host, - Port: node.Port, - }).FirstOrCreate(ormNode).Error; err != nil { - log.Error(err) - continue - } +func (m *monitor) upSertNode(node *config.Node) error { + if node.XPub != nil { + node.PublicKey = fmt.Sprintf("%v", node.XPub.PublicKey().String()) } - m.nodeCfg.P2P.Seeds = strings.Join(seeds, ",") + + ormNode := &orm.Node{PublicKey: node.PublicKey} + if err := m.db.Where(&orm.Node{PublicKey: node.PublicKey}).First(ormNode).Error; err != nil && err != gorm.ErrRecordNotFound { + return err + } + + if node.Alias != "" { + ormNode.Alias = node.Alias + } + if node.XPub != nil { + ormNode.Xpub = node.XPub.String() + } + ormNode.Host = node.Host + ormNode.Port = node.Port + return m.db.Where(&orm.Node{PublicKey: ormNode.PublicKey}). + Assign(&orm.Node{ + Xpub: ormNode.Xpub, + Alias: ormNode.Alias, + Host: ormNode.Host, + Port: ormNode.Port, + }).FirstOrCreate(ormNode).Error } func (m *monitor) discovery() { @@ -116,8 +125,20 @@ func (m *monitor) discovery() { // whatz the pubKey? func (m *monitor) collectDiscv() { + // nodeMap maps a node's public key to the node itself + nodeMap := make(map[string]*dht.Node) for node := range m.discvCh { - log.Info(node) + if n, ok := nodeMap[node.ID.String()]; ok && n.String() == node.String() { + continue + } + log.Info("discover new node: ", node) + + m.upSertNode(&config.Node{ + PublicKey: node.ID.String(), + Host: node.IP.String(), + Port: node.TCP, + }) + nodeMap[node.ID.String()] = node } }