-
Notifications
You must be signed in to change notification settings - Fork 175
/
transactionInvocator.go
378 lines (320 loc) · 10.5 KB
/
transactionInvocator.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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
package fvm
import (
"encoding/json"
"fmt"
"io/ioutil"
"path"
"time"
"github.com/onflow/cadence/runtime"
"github.com/onflow/cadence/runtime/common"
"github.com/onflow/cadence/runtime/interpreter"
"github.com/onflow/cadence/runtime/sema"
"github.com/opentracing/opentracing-go"
traceLog "github.com/opentracing/opentracing-go/log"
"github.com/rs/zerolog"
"github.com/onflow/flow-go/fvm/errors"
"github.com/onflow/flow-go/fvm/extralog"
"github.com/onflow/flow-go/fvm/programs"
"github.com/onflow/flow-go/fvm/state"
"github.com/onflow/flow-go/model/flow"
"github.com/onflow/flow-go/module/trace"
)
const (
flowServiceAccountContract = "FlowServiceAccount"
deductFeesContractFunction = "deductTransactionFee"
)
type TransactionInvocator struct {
logger zerolog.Logger
}
func NewTransactionInvocator(logger zerolog.Logger) *TransactionInvocator {
return &TransactionInvocator{
logger: logger,
}
}
func (i *TransactionInvocator) Process(
vm *VirtualMachine,
ctx *Context,
proc *TransactionProcedure,
sth *state.StateHolder,
programs *programs.Programs,
) (processErr error) {
var span opentracing.Span
if ctx.Tracer != nil && proc.TraceSpan != nil {
span = ctx.Tracer.StartSpanFromParent(proc.TraceSpan, trace.FVMExecuteTransaction)
span.LogFields(
traceLog.String("transaction.ID", proc.ID.String()),
)
defer span.Finish()
}
var blockHeight uint64
if ctx.BlockHeader != nil {
blockHeight = ctx.BlockHeader.Height
}
var env *TransactionEnv
var txError error
retry := false
numberOfRetries := 0
parentState := sth.State()
childState := sth.NewChild()
env = NewTransactionEnvironment(*ctx, vm, sth, programs, proc.Transaction, proc.TxIndex, span)
predeclaredValues := valueDeclarations(ctx, env)
defer func() {
// an extra check for state holder health, this should never happen
if childState != sth.State() {
// error transaction
msg := "child state doesn't match the active state on the state holder"
i.logger.Error().
Str("txHash", proc.ID.String()).
Uint64("blockHeight", blockHeight).
Msg(msg)
// drop delta
childState.View().DropDelta()
proc.Err = errors.NewFVMInternalErrorf(msg)
proc.Logs = make([]string, 0)
proc.Events = make([]flow.Event, 0)
proc.ServiceEvents = make([]flow.Event, 0)
}
if mergeError := parentState.MergeState(childState); mergeError != nil {
processErr = fmt.Errorf("transaction invocation failed: %w", mergeError)
}
sth.SetActiveState(parentState)
}()
for numberOfRetries = 0; numberOfRetries < int(ctx.MaxNumOfTxRetries); numberOfRetries++ {
if retry {
// rest state
sth.SetActiveState(parentState)
childState = sth.NewChild()
// force cleanup if retries
programs.ForceCleanup()
i.logger.Warn().
Str("txHash", proc.ID.String()).
Uint64("blockHeight", blockHeight).
Int("retries_count", numberOfRetries).
Uint64("ledger_interaction_used", sth.State().InteractionUsed()).
Msg("retrying transaction execution")
// reset error part of proc
// Warning right now the tx requires retry logic doesn't change
// anything on state but we might want to revert the state changes (or not commiting)
// if we decided to expand it furthur.
proc.Err = nil
proc.Logs = make([]string, 0)
proc.Events = make([]flow.Event, 0)
proc.ServiceEvents = make([]flow.Event, 0)
// reset env
env = NewTransactionEnvironment(*ctx, vm, sth, programs, proc.Transaction, proc.TxIndex, span)
}
location := common.TransactionLocation(proc.ID[:])
err := vm.Runtime.ExecuteTransaction(
runtime.Script{
Source: proc.Transaction.Script,
Arguments: proc.Transaction.Arguments,
},
runtime.Context{
Interface: env,
Location: location,
PredeclaredValues: predeclaredValues,
},
)
if err != nil {
txError = fmt.Errorf("transaction invocation failed: %w", errors.HandleRuntimeError(err))
}
// break the loop
if !i.requiresRetry(err, proc) {
break
}
retry = true
proc.Retried++
}
// (for future use) panic if we tried several times and still failing because of checking issue
// if numberOfTries == maxNumberOfRetries {
// panic(err)
// }
// applying contract changes
// this writes back the contract contents to accounts
// if any error occurs we fail the tx
updatedKeys, err := env.Commit()
if err != nil && txError == nil {
txError = fmt.Errorf("transaction invocation failed: %w", err)
}
if txError == nil {
txError = NewTransactionStorageLimiter().CheckLimits(env, sth.State().UpdatedAddresses())
}
if txError == nil {
txError = i.deductTransactionFees(env, proc)
}
proc.Logs = append(proc.Logs, env.Logs()...)
proc.ComputationUsed = proc.ComputationUsed + env.GetComputationUsed()
if txError != nil {
// drop delta
childState.View().DropDelta()
// if tx fails just do clean up
programs.Cleanup(nil)
i.logger.Info().
Str("txHash", proc.ID.String()).
Uint64("blockHeight", blockHeight).
Uint64("ledgerInteractionUsed", sth.State().InteractionUsed()).
Msg("transaction executed with error")
return txError
}
// based on the contract updates we decide how to clean up the programs
// for failed transactions we also do the same as
// transaction without any deployed contracts
programs.Cleanup(updatedKeys)
proc.Events = append(proc.Events, env.Events()...)
proc.ServiceEvents = append(proc.ServiceEvents, env.ServiceEvents()...)
i.logger.Info().
Str("txHash", proc.ID.String()).
Uint64("blockHeight", blockHeight).
Uint64("ledgerInteractionUsed", sth.State().InteractionUsed()).
Int("retried", proc.Retried).
Msg("transaction executed successfully")
return nil
}
func (i *TransactionInvocator) deductTransactionFees(env *TransactionEnv, proc *TransactionProcedure) error {
if !env.ctx.TransactionFeesEnabled {
return nil
}
invocator := NewTransactionContractFunctionInvocator(
common.AddressLocation{
Address: common.BytesToAddress(env.ctx.Chain.ServiceAddress().Bytes()),
Name: flowServiceAccountContract,
},
deductFeesContractFunction,
[]interpreter.Value{
interpreter.NewAddressValue(common.BytesToAddress(proc.Transaction.Payer.Bytes())),
},
[]sema.Type{
sema.AuthAccountType,
},
env.ctx.Logger,
)
_, err := invocator.Invoke(env, proc.TraceSpan)
if err != nil {
// TODO: Fee value is currently a constant. this should be changed when it is not
fees, ok := DefaultTransactionFees.ToGoValue().(uint64)
if !ok {
err = fmt.Errorf("could not get transaction fees during formatting of TransactionFeeDeductionFailedError: %w", err)
}
return errors.NewTransactionFeeDeductionFailedError(proc.Transaction.Payer, fees, err)
}
return nil
}
func valueDeclarations(ctx *Context, env *TransactionEnv) []runtime.ValueDeclaration {
var predeclaredValues []runtime.ValueDeclaration
if ctx.AccountFreezeAvailable {
// TODO return the errors instead of panicing
setAccountFrozen := runtime.ValueDeclaration{
Name: "setAccountFrozen",
Type: &sema.FunctionType{
Parameters: []*sema.Parameter{
{
Label: sema.ArgumentLabelNotRequired,
Identifier: "account",
TypeAnnotation: sema.NewTypeAnnotation(&sema.AddressType{}),
},
{
Label: sema.ArgumentLabelNotRequired,
Identifier: "frozen",
TypeAnnotation: sema.NewTypeAnnotation(sema.BoolType),
},
},
ReturnTypeAnnotation: &sema.TypeAnnotation{
Type: sema.VoidType,
},
},
Kind: common.DeclarationKindFunction,
IsConstant: true,
ArgumentLabels: nil,
Value: interpreter.NewHostFunctionValue(
func(invocation interpreter.Invocation) interpreter.Value {
address, ok := invocation.Arguments[0].(interpreter.AddressValue)
if !ok {
panic(errors.NewValueErrorf(invocation.Arguments[0].String(),
"first argument of setAccountFrozen must be an address"))
}
frozen, ok := invocation.Arguments[1].(interpreter.BoolValue)
if !ok {
panic(errors.NewValueErrorf(invocation.Arguments[0].String(),
"second argument of setAccountFrozen must be a boolean"))
}
err := env.SetAccountFrozen(common.Address(address), bool(frozen))
if err != nil {
panic(err)
}
return interpreter.VoidValue{}
},
),
}
predeclaredValues = append(predeclaredValues, setAccountFrozen)
}
return predeclaredValues
}
// requiresRetry returns true for transactions that has to be rerun
// this is an additional check which was introduced
func (i *TransactionInvocator) requiresRetry(err error, proc *TransactionProcedure) bool {
// if no error no retry
if err == nil {
return false
}
// Only consider runtime errors,
// in particular only consider parsing/checking errors
var runtimeErr runtime.Error
if !errors.As(err, &runtimeErr) {
return false
}
var parsingCheckingError *runtime.ParsingCheckingError
if !errors.As(err, &parsingCheckingError) {
return false
}
// Only consider errors in deployed contracts.
checkerError, ok := parsingCheckingError.Err.(*sema.CheckerError)
if !ok {
return false
}
var foundImportedProgramError bool
for _, checkingErr := range checkerError.Errors {
importedProgramError, ok := checkingErr.(*sema.ImportedProgramError)
if !ok {
continue
}
_, ok = importedProgramError.Location.(common.AddressLocation)
if !ok {
continue
}
foundImportedProgramError = true
break
}
if !foundImportedProgramError {
return false
}
i.dumpRuntimeError(&runtimeErr, proc)
return true
}
// logRuntimeError logs run time errors into a file
// This is a temporary measure.
func (i *TransactionInvocator) dumpRuntimeError(runtimeErr *runtime.Error, procedure *TransactionProcedure) {
codesJSON, err := json.Marshal(runtimeErr.Codes)
if err != nil {
i.logger.Error().Err(err).Msg("cannot marshal codes JSON")
}
programsJSON, err := json.Marshal(runtimeErr.Programs)
if err != nil {
i.logger.Error().Err(err).Msg("cannot marshal programs JSON")
}
t := time.Now().UnixNano()
codesPath := path.Join(extralog.ExtraLogDumpPath, fmt.Sprintf("%s-codes-%d", procedure.ID.String(), t))
programsPath := path.Join(extralog.ExtraLogDumpPath, fmt.Sprintf("%s-programs-%d", procedure.ID.String(), t))
err = ioutil.WriteFile(codesPath, codesJSON, 0700)
if err != nil {
i.logger.Error().Err(err).Msg("cannot write codes json")
}
err = ioutil.WriteFile(programsPath, programsJSON, 0700)
if err != nil {
i.logger.Error().Err(err).Msg("cannot write programs json")
}
i.logger.Error().
Str("txHash", procedure.ID.String()).
Str("codes", string(codesJSON)).
Str("programs", string(programsJSON)).
Msg("checking failed")
}