Skip to content

Commit

Permalink
Add support for execution requests
Browse files Browse the repository at this point in the history
  • Loading branch information
jtraglia committed Oct 14, 2024
1 parent 5da8987 commit 129b133
Show file tree
Hide file tree
Showing 20 changed files with 1,190 additions and 8 deletions.
43 changes: 43 additions & 0 deletions api/electra/builderbid.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// Copyright © 2024 Attestant Limited.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package electra

import (
"fmt"

"github.com/attestantio/go-eth2-client/spec/deneb"
"github.com/attestantio/go-eth2-client/spec/electra"
"github.com/attestantio/go-eth2-client/spec/phase0"
"github.com/goccy/go-yaml"
"github.com/holiman/uint256"
)

// BuilderBid represents a BuilderBid.
type BuilderBid struct {
Header *deneb.ExecutionPayloadHeader
BlobKZGCommitments []deneb.KZGCommitment `ssz-max:"4096" ssz-size:"?,48"`
ExecutionRequests *electra.ExecutionRequests
Value *uint256.Int `ssz-size:"32"`
Pubkey phase0.BLSPubKey `ssz-size:"48"`
}

// String returns a string version of the structure.
func (b *BuilderBid) String() string {
data, err := yaml.Marshal(b)
if err != nil {
return fmt.Sprintf("ERR: %v", err)
}

return string(data)
}
105 changes: 105 additions & 0 deletions api/electra/builderbid_json.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
// Copyright © 2024 Attestant Limited.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package electra

import (
"encoding/hex"
"encoding/json"
"fmt"
"math/big"
"strings"

"github.com/attestantio/go-eth2-client/spec/deneb"
"github.com/attestantio/go-eth2-client/spec/electra"
"github.com/attestantio/go-eth2-client/spec/phase0"
"github.com/holiman/uint256"
"github.com/pkg/errors"
)

// builderBidJSON is the spec representation of the struct.
type builderBidJSON struct {
Header *deneb.ExecutionPayloadHeader `json:"header"`
BlobKZGCommitments []deneb.KZGCommitment `json:"blob_kzg_commitments"`
ExecutionRequests *electra.ExecutionRequests `json:"execution_requests"`
Value string `json:"value"`
Pubkey string `json:"pubkey"`
}

// MarshalJSON implements json.Marshaler.
func (b *BuilderBid) MarshalJSON() ([]byte, error) {
return json.Marshal(&builderBidJSON{
Header: b.Header,
BlobKZGCommitments: b.BlobKZGCommitments,
ExecutionRequests: b.ExecutionRequests,
Value: fmt.Sprintf("%d", b.Value),
Pubkey: b.Pubkey.String(),
})
}

// UnmarshalJSON implements json.Unmarshaler.
func (b *BuilderBid) UnmarshalJSON(input []byte) error {
var data builderBidJSON
if err := json.Unmarshal(input, &data); err != nil {
return errors.Wrap(err, "invalid JSON")
}

return b.unpack(&data)
}

func (b *BuilderBid) unpack(data *builderBidJSON) error {
if data.Header == nil {
return errors.New("header missing")
}
b.Header = data.Header

if data.BlobKZGCommitments == nil {
return errors.New("blob KZG commitments missing")
}
b.BlobKZGCommitments = data.BlobKZGCommitments

if data.ExecutionRequests == nil {
return errors.New("execution requests missing")
}
b.ExecutionRequests = data.ExecutionRequests

if data.Value == "" {
return errors.New("value missing")
}
value, success := new(big.Int).SetString(data.Value, 10)
if !success {
return errors.New("invalid value for value")
}
if value.Sign() == -1 {
return errors.New("value cannot be negative")
}
var overflow bool
b.Value, overflow = uint256.FromBig(value)
if overflow {
return errors.New("value overflow")
}

if data.Pubkey == "" {
return errors.New("public key missing")
}
pubKey, err := hex.DecodeString(strings.TrimPrefix(data.Pubkey, "0x"))
if err != nil {
return errors.Wrap(err, "invalid value for public key")
}
if len(pubKey) != phase0.PublicKeyLength {
return errors.New("incorrect length for public key")
}
copy(b.Pubkey[:], pubKey)

return nil
}
220 changes: 220 additions & 0 deletions api/electra/builderbid_ssz.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 129b133

Please sign in to comment.