mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-08-20 10:52:25 +00:00
ethclient/simulated: move backend to new package
This commit is contained in:
parent
2af45491ae
commit
4978688a75
3 changed files with 181 additions and 146 deletions
|
|
@ -17,152 +17,19 @@
|
||||||
package backends
|
package backends
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
|
||||||
"math"
|
|
||||||
"time"
|
|
||||||
|
|
||||||
"github.com/ethereum/go-ethereum"
|
|
||||||
"github.com/ethereum/go-ethereum/accounts/abi/bind"
|
|
||||||
"github.com/ethereum/go-ethereum/common"
|
|
||||||
"github.com/ethereum/go-ethereum/core"
|
"github.com/ethereum/go-ethereum/core"
|
||||||
"github.com/ethereum/go-ethereum/eth"
|
"github.com/ethereum/go-ethereum/ethclient/simulated"
|
||||||
"github.com/ethereum/go-ethereum/eth/catalyst"
|
|
||||||
"github.com/ethereum/go-ethereum/eth/downloader"
|
|
||||||
"github.com/ethereum/go-ethereum/eth/ethconfig"
|
|
||||||
"github.com/ethereum/go-ethereum/eth/filters"
|
|
||||||
"github.com/ethereum/go-ethereum/ethclient"
|
|
||||||
"github.com/ethereum/go-ethereum/node"
|
|
||||||
"github.com/ethereum/go-ethereum/p2p"
|
|
||||||
"github.com/ethereum/go-ethereum/params"
|
|
||||||
"github.com/ethereum/go-ethereum/rpc"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
var _ bind.ContractBackend = (SimulatedBackend)(nil)
|
type SimulatedBackend struct {
|
||||||
var _ bind.ContractBackend = (*simBackend)(nil)
|
simulated.Backend
|
||||||
|
|
||||||
type SimChainManagement interface {
|
|
||||||
// Commit seals a block and moves the chain forward to a new empty block.
|
|
||||||
Commit() common.Hash
|
|
||||||
|
|
||||||
// Rollback un-sends previously added transactions.
|
|
||||||
Rollback()
|
|
||||||
|
|
||||||
// Fork sets the head to a new block, which is based on the provided parentHash.
|
|
||||||
Fork(ctx context.Context, parentHash common.Hash) error
|
|
||||||
|
|
||||||
// AdjustTime changes the block timestamp.
|
|
||||||
AdjustTime(adjustment time.Duration) error
|
|
||||||
|
|
||||||
// Close closes the backend. You need to call this to clean up resources.
|
|
||||||
Close() error
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// SimulatedBackend all interfaces in the ethereum package, but is based on a
|
// New creates a new binding backend using a simulated blockchain
|
||||||
// simulated blockchain. It is intended for testing purposes.
|
|
||||||
type SimulatedBackend interface {
|
|
||||||
SimChainManagement
|
|
||||||
|
|
||||||
// The backend implements all interfaces in the ethereum package.
|
|
||||||
ethereum.BlockNumberReader
|
|
||||||
ethereum.ChainReader
|
|
||||||
ethereum.ChainStateReader
|
|
||||||
ethereum.ContractCaller
|
|
||||||
ethereum.GasEstimator
|
|
||||||
ethereum.GasPricer
|
|
||||||
ethereum.GasPricer1559
|
|
||||||
ethereum.LogFilterer
|
|
||||||
ethereum.PendingStateReader
|
|
||||||
ethereum.PendingContractCaller
|
|
||||||
ethereum.TransactionReader
|
|
||||||
ethereum.TransactionSender
|
|
||||||
ethereum.ChainIDReader
|
|
||||||
}
|
|
||||||
|
|
||||||
type simBackend struct {
|
|
||||||
eth *eth.Ethereum
|
|
||||||
*catalyst.SimulatedBeacon
|
|
||||||
*ethclient.Client
|
|
||||||
}
|
|
||||||
|
|
||||||
// NewSimulatedBackend creates a new binding backend using a simulated blockchain
|
|
||||||
// for testing purposes.
|
// for testing purposes.
|
||||||
// A simulated backend always uses chainID 1337.
|
// A simulated backend always uses chainID 1337.
|
||||||
|
//
|
||||||
|
// Deprecated: please use simulated.Backend instead
|
||||||
func NewSimulatedBackend(alloc core.GenesisAlloc, gasLimit uint64) SimulatedBackend {
|
func NewSimulatedBackend(alloc core.GenesisAlloc, gasLimit uint64) SimulatedBackend {
|
||||||
// Setup the node object
|
return SimulatedBackend{simulated.New(alloc, gasLimit)}
|
||||||
nodeConf := node.DefaultConfig
|
|
||||||
nodeConf.DataDir = ""
|
|
||||||
nodeConf.P2P = p2p.Config{DiscAddr: "", ListenAddr: ""}
|
|
||||||
stack, err := node.New(&nodeConf)
|
|
||||||
if err != nil {
|
|
||||||
// This should never happen, if it does, please open an issue
|
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Setup ethereum
|
|
||||||
genesis := core.Genesis{
|
|
||||||
Config: params.AllDevChainProtocolChanges,
|
|
||||||
GasLimit: gasLimit,
|
|
||||||
Alloc: alloc,
|
|
||||||
}
|
|
||||||
conf := ethconfig.Defaults
|
|
||||||
conf.Genesis = &genesis
|
|
||||||
conf.SyncMode = downloader.FullSync
|
|
||||||
sim, err := NewSimWithNode(stack, &conf, math.MaxUint64)
|
|
||||||
if err != nil {
|
|
||||||
// This should never happen, if it does, please open an issue
|
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
return sim
|
|
||||||
}
|
|
||||||
|
|
||||||
// NewSimWithNode sets up a simulated backend on an existing node
|
|
||||||
// this allows users to do persistent simulations.
|
|
||||||
// The provided node must not be started and will be started by NewSimWithNode
|
|
||||||
func NewSimWithNode(stack *node.Node, conf *eth.Config, blockPeriod uint64) (SimulatedBackend, error) {
|
|
||||||
backend, err := eth.New(stack, conf)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
// Register the filter system
|
|
||||||
filterSystem := filters.NewFilterSystem(backend.APIBackend, filters.Config{})
|
|
||||||
stack.RegisterAPIs([]rpc.API{{
|
|
||||||
Namespace: "eth",
|
|
||||||
Service: filters.NewFilterAPI(filterSystem, false),
|
|
||||||
}})
|
|
||||||
|
|
||||||
// Start the node
|
|
||||||
if err := stack.Start(); err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
// Set up the simulated beacon
|
|
||||||
beacon, err := catalyst.NewSimulatedBeacon(blockPeriod, backend)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
// Reorg our chain back to genesis
|
|
||||||
if err := beacon.Fork(context.Background(), backend.BlockChain().GetCanonicalHash(0)); err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
return &simBackend{
|
|
||||||
eth: backend,
|
|
||||||
SimulatedBeacon: beacon,
|
|
||||||
Client: ethclient.NewClient(stack.Attach()),
|
|
||||||
}, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (n *simBackend) Close() error {
|
|
||||||
if n.Client != nil {
|
|
||||||
n.Client.Close()
|
|
||||||
n.Client = nil
|
|
||||||
}
|
|
||||||
if n.SimulatedBeacon != nil {
|
|
||||||
err := n.SimulatedBeacon.Stop()
|
|
||||||
n.SimulatedBeacon = nil
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
168
ethclient/simulated/backend.go
Normal file
168
ethclient/simulated/backend.go
Normal file
|
|
@ -0,0 +1,168 @@
|
||||||
|
// Copyright 2023 The go-ethereum Authors
|
||||||
|
// This file is part of the go-ethereum library.
|
||||||
|
//
|
||||||
|
// The go-ethereum library is free software: you can redistribute it and/or modify
|
||||||
|
// it under the terms of the GNU Lesser General Public License as published by
|
||||||
|
// the Free Software Foundation, either version 3 of the License, or
|
||||||
|
// (at your option) any later version.
|
||||||
|
//
|
||||||
|
// The go-ethereum library is distributed in the hope that it will be useful,
|
||||||
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
// GNU Lesser General Public License for more details.
|
||||||
|
//
|
||||||
|
// You should have received a copy of the GNU Lesser General Public License
|
||||||
|
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
|
package simulated
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"math"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/ethereum/go-ethereum"
|
||||||
|
"github.com/ethereum/go-ethereum/accounts/abi/bind"
|
||||||
|
"github.com/ethereum/go-ethereum/common"
|
||||||
|
"github.com/ethereum/go-ethereum/core"
|
||||||
|
"github.com/ethereum/go-ethereum/eth"
|
||||||
|
"github.com/ethereum/go-ethereum/eth/catalyst"
|
||||||
|
"github.com/ethereum/go-ethereum/eth/downloader"
|
||||||
|
"github.com/ethereum/go-ethereum/eth/ethconfig"
|
||||||
|
"github.com/ethereum/go-ethereum/eth/filters"
|
||||||
|
"github.com/ethereum/go-ethereum/ethclient"
|
||||||
|
"github.com/ethereum/go-ethereum/node"
|
||||||
|
"github.com/ethereum/go-ethereum/p2p"
|
||||||
|
"github.com/ethereum/go-ethereum/params"
|
||||||
|
"github.com/ethereum/go-ethereum/rpc"
|
||||||
|
)
|
||||||
|
|
||||||
|
var _ bind.ContractBackend = (Backend)(nil)
|
||||||
|
var _ bind.ContractBackend = (*simBackend)(nil)
|
||||||
|
|
||||||
|
type SimChainManagement interface {
|
||||||
|
// Commit seals a block and moves the chain forward to a new empty block.
|
||||||
|
Commit() common.Hash
|
||||||
|
|
||||||
|
// Rollback un-sends previously added transactions.
|
||||||
|
Rollback()
|
||||||
|
|
||||||
|
// Fork sets the head to a new block, which is based on the provided parentHash.
|
||||||
|
Fork(ctx context.Context, parentHash common.Hash) error
|
||||||
|
|
||||||
|
// AdjustTime changes the block timestamp.
|
||||||
|
AdjustTime(adjustment time.Duration) error
|
||||||
|
|
||||||
|
// Close closes the backend. You need to call this to clean up resources.
|
||||||
|
Close() error
|
||||||
|
}
|
||||||
|
|
||||||
|
// Backend all interfaces in the ethereum package, but is based on a
|
||||||
|
// simulated blockchain. It is intended for testing purposes.
|
||||||
|
type Backend interface {
|
||||||
|
SimChainManagement
|
||||||
|
|
||||||
|
// The backend implements all interfaces in the ethereum package.
|
||||||
|
ethereum.BlockNumberReader
|
||||||
|
ethereum.ChainReader
|
||||||
|
ethereum.ChainStateReader
|
||||||
|
ethereum.ContractCaller
|
||||||
|
ethereum.GasEstimator
|
||||||
|
ethereum.GasPricer
|
||||||
|
ethereum.GasPricer1559
|
||||||
|
ethereum.LogFilterer
|
||||||
|
ethereum.PendingStateReader
|
||||||
|
ethereum.PendingContractCaller
|
||||||
|
ethereum.TransactionReader
|
||||||
|
ethereum.TransactionSender
|
||||||
|
ethereum.ChainIDReader
|
||||||
|
}
|
||||||
|
|
||||||
|
type simBackend struct {
|
||||||
|
eth *eth.Ethereum
|
||||||
|
*catalyst.SimulatedBeacon
|
||||||
|
*ethclient.Client
|
||||||
|
}
|
||||||
|
|
||||||
|
// New creates a new binding backend using a simulated blockchain
|
||||||
|
// for testing purposes.
|
||||||
|
// A simulated backend always uses chainID 1337.
|
||||||
|
func New(alloc core.GenesisAlloc, gasLimit uint64) Backend {
|
||||||
|
// Setup the node object
|
||||||
|
nodeConf := node.DefaultConfig
|
||||||
|
nodeConf.DataDir = ""
|
||||||
|
nodeConf.P2P = p2p.Config{DiscAddr: "", ListenAddr: ""}
|
||||||
|
stack, err := node.New(&nodeConf)
|
||||||
|
if err != nil {
|
||||||
|
// This should never happen, if it does, please open an issue
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Setup ethereum
|
||||||
|
genesis := core.Genesis{
|
||||||
|
Config: params.AllDevChainProtocolChanges,
|
||||||
|
GasLimit: gasLimit,
|
||||||
|
Alloc: alloc,
|
||||||
|
}
|
||||||
|
conf := ethconfig.Defaults
|
||||||
|
conf.Genesis = &genesis
|
||||||
|
conf.SyncMode = downloader.FullSync
|
||||||
|
sim, err := NewWithNode(stack, &conf, math.MaxUint64)
|
||||||
|
if err != nil {
|
||||||
|
// This should never happen, if it does, please open an issue
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
return sim
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewWithNode sets up a simulated backend on an existing node
|
||||||
|
// this allows users to do persistent simulations.
|
||||||
|
// The provided node must not be started and will be started by NewWithNode
|
||||||
|
func NewWithNode(stack *node.Node, conf *eth.Config, blockPeriod uint64) (Backend, error) {
|
||||||
|
backend, err := eth.New(stack, conf)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
// Register the filter system
|
||||||
|
filterSystem := filters.NewFilterSystem(backend.APIBackend, filters.Config{})
|
||||||
|
stack.RegisterAPIs([]rpc.API{{
|
||||||
|
Namespace: "eth",
|
||||||
|
Service: filters.NewFilterAPI(filterSystem, false),
|
||||||
|
}})
|
||||||
|
|
||||||
|
// Start the node
|
||||||
|
if err := stack.Start(); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
// Set up the simulated beacon
|
||||||
|
beacon, err := catalyst.NewSimulatedBeacon(blockPeriod, backend)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
// Reorg our chain back to genesis
|
||||||
|
if err := beacon.Fork(context.Background(), backend.BlockChain().GetCanonicalHash(0)); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return &simBackend{
|
||||||
|
eth: backend,
|
||||||
|
SimulatedBeacon: beacon,
|
||||||
|
Client: ethclient.NewClient(stack.Attach()),
|
||||||
|
}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (n *simBackend) Close() error {
|
||||||
|
if n.Client != nil {
|
||||||
|
n.Client.Close()
|
||||||
|
n.Client = nil
|
||||||
|
}
|
||||||
|
if n.SimulatedBeacon != nil {
|
||||||
|
err := n.SimulatedBeacon.Stop()
|
||||||
|
n.SimulatedBeacon = nil
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
@ -14,7 +14,7 @@
|
||||||
// You should have received a copy of the GNU Lesser General Public License
|
// You should have received a copy of the GNU Lesser General Public License
|
||||||
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
|
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
package backends
|
package simulated
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
|
@ -36,15 +36,15 @@ var (
|
||||||
testAddr = crypto.PubkeyToAddress(testKey.PublicKey)
|
testAddr = crypto.PubkeyToAddress(testKey.PublicKey)
|
||||||
)
|
)
|
||||||
|
|
||||||
func simTestBackend(testAddr common.Address) SimulatedBackend {
|
func simTestBackend(testAddr common.Address) Backend {
|
||||||
return NewSimulatedBackend(
|
return New(
|
||||||
core.GenesisAlloc{
|
core.GenesisAlloc{
|
||||||
testAddr: {Balance: big.NewInt(10000000000000000)},
|
testAddr: {Balance: big.NewInt(10000000000000000)},
|
||||||
}, 10000000,
|
}, 10000000,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
func newTx(sim SimulatedBackend, key *ecdsa.PrivateKey) (*types.Transaction, error) {
|
func newTx(sim Backend, key *ecdsa.PrivateKey) (*types.Transaction, error) {
|
||||||
// create a signed transaction to send
|
// create a signed transaction to send
|
||||||
head, _ := sim.HeaderByNumber(context.Background(), nil) // Should be child's, good enough
|
head, _ := sim.HeaderByNumber(context.Background(), nil) // Should be child's, good enough
|
||||||
gasPrice := new(big.Int).Add(head.BaseFee, big.NewInt(1))
|
gasPrice := new(big.Int).Add(head.BaseFee, big.NewInt(1))
|
||||||
|
|
@ -67,7 +67,7 @@ func newTx(sim SimulatedBackend, key *ecdsa.PrivateKey) (*types.Transaction, err
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestNewSim(t *testing.T) {
|
func TestNewSim(t *testing.T) {
|
||||||
sim := NewSimulatedBackend(core.GenesisAlloc{}, 30_000_000)
|
sim := New(core.GenesisAlloc{}, 30_000_000)
|
||||||
num, err := sim.BlockNumber(context.Background())
|
num, err := sim.BlockNumber(context.Background())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
|
|
@ -87,7 +87,7 @@ func TestNewSim(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestAdjustTime(t *testing.T) {
|
func TestAdjustTime(t *testing.T) {
|
||||||
sim := NewSimulatedBackend(core.GenesisAlloc{}, 10_000_000)
|
sim := New(core.GenesisAlloc{}, 10_000_000)
|
||||||
defer sim.Close()
|
defer sim.Close()
|
||||||
|
|
||||||
block1, _ := sim.BlockByNumber(context.Background(), nil)
|
block1, _ := sim.BlockByNumber(context.Background(), nil)
|
||||||
Loading…
Reference in a new issue