accounts/abi/bind/backends: fix bugs, add basic test

This commit is contained in:
Marius van der Wijden 2023-09-26 12:45:16 +02:00
parent f0b5799572
commit 8887559f9c
2 changed files with 59 additions and 10 deletions

View file

@ -1,10 +1,15 @@
package backends package backends
import ( import (
"context"
"os"
"path/filepath"
"github.com/ethereum/go-ethereum/accounts/abi/bind" "github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/core" "github.com/ethereum/go-ethereum/core"
"github.com/ethereum/go-ethereum/eth" "github.com/ethereum/go-ethereum/eth"
"github.com/ethereum/go-ethereum/eth/catalyst" "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/ethconfig"
"github.com/ethereum/go-ethereum/ethclient" "github.com/ethereum/go-ethereum/ethclient"
"github.com/ethereum/go-ethereum/node" "github.com/ethereum/go-ethereum/node"
@ -19,36 +24,48 @@ type NewSim struct {
} }
func NewNewSim(alloc core.GenesisAlloc) (*NewSim, error) { func NewNewSim(alloc core.GenesisAlloc) (*NewSim, error) {
// Setup the node object
nodeConf := &node.DefaultConfig
nodeConf.IPCPath = "geth.ipc"
nodeConf.DataDir = filepath.Join(os.TempDir(), "simulated-geth")
stack, err := node.New(nodeConf)
if err != nil {
return nil, err
}
// Setup ethereum
genesis := core.Genesis{ genesis := core.Genesis{
Config: params.AllEthashProtocolChanges, Config: params.AllDevChainProtocolChanges,
GasLimit: 0, GasLimit: 30_000_000,
Alloc: alloc, Alloc: alloc,
} }
conf := &ethconfig.Defaults conf := &ethconfig.Defaults
conf.Genesis = &genesis conf.Genesis = &genesis
conf.SyncMode = downloader.FullSync
stack, err := node.New(&node.DefaultConfig)
if err != nil {
return nil, err
}
backend, err := eth.New(stack, conf) backend, err := eth.New(stack, conf)
if err != nil { if err != nil {
return nil, err return nil, err
} }
// Start the node
if err := stack.Start(); err != nil {
return nil, err
}
// Set up the simulated beacon
beacon, err := catalyst.NewSimulatedBeacon(12, backend) beacon, err := catalyst.NewSimulatedBeacon(12, backend)
if err != nil { if err != nil {
return nil, err return nil, err
} }
client, err := ethclient.Dial(stack.IPCEndpoint()) // Reorg our chain back to genesis
if err != nil { if err := beacon.Fork(context.Background(), backend.BlockChain().GetCanonicalHash(0)); err != nil {
return nil, err return nil, err
} }
return &NewSim{ return &NewSim{
SimulatedBeacon: beacon, SimulatedBeacon: beacon,
Client: client, Client: ethclient.NewClient(stack.Attach()),
}, nil }, nil
} }

View file

@ -0,0 +1,32 @@
package backends
import (
"context"
"testing"
"github.com/ethereum/go-ethereum/core"
)
func TestNewSim(t *testing.T) {
genAlloc := make(core.GenesisAlloc)
newSim, err := NewNewSim(genAlloc)
if err != nil {
t.Fatal(err)
}
num, err := newSim.Client.BlockNumber(context.Background())
if err != nil {
t.Fatal(err)
}
if num != 0 {
t.Fatalf("expected 0 got %v", num)
}
// Create a block
newSim.Commit()
num, err = newSim.Client.BlockNumber(context.Background())
if err != nil {
t.Fatal(err)
}
if num != 1 {
t.Fatalf("expected 1 got %v", num)
}
}