Skip to content

Commit

Permalink
fix: undo event handling
Browse files Browse the repository at this point in the history
  • Loading branch information
Alok committed Jul 9, 2024
1 parent 14abe88 commit 4a3ca37
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 66 deletions.
32 changes: 1 addition & 31 deletions p2p/pkg/autodepositor/autodepositor.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,10 @@ type BidderRegistryContract interface {
WithdrawFromWindows(opts *bind.TransactOpts, windows []*big.Int) (*types.Transaction, error)
}

type TxWatcher interface {
WaitForReceipt(ctx context.Context, tx *types.Transaction) (*types.Receipt, error)
}

type AutoDepositTracker struct {
startMu sync.Mutex
isWorking bool
eventMgr events.EventManager
watcher TxWatcher
deposits sync.Map
windowChan chan *blocktracker.BlocktrackerNewWindow
brContract BidderRegistryContract
Expand All @@ -43,14 +38,12 @@ func New(
evtMgr events.EventManager,
brContract BidderRegistryContract,
optsGetter OptsGetter,
watcher TxWatcher,
logger *slog.Logger,
) *AutoDepositTracker {
return &AutoDepositTracker{
eventMgr: evtMgr,
brContract: brContract,
optsGetter: optsGetter,
watcher: watcher,
windowChan: make(chan *blocktracker.BlocktrackerNewWindow, 1),
logger: logger,
}
Expand All @@ -77,20 +70,11 @@ func (adt *AutoDepositTracker) Start(
opts.Value = big.NewInt(0).Mul(amount, big.NewInt(2))

// Make initial deposit for the first two windows
txn, err := adt.brContract.DepositForWindows(opts, []*big.Int{startWindow, nextWindow})
if err != nil {
return err
}

receipt, err := adt.watcher.WaitForReceipt(ctx, txn)
_, err = adt.brContract.DepositForWindows(opts, []*big.Int{startWindow, nextWindow})
if err != nil {
return err
}

if receipt.Status != types.ReceiptStatusSuccessful {
return fmt.Errorf("initial deposit transaction failed: %d", receipt.Status)
}

adt.deposits.Store(startWindow.Uint64(), true)
adt.deposits.Store(nextWindow.Uint64(), true)

Expand Down Expand Up @@ -143,13 +127,6 @@ func (adt *AutoDepositTracker) Start(
if err != nil {
return err
}
r, err := adt.watcher.WaitForReceipt(egCtx, txn)
if err != nil {
return err
}
if r.Status != types.ReceiptStatusSuccessful {
return fmt.Errorf("withdraw transaction failed: %d", r.Status)
}
adt.logger.Info("withdraw from windows", "hash", txn.Hash(), "windows", withdrawWindows)
for _, window := range withdrawWindows {
adt.deposits.Delete(window.Uint64())
Expand All @@ -174,13 +151,6 @@ func (adt *AutoDepositTracker) Start(
if err != nil {
return err
}
r, err := adt.watcher.WaitForReceipt(egCtx, txn)
if err != nil {
return err
}
if r.Status != types.ReceiptStatusSuccessful {
return fmt.Errorf("deposit transaction failed: %d", r.Status)
}
adt.logger.Info(
"deposited to next window",
"hash", txn.Hash(),
Expand Down
38 changes: 4 additions & 34 deletions p2p/pkg/autodepositor/autodepositor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,6 @@ func (m *MockBidderRegistryContract) WithdrawFromWindows(opts *bind.TransactOpts
return m.WithdrawFromWindowsFunc(opts, windows)
}

type MockTxWatcher struct {
WaitForReceiptFunc func(ctx context.Context, tx *types.Transaction) (*types.Receipt, error)
}

func (m *MockTxWatcher) WaitForReceipt(ctx context.Context, tx *types.Transaction) (*types.Receipt, error) {
return m.WaitForReceiptFunc(ctx, tx)
}

func TestAutoDepositTracker_Start(t *testing.T) {
t.Parallel()

Expand Down Expand Up @@ -68,16 +60,9 @@ func TestAutoDepositTracker_Start(t *testing.T) {
optsGetter := func(ctx context.Context) (*bind.TransactOpts, error) {
return &bind.TransactOpts{}, nil
}
watcher := &MockTxWatcher{
WaitForReceiptFunc: func(ctx context.Context, tx *types.Transaction) (*types.Receipt, error) {
return &types.Receipt{
Status: 1,
}, nil
},
}

// Create AutoDepositTracker instance
adt := autodepositor.New(evtMgr, brContract, optsGetter, watcher, logger)
adt := autodepositor.New(evtMgr, brContract, optsGetter, logger)

// Start AutoDepositTracker
ctx := context.Background()
Expand Down Expand Up @@ -168,16 +153,9 @@ func TestAutoDepositTracker_Start_CancelContext(t *testing.T) {
optsGetter := func(ctx context.Context) (*bind.TransactOpts, error) {
return &bind.TransactOpts{}, nil
}
watcher := &MockTxWatcher{
WaitForReceiptFunc: func(ctx context.Context, tx *types.Transaction) (*types.Receipt, error) {
return &types.Receipt{
Status: 1,
}, nil
},
}

// Create AutoDepositTracker instance
adt := autodepositor.New(evtMgr, brContract, optsGetter, watcher, logger)
adt := autodepositor.New(evtMgr, brContract, optsGetter, logger)

// Start AutoDepositTracker with a cancelable context
ctx, cancel := context.WithCancel(context.Background())
Expand Down Expand Up @@ -210,10 +188,9 @@ func TestAutoDepositTracker_Stop_NotRunning(t *testing.T) {
optsGetter := func(ctx context.Context) (*bind.TransactOpts, error) {
return &bind.TransactOpts{}, nil
}
watcher := &MockTxWatcher{}

// Create AutoDepositTracker instance
adt := autodepositor.New(evtMgr, brContract, optsGetter, watcher, logger)
adt := autodepositor.New(evtMgr, brContract, optsGetter, logger)

// Stop AutoDepositTracker when not running
_, err = adt.Stop()
Expand Down Expand Up @@ -246,16 +223,9 @@ func TestAutoDepositTracker_IsWorking(t *testing.T) {
optsGetter := func(ctx context.Context) (*bind.TransactOpts, error) {
return &bind.TransactOpts{}, nil
}
watcher := &MockTxWatcher{
WaitForReceiptFunc: func(ctx context.Context, tx *types.Transaction) (*types.Receipt, error) {
return &types.Receipt{
Status: 1,
}, nil
},
}

// Create AutoDepositTracker instance
adt := autodepositor.New(evtMgr, brContract, optsGetter, watcher, logger)
adt := autodepositor.New(evtMgr, brContract, optsGetter, logger)

// Assert initial IsWorking status
if adt.IsWorking() {
Expand Down
1 change: 0 additions & 1 deletion p2p/pkg/node/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -440,7 +440,6 @@ func NewNode(opts *Options) (*Node, error) {
evtMgr,
bidderRegistry,
optsGetter,
monitor,
opts.Logger.With("component", "auto_deposit_tracker"),
)

Expand Down

0 comments on commit 4a3ca37

Please sign in to comment.