Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test(simulated): Add Simulation Tests for Malicious Proposer, Crashed EL and Malicious Consensus Majority (3/4) #2523

Open
wants to merge 17 commits into
base: fix-coverage-measurement
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 75 additions & 0 deletions testing/simulated/chaos_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
//go:build simulated

// SPDX-License-Identifier: BUSL-1.1
//
// Copyright (C) 2025, Berachain Foundation. All rights reserved.
// Use of this software is governed by the Business Source License included
// in the LICENSE file of this repository and at www.mariadb.com/bsl11.
//
// ANY USE OF THE LICENSED WORK IN VIOLATION OF THIS LICENSE WILL AUTOMATICALLY
// TERMINATE YOUR RIGHTS UNDER THIS LICENSE FOR THE CURRENT AND ALL OTHER
// VERSIONS OF THE LICENSED WORK.
//
// THIS LICENSE DOES NOT GRANT YOU ANY RIGHT IN ANY TRADEMARK OR LOGO OF
// LICENSOR OR ITS AFFILIATES (PROVIDED THAT YOU MAY USE A TRADEMARK OR LOGO OF
// LICENSOR AS EXPRESSLY REQUIRED BY THIS LICENSE).
//
// TO THE EXTENT PERMITTED BY APPLICABLE LAW, THE LICENSED WORK IS PROVIDED ON
// AN “AS IS” BASIS. LICENSOR HEREBY DISCLAIMS ALL WARRANTIES AND CONDITIONS,
// EXPRESS OR IMPLIED, INCLUDING (WITHOUT LIMITATION) WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, NON-INFRINGEMENT, AND
// TITLE.

package simulated_test

import (
"time"

"github.com/berachain/beacon-kit/testing/simulated"
"github.com/cometbft/cometbft/abci/types"
)

// TestProcessProposal_CrashedExecutionClient_Errors effectively serves as a test for how a valid node would react to
// a valid block being proposed but the execution client has crashed.
func (s *SimulatedSuite) TestProcessProposal_CrashedExecutionClient_Errors() {
const blockHeight = 1
const coreLoopIterations = 1

// Initialize the chain state.
s.initializeChain()

// Retrieve the BLS signer and proposer address.
blsSigner := simulated.GetBlsSigner(s.HomeDir)
pubkey, err := blsSigner.GetPubKey()
s.Require().NoError(err)

// Go through 1 iteration of the core loop to bypass any startup specific edge cases such as sync head on startup.
proposals := s.CoreLoop(blockHeight, coreLoopIterations, blsSigner)
s.Require().Len(proposals, coreLoopIterations)

// Prepare a valid block proposal.
proposalTime := time.Now()
proposal, err := s.SimComet.Comet.PrepareProposal(s.Ctx, &types.PrepareProposalRequest{
Height: blockHeight + coreLoopIterations,
Time: proposalTime,
ProposerAddress: pubkey.Address(),
})
s.Require().NoError(err)
s.Require().NotEmpty(proposal)

// Reset the log buffer to discard old logs we don't care about.
s.LogBuffer.Reset()
// Kill the execution client.
err = s.ElHandle.Close()
s.Require().NoError(err)
// Process the proposal containing the valid block.
processResp, err := s.SimComet.Comet.ProcessProposal(s.Ctx, &types.ProcessProposalRequest{
Txs: proposal.Txs,
Height: blockHeight + coreLoopIterations,
ProposerAddress: pubkey.Address(),
Time: proposalTime,
})
s.Require().NoError(err)
s.Require().Equal(types.PROCESS_PROPOSAL_STATUS_REJECT, processResp.Status)
s.Require().Contains(s.LogBuffer.String(), "got an unexpected server error in JSON-RPC response failed to convert from jsonrpc.Error")
}
92 changes: 92 additions & 0 deletions testing/simulated/malicious_consensus_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
//go:build simulated

// SPDX-License-Identifier: BUSL-1.1
//
// Copyright (C) 2025, Berachain Foundation. All rights reserved.
// Use of this software is governed by the Business Source License included
// in the LICENSE file of this repository and at www.mariadb.com/bsl11.
//
// ANY USE OF THE LICENSED WORK IN VIOLATION OF THIS LICENSE WILL AUTOMATICALLY
// TERMINATE YOUR RIGHTS UNDER THIS LICENSE FOR THE CURRENT AND ALL OTHER
// VERSIONS OF THE LICENSED WORK.
//
// THIS LICENSE DOES NOT GRANT YOU ANY RIGHT IN ANY TRADEMARK OR LOGO OF
// LICENSOR OR ITS AFFILIATES (PROVIDED THAT YOU MAY USE A TRADEMARK OR LOGO OF
// LICENSOR AS EXPRESSLY REQUIRED BY THIS LICENSE).
//
// TO THE EXTENT PERMITTED BY APPLICABLE LAW, THE LICENSED WORK IS PROVIDED ON
// AN “AS IS” BASIS. LICENSOR HEREBY DISCLAIMS ALL WARRANTIES AND CONDITIONS,
// EXPRESS OR IMPLIED, INCLUDING (WITHOUT LIMITATION) WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, NON-INFRINGEMENT, AND
// TITLE.

package simulated_test

import (
"time"

"github.com/berachain/beacon-kit/beacon/blockchain"
"github.com/berachain/beacon-kit/consensus/cometbft/service/encoding"
"github.com/berachain/beacon-kit/engine-primitives/errors"
"github.com/berachain/beacon-kit/testing/simulated"
"github.com/cometbft/cometbft/abci/types"
"github.com/stretchr/testify/require"
)

// TestFinalizeBlock_BadBlock_Errors effectively serves as a test for how a valid node would react to
// a malicious consensus majority agreeing to a block with an invalid EVM transaction.
func (s *SimulatedSuite) TestFinalizeBlock_BadBlock_Errors() {
const blockHeight = 1
const coreLoopIterations = 1

// Initialize the chain state.
s.initializeChain()

// Retrieve the BLS signer and proposer address.
blsSigner := simulated.GetBlsSigner(s.HomeDir)
pubkey, err := blsSigner.GetPubKey()
s.Require().NoError(err)

// Go through 1 iteration of the core loop to bypass any startup specific edge cases such as sync head on startup.
proposals := s.CoreLoop(blockHeight, coreLoopIterations, blsSigner)
s.Require().Len(proposals, coreLoopIterations)

// Prepare a valid block proposal.
proposalTime := time.Now()
proposal, err := s.SimComet.Comet.PrepareProposal(s.Ctx, &types.PrepareProposalRequest{
Height: blockHeight + coreLoopIterations,
Time: proposalTime,
ProposerAddress: pubkey.Address(),
})
s.Require().NoError(err)
s.Require().NotEmpty(proposal)

// Unmarshal the proposal block.
proposedBlock, err := encoding.UnmarshalBeaconBlockFromABCIRequest(
proposal.Txs,
blockchain.BeaconBlockTxIndex,
s.TestNode.ChainSpec.ActiveForkVersionForSlot(blockHeight+coreLoopIterations),
)
s.Require().NoError(err)

// Create a malicious block by injecting an invalid transaction.
maliciousBlock := simulated.CreateInvalidBlock(require.New(s.T()), proposedBlock, blsSigner, s.TestNode.ChainSpec, s.GenesisValidatorsRoot)
maliciousBlockBytes, err := maliciousBlock.MarshalSSZ()
s.Require().NoError(err)

// Replace the valid block with the malicious block in the proposal.
proposal.Txs[0] = maliciousBlockBytes

// Reset the log buffer to discard old logs we don't care about
s.LogBuffer.Reset()
// Finalize the proposal containing the malicious block.
finalizeResp, err := s.SimComet.Comet.FinalizeBlock(s.Ctx, &types.FinalizeBlockRequest{
Txs: proposal.Txs,
Height: blockHeight + coreLoopIterations,
ProposerAddress: pubkey.Address(),
Time: proposalTime,
})
s.Require().ErrorIs(err, errors.ErrInvalidPayloadStatus)
s.Require().Nil(finalizeResp)
s.Require().Contains(s.LogBuffer.String(), "max fee per gas less than block base fee: address 0x71562b71999873DB5b286dF957af199Ec94617F7, maxFeePerGas: 10000000, baseFee: 765625000")
}
140 changes: 140 additions & 0 deletions testing/simulated/malicious_proposer_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
//go:build simulated

// SPDX-License-Identifier: BUSL-1.1
//
// Copyright (C) 2025, Berachain Foundation. All rights reserved.
// Use of this software is governed by the Business Source License included
// in the LICENSE file of this repository and at www.mariadb.com/bsl11.
//
// ANY USE OF THE LICENSED WORK IN VIOLATION OF THIS LICENSE WILL AUTOMATICALLY
// TERMINATE YOUR RIGHTS UNDER THIS LICENSE FOR THE CURRENT AND ALL OTHER
// VERSIONS OF THE LICENSED WORK.
//
// THIS LICENSE DOES NOT GRANT YOU ANY RIGHT IN ANY TRADEMARK OR LOGO OF
// LICENSOR OR ITS AFFILIATES (PROVIDED THAT YOU MAY USE A TRADEMARK OR LOGO OF
// LICENSOR AS EXPRESSLY REQUIRED BY THIS LICENSE).
//
// TO THE EXTENT PERMITTED BY APPLICABLE LAW, THE LICENSED WORK IS PROVIDED ON
// AN “AS IS” BASIS. LICENSOR HEREBY DISCLAIMS ALL WARRANTIES AND CONDITIONS,
// EXPRESS OR IMPLIED, INCLUDING (WITHOUT LIMITATION) WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, NON-INFRINGEMENT, AND
// TITLE.

package simulated_test

import (
"time"

"github.com/berachain/beacon-kit/beacon/blockchain"
"github.com/berachain/beacon-kit/consensus/cometbft/service/encoding"
"github.com/berachain/beacon-kit/engine-primitives/errors"
"github.com/berachain/beacon-kit/testing/simulated"
"github.com/cometbft/cometbft/abci/types"
"github.com/stretchr/testify/require"
)

// TestProcessProposal_BadBlock_IsRejected effectively serves as a test for how a valid node would react to
// a malicious proposer proposing a block with an invalid EVM transaction.
func (s *SimulatedSuite) TestProcessProposal_BadBlock_IsRejected() {
const blockHeight = 1
const coreLoopIterations = 1

// Initialize the chain state.
s.initializeChain()

// Retrieve the BLS signer and proposer address.
blsSigner := simulated.GetBlsSigner(s.HomeDir)
pubkey, err := blsSigner.GetPubKey()
s.Require().NoError(err)

// Go through 1 iteration of the core loop to bypass any startup specific edge cases such as sync head on startup.
proposals := s.CoreLoop(blockHeight, coreLoopIterations, blsSigner)
s.Require().Len(proposals, coreLoopIterations)

// Prepare a valid block proposal.
proposalTime := time.Now()
proposal, err := s.SimComet.Comet.PrepareProposal(s.Ctx, &types.PrepareProposalRequest{
Height: blockHeight + coreLoopIterations,
Time: proposalTime,
ProposerAddress: pubkey.Address(),
})
s.Require().NoError(err)
s.Require().NotEmpty(proposal)

// Unmarshal the proposal block.
proposedBlock, err := encoding.UnmarshalBeaconBlockFromABCIRequest(
proposal.Txs,
blockchain.BeaconBlockTxIndex,
s.TestNode.ChainSpec.ActiveForkVersionForSlot(blockHeight+coreLoopIterations),
)
s.Require().NoError(err)

// Create a malicious block by injecting an invalid transaction.
maliciousBlock := simulated.CreateInvalidBlock(require.New(s.T()), proposedBlock, blsSigner, s.TestNode.ChainSpec, s.GenesisValidatorsRoot)
maliciousBlockBytes, err := maliciousBlock.MarshalSSZ()
s.Require().NoError(err)

// Replace the valid block with the malicious block in the proposal.
proposal.Txs[0] = maliciousBlockBytes

// Reset the log buffer to discard old logs we don't care about
s.LogBuffer.Reset()
// Process the proposal containing the malicious block.
processResp, err := s.SimComet.Comet.ProcessProposal(s.Ctx, &types.ProcessProposalRequest{
Txs: proposal.Txs,
Height: blockHeight + coreLoopIterations,
ProposerAddress: pubkey.Address(),
Time: proposalTime,
})
s.Require().NoError(err)
s.Require().Equal(types.PROCESS_PROPOSAL_STATUS_REJECT, processResp.Status)

// Verify that the log contains the expected error message.
s.Require().Contains(s.LogBuffer.String(), errors.ErrInvalidPayloadStatus.Error())
// Note this error message may change across execution clients. Base fee changes with number of core loop iterations.
s.Require().Contains(s.LogBuffer.String(), "max fee per gas less than block base fee: address 0x71562b71999873DB5b286dF957af199Ec94617F7, maxFeePerGas: 10000000, baseFee: 765625000")
}

// TestProcessProposal_InvalidTimestamps_Errors effectively serves as a test for how a valid node would react to
// a malicious proposer attempting to use a future timestamp in the block that does not match the consensus timestamp.
func (s *SimulatedSuite) TestProcessProposal_InvalidTimestamps_Errors() {
const blockHeight = 1
const coreLoopIterations = 1

// Initialize the chain state.
s.initializeChain()

// Retrieve the BLS signer and proposer address.
blsSigner := simulated.GetBlsSigner(s.HomeDir)
pubkey, err := blsSigner.GetPubKey()
s.Require().NoError(err)

// Go through 1 iteration of the core loop to bypass any startup specific edge cases such as sync head on startup.
proposals := s.CoreLoop(blockHeight, coreLoopIterations, blsSigner)
s.Require().Len(proposals, coreLoopIterations)

// Prepare a valid block proposal, but 2 seconds in the future (i.e. attempt to roll timestamp forward)
correctTime := time.Now()
proposalTime := correctTime.Add(2 * time.Second)
maliciousProposal, err := s.SimComet.Comet.PrepareProposal(s.Ctx, &types.PrepareProposalRequest{
Height: blockHeight + coreLoopIterations,
Time: proposalTime,
ProposerAddress: pubkey.Address(),
})
s.Require().NoError(err)
s.Require().NotEmpty(maliciousProposal)

// Reset the log buffer to discard old logs we don't care about
s.LogBuffer.Reset()
// Process the proposal containing the malicious block.
processResp, err := s.SimComet.Comet.ProcessProposal(s.Ctx, &types.ProcessProposalRequest{
Txs: maliciousProposal.Txs,
Height: blockHeight + coreLoopIterations,
ProposerAddress: pubkey.Address(),
// Use the correct time as the actual consensus time, which mismatches the proposal time.
Time: correctTime,
})
s.Require().NoError(err)
s.Require().Equal(types.PROCESS_PROPOSAL_STATUS_REJECT, processResp.Status)
s.Require().Contains(s.LogBuffer.String(), "timestamp too far in the future")
}
Loading
Loading