-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8606a69
commit 63b8a1a
Showing
7 changed files
with
615 additions
and
65 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
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 |
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,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 |
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
File renamed without changes.
Oops, something went wrong.