Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add execute voucher command #285

Merged
merged 1 commit into from
Jan 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 increase-time command to `cartesi-rollups-cli`
- Added instructions on how to run the node with Docker.
- Added validate command to `cartesi-rollups-cli`
- Added execute command to `cartesi-rollups-cli`

### Changed

Expand Down
110 changes: 110 additions & 0 deletions cmd/cartesi-rollups-cli/root/execute/execute.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
// (c) Cartesi and individual authors (see AUTHORS)
// SPDX-License-Identifier: Apache-2.0 (see LICENSE)

package execute

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

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

var (
voucherIndex int
inputIndex int
graphqlEndpoint string
ethEndpoint string
mnemonic string
account uint32
addressBookFile 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: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(&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(&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.GetVoucher(ctx, graphqlClient, voucherIndex, inputIndex)
cobra.CheckErr(err)

if resp.Proof == nil {
config.InfoLogger.Printf("The voucher 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)

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()
}

proof := readerclient.ConvertToContractProof(resp.Proof)

config.InfoLogger.Printf("executing voucher %d from input %d\n",
voucherIndex,
inputIndex,
)
txHash, err := ethutil.ExecuteVoucher(
ctx,
client,
book,
signer,
resp.Payload,
&resp.Destination,
proof,
)
cobra.CheckErr(err)

config.InfoLogger.Printf("The voucher was executed! (tx=%v)\n", txHash)
}
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 @@ -5,6 +5,7 @@ package root

import (
"github.com/cartesi/rollups-node/cmd/cartesi-rollups-cli/root/deps"
"github.com/cartesi/rollups-node/cmd/cartesi-rollups-cli/root/execute"
"github.com/cartesi/rollups-node/cmd/cartesi-rollups-cli/root/increasetime"
"github.com/cartesi/rollups-node/cmd/cartesi-rollups-cli/root/inspect"
"github.com/cartesi/rollups-node/cmd/cartesi-rollups-cli/root/read"
Expand All @@ -29,5 +30,6 @@ func init() {
Cmd.AddCommand(increasetime.Cmd)
Cmd.AddCommand(validate.Cmd)
Cmd.AddCommand(deps.Cmd)
Cmd.AddCommand(execute.Cmd)
Cmd.DisableAutoGenTag = true
}
1 change: 1 addition & 0 deletions docs/cartesi-rollups-cli.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ Cartesi Rollups node.

### SEE ALSO

* [cartesi-rollups-cli execute](cartesi-rollups-cli_execute.md) - Executes a voucher
* [cartesi-rollups-cli increase-time](cartesi-rollups-cli_increase-time.md) - Increases evm time of the current machine
* [cartesi-rollups-cli inspect](cartesi-rollups-cli_inspect.md) - Calls inspect API
* [cartesi-rollups-cli read](cartesi-rollups-cli_read.md) - Read the node state from the GraphQL API
Expand Down
32 changes: 32 additions & 0 deletions docs/cartesi-rollups-cli_execute.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
## cartesi-rollups-cli execute

Executes a voucher

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

### Examples

```
# Executes voucher 5 from input 6:
cartesi-rollups-cli execute --voucher-index 5 --input-index 6
```

### 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")
--graphql-endpoint string address used to connect to graphql (default "http://0.0.0.0:10004/graphql")
-h, --help help for execute
--input-index int index of the input
--mnemonic string mnemonic used to sign the transaction (default "test test test test test test test test test test test junk")
--voucher-index int index of the voucher
```

### SEE ALSO

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

28 changes: 28 additions & 0 deletions pkg/ethutil/ethutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,3 +131,31 @@ func ValidateNotice(

return nil
}

// Executes a voucher given its payload, destination and proof.
// This function waits until the transaction is added to a block and returns the transaction hash.
func ExecuteVoucher(
ctx context.Context,
client *ethclient.Client,
book *addresses.Book,
signer Signer,
voucher []byte,
destination *common.Address,
proof *contracts.Proof,
) (*common.Hash, error) {
dapp, err := contracts.NewCartesiDApp(book.CartesiDApp, client)
if err != nil {
return nil, fmt.Errorf("failed to connect to CartesiDapp contract: %v", err)
}
receipt, err := sendTransaction(
ctx, client, signer, big.NewInt(0), GasLimit,
func(txOpts *bind.TransactOpts) (*types.Transaction, error) {
return dapp.ExecuteVoucher(txOpts, *destination, voucher, *proof)
},
)
if err != nil {
return nil, err
}

return &receipt.TxHash, nil
}
Loading