2021-09-28 19:33:45 +00:00
|
|
|
package slasher
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"testing"
|
|
|
|
|
2023-03-17 18:52:56 +00:00
|
|
|
mock "github.com/prysmaticlabs/prysm/v4/beacon-chain/blockchain/testing"
|
|
|
|
"github.com/prysmaticlabs/prysm/v4/beacon-chain/core/signing"
|
|
|
|
dbtest "github.com/prysmaticlabs/prysm/v4/beacon-chain/db/testing"
|
|
|
|
doublylinkedtree "github.com/prysmaticlabs/prysm/v4/beacon-chain/forkchoice/doubly-linked-tree"
|
|
|
|
slashingsmock "github.com/prysmaticlabs/prysm/v4/beacon-chain/operations/slashings/mock"
|
|
|
|
slashertypes "github.com/prysmaticlabs/prysm/v4/beacon-chain/slasher/types"
|
2023-05-03 04:34:01 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/v4/beacon-chain/startup"
|
2023-03-17 18:52:56 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/v4/beacon-chain/state/stategen"
|
|
|
|
"github.com/prysmaticlabs/prysm/v4/config/params"
|
|
|
|
"github.com/prysmaticlabs/prysm/v4/consensus-types/primitives"
|
|
|
|
"github.com/prysmaticlabs/prysm/v4/crypto/bls"
|
|
|
|
"github.com/prysmaticlabs/prysm/v4/encoding/bytesutil"
|
|
|
|
ethpb "github.com/prysmaticlabs/prysm/v4/proto/prysm/v1alpha1"
|
|
|
|
"github.com/prysmaticlabs/prysm/v4/testing/require"
|
|
|
|
"github.com/prysmaticlabs/prysm/v4/testing/util"
|
2021-09-28 19:33:45 +00:00
|
|
|
logTest "github.com/sirupsen/logrus/hooks/test"
|
|
|
|
)
|
|
|
|
|
|
|
|
func Test_processQueuedBlocks_DetectsDoubleProposals(t *testing.T) {
|
|
|
|
hook := logTest.NewGlobal()
|
|
|
|
slasherDB := dbtest.SetupSlasherDB(t)
|
|
|
|
beaconDB := dbtest.SetupDB(t)
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
|
|
|
|
beaconState, err := util.NewBeaconState()
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// Initialize validators in the state.
|
|
|
|
numVals := params.BeaconConfig().MinGenesisActiveValidatorCount
|
|
|
|
validators := make([]*ethpb.Validator, numVals)
|
|
|
|
privKeys := make([]bls.SecretKey, numVals)
|
|
|
|
for i := range validators {
|
|
|
|
privKey, err := bls.RandKey()
|
|
|
|
require.NoError(t, err)
|
|
|
|
privKeys[i] = privKey
|
|
|
|
validators[i] = ðpb.Validator{
|
|
|
|
PublicKey: privKey.PublicKey().Marshal(),
|
|
|
|
WithdrawalCredentials: make([]byte, 32),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
err = beaconState.SetValidators(validators)
|
|
|
|
require.NoError(t, err)
|
|
|
|
domain, err := signing.Domain(
|
|
|
|
beaconState.Fork(),
|
|
|
|
0,
|
|
|
|
params.BeaconConfig().DomainBeaconProposer,
|
2022-02-14 13:34:38 +00:00
|
|
|
beaconState.GenesisValidatorsRoot(),
|
2021-09-28 19:33:45 +00:00
|
|
|
)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
mockChain := &mock.ChainService{
|
|
|
|
State: beaconState,
|
|
|
|
}
|
|
|
|
s := &Service{
|
|
|
|
serviceCfg: &ServiceConfig{
|
|
|
|
Database: slasherDB,
|
|
|
|
StateNotifier: &mock.MockStateNotifier{},
|
|
|
|
HeadStateFetcher: mockChain,
|
2022-09-28 20:10:27 +00:00
|
|
|
StateGen: stategen.New(beaconDB, doublylinkedtree.New()),
|
2022-01-26 14:48:20 +00:00
|
|
|
SlashingPoolInserter: &slashingsmock.PoolMock{},
|
2023-05-03 04:34:01 +00:00
|
|
|
ClockWaiter: startup.NewClockSynchronizer(),
|
2021-09-28 19:33:45 +00:00
|
|
|
},
|
|
|
|
params: DefaultParams(),
|
|
|
|
blksQueue: newBlocksQueue(),
|
|
|
|
}
|
|
|
|
|
|
|
|
parentRoot := bytesutil.ToBytes32([]byte("parent"))
|
|
|
|
err = s.serviceCfg.StateGen.SaveState(ctx, parentRoot, beaconState)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2023-01-26 14:40:12 +00:00
|
|
|
currentSlotChan := make(chan primitives.Slot)
|
2021-09-28 19:33:45 +00:00
|
|
|
exitChan := make(chan struct{})
|
|
|
|
go func() {
|
|
|
|
s.processQueuedBlocks(ctx, currentSlotChan)
|
|
|
|
exitChan <- struct{}{}
|
|
|
|
}()
|
|
|
|
|
|
|
|
signedBlkHeaders := []*slashertypes.SignedBlockHeaderWrapper{
|
|
|
|
createProposalWrapper(t, 4, 1, []byte{1}),
|
|
|
|
createProposalWrapper(t, 4, 1, []byte{1}),
|
|
|
|
createProposalWrapper(t, 4, 1, []byte{1}),
|
|
|
|
createProposalWrapper(t, 4, 1, []byte{2}),
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add valid signatures to the block headers we are testing.
|
|
|
|
for _, proposalWrapper := range signedBlkHeaders {
|
|
|
|
proposalWrapper.SignedBeaconBlockHeader.Header.ParentRoot = parentRoot[:]
|
|
|
|
headerHtr, err := proposalWrapper.SignedBeaconBlockHeader.Header.HashTreeRoot()
|
|
|
|
require.NoError(t, err)
|
|
|
|
container := ðpb.SigningData{
|
|
|
|
ObjectRoot: headerHtr[:],
|
|
|
|
Domain: domain,
|
|
|
|
}
|
|
|
|
signingRoot, err := container.HashTreeRoot()
|
|
|
|
require.NoError(t, err)
|
|
|
|
privKey := privKeys[proposalWrapper.SignedBeaconBlockHeader.Header.ProposerIndex]
|
|
|
|
proposalWrapper.SignedBeaconBlockHeader.Signature = privKey.Sign(signingRoot[:]).Marshal()
|
|
|
|
}
|
|
|
|
|
|
|
|
s.blksQueue.extend(signedBlkHeaders)
|
|
|
|
|
2023-01-26 14:40:12 +00:00
|
|
|
currentSlot := primitives.Slot(4)
|
2021-09-28 19:33:45 +00:00
|
|
|
currentSlotChan <- currentSlot
|
|
|
|
cancel()
|
|
|
|
<-exitChan
|
|
|
|
require.LogsContain(t, hook, "Proposer slashing detected")
|
|
|
|
}
|
|
|
|
|
|
|
|
func Test_processQueuedBlocks_NotSlashable(t *testing.T) {
|
|
|
|
hook := logTest.NewGlobal()
|
|
|
|
slasherDB := dbtest.SetupSlasherDB(t)
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
|
|
|
|
beaconState, err := util.NewBeaconState()
|
|
|
|
require.NoError(t, err)
|
2023-01-26 14:40:12 +00:00
|
|
|
currentSlot := primitives.Slot(4)
|
2021-09-28 19:33:45 +00:00
|
|
|
require.NoError(t, beaconState.SetSlot(currentSlot))
|
|
|
|
mockChain := &mock.ChainService{
|
|
|
|
State: beaconState,
|
|
|
|
Slot: ¤tSlot,
|
|
|
|
}
|
|
|
|
|
|
|
|
s := &Service{
|
|
|
|
serviceCfg: &ServiceConfig{
|
|
|
|
Database: slasherDB,
|
|
|
|
StateNotifier: &mock.MockStateNotifier{},
|
|
|
|
HeadStateFetcher: mockChain,
|
2023-05-03 04:34:01 +00:00
|
|
|
ClockWaiter: startup.NewClockSynchronizer(),
|
2021-09-28 19:33:45 +00:00
|
|
|
},
|
|
|
|
params: DefaultParams(),
|
|
|
|
blksQueue: newBlocksQueue(),
|
|
|
|
}
|
2023-01-26 14:40:12 +00:00
|
|
|
currentSlotChan := make(chan primitives.Slot)
|
2021-09-28 19:33:45 +00:00
|
|
|
exitChan := make(chan struct{})
|
|
|
|
go func() {
|
|
|
|
s.processQueuedBlocks(ctx, currentSlotChan)
|
|
|
|
exitChan <- struct{}{}
|
|
|
|
}()
|
|
|
|
s.blksQueue.extend([]*slashertypes.SignedBlockHeaderWrapper{
|
|
|
|
createProposalWrapper(t, 4, 1, []byte{1}),
|
|
|
|
createProposalWrapper(t, 4, 1, []byte{1}),
|
|
|
|
})
|
|
|
|
currentSlotChan <- currentSlot
|
|
|
|
cancel()
|
|
|
|
<-exitChan
|
|
|
|
require.LogsDoNotContain(t, hook, "Proposer slashing detected")
|
|
|
|
}
|
2021-09-29 18:17:37 +00:00
|
|
|
|
2023-01-26 14:40:12 +00:00
|
|
|
func createProposalWrapper(t *testing.T, slot primitives.Slot, proposerIndex primitives.ValidatorIndex, signingRoot []byte) *slashertypes.SignedBlockHeaderWrapper {
|
2021-09-29 18:17:37 +00:00
|
|
|
header := ðpb.BeaconBlockHeader{
|
|
|
|
Slot: slot,
|
|
|
|
ProposerIndex: proposerIndex,
|
|
|
|
ParentRoot: params.BeaconConfig().ZeroHash[:],
|
|
|
|
StateRoot: bytesutil.PadTo(signingRoot, 32),
|
|
|
|
BodyRoot: params.BeaconConfig().ZeroHash[:],
|
|
|
|
}
|
|
|
|
signRoot, err := header.HashTreeRoot()
|
|
|
|
require.NoError(t, err)
|
|
|
|
fakeSig := make([]byte, 96)
|
|
|
|
copy(fakeSig, "hello")
|
|
|
|
return &slashertypes.SignedBlockHeaderWrapper{
|
|
|
|
SignedBeaconBlockHeader: ðpb.SignedBeaconBlockHeader{
|
|
|
|
Header: header,
|
|
|
|
Signature: fakeSig,
|
|
|
|
},
|
|
|
|
SigningRoot: signRoot,
|
|
|
|
}
|
|
|
|
}
|