2019-10-02 17:20:30 +00:00
|
|
|
package build
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/hex"
|
|
|
|
"encoding/json"
|
|
|
|
"io"
|
|
|
|
"net/http"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
rice "github.com/GeertJohan/go.rice"
|
|
|
|
logging "github.com/ipfs/go-log"
|
|
|
|
"github.com/minio/blake2b-simd"
|
|
|
|
pb "gopkg.in/cheggaaa/pb.v1"
|
|
|
|
)
|
|
|
|
|
2019-10-02 20:29:40 +00:00
|
|
|
var log = logging.Logger("build")
|
2019-10-02 17:20:30 +00:00
|
|
|
|
|
|
|
const gateway = "http://198.211.99.118/ipfs/"
|
|
|
|
const paramdir = "/var/tmp/filecoin-proof-parameters"
|
|
|
|
|
2019-10-02 20:29:40 +00:00
|
|
|
type paramFile struct {
|
|
|
|
Cid string `json:"cid"`
|
|
|
|
Digest string `json:"digest"`
|
2019-10-02 17:20:30 +00:00
|
|
|
SectorSize uint64 `json:"sector_size"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func GetParams(storage bool) error {
|
2019-10-02 23:36:33 +00:00
|
|
|
if err := os.Mkdir(paramdir, 0755); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-10-02 17:20:30 +00:00
|
|
|
|
|
|
|
var params map[string]paramFile
|
|
|
|
|
|
|
|
paramBytes := rice.MustFindBox("proof-params").MustBytes("parameters.json")
|
|
|
|
if err := json.Unmarshal(paramBytes, ¶ms); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for name, info := range params {
|
|
|
|
if info.SectorSize != SectorSize {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if !storage && strings.HasSuffix(name, ".params") {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := maybeFetch(name, info); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func maybeFetch(name string, info paramFile) error {
|
|
|
|
path := filepath.Join(paramdir, name)
|
|
|
|
f, err := os.Open(path)
|
|
|
|
if err == nil {
|
|
|
|
defer f.Close()
|
|
|
|
|
|
|
|
h := blake2b.New512()
|
|
|
|
if _, err := io.Copy(h, f); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
sum := h.Sum(nil)
|
|
|
|
strSum := hex.EncodeToString(sum[:16])
|
|
|
|
if strSum == info.Digest {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Warnf("Checksum mismatch in param file %s, %s != %s", name, strSum, info.Digest)
|
|
|
|
}
|
|
|
|
|
|
|
|
return doFetch(path, info)
|
|
|
|
}
|
|
|
|
|
|
|
|
func doFetch(out string, info paramFile) error {
|
|
|
|
log.Infof("Fetching %s", out)
|
|
|
|
|
|
|
|
resp, err := http.Get(gateway + info.Cid)
|
2019-10-02 20:29:40 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
outf, err := os.Create(out)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer outf.Close()
|
|
|
|
|
|
|
|
bar := pb.New64(resp.ContentLength)
|
2019-10-02 17:20:30 +00:00
|
|
|
bar.Units = pb.U_BYTES
|
|
|
|
bar.ShowSpeed = true
|
|
|
|
bar.Start()
|
|
|
|
|
2019-10-02 20:29:40 +00:00
|
|
|
_, err = io.Copy(outf, bar.NewProxyReader(resp.Body))
|
2019-10-02 17:20:30 +00:00
|
|
|
|
2019-10-02 20:29:40 +00:00
|
|
|
bar.Finish()
|
2019-10-02 17:20:30 +00:00
|
|
|
|
|
|
|
return err
|
|
|
|
}
|