-
Notifications
You must be signed in to change notification settings - Fork 175
/
emulator.go
281 lines (244 loc) · 7.38 KB
/
emulator.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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
package emulator
import (
"math/big"
gethCommon "github.com/ethereum/go-ethereum/common"
gethCore "github.com/ethereum/go-ethereum/core"
gethTypes "github.com/ethereum/go-ethereum/core/types"
gethVM "github.com/ethereum/go-ethereum/core/vm"
gethCrypto "github.com/ethereum/go-ethereum/crypto"
"github.com/onflow/atree"
"github.com/onflow/flow-go/fvm/evm/emulator/state"
"github.com/onflow/flow-go/fvm/evm/types"
"github.com/onflow/flow-go/model/flow"
)
// Emulator handles operations against evm runtime
type Emulator struct {
rootAddr flow.Address
ledger atree.Ledger
}
var _ types.Emulator = &Emulator{}
// NewEmulator constructs a new EVM Emulator
func NewEmulator(
ledger atree.Ledger,
rootAddr flow.Address,
) *Emulator {
return &Emulator{
rootAddr: rootAddr,
ledger: ledger,
}
}
func newConfig(ctx types.BlockContext) *Config {
return NewConfig(
WithBlockNumber(new(big.Int).SetUint64(ctx.BlockNumber)),
WithCoinbase(ctx.GasFeeCollector.ToCommon()),
WithDirectCallBaseGasUsage(ctx.DirectCallBaseGasUsage),
)
}
// NewReadOnlyBlockView constructs a new readonly block view
func (em *Emulator) NewReadOnlyBlockView(ctx types.BlockContext) (types.ReadOnlyBlockView, error) {
execState, err := state.NewStateDB(em.ledger, em.rootAddr)
return &ReadOnlyBlockView{
state: execState,
}, err
}
// NewBlockView constructs a new block view (mutable)
func (em *Emulator) NewBlockView(ctx types.BlockContext) (types.BlockView, error) {
cfg := newConfig(ctx)
return &BlockView{
config: cfg,
rootAddr: em.rootAddr,
ledger: em.ledger,
}, nil
}
// ReadOnlyBlockView provides a read only view of a block
// could be used multiple times for queries
type ReadOnlyBlockView struct {
state types.StateDB
}
// BalanceOf returns the balance of the given address
func (bv *ReadOnlyBlockView) BalanceOf(address types.Address) (*big.Int, error) {
return bv.state.GetBalance(address.ToCommon()), nil
}
// CodeOf returns the code of the given address
func (bv *ReadOnlyBlockView) CodeOf(address types.Address) (types.Code, error) {
return bv.state.GetCode(address.ToCommon()), nil
}
// NonceOf returns the nonce of the given address
func (bv *ReadOnlyBlockView) NonceOf(address types.Address) (uint64, error) {
return bv.state.GetNonce(address.ToCommon()), nil
}
// BlockView allows mutation of the evm state as part of a block
//
// TODO: allow multiple calls per block view
// TODO: add block level commit (separation of trie commit to storage)
type BlockView struct {
config *Config
rootAddr flow.Address
ledger atree.Ledger
}
// DirectCall executes a direct call
func (bl *BlockView) DirectCall(call *types.DirectCall) (*types.Result, error) {
proc, err := bl.newProcedure()
if err != nil {
return nil, err
}
var res *types.Result
switch call.SubType {
case types.DepositCallSubType:
res, err = proc.mintTo(call.To, call.Value)
case types.WithdrawCallSubType:
res, err = proc.withdrawFrom(call.From, call.Value)
default:
res, err = proc.run(call.Message(), types.DirectCallTxType)
}
return res, err
}
// RunTransaction runs an evm transaction
func (bl *BlockView) RunTransaction(
tx *gethTypes.Transaction,
) (*types.Result, error) {
var err error
proc, err := bl.newProcedure()
if err != nil {
return nil, err
}
msg, err := gethCore.TransactionToMessage(tx, GetSigner(bl.config), proc.config.BlockContext.BaseFee)
if err != nil {
// note that this is not a fatal error (e.g. due to bad signature)
// not a valid transaction
return nil, types.NewEVMValidationError(err)
}
// update tx context origin
proc.evm.TxContext.Origin = msg.From
res, err := proc.run(msg, tx.Type())
return res, err
}
func (bl *BlockView) newProcedure() (*procedure, error) {
execState, err := state.NewStateDB(bl.ledger, bl.rootAddr)
if err != nil {
return nil, err
}
cfg := bl.config
return &procedure{
config: cfg,
evm: gethVM.NewEVM(
*cfg.BlockContext,
*cfg.TxContext,
execState,
cfg.ChainConfig,
cfg.EVMConfig,
),
state: execState,
}, nil
}
type procedure struct {
config *Config
evm *gethVM.EVM
state types.StateDB
}
// commit commits the changes to the state.
func (proc *procedure) commit() error {
return handleCommitError(proc.state.Commit())
}
func handleCommitError(err error) error {
if err == nil {
return nil
}
// if known types (state errors) don't do anything and return
if types.IsAFatalError(err) || types.IsAStateError(err) {
return err
}
// else is a new fatal error
return types.NewFatalError(err)
}
func (proc *procedure) mintTo(address types.Address, amount *big.Int) (*types.Result, error) {
addr := address.ToCommon()
res := &types.Result{
GasConsumed: proc.config.DirectCallBaseGasUsage,
TxType: types.DirectCallTxType,
}
// create account if not exist
if !proc.state.Exist(addr) {
proc.state.CreateAccount(addr)
}
// add balance
proc.state.AddBalance(addr, amount)
// we don't need to increment any nonce, given the origin doesn't exist
return res, proc.commit()
}
func (proc *procedure) withdrawFrom(address types.Address, amount *big.Int) (*types.Result, error) {
addr := address.ToCommon()
res := &types.Result{
GasConsumed: proc.config.DirectCallBaseGasUsage,
TxType: types.DirectCallTxType,
}
// check if account exists
// while this method is only called from bridged accounts
// it might be the case that someone creates a bridged account
// and never transfer tokens to and call for withdraw
// TODO: we might revisit this apporach and
// return res, types.ErrAccountDoesNotExist
// instead
if !proc.state.Exist(addr) {
proc.state.CreateAccount(addr)
}
// check the source account balance
// if balance is lower than amount needed for withdrawal, error out
if proc.state.GetBalance(addr).Cmp(amount) < 0 {
return res, types.ErrInsufficientBalance
}
// sub balance
proc.state.SubBalance(addr, amount)
// we increment the nonce for source account cause
// withdraw counts as a transaction
nonce := proc.state.GetNonce(addr)
proc.state.SetNonce(addr, nonce+1)
return res, proc.commit()
}
func (proc *procedure) run(msg *gethCore.Message, txType uint8) (*types.Result, error) {
res := types.Result{
TxType: txType,
}
gasPool := (*gethCore.GasPool)(&proc.config.BlockContext.GasLimit)
execResult, err := gethCore.NewStateTransition(
proc.evm,
msg,
gasPool,
).TransitionDb()
if err != nil {
res.Failed = true
// if the error is a fatal error or a non-fatal state error return it
if types.IsAFatalError(err) || types.IsAStateError(err) {
return &res, err
}
// otherwise is a validation error (pre-check failure)
// no state change, wrap the error and return
return &res, types.NewEVMValidationError(err)
}
// if prechecks are passed, the exec result won't be nil
if execResult != nil {
res.GasConsumed = execResult.UsedGas
if !execResult.Failed() { // collect vm errors
res.ReturnedValue = execResult.ReturnData
// If the transaction created a contract, store the creation address in the receipt.
if msg.To == nil {
res.DeployedContractAddress = types.NewAddress(gethCrypto.CreateAddress(msg.From, msg.Nonce))
}
res.Logs = proc.state.Logs(
// TODO pass proper hash values
gethCommon.Hash{},
proc.config.BlockContext.BlockNumber.Uint64(),
gethCommon.Hash{},
0,
)
} else {
res.Failed = true
err = types.NewEVMExecutionError(execResult.Err)
}
}
commitErr := proc.commit()
if commitErr != nil {
return &res, commitErr
}
return &res, err
}