Nuke preimage remnants (#2203)

This commit is contained in:
Artem Vorotnikov 2021-06-20 09:00:22 +03:00 committed by GitHub
parent a535e8f30c
commit d8a009837b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 23 additions and 93 deletions

View File

@ -719,14 +719,6 @@ func invTree(wrong, right, diff string, name string) {
t1.PrintDiff(t2, c)
}
func preimage(chaindata string, image common.Hash) {
ethDb := kv2.MustOpen(chaindata)
defer ethDb.Close()
p, err := ethDb.Get(dbutils.PreimagePrefix, image[:])
tool.Check(err)
fmt.Printf("%x\n", p)
}
func printBranches(block uint64) {
//ethDb := ethdb.MustOpen("/home/akhounov/.ethereum/geth/chaindata")
ethDb := kv2.MustOpen(paths.DefaultDataDir() + "/testnet/geth/chaindata")
@ -1992,9 +1984,6 @@ func main() {
case "printBranches":
printBranches(uint64(*block))
case "preimage":
preimage(*chaindata, common.HexToHash(*hash))
case "printFullNodeRLPs":
printFullNodeRLPs()

View File

@ -11,6 +11,7 @@ import (
"github.com/ledgerwatch/erigon/common"
"github.com/ledgerwatch/erigon/common/dbutils"
"github.com/ledgerwatch/erigon/core/rawdb"
"github.com/ledgerwatch/erigon/core/state"
"github.com/ledgerwatch/erigon/core/types/accounts"
"github.com/ledgerwatch/erigon/ethdb"
"github.com/ledgerwatch/erigon/ethdb/kv"
@ -31,27 +32,26 @@ func FixState(chaindata string, url string) {
fmt.Printf("Block number: %d\n", blockNum)
fmt.Printf("Block root hash: %x\n", currentHeader.Root)
reqID := 0
roots := make(map[common.Hash]*accounts.Account)
roots := make(map[common.Address]*accounts.Account)
var client = &http.Client{
Timeout: time.Second * 600,
}
c, err := tx.Cursor(dbutils.HashedAccountsBucket)
c, err := tx.Cursor(dbutils.PlainStateBucket)
if err != nil {
panic(err)
}
defer c.Close()
if err := ethdb.ForEach(c, func(k, v []byte) (bool, error) {
var addrHash common.Hash
copy(addrHash[:], k[:32])
if _, ok := roots[addrHash]; !ok {
var account accounts.Account
if ok, err2 := rawdb.ReadAccount(tx, addrHash, &account); err2 != nil {
return false, err2
} else if !ok {
roots[addrHash] = nil
} else {
roots[addrHash] = &account
if len(k) == common.AddressLength {
var address common.Address
address.SetBytes(k)
if _, ok := roots[address]; !ok {
if account, err2 := state.NewPlainStateReader(tx).ReadAccountData(address); err2 != nil {
return false, err2
} else {
roots[address] = account
}
}
}
@ -59,9 +59,10 @@ func FixState(chaindata string, url string) {
}); err != nil {
panic(err)
}
for addrHash, account := range roots {
for address, account := range roots {
if account != nil && account.Root != trie.EmptyRoot {
contractPrefix := make([]byte, common.HashLength+common.IncarnationLength)
addrHash, _ := common.HashData(address.Bytes())
copy(contractPrefix, addrHash[:])
binary.BigEndian.PutUint64(contractPrefix[common.HashLength:], account.Incarnation)
rl := trie.NewRetainList(0)
@ -72,7 +73,6 @@ func FixState(chaindata string, url string) {
root, err1 := loader.CalcTrieRoot(tx, contractPrefix, nil)
if err1 != nil || root != account.Root {
fmt.Printf("%x: error %v, got hash %x, expected hash %x\n", addrHash, err1, root, account.Root)
address, _ := tx.GetOne(dbutils.PreimagePrefix, addrHash[:])
template := `{"jsonrpc":"2.0","method":"debug_storageRangeAt","params":["0x%x", %d,"0x%x","0x%x",%d],"id":%d}`
sm := make(map[common.Hash]storageEntry)
nextKey := &common.Hash{}

View File

@ -19,9 +19,9 @@ import (
func IndexStats(chaindata string, indexBucket string, statsFile string) error {
db := kv.MustOpen(chaindata)
startTime := time.Now()
lenOfKey := common.HashLength
lenOfKey := common.AddressLength
if strings.HasPrefix(indexBucket, dbutils.StorageHistoryBucket) {
lenOfKey = common.HashLength*2 + common.IncarnationLength
lenOfKey = common.AddressLength + common.HashLength + common.IncarnationLength
}
more1index := 0
@ -67,7 +67,7 @@ func IndexStats(chaindata string, indexBucket string, statsFile string) error {
} else {
added = false
count = 1
prevKey = common.CopyBytes(k[:common.HashLength])
prevKey = common.CopyBytes(k[:common.AddressLength])
}
return true, nil
@ -95,10 +95,7 @@ func IndexStats(chaindata string, indexBucket string, statsFile string) error {
NumOfIndexes uint64
}, 0, len(more10index))
for hash, v := range more10index {
p, innerErr := db.Get(dbutils.PreimagePrefix, []byte(hash)[:common.HashLength])
if innerErr != nil {
return innerErr
}
p := []byte(hash)[:common.AddressLength]
if len(p) == 0 {
p = make([]byte, 20)
}

View File

@ -232,8 +232,7 @@ const (
TxLookupPrefix = "BlockTransactionLookup" // hash -> transaction/receipt lookup metadata
BloomBitsPrefix = "BloomBits" // bit (uint16 big endian) + section (uint64 big endian) + hash -> bloom bits
PreimagePrefix = "Preimage" // preimagePrefix + hash -> preimage
ConfigPrefix = "Config" // config prefix for the db
ConfigPrefix = "Config" // config prefix for the db
// Chain index prefixes (use `i` + single byte to avoid mixing data types).
BloomBitsIndexPrefix = "BloomBitsIndex" // BloomBitsIndexPrefix is the data table of a chain indexer to track its progress
@ -304,7 +303,6 @@ var Buckets = []string{
BlockReceiptsPrefix,
TxLookupPrefix,
BloomBitsPrefix,
PreimagePrefix,
ConfigPrefix,
BloomBitsIndexPrefix,
DatabaseInfoBucket,

View File

@ -1,44 +0,0 @@
package state
import (
"github.com/ledgerwatch/erigon/common"
"github.com/ledgerwatch/erigon/common/dbutils"
"github.com/ledgerwatch/erigon/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)
}

View File

@ -27,14 +27,12 @@ func NewDbStateWriter(db ethdb.Database, blockNr uint64) *DbStateWriter {
return &DbStateWriter{
db: db,
blockNr: blockNr,
pw: &PreimageWriter{db: db, savePreimages: false},
csw: NewChangeSetWriter(),
}
}
type DbStateWriter struct {
db ethdb.Database
pw *PreimageWriter
blockNr uint64
csw *ChangeSetWriter
}
@ -66,7 +64,7 @@ func (dsw *DbStateWriter) UpdateAccountData(ctx context.Context, address common.
if err := dsw.csw.UpdateAccountData(ctx, address, original, account); err != nil {
return err
}
addrHash, err := dsw.pw.HashAddress(address, true /*save*/)
addrHash, err := common.HashData(address[:])
if err != nil {
return err
}
@ -82,7 +80,7 @@ func (dsw *DbStateWriter) DeleteAccount(ctx context.Context, address common.Addr
if err := dsw.csw.DeleteAccount(ctx, address, original); err != nil {
return err
}
addrHash, err := dsw.pw.HashAddress(address, true /*save*/)
addrHash, err := common.HashData(address[:])
if err != nil {
return err
}
@ -126,11 +124,11 @@ func (dsw *DbStateWriter) WriteAccountStorage(ctx context.Context, address commo
if *original == *value {
return nil
}
seckey, err := dsw.pw.HashKey(key, true /*save*/)
seckey, err := common.HashData(key[:])
if err != nil {
return err
}
addrHash, err := dsw.pw.HashAddress(address, false /*save*/)
addrHash, err := common.HashData(address[:])
if err != nil {
return err
}

View File

@ -759,14 +759,6 @@ func doModesTest(t *testing.T, history, receipts, txlookup bool) error {
numberOfEntries--
}
if bucketName == dbutils.PreimagePrefix {
// we will always have 2 preimages because core.GenerateChain interface does not
// allow us to set it to ignore them
// but if the preimages are enabled in BlockChain, we will have more than 2.
// TODO: with a better interface to core.GenerateChain allow to check preimages
numberOfEntries -= 2
}
if (shouldBeEmpty && numberOfEntries > 0) || (!shouldBeEmpty && numberOfEntries == 0) {
return fmt.Errorf("bucket '%s' should be empty? %v (actually %d entries)", bucketName, shouldBeEmpty, numberOfEntries)
}