-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add
bankplus
function to restrict to send coin to inactive sm…
…art contract. (#400) * feat: add `bankplus` function to restrict to send coin to inactive smart contract. * chore: add bypass codes of `bankplus` * chore: apply sending restriction function to wasm module * fix protobuf lint warning * fix unittest error * fix golang lint error * fix golang lint error * Add a unittest of bankplus.keeper * fix typo * fix lint warning * chore: change `blockedAddr` of `bankplus` module to `inactiveAddr` * fix wrong linking. * chore: change the name of `block.go` to `inactive.go` * chore: apply `bankplus` module to `simd`. * chore: apply feedbacks * fix: fix unittest failure.
- Loading branch information
Showing
16 changed files
with
950 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
syntax = "proto3"; | ||
package lbm.bankplus.v1; | ||
|
||
import "gogoproto/gogo.proto"; | ||
|
||
option go_package = "github.com/line/lbm-sdk/x/bankplus/types"; | ||
|
||
// InactiveAddr models the blocked address for the bankplus module | ||
message InactiveAddr { | ||
option (gogoproto.equal) = true; | ||
|
||
string address = 1; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package keeper | ||
|
||
import ( | ||
sdk "github.com/line/lbm-sdk/types" | ||
"github.com/line/lbm-sdk/x/bankplus/types" | ||
) | ||
|
||
// Keys for bankplus store but this prefix must not be overlap with bank key prefix. | ||
var inactiveAddrsKeyPrefix = []byte{0xa0} | ||
|
||
// inactiveAddrKey key of a specific inactiveAddr from store | ||
func inactiveAddrKey(addr sdk.AccAddress) []byte { | ||
return append(inactiveAddrsKeyPrefix, addr.Bytes()...) | ||
} | ||
|
||
//nolint:deadcode,unused | ||
// isStoredInactiveAddr checks if the address is stored or not as blocked address | ||
func (keeper BaseKeeper) isStoredInactiveAddr(ctx sdk.Context, address sdk.AccAddress) bool { | ||
store := ctx.KVStore(keeper.storeKey) | ||
bz := store.Get(inactiveAddrKey(address)) | ||
return bz != nil | ||
} | ||
|
||
// addToInactiveAddr adds a blocked address to the store. | ||
func (keeper BaseKeeper) addToInactiveAddr(ctx sdk.Context, address sdk.AccAddress) { | ||
store := ctx.KVStore(keeper.storeKey) | ||
blockedCAddr := types.InactiveAddr{Address: address.String()} | ||
bz := keeper.cdc.MustMarshalBinaryBare(&blockedCAddr) | ||
store.Set(inactiveAddrKey(address), bz) | ||
} | ||
|
||
// deleteFromInactiveAddr deletes blocked address from store | ||
func (keeper BaseKeeper) deleteFromInactiveAddr(ctx sdk.Context, address sdk.AccAddress) { | ||
store := ctx.KVStore(keeper.storeKey) | ||
store.Delete(inactiveAddrKey(address)) | ||
} | ||
|
||
// loadAllInactiveAddrs loads all blocked address and set to `inactiveAddr`. | ||
// This function is executed when the app is initiated and save all inactive address in caches | ||
// in order to prevent to query to store in every time to send | ||
func (keeper BaseKeeper) loadAllInactiveAddrs(ctx sdk.Context) { | ||
store := ctx.KVStore(keeper.storeKey) | ||
iterator := sdk.KVStorePrefixIterator(store, inactiveAddrsKeyPrefix) | ||
|
||
defer iterator.Close() | ||
for ; iterator.Valid(); iterator.Next() { | ||
var bAddr types.InactiveAddr | ||
keeper.cdc.MustUnmarshalBinaryBare(iterator.Value(), &bAddr) | ||
|
||
keeper.inactiveAddrs[bAddr.Address] = true | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
package keeper | ||
|
||
import ( | ||
"github.com/line/lbm-sdk/codec" | ||
codectypes "github.com/line/lbm-sdk/codec/types" | ||
"github.com/line/lbm-sdk/store" | ||
sdk "github.com/line/lbm-sdk/types" | ||
accountkeeper "github.com/line/lbm-sdk/x/auth/keeper" | ||
accounttypes "github.com/line/lbm-sdk/x/auth/types" | ||
banktypes "github.com/line/lbm-sdk/x/bank/types" | ||
paramtypes "github.com/line/lbm-sdk/x/params/types" | ||
"github.com/line/ostracon/libs/log" | ||
ostproto "github.com/line/ostracon/proto/ostracon/types" | ||
"github.com/line/tm-db/v2/memdb" | ||
"github.com/stretchr/testify/require" | ||
"math/rand" | ||
"testing" | ||
) | ||
|
||
func genAddress() sdk.AccAddress { | ||
b := make([]byte, 20) | ||
rand.Read(b) | ||
return sdk.BytesToAccAddress(b) | ||
} | ||
|
||
func setupKeeper(storeKey *sdk.KVStoreKey) BaseKeeper { | ||
registry := codectypes.NewInterfaceRegistry() | ||
cdc := codec.NewProtoCodec(registry) | ||
amino := codec.NewLegacyAmino() | ||
|
||
accountStoreKey := sdk.NewKVStoreKey(accounttypes.StoreKey) | ||
accountSubspace := paramtypes.NewSubspace(cdc, amino, accountStoreKey, accounttypes.ModuleName) | ||
accountKeeper := accountkeeper.NewAccountKeeper(cdc, accountStoreKey, accountSubspace, accounttypes.ProtoBaseAccount, nil) | ||
|
||
bankSubspace := paramtypes.NewSubspace(cdc, amino, storeKey, banktypes.StoreKey) | ||
return NewBaseKeeper(cdc, storeKey, accountKeeper, bankSubspace, nil) | ||
} | ||
|
||
func setupContext(t *testing.T, storeKey *sdk.KVStoreKey) sdk.Context { | ||
db := memdb.NewDB() | ||
stateStore := store.NewCommitMultiStore(db) | ||
stateStore.MountStoreWithDB(storeKey, sdk.StoreTypeIAVL, db) | ||
require.NoError(t, stateStore.LoadLatestVersion()) | ||
|
||
return sdk.NewContext(stateStore, ostproto.Header{}, false, log.NewNopLogger()) | ||
} | ||
|
||
func TestInactiveAddr(t *testing.T) { | ||
storeKey := sdk.NewKVStoreKey(banktypes.StoreKey) | ||
bankKeeper := setupKeeper(storeKey) | ||
ctx := setupContext(t, storeKey) | ||
|
||
addr := genAddress() | ||
|
||
require.Equal(t, 0, len(bankKeeper.inactiveAddrs)) | ||
|
||
bankKeeper.addToInactiveAddr(ctx, addr) | ||
require.True(t, bankKeeper.isStoredInactiveAddr(ctx, addr)) | ||
|
||
// duplicate addition, no error expects. | ||
bankKeeper.addToInactiveAddr(ctx, addr) | ||
require.True(t, bankKeeper.isStoredInactiveAddr(ctx, addr)) | ||
|
||
bankKeeper.deleteFromInactiveAddr(ctx, addr) | ||
require.False(t, bankKeeper.isStoredInactiveAddr(ctx, addr)) | ||
|
||
addr2 := genAddress() | ||
require.False(t, bankKeeper.isStoredInactiveAddr(ctx, addr2)) | ||
|
||
// expect no error | ||
bankKeeper.deleteFromInactiveAddr(ctx, addr2) | ||
|
||
// test loadAllInactiveAddrs | ||
bankKeeper.addToInactiveAddr(ctx, addr) | ||
bankKeeper.addToInactiveAddr(ctx, addr2) | ||
require.Equal(t, 0, len(bankKeeper.inactiveAddrs)) | ||
bankKeeper.loadAllInactiveAddrs(ctx) | ||
require.Equal(t, 2, len(bankKeeper.inactiveAddrs)) | ||
} |
Oops, something went wrong.