mirror of
https://gitlab.com/pulsechaincom/erigon-pulse.git
synced 2025-01-03 17:44:29 +00:00
88 lines
2.4 KiB
Go
88 lines
2.4 KiB
Go
/*
|
|
Copyright 2022 Erigon contributors
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
you may not use this file except in compliance with the License.
|
|
You may obtain a copy of the License at
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
See the License for the specific language governing permissions and
|
|
limitations under the License.
|
|
*/
|
|
|
|
package historyv2
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/binary"
|
|
"errors"
|
|
"fmt"
|
|
"sort"
|
|
|
|
"github.com/ledgerwatch/erigon-lib/common/length"
|
|
"github.com/ledgerwatch/erigon-lib/kv"
|
|
)
|
|
|
|
var (
|
|
ErrNotFound = errors.New("not found")
|
|
)
|
|
|
|
func NewStorageChangeSet() *ChangeSet {
|
|
return &ChangeSet{
|
|
Changes: make([]Change, 0),
|
|
keyLen: length.Addr + length.Hash + length.Incarnation,
|
|
}
|
|
}
|
|
|
|
func EncodeStorage(blockN uint64, s *ChangeSet, f func(k, v []byte) error) error {
|
|
sort.Sort(s)
|
|
keyPart := length.Addr + length.Incarnation
|
|
for _, cs := range s.Changes {
|
|
newK := make([]byte, length.BlockNum+keyPart)
|
|
binary.BigEndian.PutUint64(newK, blockN)
|
|
copy(newK[8:], cs.Key[:keyPart])
|
|
newV := make([]byte, 0, length.Hash+len(cs.Value))
|
|
newV = append(append(newV, cs.Key[keyPart:]...), cs.Value...)
|
|
if err := f(newK, newV); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func DecodeStorage(dbKey, dbValue []byte) (uint64, []byte, []byte, error) {
|
|
blockN := binary.BigEndian.Uint64(dbKey)
|
|
if len(dbValue) < length.Hash {
|
|
return 0, nil, nil, fmt.Errorf("storage changes purged for block %d", blockN)
|
|
}
|
|
k := make([]byte, length.Addr+length.Incarnation+length.Hash)
|
|
dbKey = dbKey[length.BlockNum:] // remove BlockN bytes
|
|
copy(k, dbKey)
|
|
copy(k[len(dbKey):], dbValue[:length.Hash])
|
|
v := dbValue[length.Hash:]
|
|
if len(v) == 0 {
|
|
v = nil
|
|
}
|
|
|
|
return blockN, k, v, nil
|
|
}
|
|
|
|
func FindStorage(c kv.CursorDupSort, blockNumber uint64, k []byte) ([]byte, error) {
|
|
addWithInc, loc := k[:length.Addr+length.Incarnation], k[length.Addr+length.Incarnation:]
|
|
seek := make([]byte, length.BlockNum+length.Addr+length.Incarnation)
|
|
binary.BigEndian.PutUint64(seek, blockNumber)
|
|
copy(seek[8:], addWithInc)
|
|
v, err := c.SeekBothRange(seek, loc)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if !bytes.HasPrefix(v, loc) {
|
|
return nil, ErrNotFound
|
|
}
|
|
return v[length.Hash:], nil
|
|
}
|