Skip to content

Commit 501f20a

Browse files
authored
fix: prevent started process starting again (#160)
<!--- Provide a general summary of your changes in the Title above --> ## Description <!--- Describe your changes in detail --> ## Related Issue Or Context <!--- If suggesting a new feature or change, please discuss it in an issue first --> <!--- If fixing a bug, there should be an issue describing it with steps to reproduce --> <!--- Otherwise, describe context and motivation for change herre --> Closes: #<issue> ## How Has This Been Tested? Testing details. <!--- Please describe in detail how you tested your changes. --> <!--- Include details of your testing environment, and the tests you ran to --> <!--- see how your change affects other areas of the code, etc. --> ## Types of changes <!--- What types of changes does your code introduce? Put an `x` in all the boxes that apply: --> - [ ] Bug fix (non-breaking change which fixes an issue) - [ ] New feature (non-breaking change which adds functionality) - [ ] Breaking change (fix or feature that would cause existing functionality to change) - [ ] Documentation ## Checklist: <!--- Go over all the following points, and put an `x` in all the boxes that apply. --> <!--- If you're unsure about any of these, don't hesitate to ask. We're here to help! --> - [ ] I have commented my code, particularly in hard-to-understand areas. - [ ] I have ensured that all acceptance criteria (or expected behavior) from issue are met - [ ] I have updated the documentation locally and in docs. - [ ] I have added tests to cover my changes. - [ ] I have ensured that all the checks are passing and green, I've signed the CLA bot
1 parent a6203a2 commit 501f20a

5 files changed

Lines changed: 63 additions & 15 deletions

File tree

tss/coordinator.go

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import (
1616
"github.com/sourcegraph/conc/pool"
1717
"github.com/sprintertech/sprinter-signing/comm"
1818
"github.com/sprintertech/sprinter-signing/comm/elector"
19+
"github.com/sprintertech/sprinter-signing/tss/ecdsa/common"
1920
"github.com/sprintertech/sprinter-signing/tss/message"
2021
"golang.org/x/exp/slices"
2122
)
@@ -236,13 +237,7 @@ func (c *Coordinator) initiate(
236237

237238
_ = c.communication.Broadcast(c.host.Peerstore().Peers(), startMsgBytes, comm.TssStartMsg, tssProcess.SessionID())
238239
ticker.Stop()
239-
go func() {
240-
err := tssProcess.Run(ctx, true, resultChn, startParams)
241-
select {
242-
case errChn <- err:
243-
default:
244-
}
245-
}()
240+
go c.startProcess(ctx, tssProcess, true, startParams, resultChn, errChn)
246241
}
247242
case <-ticker.C:
248243
{
@@ -305,13 +300,7 @@ func (c *Coordinator) waitForStart(
305300
return err
306301
}
307302

308-
go func() {
309-
err := tssProcess.Run(ctx, false, resultChn, msg.Params)
310-
select {
311-
case errChn <- err:
312-
default:
313-
}
314-
}()
303+
go c.startProcess(ctx, tssProcess, false, msg.Params, resultChn, errChn)
315304
}
316305
case <-ctx.Done():
317306
{
@@ -320,3 +309,21 @@ func (c *Coordinator) waitForStart(
320309
}
321310
}
322311
}
312+
313+
func (c *Coordinator) startProcess(
314+
ctx context.Context,
315+
tssProcess TssProcess,
316+
coordinator bool,
317+
startParams []byte,
318+
resultChn chan interface{},
319+
errChn chan error) {
320+
err := tssProcess.Run(ctx, coordinator, resultChn, startParams)
321+
if errors.Is(err, common.ErrProcessStarted) {
322+
return
323+
}
324+
325+
select {
326+
case errChn <- err:
327+
default:
328+
}
329+
}

tss/ecdsa/common/base.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,11 @@ package common
55

66
import (
77
"context"
8+
"errors"
89
"fmt"
910
"math/big"
1011
"runtime/debug"
12+
"sync"
1113
"time"
1214

1315
"github.com/binance-chain/tss-lib/tss"
@@ -24,13 +26,17 @@ type Party interface {
2426
WaitingFor() []*tss.PartyID
2527
}
2628

29+
var ErrProcessStarted = errors.New("process already started")
30+
2731
// BaseTss contains common variables and methods to
2832
// all tss processes.
2933
type BaseTss struct {
3034
Host host.Host
3135
SID string
3236
Party Party
3337
PartyStore map[string]*tss.PartyID
38+
Started bool
39+
Mux *sync.Mutex
3440
Communication comm.Communication
3541
Peers []peer.ID
3642
Log zerolog.Logger

tss/ecdsa/keygen/keygen.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"context"
88
"errors"
99
"math/big"
10+
"sync"
1011
"time"
1112

1213
"github.com/binance-chain/tss-lib/ecdsa/keygen"
@@ -51,6 +52,8 @@ func NewKeygen(
5152
Peers: host.Peerstore().Peers(),
5253
SID: sessionID,
5354
Log: log.With().Str("SessionID", sessionID).Str("Process", "keygen").Logger(),
55+
Started: false,
56+
Mux: &sync.Mutex{},
5457
Cancel: func() {},
5558
TssTimeout: time.Minute * 10,
5659
},
@@ -68,6 +71,14 @@ func (k *Keygen) Run(
6871
resultChn chan interface{},
6972
params []byte,
7073
) error {
74+
k.Mux.Lock()
75+
if k.Started {
76+
k.Mux.Unlock()
77+
k.Log.Warn().Msgf("Keygen already started")
78+
return common.ErrProcessStarted
79+
}
80+
k.Started = true
81+
k.Mux.Unlock()
7182
ctx, k.Cancel = context.WithCancel(ctx)
7283

7384
k.storer.LockKeyshare()

tss/ecdsa/resharing/resharing.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"encoding/json"
99
"errors"
1010
"math/big"
11+
"sync"
1112
"time"
1213

1314
"github.com/binance-chain/tss-lib/ecdsa/keygen"
@@ -68,6 +69,8 @@ func NewResharing(
6869
Peers: host.Peerstore().Peers(),
6970
SID: sessionID,
7071
Log: log.With().Str("SessionID", sessionID).Str("Process", "resharing").Logger(),
72+
Started: false,
73+
Mux: &sync.Mutex{},
7174
Cancel: func() {},
7275
TssTimeout: time.Minute * 10,
7376
},
@@ -85,6 +88,15 @@ func (r *Resharing) Run(
8588
resultChn chan interface{},
8689
params []byte,
8790
) error {
91+
r.Mux.Lock()
92+
if r.Started {
93+
r.Mux.Unlock()
94+
r.Log.Warn().Msgf("Resharing already started")
95+
return common.ErrProcessStarted
96+
}
97+
r.Started = true
98+
r.Mux.Unlock()
99+
88100
ctx, r.Cancel = context.WithCancel(ctx)
89101

90102
startParams, err := r.unmarshallStartParams(params)

tss/ecdsa/signing/signing.go

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"fmt"
1010
"math/big"
1111
"reflect"
12+
"sync"
1213
"time"
1314

1415
tssCommon "github.com/binance-chain/tss-lib/common"
@@ -72,9 +73,11 @@ func NewSigning(
7273
Communication: comm,
7374
Peers: key.Peers,
7475
SID: sessionID,
76+
Started: false,
77+
Mux: &sync.Mutex{},
7578
Log: log.With().Str("SessionID", sessionID).Str("messageID", messageID).Str("Process", "signing").Logger(),
7679
Cancel: func() {},
77-
TssTimeout: time.Second * 30,
80+
TssTimeout: time.Second * 8,
7881
},
7982
key: key,
8083
msg: msg,
@@ -89,6 +92,15 @@ func (s *Signing) Run(
8992
resultChn chan interface{},
9093
params []byte,
9194
) error {
95+
s.Mux.Lock()
96+
if s.Started {
97+
s.Mux.Unlock()
98+
s.Log.Warn().Msgf("Signing already started")
99+
return common.ErrProcessStarted
100+
}
101+
s.Started = true
102+
s.Mux.Unlock()
103+
92104
s.coordinator = coordinator
93105
s.resultChn = resultChn
94106
ctx, s.Cancel = context.WithCancel(ctx)

0 commit comments

Comments
 (0)