Skip to content

Commit

Permalink
Merge pull request #4070 from filecoin-project/feat/import-ux
Browse files Browse the repository at this point in the history
snapshot import progress bar, http support
  • Loading branch information
magik6k authored Sep 28, 2020
2 parents 2633198 + f809529 commit 4b61f17
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 14 deletions.
70 changes: 56 additions & 14 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 All @@ -23,6 +26,7 @@ import (
"go.opencensus.io/stats/view"
"go.opencensus.io/tag"
"golang.org/x/xerrors"
"gopkg.in/cheggaaa/pb.v1"

"github.com/filecoin-project/lotus/api"
"github.com/filecoin-project/lotus/build"
Expand Down Expand Up @@ -100,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 @@ -206,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 @@ -326,12 +325,42 @@ 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
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) //nolint:gosec
if err != nil {
return err
}
defer resp.Body.Close() //nolint:errcheck

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()
}
defer fi.Close() //nolint:errcheck

lr, err := r.Lock(repo.FullNode)
if err != nil {
Expand All @@ -353,8 +382,21 @@ 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...")
ts, err := cst.Import(fi)
log.Infof("importing chain from %s...", fname)

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

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

bar.Start()
ts, err := cst.Import(br)
bar.Finish()

if err != nil {
return xerrors.Errorf("importing chain failed: %w", err)
}
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ require (
golang.org/x/sys v0.0.0-20200602225109-6fdc65e7d980
golang.org/x/time v0.0.0-20191024005414-555d28b269f0
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1
gopkg.in/cheggaaa/pb.v1 v1.0.28
gotest.tools v2.2.0+incompatible
launchpad.net/gocheck v0.0.0-20140225173054-000000000087 // indirect
)
Expand Down

0 comments on commit 4b61f17

Please sign in to comment.