Skip to content

Commit

Permalink
refactor: Use cleanup manager to unsubscribe web3 subscriptions (#432)
Browse files Browse the repository at this point in the history
* chore: Add unsubscribe helper

* refactor: Use cleanup manager to unsubscribe
  • Loading branch information
bgins authored Nov 18, 2024
1 parent 52de132 commit 53b17c5
Show file tree
Hide file tree
Showing 7 changed files with 15 additions and 35 deletions.
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
}
}

0 comments on commit 53b17c5

Please sign in to comment.