2021-11-15 17:39:55 +00:00
|
|
|
package monitor
|
|
|
|
|
|
|
|
import (
|
2021-11-22 21:20:31 +00:00
|
|
|
"context"
|
|
|
|
"fmt"
|
2021-11-15 17:39:55 +00:00
|
|
|
"testing"
|
|
|
|
|
2024-02-15 05:46:47 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/v5/beacon-chain/core/altair"
|
|
|
|
"github.com/prysmaticlabs/prysm/v5/config/params"
|
|
|
|
"github.com/prysmaticlabs/prysm/v5/consensus-types/blocks"
|
|
|
|
"github.com/prysmaticlabs/prysm/v5/consensus-types/primitives"
|
|
|
|
"github.com/prysmaticlabs/prysm/v5/encoding/bytesutil"
|
|
|
|
ethpb "github.com/prysmaticlabs/prysm/v5/proto/prysm/v1alpha1"
|
|
|
|
"github.com/prysmaticlabs/prysm/v5/testing/require"
|
|
|
|
"github.com/prysmaticlabs/prysm/v5/testing/util"
|
2021-11-15 17:39:55 +00:00
|
|
|
logTest "github.com/sirupsen/logrus/hooks/test"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestProcessSlashings(t *testing.T) {
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
block *ethpb.BeaconBlock
|
|
|
|
wantedErr string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "Proposer slashing a tracked index",
|
|
|
|
block: ðpb.BeaconBlock{
|
|
|
|
Body: ðpb.BeaconBlockBody{
|
|
|
|
ProposerSlashings: []*ethpb.ProposerSlashing{
|
|
|
|
{
|
|
|
|
Header_1: ðpb.SignedBeaconBlockHeader{
|
|
|
|
Header: ðpb.BeaconBlockHeader{
|
|
|
|
ProposerIndex: 2,
|
|
|
|
Slot: params.BeaconConfig().SlotsPerEpoch + 1,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Header_2: ðpb.SignedBeaconBlockHeader{
|
|
|
|
Header: ðpb.BeaconBlockHeader{
|
|
|
|
ProposerIndex: 2,
|
|
|
|
Slot: 0,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2024-02-22 22:40:36 +00:00
|
|
|
wantedErr: "\"Proposer slashing was included\" bodyRoot1= bodyRoot2= prefix=monitor proposerIndex=2",
|
2021-11-15 17:39:55 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Proposer slashing an untracked index",
|
|
|
|
block: ðpb.BeaconBlock{
|
|
|
|
Body: ðpb.BeaconBlockBody{
|
|
|
|
ProposerSlashings: []*ethpb.ProposerSlashing{
|
|
|
|
{
|
|
|
|
Header_1: ðpb.SignedBeaconBlockHeader{
|
|
|
|
Header: ðpb.BeaconBlockHeader{
|
|
|
|
ProposerIndex: 3,
|
|
|
|
Slot: params.BeaconConfig().SlotsPerEpoch + 4,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Header_2: ðpb.SignedBeaconBlockHeader{
|
|
|
|
Header: ðpb.BeaconBlockHeader{
|
|
|
|
ProposerIndex: 3,
|
|
|
|
Slot: 0,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
wantedErr: "",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Attester slashing a tracked index",
|
|
|
|
block: ðpb.BeaconBlock{
|
|
|
|
Body: ðpb.BeaconBlockBody{
|
|
|
|
AttesterSlashings: []*ethpb.AttesterSlashing{
|
|
|
|
{
|
2022-07-19 12:41:15 +00:00
|
|
|
Attestation_1: util.HydrateIndexedAttestation(ðpb.IndexedAttestation{
|
2021-11-15 17:39:55 +00:00
|
|
|
Data: ðpb.AttestationData{
|
|
|
|
Source: ðpb.Checkpoint{Epoch: 1},
|
|
|
|
},
|
|
|
|
AttestingIndices: []uint64{1, 3, 4},
|
|
|
|
}),
|
2022-07-19 12:41:15 +00:00
|
|
|
Attestation_2: util.HydrateIndexedAttestation(ðpb.IndexedAttestation{
|
2021-11-15 17:39:55 +00:00
|
|
|
AttestingIndices: []uint64{1, 5, 6},
|
|
|
|
}),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2024-02-22 22:40:36 +00:00
|
|
|
wantedErr: "\"Attester slashing was included\" attestationSlot1=0 attestationSlot2=0 attesterIndex=1 " +
|
|
|
|
"beaconBlockRoot1=0x000000000000 beaconBlockRoot2=0x000000000000 blockInclusionSlot=0 prefix=monitor sourceEpoch1=1 sourceEpoch2=0 targetEpoch1=0 targetEpoch2=0",
|
2021-11-15 17:39:55 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Attester slashing untracked index",
|
|
|
|
block: ðpb.BeaconBlock{
|
|
|
|
Body: ðpb.BeaconBlockBody{
|
|
|
|
AttesterSlashings: []*ethpb.AttesterSlashing{
|
|
|
|
{
|
2022-07-19 12:41:15 +00:00
|
|
|
Attestation_1: util.HydrateIndexedAttestation(ðpb.IndexedAttestation{
|
2021-11-15 17:39:55 +00:00
|
|
|
Data: ðpb.AttestationData{
|
|
|
|
Source: ðpb.Checkpoint{Epoch: 1},
|
|
|
|
},
|
|
|
|
AttestingIndices: []uint64{1, 3, 4},
|
|
|
|
}),
|
2022-07-19 12:41:15 +00:00
|
|
|
Attestation_2: util.HydrateIndexedAttestation(ðpb.IndexedAttestation{
|
2021-11-15 17:39:55 +00:00
|
|
|
AttestingIndices: []uint64{3, 5, 6},
|
|
|
|
}),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
wantedErr: "",
|
|
|
|
}}
|
|
|
|
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
hook := logTest.NewGlobal()
|
|
|
|
s := &Service{
|
2023-01-26 14:40:12 +00:00
|
|
|
TrackedValidators: map[primitives.ValidatorIndex]bool{
|
2021-11-30 22:27:03 +00:00
|
|
|
1: true,
|
|
|
|
2: true,
|
2021-11-15 17:39:55 +00:00
|
|
|
},
|
|
|
|
}
|
2022-08-02 15:30:46 +00:00
|
|
|
wb, err := blocks.NewBeaconBlock(tt.block)
|
2022-05-04 03:55:35 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
s.processSlashings(wb)
|
2021-11-15 17:39:55 +00:00
|
|
|
if tt.wantedErr != "" {
|
|
|
|
require.LogsContain(t, hook, tt.wantedErr)
|
|
|
|
} else {
|
|
|
|
require.LogsDoNotContain(t, hook, "slashing")
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2021-11-22 21:20:31 +00:00
|
|
|
|
|
|
|
func TestProcessProposedBlock(t *testing.T) {
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
block *ethpb.BeaconBlock
|
|
|
|
wantedErr string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "Block proposed by tracked validator",
|
|
|
|
block: ðpb.BeaconBlock{
|
|
|
|
Slot: 6,
|
|
|
|
ProposerIndex: 12,
|
|
|
|
ParentRoot: bytesutil.PadTo([]byte("hello-world"), 32),
|
|
|
|
StateRoot: bytesutil.PadTo([]byte("state-world"), 32),
|
2022-08-02 15:30:46 +00:00
|
|
|
Body: ðpb.BeaconBlockBody{},
|
2021-11-22 21:20:31 +00:00
|
|
|
},
|
2024-02-22 22:40:36 +00:00
|
|
|
wantedErr: "\"Proposed beacon block was included\" balanceChange=100000000 blockRoot=0x68656c6c6f2d newBalance=32000000000 parentRoot=0x68656c6c6f2d prefix=monitor proposerIndex=12 slot=6 version=0",
|
2021-11-22 21:20:31 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Block proposed by untracked validator",
|
|
|
|
block: ðpb.BeaconBlock{
|
|
|
|
Slot: 6,
|
|
|
|
ProposerIndex: 13,
|
|
|
|
ParentRoot: bytesutil.PadTo([]byte("hello-world"), 32),
|
|
|
|
StateRoot: bytesutil.PadTo([]byte("state-world"), 32),
|
2022-08-02 15:30:46 +00:00
|
|
|
Body: ðpb.BeaconBlockBody{},
|
2021-11-22 21:20:31 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
hook := logTest.NewGlobal()
|
|
|
|
s := setupService(t)
|
|
|
|
beaconState, _ := util.DeterministicGenesisState(t, 256)
|
2022-12-22 09:20:10 +00:00
|
|
|
var root [32]byte
|
2021-11-22 21:20:31 +00:00
|
|
|
copy(root[:], "hello-world")
|
2022-08-02 15:30:46 +00:00
|
|
|
wb, err := blocks.NewBeaconBlock(tt.block)
|
2022-05-04 03:55:35 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
s.processProposedBlock(beaconState, root, wb)
|
2021-11-22 21:20:31 +00:00
|
|
|
if tt.wantedErr != "" {
|
|
|
|
require.LogsContain(t, hook, tt.wantedErr)
|
|
|
|
} else {
|
|
|
|
require.LogsDoNotContain(t, hook, "included")
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2021-11-24 01:56:34 +00:00
|
|
|
func TestProcessBlock_AllEventsTrackedVals(t *testing.T) {
|
2021-11-22 21:20:31 +00:00
|
|
|
hook := logTest.NewGlobal()
|
|
|
|
ctx := context.Background()
|
2021-11-24 01:56:34 +00:00
|
|
|
|
|
|
|
genesis, keys := util.DeterministicGenesisStateAltair(t, 64)
|
|
|
|
c, err := altair.NextSyncCommittee(ctx, genesis)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.NoError(t, genesis.SetCurrentSyncCommittee(c))
|
|
|
|
|
2021-11-22 21:20:31 +00:00
|
|
|
genConfig := util.DefaultBlockGenConfig()
|
|
|
|
genConfig.NumProposerSlashings = 1
|
2022-08-02 14:55:05 +00:00
|
|
|
genConfig.FullSyncAggregate = true
|
2021-11-24 01:56:34 +00:00
|
|
|
b, err := util.GenerateFullBlockAltair(genesis, keys, genConfig, 1)
|
|
|
|
require.NoError(t, err)
|
|
|
|
s := setupService(t)
|
|
|
|
|
|
|
|
pubKeys := make([][]byte, 3)
|
|
|
|
pubKeys[0] = genesis.Validators()[0].PublicKey
|
|
|
|
pubKeys[1] = genesis.Validators()[1].PublicKey
|
|
|
|
pubKeys[2] = genesis.Validators()[2].PublicKey
|
|
|
|
|
|
|
|
currentSyncCommittee := util.ConvertToCommittee([][]byte{
|
|
|
|
pubKeys[0], pubKeys[1], pubKeys[2], pubKeys[1], pubKeys[1],
|
|
|
|
})
|
|
|
|
require.NoError(t, genesis.SetCurrentSyncCommittee(currentSyncCommittee))
|
|
|
|
|
2021-11-22 21:20:31 +00:00
|
|
|
idx := b.Block.Body.ProposerSlashings[0].Header_1.Header.ProposerIndex
|
2021-11-24 01:56:34 +00:00
|
|
|
s.RLock()
|
|
|
|
if !s.trackedIndex(idx) {
|
2021-11-30 22:27:03 +00:00
|
|
|
s.TrackedValidators[idx] = true
|
2021-11-22 21:20:31 +00:00
|
|
|
s.latestPerformance[idx] = ValidatorLatestPerformance{
|
|
|
|
balance: 31900000000,
|
|
|
|
}
|
|
|
|
s.aggregatedPerformance[idx] = ValidatorAggregatedPerformance{}
|
|
|
|
}
|
2021-11-24 01:56:34 +00:00
|
|
|
s.RUnlock()
|
|
|
|
s.updateSyncCommitteeTrackedVals(genesis)
|
2021-11-22 21:20:31 +00:00
|
|
|
|
|
|
|
root, err := b.GetBlock().HashTreeRoot()
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.NoError(t, s.config.StateGen.SaveState(ctx, root, genesis))
|
2024-02-22 22:40:36 +00:00
|
|
|
wanted1 := fmt.Sprintf("\"Proposed beacon block was included\" balanceChange=100000000 blockRoot=%#x newBalance=32000000000 parentRoot=0xf732eaeb7fae prefix=monitor proposerIndex=15 slot=1 version=1", bytesutil.Trunc(root[:]))
|
|
|
|
wanted2 := fmt.Sprintf("\"Proposer slashing was included\" bodyRoot1=0x000100000000 bodyRoot2=0x000200000000 prefix=monitor proposerIndex=%d slashingSlot=0 slot=1", idx)
|
|
|
|
wanted3 := "\"Sync committee contribution included\" balanceChange=0 contribCount=3 expectedContribCount=3 newBalance=32000000000 prefix=monitor validatorIndex=1"
|
|
|
|
wanted4 := "\"Sync committee contribution included\" balanceChange=0 contribCount=1 expectedContribCount=1 newBalance=32000000000 prefix=monitor validatorIndex=2"
|
2022-08-02 15:30:46 +00:00
|
|
|
wrapped, err := blocks.NewSignedBeaconBlock(b)
|
2021-11-24 01:56:34 +00:00
|
|
|
require.NoError(t, err)
|
2021-11-22 21:20:31 +00:00
|
|
|
s.processBlock(ctx, wrapped)
|
|
|
|
require.LogsContain(t, hook, wanted1)
|
|
|
|
require.LogsContain(t, hook, wanted2)
|
2021-11-24 01:56:34 +00:00
|
|
|
require.LogsContain(t, hook, wanted3)
|
|
|
|
require.LogsContain(t, hook, wanted4)
|
2021-11-22 21:20:31 +00:00
|
|
|
}
|
2021-12-01 03:35:55 +00:00
|
|
|
|
|
|
|
func TestLogAggregatedPerformance(t *testing.T) {
|
|
|
|
hook := logTest.NewGlobal()
|
2023-05-03 04:34:01 +00:00
|
|
|
latestPerformance := map[primitives.ValidatorIndex]ValidatorLatestPerformance{
|
|
|
|
1: {
|
|
|
|
balance: 32000000000,
|
|
|
|
},
|
|
|
|
2: {
|
|
|
|
balance: 32000000000,
|
|
|
|
},
|
|
|
|
12: {
|
|
|
|
balance: 31900000000,
|
|
|
|
},
|
|
|
|
15: {
|
|
|
|
balance: 31900000000,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
aggregatedPerformance := map[primitives.ValidatorIndex]ValidatorAggregatedPerformance{
|
|
|
|
1: {
|
|
|
|
startEpoch: 0,
|
|
|
|
startBalance: 31700000000,
|
|
|
|
totalAttestedCount: 12,
|
|
|
|
totalRequestedCount: 15,
|
|
|
|
totalDistance: 14,
|
|
|
|
totalCorrectHead: 8,
|
|
|
|
totalCorrectSource: 11,
|
|
|
|
totalCorrectTarget: 12,
|
|
|
|
totalProposedCount: 1,
|
|
|
|
totalSyncCommitteeContributions: 0,
|
|
|
|
totalSyncCommitteeAggregations: 0,
|
|
|
|
},
|
|
|
|
2: {},
|
|
|
|
12: {},
|
|
|
|
15: {},
|
|
|
|
}
|
|
|
|
s := &Service{
|
|
|
|
latestPerformance: latestPerformance,
|
|
|
|
aggregatedPerformance: aggregatedPerformance,
|
|
|
|
}
|
2021-12-01 03:35:55 +00:00
|
|
|
|
|
|
|
s.logAggregatedPerformance()
|
2024-02-22 22:40:36 +00:00
|
|
|
wanted := "\"Aggregated performance since launch\" attestationInclusion=\"80.00%\"" +
|
|
|
|
" averageInclusionDistance=1.2 balanceChangePct=\"0.95%\" correctlyVotedHeadPct=\"66.67%\" " +
|
|
|
|
"correctlyVotedSourcePct=\"91.67%\" correctlyVotedTargetPct=\"100.00%\" prefix=monitor startBalance=31700000000 " +
|
|
|
|
"startEpoch=0 totalAggregations=0 totalProposedBlocks=1 totalRequested=15 totalSyncContributions=0 " +
|
|
|
|
"validatorIndex=1"
|
2021-12-01 03:35:55 +00:00
|
|
|
require.LogsContain(t, hook, wanted)
|
|
|
|
}
|