Skip to content

Commit

Permalink
Migration package tests (#8524)
Browse files Browse the repository at this point in the history
* first few tests

* rest of tests

* gzl

* test block hydration

* compare roots

* gzl

* compare exit roots

Co-authored-by: prylabs-bulldozer[bot] <58059840+prylabs-bulldozer[bot]@users.noreply.github.com>
  • Loading branch information
rkapka and prylabs-bulldozer[bot] authored Mar 1, 2021
1 parent c30ee6c commit 9547f53
Show file tree
Hide file tree
Showing 5 changed files with 276 additions and 0 deletions.
16 changes: 16 additions & 0 deletions proto/migration/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
load("@io_bazel_rules_go//go:def.bzl", "go_test")
load("@prysm//tools/go:def.bzl", "go_library")

go_library(
Expand All @@ -12,3 +13,18 @@ go_library(
"@com_github_prysmaticlabs_ethereumapis//eth/v1alpha1:go_default_library",
],
)

go_test(
name = "go_default_test",
srcs = ["migration_test.go"],
embed = [":go_default_library"],
deps = [
"//shared/bytesutil:go_default_library",
"//shared/testutil:go_default_library",
"//shared/testutil/assert:go_default_library",
"//shared/testutil/require:go_default_library",
"@com_github_prysmaticlabs_eth2_types//:go_default_library",
"@com_github_prysmaticlabs_ethereumapis//eth/v1:go_default_library",
"@com_github_prysmaticlabs_ethereumapis//eth/v1alpha1:go_default_library",
],
)
198 changes: 198 additions & 0 deletions proto/migration/migration_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,198 @@
package migration

import (
"testing"

types "github.com/prysmaticlabs/eth2-types"
ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1"
ethpb_alpha "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1"
"github.com/prysmaticlabs/prysm/shared/bytesutil"
"github.com/prysmaticlabs/prysm/shared/testutil"
"github.com/prysmaticlabs/prysm/shared/testutil/assert"
"github.com/prysmaticlabs/prysm/shared/testutil/require"
)

var (
slot = types.Slot(1)
epoch = types.Epoch(1)
validatorIndex = types.ValidatorIndex(1)
committeeIndex = types.CommitteeIndex(1)
depositCount = uint64(2)
attestingIndices = []uint64{1, 2}
parentRoot = bytesutil.PadTo([]byte("parentroot"), 32)
stateRoot = bytesutil.PadTo([]byte("stateroot"), 32)
signature = bytesutil.PadTo([]byte("signature"), 96)
randaoReveal = bytesutil.PadTo([]byte("randaoreveal"), 96)
depositRoot = bytesutil.PadTo([]byte("depositroot"), 32)
blockHash = bytesutil.PadTo([]byte("blockhash"), 32)
beaconBlockRoot = bytesutil.PadTo([]byte("beaconblockroot"), 32)
sourceRoot = bytesutil.PadTo([]byte("sourceroot"), 32)
targetRoot = bytesutil.PadTo([]byte("targetroot"), 32)
bodyRoot = bytesutil.PadTo([]byte("bodyroot"), 32)
)

func Test_V1Alpha1BlockToV1BlockHeader(t *testing.T) {
alphaBlock := testutil.HydrateSignedBeaconBlock(&ethpb_alpha.SignedBeaconBlock{})
alphaBlock.Block.Slot = slot
alphaBlock.Block.ProposerIndex = validatorIndex
alphaBlock.Block.ParentRoot = parentRoot
alphaBlock.Block.StateRoot = stateRoot
alphaBlock.Signature = signature

v1Header, err := V1Alpha1BlockToV1BlockHeader(alphaBlock)
require.NoError(t, err)
bodyRoot, err := alphaBlock.Block.Body.HashTreeRoot()
require.NoError(t, err)
assert.DeepEqual(t, bodyRoot[:], v1Header.Header.BodyRoot)
assert.Equal(t, slot, v1Header.Header.Slot)
assert.Equal(t, validatorIndex, v1Header.Header.ProposerIndex)
assert.DeepEqual(t, parentRoot, v1Header.Header.ParentRoot)
assert.DeepEqual(t, stateRoot, v1Header.Header.StateRoot)
assert.DeepEqual(t, signature, v1Header.Signature)
}

func Test_V1Alpha1ToV1Block(t *testing.T) {
alphaBlock := testutil.HydrateSignedBeaconBlock(&ethpb_alpha.SignedBeaconBlock{})
alphaBlock.Block.Slot = slot
alphaBlock.Block.ProposerIndex = validatorIndex
alphaBlock.Block.ParentRoot = parentRoot
alphaBlock.Block.StateRoot = stateRoot
alphaBlock.Block.Body.RandaoReveal = randaoReveal
alphaBlock.Block.Body.Eth1Data = &ethpb_alpha.Eth1Data{
DepositRoot: depositRoot,
DepositCount: depositCount,
BlockHash: blockHash,
}
alphaBlock.Signature = signature

v1Block, err := V1Alpha1ToV1Block(alphaBlock)
require.NoError(t, err)
alphaRoot, err := alphaBlock.HashTreeRoot()
require.NoError(t, err)
v1Root, err := v1Block.HashTreeRoot()
require.NoError(t, err)
assert.DeepEqual(t, alphaRoot, v1Root)
}

func Test_V1ToV1Alpha1Block(t *testing.T) {
v1Block := testutil.HydrateV1SignedBeaconBlock(&ethpb.SignedBeaconBlock{})
v1Block.Block.Slot = slot
v1Block.Block.ProposerIndex = validatorIndex
v1Block.Block.ParentRoot = parentRoot
v1Block.Block.StateRoot = stateRoot
v1Block.Block.Body.RandaoReveal = randaoReveal
v1Block.Block.Body.Eth1Data = &ethpb.Eth1Data{
DepositRoot: depositRoot,
DepositCount: depositCount,
BlockHash: blockHash,
}
v1Block.Signature = signature

alphaBlock, err := V1ToV1Alpha1Block(v1Block)
require.NoError(t, err)
alphaRoot, err := alphaBlock.HashTreeRoot()
require.NoError(t, err)
v1Root, err := v1Block.HashTreeRoot()
require.NoError(t, err)
assert.DeepEqual(t, v1Root, alphaRoot)
}

func Test_V1Alpha1AttSlashingToV1(t *testing.T) {
alphaAttestation := &ethpb_alpha.IndexedAttestation{
AttestingIndices: attestingIndices,
Data: &ethpb_alpha.AttestationData{
Slot: slot,
CommitteeIndex: committeeIndex,
BeaconBlockRoot: beaconBlockRoot,
Source: &ethpb_alpha.Checkpoint{
Epoch: epoch,
Root: sourceRoot,
},
Target: &ethpb_alpha.Checkpoint{
Epoch: epoch,
Root: targetRoot,
},
},
Signature: signature,
}
alphaSlashing := &ethpb_alpha.AttesterSlashing{
Attestation_1: alphaAttestation,
Attestation_2: alphaAttestation,
}

v1Slashing := V1Alpha1AttSlashingToV1(alphaSlashing)
alphaRoot, err := alphaSlashing.HashTreeRoot()
require.NoError(t, err)
v1Root, err := v1Slashing.HashTreeRoot()
require.NoError(t, err)
assert.DeepEqual(t, alphaRoot, v1Root)
}

func Test_V1Alpha1ProposerSlashingToV1(t *testing.T) {
alphaHeader := testutil.HydrateSignedBeaconHeader(&ethpb_alpha.SignedBeaconBlockHeader{})
alphaHeader.Header.Slot = slot
alphaHeader.Header.ProposerIndex = validatorIndex
alphaHeader.Header.ParentRoot = parentRoot
alphaHeader.Header.StateRoot = stateRoot
alphaHeader.Header.BodyRoot = bodyRoot
alphaHeader.Signature = signature
alphaSlashing := &ethpb_alpha.ProposerSlashing{
Header_1: alphaHeader,
Header_2: alphaHeader,
}

v1Slashing := V1Alpha1ProposerSlashingToV1(alphaSlashing)
alphaRoot, err := alphaSlashing.HashTreeRoot()
require.NoError(t, err)
v1Root, err := v1Slashing.HashTreeRoot()
require.NoError(t, err)
assert.DeepEqual(t, alphaRoot, v1Root)
}

func Test_V1Alpha1ExitToV1(t *testing.T) {
alphaExit := &ethpb_alpha.SignedVoluntaryExit{
Exit: &ethpb_alpha.VoluntaryExit{
Epoch: epoch,
ValidatorIndex: validatorIndex,
},
Signature: signature,
}

v1Exit := V1Alpha1ExitToV1(alphaExit)
alphaRoot, err := alphaExit.HashTreeRoot()
require.NoError(t, err)
v1Root, err := v1Exit.HashTreeRoot()
require.NoError(t, err)
assert.DeepEqual(t, alphaRoot, v1Root)
}

func Test_V1AttSlashingToV1Alpha1(t *testing.T) {
v1Attestation := &ethpb.IndexedAttestation{
AttestingIndices: attestingIndices,
Data: &ethpb.AttestationData{
Slot: slot,
CommitteeIndex: committeeIndex,
BeaconBlockRoot: beaconBlockRoot,
Source: &ethpb.Checkpoint{
Epoch: epoch,
Root: sourceRoot,
},
Target: &ethpb.Checkpoint{
Epoch: epoch,
Root: targetRoot,
},
},
Signature: signature,
}
v1Slashing := &ethpb.AttesterSlashing{
Attestation_1: v1Attestation,
Attestation_2: v1Attestation,
}

alphaSlashing := V1AttSlashingToV1Alpha1(v1Slashing)
alphaRoot, err := alphaSlashing.HashTreeRoot()
require.NoError(t, err)
v1Root, err := v1Slashing.HashTreeRoot()
require.NoError(t, err)
assert.DeepEqual(t, v1Root, alphaRoot)
}
2 changes: 2 additions & 0 deletions shared/testutil/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ go_library(
"@com_github_json_iterator_go//:go_default_library",
"@com_github_pkg_errors//:go_default_library",
"@com_github_prysmaticlabs_eth2_types//:go_default_library",
"@com_github_prysmaticlabs_ethereumapis//eth/v1:go_default_library",
"@com_github_prysmaticlabs_ethereumapis//eth/v1alpha1:go_default_library",
"@com_github_prysmaticlabs_go_bitfield//:go_default_library",
"@com_github_sirupsen_logrus//:go_default_library",
Expand Down Expand Up @@ -62,6 +63,7 @@ go_test(
"//shared/testutil/require:go_default_library",
"@com_github_gogo_protobuf//proto:go_default_library",
"@com_github_prysmaticlabs_eth2_types//:go_default_library",
"@com_github_prysmaticlabs_ethereumapis//eth/v1:go_default_library",
"@com_github_prysmaticlabs_ethereumapis//eth/v1alpha1:go_default_library",
],
)
48 changes: 48 additions & 0 deletions shared/testutil/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (

"github.com/pkg/errors"
types "github.com/prysmaticlabs/eth2-types"
v1 "github.com/prysmaticlabs/ethereumapis/eth/v1"
ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1"
"github.com/prysmaticlabs/prysm/beacon-chain/core/helpers"
stateTrie "github.com/prysmaticlabs/prysm/beacon-chain/state"
Expand Down Expand Up @@ -455,3 +456,50 @@ func HydrateBeaconBlockBody(b *ethpb.BeaconBlockBody) *ethpb.BeaconBlockBody {
}
return b
}

// HydrateV1SignedBeaconBlock hydrates a signed beacon block with correct field length sizes
// to comply with fssz marshalling and unmarshalling rules.
func HydrateV1SignedBeaconBlock(b *v1.SignedBeaconBlock) *v1.SignedBeaconBlock {
if b.Signature == nil {
b.Signature = make([]byte, params.BeaconConfig().BLSSignatureLength)
}
b.Block = HydrateV1BeaconBlock(b.Block)
return b
}

// HydrateV1BeaconBlock hydrates a beacon block with correct field length sizes
// to comply with fssz marshalling and unmarshalling rules.
func HydrateV1BeaconBlock(b *v1.BeaconBlock) *v1.BeaconBlock {
if b == nil {
b = &v1.BeaconBlock{}
}
if b.ParentRoot == nil {
b.ParentRoot = make([]byte, 32)
}
if b.StateRoot == nil {
b.StateRoot = make([]byte, 32)
}
b.Body = HydrateV1BeaconBlockBody(b.Body)
return b
}

// HydrateV1BeaconBlockBody hydrates a beacon block body with correct field length sizes
// to comply with fssz marshalling and unmarshalling rules.
func HydrateV1BeaconBlockBody(b *v1.BeaconBlockBody) *v1.BeaconBlockBody {
if b == nil {
b = &v1.BeaconBlockBody{}
}
if b.RandaoReveal == nil {
b.RandaoReveal = make([]byte, params.BeaconConfig().BLSSignatureLength)
}
if b.Graffiti == nil {
b.Graffiti = make([]byte, 32)
}
if b.Eth1Data == nil {
b.Eth1Data = &v1.Eth1Data{
DepositRoot: make([]byte, 32),
BlockHash: make([]byte, 32),
}
}
return b
}
12 changes: 12 additions & 0 deletions shared/testutil/block_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"testing"

types "github.com/prysmaticlabs/eth2-types"
v1 "github.com/prysmaticlabs/ethereumapis/eth/v1"
eth "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1"
"github.com/prysmaticlabs/prysm/beacon-chain/core/helpers"
"github.com/prysmaticlabs/prysm/beacon-chain/core/state"
Expand Down Expand Up @@ -183,3 +184,14 @@ func TestHydrateSignedBeaconBlock_NoError(t *testing.T) {
_, err = b.Block.Body.HashTreeRoot()
require.NoError(t, err)
}

func TestHydrateV1SignedBeaconBlock_NoError(t *testing.T) {
b := &v1.SignedBeaconBlock{}
b = HydrateV1SignedBeaconBlock(b)
_, err := b.HashTreeRoot()
require.NoError(t, err)
_, err = b.Block.HashTreeRoot()
require.NoError(t, err)
_, err = b.Block.Body.HashTreeRoot()
require.NoError(t, err)
}

0 comments on commit 9547f53

Please sign in to comment.