prysm-pulse/proto/migration/v1alpha1_to_v1_test.go
Radosław Kapka 89941d4be2
Implement GetBlockV2 in the beacon API (#9433)
* Implement `GetBlockV2` in the beacon API

* fix gateway config test

* Revert "fix gateway config test"

This reverts commit 8179400b2ac7cd302c1bca0f191145d017838f5d.

* unregister v2

* review feedback

* improve comment

* reduce duplication

Co-authored-by: Raul Jordan <raul@prysmaticlabs.com>
2021-08-26 15:22:06 +00:00

486 lines
16 KiB
Go

package migration
import (
"testing"
types "github.com/prysmaticlabs/eth2-types"
"github.com/prysmaticlabs/go-bitfield"
ethpbv1 "github.com/prysmaticlabs/prysm/proto/eth/v1"
ethpbalpha "github.com/prysmaticlabs/prysm/proto/prysm/v1alpha1"
"github.com/prysmaticlabs/prysm/proto/prysm/v1alpha1/wrapper"
"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)
selectionProof = bytesutil.PadTo([]byte("selectionproof"), 96)
aggregationBits = bitfield.Bitlist{0x01}
)
func Test_BlockIfaceToV1BlockHeader(t *testing.T) {
alphaBlock := testutil.HydrateSignedBeaconBlock(&ethpbalpha.SignedBeaconBlock{})
alphaBlock.Block.Slot = slot
alphaBlock.Block.ProposerIndex = validatorIndex
alphaBlock.Block.ParentRoot = parentRoot
alphaBlock.Block.StateRoot = stateRoot
alphaBlock.Signature = signature
v1Header, err := BlockIfaceToV1BlockHeader(wrapper.WrappedPhase0SignedBeaconBlock(alphaBlock))
require.NoError(t, err)
bodyRoot, err := alphaBlock.Block.Body.HashTreeRoot()
require.NoError(t, err)
assert.DeepEqual(t, bodyRoot[:], v1Header.Message.BodyRoot)
assert.Equal(t, slot, v1Header.Message.Slot)
assert.Equal(t, validatorIndex, v1Header.Message.ProposerIndex)
assert.DeepEqual(t, parentRoot, v1Header.Message.ParentRoot)
assert.DeepEqual(t, stateRoot, v1Header.Message.StateRoot)
assert.DeepEqual(t, signature, v1Header.Signature)
}
func Test_V1Alpha1AggregateAttAndProofToV1(t *testing.T) {
proof := [32]byte{1}
att := testutil.HydrateAttestation(&ethpbalpha.Attestation{
Data: &ethpbalpha.AttestationData{
Slot: 5,
},
})
alpha := &ethpbalpha.AggregateAttestationAndProof{
AggregatorIndex: 1,
Aggregate: att,
SelectionProof: proof[:],
}
v1 := V1Alpha1AggregateAttAndProofToV1(alpha)
assert.Equal(t, v1.AggregatorIndex, types.ValidatorIndex(1))
assert.DeepSSZEqual(t, v1.Aggregate.Data.Slot, att.Data.Slot)
assert.DeepEqual(t, v1.SelectionProof, proof[:])
}
func Test_V1Alpha1BlockToV1BlockHeader(t *testing.T) {
alphaBlock := testutil.HydrateSignedBeaconBlock(&ethpbalpha.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.Message.BodyRoot)
assert.Equal(t, slot, v1Header.Message.Slot)
assert.Equal(t, validatorIndex, v1Header.Message.ProposerIndex)
assert.DeepEqual(t, parentRoot, v1Header.Message.ParentRoot)
assert.DeepEqual(t, stateRoot, v1Header.Message.StateRoot)
assert.DeepEqual(t, signature, v1Header.Signature)
}
func Test_V1Alpha1ToV1SignedBlock(t *testing.T) {
alphaBlock := testutil.HydrateSignedBeaconBlock(&ethpbalpha.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 = &ethpbalpha.Eth1Data{
DepositRoot: depositRoot,
DepositCount: depositCount,
BlockHash: blockHash,
}
alphaBlock.Signature = signature
v1Block, err := V1Alpha1ToV1SignedBlock(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_V1ToV1Alpha1SignedBlock(t *testing.T) {
v1Block := testutil.HydrateV1SignedBeaconBlock(&ethpbv1.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 = &ethpbv1.Eth1Data{
DepositRoot: depositRoot,
DepositCount: depositCount,
BlockHash: blockHash,
}
v1Block.Signature = signature
alphaBlock, err := V1ToV1Alpha1SignedBlock(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_V1ToV1Alpha1Block(t *testing.T) {
alphaBlock := testutil.HydrateBeaconBlock(&ethpbalpha.BeaconBlock{})
alphaBlock.Slot = slot
alphaBlock.ProposerIndex = validatorIndex
alphaBlock.ParentRoot = parentRoot
alphaBlock.StateRoot = stateRoot
alphaBlock.Body.RandaoReveal = randaoReveal
alphaBlock.Body.Eth1Data = &ethpbalpha.Eth1Data{
DepositRoot: depositRoot,
DepositCount: depositCount,
BlockHash: blockHash,
}
v1Block, err := V1Alpha1ToV1Block(alphaBlock)
require.NoError(t, err)
v1Root, err := v1Block.HashTreeRoot()
require.NoError(t, err)
alphaRoot, err := alphaBlock.HashTreeRoot()
require.NoError(t, err)
assert.DeepEqual(t, alphaRoot, v1Root)
}
func Test_V1Alpha1AttSlashingToV1(t *testing.T) {
alphaAttestation := &ethpbalpha.IndexedAttestation{
AttestingIndices: attestingIndices,
Data: &ethpbalpha.AttestationData{
Slot: slot,
CommitteeIndex: committeeIndex,
BeaconBlockRoot: beaconBlockRoot,
Source: &ethpbalpha.Checkpoint{
Epoch: epoch,
Root: sourceRoot,
},
Target: &ethpbalpha.Checkpoint{
Epoch: epoch,
Root: targetRoot,
},
},
Signature: signature,
}
alphaSlashing := &ethpbalpha.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(&ethpbalpha.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 := &ethpbalpha.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 := &ethpbalpha.SignedVoluntaryExit{
Exit: &ethpbalpha.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_V1ExitToV1Alpha1(t *testing.T) {
v1Exit := &ethpbv1.SignedVoluntaryExit{
Message: &ethpbv1.VoluntaryExit{
Epoch: epoch,
ValidatorIndex: validatorIndex,
},
Signature: signature,
}
alphaExit := V1ExitToV1Alpha1(v1Exit)
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 := &ethpbv1.IndexedAttestation{
AttestingIndices: attestingIndices,
Data: &ethpbv1.AttestationData{
Slot: slot,
Index: committeeIndex,
BeaconBlockRoot: beaconBlockRoot,
Source: &ethpbv1.Checkpoint{
Epoch: epoch,
Root: sourceRoot,
},
Target: &ethpbv1.Checkpoint{
Epoch: epoch,
Root: targetRoot,
},
},
Signature: signature,
}
v1Slashing := &ethpbv1.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)
}
func Test_V1ProposerSlashingToV1Alpha1(t *testing.T) {
v1Header := &ethpbv1.SignedBeaconBlockHeader{
Message: &ethpbv1.BeaconBlockHeader{
Slot: slot,
ProposerIndex: validatorIndex,
ParentRoot: parentRoot,
StateRoot: stateRoot,
BodyRoot: bodyRoot,
},
Signature: signature,
}
v1Slashing := &ethpbv1.ProposerSlashing{
SignedHeader_1: v1Header,
SignedHeader_2: v1Header,
}
alphaSlashing := V1ProposerSlashingToV1Alpha1(v1Slashing)
alphaRoot, err := alphaSlashing.HashTreeRoot()
require.NoError(t, err)
v1Root, err := v1Slashing.HashTreeRoot()
require.NoError(t, err)
assert.DeepEqual(t, alphaRoot, v1Root)
}
func Test_V1Alpha1AttToV1(t *testing.T) {
alphaAtt := &ethpbalpha.Attestation{
AggregationBits: aggregationBits,
Data: &ethpbalpha.AttestationData{
Slot: slot,
CommitteeIndex: committeeIndex,
BeaconBlockRoot: beaconBlockRoot,
Source: &ethpbalpha.Checkpoint{
Epoch: epoch,
Root: sourceRoot,
},
Target: &ethpbalpha.Checkpoint{
Epoch: epoch,
Root: targetRoot,
},
},
Signature: signature,
}
v1Att := V1Alpha1AttestationToV1(alphaAtt)
v1Root, err := v1Att.HashTreeRoot()
require.NoError(t, err)
alphaRoot, err := alphaAtt.HashTreeRoot()
require.NoError(t, err)
assert.DeepEqual(t, v1Root, alphaRoot)
}
func Test_V1AttToV1Alpha1(t *testing.T) {
v1Att := &ethpbv1.Attestation{
AggregationBits: aggregationBits,
Data: &ethpbv1.AttestationData{
Slot: slot,
Index: committeeIndex,
BeaconBlockRoot: beaconBlockRoot,
Source: &ethpbv1.Checkpoint{
Epoch: epoch,
Root: sourceRoot,
},
Target: &ethpbv1.Checkpoint{
Epoch: epoch,
Root: targetRoot,
},
},
Signature: signature,
}
alphaAtt := V1AttToV1Alpha1(v1Att)
alphaRoot, err := alphaAtt.HashTreeRoot()
require.NoError(t, err)
v1Root, err := v1Att.HashTreeRoot()
require.NoError(t, err)
assert.DeepEqual(t, v1Root, alphaRoot)
}
func Test_BlockInterfaceToV1Block(t *testing.T) {
v1Alpha1Block := testutil.HydrateSignedBeaconBlock(&ethpbalpha.SignedBeaconBlock{})
v1Alpha1Block.Block.Slot = slot
v1Alpha1Block.Block.ProposerIndex = validatorIndex
v1Alpha1Block.Block.ParentRoot = parentRoot
v1Alpha1Block.Block.StateRoot = stateRoot
v1Alpha1Block.Block.Body.RandaoReveal = randaoReveal
v1Alpha1Block.Block.Body.Eth1Data = &ethpbalpha.Eth1Data{
DepositRoot: depositRoot,
DepositCount: depositCount,
BlockHash: blockHash,
}
v1Alpha1Block.Signature = signature
v1Block, err := SignedBeaconBlock(wrapper.WrappedPhase0SignedBeaconBlock(v1Alpha1Block))
require.NoError(t, err)
v1Root, err := v1Block.HashTreeRoot()
require.NoError(t, err)
v1Alpha1Root, err := v1Alpha1Block.HashTreeRoot()
require.NoError(t, err)
assert.DeepEqual(t, v1Root, v1Alpha1Root)
}
func Test_V1Alpha1ValidatorToV1(t *testing.T) {
v1Alpha1Validator := &ethpbalpha.Validator{
PublicKey: []byte("pubkey"),
WithdrawalCredentials: []byte("withdraw"),
EffectiveBalance: 99,
Slashed: true,
ActivationEligibilityEpoch: 1,
ActivationEpoch: 11,
ExitEpoch: 111,
WithdrawableEpoch: 1111,
}
v1Validator := V1Alpha1ValidatorToV1(v1Alpha1Validator)
require.NotNil(t, v1Validator)
assert.DeepEqual(t, []byte("pubkey"), v1Validator.Pubkey)
assert.DeepEqual(t, []byte("withdraw"), v1Validator.WithdrawalCredentials)
assert.Equal(t, uint64(99), v1Validator.EffectiveBalance)
assert.Equal(t, true, v1Validator.Slashed)
assert.Equal(t, types.Epoch(1), v1Validator.ActivationEligibilityEpoch)
assert.Equal(t, types.Epoch(11), v1Validator.ActivationEpoch)
assert.Equal(t, types.Epoch(111), v1Validator.ExitEpoch)
assert.Equal(t, types.Epoch(1111), v1Validator.WithdrawableEpoch)
}
func Test_V1ValidatorToV1Alpha1(t *testing.T) {
v1Validator := &ethpbv1.Validator{
Pubkey: []byte("pubkey"),
WithdrawalCredentials: []byte("withdraw"),
EffectiveBalance: 99,
Slashed: true,
ActivationEligibilityEpoch: 1,
ActivationEpoch: 11,
ExitEpoch: 111,
WithdrawableEpoch: 1111,
}
v1Alpha1Validator := V1ValidatorToV1Alpha1(v1Validator)
require.NotNil(t, v1Alpha1Validator)
assert.DeepEqual(t, []byte("pubkey"), v1Alpha1Validator.PublicKey)
assert.DeepEqual(t, []byte("withdraw"), v1Alpha1Validator.WithdrawalCredentials)
assert.Equal(t, uint64(99), v1Alpha1Validator.EffectiveBalance)
assert.Equal(t, true, v1Alpha1Validator.Slashed)
assert.Equal(t, types.Epoch(1), v1Alpha1Validator.ActivationEligibilityEpoch)
assert.Equal(t, types.Epoch(11), v1Alpha1Validator.ActivationEpoch)
assert.Equal(t, types.Epoch(111), v1Alpha1Validator.ExitEpoch)
assert.Equal(t, types.Epoch(1111), v1Alpha1Validator.WithdrawableEpoch)
}
func Test_V1SignedAggregateAttAndProofToV1Alpha1(t *testing.T) {
v1Att := &ethpbv1.SignedAggregateAttestationAndProof{
Message: &ethpbv1.AggregateAttestationAndProof{
AggregatorIndex: 1,
Aggregate: testutil.HydrateV1Attestation(&ethpbv1.Attestation{}),
SelectionProof: selectionProof,
},
Signature: signature,
}
v1Alpha1Att := V1SignedAggregateAttAndProofToV1Alpha1(v1Att)
v1Root, err := v1Att.HashTreeRoot()
require.NoError(t, err)
v1Alpha1Root, err := v1Alpha1Att.HashTreeRoot()
require.NoError(t, err)
assert.DeepEqual(t, v1Root, v1Alpha1Root)
}
func Test_V1AttestationToV1Alpha1(t *testing.T) {
v1Att := testutil.HydrateV1Attestation(&ethpbv1.Attestation{})
v1Alpha1Att := V1AttToV1Alpha1(v1Att)
v1Root, err := v1Att.HashTreeRoot()
require.NoError(t, err)
v1Alpha1Root, err := v1Alpha1Att.HashTreeRoot()
require.NoError(t, err)
assert.DeepEqual(t, v1Root, v1Alpha1Root)
}
func Test_V1Alpha1BeaconBlockAltairToV2(t *testing.T) {
alphaBlock := testutil.HydrateBeaconBlockAltair(&ethpbalpha.BeaconBlockAltair{})
alphaBlock.Slot = slot
alphaBlock.ProposerIndex = validatorIndex
alphaBlock.ParentRoot = parentRoot
alphaBlock.StateRoot = stateRoot
alphaBlock.Body.RandaoReveal = randaoReveal
alphaBlock.Body.Eth1Data = &ethpbalpha.Eth1Data{
DepositRoot: depositRoot,
DepositCount: depositCount,
BlockHash: blockHash,
}
syncCommitteeBits := bitfield.NewBitvector512()
syncCommitteeBits.SetBitAt(100, true)
alphaBlock.Body.SyncAggregate = &ethpbalpha.SyncAggregate{
SyncCommitteeBits: syncCommitteeBits,
SyncCommitteeSignature: signature,
}
v2Block, err := V1Alpha1BeaconBlockAltairToV2(alphaBlock)
require.NoError(t, err)
alphaRoot, err := alphaBlock.HashTreeRoot()
require.NoError(t, err)
v2Root, err := v2Block.HashTreeRoot()
require.NoError(t, err)
assert.DeepEqual(t, alphaRoot, v2Root)
}