Skip to content

Commit

Permalink
Merge branch 'get-validators-filter' into state-finality-checkpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
nidhi-singh02 authored Nov 4, 2024
2 parents 038e26f + 47faff9 commit ad23019
Show file tree
Hide file tree
Showing 135 changed files with 3,483 additions and 2,863 deletions.
2 changes: 1 addition & 1 deletion CODEOWNERS
Validating CODEOWNERS rules …
Original file line number Diff line number Diff line change
@@ -1 +1 @@
* @itsdevbear @ocnc
* @itsdevbear
3 changes: 2 additions & 1 deletion kurtosis/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ install-kurtosis:
if [ "$$ARCH" = "x86_64" ]; then ARCH="amd64"; \
elif [ "$$ARCH" = "arm64" ]; then ARCH="arm64"; \
else echo "Unsupported architecture $$ARCH for Kurtosis installation" && exit 1; fi; \
curl -Lo kurtosis.tar.gz "https://github.com/kurtosis-tech/kurtosis-cli/releases/latest/download/kurtosis-$$OS-$$ARCH.tar.gz"; \
TAG=`curl -s "https://api.github.com/repos/kurtosis-tech/kurtosis-cli-release-artifacts/releases/latest" | grep '"tag_name":' | sed -E 's/.*"([^"]+)".*/\1/'`; \
curl -Lo kurtosis.tar.gz "https://github.com/kurtosis-tech/kurtosis-cli-release-artifacts/releases/download/$TAG/kurtosis-cli_${TAG}_${OS}_${ARCH}.tar.gz"; \
tar -xzf kurtosis.tar.gz; \
rm kurtosis.tar.gz; \
chmod +x kurtosis; \
Expand Down
20 changes: 3 additions & 17 deletions mod/beacon/blockchain/execution_engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ package blockchain

import (
"context"
"time"

payloadtime "github.com/berachain/beacon-kit/mod/beacon/payload-time"
engineprimitives "github.com/berachain/beacon-kit/mod/engine-primitives/pkg/engine-primitives"
)

Expand Down Expand Up @@ -75,11 +75,12 @@ func (s *Service[
}

prevBlockRoot := blk.HashTreeRoot()
payloadTime := blk.GetBody().GetExecutionPayload().GetTimestamp()
if _, err = s.localBuilder.RequestPayloadAsync(
ctx,
stCopy,
blk.GetSlot()+1,
s.calculateNextTimestamp(blk),
payloadtime.Next(s.chainSpec, payloadTime),
prevBlockRoot,
lph.GetBlockHash(),
lph.GetParentHash(),
Expand Down Expand Up @@ -121,18 +122,3 @@ func (s *Service[
)
}
}

// calculateNextTimestamp calculates the next timestamp for an execution
// payload.
//
// TODO: This is hood and needs to be improved.
func (s *Service[
_, BeaconBlockT, _, _, _, _, _, _, _, _,
]) calculateNextTimestamp(blk BeaconBlockT) uint64 {
//#nosec:G701 // not an issue in practice.
return max(
uint64(time.Now().Unix()+
int64(s.chainSpec.TargetSecondsPerEth1Block())),
uint64(blk.GetBody().GetExecutionPayload().GetTimestamp()+1),
)
}
38 changes: 9 additions & 29 deletions mod/beacon/blockchain/payload.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,8 @@ package blockchain

import (
"context"
"time"

"github.com/berachain/beacon-kit/mod/primitives/pkg/math"
payloadtime "github.com/berachain/beacon-kit/mod/beacon/payload-time"
)

// forceStartupHead sends a force head FCU to the execution client.
Expand Down Expand Up @@ -62,12 +61,10 @@ func (s *Service[
ctx context.Context,
st BeaconStateT,
) {
if pErr := s.rebuildPayloadForRejectedBlock(
ctx, st,
); pErr != nil {
if err := s.rebuildPayloadForRejectedBlock(ctx, st); err != nil {
s.logger.Error(
"failed to rebuild payload for nil block",
"error", pErr,
"error", err,
)
}
}
Expand All @@ -85,11 +82,6 @@ func (s *Service[
ctx context.Context,
st BeaconStateT,
) error {
var (
lph ExecutionPayloadHeaderT
slot math.Slot
)

s.logger.Info("Rebuilding payload for rejected block ⏳ ")

// In order to rebuild a payload for the current slot, we need to know the
Expand All @@ -111,7 +103,7 @@ func (s *Service[

// We need to get the *last* finalized execution payload, thus
// the BeaconState that was passed in must be `unmodified`.
lph, err = st.GetLatestExecutionPayloadHeader()
lph, err := st.GetLatestExecutionPayloadHeader()
if err != nil {
return err
}
Expand All @@ -122,12 +114,7 @@ func (s *Service[
st,
// We are rebuilding for the current slot.
stateSlot,
// TODO: this is hood as fuck.
max(
//#nosec:G701
uint64(time.Now().Unix()+1),
uint64((lph.GetTimestamp()+1)),
),
payloadtime.Next(s.chainSpec, lph.GetTimestamp()),
// We set the parent root to the previous block root.
latestHeader.HashTreeRoot(),
// We set the head of our chain to the previous finalized block.
Expand All @@ -137,10 +124,10 @@ func (s *Service[
// and possibly should be made more explicit later on.
lph.GetParentHash(),
); err != nil {
s.metrics.markRebuildPayloadForRejectedBlockFailure(slot, err)
s.metrics.markRebuildPayloadForRejectedBlockFailure(stateSlot, err)
return err
}
s.metrics.markRebuildPayloadForRejectedBlockSuccess(slot)
s.metrics.markRebuildPayloadForRejectedBlockSuccess(stateSlot)
return nil
}

Expand Down Expand Up @@ -180,9 +167,7 @@ func (s *Service[
)

// We process the slot to update any RANDAO values.
if _, err := s.stateProcessor.ProcessSlots(
st, slot,
); err != nil {
if _, err := s.stateProcessor.ProcessSlots(st, slot); err != nil {
return err
}

Expand All @@ -191,12 +176,7 @@ func (s *Service[
if _, err := s.localBuilder.RequestPayloadAsync(
ctx, st,
slot,
// TODO: this is hood as fuck.
max(
//#nosec:G701
uint64(time.Now().Unix()+int64(s.chainSpec.TargetSecondsPerEth1Block())),
uint64((payload.GetTimestamp()+1)),
),
payloadtime.Next(s.chainSpec, payload.GetTimestamp()),
// The previous block root is simply the root of the block we just
// processed.
blk.HashTreeRoot(),
Expand Down
14 changes: 8 additions & 6 deletions mod/beacon/blockchain/process.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,6 @@ func (s *Service[
return nil, ErrNilBlk
}

// We set `OptimisticEngine` to true since this is called during
// FinalizeBlock. We want to assume the payload is valid. If it
// ends up not being valid later, the node will simply AppHash,
// which is completely fine. This means we were syncing from a
// bad peer, and we would likely AppHash anyways.
st := s.storageBackend.StateFromContext(ctx)
valUpdates, err := s.executeStateTransition(ctx, st, blk)
if err != nil {
Expand Down Expand Up @@ -107,8 +102,15 @@ func (s *Service[
defer s.metrics.measureStateTransitionDuration(startTime)
valUpdates, err := s.stateProcessor.Transition(
&transition.Context{
Context: ctx,
Context: ctx,

// We set `OptimisticEngine` to true since this is called during
// FinalizeBlock. We want to assume the payload is valid. If it
// ends up not being valid later, the node will simply AppHash,
// which is completely fine. This means we were syncing from a
// bad peer, and we would likely AppHash anyways.
OptimisticEngine: true,

// When we are NOT synced to the tip, process proposal
// does NOT get called and thus we must ensure that
// NewPayload is called to get the execution
Expand Down
6 changes: 2 additions & 4 deletions mod/beacon/blockchain/receive.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,16 +59,14 @@ func (s *Service[
"state_root", blk.GetStateRoot(), "slot", blk.GetSlot(),
)

// We purposefully make a copy of the BeaconState in orer
// We purposefully make a copy of the BeaconState in order
// to avoid modifying the underlying state, for the event in which
// we have to rebuild a payload for this slot again, if we do not agree
// with the incoming block.
postState := preState.Copy()

// Verify the state root of the incoming block.
if err := s.verifyStateRoot(
ctx, postState, blk,
); err != nil {
if err := s.verifyStateRoot(ctx, postState, blk); err != nil {
s.logger.Error(
"Rejecting incoming beacon block ❌ ",
"state_root",
Expand Down
4 changes: 2 additions & 2 deletions mod/beacon/blockchain/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -255,8 +255,8 @@ func (s *Service[
return
}

// emit a BeaconBlockVerified event with the error result from \
// VerifyIncomingBlock
// emit a BeaconBlockVerified event with
// the error result from VerifyIncomingBlock
if err := s.dispatcher.Publish(
async.NewEvent(
msg.Context(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,26 @@
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, NON-INFRINGEMENT, AND
// TITLE.

package hex
package payloadtime

import (
"encoding"
"time"

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

// UnmarshalJSONText unmarshals a JSON string with a 0x prefix into a given
// TextUnmarshaler. It validates the input and then removes the surrounding
// quotes before passing the inner content to the UnmarshalText method.
func UnmarshalJSONText(input []byte,
u encoding.TextUnmarshaler,
) error {
if err := ValidateUnmarshalInput(input); err != nil {
return err
}
return u.UnmarshalText(input[1 : len(input)-1])
// Next calculates the
// next timestamp for an execution payload
//
// TODO: This is hood and needs to be improved.
func Next(
chainSpec common.ChainSpec,
parentPayloadTime math.U64,
) uint64 {
//#nosec:G701 // not an issue in practice.
return max(
uint64(time.Now().Unix())+chainSpec.TargetSecondsPerEth1Block(),
uint64(parentPayloadTime+1),
)
}
18 changes: 10 additions & 8 deletions mod/beacon/validator/block_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,10 @@ package validator

import (
"context"
"fmt"
"time"

payloadtime "github.com/berachain/beacon-kit/mod/beacon/payload-time"
engineprimitives "github.com/berachain/beacon-kit/mod/engine-primitives/pkg/engine-primitives"
"github.com/berachain/beacon-kit/mod/primitives/pkg/bytes"
"github.com/berachain/beacon-kit/mod/primitives/pkg/common"
Expand Down Expand Up @@ -221,7 +223,7 @@ func (s *Service[
return nil, err
}

// If we failed to retrieve the payload, request a synchrnous payload.
// If we failed to retrieve the payload, request a synchronous payload.
//
// NOTE: The state here is properly configured by the
// prepareStateForBuilding
Expand All @@ -233,12 +235,7 @@ func (s *Service[
ctx,
st,
blk.GetSlot(),
// TODO: this is hood.
max(
//#nosec:G701
uint64(time.Now().Unix()+1),
uint64((lph.GetTimestamp()+1)),
),
payloadtime.Next(s.chainSpec, lph.GetTimestamp()),
blk.GetParentBlockRoot(),
lph.GetBlockHash(),
lph.GetParentHash(),
Expand Down Expand Up @@ -303,7 +300,12 @@ func (s *Service[
))

// Set the graffiti on the block body.
body.SetGraffiti(bytes.ToBytes32([]byte(s.cfg.Graffiti)))
sizedGraffiti := bytes.ExtendToSize([]byte(s.cfg.Graffiti), bytes.B32Size)
graffiti, err := bytes.ToBytes32(sizedGraffiti)
if err != nil {
return fmt.Errorf("failed processing graffiti: %w", err)
}
body.SetGraffiti(graffiti)

// Get the epoch to find the active fork version.
epoch := s.chainSpec.SlotToEpoch(blk.GetSlot())
Expand Down
28 changes: 19 additions & 9 deletions mod/cli/pkg/commands/genesis/payload.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
package genesis

import (
"fmt"
"unsafe"

"github.com/berachain/beacon-kit/mod/cli/pkg/context"
Expand Down Expand Up @@ -93,11 +94,15 @@ func AddExecutionPayloadCmd(chainSpec common.ChainSpec) *cobra.Command {
}

// Inject the execution payload.
genesisInfo.ExecutionPayloadHeader = executableDataToExecutionPayloadHeader(
version.ToUint32(genesisInfo.ForkVersion),
payload,
chainSpec.MaxWithdrawalsPerPayload(),
)
genesisInfo.ExecutionPayloadHeader, err =
executableDataToExecutionPayloadHeader(
version.ToUint32(genesisInfo.ForkVersion),
payload,
chainSpec.MaxWithdrawalsPerPayload(),
)
if err != nil {
return errors.Wrap(err, "failed to unmarshal beacon state")
}

appGenesisState["beacon"], err = json.Marshal(genesisInfo)
if err != nil {
Expand All @@ -124,7 +129,7 @@ func executableDataToExecutionPayloadHeader(
data *gethprimitives.ExecutableData,
// todo: re-enable when codec supports.
_ uint64,
) *types.ExecutionPayloadHeader {
) (*types.ExecutionPayloadHeader, error) {
var executionPayloadHeader *types.ExecutionPayloadHeader
switch forkVersion {
case version.Deneb, version.DenebPlus:
Expand Down Expand Up @@ -155,6 +160,11 @@ func executableDataToExecutionPayloadHeader(
excessBlobGas = *data.ExcessBlobGas
}

baseFeePerGas, err := math.NewU256FromBigInt(data.BaseFeePerGas)
if err != nil {
return nil, fmt.Errorf("failed baseFeePerGas conversion: %w", err)
}

executionPayloadHeader = &types.ExecutionPayloadHeader{
ParentHash: common.ExecutionHash(data.ParentHash),
FeeRecipient: common.ExecutionAddress(data.FeeRecipient),
Expand All @@ -167,7 +177,7 @@ func executableDataToExecutionPayloadHeader(
GasUsed: math.U64(data.GasUsed),
Timestamp: math.U64(data.Timestamp),
ExtraData: data.ExtraData,
BaseFeePerGas: math.NewU256FromBigInt(data.BaseFeePerGas),
BaseFeePerGas: baseFeePerGas,
BlockHash: common.ExecutionHash(data.BlockHash),
// TODO: Decouple from broken bArtio.
TransactionsRoot: engineprimitives.
Expand All @@ -180,8 +190,8 @@ func executableDataToExecutionPayloadHeader(
ExcessBlobGas: math.U64(excessBlobGas),
}
default:
panic("unsupported fork version")
return nil, types.ErrForkVersionNotSupported
}

return executionPayloadHeader
return executionPayloadHeader, nil
}
Loading

0 comments on commit ad23019

Please sign in to comment.