From 550bac1e41d364c66d46c6ea75878cf605002616 Mon Sep 17 00:00:00 2001 From: jon4hz Date: Mon, 30 May 2022 02:06:07 +0200 Subject: [PATCH 1/3] chore: add .gitignore --- .gitignore | 1 + 1 file changed, 1 insertion(+) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 00000000..722d5e71 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +.vscode From c0d624918fb83e30f1e0f138868c02bf76d97621 Mon Sep 17 00:00:00 2001 From: jon4hz Date: Mon, 30 May 2022 02:56:32 +0200 Subject: [PATCH 2/3] feat: add rpc method to get infos about connected peers --- .gitignore | 2 + cmd/derod/rpc/rpc_dero_getpeers.go | 27 ++++++++++ cmd/derod/rpc/websocket_server.go | 57 ++++++++++----------- p2p/peer_pool.go | 79 ++++++++++++++++++------------ rpc/daemon_rpc.go | 4 +- 5 files changed, 108 insertions(+), 61 deletions(-) create mode 100644 cmd/derod/rpc/rpc_dero_getpeers.go diff --git a/.gitignore b/.gitignore index 722d5e71..9a397f62 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,3 @@ .vscode +cmd/derod/mainnet +cmd/derod/testnet \ No newline at end of file diff --git a/cmd/derod/rpc/rpc_dero_getpeers.go b/cmd/derod/rpc/rpc_dero_getpeers.go new file mode 100644 index 00000000..f5892a37 --- /dev/null +++ b/cmd/derod/rpc/rpc_dero_getpeers.go @@ -0,0 +1,27 @@ +// Copyright 2017-2021 DERO Project. All rights reserved. +// Use of this source code in any form is governed by RESEARCH license. +// license can be found in the LICENSE file. +// GPG: 0F39 E425 8C65 3947 702A 8234 08B2 0360 A03A 9DE8 +// +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY +// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF +// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL +// THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, +// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, +// STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF +// THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +package rpc + +import ( + "context" + + "github.com/deroproject/derohe/p2p" +) + +func GetPeersInfo(ctx context.Context) (result *p2p.PeersInfo) { + return p2p.GetPeersInfo() +} diff --git a/cmd/derod/rpc/websocket_server.go b/cmd/derod/rpc/websocket_server.go index 63549572..a035f80d 100644 --- a/cmd/derod/rpc/websocket_server.go +++ b/cmd/derod/rpc/websocket_server.go @@ -16,36 +16,36 @@ package rpc -import "io" -import "os" -import "net" -import "fmt" -import "net/http" -import "net/http/pprof" -import "time" -import "sort" -import "sync" -import "sync/atomic" -import "context" -import "strings" -import "runtime/debug" -import "encoding/json" - -import "github.com/deroproject/derohe/config" -import "github.com/deroproject/derohe/globals" -import "github.com/deroproject/derohe/blockchain" -import "github.com/deroproject/derohe/glue/rwc" -import "github.com/deroproject/derohe/metrics" - -import "github.com/go-logr/logr" -import "github.com/gorilla/websocket" - -import "github.com/creachadair/jrpc2" -import "github.com/creachadair/jrpc2/handler" -import "github.com/creachadair/jrpc2/channel" +import ( + "context" + "encoding/json" + "fmt" + "io" + "net" + "net/http" + "net/http/pprof" + "os" + "runtime/debug" + "sort" + "strings" + "sync" + "sync/atomic" + "time" + + "github.com/creachadair/jrpc2" + "github.com/creachadair/jrpc2/channel" + "github.com/creachadair/jrpc2/handler" + "github.com/creachadair/jrpc2/jhttp" + "github.com/deroproject/derohe/blockchain" + "github.com/deroproject/derohe/config" + "github.com/deroproject/derohe/globals" + "github.com/deroproject/derohe/glue/rwc" + "github.com/deroproject/derohe/metrics" + "github.com/go-logr/logr" + "github.com/gorilla/websocket" +) //import "github.com/creachadair/jrpc2/server" -import "github.com/creachadair/jrpc2/jhttp" /* this file implements the rpcserver api, so as wallet and block explorer tools can work without migration */ @@ -338,6 +338,7 @@ var servicemux = handler.ServiceMap{ "GetSC": handler.New(GetSC), "GetGasEstimate": handler.New(GetGasEstimate), "NameToAddress": handler.New(NameToAddress), + "GetPeers": handler.New(GetPeersInfo), }, "DAEMON": handler.Map{ "Echo": handler.New(DAEMON_Echo), diff --git a/p2p/peer_pool.go b/p2p/peer_pool.go index c48348fe..3976df12 100644 --- a/p2p/peer_pool.go +++ b/p2p/peer_pool.go @@ -19,23 +19,24 @@ package p2p /* this file implements the peer manager, keeping a list of peers which can be tried for connection etc * */ -import "os" -import "fmt" - -import "errors" -import "sync" -import "time" -import "sort" -import "path/filepath" -import "encoding/json" +import ( + "encoding/json" + "errors" + "fmt" + "os" + "path/filepath" + "sort" + "sync" + "time" + + "github.com/deroproject/derohe/globals" +) //import "encoding/binary" //import "container/list" //import log "github.com/sirupsen/logrus" -import "github.com/deroproject/derohe/globals" - //import "github.com/deroproject/derosuite/crypto" // This structure is used to do book keeping for the peer list and keeps other DATA related to peer @@ -53,16 +54,23 @@ type Peer struct { ID uint64 `json:"peerid"` // peer id Miner bool `json:"miner"` // miner //NeverBlacklist bool // this address will never be blacklisted - LastConnected uint64 `json:"lastconnected"` // epoch time when it was connected , 0 if never connected - FailCount uint64 `json:"failcount"` // how many times have we failed (tcp errors) - ConnectAfter uint64 `json:"connectafter"` // we should connect when the following timestamp passes - BlacklistBefore uint64 `json:"blacklistbefore"` // peer blacklisted till epoch , priority nodes are never blacklisted, 0 if not blacklist - GoodCount uint64 `json:"goodcount"` // how many times peer has been shared with us - Version int `json:"version"` // version 1 is original C daemon peer, version 2 is golang p2p version - Whitelist bool `json:"whitelist"` + LastConnected uint64 `json:"lastconnected"` // epoch time when it was connected , 0 if never connected + FailCount uint64 `json:"failcount"` // how many times have we failed (tcp errors) + ConnectAfter uint64 `json:"connectafter"` // we should connect when the following timestamp passes + BlacklistBefore uint64 `json:"blacklistbefore"` // peer blacklisted till epoch , priority nodes are never blacklisted, 0 if not blacklist + GoodCount uint64 `json:"goodcount"` // how many times peer has been shared with us + Version int `json:"version"` // version 1 is original C daemon peer, version 2 is golang p2p version + Whitelist bool `json:"whitelist"` + ConnectionStatus string `json:"connectionstatus"` sync.Mutex } +type PeersInfo struct { + Peers []*Peer `json:"peers"` + WhitelistSize int `json:"whitelist_size"` + GreylistSize int `json:"greylist_size"` +} + var peer_map = map[string]*Peer{} var peer_mutex sync.Mutex @@ -242,35 +250,42 @@ func Peer_Delete(p *Peer) { // prints all the connection info to screen func PeerList_Print() { - peer_mutex.Lock() - defer peer_mutex.Unlock() fmt.Printf("Peer List\n") fmt.Printf("%-22s %-6s %-4s %-5s %-7s %9s %3s\n", "Remote Addr", "Active", "Good", "Fail", " State", "Height", "DIR") + info := GetPeersInfo() + for _, v := range info.Peers { + fmt.Printf("%-22s %-6s %4d %5d \n", v.Address, v.ConnectionStatus, v.GoodCount, v.FailCount) + } + + fmt.Printf("\nWhitelist size %d\n", info.WhitelistSize) + fmt.Printf("Greylist size %d\n", info.GreylistSize) + +} + +// GetAllPeers returns a list of all peers +func GetPeersInfo() *PeersInfo { + peer_mutex.Lock() + defer peer_mutex.Unlock() var list []*Peer greycount := 0 for _, v := range peer_map { if v.Whitelist { // only display white listed peer + if IsAddressConnected(ParseIPNoError(v.Address)) { + v.ConnectionStatus = "ACTIVE" + } list = append(list, v) } else { greycount++ } } - // sort the list sort.Slice(list, func(i, j int) bool { return list[i].Address < list[j].Address }) - - for i := range list { - connected := "" - if IsAddressConnected(ParseIPNoError(list[i].Address)) { - connected = "ACTIVE" - } - fmt.Printf("%-22s %-6s %4d %5d \n", list[i].Address, connected, list[i].GoodCount, list[i].FailCount) + return &PeersInfo{ + Peers: list, + WhitelistSize: len(peer_map) - greycount, + GreylistSize: greycount, } - - fmt.Printf("\nWhitelist size %d\n", len(peer_map)-greycount) - fmt.Printf("Greylist size %d\n", greycount) - } // this function return peer count which are in our list diff --git a/rpc/daemon_rpc.go b/rpc/daemon_rpc.go index a25263c0..3f8816d7 100644 --- a/rpc/daemon_rpc.go +++ b/rpc/daemon_rpc.go @@ -18,7 +18,9 @@ package rpc -import "github.com/deroproject/derohe/cryptography/crypto" +import ( + "github.com/deroproject/derohe/cryptography/crypto" +) // this is used to print blockheader for the rpc and the daemon type BlockHeader_Print struct { From 75bf536a4d387f7be0f689c808cf124ac941c228 Mon Sep 17 00:00:00 2001 From: jon4hz Date: Fri, 3 Jun 2022 21:22:31 +0200 Subject: [PATCH 3/3] refactor: getpeers to be more consistant --- cmd/derod/rpc/rpc_dero_getpeers.go | 30 ++++++++++++++++++++++++-- cmd/derod/rpc/websocket_server.go | 2 +- rpc/daemon_rpc.go | 34 ++++++++++++++++++++++++++++++ 3 files changed, 63 insertions(+), 3 deletions(-) diff --git a/cmd/derod/rpc/rpc_dero_getpeers.go b/cmd/derod/rpc/rpc_dero_getpeers.go index f5892a37..fcee8f09 100644 --- a/cmd/derod/rpc/rpc_dero_getpeers.go +++ b/cmd/derod/rpc/rpc_dero_getpeers.go @@ -20,8 +20,34 @@ import ( "context" "github.com/deroproject/derohe/p2p" + "github.com/deroproject/derohe/rpc" ) -func GetPeersInfo(ctx context.Context) (result *p2p.PeersInfo) { - return p2p.GetPeersInfo() +func GetPeers(ctx context.Context) (result *rpc.GetPeersResult) { + p := p2p.GetPeersInfo() + return &rpc.GetPeersResult{ + Peers: toRpcPeers(p.Peers), + WhitelistSize: p.WhitelistSize, + GreylistSize: p.GreylistSize, + } +} + +func toRpcPeers(p []*p2p.Peer) []*rpc.Peer { + rp := make([]*rpc.Peer, len(p)) + for i, v := range p { + rp[i] = &rpc.Peer{ + Address: v.Address, + ID: v.ID, + Miner: v.Miner, + LastConnected: v.LastConnected, + FailCount: v.FailCount, + ConnectAfter: v.ConnectAfter, + BlacklistBefore: v.BlacklistBefore, + GoodCount: v.GoodCount, + Version: v.Version, + Whitelist: v.Whitelist, + ConnectionStatus: v.ConnectionStatus, + } + } + return rp } diff --git a/cmd/derod/rpc/websocket_server.go b/cmd/derod/rpc/websocket_server.go index a035f80d..1989ccc7 100644 --- a/cmd/derod/rpc/websocket_server.go +++ b/cmd/derod/rpc/websocket_server.go @@ -338,7 +338,7 @@ var servicemux = handler.ServiceMap{ "GetSC": handler.New(GetSC), "GetGasEstimate": handler.New(GetGasEstimate), "NameToAddress": handler.New(NameToAddress), - "GetPeers": handler.New(GetPeersInfo), + "GetPeers": handler.New(GetPeers), }, "DAEMON": handler.Map{ "Echo": handler.New(DAEMON_Echo), diff --git a/rpc/daemon_rpc.go b/rpc/daemon_rpc.go index 3f8816d7..664997d8 100644 --- a/rpc/daemon_rpc.go +++ b/rpc/daemon_rpc.go @@ -323,3 +323,37 @@ type GasEstimate_Result struct { GasStorage uint64 `json:"gasstorage"` Status string `json:"status"` } +<<<<<<< HEAD +======= + +type ( + GetPeersParams struct{} + GetPeersResult struct { + Peers []*Peer `json:"peers"` + WhitelistSize int `json:"whitelist_size"` + GreylistSize int `json:"greylist_size"` + } + Peer struct { + Address string `json:"address"` // pairs in the ip:port or dns:port, basically endpoint + ID uint64 `json:"peerid"` // peer id + Miner bool `json:"miner"` // miner + LastConnected uint64 `json:"lastconnected"` // epoch time when it was connected , 0 if never connected + FailCount uint64 `json:"failcount"` // how many times have we failed (tcp errors) + ConnectAfter uint64 `json:"connectafter"` // we should connect when the following timestamp passes + BlacklistBefore uint64 `json:"blacklistbefore"` // peer blacklisted till epoch , priority nodes are never blacklisted, 0 if not blacklist + GoodCount uint64 `json:"goodcount"` // how many times peer has been shared with us + Version int `json:"version"` // version 1 is original C daemon peer, version 2 is golang p2p version + Whitelist bool `json:"whitelist"` + ConnectionStatus string `json:"connectionstatus"` + } +) + +type ( + CheckAddressStatusParams struct { + Address string `json:"address"` + } + CheckAddressStatusResult struct { + Registered bool `json:"registered"` + } +) +>>>>>>> f492d5e (refactor: getpeers to be more consistant)