erigon-pulse/core/state/database_writer.go
Igor Mandrigin 468bd65109
Add "execute blocks" stage to the staged sync. (#486)
* extract functions to different files

* post-rebase fixups

* stage v0

* add a log

* better log entries

* fixes in log messages

* fix some stuff

* review fixes

* fix linters

* pw as a variable

* fix a test

* add Byzantium check

* batch save progress too
2020-04-26 17:02:38 +01:00

45 lines
1.2 KiB
Go

package state
import (
"github.com/ledgerwatch/turbo-geth/common"
"github.com/ledgerwatch/turbo-geth/common/dbutils"
"github.com/ledgerwatch/turbo-geth/ethdb"
)
type PreimageWriter struct {
db ethdb.GetterPutter
savePreimages bool
}
func (pw *PreimageWriter) SetSavePreimages(save bool) {
pw.savePreimages = save
}
func (pw *PreimageWriter) HashAddress(address common.Address, save bool) (common.Hash, error) {
addrHash, err := common.HashData(address[:])
if err != nil {
return common.Hash{}, err
}
return addrHash, pw.savePreimage(save, addrHash[:], address[:])
}
func (pw *PreimageWriter) HashKey(key *common.Hash, save bool) (common.Hash, error) {
keyHash, err := common.HashData(key[:])
if err != nil {
return common.Hash{}, err
}
return keyHash, pw.savePreimage(save, keyHash[:], key[:])
}
func (pw *PreimageWriter) savePreimage(save bool, hash []byte, preimage []byte) error {
if !save || !pw.savePreimages {
return nil
}
// Following check is to minimise the overwriting the same value of preimage
// in the database, which would cause extra write churn
if p, _ := pw.db.Get(dbutils.PreimagePrefix, hash); p != nil {
return nil
}
return pw.db.Put(dbutils.PreimagePrefix, hash, preimage)
}