From 13873981ae739fd6f02e7e7a418c68982a11f35b Mon Sep 17 00:00:00 2001 From: Mikhail Wall Date: Mon, 29 Jul 2024 21:04:16 +0200 Subject: [PATCH] Feat/272/window 1 (#274) * chore: from 2 windows to 1 * fix: fixed comment * chore: merged with main and made oracle offset const --- .../templates/jobs/mev-commit-oracle.nomad.j2 | 2 +- oracle/cmd/main.go | 2 +- p2p/cmd/main.go | 3 ++ p2p/pkg/autodepositor/autodepositor.go | 20 ++++++----- p2p/pkg/autodepositor/autodepositor_test.go | 35 ++++++++++++------- p2p/pkg/depositmanager/deposit.go | 2 +- p2p/pkg/node/node.go | 3 ++ p2p/pkg/rpc/bidder/service.go | 17 +++++---- p2p/pkg/rpc/bidder/service_test.go | 2 ++ 9 files changed, 56 insertions(+), 30 deletions(-) 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 8f1d73f0e..73e2b8356 100644 --- a/infrastructure/nomad/playbooks/templates/jobs/mev-commit-oracle.nomad.j2 +++ b/infrastructure/nomad/playbooks/templates/jobs/mev-commit-oracle.nomad.j2 @@ -185,7 +185,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 120af20a0..9426f38d7 100644 --- a/p2p/cmd/main.go +++ b/p2p/cmd/main.go @@ -31,6 +31,8 @@ const ( defaultSecret = "secret" defaultKeystore = "keystore" defaultDataDir = "db" + + defaultOracleWindowOffset = 1 ) var ( @@ -481,6 +483,7 @@ func launchNodeWithConfig(c *cli.Context) error { DefaultGasLimit: uint64(c.Int(optionGasLimit.Name)), DefaultGasTipCap: gasTipCap, DefaultGasFeeCap: gasFeeCap, + OracleWindowOffset: big.NewInt(defaultOracleWindowOffset), }) 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 15967f3e0..f1dc1ccd9 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) } @@ -51,6 +52,7 @@ type AutoDepositTracker struct { store DepositStore optsGetter OptsGetter currentOracleWindow atomic.Value + oracleWindowOffset *big.Int logger *slog.Logger cancelFunc context.CancelFunc } @@ -61,6 +63,7 @@ func New( btContract BlockTrackerContract, optsGetter OptsGetter, store DepositStore, + oracleWindowOffset *big.Int, logger *slog.Logger, ) *AutoDepositTracker { return &AutoDepositTracker{ @@ -69,6 +72,7 @@ func New( btContract: btContract, optsGetter: optsGetter, store: store, + oracleWindowOffset: oracleWindowOffset, windowChan: make(chan *blocktracker.BlocktrackerNewWindow, 1), logger: logger, } @@ -93,8 +97,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 + N as oracle runs N window behind + startWindow = new(big.Int).Add(startWindow, adt.oracleWindowOffset) } eg, egCtx := errgroup.WithContext(context.Background()) @@ -219,10 +223,10 @@ 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 N 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 adt.store.IsDepositMade(egCtx, nextWindow) { continue } @@ -232,8 +236,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 dbf650557..8ba1f2267 100644 --- a/p2p/pkg/autodepositor/autodepositor_test.go +++ b/p2p/pkg/autodepositor/autodepositor_test.go @@ -25,12 +25,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) } @@ -58,6 +63,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{ @@ -67,6 +73,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) { @@ -80,7 +89,7 @@ func TestAutoDepositTracker_Start(t *testing.T) { st := store.New(inmemstorage.New()) // Create AutoDepositTracker instance - adt := autodepositor.New(evtMgr, brContract, btContract, optsGetter, st, logger) + adt := autodepositor.New(evtMgr, brContract, btContract, optsGetter, st, oracleWindowOffset, logger) // Start AutoDepositTracker ctx := context.Background() @@ -114,17 +123,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() @@ -133,7 +138,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)) } @@ -143,7 +148,7 @@ func TestAutoDepositTracker_Start(t *testing.T) { } } - assertStatus(t, false, []uint64{3, 4, 5, 6}) + assertStatus(t, false, []uint64{3, 4, 5}) } func TestAutoDepositTracker_Start_CancelContext(t *testing.T) { @@ -166,6 +171,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) { @@ -176,10 +184,11 @@ func TestAutoDepositTracker_Start_CancelContext(t *testing.T) { return &bind.TransactOpts{}, nil } + oracleWindowOffset := big.NewInt(1) st := store.New(inmemstorage.New()) // Create AutoDepositTracker instance - adt := autodepositor.New(evtMgr, brContract, btContract, optsGetter, st, logger) + adt := autodepositor.New(evtMgr, brContract, btContract, optsGetter, st, oracleWindowOffset, logger) // Start AutoDepositTracker with a cancelable context ctx, cancel := context.WithCancel(context.Background()) @@ -214,10 +223,11 @@ func TestAutoDepositTracker_Stop_NotRunning(t *testing.T) { return &bind.TransactOpts{}, nil } + oracleWindowOffset := big.NewInt(1) st := store.New(inmemstorage.New()) // Create AutoDepositTracker instance - adt := autodepositor.New(evtMgr, brContract, btContract, optsGetter, st, logger) + adt := autodepositor.New(evtMgr, brContract, btContract, optsGetter, st, oracleWindowOffset, logger) // Stop AutoDepositTracker when not running _, err = adt.Stop() @@ -257,10 +267,11 @@ func TestAutoDepositTracker_IsWorking(t *testing.T) { return &bind.TransactOpts{}, nil } + oracleWindowOffset := big.NewInt(1) st := store.New(inmemstorage.New()) // Create AutoDepositTracker instance - adt := autodepositor.New(evtMgr, brContract, btContract, optsGetter, st, logger) + adt := autodepositor.New(evtMgr, brContract, btContract, optsGetter, st, 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 0305a60b3..6c1ef4496 100644 --- a/p2p/pkg/node/node.go +++ b/p2p/pkg/node/node.go @@ -93,6 +93,7 @@ type Options struct { DefaultGasLimit uint64 DefaultGasTipCap *big.Int DefaultGasFeeCap *big.Int + OracleWindowOffset *big.Int } type Node struct { @@ -465,6 +466,7 @@ func NewNode(opts *Options) (*Node, error) { blockTrackerSession, optsGetter, autodepositorStore, + opts.OracleWindowOffset, opts.Logger.With("component", "auto_deposit_tracker"), ) @@ -488,6 +490,7 @@ func NewNode(opts *Options) (*Node, error) { optsGetter, autoDeposit, autodepositorStore, + 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 7d8998fbb..b08336eb4 100644 --- a/p2p/pkg/rpc/bidder/service.go +++ b/p2p/pkg/rpc/bidder/service.go @@ -31,6 +31,7 @@ type Service struct { optsGetter OptsGetter autoDepositTracker AutoDepositTracker store DepositStore + oracleWindowOffset *big.Int logger *slog.Logger metrics *metrics validator *protovalidate.Validator @@ -47,6 +48,7 @@ func NewService( optsGetter OptsGetter, autoDepositTracker AutoDepositTracker, store DepositStore, + oracleWindowOffset *big.Int, logger *slog.Logger, ) *Service { return &Service{ @@ -60,6 +62,7 @@ func NewService( logger: logger, metrics: newMetrics(), autoDepositTracker: autoDepositTracker, + oracleWindowOffset: oracleWindowOffset, store: store, validator: validator, } @@ -243,9 +246,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( @@ -261,8 +264,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) } @@ -535,8 +538,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 584de9787..e068506ab 100644 --- a/p2p/pkg/rpc/bidder/service_test.go +++ b/p2p/pkg/rpc/bidder/service_test.go @@ -218,6 +218,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) store := autodepositorstore.New(inmemstorage.New()) srvImpl := bidderapi.NewService( owner, @@ -235,6 +236,7 @@ func startServer(t *testing.T) bidderapiv1.BidderClient { }, testAutoDepositTracker, store, + oracleWindowOffset, logger, )