-
Notifications
You must be signed in to change notification settings - Fork 177
/
emulator.go
65 lines (54 loc) · 1.9 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
package types
import (
"math/big"
gethTypes "github.com/ethereum/go-ethereum/core/types"
)
var (
DefaultDirectCallBaseGasUsage = uint64(21_000)
DefaultDirectCallGasPrice = uint64(0)
// anything block number above 0 works here
BlockNumberForEVMRules = big.NewInt(1)
)
// BlockContext holds the context needed for the emulator operations
type BlockContext struct {
BlockNumber uint64
DirectCallBaseGasUsage uint64
DirectCallGasPrice uint64
GasFeeCollector Address
}
// NewDefaultBlockContext returns a new default block context
func NewDefaultBlockContext(BlockNumber uint64) BlockContext {
return BlockContext{
BlockNumber: BlockNumber,
DirectCallBaseGasUsage: DefaultDirectCallBaseGasUsage,
DirectCallGasPrice: DefaultDirectCallGasPrice,
}
}
// ReadOnlyBlockView provides a read only view of a block
type ReadOnlyBlockView interface {
// BalanceOf returns the balance of this address
BalanceOf(address Address) (*big.Int, error)
// NonceOf returns the nonce of this address
NonceOf(address Address) (uint64, error)
// CodeOf returns the code for this address (if smart contract is deployed at this address)
CodeOf(address Address) (Code, error)
}
// BlockView facilitates execution of a transaction or a direct evm call in the context of a block
// Errors returned by the methods are one of the followings:
// - Fatal error
// - Database error (non-fatal)
// - EVM validation error
// - EVM execution error
type BlockView interface {
// executes a direct call
DirectCall(call *DirectCall) (*Result, error)
// RunTransaction executes an evm transaction
RunTransaction(tx *gethTypes.Transaction) (*Result, error)
}
// Emulator emulates an evm-compatible chain
type Emulator interface {
// constructs a new block view
NewReadOnlyBlockView(ctx BlockContext) (ReadOnlyBlockView, error)
// constructs a new block
NewBlockView(ctx BlockContext) (BlockView, error)
}