Build libp2p node
License: MIT Signed-off-by: Jakub Sztandera <kubuxu@protonmail.ch>
This commit is contained in:
parent
63627e863e
commit
0779e7dc00
28
go.mod
28
go.mod
@ -4,17 +4,29 @@ go 1.12
|
||||
|
||||
require (
|
||||
github.com/gorilla/rpc v1.2.0
|
||||
github.com/ipfs/go-ipfs-routing v0.1.0 // indirect
|
||||
github.com/libp2p/go-libp2p v0.2.0 // indirect
|
||||
github.com/ipfs/go-datastore v0.0.5
|
||||
github.com/ipfs/go-ipfs-routing v0.1.0
|
||||
github.com/ipfs/go-log v0.0.1
|
||||
github.com/libp2p/go-libp2p v0.2.0
|
||||
github.com/libp2p/go-libp2p-autonat-svc v0.1.0 // indirect
|
||||
github.com/libp2p/go-libp2p-connmgr v0.1.0 // indirect
|
||||
github.com/libp2p/go-libp2p-circuit v0.1.0
|
||||
github.com/libp2p/go-libp2p-connmgr v0.1.0
|
||||
github.com/libp2p/go-libp2p-core v0.0.6
|
||||
github.com/libp2p/go-libp2p-kad-dht v0.1.1 // indirect
|
||||
github.com/libp2p/go-libp2p-pubsub v0.1.0 // indirect
|
||||
github.com/libp2p/go-libp2p-quic-transport v0.1.1 // indirect
|
||||
github.com/libp2p/go-libp2p-routing-helpers v0.1.0 // indirect
|
||||
github.com/libp2p/go-libp2p-tls v0.1.0 // indirect
|
||||
github.com/libp2p/go-libp2p-discovery v0.1.0
|
||||
github.com/libp2p/go-libp2p-kad-dht v0.1.1
|
||||
github.com/libp2p/go-libp2p-mplex v0.2.1
|
||||
github.com/libp2p/go-libp2p-peerstore v0.1.1
|
||||
github.com/libp2p/go-libp2p-pubsub v0.1.0
|
||||
github.com/libp2p/go-libp2p-quic-transport v0.1.1
|
||||
github.com/libp2p/go-libp2p-record v0.1.0
|
||||
github.com/libp2p/go-libp2p-routing-helpers v0.1.0
|
||||
github.com/libp2p/go-libp2p-secio v0.1.0
|
||||
github.com/libp2p/go-libp2p-tls v0.1.0
|
||||
github.com/libp2p/go-libp2p-yamux v0.2.1
|
||||
github.com/libp2p/go-maddr-filter v0.0.4
|
||||
github.com/multiformats/go-multiaddr v0.0.4
|
||||
github.com/multiformats/go-multihash v0.0.5
|
||||
github.com/whyrusleeping/multiaddr-filter v0.0.0-20160516205228-e903e4adabd7
|
||||
go.uber.org/atomic v1.4.0 // indirect
|
||||
go.uber.org/dig v1.7.0 // indirect
|
||||
go.uber.org/fx v1.9.0
|
||||
|
4
go.sum
4
go.sum
@ -139,6 +139,7 @@ github.com/libp2p/go-libp2p-mplex v0.2.1 h1:E1xaJBQnbSiTHGI1gaBKmKhu1TUKkErKJnE8
|
||||
github.com/libp2p/go-libp2p-mplex v0.2.1/go.mod h1:SC99Rxs8Vuzrf/6WhmH41kNn13TiYdAWNYHrwImKLnE=
|
||||
github.com/libp2p/go-libp2p-nat v0.0.4 h1:+KXK324yaY701On8a0aGjTnw8467kW3ExKcqW2wwmyw=
|
||||
github.com/libp2p/go-libp2p-nat v0.0.4/go.mod h1:N9Js/zVtAXqaeT99cXgTV9e75KpnWCvVOiGzlcHmBbY=
|
||||
github.com/libp2p/go-libp2p-netutil v0.1.0 h1:zscYDNVEcGxyUpMd0JReUZTrpMfia8PmLKcKF72EAMQ=
|
||||
github.com/libp2p/go-libp2p-netutil v0.1.0/go.mod h1:3Qv/aDqtMLTUyQeundkKsA+YCThNdbQD54k3TqjpbFU=
|
||||
github.com/libp2p/go-libp2p-peer v0.2.0/go.mod h1:RCffaCvUyW2CJmG2gAWVqwePwW7JMgxjsHm7+J5kjWY=
|
||||
github.com/libp2p/go-libp2p-peerstore v0.1.0/go.mod h1:2CeHkQsr8svp4fZ+Oi9ykN1HBb6u0MOvdJ7YIsmcwtY=
|
||||
@ -160,6 +161,7 @@ github.com/libp2p/go-libp2p-swarm v0.1.0 h1:HrFk2p0awrGEgch9JXK/qp/hfjqQfgNxpLWn
|
||||
github.com/libp2p/go-libp2p-swarm v0.1.0/go.mod h1:wQVsCdjsuZoc730CgOvh5ox6K8evllckjebkdiY5ta4=
|
||||
github.com/libp2p/go-libp2p-testing v0.0.2/go.mod h1:gvchhf3FQOtBdr+eFUABet5a4MBLK8jM3V4Zghvmi+E=
|
||||
github.com/libp2p/go-libp2p-testing v0.0.3/go.mod h1:gvchhf3FQOtBdr+eFUABet5a4MBLK8jM3V4Zghvmi+E=
|
||||
github.com/libp2p/go-libp2p-testing v0.0.4 h1:Qev57UR47GcLPXWjrunv5aLIQGO4n9mhI/8/EIrEEFc=
|
||||
github.com/libp2p/go-libp2p-testing v0.0.4/go.mod h1:gvchhf3FQOtBdr+eFUABet5a4MBLK8jM3V4Zghvmi+E=
|
||||
github.com/libp2p/go-libp2p-tls v0.1.0 h1:o4bjjAdnUjNgJoPoDd0wUaZH7K+EenlNWJpgyXB3ulA=
|
||||
github.com/libp2p/go-libp2p-tls v0.1.0/go.mod h1:VZdoSWQDeNpIIAFJFv+6uqTqpnIIDHcqZQSTC/A1TT0=
|
||||
@ -201,6 +203,7 @@ github.com/mattn/go-colorable v0.1.1 h1:G1f5SKeVxmagw/IyvzvtZE4Gybcc4Tr1tf7I8z0X
|
||||
github.com/mattn/go-colorable v0.1.1/go.mod h1:FuOcm+DKB9mbwrcAfNl7/TZVBZ6rcnceauSikq3lYCQ=
|
||||
github.com/mattn/go-isatty v0.0.5 h1:tHXDdz1cpzGaovsTB+TVB8q90WEokoVmfMqoVcrLUgw=
|
||||
github.com/mattn/go-isatty v0.0.5/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s=
|
||||
github.com/miekg/dns v1.1.12 h1:WMhc1ik4LNkTg8U9l3hI1LvxKmIL+f1+WV/SZtCbDDA=
|
||||
github.com/miekg/dns v1.1.12/go.mod h1:W1PPwlIAgtquWBMBEV9nkV9Cazfe8ScdGz/Lj7v3Nrg=
|
||||
github.com/minio/blake2b-simd v0.0.0-20160723061019-3f5f724cb5b1 h1:lYpkrQH5ajf0OXOcUbGjvZxxijuBwbbmlSxLiuofa+g=
|
||||
github.com/minio/blake2b-simd v0.0.0-20160723061019-3f5f724cb5b1/go.mod h1:pD8RvIylQ358TN4wwqatJ8rNavkEINozVn9DtGI3dfQ=
|
||||
@ -259,6 +262,7 @@ github.com/whyrusleeping/go-notifier v0.0.0-20170827234753-097c5d47330f h1:M/lL3
|
||||
github.com/whyrusleeping/go-notifier v0.0.0-20170827234753-097c5d47330f/go.mod h1:cZNvX9cFybI01GriPRMXDtczuvUhgbcYr9iCGaNlRv8=
|
||||
github.com/whyrusleeping/mafmt v1.2.8 h1:TCghSl5kkwEE0j+sU/gudyhVMRlpBin8fMBBHg59EbA=
|
||||
github.com/whyrusleeping/mafmt v1.2.8/go.mod h1:faQJFPbLSxzD9xpA02ttW/tS9vZykNvXwGvqIpk20FA=
|
||||
github.com/whyrusleeping/mdns v0.0.0-20180901202407-ef14215e6b30 h1:nMCC9Pwz1pxfC1Y6mYncdk+kq8d5aLx0Q+/gyZGE44M=
|
||||
github.com/whyrusleeping/mdns v0.0.0-20180901202407-ef14215e6b30/go.mod h1:j4l84WPFclQPj320J9gp0XwNKBb3U0zt5CBqjPp22G4=
|
||||
github.com/whyrusleeping/multiaddr-filter v0.0.0-20160516205228-e903e4adabd7 h1:E9S12nwJwEOXe2d6gT6qxdvqMnNq+VnSsKPgm2ZZNds=
|
||||
github.com/whyrusleeping/multiaddr-filter v0.0.0-20160516205228-e903e4adabd7/go.mod h1:X2c0RVCI1eSUFI8eLcY3c0423ykwiUdxLJtkDvruhjI=
|
||||
|
137
node/builder.go
137
node/builder.go
@ -2,20 +2,68 @@ package node
|
||||
|
||||
import (
|
||||
"context"
|
||||
"reflect"
|
||||
"time"
|
||||
|
||||
"github.com/ipfs/go-datastore"
|
||||
ci "github.com/libp2p/go-libp2p-core/crypto"
|
||||
"github.com/libp2p/go-libp2p-core/peer"
|
||||
"github.com/libp2p/go-libp2p-peerstore/pstoremem"
|
||||
"go.uber.org/fx"
|
||||
|
||||
"github.com/filecoin-project/go-lotus/api"
|
||||
"github.com/filecoin-project/go-lotus/build"
|
||||
"github.com/filecoin-project/go-lotus/node/modules"
|
||||
"github.com/filecoin-project/go-lotus/node/modules/helpers"
|
||||
"github.com/filecoin-project/go-lotus/node/modules/libp2p"
|
||||
)
|
||||
|
||||
var defaultListenAddrs = []string{ // TODO: better defaults?
|
||||
"/ip4/0.0.0.0/tcp/4001",
|
||||
"/ip6/::/tcp/4001",
|
||||
}
|
||||
|
||||
func New(ctx context.Context) (api.API, error) {
|
||||
var resApi api.Struct
|
||||
|
||||
online := true
|
||||
|
||||
app := fx.New(
|
||||
fx.Provide(modules.RandomPeerID),
|
||||
fx.Provide(as(ctx, new(helpers.MetricsCtx))),
|
||||
|
||||
//fx.Provide(modules.RandomPeerID),
|
||||
randomIdentity(),
|
||||
memrepo(),
|
||||
|
||||
fx.Provide(modules.RecordValidator),
|
||||
|
||||
ifOpt(online,
|
||||
fx.Provide(
|
||||
pstoremem.NewPeerstore,
|
||||
|
||||
libp2p.DefaultTransports,
|
||||
libp2p.Host,
|
||||
libp2p.RoutedHost,
|
||||
libp2p.DHTRouting(false),
|
||||
|
||||
libp2p.DiscoveryHandler,
|
||||
libp2p.AddrsFactory(nil, nil),
|
||||
libp2p.SmuxTransport(true),
|
||||
libp2p.Relay(true, false),
|
||||
libp2p.Security(true, false),
|
||||
|
||||
libp2p.BaseRouting,
|
||||
libp2p.Routing,
|
||||
|
||||
libp2p.NatPortMap,
|
||||
libp2p.ConnectionManager(50, 200, 20 * time.Second),
|
||||
),
|
||||
|
||||
fx.Invoke(
|
||||
libp2p.PstoreAddSelfKeys,
|
||||
libp2p.StartListening(defaultListenAddrs),
|
||||
),
|
||||
),
|
||||
|
||||
fx.Invoke(versionApi(&resApi.Internal.Version)),
|
||||
fx.Invoke(idApi(&resApi.Internal.ID)),
|
||||
@ -28,6 +76,40 @@ func New(ctx context.Context) (api.API, error) {
|
||||
return &resApi, nil
|
||||
}
|
||||
|
||||
// In-memory / testing
|
||||
|
||||
func memrepo() fx.Option {
|
||||
return fx.Provide(
|
||||
func() datastore.Batching {
|
||||
return datastore.NewMapDatastore()
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
func randomIdentity() fx.Option {
|
||||
sk, pk, err := ci.GenerateKeyPair(ci.RSA, 512)
|
||||
if err != nil {
|
||||
return fx.Error(err)
|
||||
}
|
||||
|
||||
return fx.Options(
|
||||
fx.Provide(as(sk, new(ci.PrivKey))),
|
||||
fx.Provide(as(pk, new(ci.PubKey))),
|
||||
fx.Provide(peer.IDFromPublicKey),
|
||||
)
|
||||
}
|
||||
|
||||
// UTILS
|
||||
|
||||
func ifOpt(cond bool, options ...fx.Option) fx.Option {
|
||||
if cond {
|
||||
return fx.Options(options...)
|
||||
}
|
||||
return fx.Options()
|
||||
}
|
||||
|
||||
// API IMPL
|
||||
|
||||
// TODO: figure out a better way, this isn't usable in long term
|
||||
func idApi(set *func(ctx context.Context) (peer.ID, error)) func(id peer.ID) {
|
||||
return func(id peer.ID) {
|
||||
@ -46,3 +128,56 @@ func versionApi(set *func(context.Context) (api.Version, error)) func() {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// from go-ipfs
|
||||
// as casts input constructor to a given interface (if a value is given, it
|
||||
// wraps it into a constructor).
|
||||
//
|
||||
// Note: this method may look like a hack, and in fact it is one.
|
||||
// This is here only because https://github.com/uber-go/fx/issues/673 wasn't
|
||||
// released yet
|
||||
//
|
||||
// Note 2: when making changes here, make sure this method stays at
|
||||
// 100% coverage. This makes it less likely it will be terribly broken
|
||||
func as(in interface{}, as interface{}) interface{} {
|
||||
outType := reflect.TypeOf(as)
|
||||
|
||||
if outType.Kind() != reflect.Ptr {
|
||||
panic("outType is not a pointer")
|
||||
}
|
||||
|
||||
if reflect.TypeOf(in).Kind() != reflect.Func {
|
||||
ctype := reflect.FuncOf(nil, []reflect.Type{outType.Elem()}, false)
|
||||
|
||||
return reflect.MakeFunc(ctype, func(args []reflect.Value) (results []reflect.Value) {
|
||||
out := reflect.New(outType.Elem())
|
||||
out.Elem().Set(reflect.ValueOf(in))
|
||||
|
||||
return []reflect.Value{out.Elem()}
|
||||
}).Interface()
|
||||
}
|
||||
|
||||
inType := reflect.TypeOf(in)
|
||||
|
||||
ins := make([]reflect.Type, inType.NumIn())
|
||||
outs := make([]reflect.Type, inType.NumOut())
|
||||
|
||||
for i := range ins {
|
||||
ins[i] = inType.In(i)
|
||||
}
|
||||
outs[0] = outType.Elem()
|
||||
for i := range outs[1:] {
|
||||
outs[i+1] = inType.Out(i + 1)
|
||||
}
|
||||
|
||||
ctype := reflect.FuncOf(ins, outs, false)
|
||||
|
||||
return reflect.MakeFunc(ctype, func(args []reflect.Value) (results []reflect.Value) {
|
||||
outs := reflect.ValueOf(in).Call(args)
|
||||
out := reflect.New(outType.Elem())
|
||||
out.Elem().Set(outs[0])
|
||||
outs[0] = out.Elem()
|
||||
|
||||
return outs
|
||||
}).Interface()
|
||||
}
|
||||
|
@ -1 +1,13 @@
|
||||
package modules
|
||||
|
||||
import (
|
||||
"github.com/libp2p/go-libp2p-core/peerstore"
|
||||
record "github.com/libp2p/go-libp2p-record"
|
||||
)
|
||||
|
||||
// RecordValidator provides namesys compatible routing record validator
|
||||
func RecordValidator(ps peerstore.Peerstore) record.Validator {
|
||||
return record.NamespacedValidator{
|
||||
"pk": record.PublicKeyValidator{},
|
||||
}
|
||||
}
|
||||
|
@ -1,5 +1,9 @@
|
||||
package libp2p
|
||||
|
||||
import (
|
||||
"github.com/libp2p/go-libp2p"
|
||||
)
|
||||
|
||||
/*import (
|
||||
"github.com/libp2p/go-libp2p"
|
||||
autonat "github.com/libp2p/go-libp2p-autonat-svc"
|
||||
@ -12,8 +16,6 @@ package libp2p
|
||||
"github.com/filecoin-project/go-lotus/node/modules/helpers"
|
||||
)
|
||||
|
||||
var NatPortMap = simpleOpt(libp2p.NATPortMap())
|
||||
|
||||
func AutoNATService(quic bool) func(repo repo.Repo, mctx helpers.MetricsCtx, lc fx.Lifecycle, host host.Host) error {
|
||||
return func(repo repo.Repo, mctx helpers.MetricsCtx, lc fx.Lifecycle, host host.Host) error {
|
||||
// collect private net option in case swarm.key is presented
|
||||
@ -31,4 +33,6 @@ func AutoNATService(quic bool) func(repo repo.Repo, mctx helpers.MetricsCtx, lc
|
||||
return err
|
||||
}
|
||||
}
|
||||
*/
|
||||
*/
|
||||
|
||||
var NatPortMap = simpleOpt(libp2p.NATPortMap())
|
||||
|
Loading…
Reference in New Issue
Block a user