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

Validate Chain ID #339

Merged
merged 2 commits into from
Mar 12, 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
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,16 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## Unreleased

### Added

- Added verification to ensure CARTESI_BLOCKCHAIN_ID matches the id returned from the Ethereum node

## Changed

- Changed CARTESI_BLOCKCHAIN_ID type from int to uint64

## [1.3.0] 2024-02-09

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

package main

import (
"context"
"fmt"
"time"

"github.com/cartesi/rollups-node/internal/config"
"github.com/ethereum/go-ethereum/ethclient"
)

const defaultTimeout = 3 * time.Second

// Checks if the chain id from the configuration matches the chain id reported
// by the Ethereum node. If they don't, it returns an error.
func validateChainId(ctx context.Context, chainId uint64, ethereumNodeAddr string) error {
remoteChainId, err := getChainId(ctx, ethereumNodeAddr)
if err != nil {
config.ErrorLogger.Printf("Couldn't validate chainId: %v\n", err)
} else if chainId != remoteChainId {
return fmt.Errorf(
"chainId mismatch. Expected %v but Ethereum node returned %v",
chainId,
remoteChainId,
)
}
return nil
}

func getChainId(ctx context.Context, ethereumNodeAddr string) (uint64, error) {
ctx, cancel := context.WithTimeout(ctx, defaultTimeout)
defer cancel()

client, err := ethclient.Dial(ethereumNodeAddr)
if err != nil {
return 0, fmt.Errorf("Failed to create RPC client: %v", err)
}
chainId, err := client.ChainID(ctx)
if err != nil {
return 0, fmt.Errorf("Failed to get chain id: %v", err)
}
return chainId.Uint64(), nil
}
48 changes: 48 additions & 0 deletions cmd/cartesi-rollups-node/chainid_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// (c) Cartesi and individual authors (see AUTHORS)
// SPDX-License-Identifier: Apache-2.0 (see LICENSE)

package main

import (
"context"
"fmt"
"net/http"
"net/http/httptest"
"testing"

"github.com/stretchr/testify/suite"
)

type ValidateChainIdSuite struct {
suite.Suite
}

func TestValidateChainId(t *testing.T) {
suite.Run(t, new(ValidateChainIdSuite))
}

func (s *ValidateChainIdSuite) TestItFailsIfChainIdsDoNotMatch() {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
torives marked this conversation as resolved.
Show resolved Hide resolved
w.Header().Set("Content-Type", "application/json")
fmt.Fprintln(w, `{"jsonrpc":"2.0","id":67,"result":"0x7a69"}`)
}))
defer ts.Close()
localChainId := uint64(11111)

err := validateChainId(context.Background(), localChainId, ts.URL)

s.NotNil(err)
}

func (s *ValidateChainIdSuite) TestItReturnsNilIfChainIdsMatch() {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
fmt.Fprintln(w, `{"jsonrpc":"2.0","id":67,"result":"0x7a69"}`)
}))
defer ts.Close()
localChainId := uint64(31337)

err := validateChainId(context.Background(), localChainId, ts.URL)

s.Nil(err)
}
8 changes: 8 additions & 0 deletions cmd/cartesi-rollups-node/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,14 @@ func main() {
ctx, stop := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM)
defer stop()

if err := validateChainId(
ctx,
config.GetCartesiBlockchainId(),
config.GetCartesiBlockchainHttpEndpoint(),
); err != nil {
config.ErrorLogger.Fatal(err)
}

sunodoValidatorEnabled := config.GetCartesiExperimentalSunodoValidatorEnabled()
if !sunodoValidatorEnabled {
// add Redis first
Expand Down
2 changes: 1 addition & 1 deletion docs/config.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ HTTP endpoint for the blockchain RPC provider.

An unique identifier representing a blockchain network.

* **Type:** `int`
* **Type:** `uint64`

## `CARTESI_BLOCKCHAIN_IS_LEGACY`

Expand Down
6 changes: 6 additions & 0 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@ func toInt64FromString(s string) (int64, error) {
return strconv.ParseInt(s, 10, 64)
}

func toUint64FromString(s string) (uint64, error) {
value, err := strconv.ParseUint(s, 10, 64)
return value, err
}

func toStringFromString(s string) (string, error) {
return s, nil
}
Expand All @@ -42,6 +47,7 @@ func toDurationFromSeconds(s string) (time.Duration, error) {
var (
toBool = strconv.ParseBool
toInt = strconv.Atoi
toUint64 = toUint64FromString
toInt64 = toInt64FromString
toString = toStringFromString
toDuration = toDurationFromSeconds
Expand Down
2 changes: 1 addition & 1 deletion internal/config/generate/Config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ At the end of each epoch, the node will send claims to the blockchain."""
#

[blockchain.CARTESI_BLOCKCHAIN_ID]
go-type = "int"
go-type = "uint64"
description = """
An unique identifier representing a blockchain network."""

Expand Down
4 changes: 2 additions & 2 deletions internal/config/get.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading