diff --git a/api/fiber.go b/api/fiber.go index 20d424c..83def71 100644 --- a/api/fiber.go +++ b/api/fiber.go @@ -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 +} diff --git a/cli.go b/cli.go index d288249..279c2bf 100644 --- a/cli.go +++ b/cli.go @@ -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 }, diff --git a/fmt.go b/fmt.go index c6bc7c5..53d5905 100644 --- a/fmt.go +++ b/fmt.go @@ -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 {