Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Start to add support for Electra #27

Merged
merged 7 commits into from
May 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions api/electra/builderbid.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// 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 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 *electra.ExecutionPayloadHeader
BlobKZGCommitments []deneb.KZGCommitment `ssz-max:"4096" ssz-size:"?,48"`
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)
}
97 changes: 97 additions & 0 deletions api/electra/builderbid_json.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
// 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 *electra.ExecutionPayloadHeader `json:"header"`
BlobKZGCommitments []deneb.KZGCommitment `json:"blob_kzg_commitments"`
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,
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.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.BlobKZGCommitments == nil {
return errors.New("blob KZG commitments missing")
}
b.BlobKZGCommitments = data.BlobKZGCommitments

return nil
}
185 changes: 185 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.

55 changes: 55 additions & 0 deletions api/electra/builderbid_yaml.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
// 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 (
"bytes"
"math/big"

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

// builderBidYAML is the spec representation of the struct.
type builderBidYAML struct {
Header *electra.ExecutionPayloadHeader `yaml:"header"`
BlobKZGCommitments []deneb.KZGCommitment `yaml:"blob_kzg_commitments"`
Value *big.Int `yaml:"value"`
Pubkey string `yaml:"pubkey"`
}

// MarshalYAML implements yaml.Marshaler.
func (b *BuilderBid) MarshalYAML() ([]byte, error) {
yamlBytes, err := yaml.MarshalWithOptions(&builderBidYAML{
Header: b.Header,
BlobKZGCommitments: b.BlobKZGCommitments,
Value: b.Value.ToBig(),
Pubkey: b.Pubkey.String(),
}, yaml.Flow(true))
if err != nil {
return nil, err
}
return bytes.ReplaceAll(yamlBytes, []byte(`"`), []byte(`'`)), nil
}

// UnmarshalYAML implements yaml.Unmarshaler.
func (b *BuilderBid) UnmarshalYAML(input []byte) error {
// We unmarshal to the JSON struct to save on duplicate code.
var data builderBidJSON
if err := yaml.Unmarshal(input, &data); err != nil {
return err
}
return b.unpack(&data)
}
Loading
Loading