mirror of
https://gitlab.com/pulsechaincom/prysm-pulse.git
synced 2024-12-25 04:47:18 +00:00
cb02a6897d
* proto+ssz * refactor GetBlindedBlockSSZ (cherry picked from commit 97483c339f99b0d96bd81846a979383ffd2b0cda) # Conflicts: # beacon-chain/rpc/eth/beacon/blocks.go (cherry picked from commit 9e4e82d2c55e09be7568b28eaa33cdd1141445f4) # Conflicts: # beacon-chain/rpc/eth/beacon/blocks.go * add Capella version (cherry picked from commit 5d6fd0bbe663e5dd16df5b2e773f68982bbcd24e) (cherry picked from commit 82f6ddb693ac9e8b4336b30fae724e478b9e8ec0) * support SSZ lol (cherry picked from commit 52bc2c8d617ac3e1254c493fa053cdce4a1ebd63) (cherry picked from commit d7d70bc25b3ee8acbea10aaf77d26cd1f8c5f26f) * update withdrawals proto * refactor and test GetBlockV2 (cherry picked from commit c1d4eaa79d4df04bd284ec65cf261b6f5f260a97) # Conflicts: # beacon-chain/rpc/eth/beacon/blocks.go * refactor and test GetSSZBlockV2 (cherry picked from commit fbc4e73d31c2f68d55d1e2bb8e7f2d8c7458c0a0) # Conflicts: # beacon-chain/rpc/eth/beacon/blocks.go * test other functions (cherry picked from commit 31d4a4cd1165b882d823696e5983ac6635262ec2) * move stuff to blinded_blocks.go (cherry picked from commit 0a9e1658ddb28f45ae5c1cb9fc2703cbb8c6708d) # Conflicts: # beacon-chain/rpc/eth/beacon/blocks.go * fix migration code * add Capella to SubmitBlock * custom hooks * missing structs * fix tests * fix tests * review * fix build issues * replace ioutil with io Co-authored-by: prylabs-bulldozer[bot] <58059840+prylabs-bulldozer[bot]@users.noreply.github.com>
234 lines
7.2 KiB
Go
234 lines
7.2 KiB
Go
package ssz_test
|
|
|
|
import (
|
|
"reflect"
|
|
"testing"
|
|
|
|
fieldparams "github.com/prysmaticlabs/prysm/v3/config/fieldparams"
|
|
"github.com/prysmaticlabs/prysm/v3/crypto/hash"
|
|
"github.com/prysmaticlabs/prysm/v3/encoding/ssz"
|
|
enginev1 "github.com/prysmaticlabs/prysm/v3/proto/engine/v1"
|
|
ethpb "github.com/prysmaticlabs/prysm/v3/proto/prysm/v1alpha1"
|
|
"github.com/prysmaticlabs/prysm/v3/testing/assert"
|
|
"github.com/prysmaticlabs/prysm/v3/testing/require"
|
|
)
|
|
|
|
func TestUint64Root(t *testing.T) {
|
|
uintVal := uint64(1234567890)
|
|
expected := [32]byte{210, 2, 150, 73, 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, 0, 0}
|
|
|
|
result := ssz.Uint64Root(uintVal)
|
|
assert.Equal(t, expected, result)
|
|
}
|
|
|
|
func TestForkRoot(t *testing.T) {
|
|
testFork := ethpb.Fork{
|
|
PreviousVersion: []byte{123},
|
|
CurrentVersion: []byte{124},
|
|
Epoch: 1234567890,
|
|
}
|
|
expected := [32]byte{19, 46, 77, 103, 92, 175, 247, 33, 100, 64, 17, 111, 199, 145, 69, 38, 217, 112, 6, 16, 149, 201, 225, 144, 192, 228, 197, 172, 157, 78, 114, 140}
|
|
|
|
result, err := ssz.ForkRoot(&testFork)
|
|
require.NoError(t, err)
|
|
assert.Equal(t, expected, result)
|
|
}
|
|
|
|
func TestCheckPointRoot(t *testing.T) {
|
|
testHasher := hash.CustomSHA256Hasher()
|
|
testCheckpoint := ethpb.Checkpoint{
|
|
Epoch: 1234567890,
|
|
Root: []byte{222},
|
|
}
|
|
expected := [32]byte{228, 65, 39, 109, 183, 249, 167, 232, 125, 239, 25, 155, 207, 4, 84, 174, 176, 229, 175, 224, 62, 33, 215, 254, 170, 220, 132, 65, 246, 128, 68, 194}
|
|
|
|
result, err := ssz.CheckpointRoot(testHasher, &testCheckpoint)
|
|
require.NoError(t, err)
|
|
assert.Equal(t, expected, result)
|
|
}
|
|
|
|
func TestByteArrayRootWithLimit(t *testing.T) {
|
|
testHistoricalRoots := [][]byte{{123}, {234}}
|
|
expected := [32]byte{70, 204, 150, 196, 89, 138, 190, 205, 65, 207, 120, 166, 179, 247, 147, 20, 29, 133, 117, 116, 151, 234, 129, 32, 22, 15, 79, 178, 98, 73, 132, 152}
|
|
|
|
result, err := ssz.ByteArrayRootWithLimit(testHistoricalRoots, 16777216)
|
|
require.NoError(t, err)
|
|
assert.Equal(t, expected, result)
|
|
}
|
|
|
|
func TestSlashingsRoot(t *testing.T) {
|
|
testSlashingsRoot := []uint64{123, 234}
|
|
expected := [32]byte{123, 0, 0, 0, 0, 0, 0, 0, 234, 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.SlashingsRoot(testSlashingsRoot)
|
|
require.NoError(t, err)
|
|
assert.Equal(t, expected, result)
|
|
}
|
|
|
|
func TestTransactionsRoot(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
txs [][]byte
|
|
want [32]byte
|
|
wantErr bool
|
|
}{
|
|
{
|
|
name: "nil",
|
|
txs: nil,
|
|
want: [32]byte{127, 254, 36, 30, 166, 1, 135, 253, 176, 24, 123, 250, 34, 222, 53, 209, 249, 190, 215, 171, 6, 29, 148, 1, 253, 71, 227, 74, 84, 251, 237, 225},
|
|
},
|
|
{
|
|
name: "empty",
|
|
txs: [][]byte{},
|
|
want: [32]byte{127, 254, 36, 30, 166, 1, 135, 253, 176, 24, 123, 250, 34, 222, 53, 209, 249, 190, 215, 171, 6, 29, 148, 1, 253, 71, 227, 74, 84, 251, 237, 225},
|
|
},
|
|
{
|
|
name: "one tx",
|
|
txs: [][]byte{{1, 2, 3}},
|
|
want: [32]byte{102, 209, 140, 87, 217, 28, 68, 12, 133, 42, 77, 136, 191, 18, 234, 105, 166, 228, 216, 235, 230, 95, 200, 73, 85, 33, 134, 254, 219, 97, 82, 209},
|
|
},
|
|
{
|
|
name: "max txs",
|
|
txs: func() [][]byte {
|
|
var txs [][]byte
|
|
for i := 0; i < fieldparams.MaxTxsPerPayloadLength; i++ {
|
|
txs = append(txs, []byte{})
|
|
}
|
|
return txs
|
|
}(),
|
|
want: [32]byte{13, 66, 254, 206, 203, 58, 48, 133, 78, 218, 48, 231, 120, 90, 38, 72, 73, 137, 86, 9, 31, 213, 185, 101, 103, 144, 0, 236, 225, 57, 47, 244},
|
|
},
|
|
{
|
|
name: "exceed max txs",
|
|
txs: func() [][]byte {
|
|
var txs [][]byte
|
|
for i := 0; i < fieldparams.MaxTxsPerPayloadLength+1; i++ {
|
|
txs = append(txs, []byte{})
|
|
}
|
|
return txs
|
|
}(),
|
|
wantErr: true,
|
|
},
|
|
}
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
got, err := ssz.TransactionsRoot(tt.txs)
|
|
if (err != nil) != tt.wantErr {
|
|
t.Errorf("TransactionsRoot() error = %v, wantErr %v", err, tt.wantErr)
|
|
return
|
|
}
|
|
if !reflect.DeepEqual(got, tt.want) {
|
|
t.Errorf("TransactionsRoot() got = %v, want %v", got, tt.want)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestPackByChunk_SingleList(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
input []byte
|
|
want [][32]byte
|
|
}{
|
|
{
|
|
name: "nil",
|
|
input: nil,
|
|
want: [][32]byte{{}},
|
|
},
|
|
{
|
|
name: "empty",
|
|
input: []byte{},
|
|
want: [][32]byte{{}},
|
|
},
|
|
{
|
|
name: "one",
|
|
input: []byte{1},
|
|
want: [][32]byte{{1, 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, 0, 0, 0, 0, 0, 0}},
|
|
},
|
|
{
|
|
name: "one, two",
|
|
input: []byte{1, 2},
|
|
want: [][32]byte{{1, 2, 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, 0, 0, 0, 0, 0}},
|
|
},
|
|
}
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
got, err := ssz.PackByChunk([][]byte{tt.input})
|
|
require.NoError(t, err)
|
|
require.DeepSSZEqual(t, tt.want, got)
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestWithdrawalRoot(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
input *enginev1.Withdrawal
|
|
want [32]byte
|
|
}{
|
|
{
|
|
name: "nil",
|
|
input: &enginev1.Withdrawal{},
|
|
want: [32]byte{0xdb, 0x56, 0x11, 0x4e, 0x0, 0xfd, 0xd4, 0xc1, 0xf8, 0x5c, 0x89, 0x2b, 0xf3, 0x5a, 0xc9, 0xa8, 0x92, 0x89, 0xaa, 0xec, 0xb1, 0xeb, 0xd0, 0xa9, 0x6c, 0xde, 0x60, 0x6a, 0x74, 0x8b, 0x5d, 0x71},
|
|
},
|
|
{
|
|
name: "empty",
|
|
input: &enginev1.Withdrawal{
|
|
Address: make([]byte, 20),
|
|
},
|
|
want: [32]byte{0xdb, 0x56, 0x11, 0x4e, 0x0, 0xfd, 0xd4, 0xc1, 0xf8, 0x5c, 0x89, 0x2b, 0xf3, 0x5a, 0xc9, 0xa8, 0x92, 0x89, 0xaa, 0xec, 0xb1, 0xeb, 0xd0, 0xa9, 0x6c, 0xde, 0x60, 0x6a, 0x74, 0x8b, 0x5d, 0x71},
|
|
},
|
|
{
|
|
name: "non-empty",
|
|
input: &enginev1.Withdrawal{
|
|
Index: 123,
|
|
ValidatorIndex: 123123,
|
|
Address: []byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0},
|
|
Amount: 50,
|
|
},
|
|
want: [32]byte{0x4f, 0xca, 0x3a, 0x43, 0x6e, 0xcc, 0x34, 0xad, 0x33, 0xde, 0x3c, 0x22, 0xa3, 0x32, 0x27, 0xa, 0x8c, 0x4e, 0x75, 0xd8, 0x39, 0xc1, 0xd7, 0x55, 0x78, 0x77, 0xd7, 0x14, 0x6b, 0x34, 0x6a, 0xb6},
|
|
},
|
|
}
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
hasher := hash.CustomSHA256Hasher()
|
|
got, err := ssz.WithdrawalRoot(hasher, tt.input)
|
|
require.NoError(t, err)
|
|
require.DeepSSZEqual(t, tt.want, got)
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestWithrawalSliceRoot(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
input []*enginev1.Withdrawal
|
|
want [32]byte
|
|
}{
|
|
{
|
|
name: "empty",
|
|
input: make([]*enginev1.Withdrawal, 0),
|
|
want: [32]byte{0x79, 0x29, 0x30, 0xbb, 0xd5, 0xba, 0xac, 0x43, 0xbc, 0xc7, 0x98, 0xee, 0x49, 0xaa, 0x81, 0x85, 0xef, 0x76, 0xbb, 0x3b, 0x44, 0xba, 0x62, 0xb9, 0x1d, 0x86, 0xae, 0x56, 0x9e, 0x4b, 0xb5, 0x35},
|
|
},
|
|
{
|
|
name: "non-empty",
|
|
input: []*enginev1.Withdrawal{&enginev1.Withdrawal{
|
|
Index: 123,
|
|
ValidatorIndex: 123123,
|
|
Address: []byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0},
|
|
Amount: 50,
|
|
},
|
|
},
|
|
want: [32]byte{0x10, 0x34, 0x29, 0xd1, 0x34, 0x30, 0xa0, 0x1c, 0x4, 0xdd, 0x3, 0xed, 0xe6, 0xa6, 0x33, 0xb2, 0xc9, 0x24, 0x23, 0x5c, 0x43, 0xca, 0xb2, 0x32, 0xaa, 0xed, 0xfe, 0xd5, 0x9, 0x78, 0xd1, 0x6f},
|
|
},
|
|
}
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
hasher := hash.CustomSHA256Hasher()
|
|
got, err := ssz.WithdrawalSliceRoot(hasher, tt.input, 16)
|
|
require.NoError(t, err)
|
|
require.DeepSSZEqual(t, tt.want, got)
|
|
})
|
|
}
|
|
}
|