Skip to content

Commit

Permalink
chore: get rid of startFromApi
Browse files Browse the repository at this point in the history
  • Loading branch information
Mikelle committed Jul 9, 2024
1 parent 121e301 commit a34ba77
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 65 deletions.
69 changes: 13 additions & 56 deletions p2p/pkg/autodepositor/autodepositor.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,62 +59,7 @@ func New(
}
}

func (adt *AutoDepositTracker) Start(ctx context.Context) <-chan struct{} {
adt.startMu.Lock()
defer adt.startMu.Unlock()

doneChan := make(chan struct{})

if adt.isWorking {
close(doneChan)
adt.logger.Error("failed to subscribe to events", "error", "auto deposit tracker is already running")
return doneChan
}

currentWindow, err := adt.btContract.GetCurrentWindow()
if err != nil {
close(doneChan)
adt.logger.Error("failed to get current window", "error", err)
return doneChan
}
// adding +2 as oracle runs two windows behind
currentWindow = new(big.Int).Add(currentWindow, big.NewInt(2))

eg, egCtx := errgroup.WithContext(context.Background())
egCtx, cancel := context.WithCancel(egCtx)
adt.cancelFunc = cancel

sub, err := adt.initSub(egCtx)

if err != nil {
close(doneChan)
adt.logger.Error("error subscribing to event", "error", err)
return doneChan
}

err = adt.doInitialDeposit(ctx, currentWindow, adt.initialAmount)
if err != nil {
close(doneChan)
adt.logger.Error("failed to do initial deposit", "error", err)
return doneChan
}

adt.startAutodeposit(egCtx, eg, adt.initialAmount, sub)

go func() {
defer close(doneChan)
if err := eg.Wait(); err != nil {
adt.logger.Error("error in errgroup", "error", err)
}
adt.startMu.Lock()
adt.isWorking = false
adt.startMu.Unlock()
}()

return doneChan
}

func (adt *AutoDepositTracker) StartFromApi(
func (adt *AutoDepositTracker) Start(
ctx context.Context,
startWindow, amount *big.Int,
) error {
Expand All @@ -125,6 +70,18 @@ func (adt *AutoDepositTracker) StartFromApi(
return fmt.Errorf("auto deposit tracker is already running")
}

if startWindow == nil {
var err error
startWindow, err = adt.btContract.GetCurrentWindow()
if err != nil {
adt.logger.Error("failed to get current window", "error", err)
return err
}
// adding +2 as oracle runs two windows behind
startWindow = new(big.Int).Add(startWindow, big.NewInt(2))

}

eg, egCtx := errgroup.WithContext(context.Background())
egCtx, cancel := context.WithCancel(egCtx)
adt.cancelFunc = cancel
Expand Down
6 changes: 3 additions & 3 deletions p2p/pkg/autodepositor/autodepositor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ func TestAutoDepositTracker_Start(t *testing.T) {
// Start AutoDepositTracker
ctx := context.Background()
startWindow := big.NewInt(2)
err = adt.StartFromApi(ctx, startWindow, amount)
err = adt.Start(ctx, startWindow, amount)
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -201,7 +201,7 @@ func TestAutoDepositTracker_Start_CancelContext(t *testing.T) {
startWindow := big.NewInt(1)
amount := big.NewInt(100)
cancel()
err = adt.StartFromApi(ctx, startWindow, amount)
err = adt.Start(ctx, startWindow, amount)
if err != context.Canceled {
t.Fatalf("expected context canceled error, got %v", err)
}
Expand Down Expand Up @@ -282,7 +282,7 @@ func TestAutoDepositTracker_IsWorking(t *testing.T) {
ctx := context.Background()
startWindow := big.NewInt(1)
amount := big.NewInt(100)
err = adt.StartFromApi(ctx, startWindow, amount)
err = adt.Start(ctx, startWindow, amount)
if err != nil {
t.Fatal(err)
}
Expand Down
11 changes: 8 additions & 3 deletions p2p/pkg/node/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,9 @@ type Options struct {
}

type Node struct {
cancelFunc context.CancelFunc
closers []io.Closer
cancelFunc context.CancelFunc
closers []io.Closer
autoDeposit *autodepositor.AutoDepositTracker
}

func NewNode(opts *Options) (*Node, error) {
Expand Down Expand Up @@ -448,7 +449,8 @@ func NewNode(opts *Options) (*Node, error) {
)

if opts.AutodepositAmount != nil {
startables = append(startables, autoDeposit)
autoDeposit.Start(context.Background(), nil, opts.AutodepositAmount)
nd.autoDeposit = autoDeposit
}

bidderAPI := bidderapi.NewService(
Expand Down Expand Up @@ -651,6 +653,9 @@ func (n *Node) Close() error {
for _, c := range n.closers {
err = errors.Join(err, c.Close())
}

_, adErr := n.autoDeposit.Stop()
errors.Join(err, adErr)

return err
}
Expand Down
4 changes: 2 additions & 2 deletions p2p/pkg/rpc/bidder/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func NewService(
}

type AutoDepositTracker interface {
StartFromApi(context.Context, *big.Int, *big.Int) error
Start(context.Context, *big.Int, *big.Int) error
Stop() ([]*big.Int, error)
IsWorking() bool
GetStatus() (map[uint64]bool, bool)
Expand Down Expand Up @@ -348,7 +348,7 @@ func (s *Service) AutoDeposit(
return nil, status.Errorf(codes.InvalidArgument, "parsing amount: %v", r.Amount)
}

err = s.autoDepositTracker.StartFromApi(ctx, windowToDeposit, amount)
err = s.autoDepositTracker.Start(ctx, windowToDeposit, amount)
if err != nil {
return nil, status.Errorf(codes.Internal, "starting auto deposit: %v", err)
}
Expand Down
2 changes: 1 addition & 1 deletion p2p/pkg/rpc/bidder/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ type testAutoDepositTracker struct {
isWorking bool
}

func (t *testAutoDepositTracker) StartFromApi(ctx context.Context, startWindow, amount *big.Int) error {
func (t *testAutoDepositTracker) Start(ctx context.Context, startWindow, amount *big.Int) error {
t.mtx.Lock()
defer t.mtx.Unlock()

Expand Down

0 comments on commit a34ba77

Please sign in to comment.