Skip to content

Commit

Permalink
feat: support for tracing blocks
Browse files Browse the repository at this point in the history
  • Loading branch information
mempirate committed Jul 13, 2023
1 parent 4d5cc80 commit ec5b0ed
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 2 deletions.
40 changes: 40 additions & 0 deletions api/fiber.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,3 +117,43 @@ func (f *FiberAPI) TraceTransaction(hash, observationType string, private bool)

return response.Message, nil
}

func (f *FiberAPI) TraceBlock(hashOrNumber, observationType string) ([]*TraceEntry, error) {
req, err := http.NewRequest("GET", f.url+"/trace/block/"+hashOrNumber, nil)
if err != nil {
return nil, err
}

if observationType == "" {
observationType = "all"
}

q := req.URL.Query()
q.Add("observation_type", observationType)
req.URL.RawQuery = q.Encode()

f.prepareRequest(req)

res, err := http.DefaultClient.Do(req)
if err != nil {
return nil, err
}

defer res.Body.Close()

body, err := io.ReadAll(res.Body)
if err != nil {
return nil, err
}

response := new(Response[[]*TraceEntry])
if err := json.Unmarshal(body, response); err != nil {
return nil, err
}

if response.Status != "success" {
return nil, fmt.Errorf("Error getting quota: %s", response.Error)
}

return response.Message, nil
}
60 changes: 59 additions & 1 deletion cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,65 @@ func NewApp() *cli.App {
return fmt.Errorf("Error getting quota: %w", err)
}

printTransactionTrace(traces, showSource)
printMessageTrace(traces, showSource)

return nil
},
},
{
Name: "block",
Usage: "Trace a block",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "hash",
Aliases: []string{"H"},
Usage: "The block hash to trace",
},
&cli.StringFlag{
Name: "number",
Aliases: []string{"n"},
Usage: "The block number to trace",
},
&cli.StringFlag{
Name: "type",
Aliases: []string{"t"},
Usage: "The observation type to trace (p2p | fiber | all)",
Value: "all",
},
&cli.BoolFlag{
Name: "show-source",
Aliases: []string{"s"},
Usage: "Whether or not to show the source of the transaction",
Value: false,
},
},
Action: func(c *cli.Context) error {
cfg, err := ReadConfig()
if err != nil {
return fmt.Errorf("Error reading config, did you run cbctl init?: %w", err)
}

hash := c.String("hash")
number := c.String("number")
if hash == "" && number == "" {
return fmt.Errorf("Must specify either a block hash or block number")
}

hashOrNumber := number
if hashOrNumber == "" {
hashOrNumber = hash
}

observationType := c.String("type")
showSource := c.Bool("show-source")

api := api.NewFiberAPI(cfg.Url, cfg.ApiKey)
traces, err := api.TraceBlock(hashOrNumber, observationType)
if err != nil {
return fmt.Errorf("Error getting quota: %w", err)
}

printMessageTrace(traces, showSource)

return nil
},
Expand Down
2 changes: 1 addition & 1 deletion fmt.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"github.com/chainbound/cbctl/api"
)

func printTransactionTrace(trace []*api.TraceEntry, showSource bool) {
func printMessageTrace(trace []*api.TraceEntry, showSource bool) {
if showSource {
fmt.Printf("Timestamp\tNode ID\t\t\tRegion\t\t\tObservation Type\tSource\n")
} else {
Expand Down

0 comments on commit ec5b0ed

Please sign in to comment.