Skip to content

Commit

Permalink
fix accaddress parse
Browse files Browse the repository at this point in the history
  • Loading branch information
Pantani committed Oct 16, 2024
1 parent 0e352f7 commit 400ff0c
Show file tree
Hide file tree
Showing 19 changed files with 82 additions and 58 deletions.
4 changes: 2 additions & 2 deletions x/launch/keeper/msg_server_create_chain_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func initCreationFeeAndFundCoordAccounts(
// add `coins` to balance of each coordinator address
// using `project` module account for minting as `launch` does not have one
for _, addr := range addrs {
accAddr, err := sdk.AccAddressFromBech32(addr)
accAddr, err := keeper.AddressCodec().StringToBytes(addr)
require.NoError(t, err)
err = bk.MintCoins(ctx, projecttypes.ModuleName, coins)
require.NoError(t, err)
Expand Down Expand Up @@ -146,7 +146,7 @@ func TestMsgCreateChain(t *testing.T) {
} {
t.Run(tc.name, func(t *testing.T) {
// get account initial balance
accAddr, err := sdk.AccAddressFromBech32(tc.msg.Coordinator)
accAddr, err := tk.LaunchKeeper.AddressCodec().StringToBytes(tc.msg.Coordinator)
require.NoError(t, err)
preBalance := tk.BankKeeper.SpendableCoins(ctx, accAddr)

Expand Down
3 changes: 1 addition & 2 deletions x/launch/keeper/msg_server_revert_launch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"testing"
"time"

sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/stretchr/testify/require"

testkeeper "github.com/ignite/network/testutil/keeper"
Expand Down Expand Up @@ -229,7 +228,7 @@ func TestMsgRevertLaunch(t *testing.T) {
if !tt.inputState.noCoordinator {
err = tk.ProfileKeeper.Coordinator.Set(ctx, tt.inputState.coordinator.CoordinatorID, tt.inputState.coordinator)
require.NoError(t, err)
addr, err := sdk.AccAddressFromBech32(tt.inputState.coordinator.Address)
addr, err := tk.ProfileKeeper.AddressCodec().StringToBytes(tt.inputState.coordinator.Address)
require.NoError(t, err)
err = tk.ProfileKeeper.CoordinatorByAddress.Set(ctx, addr, profiletypes.CoordinatorByAddress{
Address: tt.inputState.coordinator.Address,
Expand Down
6 changes: 3 additions & 3 deletions x/launch/keeper/msg_server_send_request_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@ func TestMsgRequestAddAccount(t *testing.T) {
if !tt.inputState.noCoordinator {
err := tk.ProfileKeeper.Coordinator.Set(ctx, tt.inputState.coordinator.CoordinatorID, tt.inputState.coordinator)
require.NoError(t, err)
addr, err := sdk.AccAddressFromBech32(tt.inputState.coordinator.Address)
addr, err := tk.ProfileKeeper.AddressCodec().StringToBytes(tt.inputState.coordinator.Address)
require.NoError(t, err)
err = tk.ProfileKeeper.CoordinatorByAddress.Set(ctx, addr, profiletypes.CoordinatorByAddress{
CoordinatorID: tt.inputState.coordinator.CoordinatorID,
Expand All @@ -284,9 +284,9 @@ func TestMsgRequestAddAccount(t *testing.T) {
require.NoError(t, err)
}
if !tt.inputState.noAccount {
addr, err := sdk.AccAddressFromBech32(tt.inputState.account.Address)
addr, err := tk.LaunchKeeper.AddressCodec().StringToBytes(tt.inputState.account.Address)
require.NoError(t, err)
err = tk.LaunchKeeper.GenesisAccount.Set(ctx, collections.Join(tt.inputState.account.LaunchID, addr), tt.inputState.account)
err = tk.LaunchKeeper.GenesisAccount.Set(ctx, collections.Join(tt.inputState.account.LaunchID, sdk.AccAddress(addr)), tt.inputState.account)
require.NoError(t, err)
}
if !tt.inputState.fee.Empty() {
Expand Down
4 changes: 2 additions & 2 deletions x/launch/keeper/msg_server_settle_request_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -231,8 +231,8 @@ func TestMsgSettleRequest(t *testing.T) {
require.NoError(t, err, "request not found")
require.Equal(t, tt.wantStatus, request.Status)

checkAddr, err := sdk.AccAddressFromBech32(tt.checkAddr)
_, err = tk.LaunchKeeper.GenesisAccount.Get(ctx, collections.Join(tt.msg.LaunchID, checkAddr))
checkAddr, err := tk.LaunchKeeper.AddressCodec().StringToBytes(tt.checkAddr)
_, err = tk.LaunchKeeper.GenesisAccount.Get(ctx, collections.Join(tt.msg.LaunchID, sdk.AccAddress(checkAddr)))
if tt.msg.Approve {
require.NoError(t, err, "request apply performed")
} else {
Expand Down
4 changes: 1 addition & 3 deletions x/launch/keeper/msg_server_trigger_launch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ import (
"testing"
"time"

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

"github.com/stretchr/testify/require"

testkeeper "github.com/ignite/network/testutil/keeper"
Expand Down Expand Up @@ -194,7 +192,7 @@ func TestMsgTriggerLaunch(t *testing.T) {
if !tt.inputState.noCoordinator {
err := tk.ProfileKeeper.Coordinator.Set(ctx, tt.inputState.coordinator.CoordinatorID, tt.inputState.coordinator)
require.NoError(t, err)
addr, err := sdk.AccAddressFromBech32(tt.inputState.coordinator.Address)
addr, err := tk.ProfileKeeper.AddressCodec().StringToBytes(tt.inputState.coordinator.Address)
require.NoError(t, err)
err = tk.ProfileKeeper.CoordinatorByAddress.Set(ctx, addr, profiletypes.CoordinatorByAddress{
Address: tt.inputState.coordinator.Address,
Expand Down
22 changes: 11 additions & 11 deletions x/launch/keeper/request_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -213,35 +213,35 @@ func TestApplyRequest(t *testing.T) {
switch requestContent := tt.request.Content.Content.(type) {
case *types.RequestContent_GenesisAccount:
ga := requestContent.GenesisAccount
address, err := sdk.AccAddressFromBech32(ga.Address)
address, err := tk.LaunchKeeper.AddressCodec().StringToBytes(ga.Address)
require.NoError(t, err)
_, err = tk.LaunchKeeper.GenesisAccount.Get(ctx, collections.Join(launchID, address))
_, err = tk.LaunchKeeper.GenesisAccount.Get(ctx, collections.Join(launchID, sdk.AccAddress(address)))
require.NoError(t, err, "genesis account not found")
case *types.RequestContent_VestingAccount:
va := requestContent.VestingAccount
address, err := sdk.AccAddressFromBech32(va.Address)
address, err := tk.LaunchKeeper.AddressCodec().StringToBytes(va.Address)
require.NoError(t, err)
_, err = tk.LaunchKeeper.VestingAccount.Get(ctx, collections.Join(launchID, address))
_, err = tk.LaunchKeeper.VestingAccount.Get(ctx, collections.Join(launchID, sdk.AccAddress(address)))
require.NoError(t, err, "vesting account not found")
case *types.RequestContent_AccountRemoval:
ar := requestContent.AccountRemoval
address, err := sdk.AccAddressFromBech32(ar.Address)
address, err := tk.LaunchKeeper.AddressCodec().StringToBytes(ar.Address)
require.NoError(t, err)
_, err = tk.LaunchKeeper.GenesisAccount.Get(ctx, collections.Join(launchID, address))
_, err = tk.LaunchKeeper.GenesisAccount.Get(ctx, collections.Join(launchID, sdk.AccAddress(address)))
require.Error(t, err, "genesis account not removed")
_, err = tk.LaunchKeeper.VestingAccount.Get(ctx, collections.Join(launchID, address))
_, err = tk.LaunchKeeper.VestingAccount.Get(ctx, collections.Join(launchID, sdk.AccAddress(address)))
require.Error(t, err, "vesting account not removed")
case *types.RequestContent_GenesisValidator:
ga := requestContent.GenesisValidator
address, err := sdk.AccAddressFromBech32(ga.Address)
address, err := tk.LaunchKeeper.AddressCodec().StringToBytes(ga.Address)
require.NoError(t, err)
_, err = tk.LaunchKeeper.GenesisValidator.Get(ctx, collections.Join(launchID, address))
_, err = tk.LaunchKeeper.GenesisValidator.Get(ctx, collections.Join(launchID, sdk.AccAddress(address)))
require.NoError(t, err, "genesis validator not found")
case *types.RequestContent_ValidatorRemoval:
vr := requestContent.ValidatorRemoval
address, err := sdk.AccAddressFromBech32(vr.ValAddress)
address, err := tk.LaunchKeeper.AddressCodec().StringToBytes(vr.ValAddress)
require.NoError(t, err)
_, err = tk.LaunchKeeper.GenesisValidator.Get(ctx, collections.Join(launchID, address))
_, err = tk.LaunchKeeper.GenesisValidator.Get(ctx, collections.Join(launchID, sdk.AccAddress(address)))
require.Error(t, err, "genesis validator not removed")
}
})
Expand Down
8 changes: 4 additions & 4 deletions x/launch/simulation/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@ func IsLaunchTriggeredChain(ctx sdk.Context, k *keeper.Keeper, chainID uint64) b
}

// FindAccount find account by string hex address
func FindAccount(accs []simtypes.Account, address string) (simtypes.Account, error) {
coordAddr, err := sdk.AccAddressFromBech32(address)
func FindAccount(k *keeper.Keeper, accs []simtypes.Account, address string) (simtypes.Account, error) {
coordinatorAddress, err := k.AddressCodec().StringToBytes(address)
if err != nil {
return simtypes.Account{}, err
}
simAccount, found := simtypes.FindAccount(accs, coordAddr)
simAccount, found := simtypes.FindAccount(accs, sdk.AccAddress(coordinatorAddress))
if !found {
return simAccount, fmt.Errorf("address %s not found in the sim accounts", address)
}
Expand Down Expand Up @@ -125,7 +125,7 @@ func FindChainCoordinatorAccount(
return simtypes.Account{}, fmt.Errorf("coordinator %d inactive", chain.CoordinatorID)
}

return FindAccount(accs, coord.Address)
return FindAccount(k, accs, coord.Address)
}

// FindRandomChain find a random chain from store
Expand Down
16 changes: 12 additions & 4 deletions x/monitoringc/simulation/helpers.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,23 @@
package simulation

import (
"fmt"

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

"github.com/ignite/network/x/monitoringc/keeper"
)

// FindAccount find a specific address from an account list
func FindAccount(accs []simtypes.Account, address string) (simtypes.Account, bool) {
creator, err := sdk.AccAddressFromBech32(address)
func FindAccount(k keeper.Keeper, accs []simtypes.Account, address string) (simtypes.Account, error) {
creator, err := k.AddressCodec().StringToBytes(address)
if err != nil {
panic(err)
return simtypes.Account{}, err
}
simAccount, found := simtypes.FindAccount(accs, sdk.AccAddress(creator))
if !found {
return simAccount, fmt.Errorf("address %s not found in the sim accounts", address)
}
return simtypes.FindAccount(accs, creator)
return simAccount, nil
}
16 changes: 12 additions & 4 deletions x/monitoringp/simulation/helpers.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,23 @@
package simulation

import (
"fmt"

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

"github.com/ignite/network/x/monitoringp/keeper"
)

// FindAccount find a specific address from an account list
func FindAccount(accs []simtypes.Account, address string) (simtypes.Account, bool) {
creator, err := sdk.AccAddressFromBech32(address)
func FindAccount(k keeper.Keeper, accs []simtypes.Account, address string) (simtypes.Account, error) {
creator, err := k.AddressCodec().StringToBytes(address)
if err != nil {
panic(err)
return simtypes.Account{}, err
}
simAccount, found := simtypes.FindAccount(accs, sdk.AccAddress(creator))
if !found {
return simAccount, fmt.Errorf("address %s not found in the sim accounts", address)
}
return simtypes.FindAccount(accs, creator)
return simAccount, nil
}
3 changes: 2 additions & 1 deletion x/participation/keeper/invariants_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,9 @@ func TestMismatchUsedAllocationsInvariant(t *testing.T) {
NumAllocations: sdkmath.NewInt(2),
}
)
accAddr, err := sdk.AccAddressFromBech32(addr)
byteAddr, err := tk.ParticipationKeeper.AddressCodec().StringToBytes(addr)
require.NoError(t, err)
accAddr := sdk.AccAddress(byteAddr)

t.Run("should allow valid case", func(t *testing.T) {
err := tk.ParticipationKeeper.UsedAllocations.Set(ctx, accAddr.String(), validUsedAllocs)
Expand Down
4 changes: 2 additions & 2 deletions x/participation/keeper/msg_server_participate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -240,9 +240,9 @@ func Test_msgServer_Participate(t *testing.T) {
require.EqualValues(t, tt.desiredUsedAlloc, usedAllocations.NumAllocations)

// check valid auction used allocations entry for bidder exists
participantAddress, err := sdk.AccAddressFromBech32(tt.msg.Participant)
participantAddress, err := tk.ParticipationKeeper.AddressCodec().StringToBytes(tt.msg.Participant)
require.NoError(t, err)
auctionUsedAllocations, err := tk.ParticipationKeeper.AuctionUsedAllocations.Get(tmpCtx, collections.Join(participantAddress, tt.msg.AuctionID))
auctionUsedAllocations, err := tk.ParticipationKeeper.AuctionUsedAllocations.Get(tmpCtx, collections.Join(sdk.AccAddress(participantAddress), tt.msg.AuctionID))
require.NoError(t, err)
require.Equal(t, tier.RequiredAllocations, auctionUsedAllocations.NumAllocations)
require.False(t, auctionUsedAllocations.Withdrawn)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -161,9 +161,9 @@ func Test_msgServer_WithdrawAllocations(t *testing.T) {
require.NoError(t, err)

// check auctionUsedAllocations is set to `withdrawn`
participantAddress, err := sdk.AccAddressFromBech32(tt.msg.Participant)
participantAddress, err := tk.ParticipationKeeper.AddressCodec().StringToBytes(tt.msg.Participant)
require.NoError(t, err)
postAuctionUsedAllocations, err := tk.ParticipationKeeper.AuctionUsedAllocations.Get(tmpCtx, collections.Join(participantAddress, tt.msg.AuctionID))
postAuctionUsedAllocations, err := tk.ParticipationKeeper.AuctionUsedAllocations.Get(tmpCtx, collections.Join(sdk.AccAddress(participantAddress), tt.msg.AuctionID))
require.NoError(t, err)
require.True(t, postAuctionUsedAllocations.Withdrawn)
require.Equal(t, preAuctionUsedAllocations.NumAllocations, postAuctionUsedAllocations.NumAllocations)
Expand Down
16 changes: 12 additions & 4 deletions x/profile/simulation/helpers.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,23 @@
package simulation

import (
"fmt"

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

"github.com/ignite/network/x/profile/keeper"
)

// FindAccount find a specific address from an account list
func FindAccount(accs []simtypes.Account, address string) (simtypes.Account, bool) {
creator, err := sdk.AccAddressFromBech32(address)
func FindAccount(k *keeper.Keeper, accs []simtypes.Account, address string) (simtypes.Account, error) {
creator, err := k.AddressCodec().StringToBytes(address)
if err != nil {
panic(err)
return simtypes.Account{}, err
}
simAccount, found := simtypes.FindAccount(accs, sdk.AccAddress(creator))
if !found {
return simAccount, fmt.Errorf("address %s not found in the sim accounts", address)
}
return simtypes.FindAccount(accs, creator)
return simAccount, nil
}
2 changes: 1 addition & 1 deletion x/project/keeper/msg_server_burn_vouchers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ func TestMsgBurnVouchers(t *testing.T) {
previousProject, err = tk.ProjectKeeper.GetProject(ctx, tc.msg.ProjectID)
require.NoError(t, err)

creatorAddr, err = sdk.AccAddressFromBech32(tc.msg.Sender)
creatorAddr, err = tk.ProjectKeeper.AddressCodec().StringToBytes(tc.msg.Sender)
require.NoError(t, err)
previousBalance = tk.BankKeeper.GetAllBalances(ctx, creatorAddr)
}
Expand Down
4 changes: 2 additions & 2 deletions x/project/keeper/msg_server_create_project_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func initCreationFeeAndFundCoordAccounts(

t.Run("should add coins to balance of each coordinator address", func(t *testing.T) {
for _, addr := range addrs {
accAddr, err := sdk.AccAddressFromBech32(addr)
accAddr, err := keeper.AddressCodec().StringToBytes(addr)
require.NoError(t, err)
err = bk.MintCoins(ctx, types.ModuleName, coins)
require.NoError(t, err)
Expand Down Expand Up @@ -141,7 +141,7 @@ func TestMsgCreateProject(t *testing.T) {
} {
t.Run(tc.name, func(t *testing.T) {
// get account initial balance
accAddr, err := sdk.AccAddressFromBech32(tc.msg.Coordinator)
accAddr, err := tk.ProjectKeeper.AddressCodec().StringToBytes(tc.msg.Coordinator)
require.NoError(t, err)
preBalance := tk.BankKeeper.SpendableCoins(ctx, accAddr)

Expand Down
2 changes: 1 addition & 1 deletion x/project/keeper/msg_server_mint_vouchers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ func TestMsgMintVouchers(t *testing.T) {
var previousProject types.Project
var previousBalance sdk.Coins

coordAddr, err := sdk.AccAddressFromBech32(tc.msg.Coordinator)
coordAddr, err := tk.ProjectKeeper.AddressCodec().StringToBytes(tc.msg.Coordinator)
require.NoError(t, err)

// Get values before message execution
Expand Down
10 changes: 6 additions & 4 deletions x/project/keeper/msg_server_redeem_vouchers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/stretchr/testify/require"

ignterrors "github.com/ignite/network/pkg/errors"
networktypes "github.com/ignite/network/pkg/types"
testkeeper "github.com/ignite/network/testutil/keeper"
"github.com/ignite/network/testutil/sample"
Expand Down Expand Up @@ -53,6 +52,9 @@ func TestMsgRedeemVouchers(t *testing.T) {
vouchersErr, err := types.SharesToVouchers(shares, invalidProjectID)
require.NoError(t, err)

vouchersMainnet, err := types.SharesToVouchers(shares, projectMainnetLaunched.ProjectID)
require.NoError(t, err)

t.Run("should allow setting test balances", func(t *testing.T) {
err = tk.BankKeeper.MintCoins(ctx, types.ModuleName, vouchers)
require.NoError(t, err)
Expand Down Expand Up @@ -130,7 +132,7 @@ func TestMsgRedeemVouchers(t *testing.T) {
ProjectID: project.ProjectID,
Vouchers: vouchersErr,
},
err: ignterrors.ErrCritical,
err: types.ErrNoMatchVouchers,
},
{
name: "should fail with invalid sender address",
Expand Down Expand Up @@ -179,7 +181,7 @@ func TestMsgRedeemVouchers(t *testing.T) {
Sender: addr.String(),
Account: addr.String(),
ProjectID: projectMainnetLaunched.ProjectID,
Vouchers: vouchers,
Vouchers: vouchersMainnet,
},
err: types.ErrMainnetLaunchTriggered,
},
Expand All @@ -192,7 +194,7 @@ func TestMsgRedeemVouchers(t *testing.T) {

// Get values before message execution
if tc.err == nil {
accountAddr, err = sdk.AccAddressFromBech32(tc.msg.Account)
accountAddr, err = tk.ProjectKeeper.AddressCodec().StringToBytes(tc.msg.Account)
require.NoError(t, err)

previousAccount, err = tk.ProjectKeeper.GetMainnetAccount(ctx, tc.msg.ProjectID, accountAddr)
Expand Down
2 changes: 1 addition & 1 deletion x/project/keeper/msg_server_unredeem_vouchers.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func (k msgServer) UnredeemVouchers(ctx context.Context, msg *types.MsgUnredeemV
// Check if the account already exists
account, err := k.MainnetAccount.Get(ctx, collections.Join(msg.ProjectID, sdk.AccAddress(sender)))
if err != nil {
return nil, ignterrors.Criticalf("can't get mainnet account %s", err.Error())
return nil, sdkerrors.Wrapf(types.ErrAccountNotFound, "%s: %s", err.Error(), msg.Sender)
}

// Update the shares of the account
Expand Down
10 changes: 5 additions & 5 deletions x/project/keeper/msg_server_unredeem_vouchers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,14 @@ func TestMsgUnredeemVouchers(t *testing.T) {
require.NoError(t, err)

// Create accounts
accountAddress, err := sdk.AccAddressFromBech32(account.Address)
accountAddress, err := tk.ProjectKeeper.AddressCodec().StringToBytes(account.Address)
require.NoError(t, err)
err = tk.ProjectKeeper.MainnetAccount.Set(ctx, collections.Join(projectID, accountAddress), account)
err = tk.ProjectKeeper.MainnetAccount.Set(ctx, collections.Join(projectID, sdk.AccAddress(accountAddress)), account)
require.NoError(t, err)

accountFewSharesAddress, err := sdk.AccAddressFromBech32(accountFewShares.Address)
accountFewSharesAddress, err := tk.ProjectKeeper.AddressCodec().StringToBytes(accountFewShares.Address)
require.NoError(t, err)
err = tk.ProjectKeeper.MainnetAccount.Set(ctx, collections.Join(projectID, accountFewSharesAddress), accountFewShares)
err = tk.ProjectKeeper.MainnetAccount.Set(ctx, collections.Join(projectID, sdk.AccAddress(accountFewSharesAddress)), accountFewShares)
require.NoError(t, err)

for _, tc := range []struct {
Expand Down Expand Up @@ -132,7 +132,7 @@ func TestMsgUnredeemVouchers(t *testing.T) {
var previousAccount types.MainnetAccount
var previousBalance sdk.Coins

accountAddr, err := sdk.AccAddressFromBech32(tc.msg.Sender)
accountAddr, err := tk.ProjectKeeper.AddressCodec().StringToBytes(tc.msg.Sender)
require.NoError(t, err)

// Get values before message execution
Expand Down

0 comments on commit 400ff0c

Please sign in to comment.