2020-01-22 10:25:07 +00:00
|
|
|
package changeset
|
2019-10-31 10:59:00 +00:00
|
|
|
|
|
|
|
import (
|
2021-05-04 12:34:08 +00:00
|
|
|
"bytes"
|
|
|
|
"encoding/binary"
|
|
|
|
"sort"
|
|
|
|
|
2021-10-08 03:20:45 +00:00
|
|
|
"github.com/ledgerwatch/erigon-lib/common/length"
|
2021-07-29 11:53:13 +00:00
|
|
|
"github.com/ledgerwatch/erigon-lib/kv"
|
2021-05-20 18:25:53 +00:00
|
|
|
"github.com/ledgerwatch/erigon/common"
|
|
|
|
"github.com/ledgerwatch/erigon/common/dbutils"
|
2019-10-31 10:59:00 +00:00
|
|
|
)
|
|
|
|
|
2020-11-16 12:08:28 +00:00
|
|
|
type Encoder func(blockN uint64, s *ChangeSet, f func(k, v []byte) error) error
|
|
|
|
type Decoder func(dbKey, dbValue []byte) (blockN uint64, k, v []byte)
|
|
|
|
|
2021-05-04 12:34:08 +00:00
|
|
|
func NewAccountChangeSet() *ChangeSet {
|
2020-05-15 07:52:45 +00:00
|
|
|
return &ChangeSet{
|
|
|
|
Changes: make([]Change, 0),
|
2021-10-08 03:20:45 +00:00
|
|
|
keyLen: length.Addr,
|
2020-01-16 21:21:40 +00:00
|
|
|
}
|
2020-05-15 07:52:45 +00:00
|
|
|
}
|
2020-01-16 21:21:40 +00:00
|
|
|
|
2021-05-04 12:34:08 +00:00
|
|
|
func EncodeAccounts(blockN uint64, s *ChangeSet, f func(k, v []byte) error) error {
|
|
|
|
sort.Sort(s)
|
|
|
|
newK := dbutils.EncodeBlockNumber(blockN)
|
|
|
|
for _, cs := range s.Changes {
|
|
|
|
newV := make([]byte, len(cs.Key)+len(cs.Value))
|
|
|
|
copy(newV, cs.Key)
|
|
|
|
copy(newV[len(cs.Key):], cs.Value)
|
|
|
|
if err := f(newK, newV); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
2020-05-15 07:52:45 +00:00
|
|
|
}
|
2020-01-16 21:21:40 +00:00
|
|
|
|
2021-05-04 12:34:08 +00:00
|
|
|
func DecodeAccounts(dbKey, dbValue []byte) (uint64, []byte, []byte) {
|
|
|
|
blockN := binary.BigEndian.Uint64(dbKey)
|
2021-10-08 03:20:45 +00:00
|
|
|
k := dbValue[:length.Addr]
|
|
|
|
v := dbValue[length.Addr:]
|
2021-05-04 12:34:08 +00:00
|
|
|
|
|
|
|
return blockN, k, v
|
|
|
|
}
|
2020-11-16 12:08:28 +00:00
|
|
|
|
2021-07-28 02:47:38 +00:00
|
|
|
func FindAccount(c kv.CursorDupSort, blockNumber uint64, key []byte) ([]byte, error) {
|
2021-05-04 12:34:08 +00:00
|
|
|
k := dbutils.EncodeBlockNumber(blockNumber)
|
2021-07-04 07:49:31 +00:00
|
|
|
v, err := c.SeekBothRange(k, key)
|
2021-05-04 12:34:08 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
_, k, v = DecodeAccounts(k, v)
|
|
|
|
if !bytes.HasPrefix(k, key) {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
return v, nil
|
2020-05-15 07:52:45 +00:00
|
|
|
}
|
2020-01-16 21:21:40 +00:00
|
|
|
|
2020-11-16 12:08:28 +00:00
|
|
|
// GetModifiedAccounts returns a list of addresses that were modified in the block range
|
2021-09-22 00:54:29 +00:00
|
|
|
// [startNum:endNum)
|
2021-07-28 02:47:38 +00:00
|
|
|
func GetModifiedAccounts(db kv.Tx, startNum, endNum uint64) ([]common.Address, error) {
|
2020-11-16 12:08:28 +00:00
|
|
|
changedAddrs := make(map[common.Address]struct{})
|
2021-09-22 00:54:29 +00:00
|
|
|
if err := ForRange(db, kv.AccountChangeSet, startNum, endNum, func(blockN uint64, k, v []byte) error {
|
2020-11-16 12:08:28 +00:00
|
|
|
changedAddrs[common.BytesToAddress(k)] = struct{}{}
|
2021-09-22 00:54:29 +00:00
|
|
|
return nil
|
2020-11-16 12:08:28 +00:00
|
|
|
}); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(changedAddrs) == 0 {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
idx := 0
|
|
|
|
result := make([]common.Address, len(changedAddrs))
|
|
|
|
for addr := range changedAddrs {
|
|
|
|
copy(result[idx][:], addr[:])
|
|
|
|
idx++
|
|
|
|
}
|
|
|
|
|
|
|
|
return result, nil
|
2020-01-16 21:21:40 +00:00
|
|
|
}
|