Skip to content

Commit

Permalink
Merge branch 'main' into fix-and-run-make-tidy
Browse files Browse the repository at this point in the history
  • Loading branch information
abi87 authored Nov 13, 2024
2 parents 2997508 + 666ec28 commit 8f9ffe2
Show file tree
Hide file tree
Showing 22 changed files with 180 additions and 31 deletions.
2 changes: 1 addition & 1 deletion beacond/cmd/defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ func DefaultComponents() []any {
*BeaconBlock, *BeaconBlockBody, *BeaconBlockHeader,
],
components.ProvideStateProcessor[
*BeaconBlock, *BeaconBlockBody, *BeaconBlockHeader,
*Logger, *BeaconBlock, *BeaconBlockBody, *BeaconBlockHeader,
*BeaconState, *BeaconStateMarshallable, *Deposit, *ExecutionPayload,
*ExecutionPayloadHeader, *KVStore,
],
Expand Down
16 changes: 15 additions & 1 deletion mod/beacon/blockchain/execution_engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ package blockchain

import (
"context"
"time"

"github.com/berachain/beacon-kit/mod/config/pkg/spec"
engineprimitives "github.com/berachain/beacon-kit/mod/engine-primitives/pkg/engine-primitives"
)

Expand Down Expand Up @@ -74,12 +76,24 @@ func (s *Service[
return
}

nextPayloadTime := blk.GetNextPayloadTimestamp().Unwrap()

// We set timestamp check on Bartio for backward compatibility reasons
// TODO: drop this we drop other Bartio special cases.
if s.chainSpec.DepositEth1ChainID() == spec.BartioChainID {
nextPayloadTime = max(
//#nosec:G701
uint64(time.Now().Unix()+1),
uint64((lph.GetTimestamp() + 1)),
)
}

prevBlockRoot := beaconBlk.HashTreeRoot()
if _, err := s.localBuilder.RequestPayloadAsync(
ctx,
stCopy,
beaconBlk.GetSlot()+1,
blk.GetNextPayloadTimestamp().Unwrap(),
nextPayloadTime,
prevBlockRoot,
lph.GetBlockHash(),
lph.GetParentHash(),
Expand Down
1 change: 1 addition & 0 deletions mod/beacon/blockchain/process.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ func (s *Service[

ProposerAddress: blk.GetProposerAddress(),
NextPayloadTimestamp: blk.GetNextPayloadTimestamp(),
ConsensusBlockHeight: blk.GetConsensusBlockHeight(),
},
st,
blk.GetBeaconBlock(),
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 @@ -130,6 +130,7 @@ func (s *Service[
SkipValidateRandao: false,
ProposerAddress: blk.GetProposerAddress(),
NextPayloadTimestamp: blk.GetNextPayloadTimestamp(),
ConsensusBlockHeight: blk.GetConsensusBlockHeight(),
},
st, blk.GetBeaconBlock(),
)
Expand Down
5 changes: 5 additions & 0 deletions mod/beacon/blockchain/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,11 @@ type ConsensusBlock[BeaconBlockT any] interface {
// consensus for the next payload to be proposed. It is also
// used to bound current payload upon validation
GetNextPayloadTimestamp() math.U64

// GetConsensusBlockHeight returns the height of consensus block,
// which may be different from execution payload height
// in some networks. Currently only used for logging
GetConsensusBlockHeight() math.U64
}

// BeaconBlock represents a beacon block interface.
Expand Down
5 changes: 5 additions & 0 deletions mod/beacon/validator/block_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ func (s *Service[
ctx,
slotData.GetProposerAddress(),
slotData.GetNextPayloadTimestamp(),
slotData.GetConsensusBlockHeight(),
st,
blk,
)
Expand Down Expand Up @@ -339,13 +340,15 @@ func (s *Service[
ctx context.Context,
proposerAddress []byte,
nextPayloadTimestamp math.U64,
consensusBlockHeight math.U64,
st BeaconStateT,
blk BeaconBlockT,
) error {
stateRoot, err := s.computeStateRoot(
ctx,
proposerAddress,
nextPayloadTimestamp,
consensusBlockHeight,
st,
blk,
)
Expand All @@ -368,6 +371,7 @@ func (s *Service[
ctx context.Context,
proposerAddress []byte,
nextPayloadTimestamp math.U64,
consensusBlockHeight math.U64,
st BeaconStateT,
blk BeaconBlockT,
) (common.Root, error) {
Expand All @@ -385,6 +389,7 @@ func (s *Service[
SkipValidateRandao: true,
ProposerAddress: proposerAddress,
NextPayloadTimestamp: nextPayloadTimestamp,
ConsensusBlockHeight: consensusBlockHeight,
},
st, blk,
); err != nil {
Expand Down
5 changes: 5 additions & 0 deletions mod/beacon/validator/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,11 @@ type SlotData[AttestationDataT, SlashingInfoT any] interface {
// consensus for the next payload to be proposed. It is also
// used to bound current payload upon validation
GetNextPayloadTimestamp() math.U64

// GetConsensusBlockHeight returns the height of consensus block,
// which may be different from execution payload height
// in some networks. Currently only used for logging.
GetConsensusBlockHeight() math.U64
}

// StateProcessor defines the interface for processing the state.
Expand Down
30 changes: 30 additions & 0 deletions mod/config/pkg/spec/special_cases.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// 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 spec

// Special cased Bartio for some ad-hoc handling due to the way
// some bugs were handled on Bartio. To be removed.
const (
BartioChainID uint64 = 80084

//nolint:lll // temporary.
BArtioValRoot = "0x9147586693b6e8faa837715c0f3071c2000045b54233901c2e7871b15872bc43"
)
17 changes: 12 additions & 5 deletions mod/consensus-types/pkg/types/header.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,11 +176,18 @@ func (b *BeaconBlockHeader) GetTree() (*fastssz.Node, error) {

// Equals returns true if the Withdrawal is equal to the other.
func (b *BeaconBlockHeader) Equals(rhs *BeaconBlockHeader) bool {
return b.Slot == rhs.Slot &&
b.ProposerIndex == rhs.ProposerIndex &&
b.ParentBlockRoot == rhs.ParentBlockRoot &&
b.StateRoot == rhs.StateRoot &&
b.BodyRoot == rhs.BodyRoot
switch {
case b == nil && rhs == nil:
return true
case b != nil && rhs != nil:
return b.Slot == rhs.Slot &&
b.ProposerIndex == rhs.ProposerIndex &&
b.ParentBlockRoot == rhs.ParentBlockRoot &&
b.StateRoot == rhs.StateRoot &&
b.BodyRoot == rhs.BodyRoot
default:
return false
}
}

// GetSlot retrieves the slot of the BeaconBlockHeader.
Expand Down
4 changes: 2 additions & 2 deletions mod/consensus-types/pkg/types/payload.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
package types

import (
"github.com/berachain/beacon-kit/mod/config/pkg/spec"
engineprimitives "github.com/berachain/beacon-kit/mod/engine-primitives/pkg/engine-primitives"
"github.com/berachain/beacon-kit/mod/errors"
"github.com/berachain/beacon-kit/mod/primitives/pkg/bytes"
Expand Down Expand Up @@ -569,8 +570,7 @@ func (p *ExecutionPayload) ToHeader(

// TODO: This is live on bArtio with a bug and needs to be hardforked
// off of. This is a temporary solution to avoid breaking changes.
//nolint:mnd // don't want the circular dep.
if eth1ChainID == 80084 {
if eth1ChainID == spec.BartioChainID {
txsRoot = engineprimitives.BartioTransactions(
p.GetTransactions(),
).HashTreeRoot()
Expand Down
12 changes: 12 additions & 0 deletions mod/consensus/pkg/types/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,14 @@ package types
import "github.com/berachain/beacon-kit/mod/primitives/pkg/math"

type commonConsensusData struct {
// use to verify block builder
proposerAddress []byte

// used to build next block and validate current payload timestamp
nextPayloadTimestamp math.U64

// useful for logging
consensusBlkHeight math.U64
}

// GetProposerAddress returns the address of the validator
Expand All @@ -40,3 +45,10 @@ func (c *commonConsensusData) GetProposerAddress() []byte {
func (c *commonConsensusData) GetNextPayloadTimestamp() math.U64 {
return c.nextPayloadTimestamp
}

// GetConsensusBlockHeight returns the height of consensus block,
// which may be different from execution payload height
// in some networks. Currently only used for logging.
func (c *commonConsensusData) GetConsensusBlockHeight() math.U64 {
return c.consensusBlkHeight
}
17 changes: 12 additions & 5 deletions mod/engine-primitives/pkg/engine-primitives/withdrawal.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,11 +164,18 @@ func (w Withdrawal) EncodeRLP(_w io.Writer) error {
/* -------------------------------------------------------------------------- */

// Equals returns true if the Withdrawal is equal to the other.
func (w *Withdrawal) Equals(other *Withdrawal) bool {
return w.Index == other.Index &&
w.Validator == other.Validator &&
w.Address == other.Address &&
w.Amount == other.Amount
func (w *Withdrawal) Equals(rhs *Withdrawal) bool {
switch {
case w == nil && rhs == nil:
return true
case w != nil && rhs != nil:
return w.Index == rhs.Index &&
w.Validator == rhs.Validator &&
w.Address == rhs.Address &&
w.Amount == rhs.Amount
default:
return false
}
}

// GetIndex returns the unique identifier for the withdrawal.
Expand Down
5 changes: 5 additions & 0 deletions mod/node-core/pkg/components/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,11 @@ type (
// consensus for the next payload to be proposed. It is also
// used to bound current payload upon validation
GetNextPayloadTimestamp() math.U64

// GetConsensusBlockHeight returns the height of consensus block,
// which may be different from execution payload height
// in some networks. Currently only used for logging
GetConsensusBlockHeight() math.U64
}

// BeaconBlock represents a generic interface for a beacon block.
Expand Down
9 changes: 8 additions & 1 deletion mod/node-core/pkg/components/state_processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"cosmossdk.io/depinject"
engineprimitives "github.com/berachain/beacon-kit/mod/engine-primitives/pkg/engine-primitives"
"github.com/berachain/beacon-kit/mod/execution/pkg/engine"
"github.com/berachain/beacon-kit/mod/log"
"github.com/berachain/beacon-kit/mod/primitives/pkg/common"
"github.com/berachain/beacon-kit/mod/primitives/pkg/crypto"
"github.com/berachain/beacon-kit/mod/state-transition/pkg/core"
Expand All @@ -32,6 +33,7 @@ import (
// StateProcessorInput is the input for the state processor for the depinject
// framework.
type StateProcessorInput[
LoggerT log.AdvancedLogger[LoggerT],
ExecutionPayloadT ExecutionPayload[
ExecutionPayloadT, ExecutionPayloadHeaderT, WithdrawalsT,
],
Expand All @@ -40,6 +42,7 @@ type StateProcessorInput[
WithdrawalsT Withdrawals[WithdrawalT],
] struct {
depinject.In
Logger LoggerT
ChainSpec common.ChainSpec
ExecutionEngine *engine.Engine[
ExecutionPayloadT,
Expand All @@ -53,6 +56,7 @@ type StateProcessorInput[
// ProvideStateProcessor provides the state processor to the depinject
// framework.
func ProvideStateProcessor[
LoggerT log.AdvancedLogger[LoggerT],
BeaconBlockT BeaconBlock[BeaconBlockT, BeaconBlockBodyT, BeaconBlockHeaderT],
BeaconBlockBodyT BeaconBlockBody[
BeaconBlockBodyT, *AttestationData, DepositT,
Expand All @@ -78,7 +82,9 @@ func ProvideStateProcessor[
WithdrawalT Withdrawal[WithdrawalT],
](
in StateProcessorInput[
ExecutionPayloadT, ExecutionPayloadHeaderT, WithdrawalT, WithdrawalsT,
LoggerT,
ExecutionPayloadT, ExecutionPayloadHeaderT,
WithdrawalT, WithdrawalsT,
],
) *core.StateProcessor[
BeaconBlockT, BeaconBlockBodyT, BeaconBlockHeaderT,
Expand All @@ -105,6 +111,7 @@ func ProvideStateProcessor[
WithdrawalsT,
WithdrawalCredentials,
](
in.Logger.With("service", "state-processor"),
in.ChainSpec,
in.ExecutionEngine,
in.Signer,
Expand Down
12 changes: 12 additions & 0 deletions mod/primitives/pkg/transition/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,11 @@ type Context struct {
// consensus for the next payload to be proposed. It is also
// used to bound current payload upon validation
NextPayloadTimestamp math.U64

// ConsensusBlockHeight is the height of consensus block,
// which may be different from execution payload height
// in some networks. Currently only used for logging
ConsensusBlockHeight math.U64
}

// GetOptimisticEngine returns whether to optimistically assume the execution
Expand Down Expand Up @@ -88,6 +93,13 @@ func (c *Context) GetNextPayloadTimestamp() math.U64 {
return c.NextPayloadTimestamp
}

// GetConsensusBlockHeight returns the height of consensus block,
// which may be different from execution payload height
// in some networks. Currently only used for logging.
func (c *Context) GetConsensusBlockHeight() math.U64 {
return c.ConsensusBlockHeight
}

// Unwrap returns the underlying standard context.
func (c *Context) Unwrap() context.Context {
return c.Context
Expand Down
2 changes: 2 additions & 0 deletions mod/state-transition/pkg/core/helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import (
storetypes "cosmossdk.io/store/types"
"github.com/berachain/beacon-kit/mod/consensus-types/pkg/types"
engineprimitives "github.com/berachain/beacon-kit/mod/engine-primitives/pkg/engine-primitives"
"github.com/berachain/beacon-kit/mod/log/pkg/noop"
"github.com/berachain/beacon-kit/mod/node-core/pkg/components"
"github.com/berachain/beacon-kit/mod/primitives/pkg/common"
"github.com/berachain/beacon-kit/mod/primitives/pkg/crypto"
Expand Down Expand Up @@ -130,6 +131,7 @@ func createStateProcessor(
engineprimitives.Withdrawals,
types.WithdrawalCredentials,
](
noop.NewLogger[any](),
cs,
execEngine,
signer,
Expand Down
15 changes: 15 additions & 0 deletions mod/state-transition/pkg/core/state/statedb.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
package state

import (
"github.com/berachain/beacon-kit/mod/config/pkg/spec"
"github.com/berachain/beacon-kit/mod/errors"
"github.com/berachain/beacon-kit/mod/primitives/pkg/common"
"github.com/berachain/beacon-kit/mod/primitives/pkg/math"
Expand Down Expand Up @@ -244,6 +245,8 @@ func (s *StateDB[
// Set the amount of the withdrawal depending on the balance of the
// validator.
var withdrawal WithdrawalT

//nolint:gocritic // ok.
if validator.IsFullyWithdrawable(balance, epoch) {
withdrawals = append(withdrawals, withdrawal.New(
math.U64(withdrawalIndex),
Expand All @@ -266,6 +269,18 @@ func (s *StateDB[

// Increment the withdrawal index to process the next withdrawal.
withdrawalIndex++
} else if s.cs.DepositEth1ChainID() == spec.BartioChainID {
// Backward compatibility with Bartio
// TODO: Drop this when we drop other Bartio special cases.
withdrawal = withdrawal.New(
math.U64(withdrawalIndex),
validatorIndex,
withdrawalAddress,
0,
)

withdrawals = append(withdrawals, withdrawal)
withdrawalIndex++
}

// Cap the number of withdrawals to the maximum allowed per payload.
Expand Down
Loading

0 comments on commit 8f9ffe2

Please sign in to comment.