-
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: update x/bankplus for deprecation (#1223)
* ci: fix pr-labeler * feat: add bankplus * chore: temporal removal of ci * chore: fix for gosec * chore: add bankplus module to go.work.example * chore: update module for DI * chore: go mod tidy * chore: temporal fix for lint * chore: temporal fix for lint * chore: should fail * chore: test-cosmossdk.io/errors * chore: test-cosmossdk.io/store * chore: test-cosmossdk.io/core * chore: should fix lint fail after cosmossdk.io/corev0.12 * chore: after go tidy * chore: add tests * Revert "chore: temporal removal of ci" This reverts commit 630804b. * Revert "ci: fix pr-labeler" This reverts commit 587d2da. * chore: replace cometbft * chore: add bankplus to pr_labeler * chore: update CHANGELOG * chore: add bankplus test ci * chore: fix * chore: add bankplus module to replace bank module * chore: go mod tidy for tests * chore: remove code that use global codec * Update .github/workflows/test.yml Co-authored-by: Shogo Hyodo <[email protected]> * chore: remove code that use global codec from tests * chore: introduce DeactMultiSend type and DI provider * chore: fix tests * chore: fix init order to apply link prefix codec * chore: fix side-effect from original bank.send * chore: remove empty bullet * chore: replace filter logic with SendRestrictionFn filter * chore: no need to check empty balance. Balance will be back while handling tx which throws error * chore: remove duplicate private method * chore: delete files for deprecation * chore: add deprecation function for bankplus * chore: remove unused dependency * chore: update changelog * chore: fix lint * chore: add all migrator from original bank modules * chore: reorder * chore: clean-up * chore: deprecate x/bankplus by moving deprecation logic to simapp * chore: rollback original bank module tests in TestRunMigrations * chore: keep bankplus in x/bankplus for history * chore: fix for buf breaking * chore: fix lint * chore: organize go.mod * chore: add test job for bankplus * chore: refactor to accept kvStoreService instead of key * chore: refactor delete logic not to touch iteration-related things while iteration --------- Co-authored-by: Shogo Hyodo <[email protected]>
- Loading branch information
1 parent
9db31c4
commit 7c39d83
Showing
13 changed files
with
1,734 additions
and
21 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,3 +15,5 @@ | |
- x/collection/**/* | ||
"C:x/foundation": | ||
- x/foundation/**/* | ||
"C:x/bankplus": | ||
- x/bankplus/**/* |
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 |
---|---|---|
|
@@ -323,3 +323,34 @@ jobs: | |
SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }} | ||
with: | ||
projectBaseDir: x/foundation/ | ||
|
||
test-x-bankplus: | ||
runs-on: ubuntu-latest | ||
steps: | ||
- uses: actions/checkout@v3 | ||
- uses: actions/setup-go@v4 | ||
with: | ||
go-version: "1.21" | ||
check-latest: true | ||
cache: true | ||
cache-dependency-path: x/bankplus/go.sum | ||
- uses: technote-space/[email protected] | ||
id: git_diff | ||
with: | ||
PATTERNS: | | ||
x/bankplus/**/*.go | ||
x/bankplus/go.mod | ||
x/bankplus/go.sum | ||
- name: tests | ||
if: env.GIT_DIFF | ||
run: | | ||
cd x/bankplus | ||
go test -mod=readonly -timeout 30m -coverprofile=coverage.out -covermode=atomic -tags='norace ledger test_ledger_mock rocksdb_build' ./... | ||
- name: sonarcloud | ||
if: ${{ env.GIT_DIFF && !github.event.pull_request.draft && env.SONAR_TOKEN != null }} | ||
uses: SonarSource/sonarcloud-github-action@master | ||
env: | ||
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} | ||
SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }} | ||
with: | ||
projectBaseDir: x/bankplus/ |
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 |
---|---|---|
|
@@ -9,4 +9,5 @@ use ( | |
./tests | ||
./x/collection | ||
./x/foundation | ||
./x/bankplus | ||
) |
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,65 @@ | ||
package bankplus | ||
|
||
import ( | ||
"context" | ||
|
||
"cosmossdk.io/core/store" | ||
storetypes "cosmossdk.io/store/types" | ||
|
||
"github.com/cosmos/cosmos-sdk/runtime" | ||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
) | ||
|
||
// inactiveAddrsKeyPrefix 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()...) | ||
} | ||
|
||
// DeprecateBankPlus performs remove logic for bankplus v1. | ||
// This will remove all the state(inactive addresses) | ||
// This supposed to be called in simapp. | ||
// | ||
// Example) simapp/upgrades.go | ||
// | ||
// func (app SimApp) RegisterUpgradeHandlers() { | ||
// app.UpgradeKeeper.SetUpgradeHandler( | ||
// UpgradeName, | ||
// func(ctx context.Context, _ upgradetypes.Plan, fromVM module.VersionMap) (module.VersionMap, error) { | ||
// app.deprecateBankPlusFromSimapp(ctx) | ||
// return app.ModuleManager.RunMigrations(ctx, app.Configurator(), fromVM) | ||
// }, | ||
// ) | ||
// ... | ||
// | ||
// func (app SimApp) deprecateBankPlusFromSimapp(ctx context.Context) { | ||
// for _, key := range app.kvStoreKeys() { | ||
// if key.Name() == banktypes.StoreKey { | ||
// bankStoreService := runtime.NewKVStoreService(key) | ||
// err := bankplus.DeprecateBankPlus(ctx, bankStoreService) | ||
// if err != nil { | ||
// panic(fmt.Errorf("failed to deprecate x/bankplus: %w", err)) | ||
// } | ||
// } | ||
// } | ||
// } | ||
func DeprecateBankPlus(ctx context.Context, bankStoreService store.KVStoreService) error { | ||
kvStore := bankStoreService.OpenKVStore(ctx) | ||
adapter := runtime.KVStoreAdapter(kvStore) | ||
iter := storetypes.KVStorePrefixIterator(adapter, inactiveAddrsKeyPrefix) | ||
defer iter.Close() | ||
|
||
keys := [][]byte{} | ||
for ; iter.Valid(); iter.Next() { | ||
keys = append(keys, iter.Key()) | ||
} | ||
for _, key := range keys { | ||
err := kvStore.Delete(key) | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
return nil | ||
} |
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,93 @@ | ||
package bankplus | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
cmtproto "github.com/cometbft/cometbft/proto/tendermint/types" | ||
cmttime "github.com/cometbft/cometbft/types/time" | ||
"github.com/stretchr/testify/suite" | ||
|
||
"cosmossdk.io/core/address" | ||
"cosmossdk.io/core/store" | ||
storetypes "cosmossdk.io/store/types" | ||
|
||
"github.com/cosmos/cosmos-sdk/codec" | ||
codectestutil "github.com/cosmos/cosmos-sdk/codec/testutil" | ||
"github.com/cosmos/cosmos-sdk/runtime" | ||
"github.com/cosmos/cosmos-sdk/testutil" | ||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
moduletestutil "github.com/cosmos/cosmos-sdk/types/module/testutil" | ||
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types" | ||
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types" | ||
|
||
"github.com/Finschia/finschia-sdk/x/bankplus/types" | ||
) | ||
|
||
func TestDeprecateTestSuite(t *testing.T) { | ||
suite.Run(t, &DeprecationTestSuite{}) | ||
} | ||
|
||
type DeprecationTestSuite struct { | ||
suite.Suite | ||
ctx sdk.Context | ||
cdc codec.Codec | ||
storeService store.KVStoreService | ||
key *storetypes.KVStoreKey | ||
} | ||
|
||
func (s *DeprecationTestSuite) SetupTest() { | ||
s.key = storetypes.NewKVStoreKey(banktypes.StoreKey) | ||
testCtx := testutil.DefaultContextWithDB(s.T(), s.key, storetypes.NewTransientStoreKey("transient_test")) | ||
s.ctx = testCtx.Ctx.WithBlockHeader(cmtproto.Header{Time: cmttime.Now()}) | ||
encCfg := moduletestutil.MakeTestEncodingConfig() | ||
encCfg.Codec = codectestutil.CodecOptions{ | ||
AccAddressPrefix: "link", | ||
ValAddressPrefix: "linkvaloper", | ||
}.NewCodec() | ||
s.cdc = encCfg.Codec | ||
|
||
storeService := runtime.NewKVStoreService(s.key) | ||
s.storeService = storeService | ||
} | ||
|
||
func (s *DeprecationTestSuite) TestDeprecateBankPlus() { | ||
oldAcc := authtypes.NewBaseAccountWithAddress(sdk.AccAddress("acc1")) | ||
anotherOldAcc := authtypes.NewBaseAccountWithAddress(sdk.AccAddress("acc2")) | ||
s.Require().False(isStoredInactiveAddr(s.ctx, s.storeService, oldAcc.GetAddress())) | ||
s.Require().False(isStoredInactiveAddr(s.ctx, s.storeService, anotherOldAcc.GetAddress())) | ||
|
||
addrCdc := s.cdc.InterfaceRegistry().SigningContext().AddressCodec() | ||
addToInactiveAddr(s.ctx, s.storeService, s.cdc, addrCdc, oldAcc.GetAddress()) | ||
addToInactiveAddr(s.ctx, s.storeService, s.cdc, addrCdc, anotherOldAcc.GetAddress()) | ||
s.Require().True(isStoredInactiveAddr(s.ctx, s.storeService, oldAcc.GetAddress())) | ||
s.Require().True(isStoredInactiveAddr(s.ctx, s.storeService, anotherOldAcc.GetAddress())) | ||
|
||
err := DeprecateBankPlus(s.ctx, s.storeService) | ||
|
||
s.Require().NoError(err) | ||
s.Require().False(isStoredInactiveAddr(s.ctx, s.storeService, oldAcc.GetAddress())) | ||
s.Require().False(isStoredInactiveAddr(s.ctx, s.storeService, anotherOldAcc.GetAddress())) | ||
} | ||
|
||
// isStoredInactiveAddr checks if the address is stored or not as blocked address | ||
func isStoredInactiveAddr(ctx context.Context, storeService store.KVStoreService, address sdk.AccAddress) bool { | ||
kvStore := storeService.OpenKVStore(ctx) | ||
bz, _ := kvStore.Get(inactiveAddrKey(address)) | ||
return bz != nil | ||
} | ||
|
||
// addToInactiveAddr adds a blocked address to the store. | ||
func addToInactiveAddr(ctx context.Context, storeService store.KVStoreService, cdc codec.Codec, addrCdc address.Codec, address sdk.AccAddress) { | ||
kvStore := storeService.OpenKVStore(ctx) | ||
addrString, err := addrCdc.BytesToString(address) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
blockedCAddr := types.InactiveAddr{Address: addrString} // nolint: staticcheck // SA1019 types.InactiveAddr is deprecated. Will be removed next version | ||
bz := cdc.MustMarshal(&blockedCAddr) | ||
if err := kvStore.Set(inactiveAddrKey(address), bz); err != nil { | ||
panic(err) | ||
} | ||
} |
Oops, something went wrong.