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

fix(consensus, beacon): hardened proposer validation #2102

Open
wants to merge 29 commits into
base: consensus_decorated_events
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 7 commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
b2bf298
wip: added proposerAddress to consensus block
abi87 Oct 25, 2024
08f00c2
wip: added proposerAddress to slot data
abi87 Oct 25, 2024
3ee634d
nit
abi87 Oct 25, 2024
d4779f9
wip: adding validation
abi87 Oct 25, 2024
b2aa1af
Merge branch 'consensus_decorated_events' into harden-proposer-valida…
abi87 Oct 25, 2024
f4a7d79
wip: adding UTs to state transition package
abi87 Oct 29, 2024
0f46172
wip: completed simple UT for state transition package
abi87 Oct 29, 2024
22c7717
wip: minimal execution engine stub
abi87 Oct 29, 2024
05cba80
extended asserts
abi87 Oct 29, 2024
443ac1b
added test case
abi87 Oct 29, 2024
10efd5e
nits
abi87 Oct 29, 2024
099716d
tests for helpers in state transition using mock
nidhi-singh02 Oct 30, 2024
151a533
Revert "tests for helpers in state transition using mock"
nidhi-singh02 Oct 30, 2024
9818c7e
tests with only mock for execution engine
nidhi-singh02 Oct 30, 2024
160cc88
removed test for VerifyAndNotifyNewPayload
nidhi-singh02 Oct 30, 2024
4a9fe1c
nit
abi87 Oct 31, 2024
e048be4
improved unit tests asserts
abi87 Oct 31, 2024
8bf34db
appease linter
abi87 Oct 31, 2024
d90a95a
fix(state-transition): fix deposit index upon genesis processing (#2116)
abi87 Oct 31, 2024
e17d29c
fixed bad merge
abi87 Oct 31, 2024
61e3cfe
Merge branch 'consensus_decorated_events' into harden-proposer-valida…
abi87 Oct 31, 2024
11df8e5
fixed comment
abi87 Oct 31, 2024
6286b20
Merge branch 'main' into state-transition-add-UTs
abi87 Nov 1, 2024
af8c5e0
fix(build): erigon repo
gummybera Nov 1, 2024
023ebfd
fix(build): bump erigon to recent version
gummybera Nov 1, 2024
d66b298
nits from code review
abi87 Nov 1, 2024
11c551e
Merge branch 'fix-erigon' into harden-proposer-validation
abi87 Nov 2, 2024
2811de8
inject validator address verification function from ctor to ease up t…
abi87 Nov 5, 2024
2c7f448
Merge branch 'state-transition-add-UTs' into harden-proposer-validation
abi87 Nov 5, 2024
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
1 change: 1 addition & 0 deletions mod/beacon/blockchain/process.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ func (s *Service[
// actually irrelevant at this point.
SkipPayloadVerification: false,

ProposerAddress: blk.GetProposerAddress(),
NextPayloadTimestamp: blk.GetNextPayloadTimestamp(),
},
st,
Expand Down
1 change: 1 addition & 0 deletions mod/beacon/blockchain/receive.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ func (s *Service[
SkipPayloadVerification: false,
SkipValidateResult: false,
SkipValidateRandao: false,
ProposerAddress: blk.GetProposerAddress(),
NextPayloadTimestamp: blk.GetNextPayloadTimestamp(),
},
st, blk.GetBeaconBlock(),
Expand Down
2 changes: 2 additions & 0 deletions mod/beacon/blockchain/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ type AvailabilityStore[BeaconBlockBodyT any] interface {
type ConsensusBlock[BeaconBlockT any] interface {
GetBeaconBlock() BeaconBlockT

GetProposerAddress() []byte

// GetNextPayloadTimestamp returns the timestamp proposed by
// consensus for the next payload to be proposed. It is also
// used to bound current payload upon validation
Expand Down
12 changes: 11 additions & 1 deletion mod/beacon/validator/block_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ func (s *Service[
g.Go(func() error {
return s.computeAndSetStateRoot(
ctx,
slotData.GetProposerAddress(),
slotData.GetNextPayloadTimestamp(),
st,
blk,
Expand Down Expand Up @@ -336,11 +337,18 @@ func (s *Service[
_, BeaconBlockT, _, BeaconStateT, _, _, _, _, _, _, _, _, _,
]) computeAndSetStateRoot(
ctx context.Context,
proposerAddress []byte,
nextPayloadTimestamp math.U64,
st BeaconStateT,
blk BeaconBlockT,
) error {
stateRoot, err := s.computeStateRoot(ctx, nextPayloadTimestamp, st, blk)
stateRoot, err := s.computeStateRoot(
ctx,
proposerAddress,
nextPayloadTimestamp,
st,
blk,
)
if err != nil {
s.logger.Error(
"failed to compute state root while building block ❗️ ",
Expand All @@ -358,6 +366,7 @@ func (s *Service[
_, BeaconBlockT, _, BeaconStateT, _, _, _, _, _, _, _, _, _,
]) computeStateRoot(
ctx context.Context,
proposerAddress []byte,
nextPayloadTimestamp math.U64,
st BeaconStateT,
blk BeaconBlockT,
Expand All @@ -374,6 +383,7 @@ func (s *Service[
SkipPayloadVerification: true,
SkipValidateResult: true,
SkipValidateRandao: true,
ProposerAddress: proposerAddress,
NextPayloadTimestamp: nextPayloadTimestamp,
},
st, blk,
Expand Down
1 change: 1 addition & 0 deletions mod/beacon/validator/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,7 @@ type SlotData[AttestationDataT, SlashingInfoT any] interface {
GetAttestationData() []AttestationDataT
// GetSlashingInfo returns the slashing info of the incoming slot.
GetSlashingInfo() []SlashingInfoT
GetProposerAddress() []byte
// GetNextPayloadTimestamp returns the timestamp proposed by
// consensus for the next payload to be proposed. It is also
// used to bound current payload upon validation
Expand Down
1 change: 1 addition & 0 deletions mod/consensus/pkg/cometbft/service/abci.go
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,7 @@ func (s *Service[LoggerT]) PrepareProposal(
math.Slot(req.GetHeight()),
nil,
nil,
req.GetProposerAddress(),

// we do not add h.minPayloadDelay here since req.GetTime()
// is guaranteed to be strictly larger than
Expand Down
2 changes: 2 additions & 0 deletions mod/consensus/pkg/cometbft/service/middleware/abci.go
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,7 @@ func (h *ABCIMiddleware[
var enrichedBlk *types.ConsensusBlock[BeaconBlockT]
enrichedBlk = enrichedBlk.New(
blk,
req.GetProposerAddress(),
req.GetTime().Add(h.minPayloadDelay),
)
blkEvent := async.NewEvent(ctx, async.BeaconBlockReceived, enrichedBlk)
Expand Down Expand Up @@ -341,6 +342,7 @@ func (h *ABCIMiddleware[
var enrichedBlk *types.ConsensusBlock[BeaconBlockT]
enrichedBlk = enrichedBlk.New(
blk,
req.GetProposerAddress(),
req.GetTime().Add(h.minPayloadDelay),
)
blkEvent := async.NewEvent(ctx, async.FinalBeaconBlockReceived, enrichedBlk)
Expand Down
40 changes: 40 additions & 0 deletions mod/consensus/pkg/types/common.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// SPDX-License-Identifier: BUSL-1.1
//
// Copyright (C) 2024, 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 types

import "github.com/berachain/beacon-kit/mod/primitives/pkg/math"

type commonConsensusData struct {
proposerAddress []byte

nextPayloadTimestamp math.U64
}

func (c *commonConsensusData) GetProposerAddress() []byte {
return c.proposerAddress
}

// GetNextPayloadTimestamp returns the timestamp proposed by consensus
// for the next payload to be proposed. It is also used to bound
// current payload upon validation.
func (c *commonConsensusData) GetNextPayloadTimestamp() math.U64 {
return c.nextPayloadTimestamp
}
17 changes: 7 additions & 10 deletions mod/consensus/pkg/types/consensus_block.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,28 +29,25 @@ import (
type ConsensusBlock[BeaconBlockT any] struct {
blk BeaconBlockT

nextPayloadTimestamp math.U64
*commonConsensusData
}

// New creates a new ConsensusBlock instance.
func (b *ConsensusBlock[BeaconBlockT]) New(
beaconBlock BeaconBlockT,
proposerAddress []byte,
nextPayloadTimestamp time.Time,
) *ConsensusBlock[BeaconBlockT] {
b = &ConsensusBlock[BeaconBlockT]{
blk: beaconBlock,
nextPayloadTimestamp: math.U64(nextPayloadTimestamp.Unix()),
blk: beaconBlock,
commonConsensusData: &commonConsensusData{
proposerAddress: proposerAddress,
nextPayloadTimestamp: math.U64(nextPayloadTimestamp.Unix()),
},
}
return b
}

func (b *ConsensusBlock[BeaconBlockT]) GetBeaconBlock() BeaconBlockT {
return b.blk
}

// GetNextPayloadTimestamp returns the timestamp proposed by consensus
// for the next payload to be proposed. It is also used to bound
// current payload upon validation.
func (b *ConsensusBlock[_]) GetNextPayloadTimestamp() math.U64 {
return b.nextPayloadTimestamp
}
26 changes: 10 additions & 16 deletions mod/consensus/pkg/types/slot_data.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,24 +34,26 @@ type SlotData[AttestationDataT, SlashingInfoT any] struct {
attestationData []AttestationDataT
// slashingInfo is the slashing info of the incoming slot.
slashingInfo []SlashingInfoT
// nextPayloadTimestamp is the timestamp proposed by
// consensus for the next payload to be proposed. It is also
// used to bound current payload upon validation
nextPayloadTimestamp math.U64

*commonConsensusData
}

// New creates a new SlotData instance.
func (b *SlotData[AttestationDataT, SlashingInfoT]) New(
slot math.Slot,
attestationData []AttestationDataT,
slashingInfo []SlashingInfoT,
proposerAddress []byte,
nextPayloadTimestamp time.Time,
) *SlotData[AttestationDataT, SlashingInfoT] {
b = &SlotData[AttestationDataT, SlashingInfoT]{
slot: slot,
attestationData: attestationData,
slashingInfo: slashingInfo,
nextPayloadTimestamp: math.U64(nextPayloadTimestamp.Unix()),
slot: slot,
attestationData: attestationData,
slashingInfo: slashingInfo,
commonConsensusData: &commonConsensusData{
proposerAddress: proposerAddress,
nextPayloadTimestamp: math.U64(nextPayloadTimestamp.Unix()),
},
}
return b
}
Expand All @@ -77,14 +79,6 @@ func (b *SlotData[
return b.slashingInfo
}

// GetNextPayloadTimestamp retrieves the proposed next payload timestamp.
func (b *SlotData[
AttestationDataT,
SlashingInfoT,
]) GetNextPayloadTimestamp() math.U64 {
return b.nextPayloadTimestamp
}

// SetAttestationData sets the attestation data of the SlotData.
func (b *SlotData[AttestationDataT, SlashingInfoT]) SetAttestationData(
attestationData []AttestationDataT,
Expand Down
2 changes: 2 additions & 0 deletions mod/node-core/pkg/components/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@ type (
ConsensusBlock[BeaconBlockT any] interface {
GetBeaconBlock() BeaconBlockT

GetProposerAddress() []byte

// GetNextPayloadTimestamp returns the timestamp proposed by
// consensus for the next payload to be proposed. It is also
// used to bound current payload upon validation
Expand Down
26 changes: 16 additions & 10 deletions mod/primitives/pkg/crypto/bls.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,17 @@

package crypto

import "github.com/berachain/beacon-kit/mod/primitives/pkg/bytes"
import (
"fmt"

const (
// CometBLSType is the BLS curve type used in the Comet BFT consensus
// algorithm.
CometBLSType = "bls12_381"

// CometBLSPower is the voting power given to a validator when they
// are in the active set.
// TODO: Move this, it doesn't really belong here.
CometBLSPower = 1e10
"github.com/berachain/beacon-kit/mod/primitives/pkg/bytes"
cometencoding "github.com/cometbft/cometbft/crypto/encoding"
)

// CometBLSType is the BLS curve type used in the Comet BFT consensus
// algorithm.
const CometBLSType = "bls12_381"

//nolint:lll // link.
type (
// BLSPubkey as per the Ethereum 2.0 Specification:
Expand All @@ -44,6 +42,14 @@ type (
BLSSignature = bytes.B96
)

func GetAddressFromPubKey(pubKey BLSPubkey) ([]byte, error) {
pk, err := cometencoding.PubKeyFromTypeAndBytes(CometBLSType, pubKey[:])
if err != nil {
return nil, fmt.Errorf("failed retrieving pubKey from bytes: %w", err)
}
return pk.Address(), nil
}

// BLSSigner defines an interface for cryptographic signing operations.
// It uses generic type parameters Signature and Pubkey, both of which are
// slices of bytes.
Expand Down
6 changes: 6 additions & 0 deletions mod/primitives/pkg/transition/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ type Context struct {
// SkipValidateResult indicates whether to validate the result of
// the state transition.
SkipValidateResult bool
// Address of current block proposer
ProposerAddress []byte
// NextPayloadTimestamp is the timestamp proposed by
// consensus for the next payload to be proposed. It is also
// used to bound current payload upon validation
Expand Down Expand Up @@ -73,6 +75,10 @@ func (c *Context) GetSkipValidateResult() bool {
return c.SkipValidateResult
}

func (c *Context) GetProposerAddress() []byte {
return c.ProposerAddress
}

// GetNextPayloadTimestamp returns the timestamp proposed by consensus
// for the next payload to be proposed. It is also used to bound
// current payload upon validation.
Expand Down
2 changes: 2 additions & 0 deletions mod/state-transition/pkg/core/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ var (
// match the expected value.
ErrSlotMismatch = errors.New("slot mismatch")

ErrProposerMismatch = errors.New("proposer key mismatch")

// ErrParentRootMismatch is returned when the parent root in an execution
// payload does not match the expected value.
ErrParentRootMismatch = errors.New("parent root mismatch")
Expand Down
Loading
Loading