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

add peer tracker to porcelain with Trust method #3408

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
19 changes: 19 additions & 0 deletions commands/chain.go
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/ipfs/go-cid"
"github.com/ipfs/go-ipfs-cmdkit"
"github.com/ipfs/go-ipfs-cmds"
"github.com/libp2p/go-libp2p-core/peer"

"github.com/filecoin-project/go-filecoin/types"
)
Expand All @@ -22,6 +23,7 @@ var chainCmd = &cmds.Command{
"head": storeHeadCmd,
"ls": storeLsCmd,
"status": storeStatusCmd,
"trust": trustPeerCmd,
},
}

Expand Down Expand Up @@ -122,3 +124,20 @@ var storeStatusCmd = &cmds.Command{
return nil
},
}

var trustPeerCmd = &cmds.Command{
Helptext: cmdkit.HelpText{
Tagline: "Trusts a peer for chain sync operations.",
},
Arguments: []cmdkit.Argument{
cmdkit.StringArg("peerid", true, false, "Base58-encoded libp2p peer ID that the peer tracker will trust"),
},
Run: func(req *cmds.Request, re cmds.ResponseEmitter, env cmds.Environment) error {
trustedPeer, err := peer.IDB58Decode(req.Arguments[0])
if err != nil {
return err
}
GetPorcelainAPI(env).TrustPeer(trustedPeer)
return nil
},
}
8 changes: 8 additions & 0 deletions net/peer_tracker.go
Expand Up @@ -69,6 +69,14 @@ func (tracker *PeerTracker) UpdateTrusted(ctx context.Context) error {
return tracker.updatePeers(ctx, tracker.trustedPeers()...)
}

// Trust adds `pid` to the peer trackers trusted node set.
func (tracker *PeerTracker) Trust(pid peer.ID) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it make sense to include an Untrust as well? If I were playing around with this on the CLI I wouldn't want to make a change I couldn't undo.

tracker.mu.Lock()
defer tracker.mu.Unlock()
tracker.trusted[pid] = struct{}{}
logPeerTracker.Infof("Trusting peer=%s", pid.Pretty())
}

// Track adds information about a given peer.ID
func (tracker *PeerTracker) Track(ci *types.ChainInfo) {
tracker.mu.Lock()
Expand Down
43 changes: 43 additions & 0 deletions net/peer_tracker_test.go
Expand Up @@ -255,3 +255,46 @@ func TestPeerTrackerNetworkDisconnect(t *testing.T) {
tracked := tracker.List()
assert.Equal(t, []*types.ChainInfo{bCI}, tracked)
}

func TestPeerTrackerTrust(t *testing.T) {
tf.UnitTest(t)

tracker := net.NewPeerTracker(peer.ID(""))
pid0 := th.RequireIntPeerID(t, 0)
pid1 := th.RequireIntPeerID(t, 1)
pid2 := th.RequireIntPeerID(t, 2)

tracker.Trust(pid0)
tracker.Trust(pid1)

ci0 := types.NewChainInfo(pid0, types.NewTipSetKey(types.CidFromString(t, "somecid")), 6)
ci1 := types.NewChainInfo(pid1, types.NewTipSetKey(), 0)
ci2 := types.NewChainInfo(pid2, types.NewTipSetKey(types.CidFromString(t, "someothercid")), 90)

tracker.Track(ci0)
tracker.Track(ci1)
tracker.Track(ci2)

// we should get back the best trusted head
ci, err := tracker.SelectHead()
assert.NoError(t, err)
assert.Equal(t, ci, ci0)

// remove them from tracking but not from trusting
tracker.Remove(pid0)
tracker.Remove(pid1)

ci, err = tracker.SelectHead()
assert.Error(t, err)
assert.Nil(t, ci)

// retrack a trusted peer and ensure we still select its head
// this verifies that we still have the peer in out trusted set.
tracker.Track(ci0)

ci, err = tracker.SelectHead()
assert.NoError(t, err)
assert.Equal(t, ci, ci0)
tracker.Track(ci0)

}
1 change: 1 addition & 0 deletions node/builder.go
Expand Up @@ -314,6 +314,7 @@ func (nc *Builder) build(ctx context.Context) (*Node, error) {
MsgWaiter: msg.NewWaiter(chainStore, messageStore, bs, &ipldCborStore),
Network: net.New(peerHost, pubsub.NewPublisher(fsub), pubsub.NewSubscriber(fsub), net.NewRouter(router), bandwidthTracker, net.NewPinger(peerHost, pingService)),
Outbox: outbox,
PeerTracker: peerTracker,
SectorBuilder: nd.SectorBuilder,
Wallet: fcWallet,
}))
Expand Down
8 changes: 8 additions & 0 deletions plumbing/api.go
Expand Up @@ -56,6 +56,7 @@ type API struct {
msgWaiter *msg.Waiter
network *net.Network
outbox *core.Outbox
peerTracker *net.PeerTracker
sectorBuilder func() sectorbuilder.SectorBuilder
storagedeals *strgdls.Store
wallet *wallet.Wallet
Expand All @@ -76,6 +77,7 @@ type APIDeps struct {
MsgWaiter *msg.Waiter
Network *net.Network
Outbox *core.Outbox
PeerTracker *net.PeerTracker
SectorBuilder func() sectorbuilder.SectorBuilder
Wallet *wallet.Wallet
}
Expand All @@ -97,6 +99,7 @@ func New(deps *APIDeps) *API {
msgWaiter: deps.MsgWaiter,
network: deps.Network,
outbox: deps.Outbox,
peerTracker: deps.PeerTracker,
sectorBuilder: deps.SectorBuilder,
storagedeals: deps.Deals,
wallet: deps.Wallet,
Expand Down Expand Up @@ -319,6 +322,11 @@ func (api *API) SignBytes(data []byte, addr address.Address) (types.Signature, e
return api.wallet.SignBytes(data, addr)
}

// TrustPeer adds `p` to the peer trackers trusted node set.
func (api *API) TrustPeer(p peer.ID) {
api.peerTracker.Trust(p)
}

// WalletAddresses gets addresses from the wallet
func (api *API) WalletAddresses() []address.Address {
return api.wallet.Addresses()
Expand Down
26 changes: 26 additions & 0 deletions plumbing/net/peer_tracker.go
@@ -0,0 +1,26 @@
package net

import (
"github.com/libp2p/go-libp2p-core/peer"
)

type peerTracker interface {
Trust(pid peer.ID)
}

// PeerTrackerProvider provides access to the peers currently being tracked.
type PeerTrackerProvider struct {
pt peerTracker
}

// NewPeerTrackerProvider returns a new PeerTrackerProvider
func NewPeerTrackerProvider(pt peerTracker) *PeerTrackerProvider {
return &PeerTrackerProvider{
pt: pt,
}
}

// TrustPeer adds `p` to the peer trackers trusted node set.
func (pt *PeerTrackerProvider) TrustPeer(p peer.ID) {
pt.pt.Trust(p)
}
9 changes: 9 additions & 0 deletions tools/fast/action_chain.go
Expand Up @@ -5,6 +5,7 @@ import (
"encoding/json"

"github.com/ipfs/go-cid"
"github.com/libp2p/go-libp2p-core/peer"

"github.com/filecoin-project/go-filecoin/chain"
)
Expand Down Expand Up @@ -32,3 +33,11 @@ func (f *Filecoin) ChainStatus(ctx context.Context) (*chain.Status, error) {
}
return out, nil
}

// TrustPeer runs the peertrackers trust command against the filecoin process.
func (f *Filecoin) TrustPeer(ctx context.Context, p peer.ID) error {
if _, err := f.RunCmdWithStdin(ctx, nil, "go-filecoin", "chain", "trust", p.Pretty()); err != nil {
return err
}
return nil
}