-
Notifications
You must be signed in to change notification settings - Fork 175
/
contractFunctionInvoker.go
78 lines (67 loc) · 2.03 KB
/
contractFunctionInvoker.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
package fvm
import (
"fmt"
"github.com/opentracing/opentracing-go"
traceLog "github.com/opentracing/opentracing-go/log"
"github.com/rs/zerolog"
"github.com/onflow/cadence"
"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/onflow/flow-go/module/trace"
)
type ContractFunctionInvoker struct {
contractLocation common.AddressLocation
functionName string
arguments []interpreter.Value
argumentTypes []sema.Type
logger zerolog.Logger
logSpanFields []traceLog.Field
}
func NewContractFunctionInvoker(
contractLocation common.AddressLocation,
functionName string,
arguments []interpreter.Value,
argumentTypes []sema.Type,
logger zerolog.Logger) *ContractFunctionInvoker {
return &ContractFunctionInvoker{
contractLocation: contractLocation,
functionName: functionName,
arguments: arguments,
argumentTypes: argumentTypes,
logger: logger,
logSpanFields: []traceLog.Field{traceLog.String("transaction.ContractFunctionCall", fmt.Sprintf("%s.%s", contractLocation.String(), functionName))},
}
}
func (i *ContractFunctionInvoker) Invoke(env Environment, parentTraceSpan opentracing.Span) (cadence.Value, error) {
var span opentracing.Span
ctx := env.Context()
if ctx.Tracer != nil && parentTraceSpan != nil {
span = ctx.Tracer.StartSpanFromParent(parentTraceSpan, trace.FVMInvokeContractFunction)
span.LogFields(
i.logSpanFields...,
)
defer span.Finish()
}
predeclaredValues := valueDeclarations(ctx, env)
value, err := env.VM().Runtime.InvokeContractFunction(
i.contractLocation,
i.functionName,
i.arguments,
i.argumentTypes,
runtime.Context{
Interface: env,
PredeclaredValues: predeclaredValues,
},
)
if err != nil {
i.logger.
Info().
Err(err).
Str("contract", i.contractLocation.String()).
Str("function", i.functionName).
Msg("Contract function call executed with error")
}
return value, err
}