@@ -13,9 +13,7 @@ import (
1313 "strings"
1414 "time"
1515
16- "github.com/ethereum/go-ethereum"
1716 "github.com/ethereum/go-ethereum/common"
18- "github.com/ethereum/go-ethereum/core/types"
1917 "github.com/ethereum/go-ethereum/ethclient"
2018 "github.com/ethereum/go-ethereum/rpc"
2119 "github.com/ethersphere/bee/v2/pkg/config"
@@ -31,8 +29,8 @@ import (
3129 "github.com/ethersphere/bee/v2/pkg/settlement/swap/swapprotocol"
3230 "github.com/ethersphere/bee/v2/pkg/storage"
3331 "github.com/ethersphere/bee/v2/pkg/transaction"
32+ "github.com/ethersphere/bee/v2/pkg/transaction/backendnoop"
3433 "github.com/ethersphere/bee/v2/pkg/transaction/wrapped"
35- "github.com/prometheus/client_golang/prometheus"
3634)
3735
3836const (
@@ -48,38 +46,42 @@ func InitChain(
4846 logger log.Logger ,
4947 stateStore storage.StateStorer ,
5048 endpoint string ,
51- oChainID int64 ,
49+ chainID int64 ,
5250 signer crypto.Signer ,
5351 pollingInterval time.Duration ,
5452 chainEnabled bool ,
5553 minimumGasTipCap uint64 ,
5654) (transaction.Backend , common.Address , int64 , transaction.Monitor , transaction.Service , error ) {
57- var backend transaction.Backend = & noOpChainBackend {
58- chainID : oChainID ,
59- }
55+ backend := backendnoop .New (chainID )
6056
6157 if chainEnabled {
62- // connect to the real one
6358 rpcClient , err := rpc .DialContext (ctx , endpoint )
6459 if err != nil {
6560 return nil , common.Address {}, 0 , nil , nil , fmt .Errorf ("dial blockchain client: %w" , err )
6661 }
6762
6863 var versionString string
69- err = rpcClient .CallContext (ctx , & versionString , "web3_clientVersion" )
70- if err != nil {
71- logger .Info ("could not connect to backend; in a swap-enabled network a working blockchain node (for xdai network in production, sepolia in testnet) is required; check your node or specify another node using --blockchain-rpc-endpoint." , "backend_endpoint" , endpoint )
72- return nil , common.Address {}, 0 , nil , nil , fmt .Errorf ("blockchain client get version: %w" , err )
64+ if err = rpcClient .CallContext (ctx , & versionString , "web3_clientVersion" ); err != nil {
65+ logger .Info ("could not connect to backend; " +
66+ "in a swap-enabled network a working blockchain node " +
67+ "(for xDAI network in production, SepoliaETH in testnet) is required; " +
68+ "check your node or specify another node using --blockchain-rpc-endpoint." ,
69+ "blockchain-rpc-endpoint" , endpoint )
70+ return nil , common.Address {}, 0 , nil , nil , fmt .Errorf ("get client version: %w" , err )
7371 }
7472
7573 logger .Info ("connected to blockchain backend" , "version" , versionString )
7674
7775 backend = wrapped .NewBackend (ethclient .NewClient (rpcClient ), minimumGasTipCap )
7876 }
7977
80- chainID , err := backend .ChainID (ctx )
78+ backendChainID , err := backend .ChainID (ctx )
8179 if err != nil {
82- return nil , common.Address {}, 0 , nil , nil , fmt .Errorf ("get chain id: %w" , err )
80+ return nil , common.Address {}, 0 , nil , nil , fmt .Errorf ("getting chain id: %w" , err )
81+ }
82+
83+ if chainID != - 1 && chainID != backendChainID .Int64 () {
84+ return nil , common.Address {}, 0 , nil , nil , fmt .Errorf ("connected to wrong network: expected chain id %d, got %d" , chainID , backendChainID .Int64 ())
8385 }
8486
8587 overlayEthAddress , err := signer .EthereumAddress ()
@@ -89,18 +91,19 @@ func InitChain(
8991
9092 transactionMonitor := transaction .NewMonitor (logger , backend , overlayEthAddress , pollingInterval , cancellationDepth )
9193
92- transactionService , err := transaction .NewService (logger , overlayEthAddress , backend , signer , stateStore , chainID , transactionMonitor )
94+ transactionService , err := transaction .NewService (logger , overlayEthAddress , backend , signer , stateStore , backendChainID , transactionMonitor )
9395 if err != nil {
94- return nil , common.Address {}, 0 , nil , nil , fmt .Errorf ("new transaction service: %w" , err )
96+ return nil , common.Address {}, 0 , nil , nil , fmt .Errorf ("transaction service: %w" , err )
9597 }
9698
97- return backend , overlayEthAddress , chainID .Int64 (), transactionMonitor , transactionService , nil
99+ return backend , overlayEthAddress , backendChainID .Int64 (), transactionMonitor , transactionService , nil
98100}
99101
100102// InitChequebookFactory will initialize the chequebook factory with the given
101103// chain backend.
102104func InitChequebookFactory (logger log.Logger , backend transaction.Backend , chainID int64 , transactionService transaction.Service , factoryAddress string ) (chequebook.Factory , error ) {
103105 var currentFactory common.Address
106+
104107 chainCfg , found := config .GetByChainID (chainID )
105108
106109 foundFactory := chainCfg .CurrentFactoryAddress
@@ -340,74 +343,3 @@ func (m *noOpChequebookService) LastCheque(common.Address) (*chequebook.SignedCh
340343func (m * noOpChequebookService ) LastCheques () (map [common.Address ]* chequebook.SignedCheque , error ) {
341344 return nil , postagecontract .ErrChainDisabled
342345}
343-
344- // noOpChainBackend is a noOp implementation for transaction.Backend interface.
345- type noOpChainBackend struct {
346- chainID int64
347- }
348-
349- func (m noOpChainBackend ) Metrics () []prometheus.Collector {
350- return nil
351- }
352-
353- func (m noOpChainBackend ) CallContract (context.Context , ethereum.CallMsg , * big.Int ) ([]byte , error ) {
354- return nil , errors .New ("disabled chain backend" )
355- }
356-
357- func (m noOpChainBackend ) HeaderByNumber (context.Context , * big.Int ) (* types.Header , error ) {
358- h := new (types.Header )
359- h .Time = uint64 (time .Now ().Unix ())
360- return h , nil
361- }
362-
363- func (m noOpChainBackend ) PendingNonceAt (context.Context , common.Address ) (uint64 , error ) {
364- panic ("chain no op: PendingNonceAt" )
365- }
366-
367- func (m noOpChainBackend ) SuggestedFeeAndTip (ctx context.Context , gasPrice * big.Int , boostPercent int ) (* big.Int , * big.Int , error ) {
368- panic ("chain no op: SuggestedFeeAndTip" )
369- }
370-
371- func (m noOpChainBackend ) SuggestGasTipCap (context.Context ) (* big.Int , error ) {
372- panic ("chain no op: SuggestGasTipCap" )
373- }
374-
375- func (m noOpChainBackend ) EstimateGasAtBlock (context.Context , ethereum.CallMsg , * big.Int ) (uint64 , error ) {
376- panic ("chain no op: EstimateGasAtBlock" )
377- }
378-
379- func (m noOpChainBackend ) SendTransaction (context.Context , * types.Transaction ) error {
380- panic ("chain no op: SendTransaction" )
381- }
382-
383- func (m noOpChainBackend ) TransactionReceipt (context.Context , common.Hash ) (* types.Receipt , error ) {
384- r := new (types.Receipt )
385- r .BlockNumber = big .NewInt (1 )
386- return r , nil
387- }
388-
389- func (m noOpChainBackend ) TransactionByHash (context.Context , common.Hash ) (tx * types.Transaction , isPending bool , err error ) {
390- panic ("chain no op: TransactionByHash" )
391- }
392-
393- func (m noOpChainBackend ) BlockNumber (context.Context ) (uint64 , error ) {
394- return 4 , nil
395- }
396-
397- func (m noOpChainBackend ) BalanceAt (context.Context , common.Address , * big.Int ) (* big.Int , error ) {
398- return nil , postagecontract .ErrChainDisabled
399- }
400-
401- func (m noOpChainBackend ) NonceAt (context.Context , common.Address , * big.Int ) (uint64 , error ) {
402- panic ("chain no op: NonceAt" )
403- }
404-
405- func (m noOpChainBackend ) FilterLogs (context.Context , ethereum.FilterQuery ) ([]types.Log , error ) {
406- panic ("chain no op: FilterLogs" )
407- }
408-
409- func (m noOpChainBackend ) ChainID (context.Context ) (* big.Int , error ) {
410- return big .NewInt (m .chainID ), nil
411- }
412-
413- func (m noOpChainBackend ) Close () {}
0 commit comments