Skip to content

Commit

Permalink
feat(cli): add read vouchers command
Browse files Browse the repository at this point in the history
  • Loading branch information
GMKrieger committed Dec 11, 2023
1 parent 967c3fd commit 95f9876
Show file tree
Hide file tree
Showing 7 changed files with 587 additions and 1 deletion.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Added `cartesi-rollups-node` Go binary as a single entrypoint to execute all Cartesi Node services
- Added unified configuration for the Node with a new set of environment variables.
- Added `cartesi-rollups-cli` binary to help develop and debug the Cartesi Rollups node
- Added read input and read notice to `cartesi-rollups-cli`
- Added read input, read notice and read voucher to `cartesi-rollups-cli`

### Changed

Expand Down
3 changes: 3 additions & 0 deletions cmd/cartesi-rollups-cli/root/read/read.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import (
"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/cartesi/rollups-node/cmd/cartesi-rollups-cli/root/read/vouchers"

"github.com/spf13/cobra"
)

Expand All @@ -23,4 +25,5 @@ func init() {
Cmd.AddCommand(notice.Cmd)
Cmd.AddCommand(notices.Cmd)
Cmd.AddCommand(voucher.Cmd)
Cmd.AddCommand(vouchers.Cmd)
}
57 changes: 57 additions & 0 deletions cmd/cartesi-rollups-cli/root/read/vouchers/vouchers.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 vouchers

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: "vouchers",
Short: "Reads all vouchers. If an input is specified, reads all vouchers from that input",
Example: examples,
Run: run,
}

const examples = `# Read all vouchers:
cartesi-rollups-cli read vouchers`

var (
inputIndex int
graphqlEndpoint string
)

func init() {
Cmd.Flags().IntVar(&inputIndex, "input-index", -1,
"index of the input")

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)

var resp []readerclient.Voucher
var err error

if cmd.Flags().Changed("input-index") {
resp, err = readerclient.GetInputVouchers(ctx, client, inputIndex)
cobra.CheckErr(err)
} else {
resp, err = readerclient.GetVouchers(ctx, client)
cobra.CheckErr(err)
}

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

fmt.Print(string(val))
}
27 changes: 27 additions & 0 deletions pkg/readerclient/generate/input_vouchers.graphql
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
query getInputVouchers($inputIndex: Int!) {
input(index: $inputIndex) {
index
vouchers {
edges {
node{
index
payload
destination
proof {
validity {
inputIndexWithinEpoch
outputIndexWithinInput
outputHashesRootHash
vouchersEpochRootHash
noticesEpochRootHash
machineStateHash
outputHashInOutputHashesSiblings
outputHashesInEpochSiblings
}
context
}
}
}
}
}
}
27 changes: 27 additions & 0 deletions pkg/readerclient/generate/vouchers.graphql
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
query getVouchers {
vouchers {
edges {
node {
index
payload
destination
proof {
validity {
inputIndexWithinEpoch
outputIndexWithinInput
outputHashesRootHash
vouchersEpochRootHash
noticesEpochRootHash
machineStateHash
outputHashInOutputHashesSiblings
outputHashesInEpochSiblings
}
context
}
input {
index
}
}
}
}
}
Loading

0 comments on commit 95f9876

Please sign in to comment.