-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: fixture generator testutil
- Loading branch information
1 parent
cf4a0c5
commit 11ee69b
Showing
2 changed files
with
120 additions
and
97 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,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 | ||
} |