2020-05-15 07:52:45 +00:00
|
|
|
package changeset
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/binary"
|
|
|
|
"sort"
|
|
|
|
|
|
|
|
"github.com/ledgerwatch/turbo-geth/common"
|
2020-11-16 12:08:28 +00:00
|
|
|
"github.com/ledgerwatch/turbo-geth/common/dbutils"
|
|
|
|
"github.com/ledgerwatch/turbo-geth/ethdb"
|
2020-05-15 07:52:45 +00:00
|
|
|
)
|
|
|
|
|
2020-11-16 12:08:28 +00:00
|
|
|
func walk(c ethdb.CursorDupSort, from, to uint64, keyPrefixLen int, f func(blockN uint64, k, v []byte) error) error {
|
|
|
|
fromDBFormat := FromDBFormat(keyPrefixLen)
|
|
|
|
var blockNum uint64
|
|
|
|
for k, v, err := c.Seek(dbutils.EncodeBlockNumber(from)); k != nil; k, v, err = c.Next() {
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2020-05-15 07:52:45 +00:00
|
|
|
}
|
2020-11-16 12:08:28 +00:00
|
|
|
blockNum, k, v = fromDBFormat(k, v)
|
|
|
|
if blockNum > to {
|
|
|
|
break
|
2020-05-15 07:52:45 +00:00
|
|
|
}
|
|
|
|
|
2020-11-16 12:08:28 +00:00
|
|
|
err = f(blockNum, k, v)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2020-05-15 07:52:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-11-16 12:08:28 +00:00
|
|
|
func findInStorageChangeSet2(c ethdb.CursorDupSort, blockNumber uint64, keyPrefixLen int, k []byte) ([]byte, error) {
|
|
|
|
return doSearch2(
|
|
|
|
c, blockNumber,
|
2020-05-15 07:52:45 +00:00
|
|
|
keyPrefixLen,
|
2020-11-16 12:08:28 +00:00
|
|
|
k[:keyPrefixLen],
|
2020-05-15 07:52:45 +00:00
|
|
|
k[keyPrefixLen+common.IncarnationLength:keyPrefixLen+common.HashLength+common.IncarnationLength],
|
2020-07-17 14:18:31 +00:00
|
|
|
binary.BigEndian.Uint64(k[keyPrefixLen:]), /* incarnation */
|
2020-05-15 07:52:45 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2020-11-16 12:08:28 +00:00
|
|
|
func findWithoutIncarnationInStorageChangeSet2(c ethdb.CursorDupSort, blockNumber uint64, keyPrefixLen int, addrBytesToFind []byte, keyBytesToFind []byte) ([]byte, error) {
|
|
|
|
return doSearch2(
|
|
|
|
c, blockNumber,
|
2020-05-15 07:52:45 +00:00
|
|
|
keyPrefixLen,
|
|
|
|
addrBytesToFind,
|
|
|
|
keyBytesToFind,
|
2020-07-06 06:34:24 +00:00
|
|
|
0, /* incarnation */
|
2020-05-15 07:52:45 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2020-11-16 12:08:28 +00:00
|
|
|
func doSearch2(
|
|
|
|
c ethdb.CursorDupSort,
|
|
|
|
blockNumber uint64,
|
2020-05-15 07:52:45 +00:00
|
|
|
keyPrefixLen int,
|
|
|
|
addrBytesToFind []byte,
|
|
|
|
keyBytesToFind []byte,
|
2020-07-06 06:34:24 +00:00
|
|
|
incarnation uint64,
|
2020-05-15 07:52:45 +00:00
|
|
|
) ([]byte, error) {
|
2020-11-16 12:08:28 +00:00
|
|
|
fromDBFormat := FromDBFormat(keyPrefixLen)
|
|
|
|
if incarnation == 0 {
|
|
|
|
seek := make([]byte, 8+keyPrefixLen)
|
|
|
|
binary.BigEndian.PutUint64(seek, blockNumber)
|
|
|
|
copy(seek[8:], addrBytesToFind)
|
|
|
|
for k, v, err := c.Seek(seek); k != nil; k, v, err = c.Next() {
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2020-07-16 09:53:34 +00:00
|
|
|
}
|
2020-11-16 12:08:28 +00:00
|
|
|
_, k, v = fromDBFormat(k, v)
|
|
|
|
if !bytes.HasPrefix(k, addrBytesToFind) {
|
|
|
|
return nil, ErrNotFound
|
2020-07-16 09:53:34 +00:00
|
|
|
}
|
|
|
|
|
2020-11-16 12:08:28 +00:00
|
|
|
stHash := k[keyPrefixLen+common.IncarnationLength:]
|
|
|
|
if bytes.Equal(stHash, keyBytesToFind) {
|
|
|
|
return v, nil
|
|
|
|
}
|
2020-07-06 06:34:24 +00:00
|
|
|
}
|
2020-11-16 12:08:28 +00:00
|
|
|
return nil, ErrNotFound
|
2020-07-06 06:34:24 +00:00
|
|
|
}
|
2020-07-16 09:53:34 +00:00
|
|
|
|
2020-11-16 12:08:28 +00:00
|
|
|
seek := make([]byte, 8+keyPrefixLen+common.IncarnationLength)
|
|
|
|
binary.BigEndian.PutUint64(seek, blockNumber)
|
|
|
|
copy(seek[8:], addrBytesToFind)
|
|
|
|
binary.BigEndian.PutUint64(seek[8+keyPrefixLen:], incarnation)
|
|
|
|
k, v, err := c.SeekBothRange(seek, keyBytesToFind)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2020-05-15 07:52:45 +00:00
|
|
|
}
|
2020-11-16 12:08:28 +00:00
|
|
|
if k == nil {
|
2020-05-15 07:52:45 +00:00
|
|
|
return nil, ErrNotFound
|
|
|
|
}
|
2020-11-16 12:08:28 +00:00
|
|
|
if !bytes.HasPrefix(v, keyBytesToFind) {
|
2020-06-11 06:19:15 +00:00
|
|
|
return nil, ErrNotFound
|
|
|
|
}
|
2020-11-16 12:08:28 +00:00
|
|
|
_, _, v = fromDBFormat(k, v)
|
|
|
|
return v, nil
|
2020-05-15 07:52:45 +00:00
|
|
|
}
|
|
|
|
|
2020-11-16 12:08:28 +00:00
|
|
|
func encodeStorage2(blockN uint64, s *ChangeSet, keyPrefixLen uint32, f func(k, v []byte) error) error {
|
|
|
|
sort.Sort(s)
|
|
|
|
keyPart := keyPrefixLen + common.IncarnationLength
|
|
|
|
for _, cs := range s.Changes {
|
|
|
|
newK := make([]byte, 8+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-05-15 07:52:45 +00:00
|
|
|
}
|