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

implement RPC system_addReservedPeer #1392

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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions dot/network/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -591,3 +591,8 @@ func (s *Service) Peers() []common.PeerInfo {
func (s *Service) NodeRoles() byte {
return s.cfg.Roles
}

// AddToPeerstore adds this address to the peerstore's AddrBook
func (s *Service) AddToPeerstore(peer peer.AddrInfo) {
s.host.addToPeerstore(peer)
}
2 changes: 2 additions & 0 deletions dot/rpc/modules/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/ChainSafe/gossamer/lib/crypto"
"github.com/ChainSafe/gossamer/lib/runtime"
"github.com/ChainSafe/gossamer/lib/transaction"
"github.com/libp2p/go-libp2p-core/peer"
)

// StorageAPI is the interface for the storage state
Expand Down Expand Up @@ -44,6 +45,7 @@ type NetworkAPI interface {
Stop() error
Start() error
IsStopped() bool
AddToPeerstore(peer peer.AddrInfo)
}

// BlockProducerAPI is the interface for BlockProducer methods
Expand Down
30 changes: 30 additions & 0 deletions dot/rpc/modules/system.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,13 @@
package modules

import (
"errors"
"net/http"
"strings"

"github.com/ChainSafe/gossamer/lib/common"
"github.com/libp2p/go-libp2p-core/peer"
ma "github.com/multiformats/go-multiaddr"
)

// SystemModule is an RPC module providing access to core API points
Expand All @@ -31,6 +35,11 @@ type SystemModule struct {
// EmptyRequest represents an RPC request with no fields
type EmptyRequest struct{}

// StringRequest holds string request
type StringRequest struct {
String string
}

// StringResponse holds the string response
type StringResponse string

Expand Down Expand Up @@ -135,3 +144,24 @@ func (sm *SystemModule) NodeRoles(r *http.Request, req *EmptyRequest, res *[]int
*res = resultArray
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 *[]interface{}) error {
addressParts := strings.Split(req.String, "/p2p/")
if len(addressParts) != 2 {
return errors.New("error parsing address string")
}
maddr, err := ma.NewMultiaddr(addressParts[0])
if err != nil {
return err
}
addrs := []ma.Multiaddr{}
addrs = append(addrs, maddr)
addr := &peer.AddrInfo{
ID: peer.ID(addressParts[1]),
Addrs: addrs,
}

sm.networkAPI.AddToPeerstore(*addr)
return err
}