Skip to content

Commit

Permalink
fix and clean up
Browse files Browse the repository at this point in the history
  • Loading branch information
domiwei committed Jun 20, 2024
1 parent 189309f commit 483e3a3
Show file tree
Hide file tree
Showing 11 changed files with 97 additions and 62 deletions.
28 changes: 14 additions & 14 deletions cl/beacon/builder/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ import (

var _ BuilderClient = &builderClient{}

var (
ErrNoContent = fmt.Errorf("no http content")
)

type builderClient struct {
// ref: https://ethereum.github.io/builder-specs/#/
httpClient *http.Client
Expand Down Expand Up @@ -53,9 +57,7 @@ func (b *builderClient) RegisterValidator(ctx context.Context, registers []*clty
}
_, err = httpCall[json.RawMessage](ctx, b.httpClient, http.MethodPost, url, nil, bytes.NewBuffer(payload))
if err != nil {
log.Warn("[mev builder] httpCall error", "err", err)
} else {
log.Trace("[mev builder] RegisterValidator", "payload", string(payload))
log.Warn("[mev builder] httpCall error on RegisterValidator", "err", err)
}
return err
}
Expand All @@ -66,16 +68,9 @@ func (b *builderClient) GetExecutionPayloadHeader(ctx context.Context, slot int6
url := b.url.JoinPath(path).String()
header, err := httpCall[ExecutionPayloadHeader](ctx, b.httpClient, http.MethodGet, url, nil, nil)
if err != nil {
log.Warn("[mev builder] httpCall error", "err", err, "path", path)
log.Warn("[mev builder] httpCall error on GetExecutionPayloadHeader", "err", err, "path", path)
return nil, err
}
builderHeaderBytes, err := json.Marshal(header)
if err != nil {
log.Warn("[mev builder] json.Marshal error", "err", err)
return nil, err
} else {
log.Info("[mev builder] builderHeaderBytes", "builderHeaderBytes", string(builderHeaderBytes))
}
return header, nil
}

Expand All @@ -92,7 +87,7 @@ func (b *builderClient) SubmitBlindedBlocks(ctx context.Context, block *cltypes.
}
resp, err := httpCall[BlindedBlockResponse](ctx, b.httpClient, http.MethodPost, url, headers, bytes.NewBuffer(payload))
if err != nil {
log.Warn("[mev builder] httpCall error", "headers", headers, "err", err, "payload", string(payload))
log.Warn("[mev builder] httpCall error on SubmitBlindedBlocks", "headers", headers, "err", err, "payload", string(payload))
return nil, nil, err
}

Expand Down Expand Up @@ -150,16 +145,21 @@ func httpCall[T any](ctx context.Context, client *http.Client, method, url strin
bytes, err := io.ReadAll(response.Body)
if err != nil {
log.Warn("[mev builder] io.ReadAll failed", "err", err, "url", url, "method", method)
} else {
log.Debug("[mev builder] httpCall failed", "status", response.Status, "content", string(bytes))
}
return nil, fmt.Errorf("status code: %d. Response content %v", response.StatusCode, string(bytes))
return nil, fmt.Errorf("status code: %d", response.StatusCode)
}
if response.StatusCode == http.StatusNoContent {
return nil, ErrNoContent
}

// read response body
bytes, err := io.ReadAll(response.Body)
if err != nil {
log.Warn("[mev builder] io.ReadAll failed", "err", err, "url", url, "method", method)
return nil, err
}
log.Info("[mev builder] httpCall success", "url", url, "method", method, "statusCode", response.StatusCode)

var body T
if len(bytes) == 0 {
Expand Down
57 changes: 22 additions & 35 deletions cl/beacon/handler/block_production.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,6 @@ func (a *ApiHandler) GetEthV3ValidatorBlock(
) (*beaconhttp.BeaconResponse, error) {
ctx := r.Context()
// parse request data

randaoRevealString := r.URL.Query().Get("randao_reveal")
var randaoReveal common.Bytes96
if err := randaoReveal.UnmarshalText([]byte(randaoRevealString)); err != nil {
Expand Down Expand Up @@ -153,11 +152,13 @@ func (a *ApiHandler) GetEthV3ValidatorBlock(

baseBlockRoot, err := s.BlockRoot()
if err != nil {
log.Warn("Failed to get block root", "err", err)
return nil, err
}

sourceBlock, err := a.blockReader.ReadBlockByRoot(ctx, tx, baseBlockRoot)
if err != nil {
log.Warn("Failed to get source block", "err", err, "root", baseBlockRoot)
return nil, err
}
if sourceBlock == nil {
Expand All @@ -184,41 +185,27 @@ func (a *ApiHandler) GetEthV3ValidatorBlock(
}
block, err := a.produceBlock(ctx, builderBoostFactor, sourceBlock.Block, baseState, targetSlot, randaoReveal, graffiti)
if err != nil {
log.Warn("Failed to produce block", "err", err, "slot", targetSlot)
return nil, err
}

// do state transition
if block.IsBlinded() {
signedBlock := &cltypes.SignedBlindedBeaconBlock{
Block: &cltypes.BlindedBeaconBlock{
Slot: block.Slot,
ProposerIndex: block.ProposerIndex,
ParentRoot: block.ParentRoot,
Body: block.BlindedBeaconBody,
},
}
if err := machine.ProcessBlindedBlock(transition.DefaultMachine, baseState, signedBlock); err != nil {
if err := machine.ProcessBlindedBlock(transition.DefaultMachine, baseState, block.ToBlinded()); err != nil {
log.Warn("Failed to process blinded block", "err", err, "slot", targetSlot)
return nil, err
}
} else {
signedBeaconBlock := &cltypes.SignedBeaconBlock{
Block: &cltypes.BeaconBlock{
Slot: block.Slot,
ProposerIndex: block.ProposerIndex,
ParentRoot: block.ParentRoot,
Body: block.BeaconBody,
},
}
if err := machine.ProcessBlock(transition.DefaultMachine, baseState, signedBeaconBlock); err != nil {
if err := machine.ProcessBlock(transition.DefaultMachine, baseState, block.ToExecution()); err != nil {
log.Warn("Failed to process execution block", "err", err, "slot", targetSlot)
return nil, err
}
}
block.StateRoot, err = baseState.HashSSZ()
if err != nil {
log.Warn("Failed to get state root", "err", err)
return nil, err
}
if block.IsBlinded() {
log.Info("BlockProduction: produced a blinded block")
}

log.Info("BlockProduction: Block produced",
"proposerIndex", block.ProposerIndex,
Expand Down Expand Up @@ -280,16 +267,19 @@ func (a *ApiHandler) produceBlock(
)
go func() {
defer wg.Done()
builderHeader, builderErr = a.getBuilderPayload(ctx, baseBlock, baseState, targetSlot)
if builderErr != nil && builderErr != errBuilderNotEnabled {
log.Error("Failed to get builder payload", "err", builderErr)
if a.routerCfg.Builder && a.builderClient != nil {
builderHeader, builderErr = a.getBuilderPayload(ctx, baseBlock, baseState, targetSlot)
if builderErr != nil && builderErr != errBuilderNotEnabled {
log.Warn("Failed to get builder payload", "err", builderErr)
}
}
}()
// wait for both tasks to finish
wg.Wait()

if localErr != nil {
// if we failed to locally produce the beacon body, we should not proceed with the block production
log.Error("Failed to produce beacon body", "err", localErr)
log.Error("Failed to produce beacon body", "err", localErr, "slot", targetSlot)
return nil, localErr
}
// prepare basic block
Expand Down Expand Up @@ -322,7 +312,8 @@ func (a *ApiHandler) produceBlock(
builderValue := builderHeader.BlockValue()
boostFactorBig := new(big.Int).SetUint64(boostFactor)
useLocalExec := new(big.Int).Mul(execValue, big.NewInt(100)).Cmp(new(big.Int).Mul(builderValue, boostFactorBig)) >= 0
log.Info("[mev] check bid", "useLocalExec", useLocalExec, "execValue", execValue, "builderValue", builderValue, "boostFactor", boostFactor, "targetSlot", targetSlot)
log.Info("Check mev bid", "useLocalExec", useLocalExec, "execValue", execValue, "builderValue", builderValue, "boostFactor", boostFactor, "targetSlot", targetSlot)

if useLocalExec {
block.BeaconBody = beaconBody
block.ExecutionValue = execValue
Expand All @@ -337,10 +328,10 @@ func (a *ApiHandler) produceBlock(
for i := 0; i < builderHeader.Data.Message.BlobKzgCommitments.Len(); i++ {
c := builderHeader.Data.Message.BlobKzgCommitments.Get(i)
cpy := cltypes.KZGCommitment{}
copy(cpy[:], (*c)[:])
copy(cpy[:], c[:])
cpyCommitments.Append(&cpy)
}

// setup blinded block
block.BlindedBeaconBody = blindedBody.
SetHeader(builderHeader.Data.Message.Header).
SetBlobKzgCommitments(cpyCommitments)
Expand Down Expand Up @@ -374,7 +365,7 @@ func (a *ApiHandler) getBuilderPayload(
if err != nil {
return nil, err
} else if header == nil {
return nil, fmt.Errorf("nil header")
return nil, fmt.Errorf("no error but nil header")
}

// check the version
Expand Down Expand Up @@ -777,7 +768,7 @@ func (a *ApiHandler) publishBlindedBlocks(w http.ResponseWriter, r *http.Request
// todo: broadcast_validation

signedBlindedBlock := cltypes.NewSignedBlindedBeaconBlock(a.beaconChainCfg)
signedBlindedBlock.Block.Body.Version = version
signedBlindedBlock.Block.SetVersion(version)
b, err := io.ReadAll(r.Body)
defer r.Body.Close()
if err != nil {
Expand Down Expand Up @@ -813,10 +804,6 @@ func (a *ApiHandler) publishBlindedBlocks(w http.ResponseWriter, r *http.Request
return nil, beaconhttp.NewEndpointError(http.StatusBadRequest, fmt.Errorf("commitments length mismatch"))
}
for i := range blobsBundle.Commitments {
//blockCommitment := blockCommitments.Get(i)
/*if !bytes.Equal(commitment, blockCommitment[:]) {
return nil, beaconhttp.NewEndpointError(http.StatusBadRequest, fmt.Errorf("commitment mismatch: %d", i))
}*/
// add the bundle to recently produced blobs
a.blobBundles.Add(libcommon.Bytes48(blobsBundle.Commitments[i]), BlobBundle{
Blob: (*cltypes.Blob)(blobsBundle.Blobs[i]),
Expand Down
4 changes: 4 additions & 0 deletions cl/cltypes/aggregate.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,10 @@ type SyncAggregate struct {
SyncCommiteeSignature libcommon.Bytes96 `json:"sync_committee_signature"`
}

func NewSyncAggregate() *SyncAggregate {
return &SyncAggregate{}
}

// return sum of the committee bits
func (agg *SyncAggregate) Sum() int {
ret := 0
Expand Down
8 changes: 8 additions & 0 deletions cl/cltypes/beacon_block.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,14 @@ func (b *BeaconBlock) Version() clparams.StateVersion {
return b.Body.Version
}

func (b *BeaconBlock) SetVersion(version clparams.StateVersion) {
b.Body.SetVersion(version)
}

func (b *BeaconBody) SetVersion(version clparams.StateVersion) {
b.Version = version
}

func (b *BeaconBody) EncodeSSZ(dst []byte) ([]byte, error) {
return ssz2.MarshalSSZ(dst, b.getSchema(false)...)
}
Expand Down
39 changes: 36 additions & 3 deletions cl/cltypes/blinded_beacon_block.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,10 @@ type BlindedBeaconBody struct {
// Getters

func NewSignedBlindedBeaconBlock(beaconCfg *clparams.BeaconChainConfig) *SignedBlindedBeaconBlock {
return &SignedBlindedBeaconBlock{Block: NewBlindedBeaconBlock(beaconCfg)}
return &SignedBlindedBeaconBlock{
Signature: libcommon.Bytes96{},
Block: NewBlindedBeaconBlock(beaconCfg),
}
}

func (s *SignedBlindedBeaconBlock) SignedBeaconBlockHeader() *SignedBeaconBlockHeader {
Expand All @@ -81,12 +84,27 @@ func (s *SignedBlindedBeaconBlock) SignedBeaconBlockHeader() *SignedBeaconBlockH
}

func NewBlindedBeaconBlock(beaconCfg *clparams.BeaconChainConfig) *BlindedBeaconBlock {
return &BlindedBeaconBlock{Body: NewBlindedBeaconBody(beaconCfg)}
return &BlindedBeaconBlock{
Body: NewBlindedBeaconBody(beaconCfg),
}
}

func NewBlindedBeaconBody(beaconCfg *clparams.BeaconChainConfig) *BlindedBeaconBody {
return &BlindedBeaconBody{
beaconCfg: beaconCfg,
RandaoReveal: libcommon.Bytes96{},
Eth1Data: NewEth1Data(),
Graffiti: libcommon.Hash{},
ProposerSlashings: solid.NewStaticListSSZ[*ProposerSlashing](MaxProposerSlashings, 416),
AttesterSlashings: solid.NewDynamicListSSZ[*AttesterSlashing](MaxAttesterSlashings),
Attestations: solid.NewDynamicListSSZ[*solid.Attestation](MaxAttestations),
Deposits: solid.NewStaticListSSZ[*Deposit](MaxDeposits, 1240),
VoluntaryExits: solid.NewStaticListSSZ[*SignedVoluntaryExit](MaxVoluntaryExits, 112),
SyncAggregate: NewSyncAggregate(),
ExecutionPayload: nil,
ExecutionChanges: solid.NewStaticListSSZ[*SignedBLSToExecutionChange](MaxExecutionChanges, 172),
BlobKzgCommitments: solid.NewStaticListSSZ[*KZGCommitment](MaxBlobsCommittmentsPerBlock, 48),
Version: 0,
beaconCfg: beaconCfg,
}
}

Expand Down Expand Up @@ -117,6 +135,21 @@ func (b *BlindedBeaconBlock) Full(txs *solid.TransactionsSSZ, withdrawals *solid
}
}

func (b *BlindedBeaconBlock) SetVersion(version clparams.StateVersion) *BlindedBeaconBlock {
b.Body.SetVersion(version)
return b
}

func (b *BlindedBeaconBody) SetVersion(version clparams.StateVersion) *BlindedBeaconBody {
b.Version = version
if b.ExecutionPayload == nil {
b.ExecutionPayload = NewEth1Header(version)
} else {
b.ExecutionPayload.SetVersion(version)
}
return b
}

func (b *BlindedBeaconBody) EncodeSSZ(dst []byte) ([]byte, error) {
return ssz2.MarshalSSZ(dst, b.getSchema(false)...)
}
Expand Down
2 changes: 2 additions & 0 deletions cl/cltypes/eth1_block.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

"github.com/holiman/uint256"
libcommon "github.com/ledgerwatch/erigon-lib/common"
"github.com/ledgerwatch/log/v3"

Check failure on line 10 in cl/cltypes/eth1_block.go

View workflow job for this annotation

GitHub Actions / tests (ubuntu-22.04)

no required module provides package github.com/ledgerwatch/log/v3; to add it:

Check failure on line 10 in cl/cltypes/eth1_block.go

View workflow job for this annotation

GitHub Actions / tests-windows (windows-2022)

no required module provides package github.com/ledgerwatch/log/v3; to add it:

Check failure on line 10 in cl/cltypes/eth1_block.go

View workflow job for this annotation

GitHub Actions / tests-windows (windows-2022)

no required module provides package github.com/ledgerwatch/log/v3; to add it:

"github.com/ledgerwatch/erigon/cl/clparams"
"github.com/ledgerwatch/erigon/cl/cltypes/solid"
Expand Down Expand Up @@ -305,6 +306,7 @@ func (b *Eth1Block) RlpHeader(parentRoot *libcommon.Hash) (*types.Header, error)
*withdrawalsHash = types.DeriveSha(types.Withdrawals(withdrawals))
}
if b.version < clparams.DenebVersion {
log.Warn("ParentRoot is nil", "parentRoot", parentRoot, "version", b.version)
parentRoot = nil
}

Expand Down
4 changes: 4 additions & 0 deletions cl/cltypes/eth1_data.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ type Eth1Data struct {
BlockHash libcommon.Hash `json:"block_hash"`
}

func NewEth1Data() *Eth1Data {
return &Eth1Data{}
}

func (e *Eth1Data) Copy() *Eth1Data {
copied := *e
return &copied
Expand Down
4 changes: 1 addition & 3 deletions cl/cltypes/eth1_header.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,7 @@ func NewEth1Header(version clparams.StateVersion) *Eth1Header {
}

func (e *Eth1Header) SetVersion(v clparams.StateVersion) {
if e != nil {
e.version = v
}
e.version = v
}

func (e *Eth1Header) Copy() *Eth1Header {
Expand Down
1 change: 1 addition & 0 deletions cl/phase1/forkchoice/on_block.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ func (f *ForkChoiceStore) OnBlock(ctx context.Context, block *cltypes.SignedBeac
return fmt.Errorf("failed to add block to optimistic store: %v", err)
}
case execution_client.PayloadStatusInvalidated:
log.Warn("OnBlock: block is invalid", "block", libcommon.Hash(blockRoot), "err", err)
log.Debug("OnBlock: block is invalid", "block", libcommon.Hash(blockRoot))
f.forkGraph.MarkHeaderAsInvalid(blockRoot)
// remove from optimistic candidate
Expand Down
10 changes: 4 additions & 6 deletions cl/transition/machine/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,10 @@ import (
)

// ProcessBlock processes a block with the block processor
func ProcessBlock(impl BlockProcessor, s abstract.BeaconState, signedBlock *cltypes.SignedBeaconBlock) error {
block := signedBlock.Block
func ProcessBlock(impl BlockProcessor, s abstract.BeaconState, block *cltypes.BeaconBlock) error {
version := s.Version()
// Check the state version is correct.
if signedBlock.Version() != version {
if block.Version() != version {
return fmt.Errorf("processBlock: wrong state version for block at slot %d", block.Slot)
}
h := metrics.NewHistTimer("beacon_process_block")
Expand Down Expand Up @@ -72,16 +71,15 @@ func ProcessBlock(impl BlockProcessor, s abstract.BeaconState, signedBlock *clty
return nil
}

func ProcessBlindedBlock(impl BlockProcessor, s abstract.BeaconState, signedBlock *cltypes.SignedBlindedBeaconBlock) error {
func ProcessBlindedBlock(impl BlockProcessor, s abstract.BeaconState, block *cltypes.BlindedBeaconBlock) error {
var (
block = signedBlock.Block
version = s.Version()
// Process the execution payload. Note that the execution payload does not contain txs and withdrawals.
partialExecutionBody = block.Body.Full(nil, nil)
)

// Check the state version is correct.
if signedBlock.Version() != version {
if block.Version() != version {
return fmt.Errorf("processBlindedBlock: wrong state version for block at slot %d", block.Slot)
}
h := metrics.NewHistTimer("beacon_process_blinded_block")
Expand Down
2 changes: 1 addition & 1 deletion cl/transition/machine/transition.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func TransitionState(impl Interface, s abstract.BeaconState, block *cltypes.Sign
}

// Transition block
if err := ProcessBlock(impl, s, block); err != nil {
if err := ProcessBlock(impl, s, block.Block); err != nil {
return err
}

Expand Down

0 comments on commit 483e3a3

Please sign in to comment.