forked from taurusgroup/multi-party-sig
/
round.go
156 lines (140 loc) · 3.52 KB
/
round.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
package test
import (
"errors"
"fmt"
"reflect"
"github.com/fxamacker/cbor/v2"
"github.com/koteld/multi-party-sig/internal/round"
"github.com/koteld/multi-party-sig/pkg/party"
"golang.org/x/sync/errgroup"
)
// Rule describes various hooks that can be applied to a protocol execution.
type Rule interface {
// ModifyBefore modifies r before r.Finalize() is called.
ModifyBefore(r round.Session)
// ModifyAfter modifies rNext, which is the round returned by r.Finalize().
ModifyAfter(rNext round.Session)
// ModifyContent modifies content for the message that is delivered in rNext.
ModifyContent(rNext round.Session, to party.ID, content round.Content)
}
func Rounds(rounds []round.Session, rule Rule) (error, bool) {
var (
err error
roundType reflect.Type
errGroup errgroup.Group
N = len(rounds)
out = make(chan *round.Message, N*(N+1))
)
if roundType, err = checkAllRoundsSame(rounds); err != nil {
return err, false
}
// get the second set of messages
for id := range rounds {
idx := id
r := rounds[idx]
errGroup.Go(func() error {
var rNew, rNewReal round.Session
if rule != nil {
rReal := getRound(r)
rule.ModifyBefore(rReal)
outFake := make(chan *round.Message, N+1)
rNew, err = r.Finalize(outFake)
close(outFake)
rNewReal = getRound(rNew)
rule.ModifyAfter(rNewReal)
for msg := range outFake {
rule.ModifyContent(rNewReal, msg.To, getContent(msg.Content))
out <- msg
}
} else {
rNew, err = r.Finalize(out)
}
if err != nil {
return err
}
if rNew != nil {
rounds[idx] = rNew
}
return nil
})
}
if err = errGroup.Wait(); err != nil {
return err, false
}
close(out)
// Check that all rounds are the same type
if roundType, err = checkAllRoundsSame(rounds); err != nil {
return err, false
}
if roundType.String() == reflect.TypeOf(&round.Output{}).String() {
return nil, true
}
if roundType.String() == reflect.TypeOf(&round.Abort{}).String() {
return nil, true
}
for msg := range out {
msgBytes, err := cbor.Marshal(msg.Content)
if err != nil {
return err, false
}
for _, r := range rounds {
m := *msg
r := r
if msg.From == r.SelfID() || msg.Content.RoundNumber() != r.Number() {
continue
}
errGroup.Go(func() error {
if m.Broadcast {
b, ok := r.(round.BroadcastRound)
if !ok {
return errors.New("broadcast message but not broadcast round")
}
m.Content = b.BroadcastContent()
if err = cbor.Unmarshal(msgBytes, m.Content); err != nil {
return err
}
if err = b.StoreBroadcastMessage(m); err != nil {
return err
}
} else {
m.Content = r.MessageContent()
if err = cbor.Unmarshal(msgBytes, m.Content); err != nil {
return err
}
if m.To == "" || m.To == r.SelfID() {
if err = r.VerifyMessage(m); err != nil {
return err
}
if err = r.StoreMessage(m); err != nil {
return err
}
}
}
return nil
})
}
if err = errGroup.Wait(); err != nil {
return err, false
}
}
return nil, false
}
func checkAllRoundsSame(rounds []round.Session) (reflect.Type, error) {
var t reflect.Type
for _, r := range rounds {
rReal := getRound(r)
t2 := reflect.TypeOf(rReal)
if t == nil {
t = t2
} else if t != t2 {
return t, fmt.Errorf("two different rounds: %s %s", t, t2)
}
}
return t, nil
}
func getRound(outerRound round.Session) round.Session {
return outerRound
}
func getContent(outerContent round.Content) round.Content {
return outerContent
}