2019-12-20 12:25:40 +00:00
|
|
|
package state
|
|
|
|
|
|
|
|
import (
|
2020-11-28 14:24:47 +00:00
|
|
|
"bytes"
|
2020-05-22 15:11:00 +00:00
|
|
|
"encoding/binary"
|
2020-04-09 17:23:29 +00:00
|
|
|
"fmt"
|
2020-03-11 10:31:49 +00:00
|
|
|
|
2020-12-04 21:16:51 +00:00
|
|
|
"github.com/RoaringBitmap/roaring/roaring64"
|
2020-05-25 11:12:25 +00:00
|
|
|
"github.com/holiman/uint256"
|
2021-05-20 18:25:53 +00:00
|
|
|
"github.com/ledgerwatch/erigon/common"
|
|
|
|
"github.com/ledgerwatch/erigon/common/changeset"
|
|
|
|
"github.com/ledgerwatch/erigon/common/dbutils"
|
|
|
|
"github.com/ledgerwatch/erigon/common/math"
|
|
|
|
"github.com/ledgerwatch/erigon/core/types/accounts"
|
|
|
|
"github.com/ledgerwatch/erigon/ethdb"
|
|
|
|
"github.com/ledgerwatch/erigon/ethdb/bitmapdb"
|
2021-07-28 02:47:38 +00:00
|
|
|
"github.com/ledgerwatch/erigon/ethdb/kv"
|
2021-05-20 18:25:53 +00:00
|
|
|
"github.com/ledgerwatch/erigon/turbo/trie"
|
2019-12-20 12:25:40 +00:00
|
|
|
)
|
|
|
|
|
2020-07-15 12:26:59 +00:00
|
|
|
// This type is now used in GenerateChain to generate blockchains for the tests (core/chain_makers.go)
|
|
|
|
// Main mode of operation uses PlainDbStateWriter
|
|
|
|
|
2020-05-15 07:52:45 +00:00
|
|
|
var _ WriterWithChangeSets = (*DbStateWriter)(nil)
|
|
|
|
|
2021-07-24 07:14:11 +00:00
|
|
|
func NewDbStateWriter(db putDel, blockNr uint64) *DbStateWriter {
|
2020-04-26 16:02:38 +00:00
|
|
|
return &DbStateWriter{
|
2020-06-09 13:11:09 +00:00
|
|
|
db: db,
|
|
|
|
blockNr: blockNr,
|
2021-01-20 07:16:20 +00:00
|
|
|
csw: NewChangeSetWriter(),
|
2020-04-26 16:02:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-20 12:25:40 +00:00
|
|
|
type DbStateWriter struct {
|
2021-07-24 07:14:11 +00:00
|
|
|
db putDel
|
2020-12-08 09:44:29 +00:00
|
|
|
blockNr uint64
|
|
|
|
csw *ChangeSetWriter
|
2020-05-21 12:27:52 +00:00
|
|
|
}
|
|
|
|
|
2020-07-15 12:26:59 +00:00
|
|
|
func (dsw *DbStateWriter) ChangeSetWriter() *ChangeSetWriter {
|
|
|
|
return dsw.csw
|
|
|
|
}
|
|
|
|
|
2020-03-26 21:52:22 +00:00
|
|
|
func originalAccountData(original *accounts.Account, omitHashes bool) []byte {
|
|
|
|
var originalData []byte
|
|
|
|
if !original.Initialised {
|
|
|
|
originalData = []byte{}
|
|
|
|
} else if omitHashes {
|
|
|
|
testAcc := original.SelfCopy()
|
|
|
|
copy(testAcc.CodeHash[:], emptyCodeHash)
|
|
|
|
testAcc.Root = trie.EmptyRoot
|
|
|
|
originalDataLen := testAcc.EncodingLengthForStorage()
|
|
|
|
originalData = make([]byte, originalDataLen)
|
|
|
|
testAcc.EncodeForStorage(originalData)
|
|
|
|
} else {
|
|
|
|
originalDataLen := original.EncodingLengthForStorage()
|
|
|
|
originalData = make([]byte, originalDataLen)
|
|
|
|
original.EncodeForStorage(originalData)
|
|
|
|
}
|
|
|
|
return originalData
|
|
|
|
}
|
|
|
|
|
2021-07-05 08:42:44 +00:00
|
|
|
func (dsw *DbStateWriter) UpdateAccountData(address common.Address, original, account *accounts.Account) error {
|
|
|
|
if err := dsw.csw.UpdateAccountData(address, original, account); err != nil {
|
2020-04-09 17:23:29 +00:00
|
|
|
return err
|
|
|
|
}
|
2021-06-20 06:00:22 +00:00
|
|
|
addrHash, err := common.HashData(address[:])
|
2019-12-20 12:25:40 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-05-22 15:11:00 +00:00
|
|
|
value := make([]byte, account.EncodingLengthForStorage())
|
|
|
|
account.EncodeForStorage(value)
|
2021-07-28 02:47:38 +00:00
|
|
|
if err := dsw.db.Put(kv.HashedAccounts, addrHash[:], value); err != nil {
|
2020-04-18 20:09:44 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
2019-12-20 12:25:40 +00:00
|
|
|
}
|
|
|
|
|
2021-07-05 08:42:44 +00:00
|
|
|
func (dsw *DbStateWriter) DeleteAccount(address common.Address, original *accounts.Account) error {
|
|
|
|
if err := dsw.csw.DeleteAccount(address, original); err != nil {
|
2019-12-20 12:25:40 +00:00
|
|
|
return err
|
|
|
|
}
|
2021-06-20 06:00:22 +00:00
|
|
|
addrHash, err := common.HashData(address[:])
|
2020-04-09 17:23:29 +00:00
|
|
|
if err != nil {
|
2019-12-20 12:25:40 +00:00
|
|
|
return err
|
|
|
|
}
|
2021-07-28 02:47:38 +00:00
|
|
|
if err := dsw.db.Delete(kv.HashedAccounts, addrHash[:], nil); err != nil {
|
2020-04-18 20:09:44 +00:00
|
|
|
return err
|
|
|
|
}
|
2020-05-08 04:52:55 +00:00
|
|
|
if original.Incarnation > 0 {
|
2020-05-26 12:27:21 +00:00
|
|
|
var b [8]byte
|
|
|
|
binary.BigEndian.PutUint64(b[:], original.Incarnation)
|
2021-07-28 02:47:38 +00:00
|
|
|
if err := dsw.db.Put(kv.IncarnationMap, address[:], b[:]); err != nil {
|
2020-05-26 12:27:21 +00:00
|
|
|
return err
|
|
|
|
}
|
2020-05-08 04:52:55 +00:00
|
|
|
}
|
2020-04-18 20:09:44 +00:00
|
|
|
return nil
|
2019-12-20 12:25:40 +00:00
|
|
|
}
|
|
|
|
|
2020-05-15 07:52:45 +00:00
|
|
|
func (dsw *DbStateWriter) UpdateAccountCode(address common.Address, incarnation uint64, codeHash common.Hash, code []byte) error {
|
|
|
|
if err := dsw.csw.UpdateAccountCode(address, incarnation, codeHash, code); err != nil {
|
2020-04-09 17:23:29 +00:00
|
|
|
return err
|
|
|
|
}
|
2019-12-20 12:25:40 +00:00
|
|
|
//save contract code mapping
|
2021-07-28 03:43:51 +00:00
|
|
|
if err := dsw.db.Put(kv.Code, codeHash[:], code); err != nil {
|
2019-12-20 12:25:40 +00:00
|
|
|
return err
|
|
|
|
}
|
2020-05-15 07:52:45 +00:00
|
|
|
addrHash, err := common.HashData(address.Bytes())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-04-15 09:33:22 +00:00
|
|
|
//save contract to codeHash mapping
|
2021-07-28 02:47:38 +00:00
|
|
|
if err := dsw.db.Put(kv.ContractCode, dbutils.GenerateStoragePrefix(addrHash[:], incarnation), codeHash[:]); err != nil {
|
2020-05-21 12:27:52 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
2019-12-20 12:25:40 +00:00
|
|
|
}
|
|
|
|
|
2021-07-05 08:42:44 +00:00
|
|
|
func (dsw *DbStateWriter) WriteAccountStorage(address common.Address, incarnation uint64, key *common.Hash, original, value *uint256.Int) error {
|
2020-04-09 17:23:29 +00:00
|
|
|
// We delegate here first to let the changeSetWrite make its own decision on whether to proceed in case *original == *value
|
2021-07-05 08:42:44 +00:00
|
|
|
if err := dsw.csw.WriteAccountStorage(address, incarnation, key, original, value); err != nil {
|
2020-04-09 17:23:29 +00:00
|
|
|
return err
|
|
|
|
}
|
2019-12-20 12:25:40 +00:00
|
|
|
if *original == *value {
|
|
|
|
return nil
|
|
|
|
}
|
2021-06-20 06:00:22 +00:00
|
|
|
seckey, err := common.HashData(key[:])
|
2019-12-20 12:25:40 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-06-20 06:00:22 +00:00
|
|
|
addrHash, err := common.HashData(address[:])
|
2019-12-20 12:25:40 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
compositeKey := dbutils.GenerateCompositeStorageKey(addrHash, incarnation, seckey)
|
2020-05-15 07:52:45 +00:00
|
|
|
|
2020-05-25 11:12:25 +00:00
|
|
|
v := value.Bytes()
|
2019-12-20 12:25:40 +00:00
|
|
|
if len(v) == 0 {
|
2021-07-28 02:47:38 +00:00
|
|
|
return dsw.db.Delete(kv.HashedStorage, compositeKey, nil)
|
2020-04-09 17:23:29 +00:00
|
|
|
}
|
2021-07-28 02:47:38 +00:00
|
|
|
return dsw.db.Put(kv.HashedStorage, compositeKey, v)
|
2020-04-09 17:23:29 +00:00
|
|
|
}
|
2020-03-11 10:31:49 +00:00
|
|
|
|
2020-05-02 18:00:42 +00:00
|
|
|
func (dsw *DbStateWriter) CreateContract(address common.Address) error {
|
2020-05-26 12:27:21 +00:00
|
|
|
if err := dsw.csw.CreateContract(address); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
2020-04-09 17:23:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// WriteChangeSets causes accumulated change sets to be written into
|
|
|
|
// the database (or batch) associated with the `dsw`
|
|
|
|
func (dsw *DbStateWriter) WriteChangeSets() error {
|
|
|
|
return nil
|
2019-12-20 12:25:40 +00:00
|
|
|
}
|
|
|
|
|
2020-04-09 17:23:29 +00:00
|
|
|
func (dsw *DbStateWriter) WriteHistory() error {
|
|
|
|
accountChanges, err := dsw.csw.GetAccountChanges()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-07-28 02:47:38 +00:00
|
|
|
err = writeIndex(dsw.blockNr, accountChanges, kv.AccountsHistory, dsw.db.(ethdb.HasTx).Tx().(kv.RwTx))
|
2020-04-20 10:35:33 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2020-04-09 17:23:29 +00:00
|
|
|
}
|
2020-04-20 10:35:33 +00:00
|
|
|
|
2020-04-09 17:23:29 +00:00
|
|
|
storageChanges, err := dsw.csw.GetStorageChanges()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-07-28 02:47:38 +00:00
|
|
|
err = writeIndex(dsw.blockNr, storageChanges, kv.StorageHistory, dsw.db.(ethdb.HasTx).Tx().(kv.RwTx))
|
2020-04-20 10:35:33 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-07-28 02:47:38 +00:00
|
|
|
func writeIndex(blocknum uint64, changes *changeset.ChangeSet, bucket string, changeDb kv.RwTx) error {
|
2020-12-04 21:16:51 +00:00
|
|
|
buf := bytes.NewBuffer(nil)
|
2020-04-20 10:35:33 +00:00
|
|
|
for _, change := range changes.Changes {
|
2020-12-04 21:16:51 +00:00
|
|
|
k := dbutils.CompositeKeyWithoutIncarnation(change.Key)
|
|
|
|
|
|
|
|
index, err := bitmapdb.Get64(changeDb, bucket, k, 0, math.MaxUint32)
|
|
|
|
if err != nil {
|
2020-04-20 10:35:33 +00:00
|
|
|
return fmt.Errorf("find chunk failed: %w", err)
|
|
|
|
}
|
2020-12-04 21:16:51 +00:00
|
|
|
index.Add(blocknum)
|
|
|
|
if err = bitmapdb.WalkChunkWithKeys64(k, index, bitmapdb.ChunkLimit, func(chunkKey []byte, chunk *roaring64.Bitmap) error {
|
|
|
|
buf.Reset()
|
|
|
|
if _, err = chunk.WriteTo(buf); err != nil {
|
2020-04-21 17:43:57 +00:00
|
|
|
return err
|
|
|
|
}
|
2020-12-04 21:16:51 +00:00
|
|
|
return changeDb.Put(bucket, chunkKey, common.CopyBytes(buf.Bytes()))
|
|
|
|
}); err != nil {
|
2020-04-15 09:33:22 +00:00
|
|
|
return err
|
2020-04-09 17:23:29 +00:00
|
|
|
}
|
|
|
|
}
|
2020-04-20 10:35:33 +00:00
|
|
|
|
2019-12-20 12:25:40 +00:00
|
|
|
return nil
|
|
|
|
}
|