-
Notifications
You must be signed in to change notification settings - Fork 208
Expand file tree
/
Copy pathscripts.go
More file actions
218 lines (186 loc) · 8.09 KB
/
scripts.go
File metadata and controls
218 lines (186 loc) · 8.09 KB
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
package execution
import (
"context"
"github.com/rs/zerolog"
"github.com/onflow/flow-go/engine/execution/computation"
"github.com/onflow/flow-go/engine/execution/computation/query"
"github.com/onflow/flow-go/fvm"
"github.com/onflow/flow-go/fvm/environment"
"github.com/onflow/flow-go/fvm/storage/derived"
"github.com/onflow/flow-go/fvm/storage/snapshot"
"github.com/onflow/flow-go/model/flow"
"github.com/onflow/flow-go/module"
"github.com/onflow/flow-go/state/protocol"
"github.com/onflow/flow-go/storage"
)
// RegisterAtHeight returns register value for provided register ID at the block height.
// Even if the register wasn't indexed at the provided height, returns the highest height the register was indexed at.
// If the register with the ID was not indexed at all return nil value and no error.
// Expected errors:
// - storage.ErrHeightNotIndexed if the given height was not indexed yet or lower than the first indexed height.
type RegisterAtHeight func(ID flow.RegisterID, height uint64) (flow.RegisterValue, error)
type ScriptExecutor interface {
// ExecuteAtBlockHeight executes provided script against the block height.
// A result value is returned encoded as byte array. An error will be returned if script
// doesn't successfully execute.
// Expected errors:
// - storage.ErrNotFound if block or register value at height was not found.
// - storage.ErrHeightNotIndexed if the data for the block height is not available
ExecuteAtBlockHeight(
ctx context.Context,
script []byte,
arguments [][]byte,
height uint64,
) ([]byte, error)
// GetAccountAtBlockHeight returns a Flow account by the provided address and block height.
// Expected errors:
// - storage.ErrHeightNotIndexed if the data for the block height is not available
GetAccountAtBlockHeight(ctx context.Context, address flow.Address, height uint64) (*flow.Account, error)
// GetAccountBalance returns a Flow account balance by the provided address and block height.
// Expected errors:
// - storage.ErrHeightNotIndexed if the data for the block height is not available
GetAccountBalance(ctx context.Context, address flow.Address, height uint64) (uint64, error)
// GetAccountAvailableBalance returns a Flow account available balance by the provided address and block height.
// Expected errors:
// - storage.ErrHeightNotIndexed if the data for the block height is not available
GetAccountAvailableBalance(ctx context.Context, address flow.Address, height uint64) (uint64, error)
// GetAccountKeys returns a Flow account public keys by the provided address and block height.
// Expected errors:
// - storage.ErrHeightNotIndexed if the data for the block height is not available
GetAccountKeys(ctx context.Context, address flow.Address, height uint64) ([]flow.AccountPublicKey, error)
// GetAccountKey returns a Flow account public key by the provided address, block height and index.
// Expected errors:
// - storage.ErrHeightNotIndexed if the data for the block height is not available
GetAccountKey(ctx context.Context, address flow.Address, keyIndex uint32, height uint64) (*flow.AccountPublicKey, error)
}
var _ ScriptExecutor = (*Scripts)(nil)
type Scripts struct {
executor *query.QueryExecutor
headers storage.Headers
registerAtHeight RegisterAtHeight
}
func NewScripts(
log zerolog.Logger,
metrics module.ExecutionMetrics,
chainID flow.ChainID,
protocolSnapshotProvider protocol.SnapshotExecutionSubsetProvider,
header storage.Headers,
registerAtHeight RegisterAtHeight,
queryConf query.QueryConfig,
derivedChainData *derived.DerivedChainData,
enableProgramCacheWrites bool,
) *Scripts {
vm := fvm.NewVirtualMachine()
options := computation.DefaultFVMOptions(
chainID,
false,
true,
)
blocks := environment.NewBlockFinder(header)
options = append(options, fvm.WithBlocks(blocks)) // add blocks for getBlocks calls in scripts
options = append(options, fvm.WithMetricsReporter(metrics))
options = append(options, fvm.WithAllowProgramCacheWritesInScriptsEnabled(enableProgramCacheWrites))
vmCtx := fvm.NewContext(options...)
queryExecutor := query.NewQueryExecutor(
queryConf,
log,
metrics,
vm,
vmCtx,
derivedChainData,
protocolSnapshotProvider,
)
return &Scripts{
executor: queryExecutor,
headers: header,
registerAtHeight: registerAtHeight,
}
}
// ExecuteAtBlockHeight executes provided script against the block height.
// A result value is returned encoded as byte array. An error will be returned if script
// doesn't successfully execute.
// Expected errors:
// - Script execution related errors
// - storage.ErrHeightNotIndexed if the data for the block height is not available
func (s *Scripts) ExecuteAtBlockHeight(
ctx context.Context,
script []byte,
arguments [][]byte,
height uint64,
) ([]byte, error) {
snap, header, err := s.snapshotWithBlock(height)
if err != nil {
return nil, err
}
value, compUsage, err := s.executor.ExecuteScript(ctx, script, arguments, header, snap)
// TODO: return compUsage when upstream can handle it
_ = compUsage
return value, err
}
// GetAccountAtBlockHeight returns a Flow account by the provided address and block height.
// Expected errors:
// - Script execution related errors
// - storage.ErrHeightNotIndexed if the data for the block height is not available
func (s *Scripts) GetAccountAtBlockHeight(ctx context.Context, address flow.Address, height uint64) (*flow.Account, error) {
snap, header, err := s.snapshotWithBlock(height)
if err != nil {
return nil, err
}
return s.executor.GetAccount(ctx, address, header, snap)
}
// GetAccountBalance returns a balance of Flow account by the provided address and block height.
// Expected errors:
// - Script execution related errors
// - storage.ErrHeightNotIndexed if the data for the block height is not available
func (s *Scripts) GetAccountBalance(ctx context.Context, address flow.Address, height uint64) (uint64, error) {
snap, header, err := s.snapshotWithBlock(height)
if err != nil {
return 0, err
}
return s.executor.GetAccountBalance(ctx, address, header, snap)
}
// GetAccountAvailableBalance returns an available balance of Flow account by the provided address and block height.
// Expected errors:
// - Script execution related errors
// - storage.ErrHeightNotIndexed if the data for the block height is not available
func (s *Scripts) GetAccountAvailableBalance(ctx context.Context, address flow.Address, height uint64) (uint64, error) {
snap, header, err := s.snapshotWithBlock(height)
if err != nil {
return 0, err
}
return s.executor.GetAccountAvailableBalance(ctx, address, header, snap)
}
// GetAccountKeys returns a public keys of Flow account by the provided address and block height.
// Expected errors:
// - Script execution related errors
// - storage.ErrHeightNotIndexed if the data for the block height is not available
func (s *Scripts) GetAccountKeys(ctx context.Context, address flow.Address, height uint64) ([]flow.AccountPublicKey, error) {
snap, header, err := s.snapshotWithBlock(height)
if err != nil {
return nil, err
}
return s.executor.GetAccountKeys(ctx, address, header, snap)
}
// GetAccountKey returns a public key of Flow account by the provided address, block height and index.
// Expected errors:
// - Script execution related errors
// - storage.ErrHeightNotIndexed if the data for the block height is not available
func (s *Scripts) GetAccountKey(ctx context.Context, address flow.Address, keyIndex uint32, height uint64) (*flow.AccountPublicKey, error) {
snap, header, err := s.snapshotWithBlock(height)
if err != nil {
return nil, err
}
return s.executor.GetAccountKey(ctx, address, keyIndex, header, snap)
}
// snapshotWithBlock is a common function for executing scripts and get account functionality.
// It creates a storage snapshot that is needed by the FVM to execute scripts.
func (s *Scripts) snapshotWithBlock(height uint64) (snapshot.StorageSnapshot, *flow.Header, error) {
header, err := s.headers.ByHeight(height)
if err != nil {
return nil, nil, err
}
storageSnapshot := snapshot.NewReadFuncStorageSnapshot(func(ID flow.RegisterID) (flow.RegisterValue, error) {
return s.registerAtHeight(ID, height)
})
return storageSnapshot, header, nil
}