Skip to content

Instantly share code, notes, and snippets.

@ryupold
Created December 11, 2017 20:42
Show Gist options
  • Save ryupold/f07569b411f5ab8a04a37f629b2440f9 to your computer and use it in GitHub Desktop.
Save ryupold/f07569b411f5ab8a04a37f629b2440f9 to your computer and use it in GitHub Desktop.
check sync status of an IOTA full node
//USAGE: go run iota_sync.go http://IP:PORT
package main
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"os"
"time"
)
func main() {
var lastIndex int64
for {
info, err := getNodeInfo(os.Args[1])
if err != nil {
fmt.Printf("error: %s\n", err)
} else if info != nil {
newIndex := info.SubtanlgeIndex
percent := 100 * float64(info.SubtanlgeIndex) / float64(info.LatestIndex)
if lastIndex != newIndex {
lastIndex = newIndex
fmt.Printf("synced: %f%%\ttips: %d\tindex: %d/%d\n", percent, info.Tips, info.SubtanlgeIndex, info.LatestIndex)
}
}
time.Sleep(time.Second * 60)
}
}
func try(err error) {
if err != nil {
panic(err)
}
}
func getNodeInfo(url string) (result *nodeInfo, rekt error) {
defer func() {
rekt := recover()
if rekt != nil {
return
}
return
}()
client := http.Client{}
req, err := http.NewRequest("POST", url, bytes.NewBuffer([]byte("{\"command\": \"getNodeInfo\"}")))
try(err)
req.Header.Add("X-IOTA-API-VERSION", "1.4")
req.Header.Add("Content-Type", "application/json")
res, err := client.Do(req)
try(err)
data, err := ioutil.ReadAll(res.Body)
try(err)
var info nodeInfo
err = json.Unmarshal(data, &info)
try(err)
result = &info
return
}
type nodeInfo struct {
Name string `json:"appName"`
AppVersion string `json:"appVersion"`
Neighbors int `json:"neighbors"`
SubtanlgeIndex int64 `json:"latestSolidSubtangleMilestoneIndex"`
LatestIndex int64 `json:"latestMilestoneIndex"`
Tips int64 `json:"tips"`
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment