Skip to content

Commit

Permalink
receive string instead of address at message instantiate
Browse files Browse the repository at this point in the history
  • Loading branch information
beer-1 committed Dec 26, 2023
1 parent 466679d commit e6fe984
Show file tree
Hide file tree
Showing 8 changed files with 234 additions and 117 deletions.
34 changes: 31 additions & 3 deletions x/opchild/client/cli/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,12 @@ Where proposal.json contains:
return err
}

msg, err := types.NewMsgExecuteMessages(clientCtx.GetFromAddress(), msgs)
fromAddr, err := ac.BytesToString(clientCtx.GetFromAddress())
if err != nil {
return err
}

msg, err := types.NewMsgExecuteMessages(fromAddr, msgs)
if err != nil {
return fmt.Errorf("invalid message: %w", err)
}
Expand All @@ -194,8 +199,17 @@ Where proposal.json contains:

func newBuildWithdrawMsg(clientCtx client.Context, ac address.Codec, txf tx.Factory, fs *flag.FlagSet, to sdk.AccAddress, amount sdk.Coin) (tx.Factory, *types.MsgInitiateTokenWithdrawal, error) {
sender := clientCtx.GetFromAddress()
senderAddr, err := ac.BytesToString(sender)
if err != nil {
return txf, nil, err
}

toAddr, err := ac.BytesToString(to)
if err != nil {
return txf, nil, err
}

msg := types.NewMsgInitiateTokenWithdrawal(sender, to, amount)
msg := types.NewMsgInitiateTokenWithdrawal(senderAddr, toAddr, amount)
if err := msg.Validate(ac); err != nil {
return txf, nil, err
}
Expand All @@ -207,8 +221,22 @@ func newBuildDepositMsg(clientCtx client.Context, ac address.Codec, txf tx.Facto
sequence uint64, from, to sdk.AccAddress, amount sdk.Coin, hookMsg []byte,
) (tx.Factory, *types.MsgFinalizeTokenDeposit, error) {
sender := clientCtx.GetFromAddress()
senderAddr, err := ac.BytesToString(sender)
if err != nil {
return txf, nil, err
}

fromAddr, err := ac.BytesToString(from)
if err != nil {
return txf, nil, err
}

toAddr, err := ac.BytesToString(to)
if err != nil {
return txf, nil, err
}

msg := types.NewMsgFinalizeTokenDeposit(sender, from, to, amount, sequence, hookMsg)
msg := types.NewMsgFinalizeTokenDeposit(senderAddr, fromAddr, toAddr, amount, sequence, hookMsg)
if err := msg.Validate(ac); err != nil {
return txf, nil, err
}
Expand Down
16 changes: 16 additions & 0 deletions x/opchild/keeper/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,14 @@ var (
sdk.AccAddress(pubKeys[4].Address()),
}

addrsStr = []string{
addrs[0].String(),
addrs[1].String(),
addrs[2].String(),
addrs[3].String(),
addrs[4].String(),
}

valAddrs = []sdk.ValAddress{
sdk.ValAddress(pubKeys[0].Address()),
sdk.ValAddress(pubKeys[1].Address()),
Expand All @@ -80,6 +88,14 @@ var (
sdk.ValAddress(pubKeys[4].Address()),
}

valAddrsStr = []string{
valAddrs[0].String(),
valAddrs[1].String(),
valAddrs[2].String(),
valAddrs[3].String(),
valAddrs[4].String(),
}

testDenoms = []string{
"test1",
"test2",
Expand Down
53 changes: 35 additions & 18 deletions x/opchild/keeper/msg_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,16 @@ func Test_MsgServer_ExecuteMessages(t *testing.T) {
// apply validator updates
input.OPChildKeeper.BlockValidatorUpdates(ctx)

addMsg, err := types.NewMsgAddValidator("val2", authtypes.NewModuleAddress(types.ModuleName), valAddrs[1], valPubKeys[1])
moduleAddr, err := input.AccountKeeper.AddressCodec().BytesToString(authtypes.NewModuleAddress(types.ModuleName))
require.NoError(t, err)

removeMsg, err := types.NewMsgRemoveValidator(authtypes.NewModuleAddress(types.ModuleName), valAddrs[0])
addMsg, err := types.NewMsgAddValidator("val2", moduleAddr, valAddrsStr[1], valPubKeys[1])
require.NoError(t, err)

msg, err := types.NewMsgExecuteMessages(addrs[0], []sdk.Msg{addMsg, removeMsg})
removeMsg, err := types.NewMsgRemoveValidator(moduleAddr, valAddrsStr[0])
require.NoError(t, err)

msg, err := types.NewMsgExecuteMessages(addrsStr[0], []sdk.Msg{addMsg, removeMsg})
require.NoError(t, err)

_, err = ms.ExecuteMessages(ctx, msg)
Expand All @@ -66,28 +69,31 @@ func Test_MsgServer_AddValidator(t *testing.T) {
ms := keeper.NewMsgServerImpl(input.OPChildKeeper)
valPubKeys := testutilsims.CreateTestPubKeys(2)

msg, err := types.NewMsgAddValidator("val1", authtypes.NewModuleAddress(types.ModuleName), valAddrs[0], valPubKeys[0])
moduleAddr, err := input.AccountKeeper.AddressCodec().BytesToString(authtypes.NewModuleAddress(types.ModuleName))
require.NoError(t, err)

msg, err := types.NewMsgAddValidator("val1", moduleAddr, valAddrsStr[0], valPubKeys[0])
require.NoError(t, err)

_, err = ms.AddValidator(ctx, msg)
require.NoError(t, err)

// invalid signer
msg, err = types.NewMsgAddValidator("val1", addrs[0], valAddrs[0], valPubKeys[0])
msg, err = types.NewMsgAddValidator("val1", addrsStr[0], valAddrsStr[0], valPubKeys[0])
require.NoError(t, err)

_, err = ms.AddValidator(ctx, msg)
require.Error(t, err)

// duplicate add validator
msg, err = types.NewMsgAddValidator("val1", authtypes.NewModuleAddress(types.ModuleName), valAddrs[0], valPubKeys[1])
msg, err = types.NewMsgAddValidator("val1", moduleAddr, valAddrsStr[0], valPubKeys[1])
require.NoError(t, err)

_, err = ms.AddValidator(ctx, msg)
require.Error(t, err)

// duplicate cons pubkey
msg, err = types.NewMsgAddValidator("val1", authtypes.NewModuleAddress(types.ModuleName), valAddrs[1], valPubKeys[0])
msg, err = types.NewMsgAddValidator("val1", moduleAddr, valAddrsStr[1], valPubKeys[0])
require.NoError(t, err)

_, err = ms.AddValidator(ctx, msg)
Expand All @@ -107,7 +113,7 @@ func Test_MsgServer_RemoveValidator(t *testing.T) {
input.OPChildKeeper.SetValidator(ctx, val)

// invalid signer
msg, err := types.NewMsgRemoveValidator(addrs[0], valAddrs[0])
msg, err := types.NewMsgRemoveValidator(addrsStr[0], valAddrsStr[0])
require.NoError(t, err)

_, err = ms.RemoveValidator(
Expand All @@ -116,8 +122,11 @@ func Test_MsgServer_RemoveValidator(t *testing.T) {
)
require.Error(t, err)

moduleAddr, err := input.AccountKeeper.AddressCodec().BytesToString(authtypes.NewModuleAddress(types.ModuleName))
require.NoError(t, err)

// remove not existing validator
msg, err = types.NewMsgRemoveValidator(authtypes.NewModuleAddress(types.ModuleName), valAddrs[1])
msg, err = types.NewMsgRemoveValidator(moduleAddr, valAddrsStr[1])
require.NoError(t, err)

_, err = ms.RemoveValidator(
Expand All @@ -127,7 +136,7 @@ func Test_MsgServer_RemoveValidator(t *testing.T) {
require.Error(t, err)

// valid remove validator
msg, err = types.NewMsgRemoveValidator(authtypes.NewModuleAddress(types.ModuleName), valAddrs[0])
msg, err = types.NewMsgRemoveValidator(moduleAddr, valAddrsStr[0])
require.NoError(t, err)

_, err = ms.RemoveValidator(
Expand All @@ -147,15 +156,21 @@ func Test_MsgServer_UpdateParams(t *testing.T) {
params.HistoricalEntries = 1
params.BridgeExecutor = addrs[1].String()

msg := types.NewMsgUpdateParams(authtypes.NewModuleAddress(types.ModuleName), &params)
moduleAddr, err := input.AccountKeeper.AddressCodec().BytesToString(authtypes.NewModuleAddress(types.ModuleName))
require.NoError(t, err)

msg := types.NewMsgUpdateParams(moduleAddr, &params)
_, err = ms.UpdateParams(ctx, msg)
require.NoError(t, err)
_params, err := ms.GetParams(ctx)
require.NoError(t, err)
require.Equal(t, params, _params)

// invalid signer
msg = types.NewMsgUpdateParams(authtypes.NewModuleAddress("gov"), &params)
govAddr, err := input.AccountKeeper.AddressCodec().BytesToString(authtypes.NewModuleAddress("gov"))
require.NoError(t, err)

msg = types.NewMsgUpdateParams(govAddr, &params)
require.NoError(t, err)

_, err = ms.UpdateParams(
Expand Down Expand Up @@ -199,10 +214,12 @@ func Test_MsgServer_Withdraw(t *testing.T) {

// fund asset
account := input.Faucet.NewFundedAccount(ctx, sdk.NewCoin(denom, math.NewInt(1_000_000_000)))
accountAddr, err := input.AccountKeeper.AddressCodec().BytesToString(account)
require.NoError(t, err)

// valid
msg := types.NewMsgInitiateTokenWithdrawal(account, addrs[1], sdk.NewCoin(denom, math.NewInt(100)))
_, err := ms.InitiateTokenWithdrawal(ctx, msg)
msg := types.NewMsgInitiateTokenWithdrawal(accountAddr, addrsStr[1], sdk.NewCoin(denom, math.NewInt(100)))
_, err = ms.InitiateTokenWithdrawal(ctx, msg)
require.NoError(t, err)

}
Expand All @@ -218,15 +235,15 @@ func Test_MsgServer_Deposit_NoHook(t *testing.T) {
denom := "l2/" + hex.EncodeToString(bz[:])

// unauthorized deposit
msg := types.NewMsgFinalizeTokenDeposit(addrs[1], addrs[1], addrs[1], sdk.NewCoin(denom, math.NewInt(100)), 1, nil)
msg := types.NewMsgFinalizeTokenDeposit(addrsStr[1], addrsStr[1], addrsStr[1], sdk.NewCoin(denom, math.NewInt(100)), 1, nil)
_, err := ms.FinalizeTokenDeposit(ctx, msg)
require.Error(t, err)

beforeBalance := input.BankKeeper.GetBalance(ctx, addrs[1], denom)
require.Equal(t, math.ZeroInt(), beforeBalance.Amount)

// valid deposit
msg = types.NewMsgFinalizeTokenDeposit(addrs[0], addrs[1], addrs[1], sdk.NewCoin(denom, math.NewInt(100)), 1, nil)
msg = types.NewMsgFinalizeTokenDeposit(addrsStr[0], addrsStr[1], addrsStr[1], sdk.NewCoin(denom, math.NewInt(100)), 1, nil)
_, err = ms.FinalizeTokenDeposit(ctx, msg)
require.NoError(t, err)

Expand All @@ -250,7 +267,7 @@ func Test_MsgServer_Deposit_HookSuccess(t *testing.T) {
input.BridgeHook.err = nil

// valid deposit
msg := types.NewMsgFinalizeTokenDeposit(addrs[0], addrs[1], addrs[1], sdk.NewCoin(denom, math.NewInt(100)), 1, hookMsgBytes)
msg := types.NewMsgFinalizeTokenDeposit(addrsStr[0], addrsStr[1], addrsStr[1], sdk.NewCoin(denom, math.NewInt(100)), 1, hookMsgBytes)
_, err = ms.FinalizeTokenDeposit(ctx, msg)
require.NoError(t, err)
require.Equal(t, hookMsgBytes, input.BridgeHook.msgBytes)
Expand All @@ -276,7 +293,7 @@ func Test_MsgServer_Deposit_HookFail(t *testing.T) {
input.BridgeHook.err = errors.New("should be failed")

// valid deposit
msg := types.NewMsgFinalizeTokenDeposit(addrs[0], addrs[1], addrs[1], sdk.NewCoin(denom, math.NewInt(100)), 1, []byte("invalid_message"))
msg := types.NewMsgFinalizeTokenDeposit(addrsStr[0], addrsStr[1], addrsStr[1], sdk.NewCoin(denom, math.NewInt(100)), 1, []byte("invalid_message"))
_, err := ms.FinalizeTokenDeposit(ctx, msg)
require.NoError(t, err)
require.Empty(t, input.BridgeHook.msgBytes)
Expand Down
40 changes: 20 additions & 20 deletions x/opchild/types/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,11 @@ const MAX_TOKEN_SYMBOL_LENGTH = 128

// NewMsgExecuteMessages creates a new MsgExecuteMessages instance.
func NewMsgExecuteMessages(
sender sdk.AccAddress, //nolint:interfacer
sender string,
messages []sdk.Msg,
) (*MsgExecuteMessages, error) {
msg := &MsgExecuteMessages{
Sender: sender.String(),
Sender: sender,
}

anys, err := sdktx.SetMsgs(messages)
Expand Down Expand Up @@ -90,8 +90,8 @@ func (m MsgExecuteMessages) UnpackInterfaces(unpacker codectypes.AnyUnpacker) er
// NewMsgAddValidator creates a new MsgAddValidator instance.
// Delegator address and validator address are the same.
func NewMsgAddValidator(
moniker string, authority sdk.AccAddress,
valAddr sdk.ValAddress, pubKey cryptotypes.PubKey, //nolint:interfacer
moniker string, authority string,
valAddr string, pubKey cryptotypes.PubKey,
) (*MsgAddValidator, error) {
var pkAny *codectypes.Any
if pubKey != nil {
Expand All @@ -102,8 +102,8 @@ func NewMsgAddValidator(
}
return &MsgAddValidator{
Moniker: moniker,
Authority: authority.String(),
ValidatorAddress: valAddr.String(),
Authority: authority,
ValidatorAddress: valAddr,
Pubkey: pkAny,
}, nil
}
Expand Down Expand Up @@ -137,12 +137,12 @@ func (msg MsgAddValidator) UnpackInterfaces(unpacker codectypes.AnyUnpacker) err

// NewMsgRemoveValidator creates a new MsgRemoveValidator instance.
func NewMsgRemoveValidator(
authority sdk.AccAddress,
valAddr sdk.ValAddress, //nolint:interfacer
authority string,
valAddr string,
) (*MsgRemoveValidator, error) {
return &MsgRemoveValidator{
Authority: authority.String(),
ValidatorAddress: valAddr.String(),
Authority: authority,
ValidatorAddress: valAddr,
}, nil
}

Expand All @@ -163,13 +163,13 @@ func (msg MsgRemoveValidator) Validate(ac, vc address.Codec) error {

// NewMsgInitiateTokenWithdrawal creates a new MsgInitiateTokenWithdrawal instance.
func NewMsgInitiateTokenWithdrawal(
sender sdk.AccAddress,
to sdk.AccAddress,
sender string,
to string,
amount sdk.Coin,
) *MsgInitiateTokenWithdrawal {
return &MsgInitiateTokenWithdrawal{
Sender: sender.String(),
To: to.String(),
Sender: sender,
To: to,
Amount: amount,
}
}
Expand All @@ -195,15 +195,15 @@ func (msg MsgInitiateTokenWithdrawal) Validate(ac address.Codec) error {

// NewMsgFinalizeTokenDeposit creates a new MsgFinalizeTokenDeposit instance.
func NewMsgFinalizeTokenDeposit(
sender, from, to sdk.AccAddress,
sender, from, to string,
amount sdk.Coin,
sequence uint64,
data []byte,
) *MsgFinalizeTokenDeposit {
return &MsgFinalizeTokenDeposit{
Sender: sender.String(),
From: from.String(),
To: to.String(),
Sender: sender,
From: from,
To: to,
Amount: amount,
Sequence: sequence,
Data: data,
Expand Down Expand Up @@ -238,9 +238,9 @@ func (msg MsgFinalizeTokenDeposit) Validate(ac address.Codec) error {
/* MsgUpdateParams */

// NewMsgUpdateParams returns a new MsgUpdateParams instance
func NewMsgUpdateParams(authority sdk.AccAddress, params *Params) *MsgUpdateParams {
func NewMsgUpdateParams(authority string, params *Params) *MsgUpdateParams {
return &MsgUpdateParams{
Authority: authority.String(),
Authority: authority,
Params: params,
}
}
Expand Down
Loading

0 comments on commit e6fe984

Please sign in to comment.