Skip to content

Commit

Permalink
Validate the --network option
Browse files Browse the repository at this point in the history
  • Loading branch information
joshklop authored and wojciechos committed Dec 20, 2023
1 parent bc8e51d commit eced4f5
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 0 deletions.
14 changes: 14 additions & 0 deletions node/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/NethermindEth/juno/blockchain"
"github.com/NethermindEth/juno/clients/feeder"
"github.com/NethermindEth/juno/clients/gateway"
"github.com/NethermindEth/juno/core"
"github.com/NethermindEth/juno/db"
"github.com/NethermindEth/juno/db/pebble"
"github.com/NethermindEth/juno/db/remote"
Expand Down Expand Up @@ -120,6 +121,19 @@ func New(cfg *Config, version string) (*Node, error) { //nolint:gocyclo,funlen
services := make([]service.Service, 0)

chain := blockchain.New(database, cfg.Network, log)

// Verify that cfg.Network is compatible with the database.
head, err := chain.Head()
if err != nil && !errors.Is(err, db.ErrKeyNotFound) {
return nil, fmt.Errorf("get head block from database: %v", err)
}
if head != nil {
// We assume that there is at least one transaction in the block or that it is a pre-0.7 block.
if _, err = core.VerifyBlockHash(head, cfg.Network); err != nil {
return nil, errors.New("unable to verify latest block hash; are the database and --network option compatible?")
}
}

feederClientTimeout := 5 * time.Second
client := feeder.NewClient(cfg.Network.FeederURL()).WithUserAgent(ua).WithLogger(log).
WithTimeout(feederClientTimeout).WithAPIKey(cfg.GatewayAPIKey)
Expand Down
47 changes: 47 additions & 0 deletions node/node_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,12 @@ import (
"testing"
"time"

"github.com/NethermindEth/juno/blockchain"
"github.com/NethermindEth/juno/clients/feeder"
"github.com/NethermindEth/juno/db/pebble"
"github.com/NethermindEth/juno/node"
adaptfeeder "github.com/NethermindEth/juno/starknetdata/feeder"
"github.com/NethermindEth/juno/sync"
"github.com/NethermindEth/juno/utils"
"github.com/stretchr/testify/require"
)
Expand Down Expand Up @@ -41,3 +46,45 @@ func TestNewNode(t *testing.T) {
cancel()
n.Run(ctx)
}

func TestNetworkVerificationOnNonEmptyDB(t *testing.T) {
network := utils.Integration
tests := map[string]struct {
network utils.Network
errString string
}{
"same network": {
network: network,
errString: "",
},
"different network": {
network: utils.Mainnet,
errString: "unable to verify latest block hash; are the database and --network option compatible?",
},
}

for description, test := range tests {
t.Run(description, func(t *testing.T) {
dbPath := t.TempDir()
log := utils.NewNopZapLogger()
database, err := pebble.New(dbPath, 1, log)
require.NoError(t, err)
chain := blockchain.New(database, network, log)
syncer := sync.New(chain, adaptfeeder.New(feeder.NewTestClient(t, network)), log, 0, false)
ctx, cancel := context.WithTimeout(context.Background(), 250*time.Millisecond)
require.NoError(t, syncer.Run(ctx))
cancel()
require.NoError(t, database.Close())

_, err = node.New(&node.Config{
DatabasePath: dbPath,
Network: test.network,
}, "v0.1")
if test.errString == "" {
require.NoError(t, err)
} else {
require.ErrorContains(t, err, test.errString)
}
})
}
}

0 comments on commit eced4f5

Please sign in to comment.