diff --git a/celo.go b/celo.go new file mode 100644 index 0000000..d7a6028 --- /dev/null +++ b/celo.go @@ -0,0 +1,55 @@ +package main + +import ( + "github.com/ChainSafe/chainbridge-core/chains/evm" + "github.com/ChainSafe/chainbridge-core/chains/evm/evmclient" + "github.com/ChainSafe/chainbridge-core/chains/evm/listener" + "github.com/ChainSafe/chainbridge-core/chains/evm/voter" + "github.com/ChainSafe/chainbridge-core/lvldb" + "github.com/google/wire" +) + +var CeloSet = wire.NewSet( + NewEVMCeloClientWithConfig, + RegisterNewCeloEventHandler, + NewCeloEVMListener, + NewCeloEVMMessageHandler, + NewCeloEVMVoter, + NewCeloEVMChain, +) + +type CeloEVMClient *evmclient.EVMClient + +func NewEVMCeloClientWithConfig(cnfg Config) CeloEVMClient { + return _newEVMClientWithConfig(cnfg, "celo_config.json") +} + +type CeloEventListener *listener.ETHEventHandler + +func RegisterNewCeloEventHandler(cnfg Config, client CeloEVMClient) CeloEventListener { + return _registerNewEventHandler(cnfg, client) +} + +type CeloEVMListener *listener.EVMListener + +func NewCeloEVMListener(cnfg Config, client CeloEVMClient, eventHandler CeloEventListener) CeloEVMListener { + return _newEVMListener(cnfg, client, eventHandler) +} + +type CeloEVMMessageHandler *voter.EVMMessageHandler + +func NewCeloEVMMessageHandler(cnfg Config, client CeloEVMClient) CeloEVMMessageHandler { + return _newEVMMessageHandler(cnfg, client) +} + +type CeloEVMVoter *voter.EVMVoter + +func NewCeloEVMVoter(mh CeloEVMMessageHandler, client CeloEVMClient, fabric voter.TxFabric) CeloEVMVoter { + return _newEVMVoter(mh, client, fabric) +} + +type CeloEVMChain *evm.EVMChain + +func NewCeloEVMChain(client CeloEVMClient, listener CeloEVMListener, voter CeloEVMVoter, db *lvldb.LVLDB) CeloEVMChain { + return _newEVMChain(client, listener, voter, db) +} diff --git a/evm.go b/evm.go new file mode 100644 index 0000000..c0d8ec0 --- /dev/null +++ b/evm.go @@ -0,0 +1,55 @@ +package main + +import ( + "github.com/ChainSafe/chainbridge-core/chains/evm" + "github.com/ChainSafe/chainbridge-core/chains/evm/evmclient" + "github.com/ChainSafe/chainbridge-core/chains/evm/listener" + "github.com/ChainSafe/chainbridge-core/chains/evm/voter" + "github.com/ChainSafe/chainbridge-core/lvldb" + "github.com/google/wire" +) + +var EvmSet = wire.NewSet( + NewEVMClientWithConfig, + RegisterNewEVMEventHandler, + NewEVMListener, + NewEVMMessageHandler, + NewEVMVoter, + NewEVMChain, +) + +type EVMClient *evmclient.EVMClient + +func NewEVMClientWithConfig(cnfg Config) EVMClient { + return _newEVMClientWithConfig(cnfg, "config_evm.json") +} + +type EVMEventListener *listener.ETHEventHandler + +func RegisterNewEVMEventHandler(cnfg Config, client EVMClient) EVMEventListener { + return _registerNewEventHandler(cnfg, client) +} + +type EVMListener *listener.EVMListener + +func NewEVMListener(cnfg Config, client EVMClient, eventHandler EVMEventListener) EVMListener { + return _newEVMListener(cnfg, client, eventHandler) +} + +type EVMMessageHandler *voter.EVMMessageHandler + +func NewEVMMessageHandler(cnfg Config, client EVMClient) EVMMessageHandler { + return _newEVMMessageHandler(cnfg, client) +} + +type EVMVoter *voter.EVMVoter + +func NewEVMVoter(mh EVMMessageHandler, client EVMClient, fabric voter.TxFabric) EVMVoter { + return _newEVMVoter(mh, client, fabric) +} + +type EVMChain *evm.EVMChain + +func NewEVMChain(client EVMClient, listener EVMListener, voter EVMVoter, db *lvldb.LVLDB) EVMChain { + return _newEVMChain(client, listener, voter, db) +} diff --git a/go.mod b/go.mod index 4201f6c..cb87822 100644 --- a/go.mod +++ b/go.mod @@ -2,14 +2,15 @@ module github.com/ChainSafe/chainbridge-core-example go 1.15 -//replace github.com/ChainSafe/chainbridge-core => ../chainbridge-core -// -//replace github.com/ChainSafe/chainbridge-celo-module => ../chainbridge-celo-module +// replace github.com/ChainSafe/chainbridge-core => ../chainbridge-core + +// replace github.com/ChainSafe/chainbridge-celo-module => ../chainbridge-celo-module require ( github.com/ChainSafe/chainbridge-celo-module v0.0.0-20210812101441-b6d7ad422a53 github.com/ChainSafe/chainbridge-core v0.0.0-20210922142450-7e66fa42a68e github.com/ethereum/go-ethereum v1.10.9 + github.com/google/wire v0.5.0 github.com/rs/zerolog v1.25.0 github.com/spf13/cobra v1.2.1 github.com/spf13/viper v1.9.0 diff --git a/go.sum b/go.sum index 2d4bfc2..dea3823 100644 --- a/go.sum +++ b/go.sum @@ -281,9 +281,13 @@ github.com/google/pprof v0.0.0-20210601050228-01bbb1931b22/go.mod h1:kpwsk12EmLe github.com/google/pprof v0.0.0-20210609004039-a478d1d731e9/go.mod h1:kpwsk12EmLew5upagYY7GY0pfYCcupk39gWOCRROcvE= github.com/google/pprof v0.0.0-20210720184732-4bb14d4b1be1/go.mod h1:kpwsk12EmLew5upagYY7GY0pfYCcupk39gWOCRROcvE= github.com/google/renameio v0.1.0/go.mod h1:KWCgfxg9yswjAJkECMjeO8J8rahYeXnNhOm40UhjYkI= +github.com/google/subcommands v1.0.1 h1:/eqq+otEXm5vhfBrbREPCSVQbvofip6kIz+mX5TUH7k= +github.com/google/subcommands v1.0.1/go.mod h1:ZjhPrFU+Olkh9WazFPsl27BQ4UPiG37m3yTrtFlrHVk= github.com/google/uuid v1.1.2/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= github.com/google/uuid v1.1.5 h1:kxhtnfFVi+rYdOALN0B3k9UT86zVJKfBimRaciULW4I= github.com/google/uuid v1.1.5/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= +github.com/google/wire v0.5.0 h1:I7ELFeVBr3yfPIcc8+MWvrjk+3VjbcSzoXm3JVa+jD8= +github.com/google/wire v0.5.0/go.mod h1:ngWDr9Qvq3yZA10YrxfyGELY/AFWGVpy9c1LTRi1EoU= github.com/googleapis/gax-go/v2 v2.0.4/go.mod h1:0Wqv26UfaUD9n4G6kQubkQ+KchISgw+vpHVxEJEs9eg= github.com/googleapis/gax-go/v2 v2.0.5/go.mod h1:DWXyrwAJ9X0FpwwEdw+IPEYBICEFu5mhpdKc/us6bOk= github.com/googleapis/gax-go/v2 v2.1.0/go.mod h1:Q3nei7sK6ybPYH7twZdmQpAd1MKb7pfu6SK+H1/DsU0= @@ -652,6 +656,7 @@ golang.org/x/mod v0.2.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= golang.org/x/mod v0.4.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= golang.org/x/mod v0.4.1/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= +golang.org/x/mod v0.4.2 h1:Gz96sIWK3OalVv/I/qNygP42zyoKp3xptRVCWRFEBvo= golang.org/x/mod v0.4.2/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= golang.org/x/net v0.0.0-20180826012351-8a410e7b638d/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= @@ -829,6 +834,7 @@ golang.org/x/tools v0.0.0-20190311212946-11955173bddd/go.mod h1:LCzVGOaR6xXOjkQ3 golang.org/x/tools v0.0.0-20190312151545-0bb0c0a6e846/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs= golang.org/x/tools v0.0.0-20190312170243-e65039ee4138/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs= golang.org/x/tools v0.0.0-20190328211700-ab21143f2384/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs= +golang.org/x/tools v0.0.0-20190422233926-fe54fb35175b/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs= golang.org/x/tools v0.0.0-20190425150028-36563e24a262/go.mod h1:RgjU9mgBXZiqYHBnxXauZ1Gv1EHHAz9KjViQ78xBX0Q= golang.org/x/tools v0.0.0-20190506145303-2d16b83fe98c/go.mod h1:RgjU9mgBXZiqYHBnxXauZ1Gv1EHHAz9KjViQ78xBX0Q= golang.org/x/tools v0.0.0-20190524140312-2c0ae7006135/go.mod h1:RgjU9mgBXZiqYHBnxXauZ1Gv1EHHAz9KjViQ78xBX0Q= @@ -878,6 +884,7 @@ golang.org/x/tools v0.1.1/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk= golang.org/x/tools v0.1.2/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk= golang.org/x/tools v0.1.3/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk= golang.org/x/tools v0.1.4/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk= +golang.org/x/tools v0.1.5 h1:ouewzE6p+/VEB31YYnTbEJdi8pFqKp4P4n85vwo3DHA= golang.org/x/tools v0.1.5/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk= golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= diff --git a/main.go b/main.go index dc51198..6d17367 100644 --- a/main.go +++ b/main.go @@ -4,9 +4,54 @@ package main import ( - "github.com/ChainSafe/chainbridge-core-example/cmd" + "fmt" + + celotx "github.com/ChainSafe/chainbridge-celo-module/transaction" + "github.com/ChainSafe/chainbridge-core/chains/evm" + "github.com/ChainSafe/chainbridge-core/chains/evm/evmtransaction" + "github.com/ChainSafe/chainbridge-core/chains/evm/voter" + "github.com/google/wire" ) +type Config struct { + blockstoreFlagName string + configFlagName string +} + func main() { - cmd.Execute() + // This would be initialized from config/flags + cnfg := Config{ + blockstoreFlagName: "", + configFlagName: "", + } + + // Decide depending on flags + celloChain := CreateEVMCelloChain( + cnfg, + celotx.NewCeloTransaction, + ) + + evmChain := CreateEVMChain( + cnfg, + evmtransaction.NewTransaction, + ) + + fmt.Println(celloChain) + fmt.Println(evmChain) +} + +func CreateEVMCelloChain( + cnfg Config, + txFabric voter.TxFabric, +) CeloEVMChain { + wire.Build(NewLvlDB, CeloSet) + return &evm.EVMChain{} +} + +func CreateEVMChain( + cnfg Config, + txFabric voter.TxFabric, +) EVMChain { + wire.Build(NewLvlDB, EvmSet) + return &evm.EVMChain{} } diff --git a/provider.go b/provider.go new file mode 100644 index 0000000..da9901a --- /dev/null +++ b/provider.go @@ -0,0 +1,55 @@ +package main + +import ( + "github.com/ChainSafe/chainbridge-core/chains/evm" + "github.com/ChainSafe/chainbridge-core/chains/evm/evmclient" + "github.com/ChainSafe/chainbridge-core/chains/evm/listener" + "github.com/ChainSafe/chainbridge-core/chains/evm/voter" + "github.com/ChainSafe/chainbridge-core/lvldb" + "github.com/ethereum/go-ethereum/common" +) + +func NewLvlDB(cnfg Config) *lvldb.LVLDB { + db, err := lvldb.NewLvlDB(cnfg.blockstoreFlagName) + if err != nil { + panic(err) + } + return db +} + +func _newEVMClientWithConfig(cnfg Config, name string) *evmclient.EVMClient { + client := evmclient.NewEVMClient() + err := client.Configurate(cnfg.configFlagName, name) + if err != nil { + panic(err) + } + return client +} + +func _registerNewEventHandler(cnfg Config, client *evmclient.EVMClient) *listener.ETHEventHandler { + cc := client.GetConfig() + eh := listener.NewETHEventHandler(common.HexToAddress(cc.SharedEVMConfig.Bridge), client) + eh.RegisterEventHandler(cc.SharedEVMConfig.Erc20Handler, listener.Erc20EventHandler) + return eh +} + +func _newEVMListener(cnfg Config, client *evmclient.EVMClient, eventHandler *listener.ETHEventHandler) *listener.EVMListener { + cc := client.GetConfig() + return listener.NewEVMListener(client, eventHandler, common.HexToAddress(cc.SharedEVMConfig.Bridge)) +} + +func _newEVMMessageHandler(cnfg Config, client *evmclient.EVMClient) *voter.EVMMessageHandler { + cc := client.GetConfig() + mh := voter.NewEVMMessageHandler(client, common.HexToAddress(cc.SharedEVMConfig.Bridge)) + mh.RegisterMessageHandler(common.HexToAddress(cc.SharedEVMConfig.Erc20Handler), voter.ERC20MessageHandler) + return mh +} + +func _newEVMVoter(mh *voter.EVMMessageHandler, client *evmclient.EVMClient, fabric voter.TxFabric) *voter.EVMVoter { + return voter.NewVoter(mh, client, fabric) +} + +func _newEVMChain(client *evmclient.EVMClient, listener *listener.EVMListener, voter *voter.EVMVoter, db *lvldb.LVLDB) *evm.EVMChain { + cc := client.GetConfig() + return evm.NewEVMChain(listener, voter, db, *cc.SharedEVMConfig.GeneralChainConfig.Id, &cc.SharedEVMConfig) +}