Skip to content

Commit

Permalink
feat: allow mining of multiple blocks
Browse files Browse the repository at this point in the history
- implement ethutil.MineBlocks
- add support to cli
  • Loading branch information
marcelstanley committed Jul 22, 2024
1 parent 4b4022d commit 2abb53c
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 29 deletions.
40 changes: 25 additions & 15 deletions cmd/cartesi-rollups-cli/root/mine/mine.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,30 +12,40 @@ import (
)

var Cmd = &cobra.Command{
Use: "mine",
Short: "Mine a new block",
Example: examples,
Run: run,
Use: "mine",
Short: "Mine blocks",
Run: run,
Example: `# Mine 10 blocks with a 5-second interval between them:
cartesi-rollups-cli mine --number-of-blocks 10 --block-interval 5`,
}

const examples = `# Mine a new block:
cartesi-rollups-cli mine`

var (
anvilEndpoint string
ethEndpoint string
numBlocks int
blockInterval int
)

func init() {

Cmd.Flags().StringVar(&anvilEndpoint, "anvil-endpoint", "http://localhost:8545",
"address of anvil endpoint to be used to send the mining request")
Cmd.Flags().StringVar(&ethEndpoint, "eth-endpoint", "http://localhost:8545",
"ethereum node JSON-RPC endpoint")
Cmd.Flags().IntVar(&numBlocks, "number-of-blocks", 1,
"number of blocks to mine")
Cmd.Flags().IntVar(&blockInterval, "block-interval", 1,
"interval, in seconds, between the timestamps of each block")
}

func run(cmd *cobra.Command, args []string) {

blockNumber, err := ethutil.MineNewBlock(context.Background(), anvilEndpoint)

ctx, cancel := context.WithCancel(context.Background())
defer cancel()

slog.Debug("mining blocks",
"numBlocks", numBlocks,
"blockInterval", blockInterval)
blockNumber, err := ethutil.MineBlocks(ctx,
ethEndpoint,
uint64(numBlocks),
uint64(blockInterval))
cobra.CheckErr(err)

slog.Info("Ok", "block number", blockNumber)
slog.Info("done", "last block number", blockNumber)
}
2 changes: 1 addition & 1 deletion docs/cli/cartesi-rollups-cli.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ Cartesi Rollups node.
* [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 mine](cartesi-rollups-cli_mine.md) - Mine a new block
* [cartesi-rollups-cli mine](cartesi-rollups-cli_mine.md) - Mine blocks
* [cartesi-rollups-cli read](cartesi-rollups-cli_read.md) - Read the node state from the GraphQL API
* [cartesi-rollups-cli run-deps](cartesi-rollups-cli_run-deps.md) - Run node dependencies with Docker
* [cartesi-rollups-cli save-snapshot](cartesi-rollups-cli_save-snapshot.md) - Saves the testing Cartesi machine snapshot to the designated folder
Expand Down
12 changes: 7 additions & 5 deletions docs/cli/cartesi-rollups-cli_mine.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
## cartesi-rollups-cli mine

Mine a new block
Mine blocks

```
cartesi-rollups-cli mine [flags]
Expand All @@ -9,15 +9,17 @@ cartesi-rollups-cli mine [flags]
### Examples

```
# Mine a new block:
cartesi-rollups-cli mine
# Mine 10 blocks with a 5-second interval between them:
cartesi-rollups-cli mine --number-of-blocks 10 --block-interval 5
```

### Options

```
--anvil-endpoint string address of anvil endpoint to be used to send the mining request (default "http://localhost:8545")
-h, --help help for mine
--block-interval int interval, in seconds, between the timestamps of each block (default 1)
--eth-endpoint string ethereum node JSON-RPC endpoint (default "http://localhost:8545")
-h, --help help for mine
--number-of-blocks int number of blocks to mine (default 1)
```

### SEE ALSO
Expand Down
12 changes: 9 additions & 3 deletions pkg/ethutil/ethutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -219,17 +219,23 @@ func SetNextDevnetBlockTimestamp(
return client.CallContext(ctx, nil, "evm_setNextBlockTimestamp", timestamp)
}

// Mines a new block
func MineNewBlock(
// Mine blocks.
// Assumes the HTTP provider is anvil as the `rpc_modules` method cannot be
// be relied upon to discover what modules are supported and decide what mine
// method to use
func MineBlocks(
ctx context.Context,
blockchainHttpEndpoint string,
numBlocks uint64,
blockInterval uint64,
) (uint64, error) {
client, err := rpc.DialContext(ctx, blockchainHttpEndpoint)
if err != nil {
return 0, err
}
defer client.Close()
err = client.CallContext(ctx, nil, "evm_mine")

err = client.CallContext(ctx, nil, "anvil_mine", numBlocks, blockInterval)
if err != nil {
return 0, err
}
Expand Down
21 changes: 16 additions & 5 deletions pkg/ethutil/ethutil_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ import (

const testTimeout = 300 * time.Second

// This suite sets up a container running a devnet Ethereum node, and connects to it using
// go-ethereum's client.
// This suite sets up a container running a devnet Ethereum node
// and connects to it using go-ethereum's client.
type EthUtilSuite struct {
suite.Suite
ctx context.Context
Expand Down Expand Up @@ -75,11 +75,22 @@ func (s *EthUtilSuite) TestAddInput() {
s.Require().Equal(payload, event.Input)
}

func (s *EthUtilSuite) TestMineNewBlock() {
blockNumber, err := MineNewBlock(s.ctx, s.endpoint)
func (s *EthUtilSuite) TestMineOneBlock() {
s.mineBlocks(1)
}

func (s *EthUtilSuite) TestMineAHundredBlocks() {
s.mineBlocks(100)
}

func (s *EthUtilSuite) mineBlocks(numBlocks uint64) {
lastBlockNumber, err := s.client.BlockNumber(s.ctx)
s.Require().Nil(err)
s.Require().Equal(uint64(22), blockNumber)
expectedBlockNumber := lastBlockNumber + numBlocks

blockNumber, err := MineBlocks(s.ctx, s.endpoint, numBlocks, 1)
s.Require().Nil(err)
s.Require().Equal(expectedBlockNumber, blockNumber)
}

// Log the output of the given container
Expand Down

0 comments on commit 2abb53c

Please sign in to comment.