Skip to content

Commit

Permalink
fix(axelarnet)!: add/trim 0x prefix when checking contract address mi…
Browse files Browse the repository at this point in the history
…n deposit (#1974)

* fix(axelarnet)!: add/trim 0x prefix when checking contract address min deposit

* add a TODO

* fix test
  • Loading branch information
fish-sammy authored Aug 1, 2023
1 parent a438f41 commit 49a1859
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 2 deletions.
2 changes: 1 addition & 1 deletion x/axelarnet/keeper/hooks.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 4 additions & 0 deletions x/axelarnet/keeper/hooks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
18 changes: 17 additions & 1 deletion x/axelarnet/types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand All @@ -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())
Expand Down Expand Up @@ -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 {
Expand All @@ -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
Expand Down

0 comments on commit 49a1859

Please sign in to comment.