NodeUnknownStatus uint8 = iota
NodeHealthyStatus
NodeCongestedStatus
- NodeBusyStatus
+ NodeOrphanStatus
NodeOfflineStatus
)
NodeUnknownStatus: "unknown",
NodeHealthyStatus: "healthy",
NodeCongestedStatus: "congested",
- NodeBusyStatus: "busy",
+ NodeOrphanStatus: "orphan",
NodeOfflineStatus: "offline",
}
type Node struct {
Alias string
- PubKey chainkd.XPub
+ PubKey string
Host string
Port uint16
BestHeight uint64
func (n *Node) MarshalJSON() ([]byte, error) {
status := common.StatusMap[n.Status]
- var lantency uint64
+ var lantency int64
var activeMinutes uint64
- if n.Status != common.NodeOfflineStatus {
- lantency = n.LantencyMS
+ switch n.Status {
+ case common.NodeHealthyStatus, common.NodeCongestedStatus, common.NodeOrphanStatus:
+ lantency = n.LantencyMS.Int64
activeMinutes = uint64(time.Since(n.ActiveBeginTime).Minutes())
}
return json.Marshal(&struct {
- Alias string `json:"alias"`
- PubKey chainkd.XPub `json:"pubkey"`
- Host string `json:"host"`
- Port uint16 `json:"port"`
- BestHeight uint64 `json:"best_height"`
- LantencyMS uint64 `json:"lantency_ms,omitempty"`
- ActiveMinutes uint64 `json:"active_minutes,omitempty"`
- Status string `json:"status"`
+ Alias string `json:"alias"`
+ PubKey string `json:"pubkey"`
+ Host string `json:"host"`
+ Port uint16 `json:"port"`
+ BestHeight uint64 `json:"best_height"`
+ LantencyMS int64 `json:"lantency_ms,omitempty"`
+ ActiveMinutes uint64 `json:"active_minutes,omitempty"`
+ Status string `json:"status"`
}{
Alias: n.Alias,
PubKey: n.PubKey,
log "github.com/sirupsen/logrus"
"github.com/vapor/toolbar/precog/config"
+ "github.com/vapor/toolbar/precog/database/orm"
)
type monitor struct {
func (m *monitor) Run() {
if err := m.updateBootstrapNodes(); err != nil {
- log.Fatal(err)
+ log.Error(err)
}
+ go m.discovery()
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() error {
- // TODO: updated existed nodes
- // TODO: add new nodes
+ for _, node := range m.cfg.Nodes {
+ ormNode := &orm.Node{
+ PubKey: node.PubKey.String(),
+ Alias: node.Alias,
+ Host: node.Host,
+ Port: node.Port,
+ }
+ if err := m.db.Where(&orm.Node{PubKey: ormNode.PubKey}).
+ Assign(&orm.Node{
+ Alias: node.Alias,
+ Host: node.Host,
+ Port: node.Port,
+ }).FirstOrCreate(ormNode).Error; err != nil {
+ return err
+ }
+ }
+
return nil
}
+// TODO:
+func (m *monitor) discovery() {
+}
+
func (m *monitor) monitorRountine() error {
// TODO: dail, get lantency & best_height
// TODO: decide check_height("best best_height" - "confirmations")