2020-01-22 10:25:07 +00:00
|
|
|
package changeset
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
2020-05-02 18:00:57 +00:00
|
|
|
|
|
|
|
"github.com/ledgerwatch/turbo-geth/common"
|
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 (
|
|
|
|
ErrNotFound = errors.New("not found")
|
|
|
|
errIncorrectData = errors.New("empty prepared data")
|
|
|
|
ErrFindValue = errors.New("find value error")
|
|
|
|
)
|
2020-02-26 22:36:34 +00:00
|
|
|
|
2020-05-15 07:52:45 +00:00
|
|
|
/* Hashed changesets (key is a hash of common.Address) */
|
|
|
|
|
2020-01-22 10:25:07 +00:00
|
|
|
func NewStorageChangeSet() *ChangeSet {
|
|
|
|
return &ChangeSet{
|
|
|
|
Changes: make([]Change, 0),
|
|
|
|
keyLen: 2*common.HashLength + common.IncarnationLength,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func EncodeStorage(s *ChangeSet) ([]byte, error) {
|
2020-05-15 07:52:45 +00:00
|
|
|
return encodeStorage(s, common.HashLength)
|
2020-04-28 07:41:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func DecodeStorage(b []byte) (*ChangeSet, error) {
|
|
|
|
cs := NewStorageChangeSet()
|
2020-05-15 07:52:45 +00:00
|
|
|
err := decodeStorage(b, common.HashLength, cs)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2020-04-12 18:36:27 +00:00
|
|
|
}
|
2020-04-28 07:41:55 +00:00
|
|
|
return cs, nil
|
2020-04-12 18:36:27 +00:00
|
|
|
}
|
|
|
|
|
2020-01-22 10:25:07 +00:00
|
|
|
type StorageChangeSetBytes []byte
|
|
|
|
|
|
|
|
func (b StorageChangeSetBytes) Walk(f func(k, v []byte) error) error {
|
2020-05-15 07:52:45 +00:00
|
|
|
return walkStorageChangeSet(b, common.HashLength, f)
|
2020-01-22 10:25:07 +00:00
|
|
|
}
|
|
|
|
|
2020-04-28 07:41:55 +00:00
|
|
|
func (b StorageChangeSetBytes) Find(k []byte) ([]byte, error) {
|
2020-07-09 06:13:45 +00:00
|
|
|
return findWithoutIncarnationInStorageChangeSet(b, common.HashLength, k[:common.HashLength], k[common.HashLength:])
|
|
|
|
}
|
|
|
|
func (b StorageChangeSetBytes) FindWithIncarnation(k []byte) ([]byte, error) {
|
2020-05-15 07:52:45 +00:00
|
|
|
return findInStorageChangeSet(b, common.HashLength, k)
|
|
|
|
}
|
2020-04-28 07:41:55 +00:00
|
|
|
|
2020-05-15 07:52:45 +00:00
|
|
|
func (b StorageChangeSetBytes) FindWithoutIncarnation(addrHashToFind []byte, keyHashToFind []byte) ([]byte, error) {
|
|
|
|
return findWithoutIncarnationInStorageChangeSet(b, common.HashLength, addrHashToFind, keyHashToFind)
|
|
|
|
}
|
2020-02-26 22:36:34 +00:00
|
|
|
|
2020-05-15 07:52:45 +00:00
|
|
|
/* Plain changesets (key is a common.Address) */
|
2020-02-26 22:36:34 +00:00
|
|
|
|
2020-05-15 07:52:45 +00:00
|
|
|
func NewStorageChangeSetPlain() *ChangeSet {
|
|
|
|
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
|
|
|
|
2020-05-15 07:52:45 +00:00
|
|
|
func EncodeStoragePlain(s *ChangeSet) ([]byte, error) {
|
|
|
|
return encodeStorage(s, common.AddressLength)
|
|
|
|
}
|
2020-02-26 22:36:34 +00:00
|
|
|
|
2020-05-15 07:52:45 +00:00
|
|
|
func DecodeStoragePlain(b []byte) (*ChangeSet, error) {
|
|
|
|
cs := NewStorageChangeSetPlain()
|
|
|
|
err := decodeStorage(b, common.AddressLength, cs)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2020-04-28 07:41:55 +00:00
|
|
|
}
|
2020-05-15 07:52:45 +00:00
|
|
|
return cs, nil
|
|
|
|
}
|
2020-03-30 18:57:53 +00:00
|
|
|
|
2020-05-15 07:52:45 +00:00
|
|
|
type StorageChangeSetPlainBytes []byte
|
2020-04-28 07:41:55 +00:00
|
|
|
|
2020-05-15 07:52:45 +00:00
|
|
|
func (b StorageChangeSetPlainBytes) Walk(f func(k, v []byte) error) error {
|
|
|
|
return walkStorageChangeSet(b, common.AddressLength, f)
|
|
|
|
}
|
2020-04-28 15:36:00 +00:00
|
|
|
|
2020-05-15 07:52:45 +00:00
|
|
|
func (b StorageChangeSetPlainBytes) Find(k []byte) ([]byte, error) {
|
2020-07-09 06:13:45 +00:00
|
|
|
return findWithoutIncarnationInStorageChangeSet(b, common.AddressLength, k[:common.AddressLength], k[common.AddressLength:])
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b StorageChangeSetPlainBytes) FindWithIncarnation(k []byte) ([]byte, error) {
|
2020-05-15 07:52:45 +00:00
|
|
|
return findInStorageChangeSet(b, common.AddressLength, k)
|
2020-04-28 07:41:55 +00:00
|
|
|
}
|
2020-02-28 19:48:52 +00:00
|
|
|
|
2020-05-15 07:52:45 +00:00
|
|
|
func (b StorageChangeSetPlainBytes) FindWithoutIncarnation(addressToFind []byte, keyToFind []byte) ([]byte, error) {
|
|
|
|
return findWithoutIncarnationInStorageChangeSet(b, common.AddressLength, addressToFind, keyToFind)
|
2020-01-22 10:25:07 +00:00
|
|
|
}
|