Skip to content

Commit

Permalink
feat(cli): add read inputs command
Browse files Browse the repository at this point in the history
  • Loading branch information
GMKrieger committed Nov 28, 2023
1 parent 9bc3230 commit d7d9620
Show file tree
Hide file tree
Showing 3 changed files with 114 additions and 37 deletions.
49 changes: 49 additions & 0 deletions cmd/cartesi-rollups-cli/root/read/inputs/inputs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// (c) Cartesi and individual authors (see AUTHORS)
// SPDX-License-Identifier: Apache-2.0 (see LICENSE)

package inputs

import (
"encoding/json"
"fmt"

"github.com/Khan/genqlient/graphql"
"github.com/cartesi/rollups-node/pkg/readerclient"
"github.com/spf13/cobra"
)

var Cmd = &cobra.Command{
Use: "inputs",
Short: "Reads first N inputs from node GraphQL",
Example: examples,
Run: run,
}

const examples = `# Read inputs from GraphQL:
cartesi-rollups-cli read inputs`

var (
first int
graphqlEndpoint string
)

func init() {
Cmd.Flags().IntVar(&first, "first", 10,
"first N inputs returned, starting at 0")

Cmd.Flags().StringVar(&graphqlEndpoint, "graphql-endpoint", "http://0.0.0.0:4000/graphql",
"address used to connect to graphql")
}

func run(cmd *cobra.Command, args []string) {
ctx := cmd.Context()
client := graphql.NewClient(graphqlEndpoint, nil)

resp, err := readerclient.GetInputs(ctx, client, first)
cobra.CheckErr(err)

val, err := json.MarshalIndent(resp, "", " ")
cobra.CheckErr(err)

fmt.Print(val)
}
14 changes: 14 additions & 0 deletions pkg/readerclient/generate/inputs.graphql
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
query getInputs($first: Int!) {
inputs(first: $first) {
edges {
node {
index
status
msgSender
timestamp
blockNumber
payload
}
}
}
}
88 changes: 51 additions & 37 deletions pkg/readerclient/input.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,42 +29,66 @@ type Input struct {
Payload hexutil.Bytes `json:"payload"`
}

func GetInput(
ctx context.Context,
client graphql.Client,
func newInput(
index int,
status CompletionStatus,
msgSender string,
timestamp string,
blockNumber string,
payload string,
) (*Input, error) {
resp, err := getInput(ctx, client, int(index))
if err != nil {
return nil, err
}

timestamp, err := strconv.ParseUint(resp.Input.Timestamp, 0, 64)
convTimestamp, err := strconv.ParseUint(timestamp, 0, 64)
if err != nil {
return nil, fmt.Errorf("failed to parse timestamp: %v", err)
}

blocknumber, err := strconv.ParseUint(resp.Input.BlockNumber, 0, 64)
convBlockNumber, err := strconv.ParseUint(blockNumber, 0, 64)
if err != nil {
return nil, fmt.Errorf("failed to parse block number: %v", err)
}
payload, err := hexutil.Decode(resp.Input.Payload)
convPayload, err := hexutil.Decode(payload)
if err != nil {
return nil, fmt.Errorf("failed to decode payload to bytes: %v", err)
}

input := Input{
resp.Input.Index,
resp.Input.Status,
common.HexToAddress(resp.Input.MsgSender),
time.Duration(timestamp),
blocknumber,
payload,
index,
status,
common.HexToAddress(msgSender),
time.Duration(convTimestamp),
convBlockNumber,
convPayload,
}

return &input, err
}

// Get input from graphql
// Uses index as parameter on graphql query
func GetInput(
ctx context.Context,
client graphql.Client,
index int,
) (*Input, error) {
resp, err := getInput(ctx, client, index)
if err != nil {
return nil, err
}

input, err := newInput(
resp.Input.Index,
resp.Input.Status,
resp.Input.MsgSender,
resp.Input.Timestamp,
resp.Input.BlockNumber,
resp.Input.Payload,
)

return input, err
}

// Get multiple inputs from graphql
// Uses first as parameter on graphql query
func GetInputs(
ctx context.Context,
client graphql.Client,
Expand All @@ -73,36 +97,26 @@ func GetInputs(

var inputs []Input

resp, err := getInputs(ctx, client, int(first))
resp, err := getInputs(ctx, client, first)
if err != nil {
return nil, err
}

for _, edge := range resp.Inputs.Edges {

timestamp, err := strconv.ParseUint(edge.Node.Timestamp, 0, 64)
if err != nil {
return nil, fmt.Errorf("failed to parse timestamp: %v", err)
}
blocknumber, err := strconv.ParseUint(edge.Node.BlockNumber, 0, 64)
if err != nil {
return nil, fmt.Errorf("failed to parse block number: %v", err)
}
payload, err := hexutil.Decode(edge.Node.Payload)
if err != nil {
return nil, fmt.Errorf("failed to decode payload to bytes: %v", err)
}

input := Input{
input, err := newInput(
edge.Node.Index,
edge.Node.Status,
common.HexToAddress(edge.Node.MsgSender),
time.Duration(timestamp),
blocknumber,
payload,
edge.Node.MsgSender,
edge.Node.Timestamp,
edge.Node.BlockNumber,
edge.Node.Payload,
)
if err != nil {
return nil, err
}

inputs = append(inputs, input)
inputs = append(inputs, *input)
}

return inputs, err
Expand Down

0 comments on commit d7d9620

Please sign in to comment.