Skip to content

Commit

Permalink
Debugging API requests
Browse files Browse the repository at this point in the history
  • Loading branch information
firelizzard18 committed May 21, 2024
1 parent 4ee9918 commit 857eb66
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 2 deletions.
3 changes: 2 additions & 1 deletion pkg/api/v3/jsonrpc/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
type Client struct {
Client http.Client
Server string
Debug bool
}

var _ api.NodeService = (*Client)(nil)
Expand Down Expand Up @@ -97,7 +98,7 @@ func (c *PrivateClient) Sequence(ctx context.Context, src, dst *url.URL, num uin
}

func (c *Client) sendRequest(ctx context.Context, method string, req, resp interface{}) error {
jc := jsonrpc2.Client{Client: c.Client}
jc := jsonrpc2.Client{Client: c.Client, DebugRequest: c.Debug}
err := jc.Request(ctx, c.Server, method, req, &resp)
if err == nil {
return nil
Expand Down
20 changes: 20 additions & 0 deletions pkg/api/v3/message/transport.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ package message
import (
"context"
"encoding/json"
"fmt"
"io"
"os"

"github.com/libp2p/go-libp2p/core/peer"
"github.com/multiformats/go-multiaddr"
Expand Down Expand Up @@ -53,6 +55,9 @@ type RoutedTransport struct {

// Attempts is the number of connection attempts to make.
Attempts int

// Debug prints round trip requests to stderr.
Debug bool
}

// RoundTrip routes each requests and executes a round-trip call. If there are
Expand Down Expand Up @@ -92,6 +97,12 @@ func (c *RoutedTransport) RoundTrip(ctx context.Context, requests []Message, cal

// Dial the address
_, err = c.dial(ctx, addr, streams, func(s Stream) error {
if c.Debug {
if b, err := json.Marshal(req); err == nil {
fmt.Fprintf(os.Stderr, "--> %s\n", b)
}
}

// Send the request
err := s.Write(req)
if err != nil {
Expand All @@ -100,6 +111,15 @@ func (c *RoutedTransport) RoundTrip(ctx context.Context, requests []Message, cal

// Wait for the response
res, err := s.Read()
if c.Debug {
var v any = res
if err != nil {
v = err
}
if b, err := json.Marshal(v); err == nil {
fmt.Fprintf(os.Stderr, "<-- %s\n", b)
}
}
if err != nil {
// Add peer ID
var perr interface{ Peer() peer.ID }
Expand Down
1 change: 1 addition & 0 deletions tools/cmd/debug/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ var (
cachedScan string
verbose bool
pretend bool
debug bool
waitForTxn bool
peerDb string
lightDb string
Expand Down
5 changes: 4 additions & 1 deletion tools/cmd/debug/sequence.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ var cmdSequence = &cobra.Command{
func init() {
cmd.AddCommand(cmdSequence)
cmdSequence.Flags().BoolVarP(&verbose, "verbose", "v", false, "More verbose output")
cmdSequence.PersistentFlags().StringVar(&cachedScan, "cached-scan", "", "A cached network scan")
cmdSequence.Flags().BoolVar(&debug, "debug", false, "Debug network requests")
cmdSequence.Flags().StringVar(&cachedScan, "cached-scan", "", "A cached network scan")
cmdSequence.Flags().StringVar(&only, "only", "", "Only scan anchors or synthetic transactions")
cmdSequence.Flags().DurationVar(&flagMaxResponseAge, "max-response-age", flagMaxResponseAge, "Maximum age of a response before it is considered too stale to use")
}
Expand All @@ -54,6 +55,7 @@ func sequence(cmd *cobra.Command, args []string) {

c := jsonrpc.NewClient(accumulate.ResolveWellKnownEndpoint(args[0], "v3"))
c.Client.Timeout = time.Hour
c.Debug = debug
Q := api.Querier2{Querier: c}

ni, err := c.NodeInfo(ctx, api.NodeInfoOptions{})
Expand Down Expand Up @@ -94,6 +96,7 @@ func sequence(cmd *cobra.Command, args []string) {
Dialer: node.DialNetwork(),
},
Router: router,
Debug: debug,
},
}
Q.Querier = c2
Expand Down

0 comments on commit 857eb66

Please sign in to comment.