-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsignable.go
255 lines (210 loc) · 6.94 KB
/
signable.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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
package mcms
import (
"context"
"errors"
"fmt"
"strconv"
"github.com/ethereum/go-ethereum/common"
"github.com/smartcontractkit/mcms/internal/core/merkle"
"github.com/smartcontractkit/mcms/sdk"
"github.com/smartcontractkit/mcms/types"
)
var (
ErrInspectorsNotProvided = errors.New("inspectors not provided")
ErrSimulatorsNotProvided = errors.New("simulators not provided")
)
// Signable provides signing functionality for an Proposal. It contains all the necessary
// information required to validate, sign, and check the quorum of a proposal.
// Signable contains the proposal itself, a Merkle tree representation of the proposal, encoders for
// different chains to perform the signing, while the inspectors are used for retrieving contract
// configurations and operational counts on chain.
type Signable struct {
proposal *Proposal
tree *merkle.Tree
encoders map[types.ChainSelector]sdk.Encoder
inspectors map[types.ChainSelector]sdk.Inspector
simulators map[types.ChainSelector]sdk.Simulator
}
// NewSignable creates a new Signable from a proposal and inspectors, and initializes the encoders
// and merkle tree.
func NewSignable(
proposal *Proposal,
inspectors map[types.ChainSelector]sdk.Inspector,
) (*Signable, error) {
encoders, err := proposal.GetEncoders()
if err != nil {
return nil, err
}
tree, err := proposal.MerkleTree()
if err != nil {
return nil, err
}
return &Signable{
proposal: proposal,
tree: tree,
encoders: encoders,
inspectors: inspectors,
}, nil
}
// SetSimulators allows setting the simulators map for the Signable instance.
func (s *Signable) SetSimulators(simulators map[types.ChainSelector]sdk.Simulator) {
s.simulators = simulators
}
// Sign signs the root of the proposal's Merkle tree with the provided signer.
func (s *Signable) Sign(signer signer) (sig types.Signature, err error) {
// Validate proposal
if err = s.proposal.Validate(); err != nil {
return sig, err
}
// Get the signing hash
payload, err := s.proposal.SigningMessage() // This should be signingMessage for ledger
if err != nil {
return sig, err
}
// Sign the payload
sigB, err := signer.Sign(payload.Bytes())
if err != nil {
return sig, err
}
return types.NewSignatureFromBytes(sigB)
}
// SignAndAppend signs the proposal using the provided signer and appends the resulting signature
// to the proposal's list of signatures.
//
// This function modifies the proposal in place by adding the new signature to its Signatures
// slice.
func (s *Signable) SignAndAppend(signer signer) (types.Signature, error) {
// Sign the proposal
sig, err := s.Sign(signer)
if err != nil {
return types.Signature{}, err
}
// Add the signature to the proposal
s.proposal.AppendSignature(sig)
return sig, nil
}
// Simulate simulates the proposal on the given chain using the provided simulator.
func (s *Signable) Simulate(ctx context.Context) error {
if s.simulators == nil {
return ErrSimulatorsNotProvided
}
for _, op := range s.proposal.Operations {
simulator, ok := s.simulators[op.ChainSelector]
if !ok {
return fmt.Errorf("simulator not found for chain %d", op.ChainSelector)
}
// TODO: should we fail on the first error or aggregate all simulation errors?
err := simulator.SimulateOperation(ctx, s.proposal.ChainMetadata[op.ChainSelector], op)
if err != nil {
return err
}
}
return nil
}
// GetConfigs retrieves the MCMS contract configurations for each chain in the proposal.
func (s *Signable) GetConfigs(ctx context.Context) (map[types.ChainSelector]*types.Config, error) {
if s.inspectors == nil {
return nil, ErrInspectorsNotProvided
}
configs := make(map[types.ChainSelector]*types.Config)
for chain, metadata := range s.proposal.ChainMetadata {
inspector, ok := s.inspectors[chain]
if !ok {
return nil, fmt.Errorf("inspector not found for chain %d", chain)
}
configuration, err := inspector.GetConfig(ctx, metadata.MCMAddress)
if err != nil {
return nil, err
}
configs[chain] = configuration
}
return configs, nil
}
// CheckQuorum checks if the quorum for the proposal on the given chain has been reached. This will
// fetch the current configuration for the chain and check if the recovered signers from the
// proposal's signatures can set the root.
func (s *Signable) CheckQuorum(ctx context.Context, chain types.ChainSelector) (bool, error) {
if s.inspectors == nil {
return false, ErrInspectorsNotProvided
}
inspector, ok := s.inspectors[chain]
if !ok {
return false, errors.New("inspector not found for chain " + strconv.FormatUint(uint64(chain), 10))
}
hash, err := s.proposal.SigningHash()
if err != nil {
return false, err
}
recoveredSigners := make([]common.Address, len(s.proposal.Signatures))
for i, sig := range s.proposal.Signatures {
recoveredAddr, rerr := sig.Recover(hash)
if rerr != nil {
return false, rerr
}
recoveredSigners[i] = recoveredAddr
}
configuration, err := inspector.GetConfig(ctx, s.proposal.ChainMetadata[chain].MCMAddress)
if err != nil {
return false, err
}
return configuration.CanSetRoot(recoveredSigners)
}
// ValidateSignatures checks if the quorum for the proposal has been reached on the MCM contracts
// across all chains in the proposal.
func (s *Signable) ValidateSignatures(ctx context.Context) (bool, error) {
for chain := range s.proposal.ChainMetadata {
checkQuorum, err := s.CheckQuorum(ctx, chain)
if err != nil {
return false, err
}
if !checkQuorum {
return false, NewQuorumNotReachedError(chain)
}
}
return true, nil
}
// ValidateConfigs checks the MCMS contract configurations for each chain in the proposal for
// consistency.
//
// We expect that the configurations for each chain are the same so that the same quorum can be
// reached across all chains in the proposal.
func (s *Signable) ValidateConfigs(ctx context.Context) error {
configs, err := s.GetConfigs(ctx)
if err != nil {
return err
}
for i, sel := range s.proposal.ChainSelectors() {
if i == 0 {
continue
}
if !configs[sel].Equals(configs[s.proposal.ChainSelectors()[i-1]]) {
return &InconsistentConfigsError{
ChainSelectorA: sel,
ChainSelectorB: s.proposal.ChainSelectors()[i-1],
}
}
}
return nil
}
// getCurrentOpCounts returns the current op counts for the MCM contract on each chain in the
// proposal. This data is fetched from the contract on the chain using the provided inspectors.
//
// Note: This function is currently not used but left for potential future use.
func (s *Signable) getCurrentOpCounts(ctx context.Context) (map[types.ChainSelector]uint64, error) {
if s.inspectors == nil {
return nil, ErrInspectorsNotProvided
}
opCounts := make(map[types.ChainSelector]uint64)
for sel, metadata := range s.proposal.ChainMetadata {
inspector, ok := s.inspectors[sel]
if !ok {
return nil, fmt.Errorf("inspector not found for chain %d", sel)
}
opCount, err := inspector.GetOpCount(ctx, metadata.MCMAddress)
if err != nil {
return nil, err
}
opCounts[sel] = opCount
}
return opCounts, nil
}