Skip to content

Commit

Permalink
feat(cli): add command to send inputs
Browse files Browse the repository at this point in the history
  • Loading branch information
gligneul committed Nov 24, 2023
1 parent 0a9dbb7 commit 8bf60c3
Show file tree
Hide file tree
Showing 14 changed files with 262 additions and 85 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Added

- Added `cartesi-rollups-cli` binary to help develop and debug the Cartesi Rollups node

## [1.2.0]

### Added

- Added `DAPP_ADDRESS`, `DAPP_DEPLOY_BLOCK_HASH`, `HISTORY_ADDRESS`, `AUTHORITY_ADDRESS`, and
`INPUT_BOX_ADDRESS` env vars to the dispatcher and the authority claimer.
These are optional and will overwrite the values from the dapp and rollups deployment files.
Expand Down
19 changes: 3 additions & 16 deletions cmd/cartesi-rollups-cli/main.go
Original file line number Diff line number Diff line change
@@ -1,30 +1,17 @@
// (c) Cartesi and individual authors (see AUTHORS)
// SPDX-License-Identifier: Apache-2.0 (see LICENSE)

// This package contains the cartesi-rollups CLI binary.
package main

import (
"os"

"github.com/cartesi/rollups-node/cmd/cartesi-rollups-cli/send"
"github.com/spf13/cobra"
"github.com/cartesi/rollups-node/cmd/cartesi-rollups-cli/root"
)

var rootCmd = &cobra.Command{
Use: "cartesi-rollups-cli",
Short: "Command line interface for Cartesi Rollups",
Long: `Command line interface for Cartesi Rollups
This command line interface provides functionality to help develop and debug
the Cartesi Rollups node.`,
}

func init() {
rootCmd.AddCommand(send.Cmd)
}

func main() {
err := rootCmd.Execute()
err := root.Cmd.Execute()
if err != nil {
os.Exit(1)
}
Expand Down
20 changes: 20 additions & 0 deletions cmd/cartesi-rollups-cli/root/root.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// (c) Cartesi and individual authors (see AUTHORS)
// SPDX-License-Identifier: Apache-2.0 (see LICENSE)

package root

import (
"github.com/cartesi/rollups-node/cmd/cartesi-rollups-cli/root/send"
"github.com/spf13/cobra"
)

var Cmd = &cobra.Command{
Use: "cartesi-rollups-cli",
Short: "Command line interface for Cartesi Rollups",
Long: `This command line interface provides functionality to help develop and debug the
Cartesi Rollups node.`,
}

func init() {
Cmd.AddCommand(send.Cmd)
}
79 changes: 79 additions & 0 deletions cmd/cartesi-rollups-cli/root/send/send.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
// (c) Cartesi and individual authors (see AUTHORS)
// SPDX-License-Identifier: Apache-2.0 (see LICENSE)

package send

import (
"github.com/cartesi/rollups-node/internal/logger"
"github.com/cartesi/rollups-node/pkg/addresses"
"github.com/cartesi/rollups-node/pkg/ethutil"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/ethclient"
"github.com/spf13/cobra"
)

var Cmd = &cobra.Command{
Use: "send",
Short: "Send a rollups input to the Ethereum node",
Example: examples,
Run: run,
}

const examples = `# Send the string "hi" encoded as hex:
cartesi-rollups-cli send --payload 0x$(printf "hi" | xxd -p)`

var (
ethEndpoint string
mnemonic string
account uint32
hexPayload string
addressBookFile string
)

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

Cmd.Flags().StringVar(&mnemonic, "mnemonic", ethutil.FoundryMnemonic,
"mnemonic used to sign the transaction")

Cmd.Flags().Uint32Var(&account, "account", 0,
"account index used to sign the transaction (default: 0)")

Cmd.Flags().StringVar(&hexPayload, "payload", "",
"input payload hex-encoded starting with 0x")

cobra.CheckErr(Cmd.MarkFlagRequired("payload"))

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) {
logger.Init("info", true)

payload, err := hexutil.Decode(hexPayload)
cobra.CheckErr(err)

ctx := cmd.Context()
client, err := ethclient.DialContext(ctx, ethEndpoint)
cobra.CheckErr(err)
logger.Info.Printf("connected to %v", ethEndpoint)

signer, err := ethutil.NewMnemonicSigner(ctx, client, mnemonic, account)
cobra.CheckErr(err)

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

logger.Info.Printf("sending input to %x", book.CartesiDApp)
inputIndex, err := ethutil.AddInput(ctx, client, book, signer, payload)
cobra.CheckErr(err)

logger.Info.Printf("added input with index %v", inputIndex)
}
43 changes: 0 additions & 43 deletions cmd/cartesi-rollups-cli/send/send.go

This file was deleted.

25 changes: 25 additions & 0 deletions cmd/gen-docs/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// (c) Cartesi and individual authors (see AUTHORS)
// SPDX-License-Identifier: Apache-2.0 (see LICENSE)

// This binary generates part of the node documentation automatically.
// It should be executed from the project root dir with: `go run ./cmd/gen-docs`.
package main

import (
"log"

"github.com/cartesi/rollups-node/cmd/cartesi-rollups-cli/root"
"github.com/spf13/cobra/doc"
)

func main() {
generateCartesiRollupsCliDocs()
}

func generateCartesiRollupsCliDocs() {
err := doc.GenMarkdownTree(root.Cmd, "./docs")
if err != nil {
log.Fatalf("failed to gen cartesi-rollups-cli docs: %v", err)
}
log.Print("generated docs for cartesi-rollups-cli")
}
20 changes: 20 additions & 0 deletions docs/cartesi-rollups-cli.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
## cartesi-rollups-cli

Command line interface for Cartesi Rollups

### Synopsis

This command line interface provides functionality to help develop and debug the
Cartesi Rollups node.

### Options

```
-h, --help help for cartesi-rollups-cli
```

### SEE ALSO

* [cartesi-rollups-cli send](cartesi-rollups-cli_send.md) - Send a rollups input to the Ethereum node

###### Auto generated by spf13/cobra on 19-Nov-2023
31 changes: 31 additions & 0 deletions docs/cartesi-rollups-cli_send.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
## cartesi-rollups-cli send

Send a rollups input to the Ethereum node

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

### Examples

```
# Send the string "hi" encoded as hex:
cartesi-rollups-cli send --payload 0x$(printf "hi" | xxd -p)
```

### Options

```
--account uint32 account index used to sign the transaction (default: 0)
--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")
-h, --help help for send
--mnemonic string mnemonic used to sign the transaction (default "test test test test test test test test test test test junk")
--payload string input payload hex-encoded starting with 0x
```

### SEE ALSO

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

###### Auto generated by spf13/cobra on 19-Nov-2023
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ require (
github.com/containerd/containerd v1.7.7 // indirect
github.com/containerd/log v0.1.0 // indirect
github.com/cpuguy83/dockercfg v0.3.1 // indirect
github.com/cpuguy83/go-md2man/v2 v2.0.2 // indirect
github.com/crate-crypto/go-kzg-4844 v0.7.0 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/deckarep/golang-set/v2 v2.1.0 // indirect
Expand Down Expand Up @@ -59,6 +60,7 @@ require (
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/power-devops/perfstat v0.0.0-20210106213030-5aafc221ea8c // indirect
github.com/russross/blackfriday/v2 v2.1.0 // indirect
github.com/shirou/gopsutil v3.21.4-0.20210419000835-c7a38de76ee5+incompatible // indirect
github.com/shirou/gopsutil/v3 v3.23.9 // indirect
github.com/shoenig/go-m1cpu v0.1.6 // indirect
Expand Down
1 change: 0 additions & 1 deletion go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,6 @@ github.com/rogpeppe/go-internal v1.9.0 h1:73kH8U+JUqXU8lRuOHeVHaa/SZPifC7BkcraZV
github.com/rogpeppe/go-internal v1.9.0/go.mod h1:WtVeX8xhTBvf0smdhujwtBcq4Qrzq/fJaraNFVN+nFs=
github.com/rs/cors v1.7.0 h1:+88SsELBHx5r+hZ8TCkggzSstaWNbDvThkVK8H6f9ik=
github.com/rs/cors v1.7.0/go.mod h1:gFx+x8UowdsKA9AchylcLynDq+nNFfI8FkUZdN/jGCU=
github.com/russross/blackfriday v1.6.0 h1:KqfZb0pUVN2lYqZUYRddxF4OR8ZMURnJIG5Y3VRLtww=
github.com/russross/blackfriday/v2 v2.0.1/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/russross/blackfriday/v2 v2.1.0 h1:JIOH55/0cWyOuilr9/qlrm0BSXldqnqwMsf35Ld67mk=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
Expand Down
60 changes: 60 additions & 0 deletions pkg/addresses/addresses.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
// (c) Cartesi and individual authors (see AUTHORS)
// SPDX-License-Identifier: Apache-2.0 (see LICENSE)

// This package manages the contract addresses.
//
// The addresses depend on the deployment of the contracts and should be provided by the node user.
// This module offers an option to load these addresses from a config file, compatible with the
// output of `sunodo address-book --json`.
// This package also contain the addresses for the test environment as hard-coded values.
package addresses

import (
"encoding/json"
"fmt"
"os"

"github.com/ethereum/go-ethereum/common"
)

// List of contract addresses.
type Book struct {
CartesiDAppFactory common.Address
DAppAddressRelay common.Address
ERC1155BatchPortal common.Address
ERC1155SinglePortal common.Address
ERC20Portal common.Address
ERC721Portal common.Address
EtherPortal common.Address
InputBox common.Address
CartesiDApp common.Address
}

// Get the addresses for the test environment.
func GetTestBook() *Book {
return &Book{
CartesiDAppFactory: common.HexToAddress("0x7122cd1221C20892234186facfE8615e6743Ab02"),
DAppAddressRelay: common.HexToAddress("0xF5DE34d6BbC0446E2a45719E718efEbaaE179daE"),
ERC1155BatchPortal: common.HexToAddress("0xedB53860A6B52bbb7561Ad596416ee9965B055Aa"),
ERC1155SinglePortal: common.HexToAddress("0x7CFB0193Ca87eB6e48056885E026552c3A941FC4"),
ERC20Portal: common.HexToAddress("0x9C21AEb2093C32DDbC53eEF24B873BDCd1aDa1DB"),
ERC721Portal: common.HexToAddress("0x237F8DD094C0e47f4236f12b4Fa01d6Dae89fb87"),
EtherPortal: common.HexToAddress("0xFfdbe43d4c855BF7e0f105c400A50857f53AB044"),
InputBox: common.HexToAddress("0x59b22D57D4f067708AB0c00552767405926dc768"),
CartesiDApp: common.HexToAddress("0x70ac08179605AF2D9e75782b8DEcDD3c22aA4D0C"),
}
}

// Get the address book from json File.
func GetBookFromFile(path string) (*Book, error) {
data, err := os.ReadFile(path)
if err != nil {
return nil, fmt.Errorf("read address book file: %v", err)
}
var book Book
err = json.Unmarshal(data, &book)
if err != nil {
return nil, fmt.Errorf("parse address book json: %v", err)
}
return &book, nil
}
14 changes: 0 additions & 14 deletions pkg/contracts/addresses.go

This file was deleted.

Loading

0 comments on commit 8bf60c3

Please sign in to comment.