Skip to content

Commit

Permalink
Merge pull request #17 from avalonche/add-deneb-builder-bid
Browse files Browse the repository at this point in the history
Add builder bid structs for deneb
  • Loading branch information
mcdee authored Jul 31, 2023
2 parents c160833 + 4d1c7f3 commit a01c7ac
Show file tree
Hide file tree
Showing 15 changed files with 1,381 additions and 0 deletions.
40 changes: 40 additions & 0 deletions api/deneb/builderbid.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// Copyright © 2022 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 deneb

import (
"fmt"

"github.com/attestantio/go-eth2-client/spec/deneb"
"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
Value *uint256.Int `ssz-size:"32"`
Pubkey phase0.BLSPubKey `ssz-size:"48"`
BlindedBlobsBundle *BlindedBlobsBundle
}

// 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)
}
96 changes: 96 additions & 0 deletions api/deneb/builderbid_json.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
// Copyright © 2022 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 deneb

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

"github.com/attestantio/go-eth2-client/spec/deneb"
"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"`
Value string `json:"value"`
Pubkey string `json:"pubkey"`
BlindedBlobsBundle *BlindedBlobsBundle `json:"blinded_blobs_bundle"`
}

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

// 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.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)

if data.BlindedBlobsBundle == nil {
return errors.New("blinded blobs bundle missing")
}
b.BlindedBlobsBundle = data.BlindedBlobsBundle

return nil
}
175 changes: 175 additions & 0 deletions api/deneb/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 a01c7ac

Please sign in to comment.