Skip to content

Commit

Permalink
feat(oracle): change window offset to 1 (#274)
Browse files Browse the repository at this point in the history
* chore: from 2 windows to 1

* fix: fixed comment

* chore: merged with main and made oracle offset const
  • Loading branch information
Mikelle authored and mrekucci committed Jul 30, 2024
1 parent d58e930 commit bf20b62
Show file tree
Hide file tree
Showing 9 changed files with 56 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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"
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
3 changes: 3 additions & 0 deletions p2p/cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ const (
defaultSecret = "secret"
defaultKeystore = "keystore"
defaultDataDir = "db"

defaultOracleWindowOffset = 1
)

var (
Expand Down Expand Up @@ -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)
Expand Down
20 changes: 12 additions & 8 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 Down Expand Up @@ -51,6 +52,7 @@ type AutoDepositTracker struct {
store DepositStore
optsGetter OptsGetter
currentOracleWindow atomic.Value
oracleWindowOffset *big.Int
logger *slog.Logger
cancelFunc context.CancelFunc
}
Expand All @@ -61,6 +63,7 @@ func New(
btContract BlockTrackerContract,
optsGetter OptsGetter,
store DepositStore,
oracleWindowOffset *big.Int,
logger *slog.Logger,
) *AutoDepositTracker {
return &AutoDepositTracker{
Expand All @@ -69,6 +72,7 @@ func New(
btContract: btContract,
optsGetter: optsGetter,
store: store,
oracleWindowOffset: oracleWindowOffset,
windowChan: make(chan *blocktracker.BlocktrackerNewWindow, 1),
logger: logger,
}
Expand All @@ -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())
Expand Down Expand Up @@ -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
}
Expand All @@ -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
}
Expand Down
35 changes: 23 additions & 12 deletions p2p/pkg/autodepositor/autodepositor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down Expand Up @@ -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{
Expand All @@ -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) {
Expand All @@ -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()
Expand Down Expand Up @@ -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()
Expand All @@ -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))
}
Expand All @@ -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) {
Expand All @@ -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) {
Expand All @@ -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())
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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() {
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 @@ -93,6 +93,7 @@ type Options struct {
DefaultGasLimit uint64
DefaultGasTipCap *big.Int
DefaultGasFeeCap *big.Int
OracleWindowOffset *big.Int
}

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

Expand All @@ -488,6 +490,7 @@ func NewNode(opts *Options) (*Node, error) {
optsGetter,
autoDeposit,
autodepositorStore,
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 @@ -31,6 +31,7 @@ type Service struct {
optsGetter OptsGetter
autoDepositTracker AutoDepositTracker
store DepositStore
oracleWindowOffset *big.Int
logger *slog.Logger
metrics *metrics
validator *protovalidate.Validator
Expand All @@ -47,6 +48,7 @@ func NewService(
optsGetter OptsGetter,
autoDepositTracker AutoDepositTracker,
store DepositStore,
oracleWindowOffset *big.Int,
logger *slog.Logger,
) *Service {
return &Service{
Expand All @@ -60,6 +62,7 @@ func NewService(
logger: logger,
metrics: newMetrics(),
autoDepositTracker: autoDepositTracker,
oracleWindowOffset: oracleWindowOffset,
store: store,
validator: validator,
}
Expand Down Expand Up @@ -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(
Expand All @@ -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)
}
Expand Down Expand Up @@ -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 {
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 @@ -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,
Expand All @@ -235,6 +236,7 @@ func startServer(t *testing.T) bidderapiv1.BidderClient {
},
testAutoDepositTracker,
store,
oracleWindowOffset,
logger,
)

Expand Down

0 comments on commit bf20b62

Please sign in to comment.