mirror of
https://github.com/OffchainLabs/prysm.git
synced 2026-05-02 03:02:54 -04:00
Merge branch '__develop' into features/native-beacon-state
# Conflicts: # beacon-chain/core/blocks/signature.go # beacon-chain/state/fieldtrie/BUILD.bazel # beacon-chain/state/fieldtrie/field_trie_helpers.go # beacon-chain/state/fieldtrie/field_trie_test.go # beacon-chain/state/stateutil/trie_helpers_test.go # beacon-chain/state/v1/BUILD.bazel # beacon-chain/state/v1/field_roots.go # beacon-chain/state/v1/getters_misc.go # beacon-chain/state/v1/setters_validator.go # beacon-chain/state/v1/state_test.go # beacon-chain/state/v1/state_trie.go # beacon-chain/state/v2/BUILD.bazel # beacon-chain/state/v2/field_roots.go # beacon-chain/state/v2/setters_validator.go # beacon-chain/state/v2/state_trie.go # deps.bzl # go.mod # go.sum # proto/migration/v1alpha1_to_v1.go
This commit is contained in:
@@ -8,6 +8,7 @@ import (
|
||||
"github.com/minio/sha256-simd"
|
||||
"github.com/pkg/errors"
|
||||
"github.com/prysmaticlabs/go-bitfield"
|
||||
"github.com/prysmaticlabs/prysm/encoding/bytesutil"
|
||||
)
|
||||
|
||||
const bytesPerChunk = 32
|
||||
@@ -113,6 +114,53 @@ func Pack(serializedItems [][]byte) ([][]byte, error) {
|
||||
return chunks, nil
|
||||
}
|
||||
|
||||
// PackByChunk a given byte array's final chunk with zeroes if needed.
|
||||
func PackByChunk(serializedItems [][]byte) ([][bytesPerChunk]byte, error) {
|
||||
emptyChunk := [bytesPerChunk]byte{}
|
||||
// If there are no items, we return an empty chunk.
|
||||
if len(serializedItems) == 0 {
|
||||
return [][bytesPerChunk]byte{emptyChunk}, nil
|
||||
} else if len(serializedItems[0]) == bytesPerChunk {
|
||||
// If each item has exactly BYTES_PER_CHUNK length, we return the list of serialized items.
|
||||
chunks := make([][bytesPerChunk]byte, 0, len(serializedItems))
|
||||
for _, c := range serializedItems {
|
||||
chunks = append(chunks, bytesutil.ToBytes32(c))
|
||||
}
|
||||
return chunks, nil
|
||||
}
|
||||
// We flatten the list in order to pack its items into byte chunks correctly.
|
||||
var orderedItems []byte
|
||||
for _, item := range serializedItems {
|
||||
orderedItems = append(orderedItems, item...)
|
||||
}
|
||||
// If all our serialized item slices are length zero, we
|
||||
// exit early.
|
||||
if len(orderedItems) == 0 {
|
||||
return [][bytesPerChunk]byte{emptyChunk}, nil
|
||||
}
|
||||
numItems := len(orderedItems)
|
||||
var chunks [][bytesPerChunk]byte
|
||||
for i := 0; i < numItems; i += bytesPerChunk {
|
||||
j := i + bytesPerChunk
|
||||
// We create our upper bound index of the chunk, if it is greater than numItems,
|
||||
// we set it as numItems itself.
|
||||
if j > numItems {
|
||||
j = numItems
|
||||
}
|
||||
// We create chunks from the list of items based on the
|
||||
// indices determined above.
|
||||
// Right-pad the last chunk with zero bytes if it does not
|
||||
// have length bytesPerChunk from the helper.
|
||||
// The ToBytes32 helper allocates a 32-byte array, before
|
||||
// copying the ordered items in. This ensures that even if
|
||||
// the last chunk is != 32 in length, we will right-pad it with
|
||||
// zero bytes.
|
||||
chunks = append(chunks, bytesutil.ToBytes32(orderedItems[i:j]))
|
||||
}
|
||||
|
||||
return chunks, nil
|
||||
}
|
||||
|
||||
// MixInLength appends hash length to root
|
||||
func MixInLength(root [32]byte, length []byte) [32]byte {
|
||||
var hash [32]byte
|
||||
|
||||
@@ -94,6 +94,22 @@ func TestPack(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestPackByChunk(t *testing.T) {
|
||||
byteSlice2D := [][]byte{
|
||||
{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 2, 5, 2, 6, 2, 7},
|
||||
{1, 1, 2, 3, 5, 8, 13, 21, 34},
|
||||
}
|
||||
expected := [][32]byte{{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 2, 5, 2, 6, 2, 7, 1, 1},
|
||||
{2, 3, 5, 8, 13, 21, 34, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}}
|
||||
|
||||
result, err := ssz.PackByChunk(byteSlice2D)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, len(expected), len(result))
|
||||
for i, v := range expected {
|
||||
assert.DeepEqual(t, v, result[i])
|
||||
}
|
||||
}
|
||||
|
||||
func TestMixInLength(t *testing.T) {
|
||||
byteSlice := [32]byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32}
|
||||
length := []byte{1, 2, 3}
|
||||
|
||||
Reference in New Issue
Block a user