mirror of
https://gitlab.com/pulsechaincom/prysm-pulse.git
synced 2024-12-23 11:57:18 +00:00
2ba29a3cfc
* Add block and attestation to container pkg * Move aggregation into attestation * Update attestation_test.go * Move them to proto * Gazelle * fix cycle Co-authored-by: prylabs-bulldozer[bot] <58059840+prylabs-bulldozer[bot]@users.noreply.github.com> Co-authored-by: Raul Jordan <raul@prysmaticlabs.com>
196 lines
6.8 KiB
Go
196 lines
6.8 KiB
Go
package block_test
|
|
|
|
import (
|
|
"testing"
|
|
|
|
eth "github.com/prysmaticlabs/prysm/proto/prysm/v1alpha1"
|
|
"github.com/prysmaticlabs/prysm/proto/prysm/v1alpha1/block"
|
|
"github.com/prysmaticlabs/prysm/proto/prysm/v1alpha1/wrapper"
|
|
"github.com/prysmaticlabs/prysm/shared/bytesutil"
|
|
"github.com/prysmaticlabs/prysm/shared/params"
|
|
"github.com/prysmaticlabs/prysm/shared/testutil/assert"
|
|
"github.com/prysmaticlabs/prysm/shared/testutil/require"
|
|
)
|
|
|
|
func TestBeaconBlockHeaderFromBlock(t *testing.T) {
|
|
hashLen := 32
|
|
blk := ð.BeaconBlock{
|
|
Slot: 200,
|
|
ProposerIndex: 2,
|
|
ParentRoot: bytesutil.PadTo([]byte("parent root"), hashLen),
|
|
StateRoot: bytesutil.PadTo([]byte("state root"), hashLen),
|
|
Body: ð.BeaconBlockBody{
|
|
Eth1Data: ð.Eth1Data{
|
|
BlockHash: bytesutil.PadTo([]byte("block hash"), hashLen),
|
|
DepositRoot: bytesutil.PadTo([]byte("deposit root"), hashLen),
|
|
DepositCount: 1,
|
|
},
|
|
RandaoReveal: bytesutil.PadTo([]byte("randao"), params.BeaconConfig().BLSSignatureLength),
|
|
Graffiti: bytesutil.PadTo([]byte("teehee"), hashLen),
|
|
ProposerSlashings: []*eth.ProposerSlashing{},
|
|
AttesterSlashings: []*eth.AttesterSlashing{},
|
|
Attestations: []*eth.Attestation{},
|
|
Deposits: []*eth.Deposit{},
|
|
VoluntaryExits: []*eth.SignedVoluntaryExit{},
|
|
},
|
|
}
|
|
bodyRoot, err := blk.Body.HashTreeRoot()
|
|
require.NoError(t, err)
|
|
want := ð.BeaconBlockHeader{
|
|
Slot: blk.Slot,
|
|
ProposerIndex: blk.ProposerIndex,
|
|
ParentRoot: blk.ParentRoot,
|
|
StateRoot: blk.StateRoot,
|
|
BodyRoot: bodyRoot[:],
|
|
}
|
|
|
|
bh, err := block.BeaconBlockHeaderFromBlock(blk)
|
|
require.NoError(t, err)
|
|
assert.DeepEqual(t, want, bh)
|
|
}
|
|
|
|
func TestBeaconBlockHeaderFromBlockInterface(t *testing.T) {
|
|
hashLen := 32
|
|
blk := ð.BeaconBlock{
|
|
Slot: 200,
|
|
ProposerIndex: 2,
|
|
ParentRoot: bytesutil.PadTo([]byte("parent root"), hashLen),
|
|
StateRoot: bytesutil.PadTo([]byte("state root"), hashLen),
|
|
Body: ð.BeaconBlockBody{
|
|
Eth1Data: ð.Eth1Data{
|
|
BlockHash: bytesutil.PadTo([]byte("block hash"), hashLen),
|
|
DepositRoot: bytesutil.PadTo([]byte("deposit root"), hashLen),
|
|
DepositCount: 1,
|
|
},
|
|
RandaoReveal: bytesutil.PadTo([]byte("randao"), params.BeaconConfig().BLSSignatureLength),
|
|
Graffiti: bytesutil.PadTo([]byte("teehee"), hashLen),
|
|
ProposerSlashings: []*eth.ProposerSlashing{},
|
|
AttesterSlashings: []*eth.AttesterSlashing{},
|
|
Attestations: []*eth.Attestation{},
|
|
Deposits: []*eth.Deposit{},
|
|
VoluntaryExits: []*eth.SignedVoluntaryExit{},
|
|
},
|
|
}
|
|
bodyRoot, err := blk.Body.HashTreeRoot()
|
|
require.NoError(t, err)
|
|
want := ð.BeaconBlockHeader{
|
|
Slot: blk.Slot,
|
|
ProposerIndex: blk.ProposerIndex,
|
|
ParentRoot: blk.ParentRoot,
|
|
StateRoot: blk.StateRoot,
|
|
BodyRoot: bodyRoot[:],
|
|
}
|
|
|
|
bh, err := block.BeaconBlockHeaderFromBlockInterface(wrapper.WrappedPhase0BeaconBlock(blk))
|
|
require.NoError(t, err)
|
|
assert.DeepEqual(t, want, bh)
|
|
}
|
|
|
|
func TestBeaconBlockHeaderFromBlock_NilBlockBody(t *testing.T) {
|
|
hashLen := 32
|
|
blk := ð.BeaconBlock{
|
|
Slot: 200,
|
|
ProposerIndex: 2,
|
|
ParentRoot: bytesutil.PadTo([]byte("parent root"), hashLen),
|
|
StateRoot: bytesutil.PadTo([]byte("state root"), hashLen),
|
|
}
|
|
_, err := block.BeaconBlockHeaderFromBlock(blk)
|
|
require.ErrorContains(t, "nil block body", err)
|
|
}
|
|
|
|
func TestSignedBeaconBlockHeaderFromBlock(t *testing.T) {
|
|
hashLen := 32
|
|
blk := ð.SignedBeaconBlock{Block: ð.BeaconBlock{
|
|
Slot: 200,
|
|
ProposerIndex: 2,
|
|
ParentRoot: bytesutil.PadTo([]byte("parent root"), hashLen),
|
|
StateRoot: bytesutil.PadTo([]byte("state root"), hashLen),
|
|
Body: ð.BeaconBlockBody{
|
|
Eth1Data: ð.Eth1Data{
|
|
BlockHash: bytesutil.PadTo([]byte("block hash"), hashLen),
|
|
DepositRoot: bytesutil.PadTo([]byte("deposit root"), hashLen),
|
|
DepositCount: 1,
|
|
},
|
|
RandaoReveal: bytesutil.PadTo([]byte("randao"), params.BeaconConfig().BLSSignatureLength),
|
|
Graffiti: bytesutil.PadTo([]byte("teehee"), hashLen),
|
|
ProposerSlashings: []*eth.ProposerSlashing{},
|
|
AttesterSlashings: []*eth.AttesterSlashing{},
|
|
Attestations: []*eth.Attestation{},
|
|
Deposits: []*eth.Deposit{},
|
|
VoluntaryExits: []*eth.SignedVoluntaryExit{},
|
|
},
|
|
},
|
|
Signature: bytesutil.PadTo([]byte("signature"), params.BeaconConfig().BLSSignatureLength),
|
|
}
|
|
bodyRoot, err := blk.Block.Body.HashTreeRoot()
|
|
require.NoError(t, err)
|
|
want := ð.SignedBeaconBlockHeader{Header: ð.BeaconBlockHeader{
|
|
Slot: blk.Block.Slot,
|
|
ProposerIndex: blk.Block.ProposerIndex,
|
|
ParentRoot: blk.Block.ParentRoot,
|
|
StateRoot: blk.Block.StateRoot,
|
|
BodyRoot: bodyRoot[:],
|
|
},
|
|
Signature: blk.Signature,
|
|
}
|
|
|
|
bh, err := block.SignedBeaconBlockHeaderFromBlock(blk)
|
|
require.NoError(t, err)
|
|
assert.DeepEqual(t, want, bh)
|
|
}
|
|
|
|
func TestSignedBeaconBlockHeaderFromBlockInterface(t *testing.T) {
|
|
hashLen := 32
|
|
blk := ð.SignedBeaconBlock{Block: ð.BeaconBlock{
|
|
Slot: 200,
|
|
ProposerIndex: 2,
|
|
ParentRoot: bytesutil.PadTo([]byte("parent root"), hashLen),
|
|
StateRoot: bytesutil.PadTo([]byte("state root"), hashLen),
|
|
Body: ð.BeaconBlockBody{
|
|
Eth1Data: ð.Eth1Data{
|
|
BlockHash: bytesutil.PadTo([]byte("block hash"), hashLen),
|
|
DepositRoot: bytesutil.PadTo([]byte("deposit root"), hashLen),
|
|
DepositCount: 1,
|
|
},
|
|
RandaoReveal: bytesutil.PadTo([]byte("randao"), params.BeaconConfig().BLSSignatureLength),
|
|
Graffiti: bytesutil.PadTo([]byte("teehee"), hashLen),
|
|
ProposerSlashings: []*eth.ProposerSlashing{},
|
|
AttesterSlashings: []*eth.AttesterSlashing{},
|
|
Attestations: []*eth.Attestation{},
|
|
Deposits: []*eth.Deposit{},
|
|
VoluntaryExits: []*eth.SignedVoluntaryExit{},
|
|
},
|
|
},
|
|
Signature: bytesutil.PadTo([]byte("signature"), params.BeaconConfig().BLSSignatureLength),
|
|
}
|
|
bodyRoot, err := blk.Block.Body.HashTreeRoot()
|
|
require.NoError(t, err)
|
|
want := ð.SignedBeaconBlockHeader{Header: ð.BeaconBlockHeader{
|
|
Slot: blk.Block.Slot,
|
|
ProposerIndex: blk.Block.ProposerIndex,
|
|
ParentRoot: blk.Block.ParentRoot,
|
|
StateRoot: blk.Block.StateRoot,
|
|
BodyRoot: bodyRoot[:],
|
|
},
|
|
Signature: blk.Signature,
|
|
}
|
|
|
|
bh, err := block.SignedBeaconBlockHeaderFromBlockInterface(wrapper.WrappedPhase0SignedBeaconBlock(blk))
|
|
require.NoError(t, err)
|
|
assert.DeepEqual(t, want, bh)
|
|
}
|
|
|
|
func TestSignedBeaconBlockHeaderFromBlock_NilBlockBody(t *testing.T) {
|
|
hashLen := 32
|
|
blk := ð.SignedBeaconBlock{Block: ð.BeaconBlock{
|
|
Slot: 200,
|
|
ProposerIndex: 2,
|
|
ParentRoot: bytesutil.PadTo([]byte("parent root"), hashLen),
|
|
StateRoot: bytesutil.PadTo([]byte("state root"), hashLen),
|
|
},
|
|
Signature: bytesutil.PadTo([]byte("signature"), params.BeaconConfig().BLSSignatureLength),
|
|
}
|
|
_, err := block.SignedBeaconBlockHeaderFromBlock(blk)
|
|
require.ErrorContains(t, "nil block", err)
|
|
}
|