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

fix: validator check lag #543

Merged
merged 5 commits into from
Dec 24, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ job "{{ job.name }}" {
data = <<-EOH
MEV_COMMIT_LOG_FMT="{{ job.env.get('log-format', 'json') }}"
MEV_COMMIT_LOG_TAGS="{{ 'service.name:' + job.name + '-{{ env "NOMAD_ALLOC_INDEX" }}' + ',service.version:' + version }}"
MEV_COMMIT_LAGGERD_MODE="{{ job.env.get('laggerd-mode', '10') }}"
MEV_COMMIT_OTEL_COLLECTOR_ENDPOINT_URL="{{ job.env.get('otel_collector_endpoint_url', '') }}"
{%- raw %}
MEV_COMMIT_KEYSTORE_PATH="/local/data-{{ env "NOMAD_ALLOC_INDEX" }}/keystore"
Expand Down
9 changes: 9 additions & 0 deletions p2p/cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -430,6 +430,14 @@ var (
Value: 30 * time.Second,
Category: categoryProvider,
})

optionLaggerdMode = altsrc.NewIntFlag(&cli.IntFlag{
Name: "laggerd-mode",
aloknerurkar marked this conversation as resolved.
Show resolved Hide resolved
Usage: "No of blocks to lag behind for L1 chain when fetching validator duties",
EnvVars: []string{"MEV_COMMIT_LAGGERD_MODE"},
Value: 10,
Category: categoryEthRPC,
})
)

func main() {
Expand Down Expand Up @@ -649,6 +657,7 @@ func launchNodeWithConfig(c *cli.Context) (err error) {
OracleWindowOffset: big.NewInt(defaultOracleWindowOffset),
BeaconAPIURL: c.String(optionBeaconAPIURL.Name),
L1RPCURL: c.String(optionL1RPCURL.Name),
LaggeredMode: c.Int(optionLaggerdMode.Name),
BidderBidTimeout: c.Duration(optionBidderBidTimeout.Name),
ProviderDecisionTimeout: c.Duration(optionProviderDecisionTimeout.Name),
})
Expand Down
20 changes: 14 additions & 6 deletions p2p/pkg/node/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ type Options struct {
OracleWindowOffset *big.Int
BeaconAPIURL string
L1RPCURL string
LaggeredMode int
BidderBidTimeout time.Duration
ProviderDecisionTimeout time.Duration
}
Expand Down Expand Up @@ -416,17 +417,24 @@ func NewNode(opts *Options) (*Node, error) {
return nil, err
}

validatorRouterSession := &validatorrouter.ValidatoroptinrouterCallerSession{
Contract: validatorRouterCaller,
CallOpts: bind.CallOpts{
From: opts.KeySigner.GetAddress(),
},
callOptsGetter := func() (*bind.CallOpts, error) {
blkNum, err := l1ContractRPC.BlockNumber(context.Background())
if err != nil {
return nil, err
}
currentBlkNum := big.NewInt(0).SetUint64(blkNum)
queryBlkNum := big.NewInt(0).Sub(currentBlkNum, big.NewInt(int64(opts.LaggeredMode)))
aloknerurkar marked this conversation as resolved.
Show resolved Hide resolved
return &bind.CallOpts{
From: opts.KeySigner.GetAddress(),
BlockNumber: queryBlkNum,
}, nil
}

validatorAPI := validatorapi.NewService(
opts.BeaconAPIURL,
validatorRouterSession,
validatorRouterCaller,
opts.Logger.With("component", "validatorapi"),
callOptsGetter,
)
validatorapiv1.RegisterValidatorServer(grpcServer, validatorAPI)

Expand Down
24 changes: 21 additions & 3 deletions p2p/pkg/rpc/validator/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"strconv"
"strings"

"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/common/hexutil"
validatoroptinrouter "github.com/primev/mev-commit/contracts-abi/clients/ValidatorOptInRouter"
validatorapiv1 "github.com/primev/mev-commit/p2p/gen/go/validatorapi/v1"
Expand All @@ -18,7 +19,7 @@ import (
)

type ValidatorRouterContract interface {
AreValidatorsOptedIn(valBLSPubKeys [][]byte) ([]validatoroptinrouter.IValidatorOptInRouterOptInStatus, error)
AreValidatorsOptedIn(opts *bind.CallOpts, valBLSPubKeys [][]byte) ([]validatoroptinrouter.IValidatorOptInRouterOptInStatus, error)
}

type Service struct {
Expand All @@ -27,14 +28,21 @@ type Service struct {
validatorRouter ValidatorRouterContract
logger *slog.Logger
metrics *metrics
optsGetter func() (*bind.CallOpts, error)
}

func NewService(apiURL string, validatorRouter ValidatorRouterContract, logger *slog.Logger) *Service {
func NewService(
apiURL string,
validatorRouter ValidatorRouterContract,
logger *slog.Logger,
optsGetter func() (*bind.CallOpts, error),
) *Service {
return &Service{
apiURL: apiURL,
validatorRouter: validatorRouter,
logger: logger,
metrics: newMetrics(),
optsGetter: optsGetter,
}
}

Expand Down Expand Up @@ -177,7 +185,17 @@ func (s *Service) processValidators(dutiesResp *ProposerDutiesResponse) (map[uin
validatorsKeys = append(validatorsKeys, pubkeyBytes)
}

areValidatorsOptedIn, err := s.validatorRouter.AreValidatorsOptedIn(validatorsKeys)
if len(validatorsKeys) == 0 {
return validators, nil
}

opts, err := s.optsGetter()
if err != nil {
s.logger.Error("getting call opts", "error", err)
return nil, status.Errorf(codes.Internal, "getting call opts: %v", err)
}

areValidatorsOptedIn, err := s.validatorRouter.AreValidatorsOptedIn(opts, validatorsKeys)
if err != nil {
s.logger.Error("checking if validators are opted in", "error", err)
return nil, status.Errorf(codes.Internal, "checking if validators are opted in: %v", err)
Expand Down
21 changes: 17 additions & 4 deletions p2p/pkg/rpc/validator/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"os"
"testing"

"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/common/hexutil"
validatoroptinrouter "github.com/primev/mev-commit/contracts-abi/clients/ValidatorOptInRouter"
validatorapiv1 "github.com/primev/mev-commit/p2p/gen/go/validatorapi/v1"
Expand All @@ -22,7 +23,7 @@ type MockValidatorRouterContract struct {
ExpectedCalls map[string]interface{}
}

func (m *MockValidatorRouterContract) AreValidatorsOptedIn(valBLSPubKeys [][]byte) ([]validatoroptinrouter.IValidatorOptInRouterOptInStatus, error) {
func (m *MockValidatorRouterContract) AreValidatorsOptedIn(_ *bind.CallOpts, valBLSPubKeys [][]byte) ([]validatoroptinrouter.IValidatorOptInRouterOptInStatus, error) {
results := make([]validatoroptinrouter.IValidatorOptInRouterOptInStatus, len(valBLSPubKeys))
for i, key := range valBLSPubKeys {
if m.ExpectedCalls[string(key)] == nil {
Expand Down Expand Up @@ -56,8 +57,12 @@ func TestGetValidators(t *testing.T) {
},
}

optsGetter := func() (*bind.CallOpts, error) {
return &bind.CallOpts{}, nil
}

logger := util.NewTestLogger(os.Stdout)
service := validatorapi.NewService(mockServer.URL, mockValidatorRouter, logger)
service := validatorapi.NewService(mockServer.URL, mockValidatorRouter, logger, optsGetter)

req := &validatorapiv1.GetValidatorsRequest{Epoch: 123}
resp, err := service.GetValidators(context.Background(), req)
Expand Down Expand Up @@ -85,9 +90,13 @@ func TestGetValidators_HTTPError(t *testing.T) {
}))
defer mockServer.Close()

optsGetter := func() (*bind.CallOpts, error) {
return &bind.CallOpts{}, nil
}

mockValidatorRouter := &MockValidatorRouterContract{}
logger := util.NewTestLogger(os.Stdout)
service := validatorapi.NewService(mockServer.URL, mockValidatorRouter, logger)
service := validatorapi.NewService(mockServer.URL, mockValidatorRouter, logger, optsGetter)

req := &validatorapiv1.GetValidatorsRequest{Epoch: 123}
_, err := service.GetValidators(context.Background(), req)
Expand Down Expand Up @@ -125,8 +134,12 @@ func TestGetValidators_EpochZero(t *testing.T) {
},
}

optsGetter := func() (*bind.CallOpts, error) {
return &bind.CallOpts{}, nil
}

logger := util.NewTestLogger(os.Stdout)
service := validatorapi.NewService(mockServer.URL, mockValidatorRouter, logger)
service := validatorapi.NewService(mockServer.URL, mockValidatorRouter, logger, optsGetter)

req := &validatorapiv1.GetValidatorsRequest{Epoch: 0}
resp, err := service.GetValidators(context.Background(), req)
Expand Down
Loading