Skip to content

Commit

Permalink
Fix context passing across plugin and bridge interfaces
Browse files Browse the repository at this point in the history
  • Loading branch information
ogtownsend committed Jul 2, 2024
1 parent a6245df commit 00689c9
Show file tree
Hide file tree
Showing 9 changed files with 31 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ type l1ToL2Bridge struct {
}

func NewL1ToL2Bridge(
ctx context.Context,
lggr logger.Logger,
localSelector,
remoteSelector models.NetworkSelector,
Expand Down Expand Up @@ -95,8 +96,6 @@ func NewL1ToL2Bridge(
remoteChain.Name,
"",
)
// FIXME Makram please pass the valid context
ctx := context.Background()
err = l1LogPoller.RegisterFilter(ctx, logpoller.Filter{
Addresses: []common.Address{l1LiquidityManagerAddress},
Name: l1FilterName,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ type l2ToL1Bridge struct {
}

func NewL2ToL1Bridge(
ctx context.Context,
lggr logger.Logger,
localSelector,
remoteSelector models.NetworkSelector,
Expand Down Expand Up @@ -81,8 +82,6 @@ func NewL2ToL1Bridge(
remoteChain.Name,
"",
)
// FIXME Makram fix the context plax
ctx := context.Background()
err := l2LogPoller.RegisterFilter(
ctx,
logpoller.Filter{
Expand Down
13 changes: 9 additions & 4 deletions core/services/ocr2/plugins/liquiditymanager/bridge/bridge.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ type Bridge interface {

//go:generate mockery --name Factory --output ./mocks --filename bridge_factory_mock.go --case=underscore
type Factory interface {
NewBridge(source, dest models.NetworkSelector) (Bridge, error)
NewBridge(ctx context.Context, source, dest models.NetworkSelector) (Bridge, error)
}

type Opt func(c *factory)
Expand Down Expand Up @@ -106,20 +106,20 @@ func WithEvmDep(
}
}

func (f *factory) NewBridge(source, dest models.NetworkSelector) (Bridge, error) {
func (f *factory) NewBridge(ctx context.Context, source, dest models.NetworkSelector) (Bridge, error) {
if source == dest {
return nil, fmt.Errorf("no bridge between the same network and itself: %d", source)
}

bridge, err := f.GetBridge(source, dest)
if errors.Is(err, ErrBridgeNotFound) {
f.lggr.Infow("Bridge not found, initializing new bridge", "source", source, "dest", dest)
return f.initBridge(source, dest)
return f.initBridge(ctx, source, dest)
}
return bridge, err
}

func (f *factory) initBridge(source, dest models.NetworkSelector) (Bridge, error) {
func (f *factory) initBridge(ctx context.Context, source, dest models.NetworkSelector) (Bridge, error) {
f.lggr.Debugw("Initializing bridge", "source", source, "dest", dest)

var bridge Bridge
Expand Down Expand Up @@ -155,6 +155,7 @@ func (f *factory) initBridge(source, dest models.NetworkSelector) (Bridge, error
"l2BridgeAdapter", l2BridgeAdapter,
)
bridge, err = arb.NewL2ToL1Bridge(
ctx,
f.lggr,
source,
dest,
Expand Down Expand Up @@ -198,6 +199,7 @@ func (f *factory) initBridge(source, dest models.NetworkSelector) (Bridge, error
"l2BridgeAdapter", l2BridgeAdapter,
)
bridge, err = opstack.NewL2ToL1Bridge(
ctx,
f.lggr,
source,
dest,
Expand Down Expand Up @@ -245,6 +247,7 @@ func (f *factory) initBridge(source, dest models.NetworkSelector) (Bridge, error
"l1BridgeAdapter", l1BridgeAdapter,
)
bridge, err = arb.NewL1ToL2Bridge(
ctx,
f.lggr,
source,
dest,
Expand All @@ -267,6 +270,7 @@ func (f *factory) initBridge(source, dest models.NetworkSelector) (Bridge, error
"l1BridgeAdapter", l1BridgeAdapter,
)
bridge, err = opstack.NewL1ToL2Bridge(
ctx,
f.lggr,
source,
dest,
Expand Down Expand Up @@ -310,6 +314,7 @@ func (f *factory) initBridge(source, dest models.NetworkSelector) (Bridge, error
return nil, fmt.Errorf("bridge adapter not found for dest selector %d in deps for selector %d", dest, source)
}
bridge, err = testonlybridge.New(
ctx,
source,
dest,
sourceDeps.liquidityManagerAddress,
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ type l1ToL2Bridge struct {
}

func NewL1ToL2Bridge(
ctx context.Context,
lggr logger.Logger,
localSelector,
remoteSelector models.NetworkSelector,
Expand Down Expand Up @@ -77,8 +78,6 @@ func NewL1ToL2Bridge(
"",
)

// TODO: FIXME pass valid context
ctx := context.Background()
err := l1LogPoller.RegisterFilter(ctx, logpoller.Filter{
Addresses: []common.Address{l1LiquidityManagerAddress}, // emits LiquidityTransferred
Name: l1FilterName,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ type l2ToL1Bridge struct {
}

func NewL2ToL1Bridge(
ctx context.Context,
lggr logger.Logger,
localSelector,
remoteSelector models.NetworkSelector,
Expand Down Expand Up @@ -68,8 +69,6 @@ func NewL2ToL1Bridge(
remoteChain.Name,
"",
)
// TODO (ogtownsend): pass context from above
ctx := context.Background()
err := l2LogPoller.RegisterFilter(
ctx,
logpoller.Filter{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,13 @@ type testBridge struct {
}

func New(
ctx context.Context,
sourceSelector, destSelector models.NetworkSelector,
sourceLiquidityManagerAddress, destLiquidityManagerAddress, sourceAdapter, destAdapter models.Address,
sourceLogPoller, destLogPoller logpoller.LogPoller,
sourceClient, destClient client.Client,
lggr logger.Logger,
) (*testBridge, error) {
ctx := context.Background()
err := sourceLogPoller.RegisterFilter(
ctx,
logpoller.Filter{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -728,7 +728,7 @@ func TestNew(t *testing.T) {
t.Run(tt.name, func(t *testing.T) {
tt.expect(t, tt.args)
defer tt.assert(t, tt.args)
got, err := New(tt.args.sourceSelector, tt.args.destSelector, tt.args.sourceLiquidityManagerAddress, tt.args.destLiquidityManagerAddress, tt.args.sourceAdapter, tt.args.destAdapter, tt.args.sourceLogPoller, tt.args.destLogPoller, tt.args.sourceClient, tt.args.destClient, tt.args.lggr)
got, err := New(testutils.Context(t), tt.args.sourceSelector, tt.args.destSelector, tt.args.sourceLiquidityManagerAddress, tt.args.destLiquidityManagerAddress, tt.args.sourceAdapter, tt.args.destAdapter, tt.args.sourceLogPoller, tt.args.destLogPoller, tt.args.sourceClient, tt.args.destClient, tt.args.lggr)
if tt.wantErr {
require.Error(t, err)
} else {
Expand Down
7 changes: 4 additions & 3 deletions core/services/ocr2/plugins/liquiditymanager/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -507,7 +507,7 @@ func (p *Plugin) loadPendingTransfers(ctx context.Context, lggr logger.Logger) (
}
for _, edge := range edges {
logger := lggr.With("sourceNetwork", edge.Source, "sourceChainID", edge.Source.ChainID(), "destNetwork", edge.Dest, "destChainID", edge.Dest.ChainID())
bridge, err := p.bridgeFactory.NewBridge(edge.Source, edge.Dest)
bridge, err := p.bridgeFactory.NewBridge(ctx, edge.Source, edge.Dest)
if err != nil {
return nil, fmt.Errorf("init bridge: %w", err)
}
Expand Down Expand Up @@ -607,7 +607,8 @@ func (p *Plugin) computeResolvedTransfersQuorum(observations []models.Observatio
}
medianizedNativeFee := rebalcalc.BigIntSortedMiddle(bridgeFees)
medianizedDateUnix := rebalcalc.BigIntSortedMiddle(datesUnix)
bridge, err := p.bridgeFactory.NewBridge(k.From, k.To)
// TODO (ogtownsend): should we set an explicit timeout on this context?
bridge, err := p.bridgeFactory.NewBridge(context.Background(), k.From, k.To)
if err != nil {
return nil, fmt.Errorf("init bridge: %w", err)
}
Expand Down Expand Up @@ -650,7 +651,7 @@ func (p *Plugin) resolveProposedTransfers(ctx context.Context, lggr logger.Logge

resolvedTransfers := make([]models.Transfer, 0, len(outcome.ProposedTransfers))
for _, proposedTransfer := range outcome.ProposedTransfers {
bridge, err := p.bridgeFactory.NewBridge(proposedTransfer.From, proposedTransfer.To)
bridge, err := p.bridgeFactory.NewBridge(ctx, proposedTransfer.From, proposedTransfer.To)
if err != nil {
return nil, fmt.Errorf("init bridge: %w", err)
}
Expand Down

0 comments on commit 00689c9

Please sign in to comment.