/
objects.go
217 lines (187 loc) · 6.9 KB
/
objects.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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
package monitor
import (
"encoding/json"
"fmt"
"math/big"
"strings"
"github.com/MadBase/MadNet/blockchain/dkg"
"github.com/MadBase/MadNet/blockchain/tasks"
"github.com/ethereum/go-ethereum/common"
)
// State contains info required to monitor Ethereum
type State struct {
// TODO decide if a mutex is required
Version uint8
CommunicationFailures uint32
EthereumInSync bool
HighestBlockProcessed uint64
HighestBlockFinalized uint64
HighestEpochProcessed uint32
HighestEpochSeen uint32
InSync bool
LatestDepositProcessed uint32
LatestDepositSeen uint32
PeerCount uint32
ValidatorSets map[uint32]ValidatorSet
Validators map[uint32][]Validator
ethdkg *EthDKGState
interestingBlocks map[uint64]func(*State, uint64) error
}
// EthDKGPhase is used to indicate what phase we are currently in
type EthDKGPhase int
// These are the valid phases of ETHDKG
const (
Registration EthDKGPhase = iota
ShareDistribution
Dispute
KeyShareSubmission
MPKSubmission
GPKJSubmission
GPKJGroupAccusation
)
// EthDKGSchedule RegistrationOpen event publishes phase schedule, so we record that here
type EthDKGSchedule struct {
RegistrationStart uint64
RegistrationEnd uint64
ShareDistributionStart uint64
ShareDistributionEnd uint64
DisputeStart uint64
DisputeEnd uint64
KeyShareSubmissionStart uint64
KeyShareSubmissionEnd uint64
MPKSubmissionStart uint64
MPKSubmissionEnd uint64
GPKJSubmissionStart uint64
GPKJSubmissionEnd uint64
GPKJGroupAccusationStart uint64
GPKJGroupAccusationEnd uint64
CompleteStart uint64
CompleteEnd uint64
}
// EthDKGState is used to track the state of the ETHDKG
type EthDKGState struct {
// Local validator info
Address common.Address
Index int
GroupPrivateKey *big.Int
GroupPublicKey [4]*big.Int
MasterPublicKey [4]*big.Int
NumberOfValidators int
PrivateCoefficients []*big.Int
Schedule *EthDKGSchedule
SecretValue *big.Int
ValidatorThreshold int
TransportPrivateKey *big.Int
TransportPublicKey [2]*big.Int
// Remote validator info
Commitments map[common.Address][][2]*big.Int // ShareDistribution Event
EncryptedShares map[common.Address][]*big.Int // "
KeyShareG1s map[common.Address][2]*big.Int // KeyShare Event
KeyShareG1CorrectnessProofs map[common.Address][2]*big.Int // "
KeyShareG2s map[common.Address][4]*big.Int // "
Participants dkg.ParticipantList // Index, Address & PublicKey
// Handlers
RegistrationTH tasks.TaskHandler
ShareDistributionTH tasks.TaskHandler
DisputeTH tasks.TaskHandler
KeyShareSubmissionTH tasks.TaskHandler
MPKSubmissionTH tasks.TaskHandler
GPKJSubmissionTH tasks.TaskHandler
GPKJGroupAccusationTH tasks.TaskHandler
CompleteTH tasks.TaskHandler
// Addresses map[uint8]common.Address
// GroupPublicKey [4]*big.Int
// GroupSecretKey *big.Int
// Commitments map[common.Address][][2]*big.Int
// EncryptedShares map[common.Address][]*big.Int
// Indices map[common.Address]uint8
// InitialMessage []byte
// InProgress bool
// KeyShareG1s map[common.Address][2]*big.Int
// KeyShareG1CorrectnessProofs map[common.Address][2]*big.Int
// KeyShareG2s map[common.Address][4]*big.Int
// MasterPublicKey *cloudflare.G2
// PublicKeyG1s map[common.Address]*cloudflare.G1
// PublishedGroupPublicKeys map[common.Address]*cloudflare.G2
// PublishedSignatures map[common.Address]*cloudflare.G1
// TransportPublicKeyG1 *cloudflare.G1
}
// NewEthDKGState creates a new EthDKGState with maps initialized
func NewEthDKGState() *EthDKGState {
return &EthDKGState{
Commitments: make(map[common.Address][][2]*big.Int),
EncryptedShares: make(map[common.Address][]*big.Int),
KeyShareG1s: make(map[common.Address][2]*big.Int),
KeyShareG1CorrectnessProofs: make(map[common.Address][2]*big.Int),
KeyShareG2s: make(map[common.Address][4]*big.Int),
Schedule: &EthDKGSchedule{},
}
}
// ValidatorSet is summary information about a ValidatorSet
type ValidatorSet struct {
ValidatorCount uint8
GroupKey [4]big.Int
NotBeforeMadNetHeight uint32
}
// Validator contains information about a Validator
type Validator struct {
Account common.Address
Index uint8
SharedKey [4]big.Int
}
// Share is temporary storage of shares coming from validators
type Share struct {
Issuer common.Address
Commitments [][2]*big.Int
EncryptedShares []*big.Int
}
func (s State) String() string {
str, err := json.Marshal(s)
if err != nil {
return fmt.Sprintf("%#v", s)
}
return string(str)
}
// Clone builds a deep copy of a state
func (s *State) Clone() *State {
ns := new(State)
ns.HighestBlockFinalized = s.HighestBlockFinalized
ns.HighestBlockProcessed = s.HighestBlockProcessed
ns.HighestEpochProcessed = s.HighestEpochProcessed
ns.HighestEpochSeen = s.HighestEpochSeen
ns.InSync = s.InSync
ns.EthereumInSync = s.EthereumInSync
return ns
}
// Diff builds a textual description between states
func (s *State) Diff(o *State) string {
d := []string{}
if s.InSync != o.InSync {
d = append(d, fmt.Sprintf("InSync: %v -> %v", s.InSync, o.InSync))
}
if s.HighestBlockProcessed != o.HighestBlockProcessed {
d = append(d, fmt.Sprintf("HighestBlockProcessed: %v -> %v", s.HighestBlockProcessed, o.HighestBlockProcessed))
}
if s.HighestBlockFinalized != o.HighestBlockFinalized {
d = append(d, fmt.Sprintf("HighestBlockFinalized: %v -> %v", s.HighestBlockFinalized, o.HighestBlockFinalized))
}
if s.HighestEpochProcessed != o.HighestEpochProcessed {
d = append(d, fmt.Sprintf("HighestEpochProcessed: %v -> %v", s.HighestEpochProcessed, o.HighestEpochProcessed))
}
if s.HighestEpochSeen != o.HighestEpochSeen {
d = append(d, fmt.Sprintf("HighestEpochSeen: %v -> %v", s.HighestEpochSeen, o.HighestEpochSeen))
}
if s.LatestDepositProcessed != o.LatestDepositProcessed {
d = append(d, fmt.Sprintf("LatestDepositProcessed: %v -> %v", s.LatestDepositProcessed, o.LatestDepositProcessed))
}
if s.LatestDepositSeen != o.LatestDepositSeen {
d = append(d, fmt.Sprintf("LatestDepositSeen: %v -> %v", s.LatestDepositSeen, o.LatestDepositSeen))
}
if s.EthereumInSync != o.EthereumInSync {
d = append(d, fmt.Sprintf("EthereumInSync: %v -> %v", s.EthereumInSync, o.EthereumInSync))
}
if s.CommunicationFailures != o.CommunicationFailures {
d = append(d, fmt.Sprintf("CommunicationFailures: %v -> %v", s.CommunicationFailures, o.CommunicationFailures))
}
return strings.Join(d, ", ")
}