github.com/onflow/flow-go@v0.35.7-crescendo-preview.23-atree-inlining/fvm/environment/env.go (about)

     1  package environment
     2  
     3  import (
     4  	"github.com/onflow/cadence"
     5  	"github.com/onflow/cadence/runtime"
     6  	"github.com/rs/zerolog"
     7  	otelTrace "go.opentelemetry.io/otel/trace"
     8  
     9  	reusableRuntime "github.com/onflow/flow-go/fvm/runtime"
    10  	"github.com/onflow/flow-go/fvm/tracing"
    11  	"github.com/onflow/flow-go/model/flow"
    12  	"github.com/onflow/flow-go/module/trace"
    13  )
    14  
    15  // Environment implements the accounts business logic and exposes cadence
    16  // runtime interface methods for the runtime.
    17  type Environment interface {
    18  	runtime.Interface
    19  
    20  	// Tracer
    21  	StartChildSpan(
    22  		name trace.SpanName,
    23  		options ...otelTrace.SpanStartOption,
    24  	) tracing.TracerSpan
    25  
    26  	Meter
    27  
    28  	// Runtime
    29  	BorrowCadenceRuntime() *reusableRuntime.ReusableCadenceRuntime
    30  	ReturnCadenceRuntime(*reusableRuntime.ReusableCadenceRuntime)
    31  
    32  	TransactionInfo
    33  
    34  	// ProgramLogger
    35  	Logger() zerolog.Logger
    36  	Logs() []string
    37  
    38  	// EventEmitter
    39  	Events() flow.EventsList
    40  	ServiceEvents() flow.EventsList
    41  	ConvertedServiceEvents() flow.ServiceEventList
    42  
    43  	// SystemContracts
    44  	ContractFunctionInvoker
    45  
    46  	AccountsStorageCapacity(
    47  		addresses []flow.Address,
    48  		payer flow.Address,
    49  		maxTxFees uint64,
    50  	) (
    51  		cadence.Value,
    52  		error,
    53  	)
    54  	CheckPayerBalanceAndGetMaxTxFees(
    55  		payer flow.Address,
    56  		inclusionEffort uint64,
    57  		executionEffort uint64,
    58  	) (
    59  		cadence.Value,
    60  		error,
    61  	)
    62  	DeductTransactionFees(
    63  		payer flow.Address,
    64  		inclusionEffort uint64,
    65  		executionEffort uint64,
    66  	) (
    67  		cadence.Value,
    68  		error,
    69  	)
    70  
    71  	// AccountInfo
    72  	GetAccount(address flow.Address) (*flow.Account, error)
    73  
    74  	// RandomSourceHistory is the current block's derived random source.
    75  	// This source is only used by the core-contract that tracks the random source
    76  	// history for commit-reveal schemes.
    77  	RandomSourceHistory() ([]byte, error)
    78  
    79  	// FlushPendingUpdates flushes pending updates from the stateful environment
    80  	// modules (i.e., ContractUpdater) to the state transaction, and return
    81  	// the updated contract keys.
    82  	FlushPendingUpdates() (
    83  		ContractUpdates,
    84  		error,
    85  	)
    86  
    87  	// Reset resets all stateful environment modules (e.g., ContractUpdater,
    88  	// EventEmitter) to initial state.
    89  	Reset()
    90  }
    91  
    92  type EnvironmentParams struct {
    93  	Chain flow.Chain
    94  
    95  	// NOTE: The ServiceAccountEnabled option is used by the playground
    96  	// https://github.com/onflow/flow-playground-api/blob/1ad967055f31db8f1ce88e008960e5fc14a9fbd1/compute/computer.go#L76
    97  	ServiceAccountEnabled bool
    98  
    99  	RuntimeParams
   100  
   101  	ProgramLoggerParams
   102  
   103  	EventEmitterParams
   104  
   105  	BlockInfoParams
   106  	TransactionInfoParams
   107  	ScriptInfoParams
   108  
   109  	EntropyProvider
   110  
   111  	ContractUpdaterParams
   112  }
   113  
   114  func DefaultEnvironmentParams() EnvironmentParams {
   115  	return EnvironmentParams{
   116  		Chain:                 flow.Mainnet.Chain(),
   117  		ServiceAccountEnabled: true,
   118  
   119  		RuntimeParams:         DefaultRuntimeParams(),
   120  		ProgramLoggerParams:   DefaultProgramLoggerParams(),
   121  		EventEmitterParams:    DefaultEventEmitterParams(),
   122  		BlockInfoParams:       DefaultBlockInfoParams(),
   123  		TransactionInfoParams: DefaultTransactionInfoParams(),
   124  		ContractUpdaterParams: DefaultContractUpdaterParams(),
   125  	}
   126  }
   127  
   128  func (env *EnvironmentParams) SetScriptInfoParams(info *ScriptInfoParams) {
   129  	env.ScriptInfoParams = *info
   130  }