Commit 1fb007e4 authored by Ralph Pichler's avatar Ralph Pichler Committed by GitHub

prevent nodes from starting with a different overlay (#1377)

parent e043c29e
...@@ -49,17 +49,22 @@ func (c *command) initDeployCmd() error { ...@@ -49,17 +49,22 @@ func (c *command) initDeployCmd() error {
stateStore, err := node.InitStateStore(logger, dataDir) stateStore, err := node.InitStateStore(logger, dataDir)
if err != nil { if err != nil {
return return err
} }
defer stateStore.Close() defer stateStore.Close()
signerConfig, err := c.configureSigner(cmd, logger) signerConfig, err := c.configureSigner(cmd, logger)
if err != nil { if err != nil {
return return err
} }
signer := signerConfig.signer signer := signerConfig.signer
err = node.CheckOverlayWithStore(signerConfig.address, stateStore)
if err != nil {
return err
}
ctx := cmd.Context() ctx := cmd.Context()
swapBackend, overlayEthAddress, chainID, transactionService, err := node.InitChain( swapBackend, overlayEthAddress, chainID, transactionService, err := node.InitChain(
...@@ -70,7 +75,7 @@ func (c *command) initDeployCmd() error { ...@@ -70,7 +75,7 @@ func (c *command) initDeployCmd() error {
signer, signer,
) )
if err != nil { if err != nil {
return return err
} }
defer swapBackend.Close() defer swapBackend.Close()
...@@ -82,7 +87,7 @@ func (c *command) initDeployCmd() error { ...@@ -82,7 +87,7 @@ func (c *command) initDeployCmd() error {
factoryAddress, factoryAddress,
) )
if err != nil { if err != nil {
return return err
} }
_, err = node.InitChequebookService( _, err = node.InitChequebookService(
...@@ -98,7 +103,7 @@ func (c *command) initDeployCmd() error { ...@@ -98,7 +103,7 @@ func (c *command) initDeployCmd() error {
swapInitialDeposit, swapInitialDeposit,
) )
return return err
}, },
PreRunE: func(cmd *cobra.Command, args []string) error { PreRunE: func(cmd *cobra.Command, args []string) error {
return c.config.BindPFlags(cmd.Flags()) return c.config.BindPFlags(cmd.Flags())
......
...@@ -10,6 +10,7 @@ import ( ...@@ -10,6 +10,7 @@ import (
"strings" "strings"
"github.com/ethersphere/bee/pkg/logging" "github.com/ethersphere/bee/pkg/logging"
"github.com/ethersphere/bee/pkg/node"
"github.com/sirupsen/logrus" "github.com/sirupsen/logrus"
"github.com/spf13/cobra" "github.com/spf13/cobra"
) )
...@@ -41,8 +42,20 @@ func (c *command) initInitCmd() (err error) { ...@@ -41,8 +42,20 @@ func (c *command) initInitCmd() (err error) {
return fmt.Errorf("unknown verbosity level %q", v) return fmt.Errorf("unknown verbosity level %q", v)
} }
_, err = c.configureSigner(cmd, logger) signerConfig, err := c.configureSigner(cmd, logger)
if err != nil {
return err return err
}
dataDir := c.config.GetString(optionNameDataDir)
stateStore, err := node.InitStateStore(logger, dataDir)
if err != nil {
return err
}
defer stateStore.Close()
return node.CheckOverlayWithStore(signerConfig.address, stateStore)
}, },
PreRunE: func(cmd *cobra.Command, args []string) error { PreRunE: func(cmd *cobra.Command, args []string) error {
return c.config.BindPFlags(cmd.Flags()) return c.config.BindPFlags(cmd.Flags())
......
...@@ -167,6 +167,11 @@ func NewBee(addr string, swarmAddress swarm.Address, publicKey ecdsa.PublicKey, ...@@ -167,6 +167,11 @@ func NewBee(addr string, swarmAddress swarm.Address, publicKey ecdsa.PublicKey,
} }
b.stateStoreCloser = stateStore b.stateStoreCloser = stateStore
err = CheckOverlayWithStore(swarmAddress, stateStore)
if err != nil {
return nil, err
}
addressbook := addressbook.New(stateStore) addressbook := addressbook.New(stateStore)
var swapBackend *ethclient.Client var swapBackend *ethclient.Client
......
...@@ -5,12 +5,15 @@ ...@@ -5,12 +5,15 @@
package node package node
import ( import (
"errors"
"fmt"
"path/filepath" "path/filepath"
"github.com/ethersphere/bee/pkg/logging" "github.com/ethersphere/bee/pkg/logging"
"github.com/ethersphere/bee/pkg/statestore/leveldb" "github.com/ethersphere/bee/pkg/statestore/leveldb"
"github.com/ethersphere/bee/pkg/statestore/mock" "github.com/ethersphere/bee/pkg/statestore/mock"
"github.com/ethersphere/bee/pkg/storage" "github.com/ethersphere/bee/pkg/storage"
"github.com/ethersphere/bee/pkg/swarm"
) )
// InitStateStore will initialze the stateStore with the given path to the // InitStateStore will initialze the stateStore with the given path to the
...@@ -24,3 +27,22 @@ func InitStateStore(log logging.Logger, dataDir string) (ret storage.StateStorer ...@@ -24,3 +27,22 @@ func InitStateStore(log logging.Logger, dataDir string) (ret storage.StateStorer
} }
return leveldb.NewStateStore(filepath.Join(dataDir, "statestore"), log) return leveldb.NewStateStore(filepath.Join(dataDir, "statestore"), log)
} }
const overlayKey = "overlay"
// CheckOverlayWithStore checks the overlay is the same as stored in the statestore
func CheckOverlayWithStore(overlay swarm.Address, storer storage.StateStorer) error {
var storedOverlay swarm.Address
err := storer.Get(overlayKey, &storedOverlay)
if err != nil {
if !errors.Is(err, storage.ErrNotFound) {
return err
}
return storer.Put(overlayKey, overlay)
}
if !storedOverlay.Equal(overlay) {
return fmt.Errorf("overlay address changed. was %s before but now is %s", storedOverlay, overlay)
}
return nil
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment