From 0c345a13675ac52c15ddfffa50fe76a4b69f3510 Mon Sep 17 00:00:00 2001 From: HuangYi Date: Mon, 16 Aug 2021 10:03:24 +0800 Subject: [PATCH] embed gravity bridge module Closes #9 - add gravity module to app - verify address format at application level - fix pystarport config files - build orchestrator in nix - add a gravity hook implementation template in cronos module - add gravity custom gentx command fix chain id and err handling code add eth_keys command --- CHANGELOG.md | 3 ++ app/app.go | 58 +++++++++++++++++++++++++- app/prefix.go | 2 + cmd/cronosd/cmd/root.go | 4 +- go.mod | 3 ++ go.sum | 2 + gomod2nix.toml | 9 ++++ integration_tests/configs/default.yaml | 4 +- nix/default.nix | 15 +++++++ nix/sources.json | 13 ++++++ scripts/cronos-devnet.yaml | 4 +- scripts/devnet.yaml | 4 +- x/cronos/keeper/hooks.go | 19 +++++++++ 13 files changed, 131 insertions(+), 9 deletions(-) create mode 100644 x/cronos/keeper/hooks.go diff --git a/CHANGELOG.md b/CHANGELOG.md index ecd90ca4c9..27c2c88600 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,6 +14,9 @@ This version is a new scaffolding of cronos project where ethermint is included - (ethermint) [tharsis#383](https://github.com/tharsis/ethermint/pull/383) `GetCommittedState` use the original context. ### Features + +- [#11](https://github.com/crypto-org-chain/cronos/pull/11) embed gravity bridge module + ### Improvements - (ethermint) [tharsis#425](https://github.com/tharsis/ethermint/pull/425) Support build on linux arm64 diff --git a/app/app.go b/app/app.go index 97ccf82f55..a9b9831dc5 100644 --- a/app/app.go +++ b/app/app.go @@ -23,6 +23,7 @@ import ( "github.com/cosmos/cosmos-sdk/server/config" servertypes "github.com/cosmos/cosmos-sdk/server/types" sdk "github.com/cosmos/cosmos-sdk/types" + sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" "github.com/cosmos/cosmos-sdk/types/module" "github.com/cosmos/cosmos-sdk/version" "github.com/cosmos/cosmos-sdk/x/auth" @@ -98,13 +99,24 @@ import ( evmkeeper "github.com/tharsis/ethermint/x/evm/keeper" evmtypes "github.com/tharsis/ethermint/x/evm/types" + "github.com/peggyjv/gravity-bridge/module/x/gravity" + gravitykeeper "github.com/peggyjv/gravity-bridge/module/x/gravity/keeper" + gravitytypes "github.com/peggyjv/gravity-bridge/module/x/gravity/types" + // this line is used by starport scaffolding # stargate/app/moduleImport cronosmodule "github.com/crypto-org-chain/cronos/x/cronos" cronosmodulekeeper "github.com/crypto-org-chain/cronos/x/cronos/keeper" cronosmoduletypes "github.com/crypto-org-chain/cronos/x/cronos/types" ) -const Name = "cronos" +const ( + Name = "cronos" + + // AddrLen is the allowed length (in bytes) for an address. + // + // NOTE: In the SDK, the default value is 255. + AddrLen = 20 +) // this line is used by starport scaffolding # stargate/wasm/app/enabledProposals @@ -151,6 +163,7 @@ var ( transfer.AppModuleBasic{}, vesting.AppModuleBasic{}, evm.AppModuleBasic{}, + gravity.AppModuleBasic{}, // this line is used by starport scaffolding # stargate/app/moduleBasic cronosmodule.AppModuleBasic{}, ) @@ -165,6 +178,7 @@ var ( govtypes.ModuleName: {authtypes.Burner}, ibctransfertypes.ModuleName: {authtypes.Minter, authtypes.Burner}, evmtypes.ModuleName: {authtypes.Minter, authtypes.Burner}, // used for secure addition and subtraction of balance using module account + gravitytypes.ModuleName: {authtypes.Minter, authtypes.Burner}, } // Module configurator @@ -226,6 +240,9 @@ type App struct { // Ethermint keepers EvmKeeper *evmkeeper.Keeper + // Gravity module + GravityKeeper gravitykeeper.Keeper + // this line is used by starport scaffolding # stargate/app/keeperDeclaration CronosKeeper cronosmodulekeeper.Keeper @@ -265,6 +282,7 @@ func New( ibchost.StoreKey, ibctransfertypes.StoreKey, // ethermint keys evmtypes.StoreKey, + gravitytypes.StoreKey, // this line is used by starport scaffolding # stargate/app/storeKey cronosmoduletypes.StoreKey, ) @@ -332,7 +350,11 @@ func New( // register the staking hooks // NOTE: stakingKeeper above is passed by reference, so that it will contain these hooks app.StakingKeeper = *stakingKeeper.SetHooks( - stakingtypes.NewMultiStakingHooks(app.DistrKeeper.Hooks(), app.SlashingKeeper.Hooks()), + stakingtypes.NewMultiStakingHooks( + app.DistrKeeper.Hooks(), + app.SlashingKeeper.Hooks(), + app.GravityKeeper.Hooks(), + ), ) // ... other modules keepers @@ -386,6 +408,18 @@ func New( &stakingKeeper, govRouter, ) + gravityKeeper := gravitykeeper.NewKeeper( + appCodec, + keys[gravitytypes.StoreKey], + app.GetSubspace(gravitytypes.ModuleName), + app.AccountKeeper, + app.StakingKeeper, + app.BankKeeper, + app.SlashingKeeper, + sdk.DefaultPowerReduction, + ) + app.GravityKeeper = *gravityKeeper.SetHooks(app.CronosKeeper) + // Create static IBC router, add transfer route, then set and seal it ibcRouter := porttypes.NewRouter() ibcRouter.AddRoute(ibctransfertypes.ModuleName, transferModule) @@ -425,6 +459,7 @@ func New( transferModule, evm.NewAppModule(app.EvmKeeper, app.AccountKeeper), + gravity.NewAppModule(app.GravityKeeper, app.BankKeeper), // this line is used by starport scaffolding # stargate/app/appModule cronosModule, ) @@ -438,11 +473,13 @@ func New( evmtypes.ModuleName, minttypes.ModuleName, distrtypes.ModuleName, slashingtypes.ModuleName, evidencetypes.ModuleName, stakingtypes.ModuleName, ibchost.ModuleName, + gravitytypes.ModuleName, ) app.mm.SetOrderEndBlockers( crisistypes.ModuleName, govtypes.ModuleName, stakingtypes.ModuleName, evmtypes.ModuleName, + gravitytypes.ModuleName, ) // NOTE: The genutils module must occur after staking so that pools are @@ -467,6 +504,7 @@ func New( evidencetypes.ModuleName, ibctransfertypes.ModuleName, evmtypes.ModuleName, + gravitytypes.ModuleName, // this line is used by starport scaffolding # stargate/app/initGenesis cronosmoduletypes.ModuleName, ) @@ -652,8 +690,24 @@ func initParamsKeeper(appCodec codec.BinaryCodec, legacyAmino *codec.LegacyAmino paramsKeeper.Subspace(ibctransfertypes.ModuleName) paramsKeeper.Subspace(ibchost.ModuleName) paramsKeeper.Subspace(evmtypes.ModuleName) + paramsKeeper.Subspace(gravitytypes.ModuleName) // this line is used by starport scaffolding # stargate/app/paramSubspace paramsKeeper.Subspace(cronosmoduletypes.ModuleName) return paramsKeeper } + +// VerifyAddressFormat verifis the address is compatible with ethereum +func VerifyAddressFormat(bz []byte) error { + if len(bz) == 0 { + return sdkerrors.Wrap(sdkerrors.ErrUnknownAddress, "invalid address; cannot be empty") + } + if len(bz) != AddrLen { + return sdkerrors.Wrapf( + sdkerrors.ErrUnknownAddress, + "invalid address length; got: %d, expect: %d", len(bz), AddrLen, + ) + } + + return nil +} diff --git a/app/prefix.go b/app/prefix.go index b898d5fcb4..54b31ee31d 100644 --- a/app/prefix.go +++ b/app/prefix.go @@ -10,5 +10,7 @@ func SetConfig() { // use the configurations from ethermint cmdcfg.SetBech32Prefixes(config) cmdcfg.SetBip44CoinType(config) + // Make sure address is compatible with ethereum + config.SetAddressVerifier(VerifyAddressFormat) config.Seal() } diff --git a/cmd/cronosd/cmd/root.go b/cmd/cronosd/cmd/root.go index 491f25485d..a8135e466f 100644 --- a/cmd/cronosd/cmd/root.go +++ b/cmd/cronosd/cmd/root.go @@ -33,6 +33,7 @@ import ( "github.com/cosmos/cosmos-sdk/x/crisis" genutilcli "github.com/cosmos/cosmos-sdk/x/genutil/client/cli" + gravitycmd "github.com/peggyjv/gravity-bridge/module/cmd/gravity/cmd" ethermintclient "github.com/tharsis/ethermint/client" "github.com/tharsis/ethermint/crypto/hd" ethermintserver "github.com/tharsis/ethermint/server" @@ -110,7 +111,7 @@ func initRootCmd(rootCmd *cobra.Command, encodingConfig params.EncodingConfig) { ), genutilcli.CollectGenTxsCmd(banktypes.GenesisBalancesIterator{}, app.DefaultNodeHome), genutilcli.MigrateGenesisCmd(), - genutilcli.GenTxCmd(app.ModuleBasics, encodingConfig.TxConfig, banktypes.GenesisBalancesIterator{}, app.DefaultNodeHome), + gravitycmd.GenTxCmd(app.ModuleBasics, encodingConfig.TxConfig, banktypes.GenesisBalancesIterator{}, app.DefaultNodeHome), genutilcli.ValidateGenesisCmd(app.ModuleBasics), AddGenesisAccountCmd(app.DefaultNodeHome), tmcli.NewCompletionCmd(rootCmd, true), @@ -129,6 +130,7 @@ func initRootCmd(rootCmd *cobra.Command, encodingConfig params.EncodingConfig) { queryCommand(), txCommand(), ethermintclient.KeyCommands(app.DefaultNodeHome), + gravitycmd.Commands(app.DefaultNodeHome), ) // add rosetta diff --git a/go.mod b/go.mod index fb58985858..0709b808dd 100644 --- a/go.mod +++ b/go.mod @@ -5,10 +5,13 @@ go 1.16 require ( github.com/cosmos/cosmos-sdk v0.43.0 github.com/cosmos/ibc-go v1.0.0 + github.com/ethereum/go-ethereum v1.10.3 github.com/gogo/protobuf v1.3.3 github.com/google/go-cmp v0.5.6 // indirect github.com/gorilla/mux v1.8.0 github.com/grpc-ecosystem/grpc-gateway v1.16.0 + github.com/peggyjv/gravity-bridge/module v0.1.21 + github.com/pkg/errors v0.9.1 github.com/spf13/cast v1.4.0 github.com/spf13/cobra v1.2.1 github.com/spf13/pflag v1.0.5 diff --git a/go.sum b/go.sum index ae3987c7e5..82b98b5913 100644 --- a/go.sum +++ b/go.sum @@ -764,6 +764,8 @@ github.com/pascaldekloe/goe v0.1.0/go.mod h1:lzWF7FIEvWOWxwDKqyGYQf6ZUaNfKdP144T github.com/paulbellamy/ratecounter v0.2.0/go.mod h1:Hfx1hDpSGoqxkVVpBi/IlYD7kChlfo5C6hzIHwPqfFE= github.com/pborman/uuid v0.0.0-20170112150404-1b00554d8222/go.mod h1:VyrYX9gd7irzKovcSS6BIIEwPRkP2Wm2m9ufcdFSJ34= github.com/pborman/uuid v1.2.0/go.mod h1:X/NO0urCmaxf9VXbdlT7C2Yzkj2IKimNn4k+gtPdI/k= +github.com/peggyjv/gravity-bridge/module v0.1.21 h1:5QBK682gf67vQoCpedeJYzTPPLWvZJtmqLxj7lWEJ+o= +github.com/peggyjv/gravity-bridge/module v0.1.21/go.mod h1:Key1D2z/KaJ5A206L3EQW6RWo4q8w+Jpl+p5HMm896g= github.com/pelletier/go-toml v1.2.0/go.mod h1:5z9KED0ma1S8pY6P1sdut58dfprrGBbd/94hg7ilaic= github.com/pelletier/go-toml v1.9.3 h1:zeC5b1GviRUyKYd6OJPvBU/mcVDVoL1OhT17FCt5dSQ= github.com/pelletier/go-toml v1.9.3/go.mod h1:u1nR/EPcESfeI/szUZKdtJ0xRNbUoANCkoOuaOx1Y+c= diff --git a/gomod2nix.toml b/gomod2nix.toml index df4c4c61a1..beffc35ec1 100644 --- a/gomod2nix.toml +++ b/gomod2nix.toml @@ -2995,6 +2995,15 @@ rev = "8b1b92947f46224e3b97bb1a3a5b0382be00d31e" sha256 = "0f146yjqwx2mr110kl8scjhqd08hys7vr5z0d0a3lskb6yy22gfg" +["github.com/peggyjv/gravity-bridge/module"] + sumVersion = "v0.1.21" + relPath = "module" + ["github.com/peggyjv/gravity-bridge/module".fetch] + type = "git" + url = "https://github.com/peggyjv/gravity-bridge" + rev = "755156abff254420eec2c4e54fb6e8d189556ee2" + sha256 = "1fjkq0jv42pq7fsah4chk2d1xkrdnkb9x52lkyagzlykw08ywcvr" + ["github.com/pelletier/go-toml"] sumVersion = "v1.9.3" ["github.com/pelletier/go-toml".fetch] diff --git a/integration_tests/configs/default.yaml b/integration_tests/configs/default.yaml index a6e0e6eb2e..06c50e071a 100644 --- a/integration_tests/configs/default.yaml +++ b/integration_tests/configs/default.yaml @@ -1,5 +1,5 @@ -cronosd-777: - cmd: ethermintd +cronos-777: + cmd: cronosd start-flags: "--trace" validators: - coins: 1000000000000000000stake,1000000000000000000basetcro diff --git a/nix/default.nix b/nix/default.nix index f8f4d15542..1fdb5882af 100644 --- a/nix/default.nix +++ b/nix/default.nix @@ -18,6 +18,21 @@ import sources.nixpkgs { }; } ) + (_: pkgs: { + orchestrator = pkgs.rustPlatform.buildRustPackage rec { + name = "orchestrator"; + src = sources.gravity-bridge; + sourceRoot = "gravity-bridge-src/${name}"; + cargoSha256 = sha256:1sp1f959qzigrpxi3qz33sr4cbl7s805p72nhv7gymjzjscr578z; + cargoBuildFlags = "-p ${name}"; + doCheck = false; + OPENSSL_NO_VENDOR = "1"; + OPENSSL_DIR = pkgs.symlinkJoin { + name = "openssl"; + paths = with pkgs.openssl; [ out dev ]; + }; + }; + }) ]; config = { }; inherit system; diff --git a/nix/sources.json b/nix/sources.json index 47339f72d7..fc98bb0ad1 100644 --- a/nix/sources.json +++ b/nix/sources.json @@ -11,6 +11,19 @@ "url": "https://github.com/tweag/gomod2nix/archive/67f22dd738d092c6ba88e420350ada0ed4992ae8.tar.gz", "url_template": "https://github.com///archive/.tar.gz" }, + "gravity-bridge": { + "branch": "main", + "description": "A CosmosSDK application for moving assets on and off of EVM based, POW chains", + "homepage": "", + "owner": "PeggyJV", + "repo": "gravity-bridge", + "rev": "755156abff254420eec2c4e54fb6e8d189556ee2", + "sha256": "1fjkq0jv42pq7fsah4chk2d1xkrdnkb9x52lkyagzlykw08ywcvr", + "type": "tarball", + "url": "https://github.com/PeggyJV/gravity-bridge/archive/refs/tags/v0.1.21.tar.gz", + "url_template": "https://github.com/PeggyJV/gravity-bridge/archive/refs/tags/v0.1.21.tar.gz", + "version": "0.1.21" + }, "niv": { "branch": "master", "description": "Easy dependency management for Nix projects", diff --git a/scripts/cronos-devnet.yaml b/scripts/cronos-devnet.yaml index a6e0e6eb2e..06c50e071a 100644 --- a/scripts/cronos-devnet.yaml +++ b/scripts/cronos-devnet.yaml @@ -1,5 +1,5 @@ -cronosd-777: - cmd: ethermintd +cronos-777: + cmd: cronosd start-flags: "--trace" validators: - coins: 1000000000000000000stake,1000000000000000000basetcro diff --git a/scripts/devnet.yaml b/scripts/devnet.yaml index ca6d7b921a..c35c21dc80 100644 --- a/scripts/devnet.yaml +++ b/scripts/devnet.yaml @@ -1,5 +1,5 @@ -chainmaind-777: - cmd: ./build/ethermintd +cronos-777: + cmd: ./build/cronosd start-flags: "--trace --evm-rpc.enable-unsafe-cors" validators: - coins: 1000000000000000000stake,1000000000000000000basetcro diff --git a/x/cronos/keeper/hooks.go b/x/cronos/keeper/hooks.go new file mode 100644 index 0000000000..3c6c02fc25 --- /dev/null +++ b/x/cronos/keeper/hooks.go @@ -0,0 +1,19 @@ +package keeper + +import ( + sdk "github.com/cosmos/cosmos-sdk/types" + gravitytypes "github.com/peggyjv/gravity-bridge/module/x/gravity/types" +) + +// TODO Implements GravityHooks interface +func (k Keeper) AfterContractCallExecutedEvent(ctx sdk.Context, event gravitytypes.ContractCallExecutedEvent) { +} + +func (k Keeper) AfterERC20DeployedEvent(ctx sdk.Context, event gravitytypes.ERC20DeployedEvent) {} + +func (k Keeper) AfterSignerSetExecutedEvent(ctx sdk.Context, event gravitytypes.SignerSetTxExecutedEvent) { +} + +func (k Keeper) AfterBatchExecutedEvent(ctx sdk.Context, event gravitytypes.BatchExecutedEvent) {} + +func (k Keeper) AfterSendToCosmosEvent(ctx sdk.Context, event gravitytypes.SendToCosmosEvent) {}