Skip to content

Commit

Permalink
Override network config with the one in database
Browse files Browse the repository at this point in the history
  • Loading branch information
weiihann committed Oct 21, 2024
1 parent 0d02f9e commit ab69e5a
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 24 deletions.
23 changes: 2 additions & 21 deletions cmd/juno/dbcmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ import (
"os"

"github.com/NethermindEth/juno/blockchain"
"github.com/NethermindEth/juno/core"
"github.com/NethermindEth/juno/core/felt"
"github.com/NethermindEth/juno/db"
"github.com/NethermindEth/juno/db/pebble"
"github.com/NethermindEth/juno/node"
"github.com/NethermindEth/juno/utils"
"github.com/olekukonko/tablewriter"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -97,7 +97,7 @@ func dbInfo(cmd *cobra.Command, args []string) error {
return fmt.Errorf("failed to get the state update: %v", err)
}

info.Network = getNetwork(headBlock, stateUpdate.StateDiff)
info.Network = node.GetNetwork(headBlock, stateUpdate.StateDiff).Name
info.ChainHeight = headBlock.Number
info.LatestBlockHash = headBlock.Hash
info.LatestStateRoot = headBlock.GlobalStateRoot
Expand Down Expand Up @@ -235,25 +235,6 @@ func dbSize(cmd *cobra.Command, args []string) error {
return nil
}

func getNetwork(head *core.Block, stateDiff *core.StateDiff) string {
networks := []*utils.Network{
&utils.Mainnet,
&utils.Sepolia,
&utils.Goerli,
&utils.Goerli2,
&utils.Integration,
&utils.SepoliaIntegration,
}

for _, network := range networks {
if _, err := core.VerifyBlockHash(head, network, stateDiff); err == nil {
return network.Name
}
}

return "unknown"
}

func openDB(path string) (db.DB, error) {
_, err := os.Stat(path)
if os.IsNotExist(err) {
Expand Down
28 changes: 25 additions & 3 deletions node/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,9 +141,12 @@ func New(cfg *Config, version string) (*Node, error) { //nolint:gocyclo,funlen
return nil, err
}

// 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, stateUpdate.StateDiff); err != nil {
return nil, errors.New("unable to verify latest block hash; are the database and --network option compatible?")
network := GetNetwork(head, stateUpdate.StateDiff)
if network != nil {
if cfg.Network.Name != network.Name {
log.Warnw("Network mismatch between config and database. Overriding config", "config", cfg.Network.Name, "database", network.Name)
}
cfg.Network = *network
}
}

Expand Down Expand Up @@ -382,3 +385,22 @@ func (n *Node) Run(ctx context.Context) {
func (n *Node) Config() Config {
return *n.cfg
}

func GetNetwork(head *core.Block, stateDiff *core.StateDiff) *utils.Network {
networks := []*utils.Network{
&utils.Mainnet,
&utils.Sepolia,
&utils.Goerli,
&utils.Goerli2,
&utils.Integration,
&utils.SepoliaIntegration,
}

for _, network := range networks {
if _, err := core.VerifyBlockHash(head, network, stateDiff); err == nil {
return network
}
}

return nil
}

0 comments on commit ab69e5a

Please sign in to comment.