Skip to content

Commit

Permalink
Merge pull request filecoin-project#5282 from filecoin-project/chore/…
Browse files Browse the repository at this point in the history
…snake_context_through_blockstore_init

Snake a context through the Chain-blockstore creation
  • Loading branch information
magik6k authored and bibibong committed Feb 21, 2021
1 parent b5b4251 commit 3e75a84
Show file tree
Hide file tree
Showing 11 changed files with 29 additions and 18 deletions.
2 changes: 1 addition & 1 deletion chain/gen/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ func NewGeneratorWithSectors(numSectors int) (*ChainGen, error) {
return nil, xerrors.Errorf("failed to get metadata datastore: %w", err)
}

bs, err := lr.Blockstore(repo.BlockstoreChain)
bs, err := lr.Blockstore(context.TODO(), repo.BlockstoreChain)
if err != nil {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion chain/store/store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func BenchmarkGetRandomness(b *testing.B) {
b.Fatal(err)
}

bs, err := lr.Blockstore(repo.BlockstoreChain)
bs, err := lr.Blockstore(context.TODO(), repo.BlockstoreChain)
if err != nil {
b.Fatal(err)
}
Expand Down
4 changes: 2 additions & 2 deletions cmd/epik-shed/balances.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ var chainBalanceStateCmd = &cli.Command{

defer lkrepo.Close() //nolint:errcheck

bs, err := lkrepo.Blockstore(repo.BlockstoreChain)
bs, err := lkrepo.Blockstore(ctx, repo.BlockstoreChain)
if err != nil {
return fmt.Errorf("failed to open blockstore: %w", err)
}
Expand Down Expand Up @@ -393,7 +393,7 @@ func printAccountInfos(infos []accountInfo, minerInfo bool) {
defer lkrepo.Close() //nolint:errcheck
bs, err := lkrepo.Blockstore(repo.BlockstoreChain)
bs, err := lkrepo.Blockstore(ctx, repo.BlockstoreChain)
if err != nil {
return xerrors.Errorf("failed to open blockstore: %w", err)
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/epik-shed/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ var exportChainCmd = &cli.Command{

defer fi.Close() //nolint:errcheck

bs, err := lr.Blockstore(repo.BlockstoreChain)
bs, err := lr.Blockstore(ctx, repo.BlockstoreChain)
if err != nil {
return fmt.Errorf("failed to open blockstore: %w", err)
}
Expand Down
9 changes: 7 additions & 2 deletions cmd/epik-shed/import-car.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"context"
"encoding/hex"
"fmt"
"io"
Expand All @@ -24,6 +25,8 @@ var importCarCmd = &cli.Command{
return xerrors.Errorf("opening fs repo: %w", err)
}

ctx := context.TODO()

exists, err := r.Exists()
if err != nil {
return err
Expand All @@ -44,7 +47,7 @@ var importCarCmd = &cli.Command{
return xerrors.Errorf("opening the car file: %w", err)
}

bs, err := lr.Blockstore(repo.BlockstoreChain)
bs, err := lr.Blockstore(ctx, repo.BlockstoreChain)
if err != nil {
return err
}
Expand Down Expand Up @@ -99,6 +102,8 @@ var importObjectCmd = &cli.Command{
return xerrors.Errorf("opening fs repo: %w", err)
}

ctx := context.TODO()

exists, err := r.Exists()
if err != nil {
return err
Expand All @@ -113,7 +118,7 @@ var importObjectCmd = &cli.Command{
}
defer lr.Close() //nolint:errcheck

bs, err := lr.Blockstore(repo.BlockstoreChain)
bs, err := lr.Blockstore(ctx, repo.BlockstoreChain)
if err != nil {
return fmt.Errorf("failed to open blockstore: %w", err)
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/epik-shed/pruning.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ var stateTreePruneCmd = &cli.Command{

defer lkrepo.Close() //nolint:errcheck

bs, err := lkrepo.Blockstore(repo.BlockstoreChain)
bs, err := lkrepo.Blockstore(ctx, repo.BlockstoreChain)
if err != nil {
return fmt.Errorf("failed to open blockstore: %w", err)
}
Expand Down
12 changes: 6 additions & 6 deletions cmd/epik/daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ var DaemonCmd = &cli.Command{
issnapshot = true
}

if err := ImportChain(r, chainfile, issnapshot); err != nil {
if err := ImportChain(ctx, r, chainfile, issnapshot); err != nil {
return err
}
if cctx.Bool("halt-after-import") {
Expand Down Expand Up @@ -392,7 +392,7 @@ func importKey(ctx context.Context, api api.FullNode, f string) error {
return nil
}

func ImportChain(r repo.Repo, fname string, snapshot bool) (err error) {
func ImportChain(ctx context.Context, 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://") {
Expand Down Expand Up @@ -435,7 +435,7 @@ func ImportChain(r repo.Repo, fname string, snapshot bool) (err error) {
}
defer lr.Close() //nolint:errcheck

bs, err := lr.Blockstore(repo.BlockstoreChain)
bs, err := lr.Blockstore(ctx, repo.BlockstoreChain)
if err != nil {
return xerrors.Errorf("failed to open blockstore: %w", err)
}
Expand Down Expand Up @@ -476,7 +476,7 @@ func ImportChain(r repo.Repo, fname string, snapshot bool) (err error) {
return xerrors.Errorf("flushing validation cache failed: %w", err)
}

gb, err := cst.GetTipsetByHeight(context.TODO(), 0, ts, true)
gb, err := cst.GetTipsetByHeight(ctx, 0, ts, true)
if err != nil {
return err
}
Expand All @@ -490,13 +490,13 @@ func ImportChain(r repo.Repo, fname string, snapshot bool) (err error) {

if !snapshot {
log.Infof("validating imported chain...")
if err := stm.ValidateChain(context.TODO(), ts); err != nil {
if err := stm.ValidateChain(ctx, ts); err != nil {
return xerrors.Errorf("chain validation failed: %w", err)
}
}

log.Infof("accepting %s as new head", ts.Cids())
if err := cst.ForceHeadSilent(context.Background(), ts); err != nil {
if err := cst.ForceHeadSilent(ctx, ts); err != nil {
return err
}

Expand Down
2 changes: 1 addition & 1 deletion node/modules/chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ func MessagePool(lc fx.Lifecycle, sm *stmgr.StateManager, ps *pubsub.PubSub, ds
}

func ChainRawBlockstore(lc fx.Lifecycle, mctx helpers.MetricsCtx, r repo.LockedRepo) (dtypes.ChainRawBlockstore, error) {
bs, err := r.Blockstore(repo.BlockstoreChain)
bs, err := r.Blockstore(helpers.LifecycleCtx(mctx, lc), repo.BlockstoreChain)
if err != nil {
return nil, err
}
Expand Down
3 changes: 2 additions & 1 deletion node/repo/fsrepo.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package repo

import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
Expand Down Expand Up @@ -298,7 +299,7 @@ func (fsr *fsLockedRepo) Close() error {
}

// Blockstore returns a blockstore for the provided data domain.
func (fsr *fsLockedRepo) Blockstore(domain BlockstoreDomain) (blockstore.Blockstore, error) {
func (fsr *fsLockedRepo) Blockstore(ctx context.Context, domain BlockstoreDomain) (blockstore.Blockstore, error) {
if domain != BlockstoreChain {
return nil, ErrInvalidBlockstoreDomain
}
Expand Down
6 changes: 5 additions & 1 deletion node/repo/interface.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package repo

import (
"context"
"errors"

"github.com/EpiK-Protocol/go-epik/lib/blockstore"
Expand Down Expand Up @@ -57,7 +58,10 @@ type LockedRepo interface {
Datastore(ctx context.Context, namespace string) (datastore.Batching, error)

// Blockstore returns an IPLD blockstore for the requested domain.
Blockstore(domain BlockstoreDomain) (blockstore.Blockstore, error)
// The supplied context must only be used to initialize the blockstore.
// The implementation should not retain the context for usage throughout
// the lifecycle.
Blockstore(ctx context.Context, domain BlockstoreDomain) (blockstore.Blockstore, error)

// Returns config in this repo
Config() (interface{}, error)
Expand Down
3 changes: 2 additions & 1 deletion node/repo/memrepo.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package repo

import (
"context"
"encoding/json"
"io/ioutil"
"os"
Expand Down Expand Up @@ -244,7 +245,7 @@ func (lmem *lockedMemRepo) Datastore(_ context.Context, ns string) (datastore.Ba
return namespace.Wrap(lmem.mem.datastore, datastore.NewKey(ns)), nil
}

func (lmem *lockedMemRepo) Blockstore(domain BlockstoreDomain) (blockstore.Blockstore, error) {
func (lmem *lockedMemRepo) Blockstore(ctx context.Context, domain BlockstoreDomain) (blockstore.Blockstore, error) {
if domain != BlockstoreChain {
return nil, ErrInvalidBlockstoreDomain
}
Expand Down

0 comments on commit 3e75a84

Please sign in to comment.