From 71f51129c275dcce267999c88027fbb07a6b39f9 Mon Sep 17 00:00:00 2001 From: Bryan White Date: Wed, 29 Jan 2025 15:17:47 +0100 Subject: [PATCH] refactor: fixture generator testutil --- cmd/poktrolld/cmd/migrate/migrate_test.go | 101 +------------------ testutil/testmigration/fixtures.go | 116 ++++++++++++++++++++++ 2 files changed, 120 insertions(+), 97 deletions(-) create mode 100644 testutil/testmigration/fixtures.go diff --git a/cmd/poktrolld/cmd/migrate/migrate_test.go b/cmd/poktrolld/cmd/migrate/migrate_test.go index dee73474d..4216d363c 100644 --- a/cmd/poktrolld/cmd/migrate/migrate_test.go +++ b/cmd/poktrolld/cmd/migrate/migrate_test.go @@ -1,22 +1,17 @@ package migrate import ( - "encoding/binary" "fmt" "math" - "math/rand" "os" "path/filepath" "strings" "testing" - cometcrypto "github.com/cometbft/cometbft/crypto/ed25519" cmtjson "github.com/cometbft/cometbft/libs/json" - cosmostypes "github.com/cosmos/cosmos-sdk/types" - "github.com/regen-network/gocuke" "github.com/stretchr/testify/require" - "github.com/pokt-network/poktroll/app/volatile" + "github.com/pokt-network/poktroll/testutil/testmigration" migrationtypes "github.com/pokt-network/poktroll/x/migration/types" ) @@ -26,7 +21,7 @@ func TestCollectMorseAccounts(t *testing.T) { inputFile, err := os.CreateTemp(tmpDir, "morse-state-input.json") require.NoError(t, err) - morseStateExportBz, morseAccountStateBz := newMorseStateExportAndAccountState(t, 10) + morseStateExportBz, morseAccountStateBz := testmigration.NewMorseStateExportAndAccountStateBytes(t, 10) _, err = inputFile.Write(morseStateExportBz) require.NoError(t, err) @@ -55,7 +50,7 @@ func TestNewTestMorseStateExport(t *testing.T) { for i := 1; i < 10; i++ { t.Run(fmt.Sprintf("num_accounts=%d", i), func(t *testing.T) { morseStateExport := new(migrationtypes.MorseStateExport) - stateExportBz, _ := newMorseStateExportAndAccountState(t, i) + stateExportBz, _ := testmigration.NewMorseStateExportAndAccountStateBytes(t, i) err := cmtjson.Unmarshal(stateExportBz, morseStateExport) require.NoError(t, err) @@ -79,7 +74,7 @@ func BenchmarkTransformMorseState(b *testing.B) { for i := 0; i < 5; i++ { numAccounts := int(math.Pow10(i + 1)) morseStateExport := new(migrationtypes.MorseStateExport) - morseStateExportBz, _ := newMorseStateExportAndAccountState(b, numAccounts) + morseStateExportBz, _ := testmigration.NewMorseStateExportAndAccountStateBytes(b, numAccounts) err := cmtjson.Unmarshal(morseStateExportBz, morseStateExport) require.NoError(b, err) @@ -94,91 +89,3 @@ func BenchmarkTransformMorseState(b *testing.B) { }) } } - -// TODO_CONSIDERATION: Test/benchmark execution speed can be optimized by refactoring this to a pre-generate fixture. -func newMorseStateExportAndAccountState( - t gocuke.TestingT, - numAccounts int, -) (morseStateExportBz []byte, morseAccountStateBz []byte) { - morseStateExport := &migrationtypes.MorseStateExport{ - AppHash: "", - AppState: &migrationtypes.MorseAppState{ - Application: &migrationtypes.MorseApplications{}, - Auth: &migrationtypes.MorseAuth{}, - Pos: &migrationtypes.MorsePos{}, - }, - } - - morseAccountState := &migrationtypes.MorseAccountState{ - Accounts: make([]*migrationtypes.MorseAccount, numAccounts), - } - - for i := 1; i < numAccounts+1; i++ { - seedUint := rand.Uint64() - seedBz := make([]byte, 8) - binary.LittleEndian.PutUint64(seedBz, seedUint) - privKey := cometcrypto.GenPrivKeyFromSecret(seedBz) - pubKey := privKey.PubKey() - balanceAmount := int64(1e6*i + i) // i_000_00i - appStakeAmount := int64(1e5*i + (i * 10)) // i00_0i0 - supplierStakeAmount := int64(1e4*i + (i * 100)) // i0_i00 - sumAmount := balanceAmount + appStakeAmount + supplierStakeAmount // i_ii0_iii - - // Add an account. - morseStateExport.AppState.Auth.Accounts = append( - morseStateExport.AppState.Auth.Accounts, - &migrationtypes.MorseAuthAccount{ - Type: "posmint/Account", - Value: &migrationtypes.MorseAccount{ - Address: pubKey.Address(), - Coins: cosmostypes.NewCoins(cosmostypes.NewInt64Coin(volatile.DenomuPOKT, balanceAmount)), - PubKey: &migrationtypes.MorsePublicKey{ - Value: pubKey.Bytes(), - }, - }, - }, - ) - - // Add an application. - morseStateExport.AppState.Application.Applications = append( - morseStateExport.AppState.Application.Applications, - &migrationtypes.MorseApplication{ - Address: pubKey.Address(), - PublicKey: pubKey.Bytes(), - Jailed: false, - Status: 2, - StakedTokens: fmt.Sprintf("%d", appStakeAmount), - }, - ) - - // Add a supplier. - morseStateExport.AppState.Pos.Validators = append( - morseStateExport.AppState.Pos.Validators, - &migrationtypes.MorseValidator{ - Address: pubKey.Address(), - PublicKey: pubKey.Bytes(), - Jailed: false, - Status: 2, - StakedTokens: fmt.Sprintf("%d", supplierStakeAmount), - }, - ) - - // Add the account to the morseAccountState. - morseAccountState.Accounts[i-1] = &migrationtypes.MorseAccount{ - Address: pubKey.Address(), - Coins: cosmostypes.NewCoins(cosmostypes.NewInt64Coin(volatile.DenomuPOKT, sumAmount)), - PubKey: &migrationtypes.MorsePublicKey{ - Value: pubKey.Bytes(), - }, - } - } - - var err error - morseStateExportBz, err = cmtjson.Marshal(morseStateExport) - require.NoError(t, err) - - morseAccountStateBz, err = cmtjson.Marshal(morseAccountState) - require.NoError(t, err) - - return morseStateExportBz, morseAccountStateBz -} diff --git a/testutil/testmigration/fixtures.go b/testutil/testmigration/fixtures.go new file mode 100644 index 000000000..e360f593d --- /dev/null +++ b/testutil/testmigration/fixtures.go @@ -0,0 +1,116 @@ +package testmigration + +import ( + "encoding/binary" + "fmt" + "math/rand" + + cometcrypto "github.com/cometbft/cometbft/crypto/ed25519" + cmtjson "github.com/cometbft/cometbft/libs/json" + cosmostypes "github.com/cosmos/cosmos-sdk/types" + "github.com/regen-network/gocuke" + "github.com/stretchr/testify/require" + + "github.com/pokt-network/poktroll/app/volatile" + migrationtypes "github.com/pokt-network/poktroll/x/migration/types" +) + +// TODO_IN_THIS_COMMIT: godoc... +// TODO_CONSIDERATION: Test/benchmark execution speed can be optimized by refactoring this to a pre-generate fixture. +func NewMorseStateExportAndAccountStateBytes( + t gocuke.TestingT, + numAccounts int, +) (morseStateExportBz []byte, morseAccountStateBz []byte) { + morseStateExport, morseAccountState := NewMorseStateExportAndAccountState(t, numAccounts) + + var err error + morseStateExportBz, err = cmtjson.Marshal(morseStateExport) + require.NoError(t, err) + + morseAccountStateBz, err = cmtjson.Marshal(morseAccountState) + require.NoError(t, err) + + return morseStateExportBz, morseAccountStateBz +} + +// TODO_IN_THIS_COMMIT: godoc... +func NewMorseStateExportAndAccountState( + t gocuke.TestingT, numAccounts int, +) (export *migrationtypes.MorseStateExport, state *migrationtypes.MorseAccountState) { + t.Helper() + + morseStateExport := &migrationtypes.MorseStateExport{ + AppHash: "", + AppState: &migrationtypes.MorseAppState{ + Application: &migrationtypes.MorseApplications{}, + Auth: &migrationtypes.MorseAuth{}, + Pos: &migrationtypes.MorsePos{}, + }, + } + + morseAccountState := &migrationtypes.MorseAccountState{ + Accounts: make([]*migrationtypes.MorseAccount, numAccounts), + } + + for i := 1; i < numAccounts+1; i++ { + seedUint := rand.Uint64() + seedBz := make([]byte, 8) + binary.LittleEndian.PutUint64(seedBz, seedUint) + privKey := cometcrypto.GenPrivKeyFromSecret(seedBz) + pubKey := privKey.PubKey() + balanceAmount := int64(1e6*i + i) // i_000_00i + appStakeAmount := int64(1e5*i + (i * 10)) // i00_0i0 + supplierStakeAmount := int64(1e4*i + (i * 100)) // i0_i00 + sumAmount := balanceAmount + appStakeAmount + supplierStakeAmount // i_ii0_iii + + // Add an account. + morseStateExport.AppState.Auth.Accounts = append( + morseStateExport.AppState.Auth.Accounts, + &migrationtypes.MorseAuthAccount{ + Type: "posmint/Account", + Value: &migrationtypes.MorseAccount{ + Address: pubKey.Address(), + Coins: cosmostypes.NewCoins(cosmostypes.NewInt64Coin(volatile.DenomuPOKT, balanceAmount)), + PubKey: &migrationtypes.MorsePublicKey{ + Value: pubKey.Bytes(), + }, + }, + }, + ) + + // Add an application. + morseStateExport.AppState.Application.Applications = append( + morseStateExport.AppState.Application.Applications, + &migrationtypes.MorseApplication{ + Address: pubKey.Address(), + PublicKey: pubKey.Bytes(), + Jailed: false, + Status: 2, + StakedTokens: fmt.Sprintf("%d", appStakeAmount), + }, + ) + + // Add a supplier. + morseStateExport.AppState.Pos.Validators = append( + morseStateExport.AppState.Pos.Validators, + &migrationtypes.MorseValidator{ + Address: pubKey.Address(), + PublicKey: pubKey.Bytes(), + Jailed: false, + Status: 2, + StakedTokens: fmt.Sprintf("%d", supplierStakeAmount), + }, + ) + + // Add the account to the morseAccountState. + morseAccountState.Accounts[i-1] = &migrationtypes.MorseAccount{ + Address: pubKey.Address(), + Coins: cosmostypes.NewCoins(cosmostypes.NewInt64Coin(volatile.DenomuPOKT, sumAmount)), + PubKey: &migrationtypes.MorsePublicKey{ + Value: pubKey.Bytes(), + }, + } + } + + return morseStateExport, morseAccountState +}