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

     1  package environment
     2  
     3  import (
     4  	"github.com/onflow/cadence/runtime"
     5  
     6  	"github.com/koko1123/flow-go-1/fvm/errors"
     7  	"github.com/koko1123/flow-go-1/fvm/state"
     8  	"github.com/koko1123/flow-go-1/fvm/tracing"
     9  	"github.com/koko1123/flow-go-1/model/flow"
    10  	"github.com/koko1123/flow-go-1/module/trace"
    11  )
    12  
    13  type TransactionInfoParams struct {
    14  	TxIndex uint32
    15  	TxId    flow.Identifier
    16  	TxBody  *flow.TransactionBody
    17  
    18  	TransactionFeesEnabled bool
    19  	LimitAccountStorage    bool
    20  }
    21  
    22  func DefaultTransactionInfoParams() TransactionInfoParams {
    23  	// NOTE: TxIndex, TxId and TxBody are populated by NewTransactionEnv rather
    24  	// than by Context.
    25  	return TransactionInfoParams{
    26  		TransactionFeesEnabled: false,
    27  		LimitAccountStorage:    false,
    28  	}
    29  }
    30  
    31  // TransactionInfo exposes information associated with the executing
    32  // transaction.
    33  //
    34  // Note that scripts have no associated transaction information, but must expose
    35  // the API in compliance with the runtime environment interface.
    36  type TransactionInfo interface {
    37  	TxIndex() uint32
    38  	TxID() flow.Identifier
    39  
    40  	TransactionFeesEnabled() bool
    41  	LimitAccountStorage() bool
    42  
    43  	SigningAccounts() []runtime.Address
    44  
    45  	IsServiceAccountAuthorizer() bool
    46  
    47  	// Cadence's runtime API.  Note that the script variant will return
    48  	// OperationNotSupportedError.
    49  	GetSigningAccounts() ([]runtime.Address, error)
    50  }
    51  
    52  type ParseRestrictedTransactionInfo struct {
    53  	txnState *state.TransactionState
    54  	impl     TransactionInfo
    55  }
    56  
    57  func NewParseRestrictedTransactionInfo(
    58  	txnState *state.TransactionState,
    59  	impl TransactionInfo,
    60  ) TransactionInfo {
    61  	return ParseRestrictedTransactionInfo{
    62  		txnState: txnState,
    63  		impl:     impl,
    64  	}
    65  }
    66  
    67  func (info ParseRestrictedTransactionInfo) TxIndex() uint32 {
    68  	return info.impl.TxIndex()
    69  }
    70  
    71  func (info ParseRestrictedTransactionInfo) TxID() flow.Identifier {
    72  	return info.impl.TxID()
    73  }
    74  
    75  func (info ParseRestrictedTransactionInfo) TransactionFeesEnabled() bool {
    76  	return info.impl.TransactionFeesEnabled()
    77  }
    78  
    79  func (info ParseRestrictedTransactionInfo) LimitAccountStorage() bool {
    80  	return info.impl.LimitAccountStorage()
    81  }
    82  
    83  func (info ParseRestrictedTransactionInfo) SigningAccounts() []runtime.Address {
    84  	return info.impl.SigningAccounts()
    85  }
    86  
    87  func (info ParseRestrictedTransactionInfo) IsServiceAccountAuthorizer() bool {
    88  	return info.impl.IsServiceAccountAuthorizer()
    89  }
    90  
    91  func (info ParseRestrictedTransactionInfo) GetSigningAccounts() (
    92  	[]runtime.Address,
    93  	error,
    94  ) {
    95  	return parseRestrict1Ret(
    96  		info.txnState,
    97  		trace.FVMEnvGetSigningAccounts,
    98  		info.impl.GetSigningAccounts)
    99  }
   100  
   101  type transactionInfo struct {
   102  	params TransactionInfoParams
   103  
   104  	tracer tracing.TracerSpan
   105  
   106  	authorizers                []runtime.Address
   107  	isServiceAccountAuthorizer bool
   108  }
   109  
   110  func NewTransactionInfo(
   111  	params TransactionInfoParams,
   112  	tracer tracing.TracerSpan,
   113  	serviceAccount flow.Address,
   114  ) TransactionInfo {
   115  
   116  	isServiceAccountAuthorizer := false
   117  	runtimeAddresses := make(
   118  		[]runtime.Address,
   119  		0,
   120  		len(params.TxBody.Authorizers))
   121  
   122  	for _, auth := range params.TxBody.Authorizers {
   123  		runtimeAddresses = append(runtimeAddresses, runtime.Address(auth))
   124  		if auth == serviceAccount {
   125  			isServiceAccountAuthorizer = true
   126  		}
   127  	}
   128  
   129  	return &transactionInfo{
   130  		params:                     params,
   131  		tracer:                     tracer,
   132  		authorizers:                runtimeAddresses,
   133  		isServiceAccountAuthorizer: isServiceAccountAuthorizer,
   134  	}
   135  }
   136  
   137  func (info *transactionInfo) TxIndex() uint32 {
   138  	return info.params.TxIndex
   139  }
   140  
   141  func (info *transactionInfo) TxID() flow.Identifier {
   142  	return info.params.TxId
   143  }
   144  
   145  func (info *transactionInfo) TransactionFeesEnabled() bool {
   146  	return info.params.TransactionFeesEnabled
   147  }
   148  
   149  func (info *transactionInfo) LimitAccountStorage() bool {
   150  	return info.params.LimitAccountStorage
   151  }
   152  
   153  func (info *transactionInfo) SigningAccounts() []runtime.Address {
   154  	return info.authorizers
   155  }
   156  
   157  func (info *transactionInfo) IsServiceAccountAuthorizer() bool {
   158  	return info.isServiceAccountAuthorizer
   159  }
   160  
   161  func (info *transactionInfo) GetSigningAccounts() ([]runtime.Address, error) {
   162  	defer info.tracer.StartExtensiveTracingChildSpan(
   163  		trace.FVMEnvGetSigningAccounts).End()
   164  
   165  	return info.authorizers, nil
   166  }
   167  
   168  var _ TransactionInfo = NoTransactionInfo{}
   169  
   170  // Scripts have no associated transaction information.
   171  type NoTransactionInfo struct {
   172  }
   173  
   174  func (NoTransactionInfo) TxIndex() uint32 {
   175  	return 0
   176  }
   177  
   178  func (NoTransactionInfo) TxID() flow.Identifier {
   179  	return flow.ZeroID
   180  }
   181  
   182  func (NoTransactionInfo) TransactionFeesEnabled() bool {
   183  	return false
   184  }
   185  
   186  func (NoTransactionInfo) LimitAccountStorage() bool {
   187  	return false
   188  }
   189  
   190  func (NoTransactionInfo) SigningAccounts() []runtime.Address {
   191  	return nil
   192  }
   193  
   194  func (NoTransactionInfo) IsServiceAccountAuthorizer() bool {
   195  	return false
   196  }
   197  
   198  func (NoTransactionInfo) GetSigningAccounts() ([]runtime.Address, error) {
   199  	return nil, errors.NewOperationNotSupportedError("GetSigningAccounts")
   200  }