-
Notifications
You must be signed in to change notification settings - Fork 124
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
feat(dot/rpc/modules): add system_addReservedPeer
and system_removeReservedPeer
RPC call
#1712
Changes from 16 commits
d844913
8099d21
965b843
3e7812b
8455f22
240326b
0e90758
cfeac1a
ef3dec0
31f4255
7984fb1
c060382
77f003a
f3a0e0d
3bb15b6
0fd592f
2a6f328
5c0b997
774b438
08214a7
e0a4a14
5d90025
95439dc
75e09d3
d5749ac
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -368,6 +368,52 @@ func (h *host) peers() []peer.ID { | |
return h.h.Network().Peers() | ||
} | ||
|
||
// addReservedPeers adds the peers `addrs` to the protected peers list and connects to them | ||
func (h *host) addReservedPeers(addrs ...string) error { | ||
if len(addrs) < 1 { | ||
return nil | ||
} | ||
|
||
for _, addr := range addrs { | ||
maddr, err := ma.NewMultiaddr(addr) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
addinfo, err := peer.AddrInfoFromP2pAddr(maddr) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
h.h.ConnManager().Protect(addinfo.ID, "") | ||
if err := h.connect(*addinfo); err != nil { | ||
return err | ||
} | ||
} | ||
|
||
return nil | ||
} | ||
|
||
// removeReservedPeers will remove the given peers from the protected peers list | ||
func (h *host) removeReservedPeers(ids ...string) error { | ||
if len(ids) < 1 { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. unnecessary check IMO There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done! |
||
return nil | ||
} | ||
|
||
for _, id := range ids { | ||
peerID, err := peer.Decode(id) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if !h.h.ConnManager().Unprotect(peerID, "") { | ||
continue | ||
} | ||
} | ||
|
||
return nil | ||
} | ||
|
||
// supportsProtocol checks if the protocol is supported by peerID | ||
// returns an error if could not get peer protocols | ||
func (h *host) supportsProtocol(peerID peer.ID, protocol protocol.ID) (bool, error) { | ||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -280,3 +280,21 @@ func (sm *SystemModule) LocalPeerId(r *http.Request, req *EmptyRequest, res *str | |
*res = base58.Encode([]byte(netstate.PeerID)) | ||
return nil | ||
} | ||
|
||
// AddReservedPeer adds a reserved peer. The string parameter should encode a p2p multiaddr. | ||
func (sm *SystemModule) AddReservedPeer(r *http.Request, req *StringRequest, res *[]byte) error { | ||
if req.String == "" { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should you trim just in case? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done! |
||
return errors.New("cannot add an empty reserved peer") | ||
} | ||
|
||
return sm.networkAPI.AddReservedPeers(req.String) | ||
} | ||
|
||
// RemoveReservedPeer remove a reserved peer. The string should encode only the PeerId | ||
func (sm *SystemModule) RemoveReservedPeer(r *http.Request, req *StringRequest, res *[]byte) error { | ||
if req.String == "" { | ||
return errors.New("cannot remove an empty reserved peer") | ||
} | ||
|
||
return sm.networkAPI.RemoveReservedPeers(req.String) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
unnecessary check IMO
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done!