2022-05-12 06:51:08 +00:00
|
|
|
package bundle
|
2022-05-11 19:30:07 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"crypto/sha256"
|
|
|
|
"encoding/hex"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
2022-05-12 07:00:20 +00:00
|
|
|
"net"
|
2022-05-11 19:30:07 +00:00
|
|
|
"net/http"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
2022-05-12 07:00:20 +00:00
|
|
|
"time"
|
2022-05-11 19:30:07 +00:00
|
|
|
|
|
|
|
"golang.org/x/xerrors"
|
|
|
|
|
|
|
|
logging "github.com/ipfs/go-log/v2"
|
|
|
|
)
|
|
|
|
|
2022-05-12 06:51:08 +00:00
|
|
|
var log = logging.Logger("bundle-fetcher")
|
2022-05-11 19:30:07 +00:00
|
|
|
|
|
|
|
type BundleFetcher struct {
|
|
|
|
path string
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewBundleFetcher(basepath string) (*BundleFetcher, error) {
|
|
|
|
path := filepath.Join(basepath, "builtin-actors")
|
|
|
|
if err := os.MkdirAll(path, 0755); err != nil {
|
2022-05-11 19:41:56 +00:00
|
|
|
return nil, xerrors.Errorf("error making bundle directory %s: %w", path, err)
|
2022-05-11 19:30:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return &BundleFetcher{path: path}, nil
|
|
|
|
}
|
|
|
|
|
2022-05-17 17:38:32 +00:00
|
|
|
func (b *BundleFetcher) FetchFromRelease(version int, release, netw string) (path string, err error) {
|
2022-05-11 19:30:07 +00:00
|
|
|
bundleName := fmt.Sprintf("builtin-actors-%s", netw)
|
|
|
|
bundleFile := fmt.Sprintf("%s.car", bundleName)
|
|
|
|
bundleHash := fmt.Sprintf("%s.sha256", bundleName)
|
|
|
|
bundleBasePath := filepath.Join(b.path, fmt.Sprintf("v%d", version), release)
|
|
|
|
|
|
|
|
if err := os.MkdirAll(bundleBasePath, 0755); err != nil {
|
2022-05-11 19:41:56 +00:00
|
|
|
return "", xerrors.Errorf("error making bundle directory %s: %w", bundleBasePath, err)
|
2022-05-11 19:30:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// check if it exists; if it does, check the hash
|
|
|
|
bundleFilePath := filepath.Join(bundleBasePath, bundleFile)
|
|
|
|
if _, err := os.Stat(bundleFilePath); err == nil {
|
2022-05-17 17:38:32 +00:00
|
|
|
err := b.checkRelease(bundleBasePath, bundleFile, bundleHash)
|
2022-05-11 19:30:07 +00:00
|
|
|
if err == nil {
|
|
|
|
return bundleFilePath, nil
|
|
|
|
}
|
|
|
|
|
2022-05-12 06:51:08 +00:00
|
|
|
log.Warnf("invalid bundle %s: %s; refetching", bundleName, err)
|
2022-05-11 19:30:07 +00:00
|
|
|
}
|
|
|
|
|
2022-05-12 06:51:08 +00:00
|
|
|
log.Infof("fetching bundle %s", bundleFile)
|
2022-05-17 17:38:32 +00:00
|
|
|
if err := b.fetchFromRelease(release, bundleBasePath, bundleFile, bundleHash); err != nil {
|
2022-05-12 06:51:08 +00:00
|
|
|
log.Errorf("error fetching bundle %s: %s", bundleName, err)
|
2022-05-11 19:30:07 +00:00
|
|
|
return "", xerrors.Errorf("error fetching bundle: %w", err)
|
|
|
|
}
|
|
|
|
|
2022-05-17 17:38:32 +00:00
|
|
|
if err := b.checkRelease(bundleBasePath, bundleFile, bundleHash); err != nil {
|
|
|
|
log.Errorf("error checking bundle %s: %s", bundleName, err)
|
|
|
|
return "", xerrors.Errorf("error checking bundle: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return bundleFilePath, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *BundleFetcher) FetchFromURL(version int, release, netw, url, cksum string) (path string, err error) {
|
|
|
|
bundleName := fmt.Sprintf("builtin-actors-%s", netw)
|
|
|
|
bundleFile := fmt.Sprintf("%s.car", bundleName)
|
|
|
|
bundleBasePath := filepath.Join(b.path, fmt.Sprintf("v%d", version), release)
|
|
|
|
|
|
|
|
if err := os.MkdirAll(bundleBasePath, 0755); err != nil {
|
|
|
|
return "", xerrors.Errorf("error making bundle directory %s: %w", bundleBasePath, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// check if it exists; if it does, check the hash
|
|
|
|
bundleFilePath := filepath.Join(bundleBasePath, bundleFile)
|
|
|
|
if _, err := os.Stat(bundleFilePath); err == nil {
|
|
|
|
err := b.checkHash(bundleBasePath, bundleFile, cksum)
|
|
|
|
if err == nil {
|
|
|
|
return bundleFilePath, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Warnf("invalid bundle %s: %s; refetching", bundleName, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Infof("fetching bundle %s", bundleFile)
|
|
|
|
if err := b.fetchFromURL(bundleBasePath, bundleFile, url); err != nil {
|
|
|
|
log.Errorf("error fetching bundle %s: %s", bundleName, err)
|
|
|
|
return "", xerrors.Errorf("error fetching bundle: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := b.checkHash(bundleBasePath, bundleFile, cksum); err != nil {
|
2022-05-12 06:51:08 +00:00
|
|
|
log.Errorf("error checking bundle %s: %s", bundleName, err)
|
2022-05-11 19:30:07 +00:00
|
|
|
return "", xerrors.Errorf("error checking bundle: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return bundleFilePath, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *BundleFetcher) fetchURL(url, path string) error {
|
2022-05-12 06:51:08 +00:00
|
|
|
log.Infof("fetching URL: %s", url)
|
2022-05-11 19:30:07 +00:00
|
|
|
|
2022-05-12 07:00:20 +00:00
|
|
|
for i := 0; i < 3; i++ {
|
|
|
|
resp, err := http.Get(url) //nolint
|
|
|
|
if err != nil {
|
|
|
|
if isTemporary(err) {
|
|
|
|
log.Warnf("temporary error fetching %s: %s; retrying in 1s", url, err)
|
|
|
|
time.Sleep(time.Second)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
return xerrors.Errorf("error fetching %s: %w", url, err)
|
|
|
|
}
|
|
|
|
defer resp.Body.Close() //nolint
|
2022-05-11 19:30:07 +00:00
|
|
|
|
2022-05-12 07:00:20 +00:00
|
|
|
if resp.StatusCode != http.StatusOK {
|
|
|
|
log.Warnf("unexpected response fetching %s: %s (%d); retrying in 1s", url, resp.Status, resp.StatusCode)
|
|
|
|
time.Sleep(time.Second)
|
|
|
|
continue
|
|
|
|
}
|
2022-05-11 19:30:07 +00:00
|
|
|
|
2022-05-12 07:00:20 +00:00
|
|
|
f, err := os.OpenFile(path, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644)
|
|
|
|
if err != nil {
|
|
|
|
return xerrors.Errorf("error opening %s for writing: %w", path, err)
|
|
|
|
}
|
|
|
|
defer f.Close() //nolint
|
|
|
|
|
|
|
|
if _, err := io.Copy(f, resp.Body); err != nil {
|
|
|
|
return xerrors.Errorf("error writing %s: %w", path, err)
|
|
|
|
}
|
2022-05-11 19:30:07 +00:00
|
|
|
|
2022-05-12 07:00:20 +00:00
|
|
|
return nil
|
2022-05-11 19:30:07 +00:00
|
|
|
}
|
|
|
|
|
2022-05-12 07:00:20 +00:00
|
|
|
return xerrors.Errorf("all attempts to fetch %s failed", url)
|
2022-05-11 19:30:07 +00:00
|
|
|
}
|
|
|
|
|
2022-05-17 17:38:32 +00:00
|
|
|
func (b *BundleFetcher) fetchFromRelease(release, bundleBasePath, bundleFile, bundleHash string) error {
|
2022-05-11 19:30:07 +00:00
|
|
|
bundleHashUrl := fmt.Sprintf("https://github.com/filecoin-project/builtin-actors/releases/download/%s/%s",
|
|
|
|
release, bundleHash)
|
|
|
|
bundleHashPath := filepath.Join(bundleBasePath, bundleHash)
|
|
|
|
if err := b.fetchURL(bundleHashUrl, bundleHashPath); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
bundleFileUrl := fmt.Sprintf("https://github.com/filecoin-project/builtin-actors/releases/download/%s/%s",
|
|
|
|
release, bundleFile)
|
|
|
|
bundleFilePath := filepath.Join(bundleBasePath, bundleFile)
|
|
|
|
if err := b.fetchURL(bundleFileUrl, bundleFilePath); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-05-17 17:38:32 +00:00
|
|
|
func (b *BundleFetcher) fetchFromURL(bundleBasePath, bundleFile, url string) error {
|
|
|
|
bundleFilePath := filepath.Join(bundleBasePath, bundleFile)
|
|
|
|
return b.fetchURL(url, bundleFilePath)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *BundleFetcher) checkRelease(bundleBasePath, bundleFile, bundleHash string) error {
|
2022-05-11 19:30:07 +00:00
|
|
|
bundleHashPath := filepath.Join(bundleBasePath, bundleHash)
|
|
|
|
f, err := os.Open(bundleHashPath)
|
|
|
|
if err != nil {
|
|
|
|
return xerrors.Errorf("error opening %s: %w", bundleHashPath, err)
|
|
|
|
}
|
2022-05-11 19:41:56 +00:00
|
|
|
defer f.Close() //nolint
|
2022-05-11 19:30:07 +00:00
|
|
|
|
|
|
|
bs, err := io.ReadAll(f)
|
|
|
|
if err != nil {
|
|
|
|
return xerrors.Errorf("error reading %s: %w", bundleHashPath, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
parts := strings.Split(string(bs), " ")
|
|
|
|
hashHex := parts[0]
|
2022-05-17 17:38:32 +00:00
|
|
|
|
|
|
|
return b.checkHash(bundleBasePath, bundleFile, hashHex)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *BundleFetcher) checkHash(bundleBasePath, bundleFile, cksum string) error {
|
|
|
|
expectedDigest, err := hex.DecodeString(cksum)
|
2022-05-11 19:30:07 +00:00
|
|
|
if err != nil {
|
2022-05-17 17:38:32 +00:00
|
|
|
return xerrors.Errorf("error decoding digest from %s: %w", cksum, err)
|
2022-05-11 19:30:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bundleFilePath := filepath.Join(bundleBasePath, bundleFile)
|
2022-05-17 17:38:32 +00:00
|
|
|
f, err := os.Open(bundleFilePath)
|
2022-05-11 19:30:07 +00:00
|
|
|
if err != nil {
|
|
|
|
return xerrors.Errorf("error opening %s: %w", bundleFilePath, err)
|
|
|
|
}
|
2022-05-11 19:41:56 +00:00
|
|
|
defer f.Close() //nolint
|
2022-05-11 19:30:07 +00:00
|
|
|
|
|
|
|
h256 := sha256.New()
|
|
|
|
if _, err := io.Copy(h256, f); err != nil {
|
|
|
|
return xerrors.Errorf("error computing digest for %s: %w", bundleFilePath, err)
|
|
|
|
}
|
|
|
|
digest := h256.Sum(nil)
|
|
|
|
|
|
|
|
if !bytes.Equal(digest, expectedDigest) {
|
|
|
|
return xerrors.Errorf("hash mismatch")
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2022-05-12 07:00:20 +00:00
|
|
|
|
|
|
|
func isTemporary(err error) bool {
|
|
|
|
if ne, ok := err.(net.Error); ok {
|
|
|
|
return ne.Temporary()
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|