Skip to content

Commit

Permalink
refactor APIProvider to NodeProvider (#400)
Browse files Browse the repository at this point in the history
* refactor APIProvider

* fix typos

* use more accurate name for mockprovider

* Revert "refactor APIProvider"

This reverts commit 9764efc.

* refactor to NodeProvider

* merge conflict fix

* merge conflict fix
  • Loading branch information
evan-forbes committed Jun 14, 2021
1 parent 4c651f0 commit a7326d5
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 52 deletions.
4 changes: 2 additions & 2 deletions abci/example/dummyapp/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ func main() {
}

// DummyNode implements NodeProvider.
func DummyNode(config *cfg.Config, ipfs ipfs.APIProvider, logger log.Logger) (*node.Node, error) {
func DummyNode(config *cfg.Config, provider ipfs.NodeProvider, logger log.Logger) (*node.Node, error) {
nodeKey, err := p2p.LoadOrGenNodeKey(config.NodeKeyFile())
if err != nil {
return nil, fmt.Errorf("failed to load or gen node key %s: %w", config.NodeKeyFile(), err)
Expand All @@ -99,7 +99,7 @@ func DummyNode(config *cfg.Config, ipfs ipfs.APIProvider, logger log.Logger) (*n
proxy.NewLocalClientCreator(app),
node.DefaultGenesisDocProviderFunc(config),
node.DefaultDBProvider,
ipfs,
provider,
node.DefaultMetricsProvider(config.Instrumentation),
logger,
)
Expand Down
7 changes: 2 additions & 5 deletions cmd/tendermint/commands/light.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import (
"strings"
"time"

coreiface "github.com/ipfs/interface-go-ipfs-core"
"github.com/spf13/cobra"

"github.com/lazyledger/lazyledger-core/crypto/merkle"
Expand Down Expand Up @@ -186,13 +185,11 @@ func runProxy(cmd *cobra.Command, args []string) error {
cfg.RootDir = dir
cfg.ServeAPI = true
// TODO(ismail): share badger instance
apiProvider := ipfs.Embedded(true, cfg, logger)
var dag coreiface.APIDagService
dag, ipfsCloser, err = apiProvider()
ipfsNode, err := ipfs.Embedded(true, cfg, logger)()
if err != nil {
return fmt.Errorf("could not start ipfs API: %w", err)
}
options = append(options, light.DataAvailabilitySampling(numSamples, dag))
options = append(options, light.DataAvailabilitySampling(numSamples, ipfsNode.DAG))
case sequential:
options = append(options, light.SequentialVerification())
default:
Expand Down
26 changes: 9 additions & 17 deletions ipfs/embedded.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,10 @@ import (
ipfscfg "github.com/ipfs/go-ipfs-config"
"github.com/ipfs/go-ipfs/commands"
"github.com/ipfs/go-ipfs/core"
"github.com/ipfs/go-ipfs/core/coreapi"
"github.com/ipfs/go-ipfs/core/corehttp"
"github.com/ipfs/go-ipfs/core/node/libp2p"
"github.com/ipfs/go-ipfs/repo"
"github.com/ipfs/go-ipfs/repo/fsrepo"
coreiface "github.com/ipfs/interface-go-ipfs-core"
"github.com/multiformats/go-multiaddr"
manet "github.com/multiformats/go-multiaddr/net"

Expand All @@ -24,29 +22,29 @@ import (

// Embedded is the provider that embeds IPFS node within the same process.
// It also returns closable for graceful node shutdown.
func Embedded(init bool, cfg *Config, logger log.Logger) APIProvider {
return func() (coreiface.APIDagService, *core.IpfsNode, error) {
func Embedded(init bool, cfg *Config, logger log.Logger) NodeProvider {
return func() (*core.IpfsNode, error) {
path := cfg.Path()
defer os.Setenv(ipfscfg.EnvDir, path)

// NOTE: no need to validate the path before
if err := plugins(path); err != nil {
return nil, nil, err
return nil, err
}
// Init Repo if requested
if init {
if err := InitRepo(path, logger); err != nil {
return nil, nil, err
return nil, err
}
}
// Open the repo
repo, err := fsrepo.Open(path)
if err != nil {
var nrerr fsrepo.NoRepoError
if errors.As(err, &nrerr) {
return nil, nil, fmt.Errorf("no IPFS repo found in %s.\nplease use flag: --ipfs-init", nrerr.Path)
return nil, fmt.Errorf("no IPFS repo found in %s.\nplease use flag: --ipfs-init", nrerr.Path)
}
return nil, nil, err
return nil, err
}
// Construct the node
nodeOptions := &core.BuildCfg{
Expand All @@ -69,24 +67,18 @@ func Embedded(init bool, cfg *Config, logger log.Logger) APIProvider {
node, err := core.NewNode(ctx, nodeOptions)
if err != nil {
_ = repo.Close()
return nil, nil, err
return nil, err
}
// Serve API if requested
if cfg.ServeAPI {
if err := serveAPI(path, repo, node); err != nil {
_ = node.Close()
return nil, nil, err
return nil, err
}
}
// Wrap Node and create CoreAPI
api, err := coreapi.NewCoreAPI(node)
if err != nil {
_ = node.Close()
return nil, nil, fmt.Errorf("failed to create an instance of the IPFS core API: %w", err)
}

logger.Info("Successfully created embedded IPFS node", "ipfs-repo", path)
return api.Dag(), node, nil
return node, nil
}
}

Expand Down
17 changes: 4 additions & 13 deletions ipfs/mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,23 @@ import (
nilrouting "github.com/ipfs/go-ipfs-routing/none"
"github.com/ipfs/go-ipfs/core"
coremock "github.com/ipfs/go-ipfs/core/mock"
ipld "github.com/ipfs/go-ipld-format"
coreiface "github.com/ipfs/interface-go-ipfs-core"
"github.com/libp2p/go-libp2p-core/routing"
mocknet "github.com/libp2p/go-libp2p/p2p/net/mock"

"github.com/lazyledger/lazyledger-core/ipfs/plugin"
)

// Mock provides simple mock IPFS API useful for testing
func Mock() APIProvider {
return func() (coreiface.APIDagService, *core.IpfsNode, error) {
func Mock() NodeProvider {
return func() (*core.IpfsNode, error) {
plugin.EnableNMT()

nd, err := MockNode()
if err != nil {
return nil, nil, err
return nil, err
}

return dagOnlyMock{nd.DAG}, nd, nil
return nd, nil
}
}

Expand All @@ -45,10 +43,3 @@ func MockRouting() routing.Routing {
croute, _ := nilrouting.ConstructNilRouting(context.TODO(), nil, nil, nil)
return croute
}

type dagOnlyMock struct {
ipld.DAGService
}

func (dom dagOnlyMock) Dag() coreiface.APIDagService { return dom }
func (dom dagOnlyMock) Pinning() ipld.NodeAdder { return dom }
5 changes: 2 additions & 3 deletions ipfs/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ package ipfs

import (
"github.com/ipfs/go-ipfs/core"
coreiface "github.com/ipfs/interface-go-ipfs-core"
)

// APIProvider allows customizable IPFS core APIs.
type APIProvider func() (coreiface.APIDagService, *core.IpfsNode, error)
// NodeProvider initializes and returns an IPFS node
type NodeProvider func() (*core.IpfsNode, error)
9 changes: 4 additions & 5 deletions light/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (

format "github.com/ipfs/go-ipld-format"
"github.com/ipfs/go-merkledag"
coreiface "github.com/ipfs/interface-go-ipfs-core"
"github.com/lazyledger/nmt/namespace"

"github.com/lazyledger/lazyledger-core/libs/log"
Expand Down Expand Up @@ -71,12 +70,12 @@ func SkippingVerification(trustLevel tmmath.Fraction) Option {
}
}

func DataAvailabilitySampling(numSamples uint32, ipfsAPI coreiface.APIDagService) Option {
func DataAvailabilitySampling(numSamples uint32, dag format.DAGService) Option {
return func(c *Client) {
c.verificationMode = dataAvailabilitySampling
c.numSamples = numSamples
c.dag = ipfsAPI
c.sessionDAG = merkledag.NewSession(context.TODO(), ipfsAPI)
c.dag = dag
c.sessionDAG = merkledag.NewSession(context.TODO(), dag)
}
}

Expand Down Expand Up @@ -157,7 +156,7 @@ type Client struct {

logger log.Logger

dag coreiface.APIDagService
dag format.DAGService
sessionDAG format.NodeGetter
}

Expand Down
14 changes: 7 additions & 7 deletions node/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,12 +87,12 @@ func DefaultGenesisDocProviderFunc(config *cfg.Config) GenesisDocProvider {
}

// Provider takes a config and a logger and returns a ready to go Node.
type Provider func(*cfg.Config, ipfs.APIProvider, log.Logger) (*Node, error)
type Provider func(*cfg.Config, ipfs.NodeProvider, log.Logger) (*Node, error)

// DefaultNewNode returns a Tendermint node with default settings for the
// PrivValidator, ClientCreator, GenesisDoc, and DBProvider.
// It implements NodeProvider.
func DefaultNewNode(config *cfg.Config, ipfs ipfs.APIProvider, logger log.Logger) (*Node, error) {
func DefaultNewNode(config *cfg.Config, ipfs ipfs.NodeProvider, logger log.Logger) (*Node, error) {
nodeKey, err := p2p.LoadOrGenNodeKey(config.NodeKeyFile())
if err != nil {
return nil, fmt.Errorf("failed to load or gen node key %s: %w", config.NodeKeyFile(), err)
Expand Down Expand Up @@ -620,7 +620,7 @@ func NewNode(config *cfg.Config,
clientCreator proxy.ClientCreator,
genesisDocProvider GenesisDocProvider,
dbProvider DBProvider,
ipfsProvider ipfs.APIProvider,
ipfsProvider ipfs.NodeProvider,
metricsProvider MetricsProvider,
logger log.Logger,
options ...Option) (*Node, error) {
Expand Down Expand Up @@ -680,17 +680,17 @@ func NewNode(config *cfg.Config,
stateSync = false
}

dag, ipfsNode, err := ipfsProvider()
blockStoreDB, err := dbProvider(&DBContext{"blockstore", config})
if err != nil {
return nil, err
}

blockStoreDB, err := dbProvider(&DBContext{"blockstore", config})
ipfsNode, err := ipfsProvider()
if err != nil {
return nil, err
}

blockStore := store.NewBlockStore(blockStoreDB, dag)
blockStore := store.NewBlockStore(blockStoreDB, ipfsNode.DAG)

// Create the handshaker, which calls RequestInfo, sets the AppVersion on the state,
// and replays any blocks as necessary to sync tendermint with the app.
Expand Down Expand Up @@ -751,7 +751,7 @@ func NewNode(config *cfg.Config,
}
consensusReactor, consensusState := createConsensusReactor(
config, state, blockExec, blockStore, mempool, evidencePool,
privValidator, csMetrics, stateSync || fastSync, eventBus, dag, ipfsNode.Routing, consensusLogger,
privValidator, csMetrics, stateSync || fastSync, eventBus, ipfsNode.DAG, ipfsNode.Routing, consensusLogger,
)

// Set up state sync reactor, and schedule a sync if requested.
Expand Down

0 comments on commit a7326d5

Please sign in to comment.