erigon-pulse/common/changeset/storage_changeset.go
b00ris eca0f233ec
[WIP] WalkAsOf (#724)
* save state

* walkAsOf by cs test passed

* cs search for plain state

* save state

* fix accounts tests

* refactor walkAsOf account tests

* fix storage test

* refactor walkAsOf storage tests

* fix lint

* fix test

* save state

* save state

* add test with fixed bits

* fmt

* add stages check

* fix lint

* fix lint

* remove obsoleted methods
2020-07-09 07:13:45 +01:00

97 lines
2.7 KiB
Go

package changeset
import (
"errors"
"github.com/ledgerwatch/turbo-geth/common"
)
const (
DefaultIncarnation = uint64(1)
)
var (
ErrNotFound = errors.New("not found")
errIncorrectData = errors.New("empty prepared data")
ErrFindValue = errors.New("find value error")
)
/* Hashed changesets (key is a hash of common.Address) */
func NewStorageChangeSet() *ChangeSet {
return &ChangeSet{
Changes: make([]Change, 0),
keyLen: 2*common.HashLength + common.IncarnationLength,
}
}
func EncodeStorage(s *ChangeSet) ([]byte, error) {
return encodeStorage(s, common.HashLength)
}
func DecodeStorage(b []byte) (*ChangeSet, error) {
cs := NewStorageChangeSet()
err := decodeStorage(b, common.HashLength, cs)
if err != nil {
return nil, err
}
return cs, nil
}
type StorageChangeSetBytes []byte
func (b StorageChangeSetBytes) Walk(f func(k, v []byte) error) error {
return walkStorageChangeSet(b, common.HashLength, f)
}
func (b StorageChangeSetBytes) Find(k []byte) ([]byte, error) {
return findWithoutIncarnationInStorageChangeSet(b, common.HashLength, k[:common.HashLength], k[common.HashLength:])
}
func (b StorageChangeSetBytes) FindWithIncarnation(k []byte) ([]byte, error) {
return findInStorageChangeSet(b, common.HashLength, k)
}
func (b StorageChangeSetBytes) FindWithoutIncarnation(addrHashToFind []byte, keyHashToFind []byte) ([]byte, error) {
return findWithoutIncarnationInStorageChangeSet(b, common.HashLength, addrHashToFind, keyHashToFind)
}
/* Plain changesets (key is a common.Address) */
func NewStorageChangeSetPlain() *ChangeSet {
return &ChangeSet{
Changes: make([]Change, 0),
keyLen: common.AddressLength + common.HashLength + common.IncarnationLength,
}
}
func EncodeStoragePlain(s *ChangeSet) ([]byte, error) {
return encodeStorage(s, common.AddressLength)
}
func DecodeStoragePlain(b []byte) (*ChangeSet, error) {
cs := NewStorageChangeSetPlain()
err := decodeStorage(b, common.AddressLength, cs)
if err != nil {
return nil, err
}
return cs, nil
}
type StorageChangeSetPlainBytes []byte
func (b StorageChangeSetPlainBytes) Walk(f func(k, v []byte) error) error {
return walkStorageChangeSet(b, common.AddressLength, f)
}
func (b StorageChangeSetPlainBytes) Find(k []byte) ([]byte, error) {
return findWithoutIncarnationInStorageChangeSet(b, common.AddressLength, k[:common.AddressLength], k[common.AddressLength:])
}
func (b StorageChangeSetPlainBytes) FindWithIncarnation(k []byte) ([]byte, error) {
return findInStorageChangeSet(b, common.AddressLength, k)
}
func (b StorageChangeSetPlainBytes) FindWithoutIncarnation(addressToFind []byte, keyToFind []byte) ([]byte, error) {
return findWithoutIncarnationInStorageChangeSet(b, common.AddressLength, addressToFind, keyToFind)
}