github.com/koko1123/flow-go-1@v0.29.6/fvm/environment/facade_env.go (about)

     1  package environment
     2  
     3  import (
     4  	"context"
     5  
     6  	"github.com/onflow/cadence/runtime/common"
     7  	"github.com/onflow/cadence/runtime/interpreter"
     8  
     9  	"github.com/koko1123/flow-go-1/fvm/derived"
    10  	"github.com/koko1123/flow-go-1/fvm/state"
    11  	"github.com/koko1123/flow-go-1/fvm/tracing"
    12  )
    13  
    14  var _ Environment = &facadeEnvironment{}
    15  
    16  // facadeEnvironment exposes various fvm business logic as a single interface.
    17  type facadeEnvironment struct {
    18  	*Runtime
    19  
    20  	tracing.TracerSpan
    21  	Meter
    22  
    23  	*ProgramLogger
    24  	EventEmitter
    25  
    26  	UnsafeRandomGenerator
    27  	CryptoLibrary
    28  
    29  	BlockInfo
    30  	AccountInfo
    31  	TransactionInfo
    32  
    33  	ValueStore
    34  
    35  	*SystemContracts
    36  
    37  	UUIDGenerator
    38  
    39  	AccountCreator
    40  	AccountFreezer
    41  
    42  	AccountKeyReader
    43  	AccountKeyUpdater
    44  
    45  	*ContractReader
    46  	ContractUpdater
    47  	*Programs
    48  
    49  	accounts Accounts
    50  	txnState *state.TransactionState
    51  }
    52  
    53  func newFacadeEnvironment(
    54  	tracer tracing.TracerSpan,
    55  	params EnvironmentParams,
    56  	txnState *state.TransactionState,
    57  	derivedTxnData DerivedTransactionData,
    58  	meter Meter,
    59  ) *facadeEnvironment {
    60  	accounts := NewAccounts(txnState)
    61  	logger := NewProgramLogger(tracer, params.ProgramLoggerParams)
    62  	runtime := NewRuntime(params.RuntimeParams)
    63  	systemContracts := NewSystemContracts(
    64  		params.Chain,
    65  		tracer,
    66  		logger,
    67  		runtime)
    68  
    69  	env := &facadeEnvironment{
    70  		Runtime: runtime,
    71  
    72  		TracerSpan: tracer,
    73  		Meter:      meter,
    74  
    75  		ProgramLogger: logger,
    76  		EventEmitter:  NoEventEmitter{},
    77  
    78  		UnsafeRandomGenerator: NewUnsafeRandomGenerator(
    79  			tracer,
    80  			params.BlockHeader,
    81  		),
    82  		CryptoLibrary: NewCryptoLibrary(tracer, meter),
    83  
    84  		BlockInfo: NewBlockInfo(
    85  			tracer,
    86  			meter,
    87  			params.BlockHeader,
    88  			params.Blocks,
    89  		),
    90  		AccountInfo: NewAccountInfo(
    91  			tracer,
    92  			meter,
    93  			accounts,
    94  			systemContracts,
    95  			params.ServiceAccountEnabled,
    96  		),
    97  		TransactionInfo: NoTransactionInfo{},
    98  
    99  		ValueStore: NewValueStore(
   100  			tracer,
   101  			meter,
   102  			accounts,
   103  		),
   104  
   105  		SystemContracts: systemContracts,
   106  
   107  		UUIDGenerator: NewUUIDGenerator(
   108  			tracer,
   109  			meter,
   110  			txnState),
   111  
   112  		AccountCreator: NoAccountCreator{},
   113  		AccountFreezer: NoAccountFreezer{},
   114  
   115  		AccountKeyReader: NewAccountKeyReader(
   116  			tracer,
   117  			meter,
   118  			accounts,
   119  		),
   120  		AccountKeyUpdater: NoAccountKeyUpdater{},
   121  
   122  		ContractReader: NewContractReader(
   123  			tracer,
   124  			meter,
   125  			accounts,
   126  		),
   127  		ContractUpdater: NoContractUpdater{},
   128  		Programs: NewPrograms(
   129  			tracer,
   130  			meter,
   131  			txnState,
   132  			accounts,
   133  			derivedTxnData),
   134  
   135  		accounts: accounts,
   136  		txnState: txnState,
   137  	}
   138  
   139  	env.Runtime.SetEnvironment(env)
   140  
   141  	return env
   142  }
   143  
   144  func NewScriptEnvironment(
   145  	ctx context.Context,
   146  	tracer tracing.TracerSpan,
   147  	params EnvironmentParams,
   148  	txnState *state.TransactionState,
   149  	derivedTxnData DerivedTransactionData,
   150  ) *facadeEnvironment {
   151  	env := newFacadeEnvironment(
   152  		tracer,
   153  		params,
   154  		txnState,
   155  		derivedTxnData,
   156  		NewCancellableMeter(ctx, txnState))
   157  
   158  	env.addParseRestrictedChecks()
   159  
   160  	return env
   161  }
   162  
   163  func NewTransactionEnvironment(
   164  	tracer tracing.TracerSpan,
   165  	params EnvironmentParams,
   166  	txnState *state.TransactionState,
   167  	derivedTxnData DerivedTransactionData,
   168  ) *facadeEnvironment {
   169  	env := newFacadeEnvironment(
   170  		tracer,
   171  		params,
   172  		txnState,
   173  		derivedTxnData,
   174  		NewMeter(txnState),
   175  	)
   176  
   177  	env.TransactionInfo = NewTransactionInfo(
   178  		params.TransactionInfoParams,
   179  		tracer,
   180  		params.Chain.ServiceAddress(),
   181  	)
   182  	env.EventEmitter = NewEventEmitter(
   183  		tracer,
   184  		env.Meter,
   185  		params.Chain,
   186  		params.TransactionInfoParams,
   187  		params.EventEmitterParams,
   188  	)
   189  	env.AccountCreator = NewAccountCreator(
   190  		txnState,
   191  		params.Chain,
   192  		env.accounts,
   193  		params.ServiceAccountEnabled,
   194  		tracer,
   195  		env.Meter,
   196  		params.MetricsReporter,
   197  		env.SystemContracts)
   198  	env.AccountFreezer = NewAccountFreezer(
   199  		params.Chain.ServiceAddress(),
   200  		env.accounts,
   201  		env.TransactionInfo)
   202  	env.ContractUpdater = NewContractUpdater(
   203  		tracer,
   204  		env.Meter,
   205  		env.accounts,
   206  		env.TransactionInfo,
   207  		params.Chain,
   208  		params.ContractUpdaterParams,
   209  		env.ProgramLogger,
   210  		env.SystemContracts,
   211  		env.Runtime)
   212  
   213  	env.AccountKeyUpdater = NewAccountKeyUpdater(
   214  		tracer,
   215  		env.Meter,
   216  		env.accounts,
   217  		txnState,
   218  		env)
   219  
   220  	env.addParseRestrictedChecks()
   221  
   222  	return env
   223  }
   224  
   225  func (env *facadeEnvironment) addParseRestrictedChecks() {
   226  	// NOTE: Cadence can access Programs, ContractReader, Meter and
   227  	// ProgramLogger while it is parsing programs; all other access are
   228  	// unexpected and are potentially program cache invalidation bugs.
   229  	//
   230  	// Also note that Tracer and SystemContracts are unguarded since these are
   231  	// not accessible by Cadence.
   232  
   233  	env.AccountCreator = NewParseRestrictedAccountCreator(
   234  		env.txnState,
   235  		env.AccountCreator)
   236  	env.AccountFreezer = NewParseRestrictedAccountFreezer(
   237  		env.txnState,
   238  		env.AccountFreezer)
   239  	env.AccountInfo = NewParseRestrictedAccountInfo(
   240  		env.txnState,
   241  		env.AccountInfo)
   242  	env.AccountKeyReader = NewParseRestrictedAccountKeyReader(
   243  		env.txnState,
   244  		env.AccountKeyReader)
   245  	env.AccountKeyUpdater = NewParseRestrictedAccountKeyUpdater(
   246  		env.txnState,
   247  		env.AccountKeyUpdater)
   248  	env.BlockInfo = NewParseRestrictedBlockInfo(
   249  		env.txnState,
   250  		env.BlockInfo)
   251  	env.ContractUpdater = NewParseRestrictedContractUpdater(
   252  		env.txnState,
   253  		env.ContractUpdater)
   254  	env.CryptoLibrary = NewParseRestrictedCryptoLibrary(
   255  		env.txnState,
   256  		env.CryptoLibrary)
   257  	env.EventEmitter = NewParseRestrictedEventEmitter(
   258  		env.txnState,
   259  		env.EventEmitter)
   260  	env.TransactionInfo = NewParseRestrictedTransactionInfo(
   261  		env.txnState,
   262  		env.TransactionInfo)
   263  	env.UnsafeRandomGenerator = NewParseRestrictedUnsafeRandomGenerator(
   264  		env.txnState,
   265  		env.UnsafeRandomGenerator)
   266  	env.UUIDGenerator = NewParseRestrictedUUIDGenerator(
   267  		env.txnState,
   268  		env.UUIDGenerator)
   269  	env.ValueStore = NewParseRestrictedValueStore(
   270  		env.txnState,
   271  		env.ValueStore)
   272  }
   273  
   274  func (env *facadeEnvironment) FlushPendingUpdates() (
   275  	derived.TransactionInvalidator,
   276  	error,
   277  ) {
   278  	contractKeys, err := env.ContractUpdater.Commit()
   279  	if err != nil {
   280  		return nil, err
   281  	}
   282  
   283  	return NewDerivedDataInvalidator(contractKeys, env), nil
   284  }
   285  
   286  func (env *facadeEnvironment) Reset() {
   287  	env.ContractUpdater.Reset()
   288  	env.EventEmitter.Reset()
   289  	env.AccountFreezer.Reset()
   290  }
   291  
   292  // Miscellaneous cadence runtime.Interface API.
   293  func (facadeEnvironment) ResourceOwnerChanged(
   294  	*interpreter.Interpreter,
   295  	*interpreter.CompositeValue,
   296  	common.Address,
   297  	common.Address,
   298  ) {
   299  }
   300  
   301  func (env *facadeEnvironment) SetInterpreterSharedState(state *interpreter.SharedState) {
   302  	// NO-OP
   303  }
   304  
   305  func (env *facadeEnvironment) GetInterpreterSharedState() *interpreter.SharedState {
   306  	return nil
   307  }