Skip to content

Commit

Permalink
feat(cli): add read notices command
Browse files Browse the repository at this point in the history
  • Loading branch information
GMKrieger committed Dec 5, 2023
1 parent f100f1b commit 0c6a839
Show file tree
Hide file tree
Showing 8 changed files with 679 additions and 4 deletions.
57 changes: 57 additions & 0 deletions cmd/cartesi-rollups-cli/root/read/notices/notices.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 notices

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: "notices",
Short: "Reads notices; if there's an input index, reads notices from that input",
Example: examples,
Run: run,
}

const examples = `# Read notices from GraphQL; if there's an input-index, reads notices from that input:
cartesi-rollups-cli read notices`

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.Notice
var err error

if inputIndex != -1 {
resp, err = readerclient.GetInputNotices(ctx, client, inputIndex)
cobra.CheckErr(err)
} else {
resp, err = readerclient.GetNotices(ctx, client)
cobra.CheckErr(err)
}

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

fmt.Print(string(val))
}
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 @@ -7,6 +7,7 @@ import (
"github.com/cartesi/rollups-node/cmd/cartesi-rollups-cli/root/read/input"
"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/spf13/cobra"
)

Expand All @@ -19,4 +20,5 @@ func init() {
Cmd.AddCommand(input.Cmd)
Cmd.AddCommand(inputs.Cmd)
Cmd.AddCommand(notice.Cmd)
Cmd.AddCommand(notices.Cmd)
}
26 changes: 26 additions & 0 deletions pkg/readerclient/generate/input_notices.graphql
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
query getInputNotices($inputIndex: Int!) {
input(index: $inputIndex) {
index
notices {
edges {
node{
index
payload
proof {
validity {
inputIndexWithinEpoch
outputIndexWithinInput
outputHashesRootHash
vouchersEpochRootHash
noticesEpochRootHash
machineStateHash
outputHashInOutputHashesSiblings
outputHashesInEpochSiblings
}
context
}
}
}
}
}
}
26 changes: 26 additions & 0 deletions pkg/readerclient/generate/notices.graphql
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
query getNotices {
notices {
edges {
node {
index
payload
proof {
validity {
inputIndexWithinEpoch
outputIndexWithinInput
outputHashesRootHash
vouchersEpochRootHash
noticesEpochRootHash
machineStateHash
outputHashInOutputHashesSiblings
outputHashesInEpochSiblings
}
context
}
input {
index
}
}
}
}
}
466 changes: 465 additions & 1 deletion pkg/readerclient/generated.go

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions pkg/readerclient/genqlient.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ operations:
- generate/input.graphql
- generate/inputs.graphql
- generate/notice.graphql
- generate/notices.graphql
- generate/input_notices.graphql
generated: generated.go
package: readerclient

Expand Down
98 changes: 95 additions & 3 deletions pkg/readerclient/notice.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@ type Notice struct {
// Notice data as a payload in Ethereum hex binary format, starting with '0x'
Payload hexutil.Bytes `json:"payload"`
// Proof object that allows this notice to be validated by the base layer blockchain
Proof Proof `json:"proof"`
Proof *Proof `json:"proof"`
}

func newNotice(
index int,
inputIndex int,
payload string,
proof Proof,
proof *Proof,
) (*Notice, error) {
convPayload, err := hexutil.Decode(payload)
if err != nil {
Expand All @@ -43,6 +43,95 @@ func newNotice(
return &notice, err
}

// Get multiple notices from graphql
// Uses notice index and input index as parameter on graphql query
func GetNotices(
ctx context.Context,
client graphql.Client,
) ([]Notice, error) {

var notices []Notice

resp, err := getNotices(ctx, client)
if err != nil {
return nil, err
}

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

proof, err := newProof(
edge.Node.Proof.Validity.InputIndexWithinEpoch,
edge.Node.Proof.Validity.OutputIndexWithinInput,
edge.Node.Proof.Validity.OutputHashesRootHash,
edge.Node.Proof.Validity.VouchersEpochRootHash,
edge.Node.Proof.Validity.NoticesEpochRootHash,
edge.Node.Proof.Validity.MachineStateHash,
edge.Node.Proof.Validity.OutputHashInOutputHashesSiblings,
edge.Node.Proof.Validity.OutputHashesInEpochSiblings,
edge.Node.Proof.Context,
)
if err != nil {
return nil, err
}

notice, err := newNotice(
edge.Node.Index,
edge.Node.Input.Index,
edge.Node.Payload,
proof,
)

notices = append(notices, *notice)
}

return notices, err
}

// Get multiple notices from graphql
// Uses notice index and input index as parameter on graphql query
func GetInputNotices(
ctx context.Context,
client graphql.Client,
inputIndex int,
) ([]Notice, error) {

var notices []Notice

resp, err := getInputNotices(ctx, client, inputIndex)
if err != nil {
return nil, err
}

for _, edge := range resp.Input.Notices.Edges {

proof, err := newProof(
edge.Node.Proof.Validity.InputIndexWithinEpoch,
edge.Node.Proof.Validity.OutputIndexWithinInput,
edge.Node.Proof.Validity.OutputHashesRootHash,
edge.Node.Proof.Validity.VouchersEpochRootHash,
edge.Node.Proof.Validity.NoticesEpochRootHash,
edge.Node.Proof.Validity.MachineStateHash,
edge.Node.Proof.Validity.OutputHashInOutputHashesSiblings,
edge.Node.Proof.Validity.OutputHashesInEpochSiblings,
edge.Node.Proof.Context,
)
if err != nil {
return nil, err
}

notice, err := newNotice(
edge.Node.Index,
resp.Input.Index,
edge.Node.Payload,
proof,
)

notices = append(notices, *notice)
}

return notices, err
}

// Get notice from graphql
// Uses notice index and input index as parameter on graphql query
func GetNotice(
Expand All @@ -67,12 +156,15 @@ func GetNotice(
resp.Notice.Proof.Validity.OutputHashesInEpochSiblings,
resp.Notice.Proof.Context,
)
if err != nil {
return nil, err
}

notice, err := newNotice(
resp.Notice.Index,
resp.Notice.Input.Index,
resp.Notice.Payload,
*proof,
proof,
)

return notice, err
Expand Down
6 changes: 6 additions & 0 deletions pkg/readerclient/proof.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,12 @@ func newProof(
outputHashOutputSiblings []hexutil.Bytes
outputHashEpochSiblings []hexutil.Bytes
)

// This tests if there's a proof, else it returns nil
if len(outputHashesRootHash) == 0 {
return nil, nil
}

outputHash, err := hexutil.Decode(outputHashesRootHash)
if err != nil {
return nil, fmt.Errorf("failed to decode OutputHashesRootHash to bytes: %v", err)
Expand Down

0 comments on commit 0c6a839

Please sign in to comment.