Skip to content

Commit

Permalink
changed simulator.go made it full copy of single file backend.go simu…
Browse files Browse the repository at this point in the history
…lated package. updated eth tests to be compatible with it
  • Loading branch information
AKorpusenko committed Aug 27, 2024
1 parent ebfdf6a commit 9faf54f
Show file tree
Hide file tree
Showing 8 changed files with 187 additions and 956 deletions.
23 changes: 9 additions & 14 deletions eth/ethtest/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
"github.com/ethereum/go-ethereum/accounts/abi/bind"
ethcommon "github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/rpc"
gomock "go.uber.org/mock/gomock"
"go.uber.org/mock/gomock"
"go.uber.org/zap/zaptest"

"github.com/ssvlabs/ssv/eth/eventsyncer"
Expand All @@ -26,7 +26,7 @@ import (

type CommonTestInput struct {
t *testing.T
sim *simulator.SimulatedBackend
sim *simulator.Backend
boundContract *simcontract.Simcontract
blockNum *uint64
nodeStorage storage.Storage
Expand All @@ -35,7 +35,7 @@ type CommonTestInput struct {

func NewCommonTestInput(
t *testing.T,
sim *simulator.SimulatedBackend,
sim *simulator.Backend,
boundContract *simcontract.Simcontract,
blockNum *uint64,
nodeStorage storage.Storage,
Expand All @@ -56,7 +56,7 @@ type TestEnv struct {
validators []*testValidatorData
ops []*testOperator
nodeStorage storage.Storage
sim *simulator.SimulatedBackend
sim *simulator.Backend
boundContract *simcontract.Simcontract
auth *bind.TransactOpts
shares [][]byte
Expand All @@ -73,10 +73,6 @@ func (e *TestEnv) shutdown() {
e.mockCtrl.Finish()
}

if e.httpSrv != nil {
e.httpSrv.Close()
}

if e.execClient != nil {
// Always returns nil error
_ = e.execClient.Close()
Expand Down Expand Up @@ -131,8 +127,7 @@ func (e *TestEnv) setup(
// Adding testAddresses to the genesis block mostly to specify some balances for them
sim := simTestBackend(testAddresses)

// Create JSON-RPC handler
rpcServer, err := sim.Node.RPCHandler()
rpcServer, err := sim.Node().RPCHandler()
e.rpcServer = rpcServer
if err != nil {
return fmt.Errorf("can't create RPC server: %w", err)
Expand All @@ -153,15 +148,15 @@ func (e *TestEnv) setup(
return err
}

contractAddr, _, _, err := bind.DeployContract(auth, parsed, ethcommon.FromHex(simcontract.SimcontractMetaData.Bin), sim)
contractAddr, _, _, err := bind.DeployContract(auth, parsed, ethcommon.FromHex(simcontract.SimcontractMetaData.Bin), sim.Client())
if err != nil {
return fmt.Errorf("deploy contract: %w", err)
}

sim.Commit()

// Check contract code at the simulated blockchain
contractCode, err := sim.CodeAt(ctx, contractAddr, nil)
contractCode, err := sim.Client().CodeAt(ctx, contractAddr, nil)
if err != nil {
return fmt.Errorf("get contract code: %w", err)
}
Expand All @@ -186,7 +181,7 @@ func (e *TestEnv) setup(
return err
}

e.boundContract, err = simcontract.NewSimcontract(contractAddr, sim)
e.boundContract, err = simcontract.NewSimcontract(contractAddr, sim.Client())
if err != nil {
return err
}
Expand Down Expand Up @@ -225,7 +220,7 @@ func (e *TestEnv) CloseFollowDistance(blockNum *uint64) {
}
}

func commitBlock(sim *simulator.SimulatedBackend, blockNum *uint64) {
func commitBlock(sim *simulator.Backend, blockNum *uint64) {
sim.Commit()
*blockNum++
}
6 changes: 4 additions & 2 deletions eth/ethtest/eth_e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
ethcommon "github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/crypto"
"github.com/stretchr/testify/require"
gomock "go.uber.org/mock/gomock"
"go.uber.org/mock/gomock"

"github.com/ssvlabs/ssv/eth/simulator/simcontract"
ssvtypes "github.com/ssvlabs/ssv/protocol/v2/types"
Expand Down Expand Up @@ -165,8 +165,10 @@ func TestEthExecLayer(t *testing.T) {
require.NoError(t, err)
require.Equal(t, expectedNonce, nonce)

lastBlockNum, err := testEnv.sim.Client().BlockByNumber(ctx, nil)
require.NoError(t, err)
// Not sure does this make sense
require.Equal(t, uint64(testEnv.sim.Blockchain.CurrentBlock().Number.Int64()), *common.blockNum)
require.Equal(t, lastBlockNum.Number().Uint64(), *common.blockNum)
}

// Step 2: Exit validator
Expand Down
9 changes: 5 additions & 4 deletions eth/ethtest/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@ import (
ethcommon "github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/ethclient/simulated"
"github.com/herumi/bls-eth-go-binary/bls"
gomock "go.uber.org/mock/gomock"
"go.uber.org/mock/gomock"
"go.uber.org/zap"

"github.com/ssvlabs/ssv/ekm"
Expand Down Expand Up @@ -281,14 +282,14 @@ func setupOperatorStorage(
return nodeStorage, operatorData
}

func simTestBackend(testAddresses []*ethcommon.Address) *simulator.SimulatedBackend {
func simTestBackend(testAddresses []*ethcommon.Address) *simulator.Backend {
genesis := types.GenesisAlloc{}

for _, testAddr := range testAddresses {
genesis[*testAddr] = types.Account{Balance: big.NewInt(10000000000000000)}
}

return simulator.NewSimulatedBackend(
genesis, 50_000_000,
return simulator.NewBackend(genesis,
simulated.WithBlockGasLimit(50_000_000),
)
}
15 changes: 8 additions & 7 deletions eth/eventhandler/event_handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
ethcommon "github.com/ethereum/go-ethereum/common"
ethtypes "github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/ethclient/simulated"
"github.com/herumi/bls-eth-go-binary/bls"
"github.com/pkg/errors"
"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -90,7 +91,7 @@ func TestHandleBlockEventsStream(t *testing.T) {
sim := simTestBackend(testAddresses)

// Create JSON-RPC handler
rpcServer, _ := sim.Node.RPCHandler()
rpcServer, _ := sim.Node().RPCHandler()
// Expose handler on a test server with ws open
httpsrv := httptest.NewServer(rpcServer.WebsocketHandler([]string{"*"}))
defer rpcServer.Stop()
Expand All @@ -99,14 +100,14 @@ func TestHandleBlockEventsStream(t *testing.T) {

parsed, _ := abi.JSON(strings.NewReader(simcontract.SimcontractMetaData.ABI))
auth, _ := bind.NewKeyedTransactorWithChainID(testKey, big.NewInt(1337))
contractAddr, _, _, err := bind.DeployContract(auth, parsed, ethcommon.FromHex(simcontract.SimcontractMetaData.Bin), sim)
contractAddr, _, _, err := bind.DeployContract(auth, parsed, ethcommon.FromHex(simcontract.SimcontractMetaData.Bin), sim.Client())
if err != nil {
t.Errorf("deploying contract: %v", err)
}
sim.Commit()

// Check contract code at the simulated blockchain
contractCode, err := sim.CodeAt(ctx, contractAddr, nil)
contractCode, err := sim.Client().CodeAt(ctx, contractAddr, nil)
if err != nil {
t.Errorf("getting contract code: %v", err)
}
Expand All @@ -124,7 +125,7 @@ func TestHandleBlockEventsStream(t *testing.T) {

logs := client.StreamLogs(ctx, 0)

boundContract, err := simcontract.NewSimcontract(contractAddr, sim)
boundContract, err := simcontract.NewSimcontract(contractAddr, sim.Client())
require.NoError(t, err)

// Generate a new validator
Expand Down Expand Up @@ -1426,15 +1427,15 @@ func unmarshalLog(t *testing.T, rawOperatorAdded string) ethtypes.Log {
return vLogOperatorAdded
}

func simTestBackend(testAddresses []*ethcommon.Address) *simulator.SimulatedBackend {
func simTestBackend(testAddresses []*ethcommon.Address) *simulator.Backend {
genesis := ethtypes.GenesisAlloc{}

for _, testAddr := range testAddresses {
genesis[*testAddr] = ethtypes.Account{Balance: big.NewInt(10000000000000000)}
}

return simulator.NewSimulatedBackend(
genesis, 50_000_000,
return simulator.NewBackend(
genesis, simulated.WithBlockGasLimit(50_000_000),
)
}

Expand Down
19 changes: 10 additions & 9 deletions eth/eventsyncer/event_syncer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@ import (
"github.com/ethereum/go-ethereum/core/types"
ethtypes "github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/ethclient/simulated"
"github.com/stretchr/testify/require"
gomock "go.uber.org/mock/gomock"
"go.uber.org/mock/gomock"
"go.uber.org/zap"
"go.uber.org/zap/zaptest"

Expand Down Expand Up @@ -61,27 +62,27 @@ func TestEventSyncer(t *testing.T) {
// Create sim instance with a delay between block execution
sim := simTestBackend(testAddr)

rpcServer, _ := sim.Node.RPCHandler()
rpcServer, _ := sim.Node().RPCHandler()
httpSrv := httptest.NewServer(rpcServer.WebsocketHandler([]string{"*"}))
defer rpcServer.Stop()
defer httpSrv.Close()

parsed, _ := abi.JSON(strings.NewReader(simcontract.SimcontractMetaData.ABI))
auth, _ := bind.NewKeyedTransactorWithChainID(testKey, big.NewInt(1337))
contractAddr, _, _, err := bind.DeployContract(auth, parsed, ethcommon.FromHex(simcontract.SimcontractMetaData.Bin), sim)
contractAddr, _, _, err := bind.DeployContract(auth, parsed, ethcommon.FromHex(simcontract.SimcontractMetaData.Bin), sim.Client())
if err != nil {
t.Errorf("deploying contract: %v", err)
}
sim.Commit()

// Check contract code at the simulated blockchain
contractCode, err := sim.CodeAt(ctx, contractAddr, nil)
contractCode, err := sim.Client().CodeAt(ctx, contractAddr, nil)
if err != nil {
t.Errorf("getting contract code: %v", err)
}
require.NotEmpty(t, contractCode)

boundContract, err := simcontract.NewSimcontract(contractAddr, sim)
boundContract, err := simcontract.NewSimcontract(contractAddr, sim.Client())
require.NoError(t, err)

addr := "ws:" + strings.TrimPrefix(httpSrv.URL, "http:")
Expand All @@ -107,7 +108,7 @@ func TestEventSyncer(t *testing.T) {
tx, err := boundContract.SimcontractTransactor.RegisterOperator(auth, pckd, big.NewInt(100_000_000))
require.NoError(t, err)
sim.Commit()
receipt, err := sim.TransactionReceipt(ctx, tx.Hash())
receipt, err := sim.Client().TransactionReceipt(ctx, tx.Hash())
if err != nil {
t.Errorf("get receipt: %v", err)
}
Expand Down Expand Up @@ -194,11 +195,11 @@ func setupEventHandler(
return eh
}

func simTestBackend(testAddr ethcommon.Address) *simulator.SimulatedBackend {
return simulator.NewSimulatedBackend(
func simTestBackend(testAddr ethcommon.Address) *simulator.Backend {
return simulator.NewBackend(
types.GenesisAlloc{
testAddr: {Balance: big.NewInt(10000000000000000)},
}, 10000000,
}, simulated.WithBlockGasLimit(10000000),
)
}

Expand Down
1 change: 1 addition & 0 deletions eth/executionclient/execution_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ func New(ctx context.Context, nodeAddr string, contractAddr ethcommon.Address, o
}
err := client.connect(ctx)
if err != nil {
fmt.Println("failed to connnect", nodeAddr)
return nil, fmt.Errorf("failed to connect to execution client: %w", err)
}
return client, nil
Expand Down
Loading

0 comments on commit 9faf54f

Please sign in to comment.