-
Notifications
You must be signed in to change notification settings - Fork 175
/
transaction_info.go
194 lines (154 loc) · 4.73 KB
/
transaction_info.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
package environment
import (
"github.com/onflow/cadence/runtime/common"
"github.com/onflow/flow-go/fvm/errors"
"github.com/onflow/flow-go/fvm/storage/state"
"github.com/onflow/flow-go/fvm/tracing"
"github.com/onflow/flow-go/model/flow"
"github.com/onflow/flow-go/module/trace"
)
type TransactionInfoParams struct {
TxIndex uint32
TxId flow.Identifier
TxBody *flow.TransactionBody
TransactionFeesEnabled bool
LimitAccountStorage bool
// RandomSourceHistoryCallAllowed is true if the transaction is allowed to call the `entropy`
// cadence function to get the entropy of that block.
RandomSourceHistoryCallAllowed bool
}
func DefaultTransactionInfoParams() TransactionInfoParams {
// NOTE: TxIndex, TxId and TxBody are populated by NewTransactionEnv rather
// than by Context.
return TransactionInfoParams{
TransactionFeesEnabled: false,
LimitAccountStorage: false,
RandomSourceHistoryCallAllowed: false,
}
}
// TransactionInfo exposes information associated with the executing
// transaction.
//
// Note that scripts have no associated transaction information, but must expose
// the API in compliance with the runtime environment interface.
type TransactionInfo interface {
TxIndex() uint32
TxID() flow.Identifier
TransactionFeesEnabled() bool
LimitAccountStorage() bool
IsServiceAccountAuthorizer() bool
// Cadence's runtime API. Note that the script variant will return
// OperationNotSupportedError.
GetSigningAccounts() ([]common.Address, error)
}
type ParseRestrictedTransactionInfo struct {
txnState state.NestedTransactionPreparer
impl TransactionInfo
}
func NewParseRestrictedTransactionInfo(
txnState state.NestedTransactionPreparer,
impl TransactionInfo,
) TransactionInfo {
return ParseRestrictedTransactionInfo{
txnState: txnState,
impl: impl,
}
}
func (info ParseRestrictedTransactionInfo) TxIndex() uint32 {
return info.impl.TxIndex()
}
func (info ParseRestrictedTransactionInfo) TxID() flow.Identifier {
return info.impl.TxID()
}
func (info ParseRestrictedTransactionInfo) TransactionFeesEnabled() bool {
return info.impl.TransactionFeesEnabled()
}
func (info ParseRestrictedTransactionInfo) LimitAccountStorage() bool {
return info.impl.LimitAccountStorage()
}
func (info ParseRestrictedTransactionInfo) IsServiceAccountAuthorizer() bool {
return info.impl.IsServiceAccountAuthorizer()
}
func (info ParseRestrictedTransactionInfo) GetSigningAccounts() (
[]common.Address,
error,
) {
return parseRestrict1Ret(
info.txnState,
trace.FVMEnvGetSigningAccounts,
info.impl.GetSigningAccounts)
}
var _ TransactionInfo = &transactionInfo{}
type transactionInfo struct {
params TransactionInfoParams
tracer tracing.TracerSpan
runtimeAuthorizers []common.Address
isServiceAccountAuthorizer bool
}
func NewTransactionInfo(
params TransactionInfoParams,
tracer tracing.TracerSpan,
serviceAccount flow.Address,
) TransactionInfo {
isServiceAccountAuthorizer := false
runtimeAddresses := make(
[]common.Address,
0,
len(params.TxBody.Authorizers))
for _, auth := range params.TxBody.Authorizers {
runtimeAddresses = append(
runtimeAddresses,
common.MustBytesToAddress(auth.Bytes()))
if auth == serviceAccount {
isServiceAccountAuthorizer = true
}
}
return &transactionInfo{
params: params,
tracer: tracer,
runtimeAuthorizers: runtimeAddresses,
isServiceAccountAuthorizer: isServiceAccountAuthorizer,
}
}
func (info *transactionInfo) TxIndex() uint32 {
return info.params.TxIndex
}
func (info *transactionInfo) TxID() flow.Identifier {
return info.params.TxId
}
func (info *transactionInfo) TransactionFeesEnabled() bool {
return info.params.TransactionFeesEnabled
}
func (info *transactionInfo) LimitAccountStorage() bool {
return info.params.LimitAccountStorage
}
func (info *transactionInfo) IsServiceAccountAuthorizer() bool {
return info.isServiceAccountAuthorizer
}
func (info *transactionInfo) GetSigningAccounts() ([]common.Address, error) {
defer info.tracer.StartExtensiveTracingChildSpan(
trace.FVMEnvGetSigningAccounts).End()
return info.runtimeAuthorizers, nil
}
var _ TransactionInfo = NoTransactionInfo{}
// Scripts have no associated transaction information.
type NoTransactionInfo struct {
}
func (NoTransactionInfo) TxIndex() uint32 {
return 0
}
func (NoTransactionInfo) TxID() flow.Identifier {
return flow.ZeroID
}
func (NoTransactionInfo) TransactionFeesEnabled() bool {
return false
}
func (NoTransactionInfo) LimitAccountStorage() bool {
return false
}
func (NoTransactionInfo) IsServiceAccountAuthorizer() bool {
return false
}
func (NoTransactionInfo) GetSigningAccounts() ([]common.Address, error) {
return nil, errors.NewOperationNotSupportedError("GetSigningAccounts")
}