Skip to content

Commit

Permalink
store denom pair for withdraw validation
Browse files Browse the repository at this point in the history
  • Loading branch information
beer-1 committed May 23, 2024
1 parent a7a1fb1 commit 33097d9
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 28 deletions.
11 changes: 2 additions & 9 deletions x/opchild/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ type Keeper struct {
Validators collections.Map[[]byte, types.Validator]
ValidatorsByConsAddr collections.Map[[]byte, []byte]
HistoricalInfos collections.Map[int64, cosmostypes.HistoricalInfo]
DenomPairs collections.Map[string, string]

ExecutorChangePlans map[uint64]types.ExecutorChangePlan

Expand Down Expand Up @@ -107,6 +108,7 @@ func NewKeeper(
Validators: collections.NewMap(sb, types.ValidatorsPrefix, "validators", collections.BytesKey, codec.CollValue[types.Validator](cdc)),
ValidatorsByConsAddr: collections.NewMap(sb, types.ValidatorsByConsAddrPrefix, "validators_by_cons_addr", collections.BytesKey, collections.BytesValue),
HistoricalInfos: collections.NewMap(sb, types.HistoricalInfoPrefix, "historical_infos", collections.Int64Key, codec.CollValue[cosmostypes.HistoricalInfo](cdc)),
DenomPairs: collections.NewMap(sb, types.DenomPairPrefix, "denom_pairs", collections.StringKey, collections.StringValue),

ExecutorChangePlans: make(map[uint64]types.ExecutorChangePlan),
HostValidatorStore: hostValidatorStore,
Expand Down Expand Up @@ -157,15 +159,6 @@ func (k Keeper) setDenomMetadata(ctx context.Context, baseDenom, denom string) {
k.bankKeeper.SetDenomMetaData(ctx, metadata)
}

func (k Keeper) getBaseDenomFromMetadata(ctx context.Context, denom string) (string, bool) {
metadata, ok := k.bankKeeper.GetDenomMetaData(ctx, denom)
if !ok {
return "", false
}

return metadata.Display, true
}

// UpdateHostValidatorSet updates the host validator set.
func (k Keeper) UpdateHostValidatorSet(ctx context.Context, clientID string, height int64, validatorSet *cmtproto.ValidatorSet) error {
if clientID == "" {
Expand Down
29 changes: 12 additions & 17 deletions x/opchild/keeper/msg_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import (
govtypes "github.com/cosmos/cosmos-sdk/x/gov/types"

"github.com/initia-labs/OPinit/x/opchild/types"
ophosttypes "github.com/initia-labs/OPinit/x/ophost/types"
)

type MsgServer struct {
Expand Down Expand Up @@ -407,6 +406,15 @@ func (ms MsgServer) FinalizeTokenDeposit(ctx context.Context, req *types.MsgFina
ms.setDenomMetadata(ctx, req.BaseDenom, coin.Denom)
}

// register denom pair
if ok, err := ms.DenomPairs.Has(ctx, coin.Denom); err != nil {
return nil, err

Check warning on line 411 in x/opchild/keeper/msg_server.go

View check run for this annotation

Codecov / codecov/patch

x/opchild/keeper/msg_server.go#L411

Added line #L411 was not covered by tests
} else if !ok {
if err := ms.DenomPairs.Set(ctx, coin.Denom, req.BaseDenom); err != nil {
return nil, err
}

Check warning on line 415 in x/opchild/keeper/msg_server.go

View check run for this annotation

Codecov / codecov/patch

x/opchild/keeper/msg_server.go#L414-L415

Added lines #L414 - L415 were not covered by tests
}

event := sdk.NewEvent(
types.EventTypeFinalizeTokenDeposit,
sdk.NewAttribute(types.AttributeKeyL1Sequence, strconv.FormatUint(req.Sequence, 10)),
Expand Down Expand Up @@ -446,23 +454,10 @@ func (ms MsgServer) InitiateTokenWithdrawal(ctx context.Context, req *types.MsgI
sdkCtx := sdk.UnwrapSDKContext(ctx)
coin := req.Amount

bridgeId, err := ms.BridgeId(ctx)
if err != nil {
// check denom pair existence
if ok, err := ms.DenomPairs.Has(ctx, coin.Denom); err != nil {
return nil, err

Check warning on line 459 in x/opchild/keeper/msg_server.go

View check run for this annotation

Codecov / codecov/patch

x/opchild/keeper/msg_server.go#L459

Added line #L459 was not covered by tests
}

// register denom metadata
if ok := ms.bankKeeper.HasDenomMetaData(ctx, coin.Denom); !ok {
return nil, types.ErrNotTokenFromL1
}

baseDenom, ok := ms.getBaseDenomFromMetadata(ctx, coin.Denom)
if !ok {
return nil, types.ErrNotTokenFromL1
}

expectedL2Denom := ophosttypes.L2Denom(bridgeId, baseDenom)
if expectedL2Denom != coin.Denom {
} else if !ok {
return nil, types.ErrNotTokenFromL1
}

Expand Down
2 changes: 2 additions & 0 deletions x/opchild/keeper/msg_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ func Test_MsgServer_AddValidator(t *testing.T) {
require.Error(t, err)

params, err := ms.GetParams(ctx)
require.NoError(t, err)
params.MaxValidators = 1
ms.SetParams(ctx, params)

Expand All @@ -156,6 +157,7 @@ func Test_MsgServer_AddValidator(t *testing.T) {
require.Error(t, err)

params, err = ms.GetParams(ctx)
require.NoError(t, err)
params.MaxValidators = 2
ms.SetParams(ctx, params)

Expand Down
7 changes: 5 additions & 2 deletions x/opchild/types/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ var (
ValidatorsByConsAddrPrefix = []byte{0x41} // prefix for each key to a validator index, by pubkey
FinalizedL1SequencePrefix = []byte{0x51} // prefix for finalized deposit sequences
HistoricalInfoPrefix = []byte{0x61} // prefix for the historical info
HostHeightKey = []byte{0x71}
HostValidatorsPrefix = []byte{0x81}
DenomPairPrefix = []byte{0x71} // prefix for the denom pair

// HostValidatorStore keys
HostHeightKey = []byte{0x81}
HostValidatorsPrefix = []byte{0x82}
)

0 comments on commit 33097d9

Please sign in to comment.