2020-06-23 16:46:48 +00:00
|
|
|
package blockutil
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
eth "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1"
|
|
|
|
"github.com/prysmaticlabs/prysm/beacon-chain/state/stateutil"
|
|
|
|
"github.com/prysmaticlabs/prysm/shared/bytesutil"
|
|
|
|
"github.com/prysmaticlabs/prysm/shared/params"
|
2020-07-18 16:31:42 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/shared/testutil/assert"
|
|
|
|
"github.com/prysmaticlabs/prysm/shared/testutil/require"
|
2020-06-23 16:46:48 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
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 := stateutil.BlockBodyRoot(blk.Body)
|
2020-07-18 16:31:42 +00:00
|
|
|
require.NoError(t, err)
|
2020-06-23 16:46:48 +00:00
|
|
|
want := ð.BeaconBlockHeader{
|
|
|
|
Slot: blk.Slot,
|
|
|
|
ProposerIndex: blk.ProposerIndex,
|
|
|
|
ParentRoot: blk.ParentRoot,
|
|
|
|
StateRoot: blk.StateRoot,
|
|
|
|
BodyRoot: bodyRoot[:],
|
|
|
|
}
|
|
|
|
|
|
|
|
bh, err := BeaconBlockHeaderFromBlock(blk)
|
2020-07-18 16:31:42 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
assert.DeepEqual(t, want, bh)
|
2020-06-23 16:46:48 +00:00
|
|
|
}
|
|
|
|
|
2020-12-08 22:27:58 +00:00
|
|
|
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 := BeaconBlockHeaderFromBlock(blk)
|
|
|
|
require.ErrorContains(t, "nil block body", err)
|
|
|
|
}
|
|
|
|
|
2020-06-23 16:46:48 +00:00
|
|
|
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 := stateutil.BlockBodyRoot(blk.Block.Body)
|
2020-07-18 16:31:42 +00:00
|
|
|
require.NoError(t, err)
|
2020-06-23 16:46:48 +00:00
|
|
|
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 := SignedBeaconBlockHeaderFromBlock(blk)
|
2020-07-18 16:31:42 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
assert.DeepEqual(t, want, bh)
|
2020-06-23 16:46:48 +00:00
|
|
|
}
|
2020-12-08 22:27:58 +00:00
|
|
|
|
|
|
|
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 := SignedBeaconBlockHeaderFromBlock(blk)
|
|
|
|
require.ErrorContains(t, "nil block", err)
|
|
|
|
}
|