diff --git a/infrastructure/nomad/playbooks/templates/jobs/mev-commit-oracle.nomad.j2 b/infrastructure/nomad/playbooks/templates/jobs/mev-commit-oracle.nomad.j2 index 82f45f2b0..09bb0cf1a 100644 --- a/infrastructure/nomad/playbooks/templates/jobs/mev-commit-oracle.nomad.j2 +++ b/infrastructure/nomad/playbooks/templates/jobs/mev-commit-oracle.nomad.j2 @@ -184,7 +184,7 @@ job "{{ job.name }}" { else 'service:' + job.name + '-{{ env "NOMAD_ALLOC_INDEX" }}' }}" MEV_ORACLE_LOG_LEVEL="{{ job.env.get('log-level', 'info') }}" - MEV_ORACLE_LAGGERD_MODE="{{ job.env.get('laggerd-mode', '20') }}" + MEV_ORACLE_LAGGERD_MODE="{{ job.env.get('laggerd-mode', '10') }}" MEV_ORACLE_L1_RPC_URL="{{ job.env['l1_rpc_url'] }}" {%- raw %} MEV_ORACLE_KEYSTORE_PATH="/local/data-{{ env "NOMAD_ALLOC_INDEX" }}/keystore" diff --git a/oracle/cmd/main.go b/oracle/cmd/main.go index 36d2ae207..77a9be805 100644 --- a/oracle/cmd/main.go +++ b/oracle/cmd/main.go @@ -190,7 +190,7 @@ var ( Name: "laggerd-mode", Usage: "No of blocks to lag behind for L1 chain", EnvVars: []string{"MEV_ORACLE_LAGGERD_MODE"}, - Value: 64, + Value: 10, }) optionOverrideWinners = altsrc.NewStringSliceFlag(&cli.StringSliceFlag{ diff --git a/p2p/cmd/main.go b/p2p/cmd/main.go index 63f5c1dee..ae732b906 100644 --- a/p2p/cmd/main.go +++ b/p2p/cmd/main.go @@ -310,6 +310,13 @@ var ( EnvVars: []string{"MEV_COMMIT_GAS_FEE_CAP"}, Value: "2000000000", // 2 gWEI }) + + optionOracleWindowOffset = altsrc.NewIntFlag(&cli.IntFlag{ + Name: "oracle-window-offset", + Usage: "Offset for the oracle window", + EnvVars: []string{"MEV_COMMIT_ORACLE_WINDOW_OFFSET"}, + Value: 1, + }) ) func main() { @@ -346,6 +353,7 @@ func main() { optionGasLimit, optionGasTipCap, optionGasFeeCap, + optionOracleWindowOffset, } app := &cli.App{ @@ -465,6 +473,7 @@ func launchNodeWithConfig(c *cli.Context) error { DefaultGasLimit: uint64(c.Int(optionGasLimit.Name)), DefaultGasTipCap: gasTipCap, DefaultGasFeeCap: gasFeeCap, + OracleWindowOffset: big.NewInt(c.Int64(optionOracleWindowOffset.Name)), }) if err != nil { return fmt.Errorf("failed starting node: %w", err) diff --git a/p2p/pkg/autodepositor/autodepositor.go b/p2p/pkg/autodepositor/autodepositor.go index 8d49b483d..e950b455b 100644 --- a/p2p/pkg/autodepositor/autodepositor.go +++ b/p2p/pkg/autodepositor/autodepositor.go @@ -22,6 +22,7 @@ type OptsGetter func(context.Context) (*bind.TransactOpts, error) type BidderRegistryContract interface { DepositForWindows(opts *bind.TransactOpts, windows []*big.Int) (*types.Transaction, error) + DepositForWindow(opts *bind.TransactOpts, window *big.Int) (*types.Transaction, error) WithdrawFromWindows(opts *bind.TransactOpts, windows []*big.Int) (*types.Transaction, error) } @@ -39,6 +40,7 @@ type AutoDepositTracker struct { btContract BlockTrackerContract optsGetter OptsGetter currentOracleWindow atomic.Value + oracleWindowOffset *big.Int logger *slog.Logger cancelFunc context.CancelFunc } @@ -48,15 +50,17 @@ func New( brContract BidderRegistryContract, btContract BlockTrackerContract, optsGetter OptsGetter, + oracleWindowOffset *big.Int, logger *slog.Logger, ) *AutoDepositTracker { return &AutoDepositTracker{ - eventMgr: evtMgr, - brContract: brContract, - btContract: btContract, - optsGetter: optsGetter, - windowChan: make(chan *blocktracker.BlocktrackerNewWindow, 1), - logger: logger, + eventMgr: evtMgr, + brContract: brContract, + btContract: btContract, + optsGetter: optsGetter, + oracleWindowOffset: oracleWindowOffset, + windowChan: make(chan *blocktracker.BlocktrackerNewWindow, 1), + logger: logger, } } @@ -79,8 +83,8 @@ func (adt *AutoDepositTracker) Start( if startWindow == nil { startWindow = currentOracleWindow - // adding +2 as oracle runs two windows behind - startWindow = new(big.Int).Add(startWindow, big.NewInt(2)) + // adding +1 as oracle runs one window behind + startWindow = new(big.Int).Add(startWindow, adt.oracleWindowOffset) } eg, egCtx := errgroup.WithContext(context.Background()) @@ -196,10 +200,11 @@ func (adt *AutoDepositTracker) startAutodeposit(egCtx context.Context, eg *errgr } } - // Make deposit for the next window. The window event is 2 windows - // behind the current window in progress. So we need to make deposit - // for the next window. - nextWindow := new(big.Int).Add(window.Window, big.NewInt(3)) + // Make deposit for the next window. The window event is 1 windows + // behind the current window in progress. + nextWindow := new(big.Int).Add(window.Window, adt.oracleWindowOffset) + nextWindow = new(big.Int).Add(nextWindow, big.NewInt(1)) + if _, ok := adt.deposits.Load(nextWindow.Uint64()); ok { continue } @@ -209,8 +214,8 @@ func (adt *AutoDepositTracker) startAutodeposit(egCtx context.Context, eg *errgr return err } opts.Value = amount - - txn, err := adt.brContract.DepositForWindows(opts, []*big.Int{nextWindow}) + + txn, err := adt.brContract.DepositForWindow(opts, nextWindow) if err != nil { return err } diff --git a/p2p/pkg/autodepositor/autodepositor_test.go b/p2p/pkg/autodepositor/autodepositor_test.go index 8c9f096fc..eaa207150 100644 --- a/p2p/pkg/autodepositor/autodepositor_test.go +++ b/p2p/pkg/autodepositor/autodepositor_test.go @@ -23,12 +23,17 @@ import ( type MockBidderRegistryContract struct { DepositForWindowsFunc func(opts *bind.TransactOpts, windows []*big.Int) (*types.Transaction, error) WithdrawFromWindowsFunc func(opts *bind.TransactOpts, windows []*big.Int) (*types.Transaction, error) + DepositForWindowFunc func(opts *bind.TransactOpts, window *big.Int) (*types.Transaction, error) } func (m *MockBidderRegistryContract) DepositForWindows(opts *bind.TransactOpts, windows []*big.Int) (*types.Transaction, error) { return m.DepositForWindowsFunc(opts, windows) } +func (m *MockBidderRegistryContract) DepositForWindow(opts *bind.TransactOpts, window *big.Int) (*types.Transaction, error) { + return m.DepositForWindowFunc(opts, window) +} + func (m *MockBidderRegistryContract) WithdrawFromWindows(opts *bind.TransactOpts, windows []*big.Int) (*types.Transaction, error) { return m.WithdrawFromWindowsFunc(opts, windows) } @@ -56,6 +61,7 @@ func TestAutoDepositTracker_Start(t *testing.T) { } amount := big.NewInt(100) + oracleWindowOffset := big.NewInt(1) logger := util.NewTestLogger(os.Stdout) evtMgr := events.NewListener(logger, &btABI, &brABI) brContract := &MockBidderRegistryContract{ @@ -65,6 +71,9 @@ func TestAutoDepositTracker_Start(t *testing.T) { WithdrawFromWindowsFunc: func(opts *bind.TransactOpts, windows []*big.Int) (*types.Transaction, error) { return types.NewTransaction(1, common.Address{}, nil, 0, nil, nil), nil }, + DepositForWindowFunc: func(opts *bind.TransactOpts, window *big.Int) (*types.Transaction, error) { + return types.NewTransaction(1, common.Address{}, nil, 0, nil, nil), nil + }, } btContract := &MockBlockTrackerContract{ GetCurrentWindowFunc: func() (*big.Int, error) { @@ -76,7 +85,7 @@ func TestAutoDepositTracker_Start(t *testing.T) { } // Create AutoDepositTracker instance - adt := autodepositor.New(evtMgr, brContract, btContract, optsGetter, logger) + adt := autodepositor.New(evtMgr, brContract, btContract, optsGetter, oracleWindowOffset, logger) // Start AutoDepositTracker ctx := context.Background() @@ -110,17 +119,13 @@ func TestAutoDepositTracker_Start(t *testing.T) { assertStatus(t, true, []uint64{2, 3}) - publishNewWindow(evtMgr, &btABI, big.NewInt(1)) - - assertStatus(t, true, []uint64{2, 3, 4}) - publishNewWindow(evtMgr, &btABI, big.NewInt(2)) - assertStatus(t, true, []uint64{2, 3, 4, 5}) + assertStatus(t, true, []uint64{2, 3, 4}) publishNewWindow(evtMgr, &btABI, big.NewInt(3)) - assertStatus(t, true, []uint64{3, 4, 5, 6}) + assertStatus(t, true, []uint64{3, 4, 5}) // Stop AutoDepositTracker windowNumbers, err := adt.Stop() @@ -129,7 +134,7 @@ func TestAutoDepositTracker_Start(t *testing.T) { } // Assert window numbers - expectedWindowNumbers := []*big.Int{big.NewInt(3), big.NewInt(4), big.NewInt(5), big.NewInt(6)} + expectedWindowNumbers := []*big.Int{big.NewInt(3), big.NewInt(4), big.NewInt(5)} if len(windowNumbers) != len(expectedWindowNumbers) { t.Fatalf("expected %d window numbers, got %d", len(expectedWindowNumbers), len(windowNumbers)) } @@ -162,6 +167,9 @@ func TestAutoDepositTracker_Start_CancelContext(t *testing.T) { DepositForWindowsFunc: func(opts *bind.TransactOpts, windows []*big.Int) (*types.Transaction, error) { return types.NewTransaction(1, common.Address{}, nil, 0, nil, nil), nil }, + DepositForWindowFunc: func(opts *bind.TransactOpts, window *big.Int) (*types.Transaction, error) { + return types.NewTransaction(1, common.Address{}, nil, 0, nil, nil), nil + }, } btContract := &MockBlockTrackerContract{ GetCurrentWindowFunc: func() (*big.Int, error) { @@ -172,8 +180,10 @@ func TestAutoDepositTracker_Start_CancelContext(t *testing.T) { return &bind.TransactOpts{}, nil } + oracleWindowOffset := big.NewInt(1) + // Create AutoDepositTracker instance - adt := autodepositor.New(evtMgr, brContract, btContract, optsGetter, logger) + adt := autodepositor.New(evtMgr, brContract, btContract, optsGetter, oracleWindowOffset, logger) // Start AutoDepositTracker with a cancelable context ctx, cancel := context.WithCancel(context.Background()) @@ -208,8 +218,10 @@ func TestAutoDepositTracker_Stop_NotRunning(t *testing.T) { return &bind.TransactOpts{}, nil } + oracleWindowOffset := big.NewInt(1) + // Create AutoDepositTracker instance - adt := autodepositor.New(evtMgr, brContract, btContract, optsGetter, logger) + adt := autodepositor.New(evtMgr, brContract, btContract, optsGetter, oracleWindowOffset, logger) // Stop AutoDepositTracker when not running _, err = adt.Stop() @@ -249,8 +261,10 @@ func TestAutoDepositTracker_IsWorking(t *testing.T) { return &bind.TransactOpts{}, nil } + oracleWindowOffset := big.NewInt(1) + // Create AutoDepositTracker instance - adt := autodepositor.New(evtMgr, brContract, btContract, optsGetter, logger) + adt := autodepositor.New(evtMgr, brContract, btContract, optsGetter, oracleWindowOffset, logger) // Assert initial IsWorking status if adt.IsWorking() { diff --git a/p2p/pkg/depositmanager/deposit.go b/p2p/pkg/depositmanager/deposit.go index 6ec34c072..0978bf56e 100644 --- a/p2p/pkg/depositmanager/deposit.go +++ b/p2p/pkg/depositmanager/deposit.go @@ -115,7 +115,7 @@ func (dm *DepositManager) Start(ctx context.Context) <-chan struct{} { dm.logger.Info("clear balances set balances context done") return nil case window := <-dm.windowChan: - windowToClear := new(big.Int).Sub(window.Window, big.NewInt(2)) + windowToClear := new(big.Int).Sub(window.Window, big.NewInt(1)) windows, err := dm.store.ClearBalances(windowToClear) if err != nil { dm.logger.Error("failed to clear balances", "error", err, "window", windowToClear) diff --git a/p2p/pkg/node/node.go b/p2p/pkg/node/node.go index fc11b75bf..dc60add53 100644 --- a/p2p/pkg/node/node.go +++ b/p2p/pkg/node/node.go @@ -85,6 +85,7 @@ type Options struct { DefaultGasLimit uint64 DefaultGasTipCap *big.Int DefaultGasFeeCap *big.Int + OracleWindowOffset *big.Int } type Node struct { @@ -456,6 +457,7 @@ func NewNode(opts *Options) (*Node, error) { bidderRegistry, blockTrackerSession, optsGetter, + opts.OracleWindowOffset, opts.Logger.With("component", "auto_deposit_tracker"), ) @@ -478,6 +480,7 @@ func NewNode(opts *Options) (*Node, error) { monitor, optsGetter, autoDeposit, + opts.OracleWindowOffset, opts.Logger.With("component", "bidderapi"), ) bidderapiv1.RegisterBidderServer(grpcServer, bidderAPI) diff --git a/p2p/pkg/rpc/bidder/service.go b/p2p/pkg/rpc/bidder/service.go index 78f61b8a7..582630e23 100644 --- a/p2p/pkg/rpc/bidder/service.go +++ b/p2p/pkg/rpc/bidder/service.go @@ -30,6 +30,7 @@ type Service struct { watcher TxWatcher optsGetter OptsGetter autoDepositTracker AutoDepositTracker + oracleWindowOffset *big.Int logger *slog.Logger metrics *metrics validator *protovalidate.Validator @@ -45,6 +46,7 @@ func NewService( watcher TxWatcher, optsGetter OptsGetter, autoDepositTracker AutoDepositTracker, + oracleWindowOffset *big.Int, logger *slog.Logger, ) *Service { return &Service{ @@ -58,6 +60,7 @@ func NewService( logger: logger, metrics: newMetrics(), autoDepositTracker: autoDepositTracker, + oracleWindowOffset: oracleWindowOffset, validator: validator, } } @@ -226,9 +229,9 @@ func (s *Service) calculateWindowToDeposit(ctx context.Context, r *bidderapiv1.D } else if r.BlockNumber != nil { return new(big.Int).SetUint64((r.BlockNumber.Value-1)/s.blocksPerWindow + 1), nil } - // Default to two windows ahead of the current window if no specific block or window is given. - // This is for the case where the oracle works 2 windows behind the current window. - return new(big.Int).SetUint64(currentWindow + 2), nil + // Default to N window ahead of the current window if no specific block or window is given. + // This is for the case where the oracle works N windows behind the current window. + return new(big.Int).SetUint64(currentWindow + s.oracleWindowOffset.Uint64()), nil } func (s *Service) GetDeposit( @@ -244,8 +247,8 @@ func (s *Service) GetDeposit( if err != nil { return nil, status.Errorf(codes.Internal, "getting current window: %v", err) } - // as oracle working 2 windows behind the current window, we add + 2 here - window = new(big.Int).Add(window, big.NewInt(2)) + // as oracle working N windows behind the current window, we add + N here + window = new(big.Int).Add(window, s.oracleWindowOffset) } else { window = new(big.Int).SetUint64(r.WindowNumber.Value) } @@ -503,8 +506,8 @@ func (s *Service) AutoDepositStatus( ) (*bidderapiv1.AutoDepositStatusResponse, error) { deposits, isAutodepositEnabled, currentWindow := s.autoDepositTracker.GetStatus() if currentWindow != nil { - // as oracle working 2 windows behind the current window, we add + 2 here - currentWindow = new(big.Int).Add(currentWindow, big.NewInt(2)) + // as oracle working N windows behind the current window, we add + N here + currentWindow = new(big.Int).Add(currentWindow, s.oracleWindowOffset) } var autoDeposits []*bidderapiv1.AutoDeposit for window, ok := range deposits { diff --git a/p2p/pkg/rpc/bidder/service_test.go b/p2p/pkg/rpc/bidder/service_test.go index 16a3c8f94..298a64389 100644 --- a/p2p/pkg/rpc/bidder/service_test.go +++ b/p2p/pkg/rpc/bidder/service_test.go @@ -216,6 +216,7 @@ func startServer(t *testing.T) bidderapiv1.BidderClient { sender := &testSender{noOfPreconfs: 2} blockTrackerContract := &testBlockTrackerContract{lastBlockNumber: blocksPerWindow + 1, blocksPerWindow: blocksPerWindow, blockNumberToWinner: make(map[uint64]common.Address)} testAutoDepositTracker := &testAutoDepositTracker{deposits: make(map[uint64]bool)} + oracleWindowOffset := big.NewInt(1) srvImpl := bidderapi.NewService( owner, blockTrackerContract.blocksPerWindow, @@ -231,6 +232,7 @@ func startServer(t *testing.T) bidderapiv1.BidderClient { }, nil }, testAutoDepositTracker, + oracleWindowOffset, logger, )