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

feat: get peers info over rpc #60

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.vscode
cmd/derod/mainnet
cmd/derod/testnet
53 changes: 53 additions & 0 deletions cmd/derod/rpc/rpc_dero_getpeers.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// 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"
"github.com/deroproject/derohe/rpc"
)

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
}
57 changes: 29 additions & 28 deletions cmd/derod/rpc/websocket_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 */

Expand Down Expand Up @@ -338,6 +338,7 @@ var servicemux = handler.ServiceMap{
"GetSC": handler.New(GetSC),
"GetGasEstimate": handler.New(GetGasEstimate),
"NameToAddress": handler.New(NameToAddress),
"GetPeers": handler.New(GetPeers),
},
"DAEMON": handler.Map{
"Echo": handler.New(DAEMON_Echo),
Expand Down
79 changes: 47 additions & 32 deletions p2p/peer_pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

Expand Down Expand Up @@ -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
Expand Down
38 changes: 37 additions & 1 deletion rpc/daemon_rpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -321,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)