mirror of
https://gitlab.com/pulsechaincom/prysm-pulse.git
synced 2025-01-06 01:32:18 +00:00
c58083ec09
Former-commit-id: 8cc3e8f619fc65d6230f38757b499e25b342bd3b [formerly ee95d821432933f87e0701160849e831a6489813] Former-commit-id: 93993c0d37ade450c7796020af1d30d2481ea1c7
89 lines
2.1 KiB
Go
89 lines
2.1 KiB
Go
package utils
|
|
|
|
import (
|
|
"math/rand"
|
|
"reflect"
|
|
"testing"
|
|
)
|
|
|
|
func buildRawBlob(size int64) []RawBlob {
|
|
tempbody := make([]RawBlob, size)
|
|
for i := int64(0); i < size; i++ {
|
|
var rawblob RawBlob
|
|
rawblob.data = buildBlob(size)
|
|
flagset := byte(rand.Int()) >> 7
|
|
if flagset == byte(1) {
|
|
rawblob.flags.skipEvmExecution = true
|
|
|
|
}
|
|
tempbody[i] = rawblob
|
|
}
|
|
|
|
return tempbody
|
|
}
|
|
|
|
func buildBlob(size int64) []byte {
|
|
tempbody := make([]byte, size)
|
|
for i := int64(0); i < size; i++ {
|
|
tempbody[i] = byte(rand.Int())
|
|
}
|
|
|
|
return tempbody
|
|
}
|
|
|
|
func TestSize(t *testing.T) {
|
|
for i := 0; i < 300; i++ {
|
|
size := int64(i)
|
|
blob := buildRawBlob(size)
|
|
chunksafterSerialize := size / chunkDataSize
|
|
terminalchunk := size % chunkDataSize
|
|
if terminalchunk != 0 {
|
|
chunksafterSerialize = chunksafterSerialize + 1
|
|
}
|
|
chunksafterSerialize = chunksafterSerialize * size
|
|
sizeafterSerialize := chunksafterSerialize * chunkSize
|
|
|
|
drefbody := make([]*RawBlob, len(blob))
|
|
for s := 0; s < len(blob); s++ {
|
|
drefbody[s] = &(blob[s])
|
|
|
|
}
|
|
serializedblob, err := Serialize(drefbody)
|
|
if err != nil {
|
|
t.Errorf("Error Serializing blob:%v\n %v", err, serializedblob)
|
|
}
|
|
|
|
if int64(len(serializedblob)) != sizeafterSerialize {
|
|
t.Errorf("Error Serializing blobs the lengths are not the same:\n %d \n %d", int64(len(serializedblob)), sizeafterSerialize)
|
|
}
|
|
}
|
|
|
|
}
|
|
func TestSerializeAndDeserializeblob(t *testing.T) {
|
|
|
|
for i := 1; i < 300; i++ {
|
|
|
|
blob := buildRawBlob(int64(i))
|
|
|
|
drefbody := make([]*RawBlob, len(blob))
|
|
for s := 0; s < len(blob); s++ {
|
|
drefbody[s] = &(blob[s])
|
|
}
|
|
|
|
serializedblob, err := Serialize(drefbody)
|
|
|
|
if err != nil {
|
|
t.Errorf("Error Serializing blob at index %d:\n%v\n%v", i, err, serializedblob)
|
|
}
|
|
raw, err2 := Deserialize(serializedblob)
|
|
if err2 != nil {
|
|
t.Errorf("Error Serializing blob at index %d:\n%v due to \n%v", i, raw, err2)
|
|
}
|
|
|
|
if !reflect.DeepEqual(blob, raw) {
|
|
t.Errorf("Error Serializing blobs at index %d, the serialized and deserialized versions are not the same:\n\n %v \n\n %v \n\n %v", i, blob, serializedblob, raw)
|
|
}
|
|
}
|
|
|
|
}
|