Skip to content

Commit

Permalink
Merge pull request #1481: IBC MVP Refactor
Browse files Browse the repository at this point in the history
in progress

in progress

fix all keeper/handler/types, start working on test

finalize rebase

fixing clients

add linear_cli.go, refactor relayer cli

fix examples

add wire, msg naming format, fix examples

in progress

in progress

add client/store

in progress

in progress

cleanup

finalize cleanup

fix errors

refactor ibc
  • Loading branch information
mossid committed Sep 20, 2018
1 parent 8721dd6 commit cb492f6
Show file tree
Hide file tree
Showing 30 changed files with 1,272 additions and 265 deletions.
1 change: 0 additions & 1 deletion client/lcd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,5 @@ func createHandler(cdc *codec.Codec) http.Handler {
stake.RegisterRoutes(cliCtx, r, cdc, kb)
slashing.RegisterRoutes(cliCtx, r, cdc, kb)
gov.RegisterRoutes(cliCtx, r, cdc)

return r
}
9 changes: 5 additions & 4 deletions examples/basecoin/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ type BasecoinApp struct {
accountMapper auth.AccountMapper
feeCollectionKeeper auth.FeeCollectionKeeper
bankKeeper bank.Keeper
ibcMapper ibc.Mapper
ibcKeeper ibc.Keeper
}

// NewBasecoinApp returns a reference to a new BasecoinApp given a logger and
Expand Down Expand Up @@ -67,13 +67,14 @@ func NewBasecoinApp(logger log.Logger, db dbm.DB, baseAppOptions ...func(*bam.Ba
return &types.AppAccount{}
},
)
app.bankKeeper = bank.NewBaseKeeper(app.accountMapper)
app.ibcMapper = ibc.NewMapper(app.cdc, app.keyIBC, app.RegisterCodespace(ibc.DefaultCodespace))

app.coinKeeper = bank.NewKeeper(app.accountMapper)
app.ibcKeeper = ibc.NewKeeper(app.cdc, app.keyIBC, app.RegisterCodespace(ibc.DefaultCodespace))

// register message routes
app.Router().
AddRoute("bank", bank.NewHandler(app.bankKeeper)).
AddRoute("ibc", ibc.NewHandler(app.ibcMapper, app.bankKeeper))
AddRoute("ibc", ibc.NewHandler(app.ibcKeeper))

// perform initialization logic
app.SetInitChainer(app.initChainer)
Expand Down
2 changes: 1 addition & 1 deletion examples/basecoin/cmd/basecli/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func main() {
rootCmd.AddCommand(
client.PostCommands(
bankcmd.SendTxCmd(cdc),
ibccmd.IBCTransferCmd(cdc),
bankcmd.IBCSendTxCmd(cdc),
ibccmd.IBCRelayCmd(cdc),
stakecmd.GetCmdCreateValidator(cdc),
stakecmd.GetCmdEditValidator(cdc),
Expand Down
6 changes: 3 additions & 3 deletions examples/democoin/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ type DemocoinApp struct {
bankKeeper bank.Keeper
coolKeeper cool.Keeper
powKeeper pow.Keeper
ibcMapper ibc.Mapper
ibcKeeper ibc.Keeper
stakeKeeper simplestake.Keeper

// Manage getting and setting accounts
Expand Down Expand Up @@ -78,14 +78,14 @@ func NewDemocoinApp(logger log.Logger, db dbm.DB) *DemocoinApp {
app.bankKeeper = bank.NewBaseKeeper(app.accountMapper)
app.coolKeeper = cool.NewKeeper(app.capKeyMainStore, app.bankKeeper, app.RegisterCodespace(cool.DefaultCodespace))
app.powKeeper = pow.NewKeeper(app.capKeyPowStore, pow.NewConfig("pow", int64(1)), app.bankKeeper, app.RegisterCodespace(pow.DefaultCodespace))
app.ibcMapper = ibc.NewMapper(app.cdc, app.capKeyIBCStore, app.RegisterCodespace(ibc.DefaultCodespace))
app.ibcKeeper = ibc.NewKeeper(app.cdc, app.capKeyIBCStore, app.RegisterCodespace(ibc.DefaultCodespace))
app.stakeKeeper = simplestake.NewKeeper(app.capKeyStakingStore, app.bankKeeper, app.RegisterCodespace(simplestake.DefaultCodespace))
app.Router().
AddRoute("bank", bank.NewHandler(app.bankKeeper)).
AddRoute("cool", cool.NewHandler(app.coolKeeper)).
AddRoute("pow", app.powKeeper.Handler).
AddRoute("sketchy", sketchy.NewHandler()).
AddRoute("ibc", ibc.NewHandler(app.ibcMapper, app.bankKeeper)).
AddRoute("ibc", ibc.NewHandler(app.ibcKeeper)).
AddRoute("simplestake", simplestake.NewHandler(app.stakeKeeper))

// Initialize BaseApp.
Expand Down
5 changes: 1 addition & 4 deletions examples/democoin/cmd/democli/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,7 @@ func main() {
rootCmd.AddCommand(
client.PostCommands(
bankcmd.SendTxCmd(cdc),
)...)
rootCmd.AddCommand(
client.PostCommands(
ibccmd.IBCTransferCmd(cdc),
bankcmd.IBCSendTxCmd(cdc),
)...)
rootCmd.AddCommand(
client.PostCommands(
Expand Down
43 changes: 43 additions & 0 deletions types/lib/value.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package lib

import (
"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
)

type Value struct {
store sdk.KVStore
cdc *codec.Codec
key []byte
}

func NewValue(store sdk.KVStore, cdc *codec.Codec, key []byte) Value {
return Value{
store: store,
cdc: cdc,
key: key,
}
}

func (v Value) MustGet(ptr interface{}) {
bz := v.store.Get(v.key)
v.cdc.MustUnmarshalBinary(bz, ptr)
}

func (v Value) Get(ptr interface{}) bool {
bz := v.store.Get(v.key)
if bz == nil {
return false
}
v.cdc.MustUnmarshalBinary(bz, ptr)
return true
}

func (v Value) Has() bool {
bz := v.store.Get(v.key)
return bz != nil
}

func (v Value) Set(val interface{}) {
v.store.Set(v.key, v.cdc.MustMarshalBinary(val))
}
7 changes: 7 additions & 0 deletions x/bank/client/cli/flags.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package cli

const (
FlagTo = "to"
FlagAmount = "amount"
FlagDestChain = "dest-chain-id"
)
34 changes: 16 additions & 18 deletions x/ibc/client/cli/ibctx.go → x/bank/client/cli/ibctx.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"encoding/hex"
"os"

"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/context"
"github.com/cosmos/cosmos-sdk/client/utils"
codec "github.com/cosmos/cosmos-sdk/codec"
Expand All @@ -15,16 +14,12 @@ import (

"github.com/spf13/cobra"
"github.com/spf13/viper"
)

const (
flagTo = "to"
flagAmount = "amount"
flagChain = "chain"
"github.com/cosmos/cosmos-sdk/x/bank"
)

// IBCTransferCmd implements the IBC transfer command.
func IBCTransferCmd(cdc *codec.Codec) *cobra.Command {
// IBC transfer command
func IBCSendTxCmd(cdc *wire.Codec) *cobra.Command {
cmd := &cobra.Command{
Use: "transfer",
RunE: func(cmd *cobra.Command, args []string) error {
Expand All @@ -51,32 +46,35 @@ func IBCTransferCmd(cdc *codec.Codec) *cobra.Command {
},
}

cmd.Flags().String(flagTo, "", "Address to send coins")
cmd.Flags().String(flagAmount, "", "Amount of coins to send")
cmd.Flags().String(flagChain, "", "Destination chain to send coins")

cmd.Flags().String(FlagTo, "", "Address to send coins")
cmd.Flags().String(FlagAmount, "", "Amount of coins to send")
cmd.Flags().String(FlagDestChain, "", "Destination chain to send coins")
return cmd
}

func buildMsg(from sdk.AccAddress) (sdk.Msg, error) {
amount := viper.GetString(flagAmount)
amount := viper.GetString(FlagAmount)
coins, err := sdk.ParseCoins(amount)
if err != nil {
return nil, err
}

dest := viper.GetString(flagTo)
dest := viper.GetString(FlagTo)
bz, err := hex.DecodeString(dest)
if err != nil {
return nil, err
}
to := sdk.AccAddress(bz)

packet := ibc.NewIBCPacket(from, to, coins, viper.GetString(client.FlagChainID),
viper.GetString(flagChain))
payload := bank.PayloadSend{
SrcAddr: from,
DestAddr: to,
Coins: coins,
}

msg := ibc.IBCTransferMsg{
IBCPacket: packet,
msg := bank.MsgIBCSend{
PayloadSend: payload,
DestChain: viper.GetString(FlagDestChain),
}

return msg, nil
Expand Down
15 changes: 4 additions & 11 deletions x/bank/client/cli/sendtx.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,7 @@ import (
"github.com/spf13/viper"
)

const (
flagTo = "to"
flagAmount = "amount"
)

// SendTxCmd will create a send tx and sign it with the given key.
// SendTxCmd will create a send tx and sign it with the given key
func SendTxCmd(cdc *codec.Codec) *cobra.Command {
cmd := &cobra.Command{
Use: "send",
Expand All @@ -38,14 +33,13 @@ func SendTxCmd(cdc *codec.Codec) *cobra.Command {
}

toStr := viper.GetString(flagTo)

to, err := sdk.AccAddressFromBech32(toStr)
if err != nil {
return err
}

// parse coins trying to be sent
amount := viper.GetString(flagAmount)
amount := viper.GetString(FlagAmount)
coins, err := sdk.ParseCoins(amount)
if err != nil {
return err
Expand Down Expand Up @@ -76,8 +70,7 @@ func SendTxCmd(cdc *codec.Codec) *cobra.Command {
},
}

cmd.Flags().String(flagTo, "", "Address to send coins")
cmd.Flags().String(flagAmount, "", "Amount of coins to send")

cmd.Flags().String(FlagTo, "", "Address to send coins")
cmd.Flags().String(FlagAmount, "", "Amount of coins to send")
return cmd
}
16 changes: 16 additions & 0 deletions x/bank/client/rest/rest.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package rest

import (
"github.com/gorilla/mux"

"github.com/cosmos/cosmos-sdk/crypto/keys"

"github.com/cosmos/cosmos-sdk/client/context"
"github.com/cosmos/cosmos-sdk/codec"
)

// RegisterRoutes - Central function to define routes that get registered by the main application
func RegisterRoutes(cliCtx context.CLIContext, r *mux.Router, cdc *codec.Codec, kb keys.Keybase) {

r.HandleFunc("/accounts/{address}/send", SendRequestHandlerFn(cdc, kb, cliCtx)).Methods("POST")
}
6 changes: 0 additions & 6 deletions x/bank/client/rest/sendtx.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,6 @@ import (
"github.com/gorilla/mux"
)

// RegisterRoutes - Central function to define routes that get registered by the main application
func RegisterRoutes(cliCtx context.CLIContext, r *mux.Router, cdc *codec.Codec, kb keys.Keybase) {
r.HandleFunc("/accounts/{address}/send", SendRequestHandlerFn(cdc, kb, cliCtx)).Methods("POST")
r.HandleFunc("/tx/broadcast", BroadcastTxRequestHandlerFn(cdc, cliCtx)).Methods("POST")
}

type sendBody struct {
// fees is not used currently
// Fees sdk.Coin `json="fees"`
Expand Down
53 changes: 53 additions & 0 deletions x/bank/msgs.go
Original file line number Diff line number Diff line change
Expand Up @@ -226,3 +226,56 @@ func NewOutput(addr sdk.AccAddress, coins sdk.Coins) Output {
}
return output
}

//------------------------------------
// MsgIBCSend - IBC transaction of the coin module

// Implements sdk.Msg
type MsgIBCSend struct {
PayloadSend `json:"payload-send"`
DestChain string `json:"dest-chain"`
}

func (msg MsgIBCSend) GetSignBytes() []byte {
bin, err := msgCdc.MarshalJSON(msg)
if err != nil {
panic(err)
}
return bin
}

func (msg MsgIBCSend) GetSigners() []sdk.AccAddress {
return []sdk.AccAddress{msg.PayloadSend.SrcAddr}
}

//------------------------------------
// PayloadSend - payload for IBC sending

// Implements ibc.Payload
type PayloadSend struct {
SrcAddr sdk.AccAddress `json:"src-addr"`
DestAddr sdk.AccAddress `json:"dest-addr"`
Coins sdk.Coins `json:"coins"`
}

func (p PayloadSend) Type() string {
return "bank"
}

func (p PayloadSend) ValidateBasic() sdk.Error {
if !p.Coins.IsValid() {
return sdk.ErrInvalidCoins(p.Coins.String())
}
if !p.Coins.IsPositive() {
return sdk.ErrInvalidCoins(p.Coins.String())
}
return nil
}

//-=-------------------------------------
// ReceiptSendFail

// Implements sdk.Receipt
type ReceiptSendFail struct {
PayloadSend
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,9 @@ import (
authtxb "github.com/cosmos/cosmos-sdk/x/auth/client/txbuilder"
"github.com/cosmos/cosmos-sdk/x/ibc"

"github.com/gorilla/mux"
"github.com/cosmos/cosmos-sdk/x/ibc/bank"
)

// RegisterRoutes - Central function to define routes that get registered by the main application
func RegisterRoutes(cliCtx context.CLIContext, r *mux.Router, cdc *codec.Codec, kb keys.Keybase) {
r.HandleFunc("/ibc/{destchain}/{address}/send", TransferRequestHandlerFn(cdc, kb, cliCtx)).Methods("POST")
}

type transferBody struct {
// Fees sdk.Coin `json="fees"`
Amount sdk.Coins `json:"amount"`
Expand Down Expand Up @@ -67,9 +62,24 @@ func TransferRequestHandlerFn(cdc *codec.Codec, kb keys.Keybase, cliCtx context.
return
}

from, err := sdk.AccAddressFromBech32(string(info.GetPubKey().Address()))
if err != nil {
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte(err.Error()))
return
}

// build message
packet := ibc.NewIBCPacket(sdk.AccAddress(info.GetPubKey().Address()), to, m.Amount, m.SrcChainID, destChainID)
msg := ibc.IBCTransferMsg{packet}
p := bank.PayloadCoins{
SrcAddr: from,
DestAddr: to,
Coins: m.Amount,
}

msg := ibc.MsgSend{
Payload: p,
DestChain: destChainID,
}

simulateGas, gas, err := client.ReadGasFlag(m.Gas)
if err != nil {
Expand Down
14 changes: 14 additions & 0 deletions x/ibc/bank/client/rest/rest.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package rest

import (
"github.com/gorilla/mux"

"github.com/cosmos/cosmos-sdk/crypto/keys"

"github.com/cosmos/cosmos-sdk/client/context"
"github.com/cosmos/cosmos-sdk/wire"
)

func RegisterRoutes(cliCtx context.CLIContext, r *mux.Router, cdc *wire.Codec, kb keys.Keybase) {
r.HandleFunc("/ibc/{destchain}/{address}/send", TransferRequestHandlerFn(cdc, kb, cliCtx)).Methods("POST")
}
Loading

0 comments on commit cb492f6

Please sign in to comment.