Merge pull request #948 from filecoin-project/feat/sybill-fun
Basic sybill resistance mechanisms
This commit is contained in:
+8
-1
@@ -100,6 +100,7 @@ const (
|
||||
// daemon
|
||||
ExtractApiKey
|
||||
HeadMetricsKey
|
||||
RunPeerTaggerKey
|
||||
|
||||
SetApiEndpointKey
|
||||
|
||||
@@ -155,7 +156,8 @@ func libp2p() Option {
|
||||
Override(new(routing.Routing), lp2p.Routing),
|
||||
|
||||
Override(NatPortMapKey, lp2p.NatPortMap),
|
||||
Override(ConnectionManagerKey, lp2p.ConnectionManager(50, 200, 20*time.Second)),
|
||||
|
||||
Override(ConnectionManagerKey, lp2p.ConnectionManager(50, 200, 20*time.Second, nil)),
|
||||
|
||||
Override(new(*pubsub.PubSub), lp2p.GossipSub()),
|
||||
|
||||
@@ -296,6 +298,11 @@ func ConfigCommon(cfg *config.Common) Option {
|
||||
|
||||
ApplyIf(func(s *Settings) bool { return s.Online },
|
||||
Override(StartListeningKey, lp2p.StartListening(cfg.Libp2p.ListenAddresses)),
|
||||
Override(ConnectionManagerKey, lp2p.ConnectionManager(
|
||||
cfg.Libp2p.ConnMgrLow,
|
||||
cfg.Libp2p.ConnMgrHigh,
|
||||
time.Duration(cfg.Libp2p.ConnMgrGrace),
|
||||
cfg.Libp2p.ProtectedPeers)),
|
||||
|
||||
ApplyIf(func(s *Settings) bool { return len(cfg.Libp2p.BootstrapPeers) > 0 },
|
||||
Override(new(dtypes.BootstrapPeers), modules.ConfigBootstrap(cfg.Libp2p.BootstrapPeers)),
|
||||
|
||||
@@ -36,6 +36,11 @@ type API struct {
|
||||
type Libp2p struct {
|
||||
ListenAddresses []string
|
||||
BootstrapPeers []string
|
||||
ProtectedPeers []string
|
||||
|
||||
ConnMgrLow uint
|
||||
ConnMgrHigh uint
|
||||
ConnMgrGrace Duration
|
||||
}
|
||||
|
||||
// // Full Node
|
||||
@@ -67,6 +72,10 @@ func defCommon() Common {
|
||||
"/ip4/0.0.0.0/tcp/0",
|
||||
"/ip6/::/tcp/0",
|
||||
},
|
||||
|
||||
ConnMgrLow: 50,
|
||||
ConnMgrHigh: 200,
|
||||
ConnMgrGrace: Duration(20 * time.Second),
|
||||
},
|
||||
}
|
||||
|
||||
|
||||
+5
-3
@@ -38,7 +38,7 @@ type Message struct {
|
||||
|
||||
type NewStreamFunc func(context.Context, peer.ID, ...protocol.ID) (inet.Stream, error)
|
||||
type Service struct {
|
||||
newStream NewStreamFunc
|
||||
h host.Host
|
||||
|
||||
cs *store.ChainStore
|
||||
syncer *chain.Syncer
|
||||
@@ -51,7 +51,7 @@ func NewHelloService(h host.Host, cs *store.ChainStore, syncer *chain.Syncer, pm
|
||||
}
|
||||
|
||||
return &Service{
|
||||
newStream: h.NewStream,
|
||||
h: h,
|
||||
|
||||
cs: cs,
|
||||
syncer: syncer,
|
||||
@@ -99,6 +99,8 @@ func (hs *Service) HandleStream(s inet.Stream) {
|
||||
}
|
||||
|
||||
if ts.TipSet().Height() > 0 {
|
||||
hs.h.ConnManager().TagPeer(s.Conn().RemotePeer(), "fcpeer", 10)
|
||||
|
||||
// don't bother informing about genesis
|
||||
log.Infof("Got new tipset through Hello: %s from %s", ts.Cids(), s.Conn().RemotePeer())
|
||||
hs.syncer.InformNewHead(s.Conn().RemotePeer(), ts)
|
||||
@@ -110,7 +112,7 @@ func (hs *Service) HandleStream(s inet.Stream) {
|
||||
}
|
||||
|
||||
func (hs *Service) SayHello(ctx context.Context, pid peer.ID) error {
|
||||
s, err := hs.newStream(ctx, pid, ProtocolID)
|
||||
s, err := hs.h.NewStream(ctx, pid, ProtocolID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
+1
-12
@@ -223,18 +223,7 @@ func (a *StateAPI) StateGetReceipt(ctx context.Context, msg cid.Cid, ts *types.T
|
||||
}
|
||||
|
||||
func (a *StateAPI) StateListMiners(ctx context.Context, ts *types.TipSet) ([]address.Address, error) {
|
||||
var state actors.StoragePowerState
|
||||
if _, err := a.StateManager.LoadActorState(ctx, actors.StoragePowerAddress, &state, ts); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
cst := hamt.CSTFromBstore(a.StateManager.ChainStore().Blockstore())
|
||||
miners, err := actors.MinerSetList(ctx, cst, state.Miners)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return miners, nil
|
||||
return stmgr.ListMinerActors(ctx, a.StateManager, ts)
|
||||
}
|
||||
|
||||
func (a *StateAPI) StateListActors(ctx context.Context, ts *types.TipSet) ([]address.Address, error) {
|
||||
|
||||
@@ -16,6 +16,7 @@ import (
|
||||
mocknet "github.com/libp2p/go-libp2p/p2p/net/mock"
|
||||
"go.uber.org/fx"
|
||||
|
||||
"github.com/filecoin-project/lotus/build"
|
||||
"github.com/filecoin-project/lotus/node/modules/dtypes"
|
||||
"github.com/filecoin-project/lotus/node/modules/helpers"
|
||||
)
|
||||
@@ -41,7 +42,7 @@ func Host(mctx helpers.MetricsCtx, lc fx.Lifecycle, params P2PHostIn) (RawHost,
|
||||
return nil, fmt.Errorf("missing private key for node ID: %s", params.ID.Pretty())
|
||||
}
|
||||
|
||||
opts := []libp2p.Option{libp2p.Identity(pkey), libp2p.Peerstore(params.Peerstore), libp2p.NoListenAddrs}
|
||||
opts := []libp2p.Option{libp2p.Identity(pkey), libp2p.Peerstore(params.Peerstore), libp2p.NoListenAddrs, libp2p.UserAgent("lotus-" + build.Version)}
|
||||
for _, o := range params.Opts {
|
||||
opts = append(opts, o...)
|
||||
}
|
||||
|
||||
@@ -4,6 +4,7 @@ import (
|
||||
"crypto/rand"
|
||||
"time"
|
||||
|
||||
"github.com/filecoin-project/lotus/build"
|
||||
"github.com/filecoin-project/lotus/chain/types"
|
||||
"golang.org/x/xerrors"
|
||||
|
||||
@@ -63,11 +64,30 @@ func genLibp2pKey() (crypto.PrivKey, error) {
|
||||
|
||||
// Misc options
|
||||
|
||||
func ConnectionManager(low, high int, grace time.Duration) func() (opts Libp2pOpts, err error) {
|
||||
return func() (opts Libp2pOpts, err error) {
|
||||
cm := connmgr.NewConnManager(low, high, grace)
|
||||
opts.Opts = append(opts.Opts, libp2p.ConnectionManager(cm))
|
||||
return
|
||||
func ConnectionManager(low, high uint, grace time.Duration, protected []string) func() (opts Libp2pOpts, err error) {
|
||||
return func() (Libp2pOpts, error) {
|
||||
cm := connmgr.NewConnManager(int(low), int(high), grace)
|
||||
for _, p := range protected {
|
||||
pid, err := peer.IDFromString(p)
|
||||
if err != nil {
|
||||
return Libp2pOpts{}, xerrors.Errorf("failed to parse peer ID in protected peers array: %w", err)
|
||||
}
|
||||
|
||||
cm.Protect(pid, "config-prot")
|
||||
}
|
||||
|
||||
infos, err := build.BuiltinBootstrap()
|
||||
if err != nil {
|
||||
return Libp2pOpts{}, xerrors.Errorf("failed to get bootstrap peers: %w", err)
|
||||
}
|
||||
|
||||
for _, inf := range infos {
|
||||
cm.Protect(inf.ID, "bootstrap")
|
||||
}
|
||||
|
||||
return Libp2pOpts{
|
||||
Opts: []libp2p.Option{libp2p.ConnectionManager(cm)},
|
||||
}, nil
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -43,7 +43,7 @@ func RunBlockSync(h host.Host, svc *blocksync.BlockSyncService) {
|
||||
h.SetStreamHandler(blocksync.BlockSyncProtocolID, svc.HandleStream)
|
||||
}
|
||||
|
||||
func HandleIncomingBlocks(mctx helpers.MetricsCtx, lc fx.Lifecycle, pubsub *pubsub.PubSub, s *chain.Syncer) {
|
||||
func HandleIncomingBlocks(mctx helpers.MetricsCtx, lc fx.Lifecycle, pubsub *pubsub.PubSub, s *chain.Syncer, h host.Host) {
|
||||
ctx := helpers.LifecycleCtx(mctx, lc)
|
||||
|
||||
blocksub, err := pubsub.Subscribe("/fil/blocks")
|
||||
@@ -51,7 +51,7 @@ func HandleIncomingBlocks(mctx helpers.MetricsCtx, lc fx.Lifecycle, pubsub *pubs
|
||||
panic(err)
|
||||
}
|
||||
|
||||
go sub.HandleIncomingBlocks(ctx, blocksub, s)
|
||||
go sub.HandleIncomingBlocks(ctx, blocksub, s, h.ConnManager())
|
||||
}
|
||||
|
||||
func HandleIncomingMessages(mctx helpers.MetricsCtx, lc fx.Lifecycle, pubsub *pubsub.PubSub, mpool *messagepool.MessagePool) {
|
||||
|
||||
Reference in New Issue
Block a user