diff --git a/chains/ethereum/chain.go b/chains/ethereum/chain.go index e902e1688..62e807275 100644 --- a/chains/ethereum/chain.go +++ b/chains/ethereum/chain.go @@ -21,6 +21,7 @@ The writer recieves the message and creates a proposals on-chain. Once a proposa package ethereum import ( + "context" "fmt" "math/big" @@ -139,6 +140,15 @@ func InitializeChain(chainCfg *core.ChainConfig, logger log15.Logger, sysErr cha return nil, fmt.Errorf("chainId (%d) and configuration chainId (%d) do not match", chainId, chainCfg.Id) } + mainChainId, err := conn.Client().ChainID(context.Background()) + if err != nil { + return nil, err + } + + if cfg.mainChainId != mainChainId { + panic(fmt.Errorf("chainId (%d) doesnt match with config defined mainChainId (%d)", mainChainId, cfg.mainChainId)) + } + erc20HandlerContract, err := erc20Handler.NewERC20Handler(cfg.erc20HandlerContract, conn.Client()) if err != nil { return nil, err diff --git a/chains/ethereum/config.go b/chains/ethereum/config.go index 6fe5d654a..d0e7837cc 100644 --- a/chains/ethereum/config.go +++ b/chains/ethereum/config.go @@ -6,12 +6,11 @@ package ethereum import ( "errors" "fmt" - "math/big" - utils "github.com/ChainSafe/ChainBridge/shared/ethereum" "github.com/centrifuge/chainbridge-utils/core" "github.com/centrifuge/chainbridge-utils/msg" "github.com/ethereum/go-ethereum/common" + "math/big" ) const DefaultGasLimit = 6721975 @@ -31,6 +30,7 @@ var ( HttpOpt = "http" StartBlockOpt = "startBlock" BlockConfirmationsOpt = "blockConfirmations" + MainChainIdOpt = "mainChainId" ) // Config encapsulates all necessary parameters in ethereum compatible forms @@ -52,6 +52,7 @@ type Config struct { http bool // Config for type of connection startBlock *big.Int blockConfirmations *big.Int + mainChainId *big.Int } // parseChainConfig uses a core.ChainConfig to construct a corresponding Config @@ -75,6 +76,7 @@ func parseChainConfig(chainCfg *core.ChainConfig) (*Config, error) { http: false, startBlock: big.NewInt(0), blockConfirmations: big.NewInt(0), + mainChainId: big.NewInt(0), } if contract, ok := chainCfg.Opts[BridgeOpt]; ok && contract != "" { @@ -159,6 +161,18 @@ func parseChainConfig(chainCfg *core.ChainConfig) (*Config, error) { delete(chainCfg.Opts, BlockConfirmationsOpt) } + if mainChainId, ok := chainCfg.Opts[MainChainIdOpt]; ok && mainChainId != "" { + val := big.NewInt(0) + _, pass := val.SetString(mainChainId, 10) + if !pass { + return nil, fmt.Errorf("unable to parse %s", MainChainIdOpt) + } + config.mainChainId = val + delete(chainCfg.Opts, MainChainIdOpt) + } else { + return nil, fmt.Errorf("unable to parse %s", MainChainIdOpt) + } + if len(chainCfg.Opts) != 0 { return nil, fmt.Errorf("unknown Opts Encountered: %#v", chainCfg.Opts) } diff --git a/chains/ethereum/listener.go b/chains/ethereum/listener.go index 57ea95513..0e4aef86d 100644 --- a/chains/ethereum/listener.go +++ b/chains/ethereum/listener.go @@ -166,6 +166,15 @@ func (l *listener) getDepositEventsForBlock(latestBlock *big.Int) error { return fmt.Errorf("unable to Filter Logs: %w", err) } + mainChainId, err := l.conn.Client().ChainID(context.Background()) + if err != nil { + return err + } + + if l.cfg.mainChainId != mainChainId { + panic(fmt.Errorf("chainId (%d) doesnt match with config defined mainChainId (%d)", mainChainId, l.cfg.mainChainId)) + } + // read through the log events and handle their deposit event if handler is recognized for _, log := range logs { var m msg.Message diff --git a/chains/ethereum/writer.go b/chains/ethereum/writer.go index b4f60a4e5..b671da181 100644 --- a/chains/ethereum/writer.go +++ b/chains/ethereum/writer.go @@ -4,6 +4,8 @@ package ethereum import ( + "context" + "fmt" "github.com/ChainSafe/ChainBridge/bindings/Bridge" "github.com/ChainSafe/log15" "github.com/centrifuge/chainbridge-utils/core" @@ -55,6 +57,16 @@ func (w *writer) setContract(bridge *Bridge.Bridge) { func (w *writer) ResolveMessage(m msg.Message) bool { w.log.Info("Attempting to resolve message", "type", m.Type, "src", m.Source, "dst", m.Destination, "nonce", m.DepositNonce, "rId", m.ResourceId.Hex()) + mainChainId, err := w.conn.Client().ChainID(context.Background()) + if err != nil { + w.log.Error("failed to get MainChainID") + return false + } + + if w.cfg.mainChainId != mainChainId { + panic(fmt.Errorf("chainId (%d) doesnt match with config defined mainChainId (%d)", mainChainId, w.cfg.mainChainId)) + } + switch m.Type { case msg.FungibleTransfer: return w.createErc20Proposal(m) diff --git a/go.mod b/go.mod index b5edbabd2..8925b5a27 100644 --- a/go.mod +++ b/go.mod @@ -23,6 +23,7 @@ require ( github.com/StackExchange/wmi v0.0.0-20180116203802-5d049714c4a6 // indirect github.com/beorn7/perks v1.0.1 // indirect github.com/btcsuite/btcd/btcec/v2 v2.1.2 // indirect + github.com/btcsuite/btcd/chaincfg/chainhash v1.0.1 // indirect github.com/cespare/xxhash/v2 v2.1.1 // indirect github.com/cosmos/go-bip39 v1.0.0 // indirect github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d // indirect @@ -55,8 +56,8 @@ require ( github.com/tklauser/go-sysconf v0.3.5 // indirect github.com/tklauser/numcpus v0.2.2 // indirect github.com/vedhavyas/go-subkey v1.0.3 // indirect - golang.org/x/crypto v0.0.0-20211117183948-ae814b36b871 // indirect - golang.org/x/sys v0.0.0-20211124211545-fe61309f8881 // indirect + golang.org/x/crypto v0.0.0-20220829220503-c86fa9a7ed90 // indirect + golang.org/x/sys v0.0.0-20220913175220-63ea55921009 // indirect golang.org/x/term v0.0.0-20210503060354-a79de5458b56 // indirect google.golang.org/protobuf v1.23.0 // indirect gopkg.in/natefinch/npipe.v2 v2.0.0-20160621034901-c1b8fa8bdcce // indirect diff --git a/go.sum b/go.sum index b995ce163..47c243a5f 100644 --- a/go.sum +++ b/go.sum @@ -56,10 +56,11 @@ github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM= github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6rlkpw= github.com/bmizerany/pat v0.0.0-20170815010413-6226ea591a40/go.mod h1:8rLXio+WjiTceGBHIoTvn60HIbs7Hm7bcHjyrSqYB9c= github.com/boltdb/bolt v1.3.1/go.mod h1:clJnj/oiGkjum5o1McbSZDSLxVThjynRyGBgiAx27Ps= -github.com/btcsuite/btcd v0.22.0-beta h1:LTDpDKUM5EeOFBPM8IXpinEcmZ6FWfNZbE3lfrfdnWo= github.com/btcsuite/btcd/btcec/v2 v2.1.2 h1:YoYoC9J0jwfukodSBMzZYUVQ8PTiYg4BnOWiJVzTmLs= github.com/btcsuite/btcd/btcec/v2 v2.1.2/go.mod h1:ctjw4H1kknNJmRN4iP1R7bTQ+v3GJkZBd6mui8ZsAZE= github.com/btcsuite/btcd/chaincfg/chainhash v1.0.0/go.mod h1:7SFka0XMvUgj3hfZtydOrQY2mwhPclbT2snogU7SQQc= +github.com/btcsuite/btcd/chaincfg/chainhash v1.0.1 h1:q0rUy8C/TYNBQS1+CGKw68tLOFYSNEs0TFnxxnS9+4U= +github.com/btcsuite/btcd/chaincfg/chainhash v1.0.1/go.mod h1:7SFka0XMvUgj3hfZtydOrQY2mwhPclbT2snogU7SQQc= github.com/btcsuite/btcutil v1.0.3-0.20201208143702-a53e38424cce h1:YtWJF7RHm2pYCvA5t0RPmAaLUhREsKuKd+SLhxFbFeQ= github.com/c-bata/go-prompt v0.2.2/go.mod h1:VzqtzE2ksDBcdln8G7mk2RX9QyGjH+OVqOCSiVIqS34= github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU= @@ -429,8 +430,8 @@ golang.org/x/crypto v0.0.0-20200728195943-123391ffb6de/go.mod h1:LzIPMQfyMNhhGPh golang.org/x/crypto v0.0.0-20200820211705-5c72a883971a/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= golang.org/x/crypto v0.0.0-20201221181555-eec23a3978ad/go.mod h1:jdWPYTVW3xRLrWPugEBEK3UY2ZEsg3UU495nc5E+M+I= golang.org/x/crypto v0.0.0-20210322153248-0c34fe9e7dc2/go.mod h1:T9bdIzuCu7OtxOm1hfPfRQxPLYneinmdGuTeoZ9dtd4= -golang.org/x/crypto v0.0.0-20211117183948-ae814b36b871 h1:/pEO3GD/ABYAjuakUS6xSEmmlyVS4kxBNkeA9tLJiTI= -golang.org/x/crypto v0.0.0-20211117183948-ae814b36b871/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4= +golang.org/x/crypto v0.0.0-20220829220503-c86fa9a7ed90 h1:Y/gsMcFOcR+6S6f3YeMKl5g+dZMEWqcz5Czj/GWYbkM= +golang.org/x/crypto v0.0.0-20220829220503-c86fa9a7ed90/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4= golang.org/x/exp v0.0.0-20180321215751-8460e604b9de/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA= golang.org/x/exp v0.0.0-20180807140117-3d87b88a115f/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA= golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA= @@ -537,8 +538,8 @@ golang.org/x/sys v0.0.0-20210324051608-47abb6519492/go.mod h1:h1NjWce9XRLGQEsW7w golang.org/x/sys v0.0.0-20210420205809-ac73e9fd8988/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210816183151-1e6c022a8912/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.0.0-20211124211545-fe61309f8881 h1:TyHqChC80pFkXWraUUf6RuB5IqFdQieMLwwCJokV2pc= -golang.org/x/sys v0.0.0-20211124211545-fe61309f8881/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20220913175220-63ea55921009 h1:PuvuRMeLWqsf/ZdT1UUZz0syhioyv1mzuFZsXs4fvhw= +golang.org/x/sys v0.0.0-20220913175220-63ea55921009/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/term v0.0.0-20201117132131-f5c789dd3221/go.mod h1:Nr5EML6q2oocZ2LXRh80K7BxOlk5/8JxuGnuhpl+muw= golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= golang.org/x/term v0.0.0-20210503060354-a79de5458b56 h1:b8jxX3zqjpqb2LklXPzKSGJhzyxCOZSz8ncv8Nv+y7w=