Skip to content

Commit

Permalink
Fix/only withdraw tokens from l1 (#76)
Browse files Browse the repository at this point in the history
* check if withdrawing token is from l1

* miss error check

* store denom pair for withdraw validation

* rename error

---------

Co-authored-by: beer-1 <[email protected]>
  • Loading branch information
sh-cha and beer-1 authored May 23, 2024
1 parent e5ffbd9 commit 0e5d5a1
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 7 deletions.
2 changes: 2 additions & 0 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
16 changes: 16 additions & 0 deletions x/opchild/keeper/msg_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -406,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
} else if !ok {
if err := ms.DenomPairs.Set(ctx, coin.Denom, req.BaseDenom); err != nil {
return nil, err
}
}

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

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

senderAddr, err := ms.authKeeper.AddressCodec().StringToBytes(req.Sender)
if err != nil {
return nil, err
Expand Down
42 changes: 37 additions & 5 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 Expand Up @@ -272,19 +274,49 @@ func Test_MsgServer_Withdraw(t *testing.T) {
ctx, input := createDefaultTestInput(t)
ms := keeper.NewMsgServerImpl(input.OPChildKeeper)

bz := sha3.Sum256([]byte("test_token"))
denom := "l2/" + hex.EncodeToString(bz[:])
info := types.BridgeInfo{
BridgeId: 1,
BridgeAddr: addrsStr[1],
L1ChainId: "test-chain-id",
L1ClientId: "test-client-id",
BridgeConfig: ophosttypes.BridgeConfig{
Challenger: addrsStr[2],
Proposer: addrsStr[3],
BatchInfo: ophosttypes.BatchInfo{
Submitter: addrsStr[4],
Chain: "l1",
},
SubmissionInterval: time.Minute,
FinalizationPeriod: time.Hour,
SubmissionStartTime: time.Now().UTC(),
Metadata: []byte("metadata"),
},
}

_, err := ms.SetBridgeInfo(ctx, types.NewMsgSetBridgeInfo(addrsStr[0], info))
require.NoError(t, err)

baseDenom := "test_token"
denom := ophosttypes.L2Denom(1, baseDenom)

_, err = ms.FinalizeTokenDeposit(ctx, types.NewMsgFinalizeTokenDeposit(addrsStr[0], addrsStr[1], addrsStr[1], sdk.NewCoin(denom, math.NewInt(100)), 1, "test_token", nil))
require.NoError(t, err)

coins := sdk.NewCoins(sdk.NewCoin("foo", math.NewInt(1_000_000_000)), sdk.NewCoin(denom, math.NewInt(1_000_000_000)))
// fund asset
account := input.Faucet.NewFundedAccount(ctx, sdk.NewCoin(denom, math.NewInt(1_000_000_000)))
account := input.Faucet.NewFundedAccount(ctx, coins...)
accountAddr, err := input.AccountKeeper.AddressCodec().BytesToString(account)
require.NoError(t, err)

// not token from l1
msg := types.NewMsgInitiateTokenWithdrawal(accountAddr, addrsStr[1], sdk.NewCoin("foo", math.NewInt(100)))
_, err = ms.InitiateTokenWithdrawal(ctx, msg)
require.Error(t, err)

// valid
msg := types.NewMsgInitiateTokenWithdrawal(accountAddr, addrsStr[1], sdk.NewCoin(denom, math.NewInt(100)))
msg = types.NewMsgInitiateTokenWithdrawal(accountAddr, addrsStr[1], sdk.NewCoin(denom, math.NewInt(100)))
_, err = ms.InitiateTokenWithdrawal(ctx, msg)
require.NoError(t, err)

}

/////////////////////////////////////////
Expand Down
1 change: 1 addition & 0 deletions x/opchild/types/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,5 @@ var (
ErrInvalidPrices = errorsmod.Register(ModuleName, 19, "invalid oracle prices")
ErrMaxValidatorsExceeded = errorsmod.Register(ModuleName, 20, "max validators exceeded")
ErrMaxValidatorsLowerThanCurrent = errorsmod.Register(ModuleName, 21, "max validators cannot be lower than current number of validators")
ErrNonL1Token = errorsmod.Register(ModuleName, 22, "token is not from L1")
)
1 change: 1 addition & 0 deletions x/opchild/types/expected_keepers.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ type BankKeeper interface {

HasDenomMetaData(ctx context.Context, denom string) bool
SetDenomMetaData(ctx context.Context, denomMetaData banktypes.Metadata)
GetDenomMetaData(ctx context.Context, denom string) (banktypes.Metadata, bool)
}

type OracleKeeper interface {
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 0e5d5a1

Please sign in to comment.