Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

OTE-558 Add scaffolding for module #1941

Merged
merged 9 commits into from
Jul 23, 2024
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions protocol/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,9 @@ import (
daemontypes "github.com/dydxprotocol/v4-chain/protocol/daemons/types"

// Modules
accountplusmodule "github.com/dydxprotocol/v4-chain/protocol/x/accountplus"
accountplusmodulekeeper "github.com/dydxprotocol/v4-chain/protocol/x/accountplus/keeper"
accountplusmoduletypes "github.com/dydxprotocol/v4-chain/protocol/x/accountplus/types"
assetsmodule "github.com/dydxprotocol/v4-chain/protocol/x/assets"
assetsmodulekeeper "github.com/dydxprotocol/v4-chain/protocol/x/assets/keeper"
assetsmoduletypes "github.com/dydxprotocol/v4-chain/protocol/x/assets/types"
Expand Down Expand Up @@ -294,6 +297,7 @@ type App struct {
FeeGrantKeeper feegrantkeeper.Keeper
ConsensusParamsKeeper consensusparamkeeper.Keeper
GovPlusKeeper govplusmodulekeeper.Keeper
AccountPlusKeeper accountplusmodulekeeper.Keeper

MarketMapKeeper marketmapmodulekeeper.Keeper

Expand Down Expand Up @@ -450,6 +454,7 @@ func New(
govplusmoduletypes.StoreKey,
vaultmoduletypes.StoreKey,
revsharemoduletypes.StoreKey,
accountplusmoduletypes.StoreKey,
marketmapmoduletypes.StoreKey,
)
keys[authtypes.StoreKey] = keys[authtypes.StoreKey].WithLocking()
Expand Down Expand Up @@ -1165,6 +1170,12 @@ func New(
)
listingModule := listingmodule.NewAppModule(appCodec, app.ListingKeeper)

app.AccountPlusKeeper = *accountplusmodulekeeper.NewKeeper(
appCodec,
keys[govplusmoduletypes.StoreKey],
)
accountplusModule := accountplusmodule.NewAppModule(appCodec, app.AccountPlusKeeper)

/**** Module Options ****/

// NOTE: we may consider parsing `appOpts` inside module constructors. For the moment
Expand Down Expand Up @@ -1235,6 +1246,7 @@ func New(
vaultModule,
listingModule,
revShareModule,
accountplusModule,
marketmapModule,
)

Expand Down Expand Up @@ -1285,6 +1297,7 @@ func New(
vaultmoduletypes.ModuleName,
listingmoduletypes.ModuleName,
revsharemoduletypes.ModuleName,
accountplusmoduletypes.ModuleName,
marketmapmoduletypes.ModuleName,
)

Expand Down Expand Up @@ -1331,6 +1344,7 @@ func New(
vaultmoduletypes.ModuleName,
listingmoduletypes.ModuleName,
revsharemoduletypes.ModuleName,
accountplusmoduletypes.ModuleName,
marketmapmoduletypes.ModuleName,
authz.ModuleName, // No-op.
blocktimemoduletypes.ModuleName, // Must be last
Expand Down Expand Up @@ -1378,6 +1392,7 @@ func New(
vaultmoduletypes.ModuleName,
listingmoduletypes.ModuleName,
revsharemoduletypes.ModuleName,
accountplusmoduletypes.ModuleName,
marketmapmoduletypes.ModuleName,
authz.ModuleName,
)
Expand Down Expand Up @@ -1421,6 +1436,7 @@ func New(
vaultmoduletypes.ModuleName,
listingmoduletypes.ModuleName,
revsharemoduletypes.ModuleName,
accountplusmoduletypes.ModuleName,
marketmapmoduletypes.ModuleName,
authz.ModuleName,

Expand Down
6 changes: 4 additions & 2 deletions protocol/app/app_test.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
package app_test

import (
marketmapmodule "github.com/skip-mev/slinky/x/marketmap"
"reflect"
"strings"
"testing"
"time"

listingmodule "github.com/dydxprotocol/v4-chain/protocol/x/listing"
marketmapmodule "github.com/skip-mev/slinky/x/marketmap"

evidencemodule "cosmossdk.io/x/evidence"
feegrantmodule "cosmossdk.io/x/feegrant/module"
Expand All @@ -34,6 +33,7 @@ import (
custommodule "github.com/dydxprotocol/v4-chain/protocol/app/module"
"github.com/dydxprotocol/v4-chain/protocol/mocks"
testapp "github.com/dydxprotocol/v4-chain/protocol/testutil/app"
accountplusmodule "github.com/dydxprotocol/v4-chain/protocol/x/accountplus"
assetsmodule "github.com/dydxprotocol/v4-chain/protocol/x/assets"
blocktimemodule "github.com/dydxprotocol/v4-chain/protocol/x/blocktime"
bridgemodule "github.com/dydxprotocol/v4-chain/protocol/x/bridge"
Expand All @@ -42,6 +42,7 @@ import (
epochsmodule "github.com/dydxprotocol/v4-chain/protocol/x/epochs"
feetiersmodule "github.com/dydxprotocol/v4-chain/protocol/x/feetiers"
govplusmodule "github.com/dydxprotocol/v4-chain/protocol/x/govplus"
listingmodule "github.com/dydxprotocol/v4-chain/protocol/x/listing"
perpetualsmodule "github.com/dydxprotocol/v4-chain/protocol/x/perpetuals"
pricesmodule "github.com/dydxprotocol/v4-chain/protocol/x/prices"
ratelimitmodule "github.com/dydxprotocol/v4-chain/protocol/x/ratelimit"
Expand Down Expand Up @@ -220,6 +221,7 @@ func TestModuleBasics(t *testing.T) {
vaultmodule.AppModuleBasic{},
listingmodule.AppModuleBasic{},
revsharemodule.AppModuleBasic{},
accountplusmodule.AppModuleBasic{},

// slinky marketmap
marketmapmodule.AppModuleBasic{},
Expand Down
2 changes: 2 additions & 0 deletions protocol/app/basic_manager/basic_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
listingmodule "github.com/dydxprotocol/v4-chain/protocol/x/listing"

custommodule "github.com/dydxprotocol/v4-chain/protocol/app/module"
accountplusmodule "github.com/dydxprotocol/v4-chain/protocol/x/accountplus"
assetsmodule "github.com/dydxprotocol/v4-chain/protocol/x/assets"
blocktimemodule "github.com/dydxprotocol/v4-chain/protocol/x/blocktime"
bridgemodule "github.com/dydxprotocol/v4-chain/protocol/x/bridge"
Expand Down Expand Up @@ -99,5 +100,6 @@ var (
revsharemodule.AppModuleBasic{},
listingmodule.AppModuleBasic{},
marketmapmodule.AppModuleBasic{},
accountplusmodule.AppModuleBasic{},
)
)
3 changes: 3 additions & 0 deletions protocol/app/testdata/default_genesis_state.json
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,9 @@
"delegator_starting_infos": [],
"validator_slash_events": []
},
"dydxaccountplus": {
"accounts": []
},
"epochs": {
"epoch_info_list": [
{
Expand Down
2 changes: 2 additions & 0 deletions protocol/app/upgrades/v6.0.0/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
marketmapmoduletypes "github.com/skip-mev/slinky/x/marketmap/types"

"github.com/dydxprotocol/v4-chain/protocol/app/upgrades"
accountplustypes "github.com/dydxprotocol/v4-chain/protocol/x/accountplus/types"
listingtypes "github.com/dydxprotocol/v4-chain/protocol/x/listing/types"
revsharetypes "github.com/dydxprotocol/v4-chain/protocol/x/revshare/types"
)
Expand All @@ -22,6 +23,7 @@ var (
listingtypes.StoreKey,
revsharetypes.StoreKey,
marketmapmoduletypes.StoreKey,
accountplustypes.StoreKey,
},
},
}
Expand Down
5 changes: 4 additions & 1 deletion protocol/scripts/genesis/sample_pregenesis.json
Original file line number Diff line number Diff line change
Expand Up @@ -636,6 +636,9 @@
"validator_historical_rewards": [],
"validator_slash_events": []
},
"dydxaccountplus": {
"accounts": []
},
"epochs": {
"epoch_info_list": [
{
Expand Down Expand Up @@ -1859,7 +1862,7 @@
]
}
},
"app_version": "5.0.0-dev0-385-gb5b86472",
"app_version": "5.0.0-dev0-378-ge9777b09",
"chain_id": "dydx-sample-1",
"consensus": {
"params": {
Expand Down
3 changes: 3 additions & 0 deletions protocol/testing/containertest/preupgrade_genesis.json
Original file line number Diff line number Diff line change
Expand Up @@ -774,6 +774,9 @@
"delegator_starting_infos": [],
"validator_slash_events": []
},
"dydxaccountplus": {
"accounts": []
},
"epochs": {
"epoch_info_list": [
{
Expand Down
4 changes: 2 additions & 2 deletions protocol/x/accountplus/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ func InitGenesis(ctx sdk.Context, k keeper.Keeper, data types.GenesisState) {
}
}

func ExportGenesis(ctx sdk.Context, k keeper.Keeper) types.GenesisState {
return types.GenesisState{
func ExportGenesis(ctx sdk.Context, k keeper.Keeper) *types.GenesisState {
return &types.GenesisState{
Accounts: k.GetAllAccountStates(ctx),
}
}
105 changes: 58 additions & 47 deletions protocol/x/accountplus/genesis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,23 @@ import (
"testing"

sdk "github.com/cosmos/cosmos-sdk/types"
testapp "github.com/dydxprotocol/v4-chain/protocol/testutil/app"
"github.com/dydxprotocol/v4-chain/protocol/testutil/constants"
keepertest "github.com/dydxprotocol/v4-chain/protocol/testutil/keeper"
"github.com/dydxprotocol/v4-chain/protocol/x/accountplus"
"github.com/dydxprotocol/v4-chain/protocol/x/accountplus/keeper"
"github.com/dydxprotocol/v4-chain/protocol/x/accountplus/testutils"
"github.com/dydxprotocol/v4-chain/protocol/x/accountplus/types"
"github.com/google/go-cmp/cmp"
"github.com/stretchr/testify/require"
)

func TestImportExportGenesis(t *testing.T) {
baseTsNonce := uint64(math.Pow(2, 40))
tests := map[string]struct {
genesisState *types.GenesisState
// The order of this list may not match the order in GenesisState. We want our tests to be deterministic so
// order of expectedAccountStates was manually set based on test debug. This ordering should only be changed if
// additional accounts added to genesisState. If a feature breaks the existing ordering, should look into ßwhy.
expectedAccountStates []*types.AccountState
}{
"non-empty genesis": {
genesisState: &types.GenesisState{
Expand All @@ -36,78 +40,85 @@ func TestImportExportGenesis(t *testing.T) {
MaxEjectedNonce: baseTsNonce + 1,
},
},
{
Address: constants.CarlAccAddress.String(),
TimestampNonceDetails: &types.TimestampNonceDetails{
TimestampNonces: []uint64{baseTsNonce + 5, baseTsNonce + 6, baseTsNonce + 7},
MaxEjectedNonce: baseTsNonce + 1,
},
},
},
},
expectedAccountStates: []*types.AccountState{
{
Address: constants.AliceAccAddress.String(),
TimestampNonceDetails: &types.TimestampNonceDetails{
TimestampNonces: []uint64{baseTsNonce + 1, baseTsNonce + 2, baseTsNonce + 3},
MaxEjectedNonce: baseTsNonce,
},
},
{
Address: constants.CarlAccAddress.String(),
TimestampNonceDetails: &types.TimestampNonceDetails{
TimestampNonces: []uint64{baseTsNonce + 5, baseTsNonce + 6, baseTsNonce + 7},
MaxEjectedNonce: baseTsNonce + 1,
},
},
{
Address: constants.BobAccAddress.String(),
TimestampNonceDetails: &types.TimestampNonceDetails{
TimestampNonces: []uint64{baseTsNonce + 5, baseTsNonce + 6, baseTsNonce + 7},
MaxEjectedNonce: baseTsNonce + 1,
},
},
},
},
"empty genesis": {
genesisState: &types.GenesisState{
Accounts: []*types.AccountState{},
},
expectedAccountStates: []*types.AccountState{},
},
}

for name, tc := range tests {
t.Run(name, func(t *testing.T) {
// TODO: deprecated, reference protocol/vest/genesis_test.go for up to date initialization
ctx, k, _, _ := keepertest.TimestampNonceKeepers(t)
tApp := testapp.NewTestAppBuilder(t).Build()
ctx := tApp.InitChain()
k := tApp.App.AccountPlusKeeper

// Initialize genesis state
accountplus.InitGenesis(ctx, *k, *tc.genesisState)
accountplus.InitGenesis(ctx, k, *tc.genesisState)

// Check that keeper state is correct
compareKeeperWithGenesisState(t, ctx, k, tc.genesisState)
compareKeeperWithGenesisState(t, ctx, &k, tc.expectedAccountStates)

// Export the genesis state
exportedGenesis := accountplus.ExportGenesis(ctx, *k)
exportedGenesis := accountplus.ExportGenesis(ctx, k)

// Ensure the exported state matches the expected state
requireGenesisStatesEqual(t, tc.genesisState, &exportedGenesis)
expectedGenesis := &types.GenesisState{
Accounts: tc.expectedAccountStates,
}
requireGenesisStatesEqual(t, exportedGenesis, expectedGenesis)
})
}
}

func compareKeeperWithGenesisState(t *testing.T, ctx sdk.Context, k *keeper.Keeper, genesisState *types.GenesisState) {
accountStates := k.GetAllAccountStates(ctx)
func compareKeeperWithGenesisState(
t *testing.T,
ctx sdk.Context,
k *keeper.Keeper,
expectedAccountStates []*types.AccountState,
) {
// Compare states. Order matters.
isEqual := testutils.CompareAccountStateLists(k.GetAllAccountStates(ctx), expectedAccountStates)

compareAccountStates(t, accountStates, genesisState.GetAccounts())
require.True(t, isEqual, "Keeper account states does not match Genesis account states")
}

func requireGenesisStatesEqual(t *testing.T, actualGenesisState, expectedGenesisState *types.GenesisState) {
compareAccountStates(t, actualGenesisState.GetAccounts(), expectedGenesisState.GetAccounts())
}

func compareAccountStates(t *testing.T, actualAccountStates, expectedAccountStates []*types.AccountState) {
require.Equal(t, len(actualAccountStates), len(expectedAccountStates), "GenesisState.Accounts length mismatch")
// Iterate through the account states and test equality on each field
// We require that the ordering of accountState be deterministic so that should more complicated logic
// be introduced in the future, this test can catch any unintended effects.
for i := range actualAccountStates {
require.Equal(
t,
actualAccountStates[i].Address,
expectedAccountStates[i].Address,
"Account address mismatch at index %d", i,
)
compareTimestampNonceDetails(
t,
actualAccountStates[i].GetTimestampNonceDetails(),
expectedAccountStates[i].GetTimestampNonceDetails(),
)
}
}

func compareTimestampNonceDetails(t *testing.T, actualDetails, expectedDetails *types.TimestampNonceDetails) {
equal := cmp.Equal(
actualDetails.GetTimestampNonces(),
expectedDetails.GetTimestampNonces(),
)

require.True(t, equal, "TimestampNonces mismatch for account")
isEqual := testutils.CompareAccountStateLists(actualGenesisState.GetAccounts(), expectedGenesisState.GetAccounts())

require.Equal(
t,
actualDetails.GetMaxEjectedNonce(),
expectedDetails.GetMaxEjectedNonce(),
"LastEjectedNonce mismatch",
)
require.True(t, isEqual, "GenesisState mismatch")
}
Loading
Loading