Skip to content

Commit

Permalink
add missing error handling (#196)
Browse files Browse the repository at this point in the history
add case check in MetadataAddressFromDenom
  • Loading branch information
beer-1 authored Jun 3, 2024
1 parent 9589c4e commit 530f377
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 1 deletion.
3 changes: 3 additions & 0 deletions x/distribution/keeper/delegation.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,9 @@ func (k Keeper) CalculateDelegationRewards(ctx context.Context, val stakingtypes

// fetch starting info for delegation
startingInfo, err := k.DelegatorStartingInfos.Get(ctx, collections.Join(valAddr, delAddr))
if err != nil {
return nil, err
}

sdkCtx := sdk.UnwrapSDKContext(ctx)
if startingInfo.Height == uint64(sdkCtx.BlockHeight()) {
Expand Down
8 changes: 7 additions & 1 deletion x/move/types/denom.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package types
import (
context "context"
"encoding/hex"
"errors"
"strings"

"golang.org/x/crypto/sha3"
Expand Down Expand Up @@ -53,7 +54,12 @@ func UserDerivedObjectAddress(source vmtypes.AccountAddress, deriveFrom vmtypes.
// Extract metadata address from a denom
func MetadataAddressFromDenom(denom string) (vmtypes.AccountAddress, error) {
if strings.HasPrefix(denom, DenomTraceDenomPrefixMove) {
addrBz, err := hex.DecodeString(strings.TrimPrefix(denom, DenomTraceDenomPrefixMove))
hexStr := strings.TrimPrefix(denom, DenomTraceDenomPrefixMove)
if strings.ToLower(hexStr) != hexStr {
return vmtypes.AccountAddress{}, errors.New("metadata address should be lowercase")
}

addrBz, err := hex.DecodeString(hexStr)
if err != nil {
return vmtypes.AccountAddress{}, err
}
Expand Down
20 changes: 20 additions & 0 deletions x/move/types/denom_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package types_test

import (
"testing"

"github.com/initia-labs/initia/x/move/types"
"github.com/stretchr/testify/require"
)

func Test_MetadataAddressFromDenom(t *testing.T) {
denom := "move/944f8dd8dc49f96c25fea9849f16436dcfa6d564eec802f3ef7f8b3ea85368ff"
addr, err := types.MetadataAddressFromDenom(denom)
require.NoError(t, err)
require.Equal(t, "0x944f8dd8dc49f96c25fea9849f16436dcfa6d564eec802f3ef7f8b3ea85368ff", addr.String())

// upper case
denom = "move/944f8dd8dc49f96c25fea9849f16436dcfa6d564eec802f3ef7f8b3ea85368fF"
_, err = types.MetadataAddressFromDenom(denom)
require.Error(t, err)
}

0 comments on commit 530f377

Please sign in to comment.