-
Notifications
You must be signed in to change notification settings - Fork 175
/
env.go
122 lines (96 loc) · 3.02 KB
/
env.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
package environment
import (
"context"
"github.com/onflow/cadence"
"github.com/onflow/cadence/runtime"
"github.com/onflow/cadence/runtime/common"
"github.com/rs/zerolog"
otelTrace "go.opentelemetry.io/otel/trace"
"github.com/onflow/flow-go/fvm/programs"
reusableRuntime "github.com/onflow/flow-go/fvm/runtime"
"github.com/onflow/flow-go/fvm/state"
"github.com/onflow/flow-go/model/flow"
"github.com/onflow/flow-go/module/trace"
)
// Environment implements the accounts business logic and exposes cadence
// runtime interface methods for the runtime.
type Environment interface {
runtime.Interface
// Tracer
StartSpanFromRoot(name trace.SpanName) otelTrace.Span
Meter
// Runtime
BorrowCadenceRuntime() *reusableRuntime.ReusableCadenceRuntime
ReturnCadenceRuntime(*reusableRuntime.ReusableCadenceRuntime)
TransactionInfo
// ProgramLogger
Logger() *zerolog.Logger
Logs() []string
// EventEmitter
Events() []flow.Event
ServiceEvents() []flow.Event
// SystemContracts
AccountsStorageCapacity(addresses []common.Address) (cadence.Value, error)
DeductTransactionFees(
payer flow.Address,
inclusionEffort uint64,
executionEffort uint64,
) (
cadence.Value,
error,
)
// AccountInfo
GetAccount(address flow.Address) (*flow.Account, error)
AccountFreezer
// FlushPendingUpdates flushes pending updates from the stateful environment
// modules (i.e., ContractUpdater) to the state transaction, and return
// corresponding modified sets invalidator.
FlushPendingUpdates() (
programs.ModifiedSetsInvalidator,
error,
)
// Reset resets all stateful environment modules (e.g., ContractUpdater,
// EventEmitter, AccountFreezer) to initial state.
Reset()
}
type EnvironmentParams struct {
Chain flow.Chain
// NOTE: The ServiceAccountEnabled option is used by the playground
// https://github.com/onflow/flow-playground-api/blob/1ad967055f31db8f1ce88e008960e5fc14a9fbd1/compute/computer.go#L76
ServiceAccountEnabled bool
RuntimeParams
TracerParams
ProgramLoggerParams
EventEmitterParams
BlockInfoParams
TransactionInfoParams
ContractUpdaterParams
}
func DefaultEnvironmentParams() EnvironmentParams {
return EnvironmentParams{
Chain: flow.Mainnet.Chain(),
ServiceAccountEnabled: true,
RuntimeParams: DefaultRuntimeParams(),
TracerParams: DefaultTracerParams(),
ProgramLoggerParams: DefaultProgramLoggerParams(),
EventEmitterParams: DefaultEventEmitterParams(),
BlockInfoParams: DefaultBlockInfoParams(),
TransactionInfoParams: DefaultTransactionInfoParams(),
ContractUpdaterParams: DefaultContractUpdaterParams(),
}
}
func NewScriptEnvironment(
ctx context.Context,
params EnvironmentParams,
txnState *state.TransactionState,
programs TransactionPrograms,
) Environment {
return newScriptFacadeEnvironment(ctx, params, txnState, programs)
}
func NewTransactionEnvironment(
params EnvironmentParams,
txnState *state.TransactionState,
programs TransactionPrograms,
) Environment {
return newTransactionFacadeEnvironment(params, txnState, programs)
}