提交 3f502794 作者: Steven Allen

Merge branch 'feat/autorelay'

License: MIT
Signed-off-by: 's avatarSteven Allen <steven@stebalien.com>
......@@ -68,6 +68,7 @@ import (
metrics "gx/ipfs/QmbYN6UmTJn5UUQdi5CTsU86TXVBSrTcRk5UmyA36Qx2J6/go-libp2p-metrics"
ipld "gx/ipfs/QmcKKBwfz6FyQdHR2jsXrrF6XeSBXYL86anmWNewpFpoF5/go-ipld-format"
logging "gx/ipfs/QmcuXC5cxs79ro2cUuHs4HQ2bkDLJUYokwL8aivcX6HW3C/go-log"
autonat "gx/ipfs/QmdFdMoDmvuEJYsAKRA2BMobzNaeunmc16DqPxdHHfQ25K/go-libp2p-autonat-svc"
merkledag "gx/ipfs/QmdV35UHnL1FM52baPkeUo6u7Fxm2CRUkPTLRPxeF8a4Ap/go-merkledag"
ft "gx/ipfs/QmdYvDbHp7qAhZ7GsCj6e1cMo55ND6y2mjWVzwdvcv4f12/go-unixfs"
nilrouting "gx/ipfs/QmdmWkx54g7VfVyxeG8ic84uf4G6Eq1GohuyKA3XDuJ8oC/go-ipfs-routing/none"
......@@ -134,6 +135,7 @@ type IpfsNode struct {
Reprovider *rp.Reprovider // the value reprovider system
IpnsRepub *ipnsrp.Republisher
AutoNAT *autonat.AutoNATService
PubSub *pubsub.PubSub
PSRouter *psrouter.PubsubValueStore
DHT *dht.IpfsDHT
......@@ -259,13 +261,27 @@ func (n *IpfsNode) startOnlineServices(ctx context.Context, routingOption Routin
libp2pOpts = append(libp2pOpts, libp2p.Transport(quic.NewTransport))
}
// enable routing
libp2pOpts = append(libp2pOpts, libp2p.Routing(func(h p2phost.Host) (routing.PeerRouting, error) {
r, err := routingOption(ctx, h, n.Repo.Datastore(), n.RecordValidator)
n.Routing = r
return r, err
}))
// enable autorelay
if cfg.Swarm.EnableAutoRelay {
libp2pOpts = append(libp2pOpts, libp2p.EnableAutoRelay())
}
peerhost, err := hostOption(ctx, n.Identity, n.Peerstore, libp2pOpts...)
if err != nil {
return err
}
if err := n.startOnlineServicesWithHost(ctx, peerhost, routingOption, pubsub, ipnsps); err != nil {
n.PeerHost = peerhost
if err := n.startOnlineServicesWithHost(ctx, routingOption, pubsub, ipnsps); err != nil {
return err
}
......@@ -459,13 +475,26 @@ func (n *IpfsNode) HandlePeerFound(p pstore.PeerInfo) {
// startOnlineServicesWithHost is the set of services which need to be
// initialized with the host and _before_ we start listening.
func (n *IpfsNode) startOnlineServicesWithHost(ctx context.Context, host p2phost.Host, routingOption RoutingOption, enablePubsub bool, enableIpnsps bool) error {
if enablePubsub || enableIpnsps {
cfg, err := n.Repo.Config()
func (n *IpfsNode) startOnlineServicesWithHost(ctx context.Context, routingOption RoutingOption, enablePubsub bool, enableIpnsps bool) error {
cfg, err := n.Repo.Config()
if err != nil {
return err
}
if cfg.Swarm.EnableAutoNATService {
var opts []libp2p.Option
if cfg.Experimental.QUIC {
opts = append(opts, libp2p.DefaultTransports, libp2p.Transport(quic.NewTransport))
}
svc, err := autonat.NewAutoNATService(ctx, n.PeerHost, opts...)
if err != nil {
return err
}
n.AutoNAT = svc
}
if enablePubsub || enableIpnsps {
var service *pubsub.PubSub
var pubsubOptions []pubsub.Option
......@@ -481,10 +510,10 @@ func (n *IpfsNode) startOnlineServicesWithHost(ctx context.Context, host p2phost
case "":
fallthrough
case "floodsub":
service, err = pubsub.NewFloodSub(ctx, host, pubsubOptions...)
service, err = pubsub.NewFloodSub(ctx, n.PeerHost, pubsubOptions...)
case "gossipsub":
service, err = pubsub.NewGossipSub(ctx, host, pubsubOptions...)
service, err = pubsub.NewGossipSub(ctx, n.PeerHost, pubsubOptions...)
default:
err = fmt.Errorf("Unknown pubsub router %s", cfg.Pubsub.Router)
......@@ -496,12 +525,16 @@ func (n *IpfsNode) startOnlineServicesWithHost(ctx context.Context, host p2phost
n.PubSub = service
}
// setup routing service
r, err := routingOption(ctx, host, n.Repo.Datastore(), n.RecordValidator)
if err != nil {
return err
// this code is necessary just for tests: mock network constructions
// ignore the libp2p constructor options that actually construct the routing!
if n.Routing == nil {
r, err := routingOption(ctx, n.PeerHost, n.Repo.Datastore(), n.RecordValidator)
if err != nil {
return err
}
n.Routing = r
n.PeerHost = rhost.Wrap(n.PeerHost, n.Routing)
}
n.Routing = r
// TODO: I'm not a fan of type assertions like this but the
// `RoutingOption` system doesn't currently provide access to the
......@@ -516,14 +549,14 @@ func (n *IpfsNode) startOnlineServicesWithHost(ctx context.Context, host p2phost
// PSRouter case below.
// 3. Introduce some kind of service manager? (my personal favorite but
// that requires a fair amount of work).
if dht, ok := r.(*dht.IpfsDHT); ok {
if dht, ok := n.Routing.(*dht.IpfsDHT); ok {
n.DHT = dht
}
if enableIpnsps {
n.PSRouter = psrouter.NewPubsubValueStore(
ctx,
host,
n.PeerHost,
n.Routing,
n.PubSub,
n.RecordValidator,
......@@ -543,9 +576,6 @@ func (n *IpfsNode) startOnlineServicesWithHost(ctx context.Context, host p2phost
}
}
// Wrap standard peer host with routing system to allow unknown peer lookups
n.PeerHost = rhost.Wrap(host, n.Routing)
// setup exchange service
bitswapNetwork := bsnet.NewFromIpfsHost(n.PeerHost, n.Routing)
n.Exchange = bitswap.New(ctx, bitswapNetwork, n.Blockstore)
......
......@@ -149,11 +149,11 @@ A boolean value. If set to true, all block reads from disk will be hashed and
verified. This will cause increased CPU utilization.
- `BloomFilterSize`
A number representing the size in bytes of the blockstore's [bloom filter](https://en.wikipedia.org/wiki/Bloom_filter). A value of zero represents the feature being disabled.
A number representing the size in bytes of the blockstore's [bloom filter](https://en.wikipedia.org/wiki/Bloom_filter). A value of zero represents the feature being disabled.
This site generates useful graphs for various bloom filter values: <https://hur.st/bloomfilter/?n=1e6&p=0.01&m=&k=7>
You may use it to find a preferred optimal value, where `m` is `BloomFilterSize` in bits. Remember to convert the value `m` from bits, into bytes for use as `BloomFilterSize` in the config file.
For example, for 1,000,000 blocks, expecting a 1% false positive rate, you'd end up with a filter size of 9592955 bits, so for `BloomFilterSize` we'd want to use 1199120 bytes.
This site generates useful graphs for various bloom filter values: <https://hur.st/bloomfilter/?n=1e6&p=0.01&m=&k=7>
You may use it to find a preferred optimal value, where `m` is `BloomFilterSize` in bits. Remember to convert the value `m` from bits, into bytes for use as `BloomFilterSize` in the config file.
For example, for 1,000,000 blocks, expecting a 1% false positive rate, you'd end up with a filter size of 9592955 bits, so for `BloomFilterSize` we'd want to use 1199120 bytes.
As of writing, [7 hash functions](https://github.com/ipfs/go-ipfs-blockstore/blob/547442836ade055cc114b562a3cc193d4e57c884/caching.go#L22) are used, so the constant `k` is 7 in the formula.
......@@ -164,7 +164,7 @@ Spec defines the structure of the ipfs datastore. It is a composable structure,
This can be changed manually, however, if you make any changes that require a different on-disk structure, you will need to run the [ipfs-ds-convert tool](https://github.com/ipfs/ipfs-ds-convert) to migrate data into the new structures.
For more information on possible values for this configuration option, see docs/datastores.md
For more information on possible values for this configuration option, see docs/datastores.md
Default:
```
......@@ -334,6 +334,17 @@ Disables the p2p-circuit relay transport.
Enables HOP relay for the node. If this is enabled, the node will act as
an intermediate (Hop Relay) node in relay circuits for connected peers.
- `EnableAutoRelay`
Enables automatic relay for this node.
If the node is a HOP relay (`EnableRelayHop` is true) then it will advertise itself as a relay through the DHT.
Otherwise, the node will test its own NAT situation (dialability) using passively discovered AutoNAT services.
If the node is not publicly reachable, then it will seek HOP relays advertised through the DHT and override its public address(es) with relay addresses.
- `EnableAutoNATService`
Enables the AutoNAT service for this node.
The service allows peers to discover their NAT situation by requesting dial backs to their public addresses.
This should only be enabled on publicly reachable nodes.
### `ConnMgr`
The connection manager determines which and how many connections to keep and can be configured to keep.
......
......@@ -27,6 +27,7 @@ the above issue.
- [Directory Sharding / HAMT](#directory-sharding-hamt)
- [IPNS PubSub](#ipns-pubsub)
- [QUIC](#quic)
- [AutoRelay](#autorelay)
---
......@@ -649,3 +650,33 @@ For listening on a QUIC address, add it the swarm addresses, e.g. `/ip4/0.0.0.0/
- [ ] Make sure QUIC connections work reliably
- [ ] Make sure QUIC connection offer equal or better performance than TCP connections on real world networks
- [ ] Finalize libp2p-TLS handshake spec.
## AutoRelay
### In Version
0.4.19-dev
### State
Experimental, disabled by default.
Automatically discovers relays and advertises relay addresses when the node is behind an impenetrable NAT.
### How to enable
Modify your ipfs config:
```
ipfs config --json Swarm.EnableAutoRelay true
```
Bootstrappers (and other public nodes) need to also enable the AutoNATService:
```
ipfs config --json Swarm.EnableAutoNATService true
```
### Road to being a real feature
- [ ] needs testing
......@@ -598,6 +598,12 @@
"hash": "QmWGnBLJhzZ3xV5YQuS6CqczN143YmGAFQTMAdJ31msoK5",
"name": "go-libp2p-http",
"version": "1.1.8"
},
{
"author": "vyzo",
"hash": "QmdFdMoDmvuEJYsAKRA2BMobzNaeunmc16DqPxdHHfQ25K",
"name": "go-libp2p-autonat-svc",
"version": "1.0.3"
}
],
"gxVersion": "0.10.0",
......@@ -606,4 +612,3 @@
"name": "go-ipfs",
"version": "0.4.18"
}
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论