提交 0fd2f80b 作者: Łukasz Magiera 提交者: Steven Allen

Initial DI node implementation

License: MIT
Signed-off-by: 's avatarŁukasz Magiera <magik6k@gmail.com>
上级 2379787e
...@@ -5,7 +5,9 @@ import ( ...@@ -5,7 +5,9 @@ import (
"crypto/rand" "crypto/rand"
"encoding/base64" "encoding/base64"
"errors" "errors"
"github.com/ipfs/go-ipfs/p2p"
"github.com/ipfs/go-ipfs/provider" "github.com/ipfs/go-ipfs/provider"
"go.uber.org/fx"
"os" "os"
"syscall" "syscall"
"time" "time"
...@@ -25,19 +27,15 @@ import ( ...@@ -25,19 +27,15 @@ import (
cfg "github.com/ipfs/go-ipfs-config" cfg "github.com/ipfs/go-ipfs-config"
offline "github.com/ipfs/go-ipfs-exchange-offline" offline "github.com/ipfs/go-ipfs-exchange-offline"
offroute "github.com/ipfs/go-ipfs-routing/offline" offroute "github.com/ipfs/go-ipfs-routing/offline"
ipns "github.com/ipfs/go-ipns"
dag "github.com/ipfs/go-merkledag" dag "github.com/ipfs/go-merkledag"
metrics "github.com/ipfs/go-metrics-interface" metrics "github.com/ipfs/go-metrics-interface"
resolver "github.com/ipfs/go-path/resolver" resolver "github.com/ipfs/go-path/resolver"
uio "github.com/ipfs/go-unixfs/io" uio "github.com/ipfs/go-unixfs/io"
goprocessctx "github.com/jbenet/goprocess/context"
libp2p "github.com/libp2p/go-libp2p" libp2p "github.com/libp2p/go-libp2p"
ci "github.com/libp2p/go-libp2p-crypto" ci "github.com/libp2p/go-libp2p-crypto"
p2phost "github.com/libp2p/go-libp2p-host" p2phost "github.com/libp2p/go-libp2p-host"
peer "github.com/libp2p/go-libp2p-peer" peer "github.com/libp2p/go-libp2p-peer"
pstore "github.com/libp2p/go-libp2p-peerstore" pstore "github.com/libp2p/go-libp2p-peerstore"
pstoremem "github.com/libp2p/go-libp2p-peerstore/pstoremem"
record "github.com/libp2p/go-libp2p-record"
) )
type BuildCfg struct { type BuildCfg struct {
...@@ -55,7 +53,7 @@ type BuildCfg struct { ...@@ -55,7 +53,7 @@ type BuildCfg struct {
// DO NOT SET THIS UNLESS YOU'RE TESTING. // DO NOT SET THIS UNLESS YOU'RE TESTING.
DisableEncryptedConnections bool DisableEncryptedConnections bool
// If NilRepo is set, a repo backed by a nil datastore will be constructed // If NilRepo is set, a Repo backed by a nil datastore will be constructed
NilRepo bool NilRepo bool
Routing RoutingOption Routing RoutingOption
...@@ -73,7 +71,7 @@ func (cfg *BuildCfg) getOpt(key string) bool { ...@@ -73,7 +71,7 @@ func (cfg *BuildCfg) getOpt(key string) bool {
func (cfg *BuildCfg) fillDefaults() error { func (cfg *BuildCfg) fillDefaults() error {
if cfg.Repo != nil && cfg.NilRepo { if cfg.Repo != nil && cfg.NilRepo {
return errors.New("cannot set a repo and specify nilrepo at the same time") return errors.New("cannot set a Repo and specify nilrepo at the same time")
} }
if cfg.Repo == nil { if cfg.Repo == nil {
...@@ -142,7 +140,66 @@ func NewNode(ctx context.Context, cfg *BuildCfg) (*IpfsNode, error) { ...@@ -142,7 +140,66 @@ func NewNode(ctx context.Context, cfg *BuildCfg) (*IpfsNode, error) {
ctx = metrics.CtxScope(ctx, "ipfs") ctx = metrics.CtxScope(ctx, "ipfs")
repoOption := fx.Provide(func(lc fx.Lifecycle) repo.Repo {
lc.Append(fx.Hook{
OnStop: func(ctx context.Context) error {
return cfg.Repo.Close()
},
})
return cfg.Repo
})
// TODO: Remove this, use only for passing node config
cfgOption := fx.Provide(func() *BuildCfg {
return cfg
})
n := &IpfsNode{ n := &IpfsNode{
ctx: ctx,
}
app := fx.New(
repoOption,
cfgOption,
fx.Provide(repoConfig),
fx.Provide(identity),
fx.Provide(privateKey),
fx.Provide(peerstore),
fx.Provide(baseBlockstoreCtor),
fx.Provide(gcBlockstoreCtor),
fx.Provide(recordValidator),
ipfsp2p,
fx.Invoke(setupSharding),
fx.Provide(onlineExchangeCtor), // TODO: offline
fx.Provide(onlineNamesysCtor), // TODO: ^^
fx.Provide(bserv.New),
fx.Provide(onlineDagCtor),
fx.Provide(resolver.NewBasicResolver),
fx.Provide(pinning),
fx.Provide(files),
fx.Provide(providerQueue),
fx.Provide(providerCtor),
fx.Provide(reproviderCtor),
fx.Invoke(reprovider),
fx.Provide(p2p.NewP2P),
fx.Invoke(ipnsRepublisher),
fx.Invoke(provider.Provider.Run),
fx.Extract(n),
)
/* n := &IpfsNode{
IsOnline: cfg.Online, IsOnline: cfg.Online,
Repo: cfg.Repo, Repo: cfg.Repo,
ctx: ctx, ctx: ctx,
...@@ -153,16 +210,19 @@ func NewNode(ctx context.Context, cfg *BuildCfg) (*IpfsNode, error) { ...@@ -153,16 +210,19 @@ func NewNode(ctx context.Context, cfg *BuildCfg) (*IpfsNode, error) {
"pk": record.PublicKeyValidator{}, "pk": record.PublicKeyValidator{},
"ipns": ipns.Validator{KeyBook: n.Peerstore}, "ipns": ipns.Validator{KeyBook: n.Peerstore},
} }
*/
// TODO: port to lifetimes
// n.proc = goprocessctx.WithContextAndTeardown(ctx, n.teardown)
// TODO: this is a weird circular-ish dependency, rework it /*if err := setupNode(ctx, n, cfg); err != nil {
n.proc = goprocessctx.WithContextAndTeardown(ctx, n.teardown)
if err := setupNode(ctx, n, cfg); err != nil {
n.Close() n.Close()
return nil, err return nil, err
} }*/
if app.Err() != nil {
return nil, app.Err()
}
return n, nil return n, app.Start(ctx)
} }
func isTooManyFDError(err error) bool { func isTooManyFDError(err error) bool {
...@@ -247,6 +307,7 @@ func setupNode(ctx context.Context, n *IpfsNode, cfg *BuildCfg) error { ...@@ -247,6 +307,7 @@ func setupNode(ctx context.Context, n *IpfsNode, cfg *BuildCfg) error {
hostOption = func(ctx context.Context, id peer.ID, ps pstore.Peerstore, options ...libp2p.Option) (p2phost.Host, error) { hostOption = func(ctx context.Context, id peer.ID, ps pstore.Peerstore, options ...libp2p.Option) (p2phost.Host, error) {
return innerHostOption(ctx, id, ps, append(options, libp2p.NoSecurity)...) return innerHostOption(ctx, id, ps, append(options, libp2p.NoSecurity)...)
} }
// TODO: shouldn't this be Errorf to guarantee visibility?
log.Warningf(`Your IPFS node has been configured to run WITHOUT ENCRYPTED CONNECTIONS. log.Warningf(`Your IPFS node has been configured to run WITHOUT ENCRYPTED CONNECTIONS.
You will not be able to connect to any nodes configured to use encrypted connections`) You will not be able to connect to any nodes configured to use encrypted connections`)
} }
......
...@@ -79,8 +79,6 @@ import ( ...@@ -79,8 +79,6 @@ import (
mamask "github.com/whyrusleeping/multiaddr-filter" mamask "github.com/whyrusleeping/multiaddr-filter"
) )
const IpnsValidatorTag = "ipns"
const kReprovideFrequency = time.Hour * 12 const kReprovideFrequency = time.Hour * 12
const discoveryConnTimeout = time.Second * 30 const discoveryConnTimeout = time.Second * 30
const DefaultIpnsCacheSize = 128 const DefaultIpnsCacheSize = 128
...@@ -101,9 +99,9 @@ type IpfsNode struct { ...@@ -101,9 +99,9 @@ type IpfsNode struct {
// Local node // Local node
Pinning pin.Pinner // the pinning manager Pinning pin.Pinner // the pinning manager
Mounts Mounts // current mount state, if any. Mounts Mounts `optional:"true"` // current mount state, if any.
PrivateKey ic.PrivKey // the local node's private Key PrivateKey ic.PrivKey // the local node's private Key
PNetFingerprint []byte // fingerprint of private network PNetFingerprint PNetFingerprint // fingerprint of private network
// Services // Services
Peerstore pstore.Peerstore // storage for other Peer instances Peerstore pstore.Peerstore // storage for other Peer instances
...@@ -115,21 +113,21 @@ type IpfsNode struct { ...@@ -115,21 +113,21 @@ type IpfsNode struct {
DAG ipld.DAGService // the merkle dag service, get/add objects. DAG ipld.DAGService // the merkle dag service, get/add objects.
Resolver *resolver.Resolver // the path resolution system Resolver *resolver.Resolver // the path resolution system
Reporter metrics.Reporter Reporter metrics.Reporter
Discovery discovery.Service Discovery discovery.Service `optional:"true"`
FilesRoot *mfs.Root FilesRoot *mfs.Root
RecordValidator record.Validator RecordValidator record.Validator
// Online // Online
PeerHost p2phost.Host // the network host (server+client) PeerHost p2phost.Host // the network host (server+client)
Bootstrapper io.Closer // the periodic bootstrapper Bootstrapper io.Closer `optional:"true"` // the periodic bootstrapper
Routing routing.IpfsRouting // the routing system. recommend ipfs-dht Routing routing.IpfsRouting // the routing system. recommend ipfs-dht
Exchange exchange.Interface // the block exchange + strategy (bitswap) Exchange exchange.Interface // the block exchange + strategy (bitswap)
Namesys namesys.NameSystem // the name system, resolves paths to hashes Namesys namesys.NameSystem // the name system, resolves paths to hashes
Provider provider.Provider // the value provider system Provider provider.Provider // the value provider system
Reprovider *rp.Reprovider // the value reprovider system Reprovider *rp.Reprovider // the value reprovider system
IpnsRepub *ipnsrp.Republisher IpnsRepub *ipnsrp.Republisher `optional:"true"`
AutoNAT *autonat.AutoNATService AutoNAT *autonat.AutoNATService `optional:"true"`
PubSub *pubsub.PubSub PubSub *pubsub.PubSub
PSRouter *psrouter.PubsubValueStore PSRouter *psrouter.PubsubValueStore
DHT *dht.IpfsDHT DHT *dht.IpfsDHT
...@@ -139,8 +137,8 @@ type IpfsNode struct { ...@@ -139,8 +137,8 @@ type IpfsNode struct {
ctx context.Context ctx context.Context
// Flags // Flags
IsOnline bool // Online is set when networking is enabled. IsOnline bool `optional:"true"` // Online is set when networking is enabled.
IsDaemon bool // Daemon is set when running on a long-running daemon. IsDaemon bool `optional:"true"` // Daemon is set when running on a long-running daemon.
} }
// Mounts defines what the node's mount state is. This should // Mounts defines what the node's mount state is. This should
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论