Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: Use cleanup manager to unsubscribe web3 subscriptions #432

Merged
merged 2 commits into from
Nov 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 1 addition & 6 deletions pkg/web3/events_jobcreator.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,7 @@ func (s *JobCreatorEventChannels) Start(
if err != nil {
return err
}

defer func() {
if jobAddedSub != nil {
jobAddedSub.Unsubscribe()
}
}()
cm.RegisterCallback(unsubscribeSub(jobAddedSub))

for {
select {
Expand Down
7 changes: 1 addition & 6 deletions pkg/web3/events_mediation.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,7 @@ func (m *MediationEventChannels) Start(
if err != nil {
return err
}

defer func() {
if mediationRequestedSub != nil {
mediationRequestedSub.Unsubscribe()
}
}()
cm.RegisterCallback(unsubscribeSub(mediationRequestedSub))

for {
select {
Expand Down
6 changes: 1 addition & 5 deletions pkg/web3/events_payments.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,8 @@ func (p *PaymentEventChannels) Start(
if err != nil {
return err
}
cm.RegisterCallback(unsubscribeSub(paymentSub))

defer func() {
if paymentSub != nil {
paymentSub.Unsubscribe()
}
}()
for {
select {
case <-ctx.Done():
Expand Down
8 changes: 2 additions & 6 deletions pkg/web3/events_pow.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package web3
import (
"context"
"fmt"

"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/event"
"github.com/lilypad-tech/lilypad/pkg/system"
Expand Down Expand Up @@ -49,12 +50,7 @@ func (s *PowEventChannels) Start(
if err != nil {
return err
}

defer func() {
if newPowRoundSub != nil {
newPowRoundSub.Unsubscribe()
}
}()
cm.RegisterCallback(unsubscribeSub(newPowRoundSub))

for {
select {
Expand Down
7 changes: 1 addition & 6 deletions pkg/web3/events_storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,7 @@ func (s *StorageEventChannels) Start(
if err != nil {
return err
}

defer func() {
if dealStateChangeSub != nil {
dealStateChangeSub.Unsubscribe()
}
}()
cm.RegisterCallback(unsubscribeSub(dealStateChangeSub))

for {
select {
Expand Down
7 changes: 1 addition & 6 deletions pkg/web3/events_token.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,7 @@ func (t *TokenEventChannels) Start(
if err != nil {
return err
}

defer func() {
if transferSub != nil {
transferSub.Unsubscribe()
}
}()
cm.RegisterCallback(unsubscribeSub(transferSub))

for {
select {
Expand Down
8 changes: 8 additions & 0 deletions pkg/web3/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/event"
)

func GetPublicKey(privateKey *ecdsa.PrivateKey) ecdsa.PublicKey {
Expand Down Expand Up @@ -68,3 +69,10 @@ func ConvertStringToInt64(st string) uint64 {
bigInt := ConvertStringToBigInt(st)
return bigInt.Uint64()
}

func unsubscribeSub(sub event.Subscription) func() error {
return func() error {
sub.Unsubscribe()
return nil
}
}
Loading