Skip to content

Commit

Permalink
Interfaces update (#8)
Browse files Browse the repository at this point in the history
* Update interfaces to retain bid source

* go.mod updates

* More interface changes, add tests

* handle errors

* Fix test

---------

Co-authored-by: parithosh <[email protected]>
  • Loading branch information
marioevz and parithosh authored Sep 13, 2023
1 parent ecbe693 commit 0a70254
Show file tree
Hide file tree
Showing 11 changed files with 253 additions and 66 deletions.
6 changes: 3 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,8 @@ require (
rsc.io/tmplfunc v0.0.3 // indirect
)

replace github.com/protolambda/zrnt => github.com/marioevz/zrnt v0.26.2-0.20230824215908-29c8dd7802b5

replace github.com/ethereum/go-ethereum => github.com/lightclient/go-ethereum v1.10.10-0.20230818233413-c0d85695293e
replace github.com/ethereum/go-ethereum => github.com/lightclient/go-ethereum v1.10.10-0.20230822214239-edf4ab3a0003

replace github.com/protolambda/eth2api => github.com/marioevz/eth2api v0.0.0-20230823170424-c49aeeefbc40

replace github.com/protolambda/zrnt => github.com/marioevz/zrnt v0.26.2-0.20230830002615-6770fa799a31
12 changes: 4 additions & 8 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,6 @@ github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaS
github.com/golang/protobuf v1.5.2/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiuN0vRsmY=
github.com/golang/protobuf v1.5.3 h1:KhyjKVUg7Usr/dYsdSqoFveMYd5ko72D+zANwlG1mmg=
github.com/golang/protobuf v1.5.3/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiuN0vRsmY=
github.com/golang/snappy v0.0.3/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q=
github.com/golang/snappy v0.0.4/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q=
github.com/golang/snappy v0.0.5-0.20220116011046-fa5810519dcb h1:PBC98N2aIaM3XXiurYmW7fx4GZkL8feAMVq7nEjURHk=
github.com/golang/snappy v0.0.5-0.20220116011046-fa5810519dcb/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q=
Expand Down Expand Up @@ -207,15 +206,15 @@ github.com/kylelemons/godebug v1.1.0 h1:RPNrshWIDI6G2gRW9EHilWtl7Z6Sb1BR0xunSBf0
github.com/labstack/echo/v4 v4.5.0/go.mod h1:czIriw4a0C1dFun+ObrXp7ok03xON0N1awStJ6ArI7Y=
github.com/labstack/gommon v0.3.0/go.mod h1:MULnywXg0yavhxWKc+lOruYdAhDwPK9wf0OL7NoOu+k=
github.com/leanovate/gopter v0.2.9 h1:fQjYxZaynp97ozCzfOyOuAGOU4aU/z37zf/tOujFk7c=
github.com/lightclient/go-ethereum v1.10.10-0.20230818233413-c0d85695293e h1:nCWrj2OTMJXe8I8uIHuwahd+0y8EZJS/c4TZCCXh8Zc=
github.com/lightclient/go-ethereum v1.10.10-0.20230818233413-c0d85695293e/go.mod h1:1JU/x0JQLBndZb2727eswdCzyF2rFd8SZUj7HDQuVqc=
github.com/lightclient/go-ethereum v1.10.10-0.20230822214239-edf4ab3a0003 h1:cpyBAHdWNvnTXrLjO93Ks1TOQ7SqIiykwjSI2a+WAXA=
github.com/lightclient/go-ethereum v1.10.10-0.20230822214239-edf4ab3a0003/go.mod h1:ajli8dy5OcMaIkZSOEApZPUesGX6S9jPduNnfnISGWY=
github.com/magiconair/properties v1.8.0/go.mod h1:PppfXfuXeibc/6YijjN8zIbojt8czPbwD3XqdrwzmxQ=
github.com/marioevz/eth-clients v0.0.0-20230823175101-fdc85382188d h1:X5kDuhc/KJmftpz4lr1xGLQeLMsXScF0ui77rvyclto=
github.com/marioevz/eth-clients v0.0.0-20230823175101-fdc85382188d/go.mod h1:LZb4HPhmomFs5C3Rxj57X83yp3qzQHgB/xDAsvrFmDw=
github.com/marioevz/eth2api v0.0.0-20230823170424-c49aeeefbc40 h1:XjtFopxKQTfRvzPiVkWATIxmTzN+i068THS3r9KLoaA=
github.com/marioevz/eth2api v0.0.0-20230823170424-c49aeeefbc40/go.mod h1:hcwWCT4sF1X7KsMZ535MvDZVk5M20Uyj+x2LARZjQsM=
github.com/marioevz/zrnt v0.26.2-0.20230824215908-29c8dd7802b5 h1:4S9KHu9u3i0ndaC623cRd9zA2LPHxNKt7sxgLDYYUew=
github.com/marioevz/zrnt v0.26.2-0.20230824215908-29c8dd7802b5/go.mod h1:qcdX9CXFeVNCQK/q0nswpzhd+31RHMk2Ax/2lMsJ4Jw=
github.com/marioevz/zrnt v0.26.2-0.20230830002615-6770fa799a31 h1:Ce9yBXc1xf8zT6y3D+InCG9O/LOctot5z5bY0OEPNEM=
github.com/marioevz/zrnt v0.26.2-0.20230830002615-6770fa799a31/go.mod h1:MzAcHPo2QQIx+IEPdxXMiqKtYRUlszz7RA/fhAuOm1I=
github.com/mattn/go-colorable v0.1.2/go.mod h1:U0ppj6V5qS13XJ6of8GYAs25YV2eR4EVcfRqFIhoBtE=
github.com/mattn/go-colorable v0.1.8/go.mod h1:u6P/XSegPjTcexA+o6vUJrdnUu04hMope9wVRipJSqc=
github.com/mattn/go-colorable v0.1.11/go.mod h1:u5H1YNBxpqRaxsYJYSkiCWKzEfiAb1Gb520KVy5xxl4=
Expand All @@ -234,7 +233,6 @@ github.com/matttproud/golang_protobuf_extensions v1.0.4 h1:mmDVorXM7PCGKw94cs5zk
github.com/matttproud/golang_protobuf_extensions v1.0.4/go.mod h1:BSXmuO+STAnVfrANrmjBb36TMTDstsz7MSK+HVaYKv4=
github.com/mediocregopher/radix/v3 v3.4.2/go.mod h1:8FL3F6UQRXHXIBSPUs5h0RybMF8i4n7wVopoX3x7Bv8=
github.com/microcosm-cc/bluemonday v1.0.2/go.mod h1:iVP4YcDBq+n/5fb23BhYFvIMq/leAFZyRl6bYmGDlGc=
github.com/minio/sha256-simd v0.1.0/go.mod h1:2FMWW+8GMoPweT6+pI63m9YE3Lmw4J71hV56Chs1E/U=
github.com/minio/sha256-simd v1.0.0 h1:v1ta+49hkWZyvaKwrQB8elexRqm6Y0aMLjCNsrYxo6g=
github.com/minio/sha256-simd v1.0.0/go.mod h1:OuYzVNI5vcoYIAmbIvHPl3N3jUzVedXbKy5RFepssQM=
github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0=
Expand Down Expand Up @@ -289,10 +287,8 @@ github.com/prometheus/common v0.42.0 h1:EKsfXEYo4JpWMHH5cg+KOUWeuJSov1Id8zGR8eeI
github.com/prometheus/common v0.42.0/go.mod h1:xBwqVerjNdUDjgODMpudtOMwlOwf2SaTr1yjz4b7Zbc=
github.com/prometheus/procfs v0.9.0 h1:wzCHvIvM5SxWqYvwgVL7yJY8Lz3PKn49KQtpgMYJfhI=
github.com/prometheus/procfs v0.9.0/go.mod h1:+pB4zwohETzFnmlpe6yd2lSc+0/46IYZRB/chUwxUZY=
github.com/protolambda/bls12-381-util v0.0.0-20210720105258-a772f2aac13e/go.mod h1:MPZvj2Pr0N8/dXyTPS5REeg2sdLG7t8DRzC1rLv925w=
github.com/protolambda/bls12-381-util v0.0.0-20220416220906-d8552aa452c7 h1:cZC+usqsYgHtlBaGulVnZ1hfKAi8iWtujBnRLQE698c=
github.com/protolambda/bls12-381-util v0.0.0-20220416220906-d8552aa452c7/go.mod h1:IToEjHuttnUzwZI5KBSM/LOOW3qLbbrHOEfp3SbECGY=
github.com/protolambda/messagediff v1.4.0/go.mod h1:LboJp0EwIbJsePYpzh5Op/9G1/4mIztMRYzzwR0dR2M=
github.com/protolambda/ztyp v0.2.2 h1:rVcL3vBu9W/aV646zF6caLS/dyn9BN8NYiuJzicLNyY=
github.com/protolambda/ztyp v0.2.2/go.mod h1:9bYgKGqg3wJqT9ac1gI2hnVb0STQq7p/1lapqrqY1dU=
github.com/rauljordan/engine-proxy v0.0.0-20230316220057-4c80c36c4c3a h1:ZIfMLprHVdo2vs3WcSqSDEyz2ZsSzDhGeOyxh8VQThA=
Expand Down
50 changes: 32 additions & 18 deletions mock/mock_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"github.com/gorilla/mux"
beacon_client "github.com/marioevz/eth-clients/clients/beacon"
exec_client "github.com/marioevz/eth-clients/clients/execution"
builder_types "github.com/marioevz/mock-builder/types"
"github.com/marioevz/mock-builder/types/bellatrix"
"github.com/marioevz/mock-builder/types/capella"
"github.com/marioevz/mock-builder/types/common"
Expand Down Expand Up @@ -70,6 +71,8 @@ type MockBuilder struct {
cfg *config
}

var _ builder_types.Builder = (*MockBuilder)(nil)

const (
DEFAULT_BUILDER_HOST = "0.0.0.0"
DEFAULT_BUILDER_PORT = 18550
Expand Down Expand Up @@ -269,9 +272,13 @@ func (m *MockBuilder) Start(ctx context.Context) error {

if addr, err := m.el.EngineRPCAddress(); err == nil {
el_address = addr
} else {
logrus.Error(err)
}
if addr, err := m.cl.BeaconAPIURL(); err == nil {
cl_address = addr
} else {
logrus.Error(err)
}
fields := logrus.Fields{
"builder_id": m.cfg.id,
Expand Down Expand Up @@ -938,7 +945,23 @@ func (m *MockBuilder) HandleGetExecutionPayloadHeader(
return
}

if err = builderBid.Build(m.cfg.spec, p, apiBlobsBundle); err != nil {
// Get proposer index to add it to the context
proposerIndex, err := m.cl.ProposerIndex(ctx, slot)
if err != nil {
logrus.WithFields(logrus.Fields{
"builder_id": m.cfg.id,
"err": err,
"slot": slot,
}).Error("Error getting proposer index from CL")
http.Error(
w,
"Unable to respond to header request",
http.StatusInternalServerError,
)
return
}

if err = builderBid.Build(m.cfg.spec, p, apiBlobsBundle, blockHead.Root(), slot, proposerIndex); err != nil {
logrus.WithFields(logrus.Fields{
"builder_id": m.cfg.id,
"err": err,
Expand Down Expand Up @@ -975,30 +998,21 @@ func (m *MockBuilder) HandleGetExecutionPayloadHeader(
}
builderBid.SetPubKey(m.pkBeacon)

// Get proposer index to add it to the context
proposerIndex, err := m.cl.ProposerIndex(ctx, slot)
if err != nil {
logrus.WithFields(logrus.Fields{
"builder_id": m.cfg.id,
"err": err,
"slot": slot,
}).Error("Error getting proposer index from CL")
http.Error(
w,
"Unable to respond to header request",
http.StatusInternalServerError,
)
return
}
builderBid.SetContext(blockHead.Root(), slot, proposerIndex)

logrus.WithFields(logrus.Fields{
"builder_id": m.cfg.id,
"payload": p.BlockHash.String(),
"value": bValue.String(),
"fork": fork,
}).Info("Built payload from EL")

builtBidRoot := builderBid.HashTreeRoot(m.cfg.spec, tree.GetHashFn())
bidJson, _ := json.Marshal(builderBid)
logrus.WithFields(logrus.Fields{
"builder_id": m.cfg.id,
"bid_root": builtBidRoot,
"json": string(bidJson),
}).Debug("Built bid details")

signedBid, err := builderBid.Sign(m.cfg.spec, m.builderApiDomain, m.sk, m.pk)
if err != nil {
logrus.WithFields(logrus.Fields{
Expand Down
15 changes: 15 additions & 0 deletions mock/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/marioevz/mock-builder/types/capella"
"github.com/marioevz/mock-builder/types/common"
beacon "github.com/protolambda/zrnt/eth2/beacon/common"
"github.com/sirupsen/logrus"
)

type PayloadAttributesModifier func(*api.PayloadAttributes, beacon.Slot) (bool, error)
Expand Down Expand Up @@ -114,6 +115,20 @@ func WithExternalIP(ip net.IP) Option {
}
}

func WithLogLevel(logLevel string) Option {
return Option{
apply: func(m *MockBuilder) error {
logLevelParsed, err := logrus.ParseLevel(logLevel)
if err != nil {
return err
}
logrus.SetLevel(logLevelParsed)
return nil
},
description: fmt.Sprintf("WithLogLevel(%s)", logLevel),
}
}

func WithSpec(spec *beacon.Spec) Option {
return Option{
apply: func(m *MockBuilder) error {
Expand Down
32 changes: 20 additions & 12 deletions types/bellatrix/bellatrix.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func (b *BuilderBid) Version() string {
return Version
}

func (b *BuilderBid) HashTreeRoot(hFn tree.HashFn) tree.Root {
func (b *BuilderBid) HashTreeRoot(spec *beacon.Spec, hFn tree.HashFn) tree.Root {
return hFn.HashTreeRoot(
b.Header,
&b.Value,
Expand All @@ -72,17 +72,22 @@ func (b *BuilderBid) Build(
spec *beacon.Spec,
ed *api.ExecutableData,
_ *api.BlobsBundleV1,
parentBlockRoot tree.Root,
slot beacon.Slot,
proposerIndex beacon.ValidatorIndex,
) error {
if ed == nil {
return fmt.Errorf("nil execution payload")
}
b.Payload = new(ExecutionPayload)
if err := b.Payload.FromExecutableData(ed); err != nil {
if err := b.Payload.FromExecutableData(ed, nil); err != nil {
return err
}

b.Header = b.Payload.Header(spec)

b.ParentBlockRoot = parentBlockRoot
b.Slot = slot
b.ProposerIndex = proposerIndex
return nil
}

Expand Down Expand Up @@ -141,12 +146,6 @@ func (b *BuilderBid) SetPubKey(pk beacon.BLSPubkey) {
b.PubKey = pk
}

func (b *BuilderBid) SetContext(parentBlockRoot tree.Root, slot beacon.Slot, proposerIndex beacon.ValidatorIndex) {
b.ParentBlockRoot = parentBlockRoot
b.Slot = slot
b.ProposerIndex = proposerIndex
}

func (b *BuilderBid) Sign(
spec *beacon.Spec,
domain beacon.BLSDomain,
Expand All @@ -156,7 +155,7 @@ func (b *BuilderBid) Sign(
pkBytes := pk.Serialize()
copy(b.PubKey[:], pkBytes[:])
sigRoot := beacon.ComputeSigningRoot(
b.HashTreeRoot(tree.GetHashFn()),
b.HashTreeRoot(spec, tree.GetHashFn()),
domain,
)
return &common.SignedBuilderBid{
Expand All @@ -167,9 +166,10 @@ func (b *BuilderBid) Sign(

type ExecutionPayload struct {
*bellatrix.ExecutionPayload
Source *api.ExecutableData
}

func (p *ExecutionPayload) FromExecutableData(ed *api.ExecutableData) error {
func (p *ExecutionPayload) FromExecutableData(ed *api.ExecutableData, _ *tree.Root) error {
if ed == nil {
return fmt.Errorf("nil execution payload")
}
Expand Down Expand Up @@ -198,11 +198,19 @@ func (p *ExecutionPayload) FromExecutableData(ed *api.ExecutableData) error {
p.Transactions[i] = make(beacon.Transaction, len(tx))
copy(p.Transactions[i][:], tx[:])
}
p.Source = ed
return nil
}

func (p *ExecutionPayload) ToExecutableData() (*api.ExecutableData, *el_common.Hash, error) {
if p.Source == nil {
return nil, nil, fmt.Errorf("nil execution payload")
}
return p.Source, nil, nil
}

func (p *ExecutionPayload) GetBlockHash() tree.Root {
if p == nil {
if p.ExecutionPayload == nil {
panic("nil execution payload")
}
return p.BlockHash
Expand Down
5 changes: 2 additions & 3 deletions types/builder.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package builder_types

import (
api "github.com/ethereum/go-ethereum/beacon/engine"
"github.com/marioevz/mock-builder/types/common"
beacon "github.com/protolambda/zrnt/eth2/beacon/common"
)
Expand All @@ -12,6 +11,6 @@ type Builder interface {
GetBuiltPayloadsCount() int
GetSignedBeaconBlockCount() int
GetSignedBeaconBlocks() map[beacon.Slot]common.SignedBeaconResponse
GetModifiedPayloads() map[beacon.Slot]*api.ExecutableData
GetBuiltPayloads() map[beacon.Slot]*api.ExecutableData
GetModifiedPayloads() map[beacon.Slot]common.ExecutionPayload
GetBuiltPayloads() map[beacon.Slot]common.BuilderBid
}
37 changes: 26 additions & 11 deletions types/capella/capella.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func (b *BuilderBid) Version() string {
return Version
}

func (b *BuilderBid) HashTreeRoot(hFn tree.HashFn) tree.Root {
func (b *BuilderBid) HashTreeRoot(spec *beacon.Spec, hFn tree.HashFn) tree.Root {
return hFn.HashTreeRoot(
b.Header,
&b.Value,
Expand All @@ -72,20 +72,32 @@ func (b *BuilderBid) Build(
spec *beacon.Spec,
ed *api.ExecutableData,
_ *api.BlobsBundleV1,
parentBlockRoot tree.Root,
slot beacon.Slot,
proposerIndex beacon.ValidatorIndex,
) error {
if ed == nil {
return fmt.Errorf("nil execution payload")
}
b.Payload = new(ExecutionPayload)
if err := b.Payload.FromExecutableData(ed); err != nil {
if err := b.Payload.FromExecutableData(ed, nil); err != nil {
return err
}

b.Header = b.Payload.Header(spec)
b.ParentBlockRoot = parentBlockRoot
b.Slot = slot
b.ProposerIndex = proposerIndex
return nil
}

func (b *BuilderBid) ValidateReveal(publicKey *blsu.Pubkey, signedBeaconResponse common.SignedBeaconResponse, spec *beacon.Spec, slot beacon.Slot, genesisValidatorsRoot *tree.Root) (*common.UnblindedResponse, error) {
func (b *BuilderBid) ValidateReveal(
publicKey *blsu.Pubkey,
signedBeaconResponse common.SignedBeaconResponse,
spec *beacon.Spec,
slot beacon.Slot,
genesisValidatorsRoot *tree.Root,
) (*common.UnblindedResponse, error) {
sbb, ok := signedBeaconResponse.(*SignedBeaconResponse)
if !ok {
return nil, fmt.Errorf("invalid signed beacon response")
Expand Down Expand Up @@ -134,12 +146,6 @@ func (b *BuilderBid) SetPubKey(pk beacon.BLSPubkey) {
b.PubKey = pk
}

func (b *BuilderBid) SetContext(parentBlockRoot tree.Root, slot beacon.Slot, proposerIndex beacon.ValidatorIndex) {
b.ParentBlockRoot = parentBlockRoot
b.Slot = slot
b.ProposerIndex = proposerIndex
}

func (b *BuilderBid) Sign(
spec *beacon.Spec,
domain beacon.BLSDomain,
Expand All @@ -149,7 +155,7 @@ func (b *BuilderBid) Sign(
pkBytes := pk.Serialize()
copy(b.PubKey[:], pkBytes[:])
sigRoot := beacon.ComputeSigningRoot(
b.HashTreeRoot(tree.GetHashFn()),
b.HashTreeRoot(spec, tree.GetHashFn()),
domain,
)
return &common.SignedBuilderBid{
Expand All @@ -160,9 +166,10 @@ func (b *BuilderBid) Sign(

type ExecutionPayload struct {
*capella.ExecutionPayload
Source *api.ExecutableData
}

func (p *ExecutionPayload) FromExecutableData(ed *api.ExecutableData) error {
func (p *ExecutionPayload) FromExecutableData(ed *api.ExecutableData, _ *tree.Root) error {
if ed == nil {
return fmt.Errorf("nil execution payload")
}
Expand Down Expand Up @@ -198,9 +205,17 @@ func (p *ExecutionPayload) FromExecutableData(ed *api.ExecutableData) error {
copy(p.Withdrawals[i].Address[:], w.Address[:])
p.Withdrawals[i].Amount = beacon.Gwei(w.Amount)
}
p.Source = ed
return nil
}

func (p *ExecutionPayload) ToExecutableData() (*api.ExecutableData, *el_common.Hash, error) {
if p.Source == nil {
return nil, nil, fmt.Errorf("nil execution payload")
}
return p.Source, nil, nil
}

func (p *ExecutionPayload) GetBlockHash() tree.Root {
if p == nil {
panic("nil execution payload")
Expand Down
Loading

0 comments on commit 0a70254

Please sign in to comment.