Skip to content

Commit

Permalink
chore: from 2 windows to 1
Browse files Browse the repository at this point in the history
  • Loading branch information
Mikelle committed Jul 25, 2024
1 parent 9529ee8 commit 2d9476c
Show file tree
Hide file tree
Showing 9 changed files with 71 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
2 changes: 1 addition & 1 deletion oracle/cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down
9 changes: 9 additions & 0 deletions p2p/cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -346,6 +353,7 @@ func main() {
optionGasLimit,
optionGasTipCap,
optionGasFeeCap,
optionOracleWindowOffset,
}

app := &cli.App{
Expand Down Expand Up @@ -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)
Expand Down
33 changes: 19 additions & 14 deletions p2p/pkg/autodepositor/autodepositor.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand All @@ -39,6 +40,7 @@ type AutoDepositTracker struct {
btContract BlockTrackerContract
optsGetter OptsGetter
currentOracleWindow atomic.Value
oracleWindowOffset *big.Int
logger *slog.Logger
cancelFunc context.CancelFunc
}
Expand All @@ -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,
}
}

Expand All @@ -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())
Expand Down Expand Up @@ -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
}
Expand All @@ -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
}
Expand Down
36 changes: 25 additions & 11 deletions p2p/pkg/autodepositor/autodepositor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down Expand Up @@ -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{
Expand All @@ -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) {
Expand All @@ -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()
Expand Down Expand Up @@ -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()
Expand All @@ -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))
}
Expand Down Expand Up @@ -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) {
Expand All @@ -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())
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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() {
Expand Down
2 changes: 1 addition & 1 deletion p2p/pkg/depositmanager/deposit.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
3 changes: 3 additions & 0 deletions p2p/pkg/node/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ type Options struct {
DefaultGasLimit uint64
DefaultGasTipCap *big.Int
DefaultGasFeeCap *big.Int
OracleWindowOffset *big.Int
}

type Node struct {
Expand Down Expand Up @@ -456,6 +457,7 @@ func NewNode(opts *Options) (*Node, error) {
bidderRegistry,
blockTrackerSession,
optsGetter,
opts.OracleWindowOffset,
opts.Logger.With("component", "auto_deposit_tracker"),
)

Expand All @@ -478,6 +480,7 @@ func NewNode(opts *Options) (*Node, error) {
monitor,
optsGetter,
autoDeposit,
opts.OracleWindowOffset,
opts.Logger.With("component", "bidderapi"),
)
bidderapiv1.RegisterBidderServer(grpcServer, bidderAPI)
Expand Down
17 changes: 10 additions & 7 deletions p2p/pkg/rpc/bidder/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ type Service struct {
watcher TxWatcher
optsGetter OptsGetter
autoDepositTracker AutoDepositTracker
oracleWindowOffset *big.Int
logger *slog.Logger
metrics *metrics
validator *protovalidate.Validator
Expand All @@ -45,6 +46,7 @@ func NewService(
watcher TxWatcher,
optsGetter OptsGetter,
autoDepositTracker AutoDepositTracker,
oracleWindowOffset *big.Int,
logger *slog.Logger,
) *Service {
return &Service{
Expand All @@ -58,6 +60,7 @@ func NewService(
logger: logger,
metrics: newMetrics(),
autoDepositTracker: autoDepositTracker,
oracleWindowOffset: oracleWindowOffset,
validator: validator,
}
}
Expand Down Expand Up @@ -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(
Expand All @@ -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)
}
Expand Down Expand Up @@ -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 {
Expand Down
2 changes: 2 additions & 0 deletions p2p/pkg/rpc/bidder/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -231,6 +232,7 @@ func startServer(t *testing.T) bidderapiv1.BidderClient {
}, nil
},
testAutoDepositTracker,
oracleWindowOffset,
logger,
)

Expand Down

0 comments on commit 2d9476c

Please sign in to comment.