Skip to content

Commit

Permalink
test: MsgClaimMorseApplication
Browse files Browse the repository at this point in the history
  • Loading branch information
bryanchriswhite committed Feb 26, 2025
1 parent 8606a69 commit 63b8a1a
Show file tree
Hide file tree
Showing 7 changed files with 615 additions and 65 deletions.
Original file line number Diff line number Diff line change
@@ -1,73 +1,16 @@
package migration

import (
"testing"

"cosmossdk.io/math"
sdk "github.com/cosmos/cosmos-sdk/types"
cosmostypes "github.com/cosmos/cosmos-sdk/types"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
"github.com/stretchr/testify/require"
"github.com/stretchr/testify/suite"

"github.com/pokt-network/poktroll/app/volatile"
"github.com/pokt-network/poktroll/testutil/integration/suites"
"github.com/pokt-network/poktroll/testutil/sample"
migrationtypes "github.com/pokt-network/poktroll/x/migration/types"
)

type MigrationModuleTestSuite struct {
suites.MigrationModuleSuite

// numMorseClaimableAccounts is the number of morse claimable accounts to
// generate when calling #GenerateMorseAccountState.
numMorseClaimableAccounts int
}

func (s *MigrationModuleTestSuite) SetupTest() {
// Initialize a new integration app for the suite.
s.NewApp(s.T())

s.numMorseClaimableAccounts = 10

// Assign the app to nested suites.
// TODO_UPNEXT(@bryanchriswhite, #1043): Initialize the app module suite.
// s.AppSuite.SetApp(s.GetApp())
}

func TestMigrationModuleSuite(t *testing.T) {
suite.Run(t, &MigrationModuleTestSuite{})
}

// TestImportMorseClaimableAccounts exercises importing and persistence of morse claimable accounts.
func (s *MigrationModuleTestSuite) TestImportMorseClaimableAccounts() {
s.GenerateMorseAccountState(s.T(), s.numMorseClaimableAccounts)
msgImportRes := s.ImportMorseClaimableAccounts(s.T())
morseAccountState := s.GetAccountState(s.T())
morseAccountStateHash, err := morseAccountState.GetHash()
require.NoError(s.T(), err)

expectedMsgImportRes := &migrationtypes.MsgImportMorseClaimableAccountsResponse{
StateHash: morseAccountStateHash,
NumAccounts: uint64(s.numMorseClaimableAccounts),
}
require.Equal(s.T(), expectedMsgImportRes, msgImportRes)

foundMorseClaimableAccounts := s.QueryAllMorseClaimableAccounts(s.T())
require.Equal(s.T(), s.numMorseClaimableAccounts, len(foundMorseClaimableAccounts))

for _, expectedMorseClaimableAccount := range morseAccountState.Accounts {
isFound := false
for _, foundMorseClaimableAccount := range foundMorseClaimableAccounts {
if foundMorseClaimableAccount.GetMorseSrcAddress() == expectedMorseClaimableAccount.GetMorseSrcAddress() {
require.Equal(s.T(), *expectedMorseClaimableAccount, foundMorseClaimableAccount)
isFound = true
break
}
}
require.True(s.T(), isFound)
}
}

// TestClaimMorseAccount exercises claiming of MorseClaimableAccounts.
// It only exercises claiming of account balances and does not exercise
// the staking any actors as a result of claiming.
Expand All @@ -76,8 +19,9 @@ func (s *MigrationModuleTestSuite) TestClaimMorseAccount() {
s.ImportMorseClaimableAccounts(s.T())

shannonDestAddr := sample.AccAddress()

bankClient := s.GetBankQueryClient(s.T())

// Assert that the shannonDestAddr account initially has a zero balance.
shannonDestBalance, err := bankClient.GetBalance(s.SdkCtx(), shannonDestAddr)
require.NoError(s.T(), err)
require.True(s.T(), shannonDestBalance.IsZero())
Expand Down Expand Up @@ -111,5 +55,5 @@ func (s *MigrationModuleTestSuite) TestClaimMorseAccount() {
migrationModuleAddress := authtypes.NewModuleAddress(migrationtypes.ModuleName).String()
migrationModuleBalance, err := bankClient.GetBalance(s.SdkCtx(), migrationModuleAddress)
require.NoError(s.T(), err)
require.Equal(s.T(), sdk.NewCoin(volatile.DenomuPOKT, math.ZeroInt()), *migrationModuleBalance)
require.Equal(s.T(), cosmostypes.NewCoin(volatile.DenomuPOKT, math.ZeroInt()), *migrationModuleBalance)
}
130 changes: 130 additions & 0 deletions tests/integration/migration/morse_claim_app_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
package migration

import (
"testing"

"cosmossdk.io/math"
cosmostypes "github.com/cosmos/cosmos-sdk/types"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
"github.com/stretchr/testify/require"

"github.com/pokt-network/poktroll/app/volatile"
"github.com/pokt-network/poktroll/testutil/sample"
apptypes "github.com/pokt-network/poktroll/x/application/types"
migrationtypes "github.com/pokt-network/poktroll/x/migration/types"
sharedtypes "github.com/pokt-network/poktroll/x/shared/types"
)

// TestClaimMorseApplication exercises claiming of a MorseClaimableAccount as a staked application.
func (s *MigrationModuleTestSuite) TestClaimMorseApplication() {
s.GenerateMorseAccountState(s.T(), s.numMorseClaimableAccounts)
s.ImportMorseClaimableAccounts(s.T())

stakeOffset := cosmostypes.NewInt64Coin(volatile.DenomuPOKT, 9999)
testCases := []struct {
desc string
getStake func() *cosmostypes.Coin
}{
{
desc: "claim morse application with same staked/unstaked ratio (default)",
getStake: func() *cosmostypes.Coin { return nil },
},
{
desc: "claim morse application with same staked/unstaked ratio (explicit)",
getStake: func() *cosmostypes.Coin {
stake := s.GetAccountState(s.T()).Accounts[1].
GetApplicationStake()
return &stake
},
},
{
desc: "claim morse application with higher staked/unstaked ratio",
getStake: func() *cosmostypes.Coin {
stake := s.GetAccountState(s.T()).Accounts[2].
GetApplicationStake().
Add(stakeOffset)
return &stake
},
},
{
desc: "claim morse application with lower staked/unstaked ratio",
getStake: func() *cosmostypes.Coin {
stake := s.GetAccountState(s.T()).Accounts[3].
GetApplicationStake().
Sub(stakeOffset)
return &stake
},
},
}

for testCaseIdx, testCase := range testCases {
s.T().Run(testCase.desc, func(t *testing.T) {
shannonDestAddr := sample.AccAddress()
bankClient := s.GetBankQueryClient(s.T())

// Assert that the shannonDestAddr account initially has a zero balance.
shannonDestBalance, err := bankClient.GetBalance(s.SdkCtx(), shannonDestAddr)
require.NoError(s.T(), err)
require.Equal(s.T(), int64(0), shannonDestBalance.Amount.Int64())

morseSrcAddr, claimAppRes := s.ClaimMorseApplication(
s.T(), uint64(testCaseIdx+1),
shannonDestAddr,
testCase.getStake(),
s.appServiceConfig,
)

expectedMorseClaimableAccount := s.GetAccountState(s.T()).Accounts[testCaseIdx]
expectedStake := testCase.getStake()
if expectedStake == nil {
expectedStake = &expectedMorseClaimableAccount.ApplicationStake
}

expectedBalance := expectedMorseClaimableAccount.GetUnstakedBalance().
Add(expectedMorseClaimableAccount.GetApplicationStake()).
Add(expectedMorseClaimableAccount.GetSupplierStake()).
Sub(*expectedStake)

expectedClaimARes := &migrationtypes.MsgClaimMorseApplicationResponse{
MorseSrcAddress: morseSrcAddr,
ClaimedBalance: expectedBalance,
ClaimedApplicationStake: *expectedStake,
ClaimedAtHeight: s.SdkCtx().BlockHeight() - 1,
ServiceId: s.appServiceConfig.GetServiceId(),
}
require.Equal(s.T(), expectedClaimARes, claimAppRes)

// Assert that the MorseClaimableAccount was updated on-chain.
expectedMorseClaimableAccount.ShannonDestAddress = shannonDestAddr
expectedMorseClaimableAccount.ClaimedAtHeight = s.SdkCtx().BlockHeight() - 1
morseClaimableAccount := s.QueryMorseClaimableAccount(s.T(), morseSrcAddr)
require.Equal(s.T(), expectedMorseClaimableAccount, morseClaimableAccount)

// Assert that the shannonDestAddr account balance has been updated.
shannonDestBalance, err = bankClient.GetBalance(s.GetApp().GetSdkCtx(), shannonDestAddr)
require.NoError(s.T(), err)
require.Equal(s.T(), expectedBalance, *shannonDestBalance)

// Assert that the migration module account balance returns to zero.
migrationModuleAddress := authtypes.NewModuleAddress(migrationtypes.ModuleName).String()
migrationModuleBalance, err := bankClient.GetBalance(s.SdkCtx(), migrationModuleAddress)
require.NoError(s.T(), err)
require.Equal(s.T(), cosmostypes.NewCoin(volatile.DenomuPOKT, math.ZeroInt()), *migrationModuleBalance)

// Assert that the application was staked.
expectedApp := apptypes.Application{
Address: shannonDestAddr,
Stake: expectedStake,
ServiceConfigs: []*sharedtypes.ApplicationServiceConfig{s.appServiceConfig},
}
appClient := s.AppSuite.GetAppQueryClient(s.T())
app, err := appClient.GetApplication(s.SdkCtx(), shannonDestAddr)
require.NoError(s.T(), err)
require.Equal(s.T(), expectedApp, app)
})
}
}

// TODO_IN_THIS_COMMIT: error cases...
// - stake is below min stake
// - stake is greater than available total tokens
86 changes: 86 additions & 0 deletions tests/integration/migration/morse_import_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package migration

import (
"testing"

cosmostypes "github.com/cosmos/cosmos-sdk/types"
"github.com/stretchr/testify/require"
"github.com/stretchr/testify/suite"

"github.com/pokt-network/poktroll/app/volatile"
"github.com/pokt-network/poktroll/testutil/integration/suites"
apptypes "github.com/pokt-network/poktroll/x/application/types"
migrationtypes "github.com/pokt-network/poktroll/x/migration/types"
sharedtypes "github.com/pokt-network/poktroll/x/shared/types"
)

const testServiceId = "svc1"

type MigrationModuleTestSuite struct {
suites.MigrationModuleSuite

// numMorseClaimableAccounts is the number of morse claimable accounts to
// generate when calling #GenerateMorseAccountState.
numMorseClaimableAccounts int

appMinStake cosmostypes.Coin

// AppServiceConfig is the service config to be used when claiming morse accounts.
// It is assigned in the #SetupTest method.
appServiceConfig *sharedtypes.ApplicationServiceConfig
}

func (s *MigrationModuleTestSuite) SetupTest() {
s.appMinStake = cosmostypes.NewInt64Coin(volatile.DenomuPOKT, 100)

// Set the default application min stake.
// DEV_NOTE: This is simpler than modifying genesis or on-chain params.
apptypes.DefaultMinStake = s.appMinStake

// Initialize a new integration app for the suite.
s.NewApp(s.T())

s.numMorseClaimableAccounts = 10
s.appServiceConfig = &sharedtypes.ApplicationServiceConfig{ServiceId: testServiceId}

// Assign the app to nested suites.
s.AppSuite.SetApp(s.GetApp())
}

func TestMigrationModuleSuite(t *testing.T) {
suite.Run(t, &MigrationModuleTestSuite{})
}

// TestImportMorseClaimableAccounts exercises importing and persistence of morse claimable accounts.
func (s *MigrationModuleTestSuite) TestImportMorseClaimableAccounts() {
s.GenerateMorseAccountState(s.T(), s.numMorseClaimableAccounts)
msgImportRes := s.ImportMorseClaimableAccounts(s.T())
morseAccountState := s.GetAccountState(s.T())
morseAccountStateHash, err := morseAccountState.GetHash()
require.NoError(s.T(), err)

expectedMsgImportRes := &migrationtypes.MsgImportMorseClaimableAccountsResponse{
StateHash: morseAccountStateHash,
NumAccounts: uint64(s.numMorseClaimableAccounts),
}
require.Equal(s.T(), expectedMsgImportRes, msgImportRes)

foundMorseClaimableAccounts := s.QueryAllMorseClaimableAccounts(s.T())
require.Equal(s.T(), s.numMorseClaimableAccounts, len(foundMorseClaimableAccounts))

for _, expectedMorseClaimableAccount := range morseAccountState.Accounts {
isFound := false
for _, foundMorseClaimableAccount := range foundMorseClaimableAccounts {
if foundMorseClaimableAccount.GetMorseSrcAddress() == expectedMorseClaimableAccount.GetMorseSrcAddress() {
require.Equal(s.T(), *expectedMorseClaimableAccount, foundMorseClaimableAccount)
isFound = true
break
}
}
require.True(s.T(), isFound)
}
}

// TODO_IN_THIS_COMMIT: error cases...
// - invalid authority
// - MorseAccountState hash mismatch
45 changes: 43 additions & 2 deletions testutil/integration/suites/migration.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@ package suites
import (
"testing"

sdk "github.com/cosmos/cosmos-sdk/types"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
govtypes "github.com/cosmos/cosmos-sdk/x/gov/types"
"github.com/stretchr/testify/require"

"github.com/pokt-network/poktroll/testutil/testmigration"
migrationtypes "github.com/pokt-network/poktroll/x/migration/types"
sharedtypes "github.com/pokt-network/poktroll/x/shared/types"
)

var _ IntegrationSuite = (*MigrationModuleSuite)(nil)
Expand All @@ -17,11 +19,11 @@ var _ IntegrationSuite = (*MigrationModuleSuite)(nil)
// functionality. It is intended to be embedded in dependent integration test suites.
type MigrationModuleSuite struct {
BaseIntegrationSuite
// TODO_UPNEXT(@bryanchriswhite, #1043): Add ApplicationModuleSuite to the suite.
// AppSuite ApplicationModuleSuite
AppSuite ApplicationModuleSuite

// accountState is the generated MorseAccountState to be imported into the migration module.
accountState *migrationtypes.MorseAccountState

// numMorseClaimableAccounts is the number of morse claimable accounts to generate when calling #GenerateMorseAccountState.
numMorseClaimableAccounts int
}
Expand Down Expand Up @@ -126,3 +128,42 @@ func (s *MigrationModuleSuite) QueryAllMorseClaimableAccounts(t *testing.T) []mi

return morseClaimableAcctRes.MorseClaimableAccount
}

// ClaimMorseApplication claims the given MorseClaimableAccount as a staked application
// by running a MsgClaimMorseApplication message.
// It returns the expected Morse source address and the MsgClaimMorseAccountResponse.
// DEV_NOTE: morseAccountIdx is 1-based.
func (s *MigrationModuleSuite) ClaimMorseApplication(
t *testing.T,
morseAccountIdx uint64,
shannonDestAddr string,
stake *sdk.Coin,
serviceConfig *sharedtypes.ApplicationServiceConfig,
) (expectedMorseSrcAddr string, _ *migrationtypes.MsgClaimMorseApplicationResponse) {
t.Helper()

morsePrivateKey := testmigration.NewMorsePrivateKey(t, morseAccountIdx)
expectedMorseSrcAddr = morsePrivateKey.PubKey().Address().String()
require.Equal(t,
expectedMorseSrcAddr,
s.accountState.Accounts[morseAccountIdx-1].MorseSrcAddress,
)

morseClaimMsg, err := migrationtypes.NewMsgClaimMorseApplication(
shannonDestAddr,
expectedMorseSrcAddr,
morsePrivateKey,
stake,
serviceConfig,
)
require.NoError(t, err)

// Claim a Morse claimable account.
resAny, err := s.GetApp().RunMsg(t, morseClaimMsg)
require.NoError(t, err)

claimApplicationRes, ok := resAny.(*migrationtypes.MsgClaimMorseApplicationResponse)
require.True(t, ok)

return expectedMorseSrcAddr, claimApplicationRes
}
Loading

0 comments on commit 63b8a1a

Please sign in to comment.