Skip to content

Commit

Permalink
embed gravity bridge module
Browse files Browse the repository at this point in the history
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
  • Loading branch information
yihuang committed Aug 17, 2021
1 parent 5d6a93f commit 0c345a1
Show file tree
Hide file tree
Showing 13 changed files with 131 additions and 9 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
58 changes: 56 additions & 2 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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{},
)
Expand All @@ -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

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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,
)
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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,
)
Expand All @@ -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
Expand All @@ -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,
)
Expand Down Expand Up @@ -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
}
2 changes: 2 additions & 0 deletions app/prefix.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
4 changes: 3 additions & 1 deletion cmd/cronosd/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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),
Expand All @@ -129,6 +130,7 @@ func initRootCmd(rootCmd *cobra.Command, encodingConfig params.EncodingConfig) {
queryCommand(),
txCommand(),
ethermintclient.KeyCommands(app.DefaultNodeHome),
gravitycmd.Commands(app.DefaultNodeHome),
)

// add rosetta
Expand Down
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
9 changes: 9 additions & 0 deletions gomod2nix.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
4 changes: 2 additions & 2 deletions integration_tests/configs/default.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
cronosd-777:
cmd: ethermintd
cronos-777:
cmd: cronosd
start-flags: "--trace"
validators:
- coins: 1000000000000000000stake,1000000000000000000basetcro
Expand Down
15 changes: 15 additions & 0 deletions nix/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
13 changes: 13 additions & 0 deletions nix/sources.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,19 @@
"url": "https://github.com/tweag/gomod2nix/archive/67f22dd738d092c6ba88e420350ada0ed4992ae8.tar.gz",
"url_template": "https://github.com/<owner>/<repo>/archive/<rev>.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",
Expand Down
4 changes: 2 additions & 2 deletions scripts/cronos-devnet.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
cronosd-777:
cmd: ethermintd
cronos-777:
cmd: cronosd
start-flags: "--trace"
validators:
- coins: 1000000000000000000stake,1000000000000000000basetcro
Expand Down
4 changes: 2 additions & 2 deletions scripts/devnet.yaml
Original file line number Diff line number Diff line change
@@ -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
Expand Down
19 changes: 19 additions & 0 deletions x/cronos/keeper/hooks.go
Original file line number Diff line number Diff line change
@@ -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) {}

0 comments on commit 0c345a1

Please sign in to comment.