Skip to content

Commit

Permalink
feat(cli): add read voucher command
Browse files Browse the repository at this point in the history
  • Loading branch information
GMKrieger committed Dec 11, 2023
1 parent 48d1a28 commit 967c3fd
Show file tree
Hide file tree
Showing 6 changed files with 466 additions and 0 deletions.
2 changes: 2 additions & 0 deletions cmd/cartesi-rollups-cli/root/read/read.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/cartesi/rollups-node/cmd/cartesi-rollups-cli/root/read/inputs"
"github.com/cartesi/rollups-node/cmd/cartesi-rollups-cli/root/read/notice"
"github.com/cartesi/rollups-node/cmd/cartesi-rollups-cli/root/read/notices"
"github.com/cartesi/rollups-node/cmd/cartesi-rollups-cli/root/read/voucher"
"github.com/spf13/cobra"
)

Expand All @@ -21,4 +22,5 @@ func init() {
Cmd.AddCommand(inputs.Cmd)
Cmd.AddCommand(notice.Cmd)
Cmd.AddCommand(notices.Cmd)
Cmd.AddCommand(voucher.Cmd)
}
57 changes: 57 additions & 0 deletions cmd/cartesi-rollups-cli/root/read/voucher/voucher.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
// (c) Cartesi and individual authors (see AUTHORS)
// SPDX-License-Identifier: Apache-2.0 (see LICENSE)

package voucher

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: "voucher",
Short: "Reads a voucher",
Example: examples,
Run: run,
}

const examples = `# Read voucher 5 from input 6:
cartesi-rollups-cli read voucher --voucher-index 5 --input-index 6`

var (
voucherIndex int
inputIndex int
graphqlEndpoint string
)

func init() {
Cmd.Flags().IntVar(&voucherIndex, "voucher-index", 0,
"index of the voucher")

cobra.CheckErr(Cmd.MarkFlagRequired("voucher-index"))

Cmd.Flags().IntVar(&inputIndex, "input-index", 0,
"index of the input")

cobra.CheckErr(Cmd.MarkFlagRequired("input-index"))

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.GetVoucher(ctx, client, voucherIndex, inputIndex)
cobra.CheckErr(err)

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

fmt.Print(string(val))
}
23 changes: 23 additions & 0 deletions pkg/readerclient/generate/voucher.graphql
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
query getVoucher($voucherIndex: Int!, $inputIndex: Int!) {
voucher(voucherIndex: $voucherIndex, inputIndex: $inputIndex) {
index
payload
destination
proof {
validity {
inputIndexWithinEpoch
outputIndexWithinInput
outputHashesRootHash
vouchersEpochRootHash
noticesEpochRootHash
machineStateHash
outputHashInOutputHashesSiblings
outputHashesInEpochSiblings
}
context
}
input {
index
}
}
}
202 changes: 202 additions & 0 deletions pkg/readerclient/generated.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pkg/readerclient/genqlient.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ operations:
- generate/notice.graphql
- generate/notices.graphql
- generate/input_notices.graphql
- generate/voucher.graphql
generated: generated.go
package: readerclient

Expand Down
Loading

0 comments on commit 967c3fd

Please sign in to comment.