Skip to content

Commit

Permalink
daemon cmd: Support imports straight from http
Browse files Browse the repository at this point in the history
  • Loading branch information
magik6k committed Sep 28, 2020
1 parent a3145ba commit 0ead83a
Showing 1 changed file with 45 additions and 19 deletions.
64 changes: 45 additions & 19 deletions cmd/lotus/daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,14 @@
package main

import (
"bufio"
"context"
"encoding/hex"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"net/http"
"os"
"runtime/pprof"
"strings"
Expand Down Expand Up @@ -101,11 +104,11 @@ var DaemonCmd = &cli.Command{
},
&cli.StringFlag{
Name: "import-chain",
Usage: "on first run, load chain from given file and validate",
Usage: "on first run, load chain from given file or url and validate",
},
&cli.StringFlag{
Name: "import-snapshot",
Usage: "import chain state from a given chain export file",
Usage: "import chain state from a given chain export file or url",
},
&cli.BoolFlag{
Name: "halt-after-import",
Expand Down Expand Up @@ -207,11 +210,6 @@ var DaemonCmd = &cli.Command{
issnapshot = true
}

chainfile, err := homedir.Expand(chainfile)
if err != nil {
return err
}

if err := ImportChain(r, chainfile, issnapshot); err != nil {
return err
}
Expand Down Expand Up @@ -327,16 +325,41 @@ func importKey(ctx context.Context, api api.FullNode, f string) error {
return nil
}

func ImportChain(r repo.Repo, fname string, snapshot bool) error {
fi, err := os.Open(fname)
if err != nil {
return err
}
defer fi.Close() //nolint:errcheck
func ImportChain(r repo.Repo, fname string, snapshot bool) (err error) {
var rd io.Reader
var l int64
if strings.HasPrefix(fname, "http://") || strings.HasPrefix(fname, "https://") {
resp, err := http.Get(fname)
if err != nil {
return err
}
defer resp.Body.Close()

st, err := os.Stat(fname)
if err != nil {
return err
if resp.StatusCode != http.StatusOK {
return xerrors.Errorf("non-200 response: %d", resp.StatusCode)
}

rd = resp.Body
l = resp.ContentLength
} else {
fname, err = homedir.Expand(fname)
if err != nil {
return err
}

fi, err := os.Open(fname)
if err != nil {
return err
}
defer fi.Close() //nolint:errcheck

st, err := os.Stat(fname)
if err != nil {
return err
}

rd = fi
l = st.Size()
}

lr, err := r.Lock(repo.FullNode)
Expand All @@ -359,12 +382,15 @@ func ImportChain(r repo.Repo, fname string, snapshot bool) error {

cst := store.NewChainStore(bs, mds, vm.Syscalls(ffiwrapper.ProofVerifier))

log.Info("importing chain from file...")
log.Infof("importing chain from %s...", fname)

bufr := bufio.NewReaderSize(rd, 1<<20)

bar := pb.New64(st.Size())
br := bar.NewProxyReader(fi)
bar := pb.New64(l)
br := bar.NewProxyReader(bufr)
bar.ShowTimeLeft = true
bar.ShowPercent = true
bar.ShowSpeed = true
bar.Units = pb.U_BYTES

bar.Start()
Expand Down

0 comments on commit 0ead83a

Please sign in to comment.