From 8f3e0b7f0b732bca62a5bbbbd3fcae0e2dd520e7 Mon Sep 17 00:00:00 2001 From: Nina Barbakadze Date: Fri, 5 Jul 2024 13:06:13 +0200 Subject: [PATCH] test(v1.x): expand apphash test with all state machine msgs --- app/consistent_apphash_test.go | 26 ++++++++--- test/util/common.go | 28 ++++++++++++ test/util/genesis/modifier.go | 11 +++++ test/util/test_app.go | 79 +++++++++++++++++---------------- test/util/testfactory/common.go | 28 ++++++++++++ 5 files changed, 127 insertions(+), 45 deletions(-) diff --git a/app/consistent_apphash_test.go b/app/consistent_apphash_test.go index b7dca2420b..754ce143fc 100644 --- a/app/consistent_apphash_test.go +++ b/app/consistent_apphash_test.go @@ -35,11 +35,12 @@ import ( abci "github.com/tendermint/tendermint/abci/types" tmproto "github.com/tendermint/tendermint/proto/tendermint/types" "github.com/tendermint/tendermint/proto/tendermint/version" + coretypes "github.com/tendermint/tendermint/types" ) type BlobTx struct { author string - blobs []*blob.Blob + blobs []*tmproto.Blob txOptions []user.TxOption } @@ -74,7 +75,7 @@ func TestConsistentAppHash(t *testing.T) { accounts := createAccounts(accountInfos, accountNames) // Create a signer with accounts - signer, err := user.NewSigner(kr, enc.TxConfig, testutil.ChainID, app.DefaultInitialVersion, accounts...) + signer, err := user.NewTxSigner(kr, enc.TxConfig, testutil.ChainID, appconsts.LatestVersion, accounts...) require.NoError(t, err) // ------------ Genesis Validator Accounts ------------ @@ -92,7 +93,7 @@ func TestConsistentAppHash(t *testing.T) { valAccounts := createAccounts(valAccountInfos, valAccountNames) // Create a signer with validator accounts - valSigner, err := user.NewSigner(valKeyRing, enc.TxConfig, testutil.ChainID, app.DefaultInitialVersion, valAccounts...) + valSigner, err := user.NewTxSigner(valKeyRing, enc.TxConfig, testutil.ChainID, appconsts.LatestVersion, valAccounts...) require.NoError(t, err) // ----------- Create SDK Messages ------------ @@ -271,7 +272,7 @@ func TestConsistentAppHash(t *testing.T) { // Create a Blob Tx blobTx := BlobTx{ author: accountNames[1], - blobs: []*blob.Blob{blob.New(fixedNamespace(), []byte{1}, appconsts.DefaultShareVersion)}, + blobs: []*tmproto.Blob{New(fixedNamespace(), []byte{1}, appconsts.DefaultShareVersion)}, txOptions: blobfactory.DefaultTxOpts(), } rawBlobTx, _, err := signer.CreatePayForBlobs(blobTx.author, blobTx.blobs, blobTx.txOptions...) @@ -290,10 +291,11 @@ func TestConsistentAppHash(t *testing.T) { require.NoError(t, err) // Execute the final block and get the data hash alongside the final app hash - _, finalAppHash, err := executeTxs(testApp, []byte{}, validatorRawTxs, abciValidators, secondAppHash) + dataHash, finalAppHash, err := executeTxs(testApp, []byte{}, validatorRawTxs, abciValidators, secondAppHash) require.NoError(t, err) fmt.Println(finalAppHash, "APP HASH") + fmt.Println(dataHash, "DATA HASH") // Require that the app hash is equal to the app hash produced on a different commit // require.Equal(t, expectedAppHash, appHash) @@ -340,7 +342,7 @@ func deterministicKeyRing(cdc codec.Codec) (keyring.Keyring, []types.PubKey) { // processSdkMessages takes a list of sdk messages, forms transactions, signs them // and returns a list of raw transactions -func processSdkMessages(signer *user.Signer, sdkMessages []sdk.Msg) ([][]byte, error) { +func processSdkMessages(signer *user.TxSigner, sdkMessages []sdk.Msg) ([][]byte, error) { rawSdkTxs := make([][]byte, 0, len(sdkMessages)) for _, msg := range sdkMessages { rawSdkTx, err := signer.CreateTx([]sdk.Msg{msg}, blobfactory.DefaultTxOpts()...) @@ -429,7 +431,7 @@ func executeTxs(testApp *app.App, rawBlobTx []byte, rawSdkTxs [][]byte, validato // Deliver Blob Txs if len(rawBlobTx) != 0 { // Deliver Blob Tx - blob, isBlobTx := blob.UnmarshalBlobTx(rawBlobTx) + blob, isBlobTx := coretypes.UnmarshalBlobTx(rawBlobTx) if !isBlobTx { return nil, nil, fmt.Errorf("Not a valid BlobTx") } @@ -502,3 +504,13 @@ func convertToABCIValidators(genValidators []stakingtypes.Validator) ([]abci.Val } return abciValidators, nil } + +// New creates a new tmproto.Blob from the provided data +func New(ns appns.Namespace, blob []byte, shareVersion uint8) *tmproto.Blob { + return &tmproto.Blob{ + NamespaceId: ns.ID, + Data: blob, + ShareVersion: uint32(shareVersion), + NamespaceVersion: uint32(ns.Version), + } +} diff --git a/test/util/common.go b/test/util/common.go index fcd657744d..cfd347f7fc 100644 --- a/test/util/common.go +++ b/test/util/common.go @@ -39,6 +39,7 @@ import ( stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types" gethcommon "github.com/ethereum/go-ethereum/common" "github.com/stretchr/testify/require" + tmed "github.com/tendermint/tendermint/crypto/ed25519" "github.com/tendermint/tendermint/libs/log" tmproto "github.com/tendermint/tendermint/proto/tendermint/types" tmversion "github.com/tendermint/tendermint/proto/tendermint/version" @@ -58,6 +59,33 @@ var ( MinCommissionRate: sdk.NewDecWithPrec(0, 0), } + // HardcodedConsensusPrivKeys + FixedConsensusPrivKeys = []tmed.PrivKey{ + tmed.GenPrivKeyFromSecret([]byte("12345678901234567890123456389012")), + tmed.GenPrivKeyFromSecret([]byte("12345678901234567890123456389013")), + tmed.GenPrivKeyFromSecret([]byte("12345678901234567890123456389014")), + tmed.GenPrivKeyFromSecret([]byte("12345678901234567890123456389015")), + tmed.GenPrivKeyFromSecret([]byte("12345678901234567890123456389016")), + } + + FixedNetWorkPrivKeys = []tmed.PrivKey{ + tmed.GenPrivKeyFromSecret([]byte("12345678901234567890123456786012")), + tmed.GenPrivKeyFromSecret([]byte("12345678901234567890123456786013")), + tmed.GenPrivKeyFromSecret([]byte("12345678901234567890123456786014")), + tmed.GenPrivKeyFromSecret([]byte("12345678901234567890123456786015")), + tmed.GenPrivKeyFromSecret([]byte("12345678901234567890123456786016")), + } + + // FixedMnemonics is a set of fixed mnemonics for testing. + // Account names are: validator1, validator2, validator3, validator4, validator5 + FixedMnemonics = []string{ + "body world north giggle crop reduce height copper damp next verify orphan lens loan adjust inform utility theory now ranch motion opinion crowd fun", + "body champion street fat bone above office guess waste vivid gift around approve elevator depth fiber alarm usual skirt like organ space antique silk", + "cheap alpha render punch clap prize duty drive steel situate person radar smooth elegant over chronic wait danger thumb soft letter spatial acquire rough", + "outdoor ramp suspect office disagree world attend vanish small wish capable fall wall soon damp session emotion chest toss viable meat host clerk truth", + "ability evidence casino cram weasel chest brush bridge sister blur onion found glad own mansion amateur expect force fun dragon famous alien appear open", + } + // ConsPrivKeys generate ed25519 ConsPrivKeys to be used for validator operator keys ConsPrivKeys = []ccrypto.PrivKey{ ed25519.GenPrivKey(), diff --git a/test/util/genesis/modifier.go b/test/util/genesis/modifier.go index 9ffe274d11..9effe2859a 100644 --- a/test/util/genesis/modifier.go +++ b/test/util/genesis/modifier.go @@ -13,6 +13,7 @@ import ( banktypes "github.com/cosmos/cosmos-sdk/x/bank/types" govtypes "github.com/cosmos/cosmos-sdk/x/gov/types" v1 "github.com/cosmos/cosmos-sdk/x/gov/types/v1" + slashingtypes "github.com/cosmos/cosmos-sdk/x/slashing/types" ) // Modifier allows for arbitrary changes to be made on the genesis state @@ -30,6 +31,16 @@ func SetBlobParams(codec codec.Codec, params blobtypes.Params) Modifier { } } +// SetSlashingParams will set the provided slashing params as genesis state. +func SetSlashingParams(codec codec.Codec, parans slashingtypes.Params) Modifier { + return func(state map[string]json.RawMessage) map[string]json.RawMessage { + slashingGenState := slashingtypes.DefaultGenesisState() + slashingGenState.Params = parans + state[slashingtypes.ModuleName] = codec.MustMarshalJSON(slashingGenState) + return state + } +} + // ImmediateProposals sets the thresholds for getting a gov proposal to very low // levels. func ImmediateProposals(codec codec.Codec) Modifier { diff --git a/test/util/test_app.go b/test/util/test_app.go index 7371d43e5e..afc8ff8820 100644 --- a/test/util/test_app.go +++ b/test/util/test_app.go @@ -31,8 +31,8 @@ import ( "github.com/celestiaorg/celestia-app/test/util/genesis" "github.com/cosmos/cosmos-sdk/crypto/hd" cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types" + slashingtypes "github.com/cosmos/cosmos-sdk/x/slashing/types" stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types" - "github.com/tendermint/tendermint/crypto/ed25519" tmversion "github.com/tendermint/tendermint/proto/tendermint/version" ) @@ -40,6 +40,8 @@ const ( ChainID = "test-app" ) +var GenesisTime = time.Date(2023, 1, 1, 1, 1, 1, 1, time.UTC).UTC() + // Get flags every time the simulator is run func init() { simapp.GetSimulatorFlags() @@ -75,11 +77,14 @@ func NewTestApp() *app.App { // SetupDeterministicGenesisState sets genesis on initialized testApp with the provided arguments. func SetupDeterministicGenesisState(testApp *app.App, pubKeys []cryptotypes.PubKey, balance int64, cparams *tmproto.ConsensusParams) (keyring.Keyring, []genesis.Account, error) { + slashingParams := slashingtypes.NewParams(2, sdk.OneDec(), time.Minute, sdk.OneDec(), sdk.OneDec()) + // Create genesis gen := genesis.NewDefaultGenesis(). WithChainID(ChainID). WithConsensusParams(cparams). - WithGenesisTime(time.Date(2023, 1, 1, 1, 1, 1, 1, time.UTC).UTC()) + WithModifiers(genesis.SetSlashingParams(testApp.AppCodec(), slashingParams)). + WithGenesisTime(GenesisTime) // Add accounts to genesis for _, pk := range pubKeys { @@ -93,7 +98,7 @@ func SetupDeterministicGenesisState(testApp *app.App, pubKeys []cryptotypes.PubK } // Add validator to genesis - err := AddDeterministicValidatorToGenesis(gen) + err := AddDeterministicValidatorsToGenesis(gen) if err != nil { return nil, nil, fmt.Errorf("failed to add validator: %w", err) } @@ -210,46 +215,44 @@ func SetupTestAppWithGenesisValSet(cparams *tmproto.ConsensusParams, genAccounts return testApp, kr } -// AddDeterministicValidatorToGenesis adds a single deterministic validator to the genesis. -func AddDeterministicValidatorToGenesis(g *genesis.Genesis) error { - // Hardcoded keys for deterministic account creation - mnemo := "body world north giggle crop reduce height copper damp next verify orphan lens loan adjust inform utility theory now ranch motion opinion crowd fun" - consensusKey := ed25519.GenPrivKeyFromSecret([]byte("12345678901234567890123456389012")) - networkKey := ed25519.GenPrivKeyFromSecret([]byte("12345678901234567890123456786012")) - - val := genesis.Validator{ - KeyringAccount: genesis.KeyringAccount{ - Name: "validator1", - InitialTokens: 1_000_000_000, - }, - Stake: 1_000_000, - ConsensusKey: consensusKey, - NetworkKey: networkKey, - } - - // Initialize the validator's genesis account in the keyring - rec, err := g.Keyring().NewAccount(val.Name, mnemo, "", "", hd.Secp256k1) - if err != nil { - return fmt.Errorf("failed to create account: %w", err) - } +func AddDeterministicValidatorsToGenesis(g *genesis.Genesis) error { + for i := range FixedMnemonics { + val := genesis.Validator{ + KeyringAccount: genesis.KeyringAccount{ + Name: "validator" + fmt.Sprint(i), + InitialTokens: 5_000_000_000, + }, + Stake: 1_000_000_000, + ConsensusKey: FixedConsensusPrivKeys[i], + NetworkKey: FixedNetWorkPrivKeys[i], + } + // initialize the validator's genesis account in the keyring + rec, err := g.Keyring().NewAccount(val.Name, FixedMnemonics[i], "", "", hd.Secp256k1) + if err != nil { + return fmt.Errorf("failed to create account: %w", err) + } - validatorPubKey, err := rec.GetPubKey() - if err != nil { - return fmt.Errorf("failed to get pubkey: %w", err) - } + validatorPubKey, err := rec.GetPubKey() + if err != nil { + return fmt.Errorf("failed to get pubkey: %w", err) + } - // Make account from keyring account - account := genesis.Account{ - PubKey: validatorPubKey, - Balance: val.KeyringAccount.InitialTokens, - } + // make account from keyring account + account := genesis.Account{ + PubKey: validatorPubKey, + Balance: val.KeyringAccount.InitialTokens, + } - // Add the validator's account to the genesis - if err := g.AddAccount(account); err != nil { - return fmt.Errorf("failed to add account: %w", err) + // add the validator's account to the genesis + if err := g.AddAccount(account); err != nil { + return fmt.Errorf("failed to add account: %w", err) + } + if err := g.AddValidator(val); err != nil { + return fmt.Errorf("failed to add validator: %w", err) + } } - return g.AddValidator(val) + return nil } // AddGenesisAccount mimics the cli addGenesisAccount command, providing an diff --git a/test/util/testfactory/common.go b/test/util/testfactory/common.go index bd6c77f309..93a953878d 100644 --- a/test/util/testfactory/common.go +++ b/test/util/testfactory/common.go @@ -62,3 +62,31 @@ func GetAddress(keys keyring.Keyring, account string) sdk.AccAddress { } return addr } + +func GetAddresses(keys keyring.Keyring) []sdk.AccAddress { + recs, err := keys.List() + if err != nil { + panic(err) + } + addresses := make([]sdk.AccAddress, 0, len(recs)) + for _, rec := range recs { + address, err := rec.GetAddress() + if err != nil { + panic(err) + } + addresses = append(addresses, address) + } + return addresses +} + +func GetAccountNames(keys keyring.Keyring) []string { + recs, err := keys.List() + if err != nil { + panic(err) + } + names := make([]string, 0, len(recs)) + for _, rec := range recs { + names = append(names, rec.Name) + } + return names +}