// 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)
},
"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
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',
`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;
}
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 {
type Node struct {
Alias string
+ Xpub string
PublicKey string
Host string
Port uint16
import (
// "encoding/binary"
+ // "encoding/hex"
// "io/ioutil"
"fmt"
"os"
- "strings"
+ // "strings"
"time"
"github.com/jinzhu/gorm"
// 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"
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?
}
// 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() {
// 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
}
}