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
|
|
|
"errors"
|
|
|
|
"fmt"
|
2018-05-07 11:35:06 +00:00
|
|
|
|
2019-05-27 13:51:49 +00:00
|
|
|
"github.com/ledgerwatch/turbo-geth/common/dbutils"
|
2020-10-24 06:57:09 +00:00
|
|
|
"github.com/ledgerwatch/turbo-geth/ethdb"
|
2019-05-27 13:51:49 +00:00
|
|
|
|
|
|
|
"github.com/ledgerwatch/turbo-geth/common"
|
|
|
|
"github.com/ledgerwatch/turbo-geth/log"
|
|
|
|
"github.com/ledgerwatch/turbo-geth/params"
|
|
|
|
"github.com/ledgerwatch/turbo-geth/rlp"
|
2018-05-07 11:35:06 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// ReadDatabaseVersion retrieves the version number of the database.
|
2019-05-27 13:51:49 +00:00
|
|
|
func ReadDatabaseVersion(db DatabaseReader) *uint64 {
|
2019-01-11 11:49:12 +00:00
|
|
|
var version uint64
|
2018-05-07 11:35:06 +00:00
|
|
|
|
2020-08-10 23:55:32 +00:00
|
|
|
enc, _ := db.Get(dbutils.DatabaseVerisionKey, []byte(dbutils.DatabaseVerisionKey))
|
2019-01-11 11:49:12 +00:00
|
|
|
if len(enc) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if err := rlp.DecodeBytes(enc, &version); err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
2018-05-07 11:35:06 +00:00
|
|
|
|
2019-01-11 11:49:12 +00:00
|
|
|
return &version
|
2018-05-07 11:35:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// WriteDatabaseVersion stores the version number of the database
|
2020-10-24 06:57:09 +00:00
|
|
|
func WriteDatabaseVersion(db DatabaseWriter, version uint64) error {
|
2019-01-11 11:49:12 +00:00
|
|
|
enc, err := rlp.EncodeToBytes(version)
|
|
|
|
if err != nil {
|
2020-10-24 06:57:09 +00:00
|
|
|
return fmt.Errorf("failed to encode database version: %w", err)
|
2019-01-11 11:49:12 +00:00
|
|
|
}
|
2020-08-10 23:55:32 +00:00
|
|
|
if err = db.Put(dbutils.DatabaseVerisionKey, []byte(dbutils.DatabaseVerisionKey), enc); err != nil {
|
2020-10-24 06:57:09 +00:00
|
|
|
return fmt.Errorf("failed to store the database version: %w", err)
|
2018-05-07 11:35:06 +00:00
|
|
|
}
|
2020-10-24 06:57:09 +00:00
|
|
|
return nil
|
2018-05-07 11:35:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ReadChainConfig retrieves the consensus settings based on the given genesis hash.
|
2020-10-24 06:57:09 +00:00
|
|
|
func ReadChainConfig(db DatabaseReader, hash common.Hash) (*params.ChainConfig, error) {
|
|
|
|
data, err := db.Get(dbutils.ConfigPrefix, hash[:])
|
|
|
|
if err != nil && errors.Is(err, ethdb.ErrKeyNotFound) {
|
|
|
|
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.
|
2020-10-24 06:57:09 +00:00
|
|
|
func WriteChainConfig(db DatabaseWriter, 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
|
|
|
}
|
2020-10-24 06:57:09 +00:00
|
|
|
return nil
|
2018-05-07 11:35:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ReadPreimage retrieves a single preimage of the provided hash.
|
2019-05-27 13:51:49 +00:00
|
|
|
func ReadPreimage(db DatabaseReader, hash common.Hash) []byte {
|
|
|
|
data, _ := db.Get(dbutils.PreimagePrefix, hash.Bytes())
|
2018-05-07 11:35:06 +00:00
|
|
|
return data
|
|
|
|
}
|
|
|
|
|
2018-11-09 10:51:07 +00:00
|
|
|
// WritePreimages writes the provided set of preimages to the database.
|
2019-05-27 13:51:49 +00:00
|
|
|
func WritePreimages(db DatabaseWriter, preimages map[common.Hash][]byte) {
|
2018-05-07 11:35:06 +00:00
|
|
|
for hash, preimage := range preimages {
|
2019-05-27 13:51:49 +00:00
|
|
|
if err := db.Put(dbutils.PreimagePrefix, hash.Bytes(), preimage); err != nil {
|
2018-05-07 11:35:06 +00:00
|
|
|
log.Crit("Failed to store trie preimage", "err", err)
|
|
|
|
}
|
|
|
|
}
|
2019-05-27 13:51:49 +00:00
|
|
|
dbutils.PreimageCounter.Inc(int64(len(preimages)))
|
|
|
|
dbutils.PreimageHitCounter.Inc(int64(len(preimages)))
|
2018-05-07 11:35:06 +00:00
|
|
|
}
|