2018-05-07 11:35:06 +00:00
|
|
|
// Copyright 2018 The go-ethereum Authors
|
|
|
|
// This file is part of the go-ethereum library.
|
|
|
|
//
|
|
|
|
// The go-ethereum library is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Lesser General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// The go-ethereum library is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU Lesser General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Lesser General Public License
|
|
|
|
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
package rawdb
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2020-10-24 06:57:09 +00:00
|
|
|
"fmt"
|
2018-05-07 11:35:06 +00:00
|
|
|
|
2021-05-20 18:25:53 +00:00
|
|
|
"github.com/ledgerwatch/erigon/common/dbutils"
|
|
|
|
"github.com/ledgerwatch/erigon/ethdb"
|
2019-05-27 13:51:49 +00:00
|
|
|
|
2021-05-20 18:25:53 +00:00
|
|
|
"github.com/ledgerwatch/erigon/common"
|
|
|
|
"github.com/ledgerwatch/erigon/params"
|
2018-05-07 11:35:06 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// ReadChainConfig retrieves the consensus settings based on the given genesis hash.
|
2021-04-05 13:04:58 +00:00
|
|
|
func ReadChainConfig(db ethdb.KVGetter, hash common.Hash) (*params.ChainConfig, error) {
|
|
|
|
data, err := db.GetOne(dbutils.ConfigPrefix, hash[:])
|
|
|
|
if err != nil {
|
2020-10-24 06:57:09 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
2018-05-07 11:35:06 +00:00
|
|
|
if len(data) == 0 {
|
2020-10-24 06:57:09 +00:00
|
|
|
return nil, nil
|
2018-05-07 11:35:06 +00:00
|
|
|
}
|
|
|
|
var config params.ChainConfig
|
|
|
|
if err := json.Unmarshal(data, &config); err != nil {
|
2020-10-24 06:57:09 +00:00
|
|
|
return nil, fmt.Errorf("invalid chain config JSON: %x, %w", hash, err)
|
2018-05-07 11:35:06 +00:00
|
|
|
}
|
2020-10-24 06:57:09 +00:00
|
|
|
return &config, nil
|
2018-05-07 11:35:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// WriteChainConfig writes the chain config settings to the database.
|
2021-04-05 13:04:58 +00:00
|
|
|
func WriteChainConfig(db ethdb.Putter, hash common.Hash, cfg *params.ChainConfig) error {
|
2018-05-07 11:35:06 +00:00
|
|
|
if cfg == nil {
|
2020-10-24 06:57:09 +00:00
|
|
|
return nil
|
2018-05-07 11:35:06 +00:00
|
|
|
}
|
|
|
|
data, err := json.Marshal(cfg)
|
|
|
|
if err != nil {
|
2020-10-24 06:57:09 +00:00
|
|
|
return fmt.Errorf("failed to JSON encode chain config: %w", err)
|
2018-05-07 11:35:06 +00:00
|
|
|
}
|
2019-05-27 13:51:49 +00:00
|
|
|
if err := db.Put(dbutils.ConfigPrefix, hash[:], data); err != nil {
|
2020-10-24 06:57:09 +00:00
|
|
|
return fmt.Errorf("failed to store chain config: %w", err)
|
2018-05-07 11:35:06 +00:00
|
|
|
}
|
2021-03-12 17:26:06 +00:00
|
|
|
return nil
|
2018-05-07 11:35:06 +00:00
|
|
|
}
|
2021-06-25 18:13:40 +00:00
|
|
|
|
|
|
|
// DeleteChainConfig retrieves the consensus settings based on the given genesis hash.
|
|
|
|
func DeleteChainConfig(db ethdb.Deleter, hash common.Hash) error {
|
|
|
|
return db.Delete(dbutils.ConfigPrefix, hash[:], nil)
|
|
|
|
}
|