-
Notifications
You must be signed in to change notification settings - Fork 179
/
validator.go
322 lines (268 loc) · 8.25 KB
/
validator.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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
package access
import (
"errors"
"fmt"
"github.com/onflow/flow-go/crypto"
"github.com/onflow/cadence/runtime/parser2"
"github.com/onflow/flow-go/model/flow"
"github.com/onflow/flow-go/state/protocol"
"github.com/onflow/flow-go/storage"
)
type Blocks interface {
HeaderByID(id flow.Identifier) (*flow.Header, error)
FinalizedHeader() (*flow.Header, error)
}
type ProtocolStateBlocks struct {
state protocol.State
}
func NewProtocolStateBlocks(state protocol.State) *ProtocolStateBlocks {
return &ProtocolStateBlocks{state: state}
}
func (b *ProtocolStateBlocks) HeaderByID(id flow.Identifier) (*flow.Header, error) {
header, err := b.state.AtBlockID(id).Head()
if err != nil {
if errors.Is(err, storage.ErrNotFound) {
return nil, nil
}
return nil, err
}
return header, nil
}
func (b *ProtocolStateBlocks) FinalizedHeader() (*flow.Header, error) {
return b.state.Final().Head()
}
type TransactionValidationOptions struct {
Expiry uint
ExpiryBuffer uint
AllowEmptyReferenceBlockID bool
AllowUnknownReferenceBlockID bool
MaxGasLimit uint64
CheckScriptsParse bool
// MaxAddressIndex is a simple spam prevention measure. It rejects any
// transactions referencing an address with index newer than the specified
// maximum. A zero value indicates no address checking.
MaxAddressIndex uint64
MaxTransactionByteSize uint64
MaxCollectionByteSize uint64
}
type TransactionValidator struct {
blocks Blocks // for looking up blocks to check transaction expiry
chain flow.Chain // for checking validity of addresses
options TransactionValidationOptions
serviceAccountAddress flow.Address
}
func NewTransactionValidator(
blocks Blocks,
chain flow.Chain,
options TransactionValidationOptions,
) *TransactionValidator {
return &TransactionValidator{
blocks: blocks,
chain: chain,
options: options,
serviceAccountAddress: chain.ServiceAddress(),
}
}
func (v *TransactionValidator) Validate(tx *flow.TransactionBody) (err error) {
err = v.checkTxSizeLimit(tx)
if err != nil {
return err
}
err = v.checkMissingFields(tx)
if err != nil {
return err
}
err = v.checkGasLimit(tx)
if err != nil {
return err
}
err = v.checkExpiry(tx)
if err != nil {
return err
}
err = v.checkCanBeParsed(tx)
if err != nil {
return err
}
err = v.checkAddresses(tx)
if err != nil {
return err
}
err = v.checkSignatureFormat(tx)
if err != nil {
return err
}
err = v.checkSignatureDuplications(tx)
if err != nil {
return err
}
// TODO replace checkSignatureFormat by verifying the account/payer signatures
return nil
}
func (v *TransactionValidator) checkTxSizeLimit(tx *flow.TransactionBody) error {
txSize := uint64(tx.ByteSize())
// first check compatibility to collection byte size
// this guarantees liveness
if txSize >= v.options.MaxCollectionByteSize {
return InvalidTxByteSizeError{
Actual: txSize,
Maximum: v.options.MaxCollectionByteSize,
}
}
// this logic need the reason we don't greenlist the service account against the collection size
// limits is we can't verify the signature here yet.
if tx.Payer == v.serviceAccountAddress {
return nil
}
if txSize > v.options.MaxTransactionByteSize {
return InvalidTxByteSizeError{
Actual: txSize,
Maximum: v.options.MaxTransactionByteSize,
}
}
return nil
}
func (v *TransactionValidator) checkMissingFields(tx *flow.TransactionBody) error {
missingFields := tx.MissingFields()
if v.options.AllowEmptyReferenceBlockID {
missingFields = remove(missingFields, flow.TransactionFieldRefBlockID.String())
}
if len(missingFields) > 0 {
return IncompleteTransactionError{MissingFields: missingFields}
}
return nil
}
func (v *TransactionValidator) checkGasLimit(tx *flow.TransactionBody) error {
// if service account is the payer of the transaction accepts any gas limit
// note that even though we don't enforce any limit here, exec node later
// enforce a max value for any transaction
if tx.Payer == v.serviceAccountAddress {
return nil
}
if tx.GasLimit > v.options.MaxGasLimit || tx.GasLimit == 0 {
return InvalidGasLimitError{
Actual: tx.GasLimit,
Maximum: v.options.MaxGasLimit,
}
}
return nil
}
// checkExpiry checks whether a transaction's reference block ID is
// valid. Returns nil if the reference is valid, returns an error if the
// reference is invalid or we failed to check it.
func (v *TransactionValidator) checkExpiry(tx *flow.TransactionBody) error {
if tx.ReferenceBlockID == flow.ZeroID && v.options.AllowEmptyReferenceBlockID {
return nil
}
// look up the reference block
ref, err := v.blocks.HeaderByID(tx.ReferenceBlockID)
if err != nil {
return fmt.Errorf("could not get reference block: %w", err)
}
if ref == nil {
// the transaction references an unknown block - at this point we decide
// whether to consider it expired based on configuration
if v.options.AllowUnknownReferenceBlockID {
return nil
}
return ErrUnknownReferenceBlock
}
// get the latest finalized block we know about
final, err := v.blocks.FinalizedHeader()
if err != nil {
return fmt.Errorf("could not get finalized header: %w", err)
}
diff := final.Height - ref.Height
// check for overflow
if ref.Height > final.Height {
diff = 0
}
// discard transactions that are expired, or that will expire sooner than
// our configured buffer allows
if uint(diff) > v.options.Expiry-v.options.ExpiryBuffer {
return ExpiredTransactionError{
RefHeight: ref.Height,
FinalHeight: final.Height,
}
}
return nil
}
func (v *TransactionValidator) checkCanBeParsed(tx *flow.TransactionBody) error {
if v.options.CheckScriptsParse {
_, err := parser2.ParseProgram(string(tx.Script), nil)
if err != nil {
return InvalidScriptError{ParserErr: err}
}
}
return nil
}
func (v *TransactionValidator) checkAddresses(tx *flow.TransactionBody) error {
for _, address := range append(tx.Authorizers, tx.Payer) {
// first we check objective validity, essentially whether or not this
// is a valid output of the address generator
if !v.chain.IsValid(address) {
return InvalidAddressError{Address: address}
}
// skip second check if not configured
if v.options.MaxAddressIndex == 0 {
continue
}
// next we check subjective validity based on the configured maximum index
index, err := v.chain.IndexFromAddress(address)
if err != nil {
return fmt.Errorf("could not get index for address (%s): %w", address, err)
}
if index > v.options.MaxAddressIndex {
return InvalidAddressError{Address: address}
}
}
return nil
}
// every key (account, key index combination) can only be used once for signing
func (v *TransactionValidator) checkSignatureDuplications(tx *flow.TransactionBody) error {
type uniqueKey struct {
address flow.Address
index uint64
}
observedSigs := make(map[uniqueKey]bool)
for _, sig := range append(tx.PayloadSignatures, tx.EnvelopeSignatures...) {
if observedSigs[uniqueKey{sig.Address, sig.KeyIndex}] {
return DuplicatedSignatureError{Address: sig.Address, KeyIndex: sig.KeyIndex}
}
observedSigs[uniqueKey{sig.Address, sig.KeyIndex}] = true
}
return nil
}
func (v *TransactionValidator) checkSignatureFormat(tx *flow.TransactionBody) error {
for _, signature := range append(tx.PayloadSignatures, tx.EnvelopeSignatures...) {
// check the format of the signature is valid.
// a valid signature is an ECDSA signature of either P-256 or secp256k1 curve.
ecdsaSignature := signature.Signature
// check if the signature could be a P-256 signature
valid, err := crypto.SignatureFormatCheck(crypto.ECDSAP256, ecdsaSignature)
if err != nil {
return fmt.Errorf("could not check the signature format (%s): %w", signature, err)
}
if valid {
continue
}
// check if the signature could be a secp256k1 signature
valid, err = crypto.SignatureFormatCheck(crypto.ECDSASecp256k1, ecdsaSignature)
if err != nil {
return fmt.Errorf("could not check the signature format (%s): %w", signature, err)
}
if valid {
continue
}
return InvalidSignatureError{Signature: signature}
}
return nil
}
func remove(s []string, r string) []string {
for i, v := range s {
if v == r {
return append(s[:i], s[i+1:]...)
}
}
return s
}