@@ -34,20 +34,6 @@ import (
3434 "github.com/ethereum/go-ethereum/rpc"
3535)
3636
37- // Backend is a simulated blockchain. You can use it to test your contracts or
38- // other code that interacts with the Ethereum chain.
39- type Backend struct {
40- eth * eth.Ethereum
41- beacon * catalyst.SimulatedBeacon
42- client simClient
43- }
44-
45- // simClient wraps ethclient. This exists to prevent extracting ethclient.Client
46- // from the Client interface returned by Backend.
47- type simClient struct {
48- * ethclient.Client
49- }
50-
5137// Client exposes the methods provided by the Ethereum RPC client.
5238type Client interface {
5339 ethereum.BlockNumberReader
@@ -66,70 +52,81 @@ type Client interface {
6652 ethereum.ChainIDReader
6753}
6854
69- // New creates a new binding backend using a simulated blockchain
70- // for testing purposes.
55+ // simClient wraps ethclient. This exists to prevent extracting ethclient.Client
56+ // from the Client interface returned by Backend.
57+ type simClient struct {
58+ * ethclient.Client
59+ }
60+
61+ // Backend is a simulated blockchain. You can use it to test your contracts or
62+ // other code that interacts with the Ethereum chain.
63+ type Backend struct {
64+ eth * eth.Ethereum
65+ beacon * catalyst.SimulatedBeacon
66+ client simClient
67+ }
68+
69+ // NewBackend creates a new simulated blockchain that can be used as a backend for
70+ // contract bindings in unit tests.
71+ //
7172// A simulated backend always uses chainID 1337.
72- func New (alloc core.GenesisAlloc , gasLimit uint64 ) * Backend {
73- // Setup the node object
73+ func NewBackend (alloc core.GenesisAlloc , options ... func (nodeConf * node.Config , ethConf * ethconfig.Config )) * Backend {
74+ // Create the default configurations for the outer node shell and the Ethereum
75+ // service to mutate with the options afterwards
7476 nodeConf := node .DefaultConfig
7577 nodeConf .DataDir = ""
7678 nodeConf .P2P = p2p.Config {NoDiscovery : true }
77- stack , err := node .New (& nodeConf )
78- if err != nil {
79- // This should never happen, if it does, please open an issue
80- panic (err )
81- }
8279
83- // Setup ethereum
84- genesis := core.Genesis {
80+ ethConf := ethconfig . Defaults
81+ ethConf . Genesis = & core.Genesis {
8582 Config : params .AllDevChainProtocolChanges ,
86- GasLimit : gasLimit ,
83+ GasLimit : ethconfig . Defaults . Miner . GasCeil ,
8784 Alloc : alloc ,
8885 }
89- conf := ethconfig .Defaults
90- conf .Genesis = & genesis
91- conf .SyncMode = downloader .FullSync
92- conf .TxPool .NoLocals = true
93- sim , err := newWithNode (stack , & conf , 0 )
86+ ethConf .SyncMode = downloader .FullSync
87+ ethConf .TxPool .NoLocals = true
88+
89+ for _ , option := range options {
90+ option (& nodeConf , & ethConf )
91+ }
92+ // Assemble the Ethereum stack to run the chain with
93+ stack , err := node .New (& nodeConf )
94+ if err != nil {
95+ panic (err ) // this should never happen
96+ }
97+ sim , err := newWithNode (stack , & ethConf , 0 )
9498 if err != nil {
95- // This should never happen, if it does, please open an issue
96- panic (err )
99+ panic (err ) // this should never happen
97100 }
98101 return sim
99102}
100103
101- // newWithNode sets up a simulated backend on an existing node
102- // this allows users to do persistent simulations.
103- // The provided node must not be started and will be started by newWithNode
104+ // newWithNode sets up a simulated backend on an existing node. The provided node
105+ // must not be started and will be started by this method.
104106func newWithNode (stack * node.Node , conf * eth.Config , blockPeriod uint64 ) (* Backend , error ) {
105107 backend , err := eth .New (stack , conf )
106108 if err != nil {
107109 return nil , err
108110 }
109-
110111 // Register the filter system
111112 filterSystem := filters .NewFilterSystem (backend .APIBackend , filters.Config {})
112113 stack .RegisterAPIs ([]rpc.API {{
113114 Namespace : "eth" ,
114115 Service : filters .NewFilterAPI (filterSystem , false ),
115116 }})
116-
117117 // Start the node
118118 if err := stack .Start (); err != nil {
119119 return nil , err
120120 }
121-
122121 // Set up the simulated beacon
123122 beacon , err := catalyst .NewSimulatedBeacon (blockPeriod , backend )
124123 if err != nil {
125124 return nil , err
126125 }
127-
128126 // Reorg our chain back to genesis
129127 if err := beacon .Fork (backend .BlockChain ().GetCanonicalHash (0 )); err != nil {
130128 return nil , err
131129 }
132-
133130 return & Backend {
134131 eth : backend ,
135132 beacon : beacon ,
0 commit comments