2022-10-08 16:15:44 +02:00
|
|
|
/*
|
|
|
|
Copyright 2022 Erigon-Lightclient 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.
|
|
|
|
*/
|
|
|
|
|
2022-09-30 00:20:09 +02:00
|
|
|
package utils
|
|
|
|
|
2022-09-30 19:07:13 +02:00
|
|
|
import (
|
|
|
|
"encoding/binary"
|
2022-09-30 23:53:54 +02:00
|
|
|
|
|
|
|
"github.com/golang/snappy"
|
2022-12-27 17:22:52 +01:00
|
|
|
"github.com/klauspost/compress/zstd"
|
2023-01-07 12:25:28 +01:00
|
|
|
"github.com/ledgerwatch/erigon/cl/cltypes/ssz_utils"
|
2022-09-30 19:07:13 +02:00
|
|
|
)
|
2022-09-30 00:20:09 +02:00
|
|
|
|
|
|
|
func Uint32ToBytes4(n uint32) (ret [4]byte) {
|
|
|
|
binary.BigEndian.PutUint32(ret[:], n)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-12-05 01:25:12 +01:00
|
|
|
func Bytes4ToUint32(bytes4 [4]byte) uint32 {
|
|
|
|
return binary.BigEndian.Uint32(bytes4[:])
|
|
|
|
}
|
|
|
|
|
2022-09-30 00:20:09 +02:00
|
|
|
func BytesToBytes4(b []byte) (ret [4]byte) {
|
|
|
|
copy(ret[:], b)
|
|
|
|
return
|
|
|
|
}
|
2022-09-30 19:07:13 +02:00
|
|
|
|
2022-10-17 19:13:23 +02:00
|
|
|
func Uint64ToLE(i uint64) []byte {
|
|
|
|
buf := make([]byte, 8)
|
|
|
|
binary.LittleEndian.PutUint64(buf, i)
|
|
|
|
return buf
|
2022-09-30 19:07:13 +02:00
|
|
|
}
|
2022-09-30 23:53:54 +02:00
|
|
|
|
|
|
|
func DecompressSnappy(data []byte) ([]byte, error) {
|
|
|
|
// Decode the snappy
|
|
|
|
lenDecoded, err := snappy.DecodedLen(data)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
decodedData := make([]byte, lenDecoded)
|
|
|
|
|
2022-10-17 19:13:23 +02:00
|
|
|
return snappy.Decode(decodedData, data)
|
2022-10-06 14:34:39 +02:00
|
|
|
}
|
|
|
|
|
2022-10-17 19:13:23 +02:00
|
|
|
func CompressSnappy(data []byte) []byte {
|
|
|
|
return snappy.Encode(nil, data)
|
2022-10-06 11:01:56 +02:00
|
|
|
}
|
2022-10-06 14:34:39 +02:00
|
|
|
|
2023-01-07 12:25:28 +01:00
|
|
|
func EncodeSSZSnappy(data ssz_utils.Marshaler) ([]byte, error) {
|
2023-01-21 22:33:50 +01:00
|
|
|
var (
|
|
|
|
enc = make([]byte, 0, data.EncodingSizeSSZ())
|
|
|
|
err error
|
|
|
|
)
|
|
|
|
enc, err = data.EncodeSSZ(enc)
|
2022-10-06 14:34:39 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-10-07 14:38:12 +02:00
|
|
|
|
|
|
|
return snappy.Encode(nil, enc), nil
|
2022-10-06 14:34:39 +02:00
|
|
|
}
|
|
|
|
|
2023-01-21 22:33:50 +01:00
|
|
|
func DecodeSSZSnappy(dst ssz_utils.Unmarshaler, src []byte) error {
|
2022-10-06 14:34:39 +02:00
|
|
|
dec, err := snappy.Decode(nil, src)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-01-21 22:33:50 +01:00
|
|
|
err = dst.DecodeSSZ(dec)
|
2022-10-06 14:34:39 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2022-12-27 17:22:52 +01:00
|
|
|
|
2023-02-09 17:48:52 +01:00
|
|
|
func DecodeSSZSnappyWithVersion(dst ssz_utils.Unmarshaler, src []byte, version int) error {
|
|
|
|
dec, err := snappy.Decode(nil, src)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = dst.DecodeSSZWithVersion(dec, version)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-12-27 17:22:52 +01:00
|
|
|
func CompressZstd(b []byte) []byte {
|
|
|
|
wr, err := zstd.NewWriter(nil)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return wr.EncodeAll(b, nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
func DecompressZstd(b []byte) ([]byte, error) {
|
|
|
|
r, err := zstd.NewReader(nil)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return r.DecodeAll(b, nil)
|
|
|
|
}
|
2023-02-07 17:44:37 +01:00
|
|
|
|
|
|
|
// Check if it is sorted and check if there are duplicates. O(N) complexity.
|
|
|
|
func IsSliceSortedSet(vals []uint64) bool {
|
|
|
|
for i := 0; i < len(vals)-1; i++ {
|
|
|
|
if vals[i] >= vals[i+1] {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|