Skip to content

Commit

Permalink
refactor: fixture generator testutil
Browse files Browse the repository at this point in the history
  • Loading branch information
bryanchriswhite committed Jan 29, 2025
1 parent cf4a0c5 commit 11ee69b
Show file tree
Hide file tree
Showing 2 changed files with 120 additions and 97 deletions.
101 changes: 4 additions & 97 deletions cmd/poktrolld/cmd/migrate/migrate_test.go
Original file line number Diff line number Diff line change
@@ -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"
)

Expand All @@ -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)

Expand Down Expand Up @@ -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)

Expand All @@ -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)

Expand All @@ -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
}
116 changes: 116 additions & 0 deletions testutil/testmigration/fixtures.go
Original file line number Diff line number Diff line change
@@ -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
}

0 comments on commit 11ee69b

Please sign in to comment.