Skip to content

Commit

Permalink
fix: use provider route lock instead of global lock
Browse files Browse the repository at this point in the history
  • Loading branch information
debendraoli committed Jun 11, 2024
1 parent 9b07548 commit 3755b35
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 15 deletions.
5 changes: 2 additions & 3 deletions relayer/chains/evm/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,6 @@ var (

// Xcall contract
MethodExecuteCall = "executeCall"

// Lock for opts
globalRouteLock = &sync.Mutex{}
)

type Config struct {
Expand All @@ -64,6 +61,7 @@ type Provider struct {
contracts map[string]providerTypes.EventMap
NonceTracker types.NonceTrackerI
LastSavedHeightFunc func() uint64
routerMutex *sync.Mutex
}

func (p *Config) NewProvider(ctx context.Context, log *zap.Logger, homepath string, debug bool, chainName string) (provider.ChainProvider, error) {
Expand Down Expand Up @@ -97,6 +95,7 @@ func (p *Config) NewProvider(ctx context.Context, log *zap.Logger, homepath stri
blockReq: p.GetMonitorEventFilters(),
contracts: p.eventMap(),
NonceTracker: types.NewNonceTracker(client.PendingNonceAt),
routerMutex: new(sync.Mutex),
}, nil
}

Expand Down
6 changes: 3 additions & 3 deletions relayer/chains/evm/route.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,20 +24,20 @@ const (
// this will be executed in go route
func (p *Provider) Route(ctx context.Context, message *providerTypes.Message, callback providerTypes.TxResponseFunc) error {
// lock here to prevent transcation replacement
globalRouteLock.Lock()
p.routerMutex.Lock()

p.log.Info("starting to route message", zap.Any("message", message))

opts, err := p.GetTransationOpts(ctx)
if err != nil {
globalRouteLock.Unlock()
p.routerMutex.Unlock()
return fmt.Errorf("routing failed: %w", err)
}

messageKey := message.MessageKey()

tx, err := p.SendTransaction(ctx, opts, message)
globalRouteLock.Unlock()
p.routerMutex.Unlock()
if err != nil {
return fmt.Errorf("routing failed: %w", err)
}
Expand Down
12 changes: 7 additions & 5 deletions relayer/chains/wasm/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"os"
"path/filepath"
"strings"
"sync"
"time"

"github.com/cometbft/cometbft/rpc/client/http"
Expand Down Expand Up @@ -66,11 +67,12 @@ func (pc *Config) NewProvider(ctx context.Context, log *zap.Logger, homePath str
ws := newClient(clientContext)

return &Provider{
logger: log.With(zap.Stringp("nid", &pc.NID), zap.Stringp("name", &pc.ChainName)),
cfg: pc,
client: ws,
contracts: contracts,
eventList: pc.GetMonitorEventFilters(contracts),
logger: log.With(zap.Stringp("nid", &pc.NID), zap.Stringp("name", &pc.ChainName)),
cfg: pc,
client: ws,
contracts: contracts,
eventList: pc.GetMonitorEventFilters(contracts),
routerMutex: new(sync.Mutex),
}, nil
}

Expand Down
7 changes: 3 additions & 4 deletions relayer/chains/wasm/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@ import (

var _ provider.ChainProvider = (*Provider)(nil)

var globalRouteLock = &sync.Mutex{}

type Provider struct {
logger *zap.Logger
cfg *Config
Expand All @@ -35,6 +33,7 @@ type Provider struct {
contracts map[string]relayTypes.EventMap
eventList []sdkTypes.Event
LastSavedHeightFunc func() uint64
routerMutex *sync.Mutex
}

func (p *Provider) QueryLatestHeight(ctx context.Context) (uint64, error) {
Expand Down Expand Up @@ -206,8 +205,8 @@ func (p *Provider) call(ctx context.Context, message *relayTypes.Message) (*sdkT
}

func (p *Provider) sendMessage(ctx context.Context, msgs ...sdkTypes.Msg) (*sdkTypes.TxResponse, error) {
globalRouteLock.Lock()
defer globalRouteLock.Unlock()
p.routerMutex.Lock()
defer p.routerMutex.Unlock()
return p.prepareAndPushTxToMemPool(ctx, p.wallet.GetAccountNumber(), p.wallet.GetSequence(), msgs...)
}

Expand Down

0 comments on commit 3755b35

Please sign in to comment.