Skip to content

Commit

Permalink
feat(cli): add validate notice command
Browse files Browse the repository at this point in the history
  • Loading branch information
GMKrieger committed Jan 22, 2024
1 parent f3a59f9 commit 5f09036
Show file tree
Hide file tree
Showing 7 changed files with 184 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Added inspect command to `cartesi-rollups-cli`
- Added increase-time command to `cartesi-rollups-cli`
- Added instructions on how to run the node with Docker.
- Added validate command to `cartesi-rollups-cli`

### Changed

Expand Down
2 changes: 2 additions & 0 deletions cmd/cartesi-rollups-cli/root/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/cartesi/rollups-node/cmd/cartesi-rollups-cli/root/read"
"github.com/cartesi/rollups-node/cmd/cartesi-rollups-cli/root/savesnapshot"
"github.com/cartesi/rollups-node/cmd/cartesi-rollups-cli/root/send"
"github.com/cartesi/rollups-node/cmd/cartesi-rollups-cli/root/validate"
"github.com/spf13/cobra"
)

Expand All @@ -25,5 +26,6 @@ func init() {
Cmd.AddCommand(savesnapshot.Cmd)
Cmd.AddCommand(inspect.Cmd)
Cmd.AddCommand(increasetime.Cmd)
Cmd.AddCommand(validate.Cmd)
Cmd.DisableAutoGenTag = true
}
93 changes: 93 additions & 0 deletions cmd/cartesi-rollups-cli/root/validate/validate.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
// (c) Cartesi and individual authors (see AUTHORS)
// SPDX-License-Identifier: Apache-2.0 (see LICENSE)

package validate

import (
"os"

"github.com/Khan/genqlient/graphql"
"github.com/cartesi/rollups-node/internal/config"
"github.com/cartesi/rollups-node/pkg/addresses"
"github.com/cartesi/rollups-node/pkg/ethutil"
"github.com/cartesi/rollups-node/pkg/readerclient"
"github.com/ethereum/go-ethereum/ethclient"
"github.com/spf13/cobra"
)

var Cmd = &cobra.Command{
Use: "validate",
Short: "Validates a notice",
Example: examples,
Run: run,
}

const examples = `# Validates notice 5 from input 6:
cartesi-rollups-cli validate --notice-index 5 --input-index 6`

var (
noticeIndex int
inputIndex int
graphqlEndpoint string
ethEndpoint string
addressBookFile string
)

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

cobra.CheckErr(Cmd.MarkFlagRequired("notice-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:10004/graphql",
"address used to connect to graphql")

Cmd.Flags().StringVar(&ethEndpoint, "eth-endpoint", "http://localhost:8545",
"ethereum node JSON-RPC endpoint")

Cmd.Flags().StringVar(&addressBookFile, "address-book", "",
"if set, load the address book from the given file; else, use test addresses")
}

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

resp, err := readerclient.GetNotice(ctx, graphqlClient, noticeIndex, inputIndex)
cobra.CheckErr(err)

if resp.Proof == nil {
config.InfoLogger.Printf("The notice has no associated proof yet.\n")
os.Exit(0)
}

client, err := ethclient.DialContext(ctx, ethEndpoint)
cobra.CheckErr(err)
config.InfoLogger.Printf("connected to %v\n", ethEndpoint)

var book *addresses.Book
if addressBookFile != "" {
book, err = addresses.GetBookFromFile(addressBookFile)
cobra.CheckErr(err)
} else {
book = addresses.GetTestBook()
}

proof := readerclient.ConvertToContractProof(resp.Proof)

config.InfoLogger.Printf("validating notice %d from input %d with address %x\n",
noticeIndex,
inputIndex,
book.CartesiDApp,
)
valid, err := ethutil.CallValidateNotice(ctx, client, book, resp.Payload, proof)
_ = valid
cobra.CheckErr(err)

config.InfoLogger.Printf("The notice is valid!\n")
}
1 change: 1 addition & 0 deletions docs/cartesi-rollups-cli.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,5 @@ Cartesi Rollups node.
* [cartesi-rollups-cli read](cartesi-rollups-cli_read.md) - Read the node state from the GraphQL API
* [cartesi-rollups-cli save-snapshot](cartesi-rollups-cli_save-snapshot.md) - Saves the testing Cartesi machine snapshot to the designated folder
* [cartesi-rollups-cli send](cartesi-rollups-cli_send.md) - Send a rollups input to the Ethereum node
* [cartesi-rollups-cli validate](cartesi-rollups-cli_validate.md) - Validates a notice

30 changes: 30 additions & 0 deletions docs/cartesi-rollups-cli_validate.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
## cartesi-rollups-cli validate

Validates a notice

```
cartesi-rollups-cli validate [flags]
```

### Examples

```
# Validates notice 5 from input 6:
cartesi-rollups-cli validate --notice-index 5 --input-index 6
```

### Options

```
--address-book string if set, load the address book from the given file; else, use test addresses
--eth-endpoint string ethereum node JSON-RPC endpoint (default "http://localhost:8545")
--graphql-endpoint string address used to connect to graphql (default "http://0.0.0.0:10004/graphql")
-h, --help help for validate
--input-index int index of the input
--notice-index int index of the notice
```

### SEE ALSO

* [cartesi-rollups-cli](cartesi-rollups-cli.md) - Command line interface for Cartesi Rollups

23 changes: 23 additions & 0 deletions pkg/ethutil/ethutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,3 +107,26 @@ func GetInputFromInputBox(
}
return it.Event, nil
}

// Validate the given notice on the specified Dapp.
// Return whether the notice is valid or not.
func CallValidateNotice(
ctx context.Context,
client *ethclient.Client,
book *addresses.Book,
notice []byte,
proof *contracts.Proof,
) (bool, error) {

dapp, err := contracts.NewCartesiDApp(book.CartesiDApp, client)
if err != nil {
return false, fmt.Errorf("failed to connect to CartesiDapp contract: %v", err)
}

response, err := dapp.ValidateNotice(nil, notice, *proof)
if err != nil {
return false, err
}

return response, nil
}
34 changes: 34 additions & 0 deletions pkg/readerclient/proof.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package readerclient
import (
"fmt"

"github.com/cartesi/rollups-node/pkg/contracts"
"github.com/ethereum/go-ethereum/common/hexutil"
)

Expand Down Expand Up @@ -124,3 +125,36 @@ func newProof(

return &proof, err
}

func ConvertToContractProof(proof *Proof) *contracts.Proof {
var (
outputHashOutputSiblings [][32]byte
outputHashEpochSiblings [][32]byte
)

for _, hash := range proof.OutputHashInOutputHashesSiblings {
outputHashOutputSiblings = append(outputHashOutputSiblings, [32]byte(hash))
}

for _, hash := range proof.OutputHashesInEpochSiblings {
outputHashEpochSiblings = append(outputHashEpochSiblings, [32]byte(hash))
}

outputValidityProof := contracts.OutputValidityProof{
InputIndexWithinEpoch: uint64(proof.InputIndexWithinEpoch),
OutputIndexWithinInput: uint64(proof.OutputIndexWithinInput),
OutputHashesRootHash: [32]byte(proof.OutputHashesRootHash),
VouchersEpochRootHash: [32]byte(proof.VouchersEpochRootHash),
NoticesEpochRootHash: [32]byte(proof.NoticesEpochRootHash),
MachineStateHash: [32]byte(proof.MachineStateHash),
OutputHashInOutputHashesSiblings: outputHashOutputSiblings,
OutputHashesInEpochSiblings: outputHashEpochSiblings,
}

contractProof := contracts.Proof{
Validity: outputValidityProof,
Context: proof.Context,
}

return &contractProof
}

0 comments on commit 5f09036

Please sign in to comment.