Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor APIProvider to NodeProvider #400

Merged
merged 8 commits into from
Jun 14, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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