Skip to content

Commit bf20b62

Browse files
Mikellemrekucci
authored andcommitted
feat(oracle): change window offset to 1 (#274)
* chore: from 2 windows to 1 * fix: fixed comment * chore: merged with main and made oracle offset const
1 parent d58e930 commit bf20b62

File tree

9 files changed

+56
-30
lines changed

9 files changed

+56
-30
lines changed

infrastructure/nomad/playbooks/templates/jobs/mev-commit-oracle.nomad.j2

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ job "{{ job.name }}" {
185185
else 'service:' + job.name + '-{{ env "NOMAD_ALLOC_INDEX" }}'
186186
}}"
187187
MEV_ORACLE_LOG_LEVEL="{{ job.env.get('log-level', 'info') }}"
188-
MEV_ORACLE_LAGGERD_MODE="{{ job.env.get('laggerd-mode', '20') }}"
188+
MEV_ORACLE_LAGGERD_MODE="{{ job.env.get('laggerd-mode', '10') }}"
189189
MEV_ORACLE_L1_RPC_URL="{{ job.env['l1_rpc_url'] }}"
190190
{%- raw %}
191191
MEV_ORACLE_KEYSTORE_PATH="/local/data-{{ env "NOMAD_ALLOC_INDEX" }}/keystore"

oracle/cmd/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ var (
190190
Name: "laggerd-mode",
191191
Usage: "No of blocks to lag behind for L1 chain",
192192
EnvVars: []string{"MEV_ORACLE_LAGGERD_MODE"},
193-
Value: 64,
193+
Value: 10,
194194
})
195195

196196
optionOverrideWinners = altsrc.NewStringSliceFlag(&cli.StringSliceFlag{

p2p/cmd/main.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ const (
3131
defaultSecret = "secret"
3232
defaultKeystore = "keystore"
3333
defaultDataDir = "db"
34+
35+
defaultOracleWindowOffset = 1
3436
)
3537

3638
var (
@@ -481,6 +483,7 @@ func launchNodeWithConfig(c *cli.Context) error {
481483
DefaultGasLimit: uint64(c.Int(optionGasLimit.Name)),
482484
DefaultGasTipCap: gasTipCap,
483485
DefaultGasFeeCap: gasFeeCap,
486+
OracleWindowOffset: big.NewInt(defaultOracleWindowOffset),
484487
})
485488
if err != nil {
486489
return fmt.Errorf("failed starting node: %w", err)

p2p/pkg/autodepositor/autodepositor.go

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ type OptsGetter func(context.Context) (*bind.TransactOpts, error)
2222

2323
type BidderRegistryContract interface {
2424
DepositForWindows(opts *bind.TransactOpts, windows []*big.Int) (*types.Transaction, error)
25+
DepositForWindow(opts *bind.TransactOpts, window *big.Int) (*types.Transaction, error)
2526
WithdrawFromWindows(opts *bind.TransactOpts, windows []*big.Int) (*types.Transaction, error)
2627
}
2728

@@ -51,6 +52,7 @@ type AutoDepositTracker struct {
5152
store DepositStore
5253
optsGetter OptsGetter
5354
currentOracleWindow atomic.Value
55+
oracleWindowOffset *big.Int
5456
logger *slog.Logger
5557
cancelFunc context.CancelFunc
5658
}
@@ -61,6 +63,7 @@ func New(
6163
btContract BlockTrackerContract,
6264
optsGetter OptsGetter,
6365
store DepositStore,
66+
oracleWindowOffset *big.Int,
6467
logger *slog.Logger,
6568
) *AutoDepositTracker {
6669
return &AutoDepositTracker{
@@ -69,6 +72,7 @@ func New(
6972
btContract: btContract,
7073
optsGetter: optsGetter,
7174
store: store,
75+
oracleWindowOffset: oracleWindowOffset,
7276
windowChan: make(chan *blocktracker.BlocktrackerNewWindow, 1),
7377
logger: logger,
7478
}
@@ -93,8 +97,8 @@ func (adt *AutoDepositTracker) Start(
9397

9498
if startWindow == nil {
9599
startWindow = currentOracleWindow
96-
// adding +2 as oracle runs two windows behind
97-
startWindow = new(big.Int).Add(startWindow, big.NewInt(2))
100+
// adding + N as oracle runs N window behind
101+
startWindow = new(big.Int).Add(startWindow, adt.oracleWindowOffset)
98102
}
99103

100104
eg, egCtx := errgroup.WithContext(context.Background())
@@ -219,10 +223,10 @@ func (adt *AutoDepositTracker) startAutodeposit(egCtx context.Context, eg *errgr
219223
}
220224
}
221225

222-
// Make deposit for the next window. The window event is 2 windows
223-
// behind the current window in progress. So we need to make deposit
224-
// for the next window.
225-
nextWindow := new(big.Int).Add(window.Window, big.NewInt(3))
226+
// Make deposit for the next window. The window event is N windows
227+
// behind the current window in progress.
228+
nextWindow := new(big.Int).Add(window.Window, adt.oracleWindowOffset)
229+
nextWindow = new(big.Int).Add(nextWindow, big.NewInt(1))
226230
if adt.store.IsDepositMade(egCtx, nextWindow) {
227231
continue
228232
}
@@ -232,8 +236,8 @@ func (adt *AutoDepositTracker) startAutodeposit(egCtx context.Context, eg *errgr
232236
return err
233237
}
234238
opts.Value = amount
235-
236-
txn, err := adt.brContract.DepositForWindows(opts, []*big.Int{nextWindow})
239+
240+
txn, err := adt.brContract.DepositForWindow(opts, nextWindow)
237241
if err != nil {
238242
return err
239243
}

p2p/pkg/autodepositor/autodepositor_test.go

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,17 @@ import (
2525
type MockBidderRegistryContract struct {
2626
DepositForWindowsFunc func(opts *bind.TransactOpts, windows []*big.Int) (*types.Transaction, error)
2727
WithdrawFromWindowsFunc func(opts *bind.TransactOpts, windows []*big.Int) (*types.Transaction, error)
28+
DepositForWindowFunc func(opts *bind.TransactOpts, window *big.Int) (*types.Transaction, error)
2829
}
2930

3031
func (m *MockBidderRegistryContract) DepositForWindows(opts *bind.TransactOpts, windows []*big.Int) (*types.Transaction, error) {
3132
return m.DepositForWindowsFunc(opts, windows)
3233
}
3334

35+
func (m *MockBidderRegistryContract) DepositForWindow(opts *bind.TransactOpts, window *big.Int) (*types.Transaction, error) {
36+
return m.DepositForWindowFunc(opts, window)
37+
}
38+
3439
func (m *MockBidderRegistryContract) WithdrawFromWindows(opts *bind.TransactOpts, windows []*big.Int) (*types.Transaction, error) {
3540
return m.WithdrawFromWindowsFunc(opts, windows)
3641
}
@@ -58,6 +63,7 @@ func TestAutoDepositTracker_Start(t *testing.T) {
5863
}
5964

6065
amount := big.NewInt(100)
66+
oracleWindowOffset := big.NewInt(1)
6167
logger := util.NewTestLogger(os.Stdout)
6268
evtMgr := events.NewListener(logger, &btABI, &brABI)
6369
brContract := &MockBidderRegistryContract{
@@ -67,6 +73,9 @@ func TestAutoDepositTracker_Start(t *testing.T) {
6773
WithdrawFromWindowsFunc: func(opts *bind.TransactOpts, windows []*big.Int) (*types.Transaction, error) {
6874
return types.NewTransaction(1, common.Address{}, nil, 0, nil, nil), nil
6975
},
76+
DepositForWindowFunc: func(opts *bind.TransactOpts, window *big.Int) (*types.Transaction, error) {
77+
return types.NewTransaction(1, common.Address{}, nil, 0, nil, nil), nil
78+
},
7079
}
7180
btContract := &MockBlockTrackerContract{
7281
GetCurrentWindowFunc: func() (*big.Int, error) {
@@ -80,7 +89,7 @@ func TestAutoDepositTracker_Start(t *testing.T) {
8089
st := store.New(inmemstorage.New())
8190

8291
// Create AutoDepositTracker instance
83-
adt := autodepositor.New(evtMgr, brContract, btContract, optsGetter, st, logger)
92+
adt := autodepositor.New(evtMgr, brContract, btContract, optsGetter, st, oracleWindowOffset, logger)
8493

8594
// Start AutoDepositTracker
8695
ctx := context.Background()
@@ -114,17 +123,13 @@ func TestAutoDepositTracker_Start(t *testing.T) {
114123

115124
assertStatus(t, true, []uint64{2, 3})
116125

117-
publishNewWindow(evtMgr, &btABI, big.NewInt(1))
118-
119-
assertStatus(t, true, []uint64{2, 3, 4})
120-
121126
publishNewWindow(evtMgr, &btABI, big.NewInt(2))
122127

123-
assertStatus(t, true, []uint64{2, 3, 4, 5})
128+
assertStatus(t, true, []uint64{2, 3, 4})
124129

125130
publishNewWindow(evtMgr, &btABI, big.NewInt(3))
126131

127-
assertStatus(t, true, []uint64{3, 4, 5, 6})
132+
assertStatus(t, true, []uint64{3, 4, 5})
128133

129134
// Stop AutoDepositTracker
130135
windowNumbers, err := adt.Stop()
@@ -133,7 +138,7 @@ func TestAutoDepositTracker_Start(t *testing.T) {
133138
}
134139

135140
// Assert window numbers
136-
expectedWindowNumbers := []*big.Int{big.NewInt(3), big.NewInt(4), big.NewInt(5), big.NewInt(6)}
141+
expectedWindowNumbers := []*big.Int{big.NewInt(3), big.NewInt(4), big.NewInt(5)}
137142
if len(windowNumbers) != len(expectedWindowNumbers) {
138143
t.Fatalf("expected %d window numbers, got %d", len(expectedWindowNumbers), len(windowNumbers))
139144
}
@@ -143,7 +148,7 @@ func TestAutoDepositTracker_Start(t *testing.T) {
143148
}
144149
}
145150

146-
assertStatus(t, false, []uint64{3, 4, 5, 6})
151+
assertStatus(t, false, []uint64{3, 4, 5})
147152
}
148153

149154
func TestAutoDepositTracker_Start_CancelContext(t *testing.T) {
@@ -166,6 +171,9 @@ func TestAutoDepositTracker_Start_CancelContext(t *testing.T) {
166171
DepositForWindowsFunc: func(opts *bind.TransactOpts, windows []*big.Int) (*types.Transaction, error) {
167172
return types.NewTransaction(1, common.Address{}, nil, 0, nil, nil), nil
168173
},
174+
DepositForWindowFunc: func(opts *bind.TransactOpts, window *big.Int) (*types.Transaction, error) {
175+
return types.NewTransaction(1, common.Address{}, nil, 0, nil, nil), nil
176+
},
169177
}
170178
btContract := &MockBlockTrackerContract{
171179
GetCurrentWindowFunc: func() (*big.Int, error) {
@@ -176,10 +184,11 @@ func TestAutoDepositTracker_Start_CancelContext(t *testing.T) {
176184
return &bind.TransactOpts{}, nil
177185
}
178186

187+
oracleWindowOffset := big.NewInt(1)
179188
st := store.New(inmemstorage.New())
180189

181190
// Create AutoDepositTracker instance
182-
adt := autodepositor.New(evtMgr, brContract, btContract, optsGetter, st, logger)
191+
adt := autodepositor.New(evtMgr, brContract, btContract, optsGetter, st, oracleWindowOffset, logger)
183192

184193
// Start AutoDepositTracker with a cancelable context
185194
ctx, cancel := context.WithCancel(context.Background())
@@ -214,10 +223,11 @@ func TestAutoDepositTracker_Stop_NotRunning(t *testing.T) {
214223
return &bind.TransactOpts{}, nil
215224
}
216225

226+
oracleWindowOffset := big.NewInt(1)
217227
st := store.New(inmemstorage.New())
218228

219229
// Create AutoDepositTracker instance
220-
adt := autodepositor.New(evtMgr, brContract, btContract, optsGetter, st, logger)
230+
adt := autodepositor.New(evtMgr, brContract, btContract, optsGetter, st, oracleWindowOffset, logger)
221231

222232
// Stop AutoDepositTracker when not running
223233
_, err = adt.Stop()
@@ -257,10 +267,11 @@ func TestAutoDepositTracker_IsWorking(t *testing.T) {
257267
return &bind.TransactOpts{}, nil
258268
}
259269

270+
oracleWindowOffset := big.NewInt(1)
260271
st := store.New(inmemstorage.New())
261272

262273
// Create AutoDepositTracker instance
263-
adt := autodepositor.New(evtMgr, brContract, btContract, optsGetter, st, logger)
274+
adt := autodepositor.New(evtMgr, brContract, btContract, optsGetter, st, oracleWindowOffset, logger)
264275

265276
// Assert initial IsWorking status
266277
if adt.IsWorking() {

p2p/pkg/depositmanager/deposit.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ func (dm *DepositManager) Start(ctx context.Context) <-chan struct{} {
115115
dm.logger.Info("clear balances set balances context done")
116116
return nil
117117
case window := <-dm.windowChan:
118-
windowToClear := new(big.Int).Sub(window.Window, big.NewInt(2))
118+
windowToClear := new(big.Int).Sub(window.Window, big.NewInt(1))
119119
windows, err := dm.store.ClearBalances(windowToClear)
120120
if err != nil {
121121
dm.logger.Error("failed to clear balances", "error", err, "window", windowToClear)

p2p/pkg/node/node.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ type Options struct {
9393
DefaultGasLimit uint64
9494
DefaultGasTipCap *big.Int
9595
DefaultGasFeeCap *big.Int
96+
OracleWindowOffset *big.Int
9697
}
9798

9899
type Node struct {
@@ -465,6 +466,7 @@ func NewNode(opts *Options) (*Node, error) {
465466
blockTrackerSession,
466467
optsGetter,
467468
autodepositorStore,
469+
opts.OracleWindowOffset,
468470
opts.Logger.With("component", "auto_deposit_tracker"),
469471
)
470472

@@ -488,6 +490,7 @@ func NewNode(opts *Options) (*Node, error) {
488490
optsGetter,
489491
autoDeposit,
490492
autodepositorStore,
493+
opts.OracleWindowOffset,
491494
opts.Logger.With("component", "bidderapi"),
492495
)
493496
bidderapiv1.RegisterBidderServer(grpcServer, bidderAPI)

p2p/pkg/rpc/bidder/service.go

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ type Service struct {
3131
optsGetter OptsGetter
3232
autoDepositTracker AutoDepositTracker
3333
store DepositStore
34+
oracleWindowOffset *big.Int
3435
logger *slog.Logger
3536
metrics *metrics
3637
validator *protovalidate.Validator
@@ -47,6 +48,7 @@ func NewService(
4748
optsGetter OptsGetter,
4849
autoDepositTracker AutoDepositTracker,
4950
store DepositStore,
51+
oracleWindowOffset *big.Int,
5052
logger *slog.Logger,
5153
) *Service {
5254
return &Service{
@@ -60,6 +62,7 @@ func NewService(
6062
logger: logger,
6163
metrics: newMetrics(),
6264
autoDepositTracker: autoDepositTracker,
65+
oracleWindowOffset: oracleWindowOffset,
6366
store: store,
6467
validator: validator,
6568
}
@@ -243,9 +246,9 @@ func (s *Service) calculateWindowToDeposit(ctx context.Context, r *bidderapiv1.D
243246
} else if r.BlockNumber != nil {
244247
return new(big.Int).SetUint64((r.BlockNumber.Value-1)/s.blocksPerWindow + 1), nil
245248
}
246-
// Default to two windows ahead of the current window if no specific block or window is given.
247-
// This is for the case where the oracle works 2 windows behind the current window.
248-
return new(big.Int).SetUint64(currentWindow + 2), nil
249+
// Default to N window ahead of the current window if no specific block or window is given.
250+
// This is for the case where the oracle works N windows behind the current window.
251+
return new(big.Int).SetUint64(currentWindow + s.oracleWindowOffset.Uint64()), nil
249252
}
250253

251254
func (s *Service) GetDeposit(
@@ -261,8 +264,8 @@ func (s *Service) GetDeposit(
261264
if err != nil {
262265
return nil, status.Errorf(codes.Internal, "getting current window: %v", err)
263266
}
264-
// as oracle working 2 windows behind the current window, we add + 2 here
265-
window = new(big.Int).Add(window, big.NewInt(2))
267+
// as oracle working N windows behind the current window, we add + N here
268+
window = new(big.Int).Add(window, s.oracleWindowOffset)
266269
} else {
267270
window = new(big.Int).SetUint64(r.WindowNumber.Value)
268271
}
@@ -535,8 +538,8 @@ func (s *Service) AutoDepositStatus(
535538
) (*bidderapiv1.AutoDepositStatusResponse, error) {
536539
deposits, isAutodepositEnabled, currentWindow := s.autoDepositTracker.GetStatus()
537540
if currentWindow != nil {
538-
// as oracle working 2 windows behind the current window, we add + 2 here
539-
currentWindow = new(big.Int).Add(currentWindow, big.NewInt(2))
541+
// as oracle working N windows behind the current window, we add + N here
542+
currentWindow = new(big.Int).Add(currentWindow, s.oracleWindowOffset)
540543
}
541544
var autoDeposits []*bidderapiv1.AutoDeposit
542545
for window, ok := range deposits {

p2p/pkg/rpc/bidder/service_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,7 @@ func startServer(t *testing.T) bidderapiv1.BidderClient {
218218
sender := &testSender{noOfPreconfs: 2}
219219
blockTrackerContract := &testBlockTrackerContract{lastBlockNumber: blocksPerWindow + 1, blocksPerWindow: blocksPerWindow, blockNumberToWinner: make(map[uint64]common.Address)}
220220
testAutoDepositTracker := &testAutoDepositTracker{deposits: make(map[uint64]bool)}
221+
oracleWindowOffset := big.NewInt(1)
221222
store := autodepositorstore.New(inmemstorage.New())
222223
srvImpl := bidderapi.NewService(
223224
owner,
@@ -235,6 +236,7 @@ func startServer(t *testing.T) bidderapiv1.BidderClient {
235236
},
236237
testAutoDepositTracker,
237238
store,
239+
oracleWindowOffset,
238240
logger,
239241
)
240242

0 commit comments

Comments
 (0)