Skip to content

Commit

Permalink
Pass context to the coordination executor
Browse files Browse the repository at this point in the history
The executor needs to be aware of the root context and terminate execution
if that context is done.
  • Loading branch information
lukasz-zimnoch committed Nov 24, 2023
1 parent bfc27d2 commit cba7d03
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 16 deletions.
13 changes: 7 additions & 6 deletions pkg/tbtc/coordination.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ func watchCoordinationWindows(
for {
select {
case block := <-blocksChan:
if block % coordinationFrequencyBlocks == 0 {
if block%coordinationFrequencyBlocks == 0 {
// Make sure the current window is not the same as the last one.
// There is no guarantee that the block channel will not emit
// the same block again.
Expand Down Expand Up @@ -112,11 +112,11 @@ func newCoordinationExecutor(
protocolLatch *generator.ProtocolLatch,
) *coordinationExecutor {
return &coordinationExecutor{
lock: semaphore.NewWeighted(1),
signers: signers,
broadcastChannel: broadcastChannel,
lock: semaphore.NewWeighted(1),
signers: signers,
broadcastChannel: broadcastChannel,
membershipValidator: membershipValidator,
protocolLatch: protocolLatch,
protocolLatch: protocolLatch,
}
}

Expand All @@ -130,6 +130,7 @@ func (ce *coordinationExecutor) wallet() wallet {
// coordinate executes the coordination procedure for the given coordination
// window.
func (ce *coordinationExecutor) coordinate(
ctx context.Context,
window *coordinationWindow,
) (*coordinationResult, error) {
if lockAcquired := ce.lock.TryAcquire(1); !lockAcquired {
Expand All @@ -141,4 +142,4 @@ func (ce *coordinationExecutor) coordinate(
// context.

return nil, nil
}
}
23 changes: 13 additions & 10 deletions pkg/tbtc/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,14 +103,14 @@ func newNode(
scheduler.RegisterProtocol(latch)

node := &node{
groupParameters: groupParameters,
chain: chain,
btcChain: btcChain,
netProvider: netProvider,
walletRegistry: walletRegistry,
walletDispatcher: newWalletDispatcher(),
protocolLatch: latch,
signingExecutors: make(map[string]*signingExecutor),
groupParameters: groupParameters,
chain: chain,
btcChain: btcChain,
netProvider: netProvider,
walletRegistry: walletRegistry,
walletDispatcher: newWalletDispatcher(),
protocolLatch: latch,
signingExecutors: make(map[string]*signingExecutor),
coordinationExecutors: make(map[string]*coordinationExecutor),
}

Expand Down Expand Up @@ -648,12 +648,13 @@ type coordinationLayerSettings struct {
// executeCoordinationProcedureFn is a function executing the coordination
// procedure for the given wallet and coordination window.
executeCoordinationProcedureFn func(
ctx context.Context,
node *node,
window *coordinationWindow,
walletPublicKey *ecdsa.PublicKey,
) (*coordinationResult, bool)

// processCoordinationResultFn is a function processing the given
// processCoordinationResultFn is a function processing the given
// coordination result.
processCoordinationResultFn func(
node *node,
Expand Down Expand Up @@ -702,6 +703,7 @@ func (n *node) runCoordinationLayer(
// to the coordination result channel.
go func(walletPublicKey *ecdsa.PublicKey) {
result, ok := cls.executeCoordinationProcedureFn(
ctx,
n,
window,
walletPublicKey,
Expand Down Expand Up @@ -738,6 +740,7 @@ func (n *node) runCoordinationLayer(
// executeCoordinationProcedure executes the coordination procedure for the
// given wallet and coordination window.
func executeCoordinationProcedure(
ctx context.Context,
node *node,
window *coordinationWindow,
walletPublicKey *ecdsa.PublicKey,
Expand Down Expand Up @@ -769,7 +772,7 @@ func executeCoordinationProcedure(
return nil, false
}

result, err := executor.coordinate(window)
result, err := executor.coordinate(ctx, window)
if err != nil {
procedureLogger.Errorf("coordination procedure failed: [%v]", err)
return nil, false
Expand Down
1 change: 1 addition & 0 deletions pkg/tbtc/node_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,7 @@ func TestNode_RunCoordinationLayer(t *testing.T) {
// Mock the coordination procedure execution. Return predefined results
// on specific coordination windows.
executeCoordinationProcedureFn := func(
_ context.Context,
_ *node,
window *coordinationWindow,
walletPublicKey *ecdsa.PublicKey,
Expand Down

0 comments on commit cba7d03

Please sign in to comment.