Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DNM: IBC-Gaia Integration #130

Closed
wants to merge 31 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
0f68573
update mod
mossid Sep 3, 2019
601ea4c
fix mod
mossid Sep 3, 2019
e6563ce
update mod
mossid Sep 17, 2019
d6b9f98
Merge branch 'master' of github.com:cosmos/gaia into joon/ibc-gaia-in…
mossid Sep 17, 2019
38e34ca
fix go mod problem completly
mossid Sep 17, 2019
609c4b8
reflect sdk commit
mossid Sep 19, 2019
bbc7aed
add shell scripts, add readme
mossid Sep 19, 2019
d3e21d2
add channel handshake
mossid Sep 20, 2019
e583706
Fix IBC docs
jackzampolin Sep 30, 2019
6102c55
Update docs
jackzampolin Oct 1, 2019
90abacc
fix handshake
mossid Oct 1, 2019
114be93
add mocksend/mockrecv apps
mossid Oct 1, 2019
18a6b52
remove -mod=readonly
jackzampolin Oct 1, 2019
edffb9c
use cosmos/cosmos-sdk#5124 as SDK-IBC interface
fedekunze Oct 3, 2019
b96de61
merge master
fedekunze Oct 3, 2019
dd2f1e9
remove -mod=readonly again
fedekunze Oct 3, 2019
3dcbc39
WIP 2chainz tutorial, verifier still blockin
jackzampolin Oct 4, 2019
0d7afc9
Verifier creation working properly, debugging panic on store query be…
jackzampolin Oct 4, 2019
4848be2
Just need to be able to flush packet from channel
jackzampolin Oct 7, 2019
3ecb299
Clarify which branch in instructions
jackzampolin Oct 7, 2019
171ae0f
fix readme
mossid Oct 9, 2019
513e0cd
Update cmd/gaiacli/README.md
fedekunze Oct 9, 2019
409d5ff
update go mod
mossid Oct 10, 2019
ced210a
Merge branch 'joon/ibc-gaia-interface' of github.com:cosmos/gaia into…
mossid Oct 10, 2019
176c722
Update IBC CLI UX
jackzampolin Oct 11, 2019
2c4c56b
add ibcante, update readme
mossid Oct 12, 2019
7eec0a3
Merge branch 'joon/ibc-gaia-interface' of github.com:cosmos/gaia into…
mossid Oct 12, 2019
c1c09be
fix go.sum
mossid Oct 12, 2019
121357d
Merge PR #157: Fix typos
alexfmpe Oct 14, 2019
6df9ff7
Demo in working state
jackzampolin Oct 15, 2019
9248903
Fix go.mod
jackzampolin Oct 15, 2019
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,11 @@ all: install lint check

build: go.sum
ifeq ($(OS),Windows_NT)
go build -mod=readonly $(BUILD_FLAGS) -o build/gaiad.exe ./cmd/gaiad
go build -mod=readonly $(BUILD_FLAGS) -o build/gaiacli.exe ./cmd/gaiacli
go build $(BUILD_FLAGS) -o build/gaiad.exe ./cmd/gaiad
go build $(BUILD_FLAGS) -o build/gaiacli.exe ./cmd/gaiacli
else
go build -mod=readonly $(BUILD_FLAGS) -o build/gaiad ./cmd/gaiad
go build -mod=readonly $(BUILD_FLAGS) -o build/gaiacli ./cmd/gaiacli
go build $(BUILD_FLAGS) -o build/gaiad ./cmd/gaiad
go build $(BUILD_FLAGS) -o build/gaiacli ./cmd/gaiacli
endif

build-linux: go.sum
Expand All @@ -87,8 +87,8 @@ else
endif

install: go.sum
go install -mod=readonly $(BUILD_FLAGS) ./cmd/gaiad
go install -mod=readonly $(BUILD_FLAGS) ./cmd/gaiacli
go install $(BUILD_FLAGS) ./cmd/gaiad
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

mmmm how comes? Did you have any issues with -mod=readonly? Is there anything I can help out with?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure! I was having issues with -mod=readonly!

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this the case @alessio?

go install $(BUILD_FLAGS) ./cmd/gaiacli

install-debug: go.sum
go install -mod=readonly $(BUILD_FLAGS) ./cmd/gaiadebug
Expand Down
51 changes: 39 additions & 12 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ import (
distr "github.com/cosmos/cosmos-sdk/x/distribution"
"github.com/cosmos/cosmos-sdk/x/genutil"
"github.com/cosmos/cosmos-sdk/x/gov"
"github.com/cosmos/cosmos-sdk/x/ibc"
mockrecv "github.com/cosmos/cosmos-sdk/x/ibc/mock/recv"
mocksend "github.com/cosmos/cosmos-sdk/x/ibc/mock/send"
"github.com/cosmos/cosmos-sdk/x/mint"
"github.com/cosmos/cosmos-sdk/x/params"
paramsclient "github.com/cosmos/cosmos-sdk/x/params/client"
Expand Down Expand Up @@ -53,6 +56,9 @@ var (
crisis.AppModuleBasic{},
slashing.AppModuleBasic{},
supply.AppModuleBasic{},
ibc.AppModuleBasic{},
mocksend.AppModuleBasic{},
mockrecv.AppModuleBasic{},
)

// module account permissions
Expand Down Expand Up @@ -91,16 +97,19 @@ type GaiaApp struct {
tkeys map[string]*sdk.TransientStoreKey

// keepers
accountKeeper auth.AccountKeeper
bankKeeper bank.Keeper
supplyKeeper supply.Keeper
stakingKeeper staking.Keeper
slashingKeeper slashing.Keeper
mintKeeper mint.Keeper
distrKeeper distr.Keeper
govKeeper gov.Keeper
crisisKeeper crisis.Keeper
paramsKeeper params.Keeper
accountKeeper auth.AccountKeeper
bankKeeper bank.Keeper
supplyKeeper supply.Keeper
stakingKeeper staking.Keeper
slashingKeeper slashing.Keeper
mintKeeper mint.Keeper
distrKeeper distr.Keeper
govKeeper gov.Keeper
crisisKeeper crisis.Keeper
paramsKeeper params.Keeper
ibcKeeper ibc.Keeper
ibcMockSendKeeper mocksend.Keeper
ibcMockRecvKeeper mockrecv.Keeper

// the module manager
mm *module.Manager
Expand All @@ -122,7 +131,7 @@ func NewGaiaApp(logger log.Logger, db dbm.DB, traceStore io.Writer, loadLatest b
keys := sdk.NewKVStoreKeys(
bam.MainStoreKey, auth.StoreKey, staking.StoreKey,
supply.StoreKey, mint.StoreKey, distr.StoreKey, slashing.StoreKey,
gov.StoreKey, params.StoreKey,
gov.StoreKey, params.StoreKey, ibc.StoreKey, mocksend.ModuleName, mockrecv.ModuleName,
)
tkeys := sdk.NewTransientStoreKeys(staking.TStoreKey, params.TStoreKey)

Expand Down Expand Up @@ -159,6 +168,9 @@ func NewGaiaApp(logger log.Logger, db dbm.DB, traceStore io.Writer, loadLatest b
app.cdc, keys[slashing.StoreKey], &stakingKeeper, slashingSubspace, slashing.DefaultCodespace,
)
app.crisisKeeper = crisis.NewKeeper(crisisSubspace, invCheckPeriod, app.supplyKeeper, auth.FeeCollectorName)
app.ibcKeeper = ibc.NewKeeper(app.cdc, keys[ibc.StoreKey])
app.ibcMockSendKeeper = mocksend.NewKeeper(app.cdc, keys[mocksend.ModuleName], app.ibcKeeper.Port(mocksend.ModuleName))
app.ibcMockRecvKeeper = mockrecv.NewKeeper(app.cdc, keys[mockrecv.ModuleName], app.ibcKeeper.Port(mockrecv.ModuleName))

// register the proposal types
govRouter := gov.NewRouter()
Expand Down Expand Up @@ -189,6 +201,9 @@ func NewGaiaApp(logger log.Logger, db dbm.DB, traceStore io.Writer, loadLatest b
mint.NewAppModule(app.mintKeeper),
slashing.NewAppModule(app.slashingKeeper, app.stakingKeeper),
staking.NewAppModule(app.stakingKeeper, app.accountKeeper, app.supplyKeeper),
ibc.NewAppModule(app.ibcKeeper),
mocksend.NewAppModule(app.ibcMockSendKeeper),
mockrecv.NewAppModule(app.ibcMockRecvKeeper),
)

// During begin block slashing happens after distr.BeginBlocker so that
Expand Down Expand Up @@ -233,7 +248,19 @@ func NewGaiaApp(logger log.Logger, db dbm.DB, traceStore io.Writer, loadLatest b
// initialize BaseApp
app.SetInitChainer(app.InitChainer)
app.SetBeginBlocker(app.BeginBlocker)
app.SetAnteHandler(auth.NewAnteHandler(app.accountKeeper, app.supplyKeeper, auth.DefaultSigVerificationGasConsumer))

authante := auth.NewAnteHandler(app.accountKeeper, app.supplyKeeper, auth.DefaultSigVerificationGasConsumer)
ibcante := ibc.NewAnteHandler(app.ibcKeeper.Channel())
app.SetAnteHandler(func(ctx sdk.Context, tx sdk.Tx, simulate bool) (sdk.Context, sdk.Result, bool) {
// TODO: antedecorator
newCtx, res, abort := authante(ctx, tx, simulate)
if abort {
return newCtx, res, abort
}

newCtx, _, abort = ibcante(newCtx, tx, simulate)
return newCtx, res, abort
})
app.SetEndBlocker(app.EndBlocker)

if loadLatest {
Expand Down
207 changes: 207 additions & 0 deletions cmd/gaiacli/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,207 @@
# IBC instruction

## Dependencies

This branch uses non-canonical branch of cosmos-sdk. Before building, run `go mod vendor` on the root directory to retrieve the dependencies. To build:

```shell
git clone git@github.com:cosmos/gaia
cd gaia
git checkout joon/ibc-gaia-interface
go mod vendor
make install
gaiad version
gaiacli version
```

## Environment Setup

Stub out testnet files for 2 nodes, this example does so in your $HOME directory:

```shell
cd ~ && mkdir ibc-testnets && cd ibc-testnets
gaiad testnet -o ibc0 --v 1 --chain-id ibc0 --node-dir-prefix n
gaiad testnet -o ibc1 --v 1 --chain-id ibc1 --node-dir-prefix n
```

### Set `gaiad` and `gaiacli` Configuation

Fix the configuration files for both `gaiad` and `gaiacli` to allow both chains/nodes to run on the same machine:

```bash
# Configure the proper database backend for each node and different listening ports
sed -i '' 's/"leveldb"/"goleveldb"/g' ibc0/n0/gaiad/config/config.toml
sed -i '' 's/"leveldb"/"goleveldb"/g' ibc1/n0/gaiad/config/config.toml
sed -i '' 's#"tcp://0.0.0.0:26656"#"tcp://0.0.0.0:26556"#g' ibc1/n0/gaiad/config/config.toml
sed -i '' 's#"tcp://0.0.0.0:26657"#"tcp://0.0.0.0:26557"#g' ibc1/n0/gaiad/config/config.toml
sed -i '' 's#"localhost:6060"#"localhost:6061"#g' ibc1/n0/gaiad/config/config.toml
sed -i '' 's#"tcp://127.0.0.1:26658"#"tcp://127.0.0.1:26558"#g' ibc1/n0/gaiad/config/config.toml
gaiacli config --home ibc0/n0/gaiacli/ chain-id ibc0
gaiacli config --home ibc1/n0/gaiacli/ chain-id ibc1
gaiacli config --home ibc0/n0/gaiacli/ output json
gaiacli config --home ibc1/n0/gaiacli/ output json
gaiacli config --home ibc0/n0/gaiacli/ node http://localhost:26657
gaiacli config --home ibc1/n0/gaiacli/ node http://localhost:26557
```

Add keys from each chain to the other and make sure that the key at `ibc1/n0/gaiacli/key_seed.json` is named `n1` on each `gaiacli` instance and the same for `n0`. After this is complete the results of `gaiacli keys list` from each chain should be identical. The following are instructions for how to do this on Mac:

```bash
# These commands copy the seed phrase from each dir into the clipboard on mac
jq -r '.secret' ibc0/n0/gaiacli/key_seed.json | pbcopy
jq -r '.secret' ibc1/n0/gaiacli/key_seed.json | pbcopy

# Remove the key n0 on ibc1
gaiacli --home ibc1/n0/gaiacli keys delete n0

# seed from ibc1/n0/gaiacli/key_seed.json -> ibc0/n1
gaiacli --home ibc0/n0/gaiacli keys add n1 --recover

# seed from ibc0/n0/gaiacli/key_seed.json -> ibc1/n0
gaiacli --home ibc1/n0/gaiacli keys add n0 --recover

# seed from ibc1/n0/gaiacli/key_seed.json -> ibc1/n1
gaiacli --home ibc1/n0/gaiacli keys add n1 --recover

# Ensure keys match
gaiacli --home ibc0/n0/gaiacli keys list | jq '.[].address'
gaiacli --home ibc1/n0/gaiacli keys list | jq '.[].address'
```

After configuration is complete, start your `gaiad` processes:

```bash
nohup gaiad --home ibc0/n0/gaiad start > ibc0.log &
nohup gaiad --home ibc1/n0/gaiad start > ibc1.log &
```

## IBC Command Sequence

### Client Creation

Create IBC clients on each chain using the following commands. Note that we are using the consensus state of `ibc1` to create the client on `ibc0` and visa-versa. These "roots of trust" are used to validate transactions coming from the other chain. They will be updated periodically during handshakes and will require update at least once per unbonding period:

```bash
# client for chain ibc1 on chain ibc0
gaiacli --home ibc0/n0/gaiacli \
tx ibc client create c0 \
$(gaiacli --home ibc1/n0/gaiacli q ibc client consensus-state) \
--from n0 -y -o text

# client for chain ibc0 on chain ibc1
gaiacli --home ibc1/n0/gaiacli \
tx ibc client create c1 \
$(gaiacli --home ibc0/n0/gaiacli q ibc client consensus-state) \
--from n1 -y -o text
```

To query details about the clients use the following commands :

```bash
gaiacli --home ibc0/n0/gaiacli q ibc client client c0 --indent
gaiacli --home ibc1/n0/gaiacli q ibc client client c1 --indent
```

### Connection Creation

In order to send transactions using IBC there are two different handshakes that must be performed. First there is a `connection` created between the two chains. Once the connection is created, an application specific `channel` handshake is performed which allows the transfer of application specific data. Examples of applications are token transfer, cross-chain validation, cross-chain accounts, and in this tutorial `ibc-mock`.

Create a `connection` with the following command:

> NOTE: This command broadcasts a total of 7 transactions between the two chains from 2 different wallets. At the start of the command you will be prompted for passwords for the two different keys. The command may then take some time. Please wait for the command to return!

```shell
gaiacli \
--home ibc0/n0/gaiacli \
tx ibc connection handshake \
conn0 c0 $(gaiacli --home ibc1/n0/gaiacli q ibc client path) \
conn1 c1 $(gaiacli --home ibc0/n0/gaiacli q ibc client path) \
--chain-id2 ibc1 \
--from1 n0 --from2 n1 \
--node1 tcp://localhost:26657 \
--node2 tcp://localhost:26557
```

Once the connection is established you should be able to query it:

```bash
gaiacli --home ibc0/n0/gaiacli q ibc connection connection conn0 --indent --trust-node
gaiacli --home ibc1/n0/gaiacli q ibc connection connection conn1 --indent --trust-node
```

### Channel

Now that the `connection` has been created, it's time to establish a `channel` for the `ibc-mock` application protocol. This will allow sending of data between `ibc0` and `ibc1`. To create the `channel`, run the following command:

> NOTE: This command broadcasts a total of 7 transactions between the two chains from 2 different wallets. At the start of the command you will be prompted for passwords for the two different keys. The command may then take some time. Please wait for the command to return!

```bash
gaiacli \
--home ibc0/n0/gaiacli \
tx ibc channel handshake \
ibcmocksend chan0 conn0 \
ibcmockrecv chan1 conn1 \
--node1 tcp://localhost:26657 \
--node2 tcp://localhost:26557 \
--chain-id2 ibc1 \
--from1 n0 --from2 n1
```

You can query the `channel` after establishment by running the following command:

```bash
gaiacli --home ibc0/n0/gaiacli query ibc channel channel ibcmocksend chan0 --indent --trust-node
gaiacli --home ibc1/n0/gaiacli query ibc channel channel ibcmockrecv chan1 --indent --trust-node
```

## Send Packet
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Both send-packet commands are using ibc0, but all the other sections have matched pairs of ibc0/ibc1 commands. Is this intentional? I'm guessing it's just a typo.

Copy link
Member

@jackzampolin jackzampolin Oct 3, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How should the commands look? This part of the instructions are left over from the loopback demo

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

flush command is currently using ibc0 as its home but making transaction on ibc1, which is bit misleading

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mossid so how would you rewrite the example?


To send a packet using the `ibc-mock` application protocol, you need to know the channel you plan to send on, as well as the sequence number on the channel. To get the sequence you use the following commands:

```bash
# Returns the last sequence number
gaiacli --home ibc0/n0/gaiacli q ibcmocksend sequence chan0

# Returns the next expected sequence number, for use in scripting
gaiacli --home ibc0/n0/gaiacli q ibcmocksend next chan0
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On a fresh install:

Running gaiacli --home ibc0/n0/gaiacli q ibcmocksend sequence chan0 returns 0

Running gaiacli --home ibc0/n0/gaiacli q ibcmocksend next chan0 immediately afterward returns 2 (without having sent any packets)

I was able to reproduce the above after stopping the testnet gaiad processes, removing the entire ibc-testnets directory, and starting over. Will try on a new machine tomorrow (currently, on a Mac).

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wanted to add that manually inputting the correct sequence number (should be 1 instead of 2) to successfully send a packet the first time, then using the above to get subsequent "next sequence" numbers works.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

https://github.com/cosmos/cosmos-sdk/blob/e69029151525422924e384cd9175778fa8533c51/x/ibc/mock/send/client/cli/query.go#L70

In L74 of the function add 1 to res, so if value is nil, res should be set to 0 instead of 1.

```

Now you are ready to send an `ibc-mock` packet down the channel (`chan0`) from chain `ibc0` to chain `ibc1`! To do so run the following commands to send a packet down the channel:

```bash
gaiacli --home ibc0/n0/gaiacli tx ibcmocksend sequence chan0 $(gaiacli --home ibc0/n0/gaiacli q ibcmocksend next chan0) --from n0 -o text -y
```

### Receive Packet

Once packets are sent, receipt must be confirmed on the destination chain. To `pull` the packets from `ibc0` on `ibc1`, run the following command:

```bash
gaiacli \
--home ibc1/n0/gaiacli \
tx ibc channel pull ibcmockrecv chan1 \
--node1 tcp://localhost:26557 \
--node2 tcp://localhost:26657 \
--chain-id2 ibc0 \
--from n1
```

This command sends two transactions. You should see the reciept of the packet:

```
ibc1 <- update-client [OK] txid(7D4B4DE7A6B8E1045CA7BEB16E21DD0491BED000E5FB0D05BBB7960AABE5CC78) client(c1)
ibc1 <- empty-packet [OK] txid(6E90B9CE19394D7D41CF55E4ADCC94D6169B476B45527F9C47346080C85A289F) packets(1)
```

> Note: This command pushes all the packets out of the channel with one command. Try pushing a **couple of packets** from `ibc0` to `ibc1` then fulshing them at once. You should see output like:

```
ibc1 <- update-client [OK] txid(21E0CE99A21DD7630A7DDE62459DD82C4051CC46B231A7B31529928B1B1B2C53) client(c1)
ibc1 <- empty-packet [OK] txid(92D76EF46FDCB3739DB06960BECCD7DA30AAA6AECA687DF4D92CC272D4941F7E) packets(2)
```

Once the packets have been sent, you can check the updated sequence by running:

```bash
gaiacli --home ibc1/n0/gaiacli q ibcmockrecv sequence chan1 --trust-node
```
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ go 1.13

require (
github.com/btcsuite/btcd v0.0.0-20190807005414-4063feeff79a // indirect
github.com/cosmos/cosmos-sdk v0.34.4-0.20191002161644-532ea18724d2
github.com/cosmos/cosmos-sdk v0.34.4-0.20191015002325-ee5d7f3d62d9
github.com/cosmos/go-bip39 v0.0.0-20180819234021-555e2067c45d // indirect
github.com/golang/mock v1.3.1 // indirect
github.com/onsi/ginkgo v1.8.0 // indirect
Expand All @@ -20,7 +20,7 @@ require (
github.com/spf13/viper v1.4.0
github.com/stretchr/testify v1.4.0
github.com/tendermint/go-amino v0.15.0
github.com/tendermint/tendermint v0.32.5
github.com/tendermint/tendermint v0.32.3
github.com/tendermint/tm-db v0.2.0
golang.org/x/crypto v0.0.0-20190909091759-094676da4a83 // indirect
golang.org/x/net v0.0.0-20190827160401-ba9fcec4b297 // indirect
Expand Down
Loading