2020-06-11 06:19:15 +00:00
|
|
|
package state
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/binary"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
|
2020-12-04 21:16:51 +00:00
|
|
|
"github.com/RoaringBitmap/roaring/roaring64"
|
2020-06-11 06:19:15 +00:00
|
|
|
"github.com/ledgerwatch/turbo-geth/common"
|
|
|
|
"github.com/ledgerwatch/turbo-geth/common/changeset"
|
|
|
|
"github.com/ledgerwatch/turbo-geth/common/dbutils"
|
|
|
|
"github.com/ledgerwatch/turbo-geth/core/types/accounts"
|
|
|
|
"github.com/ledgerwatch/turbo-geth/ethdb"
|
2020-12-04 21:16:51 +00:00
|
|
|
"github.com/ledgerwatch/turbo-geth/ethdb/bitmapdb"
|
2020-06-11 06:19:15 +00:00
|
|
|
)
|
|
|
|
|
2020-07-09 06:13:45 +00:00
|
|
|
//MaxChangesetsSearch -
|
|
|
|
const MaxChangesetsSearch = 256
|
|
|
|
|
2020-10-12 08:39:04 +00:00
|
|
|
func GetAsOf(tx ethdb.Tx, storage bool, key []byte, timestamp uint64) ([]byte, error) {
|
2020-06-11 06:19:15 +00:00
|
|
|
var dat []byte
|
2020-10-12 08:39:04 +00:00
|
|
|
v, err := FindByHistory(tx, storage, key, timestamp)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if v == nil {
|
2021-04-21 03:47:46 +00:00
|
|
|
v, err = tx.GetOne(dbutils.PlainStateBucket, key)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if v == nil {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
dat = make([]byte, len(v))
|
|
|
|
copy(dat, v)
|
|
|
|
return dat, nil
|
2020-10-12 08:39:04 +00:00
|
|
|
}
|
2021-04-21 03:47:46 +00:00
|
|
|
|
2020-10-12 08:39:04 +00:00
|
|
|
dat = make([]byte, len(v))
|
|
|
|
copy(dat, v)
|
|
|
|
return dat, nil
|
2020-06-11 06:19:15 +00:00
|
|
|
}
|
|
|
|
|
2020-08-24 17:00:40 +00:00
|
|
|
func FindByHistory(tx ethdb.Tx, storage bool, key []byte, timestamp uint64) ([]byte, error) {
|
2020-08-10 23:55:32 +00:00
|
|
|
var hBucket string
|
2020-06-11 06:19:15 +00:00
|
|
|
if storage {
|
|
|
|
hBucket = dbutils.StorageHistoryBucket
|
|
|
|
} else {
|
|
|
|
hBucket = dbutils.AccountsHistoryBucket
|
|
|
|
}
|
2020-12-04 21:16:51 +00:00
|
|
|
|
2021-04-02 06:36:49 +00:00
|
|
|
ch, err := tx.Cursor(hBucket)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-12-04 21:16:51 +00:00
|
|
|
defer ch.Close()
|
|
|
|
k, v, seekErr := ch.Seek(dbutils.IndexChunkKey(key, timestamp))
|
2020-08-14 06:41:18 +00:00
|
|
|
if seekErr != nil {
|
|
|
|
return nil, seekErr
|
2020-06-11 06:19:15 +00:00
|
|
|
}
|
2020-12-04 21:16:51 +00:00
|
|
|
|
2020-06-15 21:24:08 +00:00
|
|
|
if k == nil {
|
2021-04-21 03:47:46 +00:00
|
|
|
return nil, nil
|
2020-06-15 21:24:08 +00:00
|
|
|
}
|
2020-06-11 06:19:15 +00:00
|
|
|
if storage {
|
2020-08-24 17:00:40 +00:00
|
|
|
if !bytes.Equal(k[:common.AddressLength], key[:common.AddressLength]) ||
|
|
|
|
!bytes.Equal(k[common.AddressLength:common.AddressLength+common.HashLength], key[common.AddressLength+common.IncarnationLength:]) {
|
2021-04-21 03:47:46 +00:00
|
|
|
return nil, nil
|
2020-06-11 06:19:15 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if !bytes.HasPrefix(k, key) {
|
2021-04-21 03:47:46 +00:00
|
|
|
return nil, nil
|
2020-06-11 06:19:15 +00:00
|
|
|
}
|
|
|
|
}
|
2020-12-04 21:16:51 +00:00
|
|
|
index := roaring64.New()
|
|
|
|
if _, err := index.ReadFrom(bytes.NewReader(v)); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
found, ok := bitmapdb.SeekInBitmap64(index, timestamp)
|
|
|
|
changeSetBlock := found
|
2020-06-11 06:19:15 +00:00
|
|
|
|
|
|
|
var data []byte
|
|
|
|
if ok {
|
2021-02-09 10:26:00 +00:00
|
|
|
csBucket := dbutils.ChangeSetByIndexBucket(storage)
|
2021-04-02 06:36:49 +00:00
|
|
|
c, err := tx.CursorDupSort(csBucket)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-11-16 12:08:28 +00:00
|
|
|
defer c.Close()
|
2020-08-24 17:00:40 +00:00
|
|
|
if storage {
|
2020-11-16 12:08:28 +00:00
|
|
|
data, err = changeset.Mapper[csBucket].WalkerAdapter(c).(changeset.StorageChangeSetPlain).FindWithIncarnation(changeSetBlock, key)
|
2020-06-11 06:19:15 +00:00
|
|
|
} else {
|
2020-11-16 12:08:28 +00:00
|
|
|
data, err = changeset.Mapper[csBucket].WalkerAdapter(c).Find(changeSetBlock, key)
|
2020-06-11 06:19:15 +00:00
|
|
|
}
|
|
|
|
if err != nil {
|
2020-07-06 06:34:24 +00:00
|
|
|
if !errors.Is(err, changeset.ErrNotFound) {
|
2020-06-11 06:19:15 +00:00
|
|
|
return nil, fmt.Errorf("finding %x in the changeset %d: %w", key, changeSetBlock, err)
|
|
|
|
}
|
2021-04-21 03:47:46 +00:00
|
|
|
return nil, nil
|
2020-06-11 06:19:15 +00:00
|
|
|
}
|
2020-08-24 17:00:40 +00:00
|
|
|
} else {
|
2021-04-21 03:47:46 +00:00
|
|
|
return nil, nil
|
2020-06-11 06:19:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//restore codehash
|
|
|
|
if !storage {
|
|
|
|
var acc accounts.Account
|
|
|
|
if err := acc.DecodeForStorage(data); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if acc.Incarnation > 0 && acc.IsEmptyCodeHash() {
|
|
|
|
var codeHash []byte
|
2020-08-14 06:41:18 +00:00
|
|
|
var err error
|
2020-10-24 08:09:20 +00:00
|
|
|
codeHash, err = tx.GetOne(dbutils.PlainContractCodeBucket, dbutils.PlainGenerateStoragePrefix(key, acc.Incarnation))
|
2020-08-24 17:00:40 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2020-06-11 06:19:15 +00:00
|
|
|
}
|
|
|
|
if len(codeHash) > 0 {
|
|
|
|
acc.CodeHash = common.BytesToHash(codeHash)
|
|
|
|
}
|
|
|
|
data = make([]byte, acc.EncodingLengthForStorage())
|
|
|
|
acc.EncodeForStorage(data)
|
|
|
|
}
|
|
|
|
return data, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return data, nil
|
|
|
|
}
|
2020-07-09 06:13:45 +00:00
|
|
|
|
2020-11-18 14:58:52 +00:00
|
|
|
// startKey is the concatenation of address and incarnation (BigEndian 8 byte)
|
|
|
|
func WalkAsOfStorage(tx ethdb.Tx, address common.Address, incarnation uint64, startLocation common.Hash, timestamp uint64, walker func(k1, k2, v []byte) (bool, error)) error {
|
|
|
|
var startkey = make([]byte, common.AddressLength+common.IncarnationLength+common.HashLength)
|
|
|
|
copy(startkey, address.Bytes())
|
|
|
|
binary.BigEndian.PutUint64(startkey[common.AddressLength:], incarnation)
|
|
|
|
copy(startkey[common.AddressLength+common.IncarnationLength:], startLocation.Bytes())
|
2020-07-09 06:13:45 +00:00
|
|
|
|
2020-11-18 14:58:52 +00:00
|
|
|
var startkeyNoInc = make([]byte, common.AddressLength+common.HashLength)
|
|
|
|
copy(startkeyNoInc, address.Bytes())
|
|
|
|
copy(startkeyNoInc[common.AddressLength:], startLocation.Bytes())
|
2020-10-12 08:39:04 +00:00
|
|
|
|
|
|
|
//for storage
|
2021-04-02 06:36:49 +00:00
|
|
|
mCursor, err := tx.Cursor(dbutils.PlainStateBucket)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-11-18 14:58:52 +00:00
|
|
|
defer mCursor.Close()
|
2020-10-12 08:39:04 +00:00
|
|
|
mainCursor := ethdb.NewSplitCursor(
|
2020-11-18 14:58:52 +00:00
|
|
|
mCursor,
|
2020-10-12 08:39:04 +00:00
|
|
|
startkey,
|
2020-11-18 14:58:52 +00:00
|
|
|
8*(common.AddressLength+common.IncarnationLength),
|
|
|
|
common.AddressLength, /* part1end */
|
|
|
|
common.AddressLength+common.IncarnationLength, /* part2start */
|
|
|
|
common.AddressLength+common.IncarnationLength+common.HashLength, /* part3start */
|
2020-10-12 08:39:04 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
//for historic data
|
2021-04-02 06:36:49 +00:00
|
|
|
shCursor, err := tx.Cursor(dbutils.StorageHistoryBucket)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-11-18 14:58:52 +00:00
|
|
|
defer shCursor.Close()
|
|
|
|
var hCursor = ethdb.NewSplitCursor(
|
|
|
|
shCursor,
|
2020-10-12 08:39:04 +00:00
|
|
|
startkeyNoInc,
|
2020-11-18 14:58:52 +00:00
|
|
|
8*common.AddressLength,
|
|
|
|
common.AddressLength, /* part1end */
|
|
|
|
common.AddressLength, /* part2start */
|
|
|
|
common.AddressLength+common.HashLength, /* part3start */
|
2020-10-12 08:39:04 +00:00
|
|
|
)
|
2021-04-02 06:36:49 +00:00
|
|
|
csCursor, err := tx.CursorDupSort(dbutils.PlainStorageChangeSetBucket)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-11-18 14:58:52 +00:00
|
|
|
defer csCursor.Close()
|
2020-10-12 08:39:04 +00:00
|
|
|
|
2020-11-18 14:58:52 +00:00
|
|
|
addr, loc, _, v, err1 := mainCursor.Seek()
|
2020-10-12 08:39:04 +00:00
|
|
|
if err1 != nil {
|
|
|
|
return err1
|
|
|
|
}
|
2020-07-09 06:13:45 +00:00
|
|
|
|
2020-11-18 14:58:52 +00:00
|
|
|
hAddr, hLoc, tsEnc, hV, err2 := hCursor.Seek()
|
|
|
|
if err2 != nil {
|
2020-10-12 08:39:04 +00:00
|
|
|
return err2
|
|
|
|
}
|
2020-11-18 14:58:52 +00:00
|
|
|
for hLoc != nil && binary.BigEndian.Uint64(tsEnc) < timestamp {
|
|
|
|
if hAddr, hLoc, tsEnc, hV, err2 = hCursor.Next(); err2 != nil {
|
|
|
|
return err2
|
|
|
|
}
|
|
|
|
}
|
2020-10-12 08:39:04 +00:00
|
|
|
goOn := true
|
|
|
|
for goOn {
|
2020-11-18 14:58:52 +00:00
|
|
|
cmp, br := common.KeyCmp(addr, hAddr)
|
2020-10-12 08:39:04 +00:00
|
|
|
if br {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
if cmp == 0 {
|
2020-11-18 14:58:52 +00:00
|
|
|
cmp, br = common.KeyCmp(loc, hLoc)
|
2020-10-12 08:39:04 +00:00
|
|
|
}
|
|
|
|
if br {
|
|
|
|
break
|
2020-07-09 06:13:45 +00:00
|
|
|
}
|
|
|
|
|
2020-10-12 08:39:04 +00:00
|
|
|
//next key in state
|
|
|
|
if cmp < 0 {
|
2020-11-18 14:58:52 +00:00
|
|
|
goOn, err = walker(addr, loc, v)
|
2020-10-12 08:39:04 +00:00
|
|
|
} else {
|
2020-12-04 21:16:51 +00:00
|
|
|
index := roaring64.New()
|
|
|
|
if _, err = index.ReadFrom(bytes.NewReader(hV)); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
found, ok := bitmapdb.SeekInBitmap64(index, timestamp)
|
|
|
|
changeSetBlock := found
|
|
|
|
|
|
|
|
if ok {
|
|
|
|
// Extract value from the changeSet
|
|
|
|
csKey := make([]byte, 8+common.AddressLength+common.IncarnationLength)
|
|
|
|
copy(csKey[:], dbutils.EncodeBlockNumber(changeSetBlock))
|
|
|
|
copy(csKey[8:], address[:]) // address + incarnation
|
|
|
|
binary.BigEndian.PutUint64(csKey[8+common.AddressLength:], incarnation)
|
2021-03-19 07:45:01 +00:00
|
|
|
kData := csKey
|
|
|
|
data, err3 := csCursor.SeekBothRange(csKey, hLoc)
|
2020-12-04 21:16:51 +00:00
|
|
|
if err3 != nil {
|
|
|
|
return err3
|
|
|
|
}
|
|
|
|
if !bytes.Equal(kData, csKey) || !bytes.HasPrefix(data, hLoc) {
|
|
|
|
return fmt.Errorf("inconsistent storage changeset and history kData %x, csKey %x, data %x, hLoc %x", kData, csKey, data, hLoc)
|
|
|
|
}
|
|
|
|
data = data[common.HashLength:]
|
|
|
|
if len(data) > 0 { // Skip deleted entries
|
|
|
|
goOn, err = walker(hAddr, hLoc, data)
|
2020-11-18 14:58:52 +00:00
|
|
|
}
|
|
|
|
} else if cmp == 0 {
|
|
|
|
goOn, err = walker(addr, loc, v)
|
2020-07-09 06:13:45 +00:00
|
|
|
}
|
2020-10-12 08:39:04 +00:00
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if goOn {
|
|
|
|
if cmp <= 0 {
|
2020-11-18 14:58:52 +00:00
|
|
|
if addr, loc, _, v, err1 = mainCursor.Next(); err1 != nil {
|
2020-10-12 08:39:04 +00:00
|
|
|
return err1
|
|
|
|
}
|
2020-07-09 06:13:45 +00:00
|
|
|
}
|
2020-10-12 08:39:04 +00:00
|
|
|
if cmp >= 0 {
|
2020-11-18 14:58:52 +00:00
|
|
|
hLoc0 := hLoc
|
|
|
|
for hLoc != nil && (bytes.Equal(hLoc0, hLoc) || binary.BigEndian.Uint64(tsEnc) < timestamp) {
|
|
|
|
if hAddr, hLoc, tsEnc, hV, err2 = hCursor.Next(); err2 != nil {
|
|
|
|
return err2
|
|
|
|
}
|
2020-07-09 06:13:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-10-12 08:39:04 +00:00
|
|
|
}
|
2020-11-18 14:58:52 +00:00
|
|
|
return nil
|
2020-07-09 06:13:45 +00:00
|
|
|
}
|
|
|
|
|
2020-11-18 14:58:52 +00:00
|
|
|
func WalkAsOfAccounts(tx ethdb.Tx, startAddress common.Address, timestamp uint64, walker func(k []byte, v []byte) (bool, error)) error {
|
2021-04-02 06:36:49 +00:00
|
|
|
mainCursor, err := tx.Cursor(dbutils.PlainStateBucket)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-11-18 14:58:52 +00:00
|
|
|
defer mainCursor.Close()
|
2021-04-02 06:36:49 +00:00
|
|
|
ahCursor, err := tx.Cursor(dbutils.AccountsHistoryBucket)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-11-18 14:58:52 +00:00
|
|
|
defer ahCursor.Close()
|
|
|
|
var hCursor = ethdb.NewSplitCursor(
|
|
|
|
ahCursor,
|
|
|
|
startAddress.Bytes(),
|
|
|
|
0, /* fixedBits */
|
|
|
|
common.AddressLength, /* part1end */
|
|
|
|
common.AddressLength, /* part2start */
|
|
|
|
common.AddressLength+8, /* part3start */
|
2020-10-12 08:39:04 +00:00
|
|
|
)
|
2021-04-02 06:36:49 +00:00
|
|
|
csCursor, err := tx.CursorDupSort(dbutils.PlainAccountChangeSetBucket)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-11-18 14:58:52 +00:00
|
|
|
defer csCursor.Close()
|
2020-07-09 06:13:45 +00:00
|
|
|
|
2020-11-18 14:58:52 +00:00
|
|
|
k, v, err1 := mainCursor.Seek(startAddress.Bytes())
|
2020-10-12 08:39:04 +00:00
|
|
|
if err1 != nil {
|
|
|
|
return err1
|
|
|
|
}
|
2020-11-18 14:58:52 +00:00
|
|
|
for k != nil && len(k) > common.AddressLength {
|
2020-10-12 08:39:04 +00:00
|
|
|
k, v, err1 = mainCursor.Next()
|
2020-07-09 06:13:45 +00:00
|
|
|
if err1 != nil {
|
|
|
|
return err1
|
|
|
|
}
|
2020-10-12 08:39:04 +00:00
|
|
|
}
|
2020-11-18 14:58:52 +00:00
|
|
|
hK, tsEnc, _, hV, err2 := hCursor.Seek()
|
|
|
|
if err2 != nil {
|
2020-10-12 08:39:04 +00:00
|
|
|
return err2
|
|
|
|
}
|
2020-11-18 14:58:52 +00:00
|
|
|
for hK != nil && binary.BigEndian.Uint64(tsEnc) < timestamp {
|
|
|
|
hK, tsEnc, _, hV, err2 = hCursor.Next()
|
|
|
|
if err2 != nil {
|
|
|
|
return err2
|
|
|
|
}
|
|
|
|
}
|
2020-10-12 08:39:04 +00:00
|
|
|
|
|
|
|
goOn := true
|
|
|
|
for goOn {
|
|
|
|
//exit or next conditions
|
|
|
|
cmp, br := common.KeyCmp(k, hK)
|
|
|
|
if br {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
if cmp < 0 {
|
|
|
|
goOn, err = walker(k, v)
|
|
|
|
} else {
|
2020-12-04 21:16:51 +00:00
|
|
|
index := roaring64.New()
|
|
|
|
_, err = index.ReadFrom(bytes.NewReader(hV))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
found, ok := bitmapdb.SeekInBitmap64(index, timestamp)
|
|
|
|
changeSetBlock := found
|
|
|
|
if ok {
|
|
|
|
// Extract value from the changeSet
|
|
|
|
csKey := dbutils.EncodeBlockNumber(changeSetBlock)
|
2021-03-19 07:45:01 +00:00
|
|
|
kData := csKey
|
|
|
|
data, err3 := csCursor.SeekBothRange(csKey, hK)
|
2020-12-04 21:16:51 +00:00
|
|
|
if err3 != nil {
|
|
|
|
return err3
|
|
|
|
}
|
|
|
|
if !bytes.Equal(kData, csKey) || !bytes.HasPrefix(data, hK) {
|
|
|
|
return fmt.Errorf("inconsistent account history and changesets, kData %x, csKey %x, data %x, hK %x", kData, csKey, data, hK)
|
|
|
|
}
|
|
|
|
data = data[common.AddressLength:]
|
|
|
|
if len(data) > 0 { // Skip accounts did not exist
|
|
|
|
goOn, err = walker(hK, data)
|
2020-11-18 14:58:52 +00:00
|
|
|
}
|
|
|
|
} else if cmp == 0 {
|
2020-07-09 06:13:45 +00:00
|
|
|
goOn, err = walker(k, v)
|
|
|
|
}
|
2020-10-12 08:39:04 +00:00
|
|
|
}
|
2020-11-18 14:58:52 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-10-12 08:39:04 +00:00
|
|
|
if goOn {
|
|
|
|
if cmp <= 0 {
|
|
|
|
k, v, err1 = mainCursor.Next()
|
|
|
|
if err1 != nil {
|
|
|
|
return err1
|
|
|
|
}
|
2020-11-18 14:58:52 +00:00
|
|
|
for k != nil && len(k) > common.AddressLength {
|
2020-07-09 06:13:45 +00:00
|
|
|
k, v, err1 = mainCursor.Next()
|
|
|
|
if err1 != nil {
|
|
|
|
return err1
|
|
|
|
}
|
|
|
|
}
|
2020-10-12 08:39:04 +00:00
|
|
|
}
|
|
|
|
if cmp >= 0 {
|
2020-11-18 14:58:52 +00:00
|
|
|
hK0 := hK
|
|
|
|
for hK != nil && (bytes.Equal(hK0, hK) || binary.BigEndian.Uint64(tsEnc) < timestamp) {
|
|
|
|
hK, tsEnc, _, hV, err1 = hCursor.Next()
|
|
|
|
if err1 != nil {
|
|
|
|
return err1
|
|
|
|
}
|
2020-07-09 06:13:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-10-12 08:39:04 +00:00
|
|
|
}
|
2020-07-09 06:13:45 +00:00
|
|
|
return err
|
2020-10-12 08:39:04 +00:00
|
|
|
|
2020-07-09 06:13:45 +00:00
|
|
|
}
|