From 4307a34314a2d9d0509412131f074d1530aac0bc Mon Sep 17 00:00:00 2001 From: Sergi Rene Date: Thu, 9 May 2024 15:08:14 +0200 Subject: [PATCH 1/5] show p2p info command added --- block/retriever.go | 12 +--- cmd/dymint/commands/show_p2p_info.go | 94 ++++++++++++++++++++++++++++ cmd/dymint/main.go | 1 + 3 files changed, 96 insertions(+), 11 deletions(-) create mode 100644 cmd/dymint/commands/show_p2p_info.go diff --git a/block/retriever.go b/block/retriever.go index 61a757204..a144089c0 100644 --- a/block/retriever.go +++ b/block/retriever.go @@ -135,17 +135,7 @@ func (m *Manager) fetchBatch(daMetaData *da.DASubmitMetaData) da.ResultRetrieveB }, } } - // Check batch availability - availabilityRes := m.Retriever.CheckBatchAvailability(daMetaData) - if availabilityRes.Code != da.StatusSuccess { - return da.ResultRetrieveBatch{ - BaseResult: da.BaseResult{ - Code: da.StatusError, - Message: fmt.Sprintf("Error fetching batch: %s", availabilityRes.Message), - Error: availabilityRes.Error, - }, - } - } + // batchRes.MetaData includes proofs necessary to open disputes with the Hub batchRes := m.Retriever.RetrieveBatches(daMetaData) // TODO(srene) : for invalid transactions there is no specific error code since it will need to be validated somewhere else for fraud proving. diff --git a/cmd/dymint/commands/show_p2p_info.go b/cmd/dymint/commands/show_p2p_info.go new file mode 100644 index 000000000..44acc7796 --- /dev/null +++ b/cmd/dymint/commands/show_p2p_info.go @@ -0,0 +1,94 @@ +package commands + +import ( + "encoding/json" + "fmt" + "io" + "log" + "net/http" + "strings" + + "github.com/dymensionxyz/dymint/conv" + "github.com/libp2p/go-libp2p" + "github.com/spf13/cobra" + "github.com/tendermint/tendermint/p2p" + rpctypes "github.com/tendermint/tendermint/rpc/jsonrpc/types" +) + +var ip, port string // used for flags + +// ShowNodeIDCmd dumps node's ID to the standard output. +var ShowP2PInfoCmd = &cobra.Command{ + Use: "show-p2p-info", + Aliases: []string{"show_p2p_info"}, + Short: "Show P2P status information", + RunE: showP2PInfo, +} + +func init() { + ShowP2PInfoCmd.Flags().StringVar(&ip, "ip", "127.0.0.1", "rpc ip address") + ShowP2PInfoCmd.Flags().StringVar(&port, "port", "26657", "rpc port") +} + +func showP2PInfo(cmd *cobra.Command, args []string) error { + + nodeKey, err := p2p.LoadNodeKey(tmconfig.NodeKeyFile()) + if err != nil { + return err + } + signingKey, err := conv.GetNodeKey(nodeKey) + if err != nil { + return err + } + // convert nodeKey to libp2p key + host, err := libp2p.New(libp2p.Identity(signingKey)) + if err != nil { + return err + } + data, err := json.Marshal(map[string]interface{}{ + "method": "net_info", + "jsonrpc": "2.0", + "id": 1, + "params": []interface{}{}, + }) + if err != nil { + log.Fatalf("Marshal: %v", err) + } + resp, err := http.Post("http://"+ip+":"+port, "application/json", strings.NewReader(string(data))) + if err != nil { + fmt.Println("Error connecting to Dymint RPC") + return nil + } + defer resp.Body.Close() + body, err := io.ReadAll(resp.Body) + if err != nil { + fmt.Println("Error reading data from RPC:", err) + return nil + } + response := rpctypes.RPCResponse{} + //result := make(map[string]interface{}) + err = json.Unmarshal(body, &response) + if err != nil { + fmt.Println("Error unmarshal:", err) + return nil + } + var netinfo map[string]interface{} + + //netinfo := ctypes.ResultNetInfo{} + err = json.Unmarshal([]byte(response.Result), &netinfo) + if err != nil { + fmt.Println("Error unmarshal:", err) + return nil + } + peers := netinfo["peers"].([]interface{}) + fmt.Println("Host ID:", host.ID()) + fmt.Println("Listening P2P addresses:", netinfo["listeners"]) + + for i, p := range peers { + peer := p.(map[string]interface{}) + info := peer["node_info"].(map[string]interface{}) + fmt.Printf("Peer %d Id:%s Multiaddress:%s\n", i, info["id"], peer["remote_ip"]) + } + + return nil +} diff --git a/cmd/dymint/main.go b/cmd/dymint/main.go index 631383649..557408353 100644 --- a/cmd/dymint/main.go +++ b/cmd/dymint/main.go @@ -16,6 +16,7 @@ func main() { commands.InitFilesCmd, commands.ShowSequencer, commands.ShowNodeIDCmd, + commands.ShowP2PInfoCmd, debug.DebugCmd, cli.NewCompletionCmd(rootCmd, true), ) From 0329dad6b3af817130656e9ef66b531cb305be30 Mon Sep 17 00:00:00 2001 From: Sergi Rene Date: Thu, 9 May 2024 15:34:41 +0200 Subject: [PATCH 2/5] duration added and nil pointer fix --- cmd/dymint/commands/show_p2p_info.go | 25 ++++++++++++++++++++----- 1 file changed, 20 insertions(+), 5 deletions(-) diff --git a/cmd/dymint/commands/show_p2p_info.go b/cmd/dymint/commands/show_p2p_info.go index 44acc7796..d0c9f57ad 100644 --- a/cmd/dymint/commands/show_p2p_info.go +++ b/cmd/dymint/commands/show_p2p_info.go @@ -6,7 +6,9 @@ import ( "io" "log" "net/http" + "strconv" "strings" + "time" "github.com/dymensionxyz/dymint/conv" "github.com/libp2p/go-libp2p" @@ -80,14 +82,27 @@ func showP2PInfo(cmd *cobra.Command, args []string) error { fmt.Println("Error unmarshal:", err) return nil } - peers := netinfo["peers"].([]interface{}) fmt.Println("Host ID:", host.ID()) fmt.Println("Listening P2P addresses:", netinfo["listeners"]) - for i, p := range peers { - peer := p.(map[string]interface{}) - info := peer["node_info"].(map[string]interface{}) - fmt.Printf("Peer %d Id:%s Multiaddress:%s\n", i, info["id"], peer["remote_ip"]) + if netinfo["peers"] != nil { + + peers := netinfo["peers"].([]interface{}) + + for i, p := range peers { + + peer := p.(map[string]interface{}) + info := peer["node_info"].(map[string]interface{}) + status := peer["connection_status"].(map[string]interface{}) + + duration, err := strconv.ParseInt(status["Duration"].(string), 10, 64) + if err != nil { + fmt.Println("Error parsing connection duration:", err) + } + fmt.Printf("Peer %d Id:%s Multiaddress:%s Duration:%s\n", i, info["id"], peer["remote_ip"], time.Duration(duration)) + } + } else { + fmt.Println("0 peers connected") } return nil From 2fde98ab577396b505fc9aa6788e5e1aac5f7883 Mon Sep 17 00:00:00 2001 From: Sergi Rene Date: Thu, 9 May 2024 15:37:26 +0200 Subject: [PATCH 3/5] revert avail check removed --- block/retriever.go | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/block/retriever.go b/block/retriever.go index a144089c0..61a757204 100644 --- a/block/retriever.go +++ b/block/retriever.go @@ -135,7 +135,17 @@ func (m *Manager) fetchBatch(daMetaData *da.DASubmitMetaData) da.ResultRetrieveB }, } } - + // Check batch availability + availabilityRes := m.Retriever.CheckBatchAvailability(daMetaData) + if availabilityRes.Code != da.StatusSuccess { + return da.ResultRetrieveBatch{ + BaseResult: da.BaseResult{ + Code: da.StatusError, + Message: fmt.Sprintf("Error fetching batch: %s", availabilityRes.Message), + Error: availabilityRes.Error, + }, + } + } // batchRes.MetaData includes proofs necessary to open disputes with the Hub batchRes := m.Retriever.RetrieveBatches(daMetaData) // TODO(srene) : for invalid transactions there is no specific error code since it will need to be validated somewhere else for fraud proving. From 3a180ce69f9998f08e89f969f57ae2870fe0e276 Mon Sep 17 00:00:00 2001 From: Sergi Rene Date: Thu, 9 May 2024 16:47:22 +0200 Subject: [PATCH 4/5] fix config p2p command --- cmd/dymint/commands/show_p2p_info.go | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/cmd/dymint/commands/show_p2p_info.go b/cmd/dymint/commands/show_p2p_info.go index d0c9f57ad..82f210afd 100644 --- a/cmd/dymint/commands/show_p2p_info.go +++ b/cmd/dymint/commands/show_p2p_info.go @@ -10,6 +10,7 @@ import ( "strings" "time" + "github.com/cosmos/cosmos-sdk/server" "github.com/dymensionxyz/dymint/conv" "github.com/libp2p/go-libp2p" "github.com/spf13/cobra" @@ -34,7 +35,9 @@ func init() { func showP2PInfo(cmd *cobra.Command, args []string) error { - nodeKey, err := p2p.LoadNodeKey(tmconfig.NodeKeyFile()) + serverCtx := server.GetServerContextFromCmd(cmd) + cfg := serverCtx.Config + nodeKey, err := p2p.LoadNodeKey(cfg.NodeKeyFile()) if err != nil { return err } From 6fe5b8bc0323b7e7ec3f34dd7816ab3e85df754a Mon Sep 17 00:00:00 2001 From: Sergi Rene Date: Fri, 10 May 2024 12:58:46 +0200 Subject: [PATCH 5/5] fix p2p id and comments --- cmd/dymint/commands/show_p2p_info.go | 59 ++++++++++++++++------------ rpc/client/client.go | 2 + 2 files changed, 35 insertions(+), 26 deletions(-) diff --git a/cmd/dymint/commands/show_p2p_info.go b/cmd/dymint/commands/show_p2p_info.go index 82f210afd..d3dc1c265 100644 --- a/cmd/dymint/commands/show_p2p_info.go +++ b/cmd/dymint/commands/show_p2p_info.go @@ -6,20 +6,23 @@ import ( "io" "log" "net/http" + "sort" "strconv" "strings" "time" - "github.com/cosmos/cosmos-sdk/server" - "github.com/dymensionxyz/dymint/conv" - "github.com/libp2p/go-libp2p" "github.com/spf13/cobra" - "github.com/tendermint/tendermint/p2p" rpctypes "github.com/tendermint/tendermint/rpc/jsonrpc/types" ) var ip, port string // used for flags +type peerInfo struct { + peerId string + multiAddress string + connectionDuration time.Duration +} + // ShowNodeIDCmd dumps node's ID to the standard output. var ShowP2PInfoCmd = &cobra.Command{ Use: "show-p2p-info", @@ -35,21 +38,6 @@ func init() { func showP2PInfo(cmd *cobra.Command, args []string) error { - serverCtx := server.GetServerContextFromCmd(cmd) - cfg := serverCtx.Config - nodeKey, err := p2p.LoadNodeKey(cfg.NodeKeyFile()) - if err != nil { - return err - } - signingKey, err := conv.GetNodeKey(nodeKey) - if err != nil { - return err - } - // convert nodeKey to libp2p key - host, err := libp2p.New(libp2p.Identity(signingKey)) - if err != nil { - return err - } data, err := json.Marshal(map[string]interface{}{ "method": "net_info", "jsonrpc": "2.0", @@ -71,7 +59,6 @@ func showP2PInfo(cmd *cobra.Command, args []string) error { return nil } response := rpctypes.RPCResponse{} - //result := make(map[string]interface{}) err = json.Unmarshal(body, &response) if err != nil { fmt.Println("Error unmarshal:", err) @@ -79,34 +66,54 @@ func showP2PInfo(cmd *cobra.Command, args []string) error { } var netinfo map[string]interface{} - //netinfo := ctypes.ResultNetInfo{} err = json.Unmarshal([]byte(response.Result), &netinfo) if err != nil { fmt.Println("Error unmarshal:", err) return nil } - fmt.Println("Host ID:", host.ID()) - fmt.Println("Listening P2P addresses:", netinfo["listeners"]) + listeners := netinfo["listeners"].([]interface{}) + fmt.Println("Host ID:", listeners[0]) + fmt.Println("Listening P2P addresses:", listeners[1:]) + var peers []peerInfo + + //peer info is stored from the rpc response if netinfo["peers"] != nil { - peers := netinfo["peers"].([]interface{}) + ps := netinfo["peers"].([]interface{}) - for i, p := range peers { + for _, p := range ps { peer := p.(map[string]interface{}) info := peer["node_info"].(map[string]interface{}) status := peer["connection_status"].(map[string]interface{}) + //duration is in int64 nanoseconds duration, err := strconv.ParseInt(status["Duration"].(string), 10, 64) if err != nil { fmt.Println("Error parsing connection duration:", err) } - fmt.Printf("Peer %d Id:%s Multiaddress:%s Duration:%s\n", i, info["id"], peer["remote_ip"], time.Duration(duration)) + + newPeer := peerInfo{ + peerId: info["id"].(string), + multiAddress: peer["remote_ip"].(string), + connectionDuration: time.Duration(duration), + } + peers = append(peers, newPeer) } } else { fmt.Println("0 peers connected") } + //Peers ordered by oldest connection + sort.Slice(peers[:], func(i, j int) bool { + return peers[i].connectionDuration > peers[j].connectionDuration + }) + + //Info displayed: Libp2p Peeer ID, Multiaddress (connection info) and time pasted since connection + for i, p := range peers { + fmt.Printf("Peer %d Id:%s Multiaddress:%s Connection duration:%s\n", i, p.peerId, p.multiAddress, p.connectionDuration) + } + return nil } diff --git a/rpc/client/client.go b/rpc/client/client.go index 17d3c42d5..bfbd1eb64 100644 --- a/rpc/client/client.go +++ b/rpc/client/client.go @@ -351,6 +351,8 @@ func (c *Client) NetInfo(ctx context.Context) (*ctypes.ResultNetInfo, error) { res := ctypes.ResultNetInfo{ Listening: true, } + res.Listeners = append(res.Listeners, c.node.P2P.Host.ID().String()) + for _, ma := range c.node.P2P.Addrs() { res.Listeners = append(res.Listeners, ma.String()) }