2020-01-22 10:25:07 +00:00
|
|
|
package changeset
|
|
|
|
|
|
|
|
import (
|
2021-05-04 12:34:08 +00:00
|
|
|
"bytes"
|
|
|
|
"encoding/binary"
|
2020-01-22 10:25:07 +00:00
|
|
|
"errors"
|
2021-05-04 12:34:08 +00:00
|
|
|
"sort"
|
2020-05-02 18:00:57 +00:00
|
|
|
|
2021-05-20 18:25:53 +00:00
|
|
|
"github.com/ledgerwatch/erigon/common"
|
|
|
|
"github.com/ledgerwatch/erigon/common/dbutils"
|
|
|
|
"github.com/ledgerwatch/erigon/common/etl"
|
|
|
|
"github.com/ledgerwatch/erigon/ethdb"
|
2020-01-22 10:25:07 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2020-04-28 15:36:00 +00:00
|
|
|
DefaultIncarnation = uint64(1)
|
2020-01-22 10:25:07 +00:00
|
|
|
)
|
|
|
|
|
2020-05-04 05:55:37 +00:00
|
|
|
var (
|
2021-03-29 03:58:45 +00:00
|
|
|
ErrNotFound = errors.New("not found")
|
|
|
|
ErrFindValue = errors.New("find value error")
|
2020-05-04 05:55:37 +00:00
|
|
|
)
|
2020-02-26 22:36:34 +00:00
|
|
|
|
2021-05-04 12:34:08 +00:00
|
|
|
func NewStorageChangeSet() *ChangeSet {
|
2020-05-15 07:52:45 +00:00
|
|
|
return &ChangeSet{
|
|
|
|
Changes: make([]Change, 0),
|
|
|
|
keyLen: common.AddressLength + common.HashLength + common.IncarnationLength,
|
2020-04-28 07:41:55 +00:00
|
|
|
}
|
|
|
|
}
|
2020-02-26 22:36:34 +00:00
|
|
|
|
2021-05-04 12:34:08 +00:00
|
|
|
func EncodeStorage(blockN uint64, s *ChangeSet, f func(k, v []byte) error) error {
|
|
|
|
sort.Sort(s)
|
|
|
|
keyPart := common.AddressLength + common.IncarnationLength
|
|
|
|
for _, cs := range s.Changes {
|
|
|
|
newK := make([]byte, common.BlockNumberLength+keyPart)
|
|
|
|
binary.BigEndian.PutUint64(newK, blockN)
|
|
|
|
copy(newK[8:], cs.Key[:keyPart])
|
|
|
|
newV := make([]byte, 0, common.HashLength+len(cs.Value))
|
|
|
|
newV = append(append(newV, cs.Key[keyPart:]...), cs.Value...)
|
|
|
|
if err := f(newK, newV); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
2020-11-16 12:08:28 +00:00
|
|
|
}
|
|
|
|
|
2021-05-04 12:34:08 +00:00
|
|
|
func DecodeStorage(dbKey, dbValue []byte) (uint64, []byte, []byte) {
|
|
|
|
blockN := binary.BigEndian.Uint64(dbKey)
|
|
|
|
k := make([]byte, common.AddressLength+common.IncarnationLength+common.HashLength)
|
|
|
|
dbKey = dbKey[common.BlockNumberLength:] // remove BlockN bytes
|
|
|
|
copy(k, dbKey)
|
|
|
|
copy(k[len(dbKey):], dbValue[:common.HashLength])
|
|
|
|
v := dbValue[common.HashLength:]
|
|
|
|
if len(v) == 0 {
|
|
|
|
v = nil
|
|
|
|
}
|
2020-11-16 12:08:28 +00:00
|
|
|
|
2021-05-04 12:34:08 +00:00
|
|
|
return blockN, k, v
|
|
|
|
}
|
|
|
|
|
2021-07-04 07:49:31 +00:00
|
|
|
func FindStorage(c ethdb.CursorDupSort, blockNumber uint64, k []byte) ([]byte, error) {
|
|
|
|
addWithInc, loc := k[:common.AddressLength+common.IncarnationLength], k[common.AddressLength+common.IncarnationLength:]
|
|
|
|
seek := make([]byte, common.BlockNumberLength+common.AddressLength+common.IncarnationLength)
|
2021-05-04 12:34:08 +00:00
|
|
|
binary.BigEndian.PutUint64(seek, blockNumber)
|
2021-07-04 07:49:31 +00:00
|
|
|
copy(seek[8:], addWithInc)
|
|
|
|
v, err := c.SeekBothRange(seek, loc)
|
2021-05-04 12:34:08 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-07-04 07:49:31 +00:00
|
|
|
if !bytes.HasPrefix(v, loc) {
|
2021-05-04 12:34:08 +00:00
|
|
|
return nil, ErrNotFound
|
|
|
|
}
|
2021-07-04 07:49:31 +00:00
|
|
|
return v[common.HashLength:], nil
|
2021-05-04 12:34:08 +00:00
|
|
|
}
|
|
|
|
|
2021-02-21 18:41:59 +00:00
|
|
|
// RewindDataPlain generates rewind data for all plain buckets between the timestamp
|
2021-01-20 07:16:20 +00:00
|
|
|
// timestapSrc is the current timestamp, and timestamp Dst is where we rewind
|
2021-04-26 09:41:31 +00:00
|
|
|
func RewindData(db ethdb.Tx, timestampSrc, timestampDst uint64, tmpdir string, quit <-chan struct{}) (*etl.Collector, error) {
|
2021-01-20 07:16:20 +00:00
|
|
|
// Collect list of buckets and keys that need to be considered
|
2021-04-26 09:41:31 +00:00
|
|
|
|
|
|
|
changes := etl.NewCollector(tmpdir, etl.NewOldestEntryBuffer(etl.BufferOptimalSize))
|
2021-01-20 07:16:20 +00:00
|
|
|
|
2020-11-16 12:08:28 +00:00
|
|
|
if err := walkAndCollect(
|
2021-04-26 09:41:31 +00:00
|
|
|
changes.Collect,
|
2021-05-04 12:34:08 +00:00
|
|
|
db, dbutils.AccountChangeSetBucket,
|
2020-11-16 12:08:28 +00:00
|
|
|
timestampDst+1, timestampSrc,
|
2021-02-23 17:14:32 +00:00
|
|
|
quit,
|
2020-11-16 12:08:28 +00:00
|
|
|
); err != nil {
|
2021-04-26 09:41:31 +00:00
|
|
|
return nil, err
|
2020-11-16 12:08:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := walkAndCollect(
|
2021-04-26 09:41:31 +00:00
|
|
|
changes.Collect,
|
2021-05-04 12:34:08 +00:00
|
|
|
db, dbutils.StorageChangeSetBucket,
|
2020-11-16 12:08:28 +00:00
|
|
|
timestampDst+1, timestampSrc,
|
2021-02-23 17:14:32 +00:00
|
|
|
quit,
|
2020-11-16 12:08:28 +00:00
|
|
|
); err != nil {
|
2021-04-26 09:41:31 +00:00
|
|
|
return nil, err
|
2020-11-16 12:08:28 +00:00
|
|
|
}
|
2020-04-28 07:41:55 +00:00
|
|
|
|
2021-04-26 09:41:31 +00:00
|
|
|
return changes, nil
|
2020-04-28 07:41:55 +00:00
|
|
|
}
|
2020-02-28 19:48:52 +00:00
|
|
|
|
2021-04-02 11:22:25 +00:00
|
|
|
func walkAndCollect(collectorFunc func([]byte, []byte) error, db ethdb.Tx, bucket string, timestampDst, timestampSrc uint64, quit <-chan struct{}) error {
|
|
|
|
c, err := db.Cursor(bucket)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer c.Close()
|
|
|
|
return ethdb.Walk(c, dbutils.EncodeBlockNumber(timestampDst), 0, func(dbKey, dbValue []byte) (bool, error) {
|
2021-02-23 17:14:32 +00:00
|
|
|
if err := common.Stopped(quit); err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
2021-05-04 12:34:08 +00:00
|
|
|
timestamp, k, v := Mapper[bucket].Decode(dbKey, dbValue)
|
2020-11-16 12:08:28 +00:00
|
|
|
if timestamp > timestampSrc {
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
if innerErr := collectorFunc(common.CopyBytes(k), common.CopyBytes(v)); innerErr != nil {
|
|
|
|
return false, innerErr
|
|
|
|
}
|
|
|
|
return true, nil
|
|
|
|
})
|
2020-01-22 10:25:07 +00:00
|
|
|
}
|