2021-08-09 21:27:51 +00:00
|
|
|
package altair_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/prysmaticlabs/go-bitfield"
|
|
|
|
"github.com/prysmaticlabs/prysm/beacon-chain/core/altair"
|
|
|
|
"github.com/prysmaticlabs/prysm/beacon-chain/core/helpers"
|
2021-09-30 19:00:14 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/beacon-chain/core/time"
|
2021-09-21 19:59:25 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/config/params"
|
2022-04-29 14:32:11 +00:00
|
|
|
types "github.com/prysmaticlabs/prysm/consensus-types/primitives"
|
2021-08-09 21:27:51 +00:00
|
|
|
ethpb "github.com/prysmaticlabs/prysm/proto/prysm/v1alpha1"
|
2021-09-20 16:17:03 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/proto/prysm/v1alpha1/attestation"
|
2021-09-23 18:53:46 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/testing/require"
|
|
|
|
"github.com/prysmaticlabs/prysm/testing/util"
|
2021-08-09 21:27:51 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestTranslateParticipation(t *testing.T) {
|
2021-09-26 15:27:57 +00:00
|
|
|
ctx := context.Background()
|
2021-09-23 18:53:46 +00:00
|
|
|
s, _ := util.DeterministicGenesisStateAltair(t, 64)
|
2022-02-08 09:30:06 +00:00
|
|
|
require.NoError(t, s.SetSlot(s.Slot()+params.BeaconConfig().MinAttestationInclusionDelay))
|
2021-08-09 21:27:51 +00:00
|
|
|
|
|
|
|
var err error
|
2022-02-08 09:30:06 +00:00
|
|
|
newState, err := altair.TranslateParticipation(ctx, s, nil)
|
2021-08-09 21:27:51 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
participation, err := newState.PreviousEpochParticipation()
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.DeepSSZEqual(t, make([]byte, 64), participation)
|
|
|
|
|
|
|
|
aggBits := bitfield.NewBitlist(2)
|
|
|
|
aggBits.SetBitAt(0, true)
|
|
|
|
aggBits.SetBitAt(1, true)
|
|
|
|
r, err := helpers.BlockRootAtSlot(s, 0)
|
|
|
|
require.NoError(t, err)
|
|
|
|
var pendingAtts []*ethpb.PendingAttestation
|
|
|
|
for i := 0; i < 3; i++ {
|
|
|
|
pendingAtts = append(pendingAtts, ðpb.PendingAttestation{
|
|
|
|
Data: ðpb.AttestationData{
|
|
|
|
CommitteeIndex: types.CommitteeIndex(i),
|
|
|
|
BeaconBlockRoot: r,
|
|
|
|
Source: ðpb.Checkpoint{Epoch: 0, Root: make([]byte, 32)},
|
|
|
|
Target: ðpb.Checkpoint{Epoch: 0, Root: make([]byte, 32)},
|
|
|
|
},
|
|
|
|
AggregationBits: aggBits,
|
|
|
|
InclusionDelay: 1,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-09-26 15:27:57 +00:00
|
|
|
newState, err = altair.TranslateParticipation(ctx, newState, pendingAtts)
|
2021-08-09 21:27:51 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
participation, err = newState.PreviousEpochParticipation()
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.DeepNotSSZEqual(t, make([]byte, 64), participation)
|
|
|
|
|
2022-02-08 09:30:06 +00:00
|
|
|
committee, err := helpers.BeaconCommitteeFromState(ctx, s, pendingAtts[0].Data.Slot, pendingAtts[0].Data.CommitteeIndex)
|
2021-08-09 21:27:51 +00:00
|
|
|
require.NoError(t, err)
|
2021-09-20 16:17:03 +00:00
|
|
|
indices, err := attestation.AttestingIndices(pendingAtts[0].AggregationBits, committee)
|
2021-08-09 21:27:51 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
for _, index := range indices {
|
2021-10-21 15:01:09 +00:00
|
|
|
has, err := altair.HasValidatorFlag(participation[index], params.BeaconConfig().TimelySourceFlagIndex)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, true, has)
|
|
|
|
has, err = altair.HasValidatorFlag(participation[index], params.BeaconConfig().TimelyTargetFlagIndex)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, true, has)
|
|
|
|
has, err = altair.HasValidatorFlag(participation[index], params.BeaconConfig().TimelyHeadFlagIndex)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, true, has)
|
2021-08-09 21:27:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestUpgradeToAltair(t *testing.T) {
|
2021-09-23 18:53:46 +00:00
|
|
|
st, _ := util.DeterministicGenesisState(t, params.BeaconConfig().MaxValidatorsPerCommittee)
|
2021-08-09 21:27:51 +00:00
|
|
|
preForkState := st.Copy()
|
|
|
|
aState, err := altair.UpgradeToAltair(context.Background(), st)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
require.Equal(t, preForkState.GenesisTime(), aState.GenesisTime())
|
2022-02-14 13:34:38 +00:00
|
|
|
require.DeepSSZEqual(t, preForkState.GenesisValidatorsRoot(), aState.GenesisValidatorsRoot())
|
2021-08-09 21:27:51 +00:00
|
|
|
require.Equal(t, preForkState.Slot(), aState.Slot())
|
|
|
|
require.DeepSSZEqual(t, preForkState.LatestBlockHeader(), aState.LatestBlockHeader())
|
|
|
|
require.DeepSSZEqual(t, preForkState.BlockRoots(), aState.BlockRoots())
|
|
|
|
require.DeepSSZEqual(t, preForkState.StateRoots(), aState.StateRoots())
|
|
|
|
require.DeepSSZEqual(t, preForkState.HistoricalRoots(), aState.HistoricalRoots())
|
|
|
|
require.DeepSSZEqual(t, preForkState.Eth1Data(), aState.Eth1Data())
|
|
|
|
require.DeepSSZEqual(t, preForkState.Eth1DataVotes(), aState.Eth1DataVotes())
|
|
|
|
require.DeepSSZEqual(t, preForkState.Eth1DepositIndex(), aState.Eth1DepositIndex())
|
|
|
|
require.DeepSSZEqual(t, preForkState.Validators(), aState.Validators())
|
|
|
|
require.DeepSSZEqual(t, preForkState.Balances(), aState.Balances())
|
|
|
|
require.DeepSSZEqual(t, preForkState.RandaoMixes(), aState.RandaoMixes())
|
|
|
|
require.DeepSSZEqual(t, preForkState.Slashings(), aState.Slashings())
|
|
|
|
require.DeepSSZEqual(t, preForkState.JustificationBits(), aState.JustificationBits())
|
|
|
|
require.DeepSSZEqual(t, preForkState.PreviousJustifiedCheckpoint(), aState.PreviousJustifiedCheckpoint())
|
|
|
|
require.DeepSSZEqual(t, preForkState.CurrentJustifiedCheckpoint(), aState.CurrentJustifiedCheckpoint())
|
|
|
|
require.DeepSSZEqual(t, preForkState.FinalizedCheckpoint(), aState.FinalizedCheckpoint())
|
|
|
|
numValidators := aState.NumValidators()
|
|
|
|
p, err := aState.PreviousEpochParticipation()
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.DeepSSZEqual(t, make([]byte, numValidators), p)
|
|
|
|
p, err = aState.CurrentEpochParticipation()
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.DeepSSZEqual(t, make([]byte, numValidators), p)
|
|
|
|
s, err := aState.InactivityScores()
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.DeepSSZEqual(t, make([]uint64, numValidators), s)
|
|
|
|
|
|
|
|
f := aState.Fork()
|
|
|
|
require.DeepSSZEqual(t, ðpb.Fork{
|
|
|
|
PreviousVersion: st.Fork().CurrentVersion,
|
|
|
|
CurrentVersion: params.BeaconConfig().AltairForkVersion,
|
2021-09-30 19:00:14 +00:00
|
|
|
Epoch: time.CurrentEpoch(st),
|
2021-08-09 21:27:51 +00:00
|
|
|
}, f)
|
|
|
|
csc, err := aState.CurrentSyncCommittee()
|
|
|
|
require.NoError(t, err)
|
|
|
|
nsc, err := aState.NextSyncCommittee()
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.DeepSSZEqual(t, nsc, csc)
|
|
|
|
}
|