OSDN Git Service

clean
authorHAOYUatHZ <haoyu@protonmail.com>
Mon, 12 Aug 2019 10:31:47 +0000 (18:31 +0800)
committerHAOYUatHZ <haoyu@protonmail.com>
Mon, 12 Aug 2019 10:31:47 +0000 (18:31 +0800)
crypto/ed25519/ed25519.go
docs/precog/config_example.json
docs/precog/sql_dump/precog_schema.sql
toolbar/precog/config/config.go
toolbar/precog/database/orm/node.go
toolbar/precog/monitor/monitor.go

index 530b08b..aa22b5b 100644 (file)
@@ -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)
index eb2b6c1..fa4ed40 100644 (file)
     },
     "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
index 64bc852..36d5263 100644 (file)
@@ -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;
index 753bac3..6dc69b6 100644 (file)
@@ -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 {
index 20882d9..151ad56 100644 (file)
@@ -6,6 +6,7 @@ import (
 
 type Node struct {
        Alias                    string
+       Xpub                     string
        PublicKey                string
        Host                     string
        Port                     uint16
index a77ae0f..f4555d0 100644 (file)
@@ -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
        }
 }