2021-09-03 16:00:56 +00:00
|
|
|
package sync
|
|
|
|
|
|
|
|
import (
|
|
|
|
"reflect"
|
|
|
|
"testing"
|
|
|
|
|
2024-02-15 05:46:47 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/v5/beacon-chain/blockchain"
|
|
|
|
mock "github.com/prysmaticlabs/prysm/v5/beacon-chain/blockchain/testing"
|
|
|
|
"github.com/prysmaticlabs/prysm/v5/beacon-chain/core/signing"
|
|
|
|
"github.com/prysmaticlabs/prysm/v5/config/params"
|
|
|
|
"github.com/prysmaticlabs/prysm/v5/consensus-types/blocks"
|
|
|
|
"github.com/prysmaticlabs/prysm/v5/consensus-types/interfaces"
|
|
|
|
enginev1 "github.com/prysmaticlabs/prysm/v5/proto/engine/v1"
|
|
|
|
ethpb "github.com/prysmaticlabs/prysm/v5/proto/prysm/v1alpha1"
|
|
|
|
"github.com/prysmaticlabs/prysm/v5/testing/require"
|
2021-09-03 16:00:56 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestExtractBlockDataType(t *testing.T) {
|
|
|
|
// Precompute digests
|
2021-09-27 16:19:20 +00:00
|
|
|
genDigest, err := signing.ComputeForkDigest(params.BeaconConfig().GenesisForkVersion, params.BeaconConfig().ZeroHash[:])
|
2021-09-03 16:00:56 +00:00
|
|
|
require.NoError(t, err)
|
2021-09-27 16:19:20 +00:00
|
|
|
altairDigest, err := signing.ComputeForkDigest(params.BeaconConfig().AltairForkVersion, params.BeaconConfig().ZeroHash[:])
|
2021-09-03 16:00:56 +00:00
|
|
|
require.NoError(t, err)
|
2022-01-28 16:26:52 +00:00
|
|
|
bellatrixDigest, err := signing.ComputeForkDigest(params.BeaconConfig().BellatrixForkVersion, params.BeaconConfig().ZeroHash[:])
|
|
|
|
require.NoError(t, err)
|
2021-09-03 16:00:56 +00:00
|
|
|
|
|
|
|
type args struct {
|
|
|
|
digest []byte
|
|
|
|
chain blockchain.ChainInfoFetcher
|
|
|
|
}
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
args args
|
2023-02-09 09:23:32 +00:00
|
|
|
want interfaces.ReadOnlySignedBeaconBlock
|
2021-09-03 16:00:56 +00:00
|
|
|
wantErr bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "no digest",
|
|
|
|
args: args{
|
|
|
|
digest: []byte{},
|
|
|
|
chain: &mock.ChainService{ValidatorsRoot: [32]byte{}},
|
|
|
|
},
|
2022-03-25 23:00:44 +00:00
|
|
|
|
2023-02-09 09:23:32 +00:00
|
|
|
want: func() interfaces.ReadOnlySignedBeaconBlock {
|
2022-08-02 15:30:46 +00:00
|
|
|
wsb, err := blocks.NewSignedBeaconBlock(ðpb.SignedBeaconBlock{Block: ðpb.BeaconBlock{Body: ðpb.BeaconBlockBody{}}})
|
2022-03-25 23:00:44 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
return wsb
|
|
|
|
}(),
|
2021-09-03 16:00:56 +00:00
|
|
|
wantErr: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "invalid digest",
|
|
|
|
args: args{
|
|
|
|
digest: []byte{0x00, 0x01},
|
|
|
|
chain: &mock.ChainService{ValidatorsRoot: [32]byte{}},
|
|
|
|
},
|
|
|
|
want: nil,
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "non existent digest",
|
|
|
|
args: args{
|
|
|
|
digest: []byte{0x00, 0x01, 0x02, 0x03},
|
|
|
|
chain: &mock.ChainService{ValidatorsRoot: [32]byte{}},
|
|
|
|
},
|
|
|
|
want: nil,
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "genesis fork version",
|
|
|
|
args: args{
|
|
|
|
digest: genDigest[:],
|
|
|
|
chain: &mock.ChainService{ValidatorsRoot: [32]byte{}},
|
|
|
|
},
|
2023-02-09 09:23:32 +00:00
|
|
|
want: func() interfaces.ReadOnlySignedBeaconBlock {
|
2022-08-02 15:30:46 +00:00
|
|
|
wsb, err := blocks.NewSignedBeaconBlock(ðpb.SignedBeaconBlock{Block: ðpb.BeaconBlock{Body: ðpb.BeaconBlockBody{}}})
|
2022-03-25 23:00:44 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
return wsb
|
|
|
|
}(),
|
2021-09-03 16:00:56 +00:00
|
|
|
wantErr: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "altair fork version",
|
|
|
|
args: args{
|
|
|
|
digest: altairDigest[:],
|
|
|
|
chain: &mock.ChainService{ValidatorsRoot: [32]byte{}},
|
|
|
|
},
|
2023-02-09 09:23:32 +00:00
|
|
|
want: func() interfaces.ReadOnlySignedBeaconBlock {
|
2022-08-02 15:30:46 +00:00
|
|
|
wsb, err := blocks.NewSignedBeaconBlock(ðpb.SignedBeaconBlockAltair{Block: ðpb.BeaconBlockAltair{Body: ðpb.BeaconBlockBodyAltair{}}})
|
2021-09-03 16:00:56 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
return wsb
|
|
|
|
}(),
|
|
|
|
wantErr: false,
|
|
|
|
},
|
2022-01-28 16:26:52 +00:00
|
|
|
{
|
|
|
|
name: "bellatrix fork version",
|
|
|
|
args: args{
|
|
|
|
digest: bellatrixDigest[:],
|
|
|
|
chain: &mock.ChainService{ValidatorsRoot: [32]byte{}},
|
|
|
|
},
|
2023-02-09 09:23:32 +00:00
|
|
|
want: func() interfaces.ReadOnlySignedBeaconBlock {
|
2023-12-29 21:28:19 +00:00
|
|
|
wsb, err := blocks.NewSignedBeaconBlock(ðpb.SignedBeaconBlockBellatrix{Block: ðpb.BeaconBlockBellatrix{Body: ðpb.BeaconBlockBodyBellatrix{ExecutionPayload: &enginev1.ExecutionPayload{}}}})
|
2022-01-28 16:26:52 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
return wsb
|
|
|
|
}(),
|
|
|
|
wantErr: false,
|
|
|
|
},
|
2021-09-03 16:00:56 +00:00
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
got, err := extractBlockDataType(tt.args.digest, tt.args.chain)
|
|
|
|
if (err != nil) != tt.wantErr {
|
|
|
|
t.Errorf("extractBlockDataType() error = %v, wantErr %v", err, tt.wantErr)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(got, tt.want) {
|
|
|
|
t.Errorf("extractBlockDataType() got = %v, want %v", got, tt.want)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|