From 49a1859ba9bd953d87147e72c88e336e6a5805b1 Mon Sep 17 00:00:00 2001 From: Sammy Liu Date: Wed, 2 Aug 2023 04:26:11 +0800 Subject: [PATCH] fix(axelarnet)!: add/trim 0x prefix when checking contract address min deposit (#1974) * fix(axelarnet)!: add/trim 0x prefix when checking contract address min deposit * add a TODO * fix test --- x/axelarnet/keeper/hooks.go | 2 +- x/axelarnet/keeper/hooks_test.go | 4 ++++ x/axelarnet/types/types.go | 18 +++++++++++++++++- 3 files changed, 22 insertions(+), 2 deletions(-) diff --git a/x/axelarnet/keeper/hooks.go b/x/axelarnet/keeper/hooks.go index c275b8d45..6d3d489db 100644 --- a/x/axelarnet/keeper/hooks.go +++ b/x/axelarnet/keeper/hooks.go @@ -30,7 +30,7 @@ func (h Hooks) AfterProposalDeposit(ctx sdk.Context, proposalID uint64, _ sdk.Ac switch c := proposal.GetContent().(type) { case *types.CallContractsProposal: - minDepositsMap := h.k.GetParams(ctx).CallContractsProposalMinDeposits.ToMap() + minDepositsMap := h.k.GetParams(ctx).CallContractsProposalMinDeposits.ToMap(ctx, h.nexus) for _, contractCall := range c.ContractCalls { minDeposit := minDepositsMap.Get(contractCall.Chain, contractCall.ContractAddress) diff --git a/x/axelarnet/keeper/hooks_test.go b/x/axelarnet/keeper/hooks_test.go index f57dff5da..be00d1bdc 100644 --- a/x/axelarnet/keeper/hooks_test.go +++ b/x/axelarnet/keeper/hooks_test.go @@ -49,6 +49,10 @@ func TestAfterProposalDeposit(t *testing.T) { keeper := keeper.NewKeeper(encCfg.Codec, sdk.NewKVStoreKey("nexus"), subspace, &mock.ChannelKeeperMock{}, &mock.FeegrantKeeperMock{}) keeper.SetParams(ctx, types.DefaultParams()) + nexusK.GetChainFunc = func(ctx sdk.Context, chain nexus.ChainName) (nexus.Chain, bool) { + return evm.Ethereum, true + } + Given("a proposal is created", func() { govK.GetProposalFunc = func(ctx sdk.Context, proposalID uint64) (govtypes.Proposal, bool) { return proposal, proposalID == proposal.ProposalId diff --git a/x/axelarnet/types/types.go b/x/axelarnet/types/types.go index b0b442240..eed4bc886 100644 --- a/x/axelarnet/types/types.go +++ b/x/axelarnet/types/types.go @@ -16,6 +16,7 @@ import ( "github.com/axelarnetwork/axelar-core/utils" "github.com/axelarnetwork/axelar-core/x/axelarnet/exported" + evmtypes "github.com/axelarnetwork/axelar-core/x/evm/types" nexus "github.com/axelarnetwork/axelar-core/x/nexus/exported" ) @@ -30,6 +31,8 @@ const ( DefaultRateLimitWindow = 6 * time.Hour ) +const ZERO_X_PREFIX = "0x" + // NewLinkedAddress creates a new address to make a deposit which can be transferred to another blockchain func NewLinkedAddress(ctx sdk.Context, chain nexus.ChainName, symbol, recipientAddr string) sdk.AccAddress { nonce := utils.GetNonce(ctx.HeaderHash(), ctx.BlockGasMeter()) @@ -313,7 +316,7 @@ func (minDeposits CallContractProposalMinDeposits) ValidateBasic() error { } // ToMap returns a map of chain name to contract address to min deposit -func (minDeposits CallContractProposalMinDeposits) ToMap() callContractProposalMinDepositsMap { +func (minDeposits CallContractProposalMinDeposits) ToMap(ctx sdk.Context, nexus Nexus) callContractProposalMinDepositsMap { minDepositsMap := make(callContractProposalMinDepositsMap) for _, minDeposit := range minDeposits { @@ -325,6 +328,19 @@ func (minDeposits CallContractProposalMinDeposits) ToMap() callContractProposalM } minDepositsMap[chain][contractAddress] = minDeposit.MinDeposits + + // TODO: eventually, this is confusing and bad cuz cosmos addresses will also + // show up here and be prefixed with 0x. Like the address validator, we should + // also implement chain-specific address deserializer so that we just use the + // actual bytes as map keys for this check instead of a string representation. + if chain, ok := nexus.GetChain(ctx, minDeposit.Chain); !ok || !chain.IsFrom(evmtypes.ModuleName) { + continue + } + if strings.HasPrefix(contractAddress, ZERO_X_PREFIX) { + minDepositsMap[chain][strings.TrimPrefix(contractAddress, ZERO_X_PREFIX)] = minDeposit.MinDeposits + } else { + minDepositsMap[chain][fmt.Sprintf("%s%s", ZERO_X_PREFIX, contractAddress)] = minDeposit.MinDeposits + } } return minDepositsMap