forked from bnb-chain/tss-lib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
party.go
183 lines (163 loc) · 5.3 KB
/
party.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
// Copyright © 2019 Binance
//
// This file is part of Binance. The full Binance copyright notice, including
// terms governing use, modification, and redistribution, is contained in the
// file LICENSE at the root of the source code distribution tree.
package tss
import (
"errors"
"fmt"
"sync"
"github.com/Aasifj2/tss-lib/common"
)
type Party interface {
Start() *Error
// The main entry point when updating a party's state from the wire.
// isBroadcast should represent whether the message was received via a reliable broadcast
UpdateFromBytes(wireBytes []byte, from *PartyID, isBroadcast bool) (ok bool, err *Error)
// You may use this entry point to update a party's state when running locally or in tests
Update(msg ParsedMessage) (ok bool, err *Error)
Running() bool
WaitingFor() []*PartyID
ValidateMessage(msg ParsedMessage) (bool, *Error)
StoreMessage(msg ParsedMessage) (bool, *Error)
FirstRound() Round
WrapError(err error, culprits ...*PartyID) *Error
PartyID() *PartyID
String() string
// Private lifecycle methods
setRound(Round) *Error
round() Round
advance()
lock()
unlock()
}
type BaseParty struct {
mtx sync.Mutex
rnd Round
FirstRound Round
}
func (p *BaseParty) Running() bool {
return p.rnd != nil
}
func (p *BaseParty) WaitingFor() []*PartyID {
p.lock()
defer p.unlock()
if p.rnd == nil {
return []*PartyID{}
}
return p.rnd.WaitingFor()
}
func (p *BaseParty) WrapError(err error, culprits ...*PartyID) *Error {
if p.rnd == nil {
return NewError(err, "", -1, nil, culprits...)
}
return p.rnd.WrapError(err, culprits...)
}
// an implementation of ValidateMessage that is shared across the different types of parties (keygen, signing, dynamic groups)
func (p *BaseParty) ValidateMessage(msg ParsedMessage) (bool, *Error) {
if msg == nil || msg.Content() == nil {
return false, p.WrapError(fmt.Errorf("received nil msg: %s", msg))
}
if msg.GetFrom() == nil || !msg.GetFrom().ValidateBasic() {
return false, p.WrapError(fmt.Errorf("received msg with an invalid sender: %s", msg))
}
if !msg.ValidateBasic() {
return false, p.WrapError(fmt.Errorf("message failed ValidateBasic: %s", msg), msg.GetFrom())
}
return true, nil
}
func (p *BaseParty) String() string {
return fmt.Sprintf("round: %d", p.round().RoundNumber())
}
// -----
// Private lifecycle methods
func (p *BaseParty) setRound(round Round) *Error {
if p.rnd != nil {
return p.WrapError(errors.New("a round is already set on this party"))
}
p.rnd = round
return nil
}
func (p *BaseParty) round() Round {
return p.rnd
}
func (p *BaseParty) advance() {
p.rnd = p.rnd.NextRound()
}
func (p *BaseParty) lock() {
p.mtx.Lock()
}
func (p *BaseParty) unlock() {
p.mtx.Unlock()
}
// ----- //
func BaseStart(p Party, task string, prepare ...func(Round) *Error) *Error {
p.lock()
defer p.unlock()
if p.PartyID() == nil || !p.PartyID().ValidateBasic() {
return p.WrapError(fmt.Errorf("could not start. this party has an invalid PartyID: %+v", p.PartyID()))
}
if p.round() != nil {
return p.WrapError(errors.New("could not start. this party is in an unexpected state. use the constructor and Start()"))
}
round := p.FirstRound()
if err := p.setRound(round); err != nil {
return err
}
if 1 < len(prepare) {
return p.WrapError(errors.New("too many prepare functions given to Start(); 1 allowed"))
}
if len(prepare) == 1 {
if err := prepare[0](round); err != nil {
return err
}
}
common.Logger.Infof("party %s: %s round %d starting", p.round().Params().PartyID(), task, 1)
defer func() {
common.Logger.Debugf("party %s: %s round %d finished", p.round().Params().PartyID(), task, 1)
}()
return p.round().Start()
}
// an implementation of Update that is shared across the different types of parties (keygen, signing, dynamic groups)
func BaseUpdate(p Party, msg ParsedMessage, task string) (ok bool, err *Error) {
// fast-fail on an invalid message; do not lock the mutex yet
if _, err := p.ValidateMessage(msg); err != nil {
return false, err
}
// lock the mutex. need this mtx unlock hook; L108 is recursive so cannot use defer
r := func(ok bool, err *Error) (bool, *Error) {
p.unlock()
return ok, err
}
p.lock() // data is written to P state below
common.Logger.Debugf("party %s received message: %s", p.PartyID(), msg.String())
if p.round() != nil {
common.Logger.Debugf("party %s round %d update: %s", p.PartyID(), p.round().RoundNumber(), msg.String())
}
if ok, err := p.StoreMessage(msg); err != nil || !ok {
return r(false, err)
}
if p.round() != nil {
common.Logger.Debugf("party %s: %s round %d update", p.round().Params().PartyID(), task, p.round().RoundNumber())
if _, err := p.round().Update(); err != nil {
return r(false, err)
}
if p.round().CanProceed() {
if p.advance(); p.round() != nil {
if err := p.round().Start(); err != nil {
return r(false, err)
}
rndNum := p.round().RoundNumber()
common.Logger.Infof("party %s: %s round %d started", p.round().Params().PartyID(), task, rndNum)
} else {
// finished! the round implementation will have sent the data through the `end` channel.
common.Logger.Infof("party %s: %s finished!", p.PartyID(), task)
}
p.unlock() // recursive so can't defer after return
return BaseUpdate(p, msg, task) // re-run round update or finish)
}
return r(true, nil)
}
return r(true, nil)
}