Skip to content

Commit e1bf461

Browse files
committed
resolved sync issue
1 parent d55f8a8 commit e1bf461

File tree

18 files changed

+141
-1827
lines changed

18 files changed

+141
-1827
lines changed

.github/FUNDING.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
github: [randomlogin]

cmd/sane/main.go

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -223,13 +223,6 @@ func main() {
223223
if *hnsdPath == "" {
224224
log.Fatal("path to hnsd is not provided")
225225
}
226-
if *hnsdCheckpointPath == "" {
227-
home, _ := os.UserHomeDir() //above already fails if it doesn't exist
228-
*hnsdCheckpointPath = path.Join(home, ".hnsd")
229-
if err := os.MkdirAll(*hnsdCheckpointPath, 0777); err != nil {
230-
log.Fatalf("error creating directory at %s : %s", *hnsdCheckpointPath, err)
231-
}
232-
}
233226

234227
sync.GetRoots(*hnsdPath, p, *hnsdCheckpointPath)
235228
go func() {

prove/external.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"strings"
99
"time"
1010

11+
"github.com/miekg/dns"
1112
"github.com/randomlogin/sane/debuglog"
1213
)
1314

@@ -22,8 +23,10 @@ type DNSSECJson struct {
2223
var timeout = 1 * time.Second
2324

2425
func fetchDNSSEC(domain string, externalServices []string) ([]byte, error) {
26+
labels := dns.SplitDomainName(domain)
27+
tld := labels[len(labels)-1]
2528
for _, link := range externalServices {
26-
if result, err := fetchOneDNSSEC(domain, link); err == nil {
29+
if result, err := fetchOneDNSSEC(tld, link); err == nil {
2730
return result, nil
2831
}
2932
debuglog.Logger.Debugf("couldn't fetch dnssec data for domain %s from %s", domain, link)
@@ -32,8 +35,11 @@ func fetchDNSSEC(domain string, externalServices []string) ([]byte, error) {
3235
}
3336

3437
func fetchUrkel(domain string, externalServices []string) ([]byte, error) {
38+
39+
labels := dns.SplitDomainName(domain)
40+
tld := labels[len(labels)-1]
3541
for _, link := range externalServices {
36-
if result, err := fetchOneUrkel(domain, link); err == nil {
42+
if result, err := fetchOneUrkel(tld, link); err == nil {
3743
return result, nil
3844
}
3945
debuglog.Logger.Debugf("couldn't fetch urkel data for domain %s from %s", domain, link)

prove/prove_test.go

Lines changed: 0 additions & 305 deletions
This file was deleted.

sync/hnsd.go

Lines changed: 52 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"encoding/json"
77
"errors"
88
"fmt"
9+
"io"
910
"log"
1011
"os"
1112
"os/exec"
@@ -108,6 +109,14 @@ func PeriodicallySync(interval time.Duration, pathToExecutable, confPath, pathTo
108109
}
109110

110111
func GetRoots(pathToExecutable string, confPath string, pathToCheckpoint string) {
112+
if pathToCheckpoint == "" {
113+
home, _ := os.UserHomeDir() //above already fails if it doesn't exist
114+
pathToCheckpoint = path.Join(home, ".hnsd")
115+
}
116+
if err := os.MkdirAll(pathToCheckpoint, 0777); err != nil {
117+
log.Fatalf("error creating directory at %s : %s", pathToCheckpoint, err)
118+
}
119+
111120
rootPath := path.Join(confPath, rootsFileName)
112121
ctx := context.Background()
113122
ctx, cancel := context.WithCancel(ctx)
@@ -120,12 +129,11 @@ func GetRoots(pathToExecutable string, confPath string, pathToCheckpoint string)
120129
}
121130

122131
signalChannel := make(chan os.Signal, 1)
123-
scanner := bufio.NewScanner(stdoutPipe)
124132
if err := cmd.Start(); err != nil {
125133
log.Fatalf("Error starting command: %v", err)
126134
}
127135

128-
time.Sleep(100 * time.Millisecond) //wait 0.1 should suffice for the most of the computers
136+
time.Sleep(100 * time.Millisecond) //0.1 second should suffice for the most of the computers
129137
for i := 1; i <= secondsForHNSD; i++ {
130138
if err := CheckHNSDVersion(); err == nil {
131139
break
@@ -153,10 +161,6 @@ func GetRoots(pathToExecutable string, confPath string, pathToCheckpoint string)
153161
}
154162

155163
if cmd.Process != nil && isSynced {
156-
myjson, _ := json.Marshal(slidingWindow)
157-
if err := os.WriteFile(rootPath, myjson, 0777); err != nil {
158-
log.Fatal(err)
159-
}
160164
if err := cmd.Process.Signal(syscall.SIGINT); err != nil {
161165
log.Fatal("Error killing hnsd: ", err)
162166
}
@@ -172,46 +176,72 @@ func GetRoots(pathToExecutable string, confPath string, pathToCheckpoint string)
172176
return
173177
}()
174178

175-
re := regexp.MustCompile(`chain \((\d+)\): tree_root ([a-fA-F0-9]+) timestamp (\d+)`) // Regular expression for parsing output lines
179+
parseAndWriteOutput(stdoutPipe, signalChannel, slidingWindow, rootPath)
180+
}
181+
182+
func parseAndWriteOutput(stdoutPipe io.ReadCloser, signalChannel chan os.Signal, slidingWindow []BlockInfo, rootPath string) {
183+
scanner := bufio.NewScanner(stdoutPipe)
184+
re := regexp.MustCompile(`chain \((\d+)\): tree_root ([a-fA-F0-9]+) timestamp (\d+)`)
185+
rejectRe := regexp.MustCompile(`chain \((\d+)\): +rejected:`)
186+
187+
var tempBlock *BlockInfo
176188

177-
// Process command output
178189
for {
179190
select {
180191
case <-signalChannel:
192+
myjson, _ := json.Marshal(slidingWindow)
193+
if err := os.WriteFile(rootPath, myjson, 0777); err != nil {
194+
log.Fatal(err)
195+
}
181196
return
182197
default:
183198
for scanner.Scan() {
184199
line := scanner.Text()
185-
match := re.FindStringSubmatch(line)
186200

187-
if len(match) >= 4 {
188-
blockNumberStr, treeRoot, timestampStr := match[1], match[2], match[3]
189-
blockNumber, err := strconv.ParseUint(blockNumberStr, 10, 32)
201+
if rejectMatch := rejectRe.FindStringSubmatch(line); len(rejectMatch) >= 2 {
202+
if tempBlock != nil {
203+
tempBlock = nil // Discard the tempBlock as it has been rejected
204+
}
205+
continue
206+
}
207+
208+
// Check for tree_root line
209+
if match := re.FindStringSubmatch(line); len(match) >= 4 {
210+
blockNumber, err := strconv.ParseUint(match[1], 10, 32)
190211
if err != nil {
191212
log.Printf("failed to parse block height: %v", err)
192213
continue
193214
}
194-
195-
timestamp, err := strconv.ParseUint(timestampStr, 10, 64)
215+
timestamp, err := strconv.ParseUint(match[3], 10, 64)
196216
if err != nil {
197217
log.Printf("failed to parse timestamp: %v", err)
198218
continue
199219
}
220+
treeRoot := match[2]
200221

201-
if !contains(slidingWindow, treeRoot) {
202-
blockInfo := BlockInfo{
203-
Height: uint32(blockNumber),
204-
Timestamp: timestamp,
205-
TreeRoot: treeRoot,
206-
}
207-
208-
slidingWindow = append(slidingWindow, blockInfo)
222+
// if there's a tempBlock already, add it to the slidingWindow
223+
if tempBlock != nil {
224+
slidingWindow = append(slidingWindow, *tempBlock)
209225
if len(slidingWindow) > BlocksToStore {
210226
slidingWindow = slidingWindow[1:]
211227
}
212228
}
229+
tempBlock = &BlockInfo{
230+
Height: uint32(blockNumber),
231+
Timestamp: timestamp,
232+
TreeRoot: treeRoot,
233+
}
234+
}
235+
}
236+
//add last block
237+
if !scanner.Scan() && tempBlock != nil {
238+
slidingWindow = append(slidingWindow, *tempBlock)
239+
if len(slidingWindow) > BlocksToStore {
240+
slidingWindow = slidingWindow[1:]
213241
}
214242
}
243+
215244
}
216245
}
246+
217247
}

sync/synt_test.go

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
package sync
2+
3+
import (
4+
"io"
5+
"log"
6+
"os"
7+
"syscall"
8+
"testing"
9+
)
10+
11+
// test to address the rejection of some blocks already seen by hnsd
12+
func TestParser(t *testing.T) {
13+
syncdata := `
14+
chain (217515): adding block: 0000000000000002f47cfbd4b44aa0a6c8107590c8aca9739a5a418b67fb20a6
15+
chain (217515): tree_root 622b5b297bd0485239eaf794017de020c522feab109988b517453408b320c7f6 timestamp 1711243226
16+
chain (217515): rejected: duplicate
17+
peer 0 (107.152.33.71:44806): failed adding block: EDUPLICATE
18+
chain (217515): adding block: 0000000000000001f8ef2ddc3fe93f73ba1bf3c5c0f002b3ce39fee1f2050328
19+
chain (217515): tree_root 622b5b297bd0485239eaf794017de020c522feab109988b517453408b320c7f6 timestamp 1711244754
20+
chain (217515): rejected: duplicate
21+
peer 0 (107.152.33.71:44806): failed adding block: EDUPLICATE
22+
chain (217515): adding block: 000000000000000397ecdb973332595c777c69952591cd137745bf1c3c9e2898
23+
chain (217515): tree_root 28dc17c03924644b3da99ce8afbb434a9efb2312ff21b53bfeaeb38015dc51aa timestamp 1711286661
24+
chain (217515): rejected: duplicate
25+
peer 0 (107.152.33.71:44806): failed adding block: EDUPLICATE
26+
chain (217515): adding block: 0000000000000002496db35faf8c3251d500653e00c2c4e378694b7b6bf40bc5
27+
chain (217515): tree_root 28dc17c03924644b3da99ce8afbb434a9efb2312ff21b53bfeaeb38015dc51aa timestamp 1711287670
28+
chain (217515): rejected: duplicate
29+
peer 0 (107.152.33.71:44806): failed adding block: EDUPLICATE
30+
chain (217504): adding block: 000000000000000123d5c937c585e608f2c25e03bf768206f2fc69609dac194c
31+
chain (217504): tree_root 0e6c79be15eff6e80f83a991715a1f531cdefc7df4aa4d206095aa0202f36c5b timestamp 1711144096
32+
chain (217504): rejected: duplicate
33+
peer 1 (139.162.183.168:44806): failed adding block: EDUPLICATE
34+
chain (217504): adding block: 00000000000000047622f5a5e97d39faafa729931a35b2d4ef01b2e96f5be054
35+
chain (217504): tree_root 0e6c79be15eff6e80f83a991715a1f531cdefc7df4aa4d206095aa0202f36c5b timestamp 1711144297
36+
chain (217504): rejected: duplicate
37+
peer 1 (139.162.183.168:44806): failed adding block: EDUPLICATE
38+
chain (217504): adding block: 000000000000000470951ccd613056c4eaa40890c03284cf26a560dd0ab2f567
39+
chain (217504): tree_root 0e6c79be15eff6e80f83a991715a1f531cdefc7df4aa4d206095aa0202f36c5b timestamp 1711144398
40+
chain (217504): rejected: duplicate
41+
chain (217508): adding block: 00000000000000035f3b262d2f686610884ae4449854a7e01aad415d4a1c74d8
42+
chain (217508): tree_root 28dc17c03924644b3da99ce8afbb434a9efb2312ff21b53bfeaeb38015dc51aa timestamp 1711285062
43+
chain (217509): added to main chain
44+
chain (217509): new height: 217509
45+
chain (217509): adding block: 0000000000000000e4cf104d2de322f9552e1ac4d7d2c7dc47bf08f92a82b69a
46+
chain (217509): tree_root 28dc17c03924644b3da99ce8afbb434a9efb2312ff21b53bfeaeb38015dc51aa timestamp 1711286206
47+
chain (217510): added to main chain
48+
chain (217510): new height: 217510
49+
chain (217510): adding block: 000000000000000397ecdb973332595c777c69952591cd137745bf1c3c9e2898
50+
chain (217510): tree_root 28dc17c03924644b3da99ce8afbb434a9efb2312ff21b53bfeaeb38015dc51aa timestamp 1711286661
51+
chain (217511): added to main chain
52+
chain (217511): new height: 217511
53+
chain (217511): adding block: 0000000000000002496db35faf8c3251d500653e00c2c4e378694b7b6bf40bc5
54+
chain (217511): tree_root 28dc17c03924644b3da99ce8afbb434a9efb2312ff21b53bfeaeb38015dc51aa timestamp 1711287670
55+
chain (217512): added to main chain
56+
chain (217512): new height: 217512
57+
chain (217512): adding block: 0000000000000001c9d74d480aaaa425723ac559f3da551b31f4acbd1dfae0fd
58+
chain (217512): tree_root 93648a8aa37e3501d713ce8336ff04e8ff19346f6be27099ab668b63f368c2e0 timestamp 1711287824
59+
chain (217513): added to main chain
60+
chain (217513): new height: 217513
61+
chain (217017): adding block: 00000000000000040e97f423c0216030c7582a2695adf67ed0f50277586d4245
62+
chain (217017): tree_root 10974c25520a7507f2994566e7a1345d0b5eecb9fd2f1a3c4e77194b0166aa68 timestamp 1710991231
63+
chain (217018): added to main chain
64+
chain (217018): new height: 217018
65+
`
66+
reader, writer := io.Pipe()
67+
68+
signalChannel := make(chan os.Signal, 1)
69+
slidingWindow := make([]BlockInfo, 0, BlocksToStore)
70+
go func() {
71+
defer writer.Close() // Ensure to close the writer to signal the end of input
72+
_, err := writer.Write([]byte(syncdata))
73+
if err != nil {
74+
log.Fatalf("Error writing to pipe: %v", err)
75+
}
76+
77+
signalChannel <- syscall.SIGINT
78+
}()
79+
parseAndWriteOutput(reader, signalChannel, slidingWindow, "./test_sync_out")
80+
}

test/roots.json

Lines changed: 0 additions & 1 deletion
This file was deleted.

0 commit comments

Comments
 (0)