Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use checkpoints + hnsd dns api #32

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions internal/config/debug.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,15 @@ type Debugger struct {
checkSynced func() bool

blockHeight uint64
progress float64

lastPing time.Time
sync.RWMutex
}

type DebugInfo struct {
BlockHeight uint64 `json:"blockHeight"`
Progress float64 `json:"progress"`
ProbeURL string `json:"proxyProbeUrl"`
ProbeReached bool `json:"proxyProbeReached"`
Syncing bool `json:"syncing"`
Expand Down Expand Up @@ -122,11 +124,12 @@ func exchangeWithRetry(m *dns.Msg, addrs []string) (r *dns.Msg, rtt time.Duratio
return
}

func (d *Debugger) SetBlockHeight(h uint64) {
func (d *Debugger) SetChainInfo(height uint64, progress float64) {
d.Lock()
defer d.Unlock()

d.blockHeight = h
d.blockHeight = height
d.progress = progress
}

func (d *Debugger) Ping() {
Expand Down Expand Up @@ -183,6 +186,7 @@ func (d *Debugger) GetInfo() DebugInfo {
}
return DebugInfo{
BlockHeight: d.blockHeight,
Progress: d.progress,
ProbeURL: "http://" + d.proxyProbeDomain,
ProbeReached: d.proxyProbeReached,
Syncing: d.checkSynced != nil && !d.checkSynced(),
Expand Down
5 changes: 3 additions & 2 deletions internal/config/pages/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@
}

td:nth-child(2) {
width: 90px;
width: 140px;
}

</style>
Expand Down Expand Up @@ -214,7 +214,8 @@ <h1>Fingertip</h1>
probeChecks = 0;
}

handshakeStatus.innerHTML = data.syncing ? "<span class='warning'>Syncing ...</span>" :
const progress = Math.round(data.progress * 100 * 100) / 100;
handshakeStatus.innerHTML = data.syncing ? `<span class='warning'>Syncing (${progress}%)</span>` :
"<span class='success'>Ready</span>";

blockHeight.innerText = data.blockHeight;
Expand Down
118 changes: 75 additions & 43 deletions internal/resolvers/proc/hns.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import (
"strings"
"sync"
"time"

"github.com/miekg/dns"
)

type HNSProc struct {
Expand All @@ -21,25 +23,31 @@ type HNSProc struct {
Verbose bool
procStarted bool
height uint64
lastHeightUpdate time.Time
synced bool
progress float64
retryCount int
lastRetry time.Time
hsClient *dns.Client
sync.RWMutex
}

func NewHNSProc(procPath string, rootAddr, recursiveAddr string) (*HNSProc, error) {
args := []string{"--ns-host", rootAddr, "--rs-host", recursiveAddr, "--pool-size", "4"}
func NewHNSProc(procPath string, rootAddr, recursiveAddr string, configPath string) (*HNSProc, error) {
args := []string{"--ns-host", rootAddr, "--rs-host", recursiveAddr, "--pool-size", "4", "-x", configPath}
Copy link
Collaborator

@pinheadmz pinheadmz Feb 19, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mine as well add -t as well to start from the hard coded checkpoint at height 136,000 ;-)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I misread this line from the PR and thought that -t would be ignored:

prefix will override checkpoint if both are used.

Turns out it does help starting from 136k if there's no written file. Added -t, thanks


if !strings.HasSuffix(procPath, processExtension) {
procPath += processExtension
}

hsClient := &dns.Client{
Timeout: 1 * time.Second,
SingleInflight: true,
}

p := &HNSProc{
path: procPath,
args: args,
resolverAddr: recursiveAddr,
rootAddr: rootAddr,
hsClient: hsClient,
Verbose: true,
}

Expand Down Expand Up @@ -75,32 +83,12 @@ func (h *HNSProc) goStart(stopErr chan<- error) {

func (h *HNSProc) monitor(pipe io.ReadCloser, stopErr chan<- error) {
sc := bufio.NewScanner(pipe)
p := "chain ("
plen := len(p)
for sc.Scan() {
t := sc.Text()
if h.Verbose {
log.Printf("[INFO] hns: %s", t)
}

if !strings.HasPrefix(t, p) {
continue
}

var block []rune
for _, r := range t[plen:] {
if r == ')' {
break
}
block = append(block, r)
}

val, err := strconv.ParseUint(string(block), 10, 64)
if err != nil {
val = 0
}

h.SetHeight(val)
// if we are getting some updates from hnsd process
// it started successfully so we may want
// to reset retry count
Expand All @@ -119,6 +107,55 @@ func (h *HNSProc) monitor(pipe io.ReadCloser, stopErr chan<- error) {
stopErr <- fmt.Errorf("process exited 0")
}

func (h *HNSProc) goRefreshStatus() {
go func() {
ticker := time.NewTicker(1000 * time.Millisecond)

for range ticker.C {
if !h.procStarted {
ticker.Stop()
continue
}

// Create DNS Query
msg := new(dns.Msg)
msg.SetQuestion("chain.hnsd.", dns.TypeTXT)
msg.Question[0].Qclass = dns.ClassHESIOD

// Send the query to hnsd
resp, _, err := h.hsClient.Exchange(msg, h.rootAddr)
if err != nil {
log.Printf("[WARN] hnsd: error querying hnsd dns api: %v", err)
continue
}

// Read and update chain info
for _, answer := range resp.Answer {
if txt, ok := answer.(*dns.TXT); ok {
switch txt.Hdr.Name {

// height
case "height.tip.chain.hnsd.":
height, err := strconv.ParseUint(txt.Txt[0], 10, 64)
if err != nil {
height = 0
}
h.SetHeight(height)

// progress
case "progress.chain.hnsd.":
progress, err := strconv.ParseFloat(txt.Txt[0], 64)
if err != nil {
progress = 0
}
h.SetProgress(progress)
}
}
}
}
}()
}

func (h *HNSProc) killProcess() error {
if h.cmd == nil || h.cmd.Process == nil {
return nil
Expand Down Expand Up @@ -170,37 +207,32 @@ func (h *HNSProc) IncrementRetries() {
h.lastRetry = time.Now()
}

func (h *HNSProc) GetChainInfo() (uint64, float64) {
h.RLock()
defer h.RUnlock()

return h.height, h.progress
}

func (h *HNSProc) SetHeight(height uint64) {
h.Lock()
defer h.Unlock()

if h.height == height {
return
}

h.height = height
h.lastHeightUpdate = time.Now()
}

func (h *HNSProc) GetHeight() uint64 {
h.RLock()
defer h.RUnlock()
func (h *HNSProc) SetProgress(progress float64) {
h.Lock()
defer h.Unlock()

return h.height
h.progress = progress
}

func (h *HNSProc) Synced() bool {
h.RLock()
defer h.RUnlock()

if h.synced {
return true
}

h.synced = !h.lastHeightUpdate.IsZero() &&
time.Since(h.lastHeightUpdate) > 20*time.Second

return h.synced
return h.progress == 1
}

func (h *HNSProc) Start(stopErr chan<- error) {
Expand All @@ -212,6 +244,7 @@ func (h *HNSProc) Start(stopErr chan<- error) {
defer h.Unlock()

h.goStart(stopErr)
h.goRefreshStatus()
h.procStarted = true

}
Expand All @@ -222,6 +255,5 @@ func (h *HNSProc) Stop() {
h.killProcess()
h.procStarted = false
h.height = 0
h.lastHeightUpdate = time.Time{}
h.synced = false
h.progress = 0
}
10 changes: 5 additions & 5 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -251,13 +251,13 @@ func main() {
case <-ticker.C:
if !app.proc.Started() {
ui.Data.SetBlockHeight("--")
app.config.Debug.SetBlockHeight(0)
app.config.Debug.SetChainInfo(0, 0)
continue
}

height := app.proc.GetHeight()
ui.Data.SetBlockHeight(fmt.Sprintf("#%d", height))
app.config.Debug.SetBlockHeight(height)
height, progress := app.proc.GetChainInfo()
ui.Data.SetBlockHeight(fmt.Sprintf("#%d (%.2f%%)", height, progress*100))
app.config.Debug.SetChainInfo(height, progress)
}

}
Expand Down Expand Up @@ -317,7 +317,7 @@ func NewApp(appConfig *config.App) (*App, error) {
app.proxyURL = config.GetProxyURL(usrConfig.ProxyAddr)
app.usrConfig = &usrConfig

if hnsProc, err = proc.NewHNSProc(appConfig.DNSProcPath, usrConfig.RootAddr, usrConfig.RecursiveAddr); err != nil {
if hnsProc, err = proc.NewHNSProc(appConfig.DNSProcPath, usrConfig.RootAddr, usrConfig.RecursiveAddr, appConfig.Path); err != nil {
return nil, err
}
hnsProc.SetUserAgent("fingertip:" + Version)
Expand Down