storage: put uint32 into keys using in big endianness

Which allows to iterate over the contents easily.
This commit is contained in:
Roman Khimov 2022-02-18 14:35:17 +03:00
parent 1ca918e631
commit 600da6909c
3 changed files with 6 additions and 21 deletions

View file

@ -46,7 +46,7 @@ import (
// Tuning parameters. // Tuning parameters.
const ( const (
headerBatchCount = 2000 headerBatchCount = 2000
version = "0.2.3" version = "0.2.4"
defaultInitialGAS = 52000000_00000000 defaultInitialGAS = 52000000_00000000
defaultGCPeriod = 10000 defaultGCPeriod = 10000

View file

@ -6,7 +6,6 @@ import (
"encoding/binary" "encoding/binary"
"errors" "errors"
iocore "io" iocore "io"
"sort"
"github.com/nspcc-dev/neo-go/pkg/core/block" "github.com/nspcc-dev/neo-go/pkg/core/block"
"github.com/nspcc-dev/neo-go/pkg/core/state" "github.com/nspcc-dev/neo-go/pkg/core/state"
@ -506,33 +505,19 @@ func (dao *Simple) GetStateSyncCurrentBlockHeight() (uint32, error) {
// GetHeaderHashes returns a sorted list of header hashes retrieved from // GetHeaderHashes returns a sorted list of header hashes retrieved from
// the given underlying store. // the given underlying store.
func (dao *Simple) GetHeaderHashes() ([]util.Uint256, error) { func (dao *Simple) GetHeaderHashes() ([]util.Uint256, error) {
hashMap := make(map[uint32][]util.Uint256) var hashes = make([]util.Uint256, 0)
dao.Store.Seek(storage.SeekRange{ dao.Store.Seek(storage.SeekRange{
Prefix: storage.IXHeaderHashList.Bytes(), Prefix: storage.IXHeaderHashList.Bytes(),
}, func(k, v []byte) bool { }, func(k, v []byte) bool {
storedCount := binary.LittleEndian.Uint32(k[1:]) newHashes, err := read2000Uint256Hashes(v)
hashes, err := read2000Uint256Hashes(v)
if err != nil { if err != nil {
panic(err) panic(err)
} }
hashMap[storedCount] = hashes hashes = append(hashes, newHashes...)
return true return true
}) })
var (
hashes = make([]util.Uint256, 0, len(hashMap))
sortedKeys = make([]uint32, 0, len(hashMap))
)
for k := range hashMap {
sortedKeys = append(sortedKeys, k)
}
sort.Slice(sortedKeys, func(i, j int) bool { return sortedKeys[i] < sortedKeys[j] })
for _, key := range sortedKeys {
hashes = append(hashes[:key], hashMap[key]...)
}
return hashes, nil return hashes, nil
} }

View file

@ -125,7 +125,7 @@ func AppendPrefix(k KeyPrefix, b []byte) []byte {
// AppendPrefixInt(SYSCurrentHeader, 10001) // AppendPrefixInt(SYSCurrentHeader, 10001)
func AppendPrefixInt(k KeyPrefix, n int) []byte { func AppendPrefixInt(k KeyPrefix, n int) []byte {
b := make([]byte, 4) b := make([]byte, 4)
binary.LittleEndian.PutUint32(b, uint32(n)) binary.BigEndian.PutUint32(b, uint32(n))
return AppendPrefix(k, b) return AppendPrefix(k, b)
} }