Skip to content

Commit

Permalink
Prototype of checksum checking for existing and downloaded files
Browse files Browse the repository at this point in the history
  • Loading branch information
djkazic committed Jun 3, 2023
1 parent 1853348 commit 6de2b9c
Showing 1 changed file with 113 additions and 23 deletions.
136 changes: 113 additions & 23 deletions mobile/speedloader.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package lndmobile

import (
"bufio"
"compress/gzip"
"crypto/md5"
"encoding/hex"
"errors"
"fmt"
Expand Down Expand Up @@ -233,29 +235,13 @@ func fileExists(filename string) bool {

func GossipSync(cacheDir string, dataDir string, networkType string, callback Callback) {
var (
firstRun bool
useDGraph bool
firstRun bool
useDGraph bool
dgraphPath = cacheDir + "/dgraph/channel.db"
breezURL = "https://maps.eldamar.icu/mainnet/graph/graph-001d.db"
checksumURL = "https://maps.eldamar.icu/mainnet/graph/MD5SUMS"
checksumValue string
)
dgraphPath := cacheDir + "/dgraph/channel.db"
info, err := os.Stat(dgraphPath)
// check the modified time on the existing downloaded channel.db, see if it is <= 48h old
if err == nil {
modifiedTime := info.ModTime()
now := time.Now()
diff := now.Sub(modifiedTime)
if diff.Hours() <= 48 {
// abort downloading the graph, we have a fresh-enough downloaded graph
useDGraph = true
}
}
if !useDGraph {
// we have a stale downloaded graph (older than 48h)
// check network type in preparation for DL
if networkType != "wifi" && networkType != "ethernet" {
// abort downloading the graph, we are on metered networking
useDGraph = true
}
}
// check lastRun time, return early if we ran too recently
lastRunPath := cacheDir + "/lastrun"
if !fileExists(lastRunPath) {
Expand All @@ -274,10 +260,87 @@ func GossipSync(cacheDir string, dataDir string, networkType string, callback Ca
return
}
}
// checksum fetching
client := new(http.Client)
req, err := http.NewRequest("GET", checksumURL, nil)
if err != nil {
callback.OnError(err)
return
}
resp, err := client.Do(req)
if err != nil {
callback.OnError(err)
return
}
defer resp.Body.Close()
reader := bufio.NewReader(resp.Body)
for {
line, err := reader.ReadString('\n')
if err != nil {
if err == io.EOF {
break
} else {
callback.OnError(err)
return
}
}
fields := strings.Fields(line)
if len(fields) != 2 {
callback.OnError(errors.New("unexpected_checksum_line_fmt"))
return
}
filename := fields[1]
hash := fields[0]
if filename == "graph-001d.db" {
checksumValue = hash
}
}
if checksumValue != "" {
// we have a valid checksum
fh, openErr := os.Open(dgraphPath)
// do we have a file?
if !os.IsNotExist(openErr) {
// first, calculate the md5sum of the file we have
defer fh.Close()
md5h := md5.New()
_, err = io.Copy(md5h, fh)
if err != nil {
callback.OnError(err)
return
}
sum := md5h.Sum(nil)
calculatedChecksum := hex.EncodeToString(sum)
if checksumValue != calculatedChecksum {
// failed checksum check
// delete dgraph file
err := os.Remove(dgraphPath)
if err != nil {
callback.OnError(err)
return
}
} else {
// checksum matches
// now check modtime
info, err := os.Stat(dgraphPath)
// check the modified time on the existing downloaded channel.db, see if it is <= 48h old
if err == nil {
modifiedTime := info.ModTime()
now := time.Now()
diff := now.Sub(modifiedTime)
if diff.Hours() <= 48 {
// abort downloading the graph, we have a fresh-enough downloaded graph
useDGraph = true
}
}
}
}
}
if networkType != "wifi" && networkType != "ethernet" {
useDGraph = true
}
// if the dgraph is not usable
if !useDGraph {
// download the breez gossip database
breezURL := "https://maps.eldamar.icu/mainnet/graph/graph-001d.db"
os.MkdirAll(cacheDir+"/dgraph", 0777)
out, err := os.Create(dgraphPath)
if err != nil {
Expand Down Expand Up @@ -316,6 +379,33 @@ func GossipSync(cacheDir string, dataDir string, networkType string, callback Ca
}
out.Close()
resp.Body.Close()
fh, err := os.Open(dgraphPath)
if err != nil {
callback.OnError(err)
return
}
defer fh.Close()
md5h := md5.New()
_, err = io.Copy(md5h, fh)
if err != nil {
callback.OnError(err)
return
}
sum := md5h.Sum(nil)
calculatedChecksum := hex.EncodeToString(sum)
if checksumValue != calculatedChecksum {
// failed checksum check
// delete dgraph file
err := os.Remove(dgraphPath)
if err != nil {
callback.OnError(err)
return
}
// also unconditionally remove lastRun
os.Remove(lastRunPath)
callback.OnResponse([]byte("skip_checksum_failed"))
return
}
}
// open channel.db as dest
service, release, err := serviceRefCounter.Get(
Expand Down

0 comments on commit 6de2b9c

Please sign in to comment.