Skip to content

Commit

Permalink
feat: #258 Register Counterparty Address (#376)
Browse files Browse the repository at this point in the history
* feat: adding MsgServer for RegisterCounterPartyAddress &
EscrowPacketFree

* test: adding test for ValidateBasic

* fix: removing validate basic check

* fix: removing empty file

* Update modules/apps/29-fee/keeper/msg_server.go

Co-authored-by: colin axnér <25233464+colin-axner@users.noreply.github.com>

* Update modules/apps/29-fee/types/msgs.go

Co-authored-by: colin axnér <25233464+colin-axner@users.noreply.github.com>

* Update modules/apps/29-fee/types/keys.go

Co-authored-by: colin axnér <25233464+colin-axner@users.noreply.github.com>

* Update modules/apps/29-fee/keeper/keeper.go

Co-authored-by: Aditya <adityasripal@gmail.com>

* fix: fixing typos, variable names, comments

* fix: updating import comments

* test: adding test for KeyRelayerAddress

* update: comments & key_test

* Update modules/apps/29-fee/keeper/msg_server.go

Co-authored-by: colin axnér <25233464+colin-axner@users.noreply.github.com>

* fix: error message

* docs: updating RegisterCounterpartyAddress fn description

Co-authored-by: colin axnér <25233464+colin-axner@users.noreply.github.com>
Co-authored-by: Aditya <adityasripal@gmail.com>
  • Loading branch information
3 people authored Sep 8, 2021
1 parent c63af4a commit e3704c6
Show file tree
Hide file tree
Showing 12 changed files with 222 additions and 145 deletions.
24 changes: 12 additions & 12 deletions docs/ibc/proto-docs.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@
- [ibc/applications/fee/v1/tx.proto](#ibc/applications/fee/v1/tx.proto)
- [MsgEscrowPacketFee](#ibc.applications.fee.v1.MsgEscrowPacketFee)
- [MsgEscrowPacketFeeResponse](#ibc.applications.fee.v1.MsgEscrowPacketFeeResponse)
- [MsgRegisterCounterPartyAddressResponse](#ibc.applications.fee.v1.MsgRegisterCounterPartyAddressResponse)
- [MsgRegisterCounterpartyAddress](#ibc.applications.fee.v1.MsgRegisterCounterpartyAddress)
- [MsgRegisterCounterpartyAddressResponse](#ibc.applications.fee.v1.MsgRegisterCounterpartyAddressResponse)

- [Msg](#ibc.applications.fee.v1.Msg)

Expand Down Expand Up @@ -915,16 +915,6 @@ MsgEscrowPacketFeeResponse defines the response type for Msg/EscrowPacketFee



<a name="ibc.applications.fee.v1.MsgRegisterCounterPartyAddressResponse"></a>

### MsgRegisterCounterPartyAddressResponse
MsgRegisterCounterPartyAddressResponse defines the Msg/RegisterCounteryPartyAddress response type






<a name="ibc.applications.fee.v1.MsgRegisterCounterpartyAddress"></a>

### MsgRegisterCounterpartyAddress
Expand All @@ -940,6 +930,16 @@ MsgRegisterCounterpartyAddress is the request type for registering the counter p




<a name="ibc.applications.fee.v1.MsgRegisterCounterpartyAddressResponse"></a>

### MsgRegisterCounterpartyAddressResponse
MsgRegisterCounterpartyAddressResponse defines the Msg/RegisterCounterypartyAddress response type





<!-- end messages -->

<!-- end enums -->
Expand All @@ -954,7 +954,7 @@ Msg defines the ibc/fee Msg service.

| Method Name | Request Type | Response Type | Description | HTTP Verb | Endpoint |
| ----------- | ------------ | ------------- | ------------| ------- | -------- |
| `RegisterCounterPartyAddress` | [MsgRegisterCounterpartyAddress](#ibc.applications.fee.v1.MsgRegisterCounterpartyAddress) | [MsgRegisterCounterPartyAddressResponse](#ibc.applications.fee.v1.MsgRegisterCounterPartyAddressResponse) | RegisterCounterpartyAddress defines a rpc handler method for MsgRegisterCounterpartyAddress RegisterCounterpartyAddress is called by the relayer on each channelEnd and allows them to specify their counterparty address before relaying. This ensures they will be properly compensated for forward relaying since destination chain must send back relayer's source address (counterparty address) in acknowledgement. This function may be called more than once by a relayer, in which case, latest counterparty address is always used. | |
| `RegisterCounterpartyAddress` | [MsgRegisterCounterpartyAddress](#ibc.applications.fee.v1.MsgRegisterCounterpartyAddress) | [MsgRegisterCounterpartyAddressResponse](#ibc.applications.fee.v1.MsgRegisterCounterpartyAddressResponse) | RegisterCounterpartyAddress defines a rpc handler method for MsgRegisterCounterpartyAddress RegisterCounterpartyAddress is called by the relayer on each channelEnd and allows them to specify their counterparty address before relaying. This ensures they will be properly compensated for forward relaying since destination chain must send back relayer's source address (counterparty address) in acknowledgement. This function may be called more than once by a relayer, in which case, latest counterparty address is always used. | |
| `EscrowPacketFee` | [MsgEscrowPacketFee](#ibc.applications.fee.v1.MsgEscrowPacketFee) | [MsgEscrowPacketFeeResponse](#ibc.applications.fee.v1.MsgEscrowPacketFeeResponse) | EscrowPacketFee defines a rpc handler method for MsgEscrowPacketFee EscrowPacketFee is an open callback that may be called by any module/user that wishes to escrow funds in order to incentivize the relaying of the given packet. | |

<!-- end services -->
Expand Down
4 changes: 2 additions & 2 deletions modules/apps/29-fee/client/cli/cli.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package cli

import (
"github.com/spf13/cobra"

// external library imports
"github.com/cosmos/cosmos-sdk/client"
"github.com/spf13/cobra"
)

// GetQueryCmd returns the query commands for 29-fee
Expand Down
37 changes: 25 additions & 12 deletions modules/apps/29-fee/keeper/keeper.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
package keeper

/*
import (
"github.com/tendermint/tendermint/libs/log"
// external library imports
"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
capabilitykeeper "github.com/cosmos/cosmos-sdk/x/capability/keeper"
capabilitytypes "github.com/cosmos/cosmos-sdk/x/capability/types"
paramtypes "github.com/cosmos/cosmos-sdk/x/params/types"
"github.com/cosmos/ibc-go/modules/apps/transfer/types"
"github.com/tendermint/tendermint/libs/log"

// ibc-go imports
"github.com/cosmos/ibc-go/modules/apps/29-fee/types"
host "github.com/cosmos/ibc-go/modules/core/24-host"
)

Expand All @@ -25,7 +25,6 @@ type Keeper struct {
scopedKeeper capabilitykeeper.ScopedKeeper
}

// NewKeeper creates a new 29-fee Keeper instance
func NewKeeper(
cdc codec.BinaryCodec, key sdk.StoreKey, paramSpace paramtypes.Subspace,
Expand All @@ -49,6 +48,7 @@ func (k Keeper) Logger(ctx sdk.Context) log.Logger {
return ctx.Logger().With("module", "x/"+host.ModuleName+"-"+types.ModuleName)
}

/*
// IsBound checks if the transfer module is already bound to the desired port
func (k Keeper) IsBound(ctx sdk.Context, portID string) bool {
_, ok := k.scopedKeeper.GetCapability(ctx, host.PortPath(portID))
Expand All @@ -62,12 +62,6 @@ func (k Keeper) BindPort(ctx sdk.Context, portID string) error {
return k.ClaimCapability(ctx, cap, host.PortPath(portID))
}
// GetPort returns the portID for the transfer module. Used in ExportGenesis
func (k Keeper) GetPort(ctx sdk.Context) string {
store := ctx.KVStore(k.storeKey)
return string(store.Get(types.PortKey))
}
// SetPort sets the portID for the transfer module. Used in InitGenesis
func (k Keeper) SetPort(ctx sdk.Context, portID string) {
store := ctx.KVStore(k.storeKey)
Expand All @@ -85,3 +79,22 @@ func (k Keeper) ClaimCapability(ctx sdk.Context, cap *capabilitytypes.Capability
return k.scopedKeeper.ClaimCapability(ctx, cap, name)
}
*/

// SetCounterpartyAddress maps the destination chain relayer address to the source relayer address
// The receiving chain must store the mapping from: address -> counterpartyAddress for the given channel
func (k Keeper) SetCounterpartyAddress(ctx sdk.Context, address, counterpartyAddress string) {
store := ctx.KVStore(k.storeKey)
store.Set(types.KeyRelayerAddress(address), []byte(counterpartyAddress))
}

// GetCounterpartyAddress gets the relayer counterparty address given a destination relayer address
func (k Keeper) GetCounterpartyAddress(ctx sdk.Context, address sdk.AccAddress) (sdk.AccAddress, bool) {
store := ctx.KVStore(k.storeKey)
key := types.KeyRelayerAddress(address.String())

if !store.Has(key) {
return []byte{}, false
}

return store.Get(key), true
}
17 changes: 3 additions & 14 deletions modules/apps/29-fee/keeper/keeper_test.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
package keeper_test

/*
import (
// standard library importsn
"testing"

// external library imports
"github.com/stretchr/testify/suite"
"github.com/tendermint/tendermint/crypto"

"github.com/cosmos/cosmos-sdk/baseapp"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/ibc-go/modules/apps/transfer/types"
// ibc-go imports
ibctesting "github.com/cosmos/ibc-go/testing"
)

Expand All @@ -31,15 +29,6 @@ func (suite *KeeperTestSuite) SetupTest() {
suite.chainC = suite.coordinator.GetChain(ibctesting.GetChainID(2))
}

func NewFeePath(chainA, chainB *ibctesting.TestChain) *ibctesting.Path {
path := ibctesting.NewPath(chainA, chainB)
path.EndpointA.ChannelConfig.PortID = ibctesting.FeePort
path.EndpointB.ChannelConfig.PortID = ibctesting.FeePort
return path
}
func TestKeeperTestSuite(t *testing.T) {
suite.Run(t, new(KeeperTestSuite))
}
*/
33 changes: 31 additions & 2 deletions modules/apps/29-fee/keeper/msg_server.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,33 @@
package keeper

// TODO
//var _ types.MsgServer = Keeper{}
import (
"context"

sdk "github.com/cosmos/cosmos-sdk/types"

"github.com/cosmos/ibc-go/modules/apps/29-fee/types"
)

var _ types.MsgServer = Keeper{}

// RegisterCounterpartyAddress is called by the relayer on each channelEnd and allows them to specify their counterparty address before relaying
// This ensures they will be properly compensated for forward relaying on the source chain since the destination chain must send back relayer's source address (counterparty address) in acknowledgement
// This function may be called more than once by relayers, in which case, the previous counterparty address will be overwritten by the new counterparty address
func (k Keeper) RegisterCounterpartyAddress(goCtx context.Context, msg *types.MsgRegisterCounterpartyAddress) (*types.MsgRegisterCounterpartyAddressResponse, error) {
ctx := sdk.UnwrapSDKContext(goCtx)

k.SetCounterpartyAddress(
ctx, msg.Address, msg.CounterpartyAddress,
)

k.Logger(ctx).Info("Registering counterparty address for relayer.", "Address:", msg.Address, "Counterparty Address:", msg.CounterpartyAddress)

return &types.MsgRegisterCounterpartyAddressResponse{}, nil
}

// EscrowPacketFee defines a rpc handler method for MsgEscrowPacketFee
// EscrowPacketFee is an open callback that may be called by any module/user that wishes to escrow funds in order to
// incentivize the relaying of the given packet.
func (k Keeper) EscrowPacketFee(goCtx context.Context, msg *types.MsgEscrowPacketFee) (*types.MsgEscrowPacketFeeResponse, error) {
return &types.MsgEscrowPacketFeeResponse{}, nil
}
5 changes: 3 additions & 2 deletions modules/apps/29-fee/types/expected_keepers.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package types

/*
import (
//external library imports
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/auth/types"
capabilitytypes "github.com/cosmos/cosmos-sdk/x/capability/types"

// ibc-go imports
connectiontypes "github.com/cosmos/ibc-go/modules/core/03-connection/types"
channeltypes "github.com/cosmos/ibc-go/modules/core/04-channel/types"
ibcexported "github.com/cosmos/ibc-go/modules/core/exported"
Expand Down Expand Up @@ -47,4 +49,3 @@ type ConnectionKeeper interface {
type PortKeeper interface {
BindPort(ctx sdk.Context, portID string) *capabilitytypes.Capability
}
*/
14 changes: 14 additions & 0 deletions modules/apps/29-fee/types/keys.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,29 @@
package types

// standard library imports
import "fmt"

const (
// ModuleName defines the 29-fee name
ModuleName = "ibcfee"

// StoreKey is the store key string for IBC transfer
StoreKey = ModuleName

// PortKey is the port id that is wrapped by fee middleware
PortKey = "feetransfer"

// RouterKey is the message route for IBC transfer
RouterKey = ModuleName

// QuerierRoute is the querier route for IBC transfer
QuerierRoute = ModuleName

// RelayerAddressKeyPrefix is the key prefix for relayer address mapping
RelayerAddressKeyPrefix = "relayerAddress"
)

// KeyRelayerAddress returns the key for relayer address -> counteryparty address mapping
func KeyRelayerAddress(address string) []byte {
return []byte(fmt.Sprintf("%s/%s", RelayerAddressKeyPrefix, address))
}
19 changes: 19 additions & 0 deletions modules/apps/29-fee/types/keys_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package types_test

import (
fmt "fmt"
"testing"

"github.com/stretchr/testify/require"

"github.com/cosmos/ibc-go/modules/apps/29-fee/types"
)

func TestKeyRelayerAddress(t *testing.T) {
var (
relayerAddress = "relayer_address"
)

key := types.KeyRelayerAddress(relayerAddress)
require.Equal(t, string(key), fmt.Sprintf("%s/relayer_address", types.RelayerAddressKeyPrefix))
}
38 changes: 25 additions & 13 deletions modules/apps/29-fee/types/msgs.go
Original file line number Diff line number Diff line change
@@ -1,32 +1,44 @@
package types

/*
import (
"strings"
// external library imports
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
clienttypes "github.com/cosmos/ibc-go/modules/core/02-client/types"
host "github.com/cosmos/ibc-go/modules/core/24-host"
)

// NewMsg
func NewMsg() *Msg {
return &Msg{
// msg types
const (
TypeMsgRegisterCounterpartyAddress = "registerCounterpartyAddress"
)

// NewMsgRegisterCounterpartyAddress creates a new instance of MsgRegisterCounterpartyAddress
func NewMsgRegisterCounterpartyAddress(address, counterpartyAddress string) *MsgRegisterCounterpartyAddress {
return &MsgRegisterCounterpartyAddress{
Address: address,
CounterpartyAddress: counterpartyAddress,
}
}

// ValidateBasic performs a basic check of the Msg fields.
func (msg Msg) ValidateBasic() error {
// ValidateBasic performs a basic check of the MsgRegisterCounterpartyAddress fields
func (msg MsgRegisterCounterpartyAddress) ValidateBasic() error {
_, err := sdk.AccAddressFromBech32(msg.Address)
if err != nil {
return sdkerrors.Wrap(err, "failed to convert msg.Address into sdk.AccAddress")
}

_, err = sdk.AccAddressFromBech32(msg.CounterpartyAddress)
if err != nil {
return sdkerrors.Wrap(err, "failed to convert msg.CounterpartyAddress into sdk.AccAddress")
}

return nil
}

// GetSigners implements sdk.Msg
func (msg MsgTransfer) GetSigners() []sdk.AccAddress {
signer, err := sdk.AccAddressFromBech32(msg.Sender)
func (msg MsgRegisterCounterpartyAddress) GetSigners() []sdk.AccAddress {
signer, err := sdk.AccAddressFromBech32(msg.Address)
if err != nil {
panic(err)
}
return []sdk.AccAddress{signer}
}
*/
38 changes: 19 additions & 19 deletions modules/apps/29-fee/types/msgs_test.go
Original file line number Diff line number Diff line change
@@ -1,36 +1,36 @@
package types

/*
import (
"fmt"
"testing"

sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/stretchr/testify/require"
"github.com/tendermint/tendermint/crypto/secp256k1"
)

"github.com/cosmos/cosmos-sdk/crypto/keys/secp256k1"
sdk "github.com/cosmos/cosmos-sdk/types"
var (
validAddr = sdk.AccAddress(secp256k1.GenPrivKey().PubKey().Address()).String()
invalidAddr = "invalid_address"
)

func (suite *TypesTestSuite) TestMsgValidateBasic() {
// TestMsgTransferValidation tests ValidateBasic for MsgTransfer
func TestMsgRegisterCountepartyAddressValidation(t *testing.T) {
testCases := []struct {
name string
msg *types.Msg
msg *MsgRegisterCounterpartyAddress
expPass bool
}{
{"", types.NewMsg(), true},
{"validate with correct sdk.AccAddress", NewMsgRegisterCounterpartyAddress(validAddr, validAddr), true},
{"validate with incorrect destination relayer address", NewMsgRegisterCounterpartyAddress(invalidAddr, validAddr), false},
{"validate with incorrect counterparty relayer address", NewMsgRegisterCounterpartyAddress(validAddr, invalidAddr), false},
}

for _, tc := range testCases {
tc := tc
suite.Run(tc.name, func() {
err := tc.msg.ValidateBasic()
if tc.expPass {
suite.Require().NoError(err)
} else {
suite.Require().Error(err)
}
})
for i, tc := range testCases {
err := tc.msg.ValidateBasic()
if tc.expPass {
require.NoError(t, err, "valid test case %d failed: %s", i, tc.name)
} else {
require.Error(t, err, "invalid test case %d passed: %s", i, tc.name)
}
}
}
*/
Loading

0 comments on commit e3704c6

Please sign in to comment.