-
Notifications
You must be signed in to change notification settings - Fork 123
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[OTE-882] Add prefix to accountplus keeper (#2526)
- Loading branch information
1 parent
0e185fd
commit 2198a3b
Showing
6 changed files
with
226 additions
and
27 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 |
---|---|---|
@@ -0,0 +1,81 @@ | ||
package v_8_0 | ||
|
||
import ( | ||
"testing" | ||
|
||
"cosmossdk.io/store/prefix" | ||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
testapp "github.com/dydxprotocol/v4-chain/protocol/testutil/app" | ||
accountplustypes "github.com/dydxprotocol/v4-chain/protocol/x/accountplus/types" | ||
"github.com/stretchr/testify/suite" | ||
) | ||
|
||
type UpgradeTestSuite struct { | ||
suite.Suite | ||
|
||
tApp *testapp.TestApp | ||
Ctx sdk.Context | ||
} | ||
|
||
func TestMigrateAccountplusAccountState(t *testing.T) { | ||
suite.Run(t, new(UpgradeTestSuite)) | ||
} | ||
|
||
func (s *UpgradeTestSuite) SetupTest() { | ||
s.tApp = testapp.NewTestAppBuilder(s.T()).Build() | ||
s.Ctx = s.tApp.InitChain() | ||
} | ||
|
||
func (s *UpgradeTestSuite) TestUpgrade_MigrateAccountplusAccountState() { | ||
ctx := s.Ctx | ||
store := ctx.KVStore(s.tApp.App.AccountPlusKeeper.GetStoreKey()) | ||
prefixStore := prefix.NewStore(store, []byte(accountplustypes.AccountStateKeyPrefix)) | ||
|
||
// Create some AccountState with no prefixes | ||
addresses := []string{"address1", "address2", "address3"} | ||
for _, addr := range addresses { | ||
accAddress := sdk.AccAddress([]byte(addr)) | ||
accountState := accountplustypes.AccountState{ | ||
Address: addr, | ||
TimestampNonceDetails: accountplustypes.TimestampNonceDetails{ | ||
TimestampNonces: []uint64{1, 2, 3}, | ||
MaxEjectedNonce: 0, | ||
}, | ||
} | ||
bz := s.tApp.App.AccountPlusKeeper.GetCdc().MustMarshal(&accountState) | ||
store.Set(accAddress, bz) | ||
} | ||
|
||
// Verify unprefixed keys were successfully created | ||
for _, addr := range addresses { | ||
accAddress := sdk.AccAddress([]byte(addr)) | ||
bz := store.Get(accAddress) | ||
s.Require().NotNil(bz, "Unprefixed key not created for %s", addr) | ||
} | ||
|
||
// Migrate | ||
migrateAccountplusAccountState(ctx, s.tApp.App.AccountPlusKeeper) | ||
|
||
// Verify that unprefixed keys are deleted and prefixed keys exist | ||
for _, addr := range addresses { | ||
accAddress := sdk.AccAddress([]byte(addr)) | ||
|
||
// Check that the old key no longer exists | ||
bzOld := store.Get(accAddress) | ||
s.Require().Nil(bzOld, "Unprefixed AccountState should be deleted for %s", addr) | ||
|
||
// Check that the new prefixed key exists | ||
bzNew := prefixStore.Get(accAddress) | ||
var actualAccountState accountplustypes.AccountState | ||
s.tApp.App.AccountPlusKeeper.GetCdc().MustUnmarshal(bzNew, &actualAccountState) | ||
expectedAccountState := accountplustypes.AccountState{ | ||
Address: addr, | ||
TimestampNonceDetails: accountplustypes.TimestampNonceDetails{ | ||
TimestampNonces: []uint64{1, 2, 3}, | ||
MaxEjectedNonce: 0, | ||
}, | ||
} | ||
s.Require().NotNil(bzNew, "Prefixed AccountState should exist for %s", addr) | ||
s.Require().Equal(expectedAccountState, actualAccountState, "Incorrect AccountState after migration for %s", addr) | ||
} | ||
} |
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,63 @@ | ||
package v_8_0 | ||
|
||
import ( | ||
"bytes" | ||
"fmt" | ||
|
||
storetypes "cosmossdk.io/store/types" | ||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
accountpluskeeper "github.com/dydxprotocol/v4-chain/protocol/x/accountplus/keeper" | ||
accountplustypes "github.com/dydxprotocol/v4-chain/protocol/x/accountplus/types" | ||
) | ||
|
||
// Migrate accountplus AccountState in kvstore from non-prefixed keys to prefixed keys | ||
func migrateAccountplusAccountState(ctx sdk.Context, k accountpluskeeper.Keeper) { | ||
ctx.Logger().Info("Migrating accountplus module AccountState in kvstore from non-prefixed keys to prefixed keys") | ||
|
||
store := ctx.KVStore(k.GetStoreKey()) | ||
|
||
// Iterate on unprefixed keys | ||
iterator := storetypes.KVStorePrefixIterator(store, nil) | ||
defer iterator.Close() | ||
|
||
var keysToDelete [][]byte | ||
var accountStatesToSet []struct { | ||
address sdk.AccAddress | ||
accountState accountplustypes.AccountState | ||
} | ||
for ; iterator.Valid(); iterator.Next() { | ||
key := iterator.Key() | ||
|
||
// Double check that key does not have prefix | ||
if bytes.HasPrefix(key, []byte(accountplustypes.AccountStateKeyPrefix)) { | ||
panic(fmt.Sprintf("unexpected key with prefix %X found during migration", accountplustypes.AccountStateKeyPrefix)) | ||
} | ||
|
||
value := iterator.Value() | ||
var accountState accountplustypes.AccountState | ||
if err := k.GetCdc().Unmarshal(value, &accountState); err != nil { | ||
panic(fmt.Sprintf("failed to unmarshal AccountState for key %X: %s", key, err)) | ||
} | ||
|
||
accountStatesToSet = append(accountStatesToSet, struct { | ||
address sdk.AccAddress | ||
accountState accountplustypes.AccountState | ||
}{key, accountState}) | ||
|
||
keysToDelete = append(keysToDelete, key) | ||
} | ||
|
||
// Set prefixed keys | ||
for _, item := range accountStatesToSet { | ||
k.SetAccountState(ctx, item.address, item.accountState) | ||
} | ||
|
||
// Delete unprefixed keys | ||
for _, key := range keysToDelete { | ||
store.Delete(key) | ||
} | ||
|
||
ctx.Logger().Info("Successfully migrated accountplus AccountState keys") | ||
} | ||
|
||
// TODO: Scaffolding for upgrade: https://linear.app/dydx/issue/OTE-886/v8-upgrade-handler-scaffold |
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
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