From 91194daffe0d66fa8728f8a36e045f59139c4af3 Mon Sep 17 00:00:00 2001 From: Nina Barbakadze Date: Wed, 29 May 2024 17:58:04 +0200 Subject: [PATCH 01/19] test: non determinism in different app versions --- app/test/non_determinism_test.go | 143 +++++++++++++++++++++++++++++++ 1 file changed, 143 insertions(+) create mode 100644 app/test/non_determinism_test.go diff --git a/app/test/non_determinism_test.go b/app/test/non_determinism_test.go new file mode 100644 index 0000000000..390947bff9 --- /dev/null +++ b/app/test/non_determinism_test.go @@ -0,0 +1,143 @@ +package app_test + +import ( + "fmt" + // "os" + "testing" + + "github.com/celestiaorg/celestia-app/v2/app" + "github.com/celestiaorg/celestia-app/v2/app/encoding" + + "github.com/celestiaorg/celestia-app/v2/pkg/appconsts" + // "github.com/celestiaorg/celestia-app/v2/pkg/user" + testutil "github.com/celestiaorg/celestia-app/v2/test/util" + "github.com/celestiaorg/celestia-app/v2/test/util/blobfactory" + "github.com/celestiaorg/celestia-app/v2/test/util/testfactory" + // "github.com/celestiaorg/celestia-app/v2/test/util/testnode" + + // blobtypes "github.com/celestiaorg/celestia-app/v2/x/blob/types" + "github.com/celestiaorg/go-square/blob" + // "github.com/celestiaorg/go-square/namespace" + "github.com/cosmos/cosmos-sdk/codec" + hd "github.com/cosmos/cosmos-sdk/crypto/hd" + + abci "github.com/tendermint/tendermint/abci/types" + + "strconv" + + appns "github.com/celestiaorg/go-square/namespace" + keyring "github.com/cosmos/cosmos-sdk/crypto/keyring" + sdk "github.com/cosmos/cosmos-sdk/types" + + "github.com/stretchr/testify/require" + // tmproto "github.com/tendermint/tendermint/proto/tendermint/types" +) + +func TestNonDeterminismBetweenAppVersions(t *testing.T) { + // set up testapp with genesis state + const ( + numBlobTxs, numNormalTxs = 5, 5 + account = "test" + ) + accounts := deterministicAccounts(numBlobTxs + numNormalTxs) + + testApp, _ := testutil.SetupTestAppWithGenesisValSet(app.DefaultConsensusParams(), "") + // ctx := testApp.NewContext(true, tmproto.Header{}) + enc := encoding.MakeConfig(app.ModuleEncodingRegisters...) + accounts = append(accounts, account) + kr, _ := DeterministicKeyRing(enc.Codec, accounts...) + // addr := testfactory.GetAddress(kr, account) + // acc := testutil.DirectQueryAccount(testApp, addr) + // signer, err := user.NewSigner(kr, enc.TxConfig, testutil.ChainID, appconsts.LatestVersion, user.NewAccount(account, acc.GetAccountNumber(), acc.GetSequence())) + // require.NoError(t, err) + accinfos := queryAccountInfo(testApp, accounts, kr) + fmt.Println(kr, "kr") + + + // create deterministic set of 10 transactions + normalTxs := testutil.SendTxsWithAccounts( + t, + testApp, + enc.TxConfig, + kr, + 1000, + account, + accounts[:numNormalTxs], + testutil.ChainID, + ) + + // maybe change this to signer.CreatePFBS + blobTxs := blobfactory.ManyMultiBlobTx(t, enc.TxConfig, kr, testutil.ChainID, accounts[numBlobTxs:], accinfos[numBlobTxs:], testfactory.Repeat([]*blob.Blob{ + blob.New(HardcodedNamespace(), []byte{1}, appconsts.DefaultShareVersion), + }, numBlobTxs)) + // normal sdk tx + + // deliver normal txs + for _, tx := range normalTxs { + resp := testApp.DeliverTx(abci.RequestDeliverTx{Tx: tx}) + require.EqualValues(t, 0, resp.Code, resp.Log) + } + + // deliver blob txs + for _, tx := range blobTxs { + blobTx, ok := blob.UnmarshalBlobTx(tx) + require.True(t, ok) + resp := testApp.DeliverTx(abci.RequestDeliverTx{Tx: blobTx.Tx}) + require.EqualValues(t, 0, resp.Code, resp.Log) + } + + // Commit the state + testApp.Commit() + + // // Get the app hash + appHash := testApp.LastCommitID().Hash + + fmt.Println("AppHash:", appHash) +} + +func deterministicAccounts(numAccounts uint64) []string { + const charset = "abcdefghijklmnopqrs" + accounts := make([]string, numAccounts) + for i := range accounts { + accounts[i] = charset + strconv.Itoa(i) + } + return accounts +} + +func HardcodedNamespace() appns.Namespace { + return appns.Namespace{ + Version: 0, + ID: []byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 37, 67, 154, 200, 228, 130, 74, 147, 162, 11}, + } +} + +func DeterministicKeyRing(cdc codec.Codec, accounts ...string) (keyring.Keyring, []sdk.AccAddress) { + mnemonics := []string{ + "great myself congress genuine scale muscle view uncover pipe miracle sausage broccoli lonely swap table foam brand turtle comic gorilla firm mad grunt hazard", + "cheap job month trigger flush cactus chest juice dolphin people limit crunch curious secret object beach shield snake hunt group sketch cousin puppy fox", + "oil suffer bamboo one better attack exist dolphin relief enforce cat asset raccoon lava regret found love certain plunge grocery accuse goat together kiss", + "giraffe busy subject doll jump drama sea daring again club spend toe mind organ real liar permit refuse change opinion donkey job cricket speed", + "fee vapor thing fish fan memory negative raven cram win quantum ozone job mirror shoot sting quiz black apart funny sort cancel friend curtain", + "skin beef review pilot tooth act any alarm there only kick uniform ticket material cereal radar ethics list unlock method coral smooth street frequent", + "ecology scout core guard load oil school effort near alcohol fancy save cereal owner enforce impact sand husband trophy solve amount fish festival sell", + "used describe angle twin amateur pyramid bitter pool fluid wing erode rival wife federal curious drink battle put elbow mandate another token reveal tone", + "reason fork target chimney lift typical fine divorce mixture web robot kiwi traffic stove miss crane welcome camp bless fuel october riot pluck ordinary", + "undo logic mobile modify master force donor rose crumble forget plate job canal waste turn damp sure point deposit hazard quantum car annual churn", + "charge subway treat loop donate place loan want grief leg message siren joy road exclude match empty enforce vote meadow enlist vintage wool involve", + } + kb := keyring.NewInMemory(cdc) + addresses := make([]sdk.AccAddress, len(accounts)) + for idx, acc := range accounts { + rec, err := kb.NewAccount(acc, mnemonics[idx], "", "", hd.Secp256k1) + if err != nil { + panic(err) + } + addr, err := rec.GetAddress() + if err != nil { + panic(err) + } + addresses[idx] = addr + + } + return kb, nil +} From 3d65302f0ea7cafb1bdbe7ab33d4a6e220a6ce42 Mon Sep 17 00:00:00 2001 From: Nina Barbakadze Date: Wed, 29 May 2024 18:13:07 +0200 Subject: [PATCH 02/19] test: debugging --- test/util/test_app.go | 5 +++-- test/util/testnode/utils.go | 3 +++ 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/test/util/test_app.go b/test/util/test_app.go index 765b53d326..65ca31b476 100644 --- a/test/util/test_app.go +++ b/test/util/test_app.go @@ -69,7 +69,8 @@ func SetupTestAppWithGenesisValSet(cparams *tmproto.ConsensusParams, genAccounts App: cparams.Version.AppVersion, }, }}) - + fmt.Print(testApp.LastCommitID().Hash, "LAST COMMIT ID HASH") + return testApp, kr } @@ -212,7 +213,7 @@ func GenesisStateWithSingleValidator(testApp *app.App, genAccounts ...string) (a Address: acc.GetAddress().String(), Coins: sdk.NewCoins(sdk.NewCoin(app.BondDenom, sdk.NewInt(100000000000000))), }) - + fmt.Println(genAccounts, "GEN ACCS GETTING FUNDED") kr, fundedBankAccs, fundedAuthAccs := testnode.FundKeyringAccounts(genAccounts...) accs = append(accs, fundedAuthAccs...) balances = append(balances, fundedBankAccs...) diff --git a/test/util/testnode/utils.go b/test/util/testnode/utils.go index bcdabcfa55..0d9664b918 100644 --- a/test/util/testnode/utils.go +++ b/test/util/testnode/utils.go @@ -3,6 +3,7 @@ package testnode import ( "context" "encoding/hex" + "fmt" "github.com/celestiaorg/celestia-app/v2/app" "github.com/celestiaorg/celestia-app/v2/app/encoding" @@ -47,6 +48,7 @@ func NewKeyring(accounts ...string) (keyring.Keyring, []sdk.AccAddress) { addresses := make([]sdk.AccAddress, len(accounts)) for idx, acc := range accounts { rec, _, err := kb.NewMnemonic(acc, keyring.English, "", "", hd.Secp256k1) + fmt.Println(rec.Name, "rec") if err != nil { panic(err) } @@ -55,6 +57,7 @@ func NewKeyring(accounts ...string) (keyring.Keyring, []sdk.AccAddress) { panic(err) } addresses[idx] = addr + fmt.Println("Address", addr) } return kb, addresses } From 65d77c99802ab31c6862e65c69cf70fb77910888 Mon Sep 17 00:00:00 2001 From: Nina Barbakadze Date: Wed, 29 May 2024 21:05:38 +0200 Subject: [PATCH 03/19] test: trying to find nondeterminism in testapp --- test/util/test_app.go | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/test/util/test_app.go b/test/util/test_app.go index 65ca31b476..1e9bc27255 100644 --- a/test/util/test_app.go +++ b/test/util/test_app.go @@ -1,6 +1,7 @@ package util import ( + // "bytes" "encoding/json" "fmt" "testing" @@ -20,7 +21,13 @@ import ( "github.com/cosmos/cosmos-sdk/crypto/keys/secp256k1" "github.com/cosmos/cosmos-sdk/server" "github.com/cosmos/cosmos-sdk/simapp" - "github.com/cosmos/cosmos-sdk/testutil/mock" + // "github.com/cosmos/cosmos-sdk/testutil/mock" + "github.com/tendermint/tendermint/crypto/ed25519" + + // cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types" + // "crypto/sha256" + // "crypto/rand" + // tmd25519 "github.com/cometbft/cometbft/crypto/ed25519" sdk "github.com/cosmos/cosmos-sdk/types" authtypes "github.com/cosmos/cosmos-sdk/x/auth/types" banktypes "github.com/cosmos/cosmos-sdk/x/bank/types" @@ -69,8 +76,8 @@ func SetupTestAppWithGenesisValSet(cparams *tmproto.ConsensusParams, genAccounts App: cparams.Version.AppVersion, }, }}) - fmt.Print(testApp.LastCommitID().Hash, "LAST COMMIT ID HASH") - + fmt.Print(testApp.LastCommitID().Hash, "LAST COMMIT ID HASH") + return testApp, kr } @@ -193,11 +200,8 @@ func AddAccount(addr sdk.AccAddress, appState app.GenesisState, cdc codec.Codec) // GenesisStateWithSingleValidator initializes GenesisState with a single // validator and genesis accounts that also act as delegators. func GenesisStateWithSingleValidator(testApp *app.App, genAccounts ...string) (app.GenesisState, *tmtypes.ValidatorSet, keyring.Keyring) { - privVal := mock.NewPV() - pubKey, err := privVal.GetPubKey() - if err != nil { - panic(err) - } + bytes := []byte{47, 251, 209, 243, 116, 10, 95, 100, 214, 115, 6, 62, 205, 57, 238, 123, 10, 17, 195, 84, 137, 80, 30, 23, 95, 208, 146, 135, 103, 211, 125, 83} + pubKey := ed25519.PubKey(bytes) // create validator set with single validator validator := tmtypes.NewValidator(pubKey, 1) @@ -213,7 +217,7 @@ func GenesisStateWithSingleValidator(testApp *app.App, genAccounts ...string) (a Address: acc.GetAddress().String(), Coins: sdk.NewCoins(sdk.NewCoin(app.BondDenom, sdk.NewInt(100000000000000))), }) - fmt.Println(genAccounts, "GEN ACCS GETTING FUNDED") + fmt.Println(genAccounts, "GEN ACCS GETTING FUNDED") kr, fundedBankAccs, fundedAuthAccs := testnode.FundKeyringAccounts(genAccounts...) accs = append(accs, fundedAuthAccs...) balances = append(balances, fundedBankAccs...) From a3ca919eedb9e3a6b1c2707a81d6578c48acbf8e Mon Sep 17 00:00:00 2001 From: Nina Barbakadze Date: Thu, 30 May 2024 14:34:01 +0200 Subject: [PATCH 04/19] test: fix nondeterminism in testapp --- app/app.go | 1 + app/test/non_determinism_test.go | 4 ++-- test/util/test_app.go | 29 ++++++++++++++--------------- test/util/testnode/utils.go | 12 +++++++----- 4 files changed, 24 insertions(+), 22 deletions(-) diff --git a/app/app.go b/app/app.go index c6e4f15373..1c3449d0cb 100644 --- a/app/app.go +++ b/app/app.go @@ -552,6 +552,7 @@ func (app *App) InitChain(req abci.RequestInitChain) (res abci.ResponseInitChain } res = app.BaseApp.InitChain(req) + fmt.Println(res, "RESPONSE INIT CHAIN") ctx := app.NewContext(false, tmproto.Header{}) if appVersion != v1 { diff --git a/app/test/non_determinism_test.go b/app/test/non_determinism_test.go index 390947bff9..6257a28e60 100644 --- a/app/test/non_determinism_test.go +++ b/app/test/non_determinism_test.go @@ -40,8 +40,8 @@ func TestNonDeterminismBetweenAppVersions(t *testing.T) { account = "test" ) accounts := deterministicAccounts(numBlobTxs + numNormalTxs) - - testApp, _ := testutil.SetupTestAppWithGenesisValSet(app.DefaultConsensusParams(), "") + + testApp, _ := testutil.SetupTestAppWithGenesisValSet(app.DefaultConsensusParams()) // ctx := testApp.NewContext(true, tmproto.Header{}) enc := encoding.MakeConfig(app.ModuleEncodingRegisters...) accounts = append(accounts, account) diff --git a/test/util/test_app.go b/test/util/test_app.go index 1e9bc27255..dde79728e9 100644 --- a/test/util/test_app.go +++ b/test/util/test_app.go @@ -21,13 +21,7 @@ import ( "github.com/cosmos/cosmos-sdk/crypto/keys/secp256k1" "github.com/cosmos/cosmos-sdk/server" "github.com/cosmos/cosmos-sdk/simapp" - // "github.com/cosmos/cosmos-sdk/testutil/mock" "github.com/tendermint/tendermint/crypto/ed25519" - - // cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types" - // "crypto/sha256" - // "crypto/rand" - // tmd25519 "github.com/cometbft/cometbft/crypto/ed25519" sdk "github.com/cosmos/cosmos-sdk/types" authtypes "github.com/cosmos/cosmos-sdk/x/auth/types" banktypes "github.com/cosmos/cosmos-sdk/x/bank/types" @@ -200,25 +194,30 @@ func AddAccount(addr sdk.AccAddress, appState app.GenesisState, cdc codec.Codec) // GenesisStateWithSingleValidator initializes GenesisState with a single // validator and genesis accounts that also act as delegators. func GenesisStateWithSingleValidator(testApp *app.App, genAccounts ...string) (app.GenesisState, *tmtypes.ValidatorSet, keyring.Keyring) { - bytes := []byte{47, 251, 209, 243, 116, 10, 95, 100, 214, 115, 6, 62, 205, 57, 238, 123, 10, 17, 195, 84, 137, 80, 30, 23, 95, 208, 146, 135, 103, 211, 125, 83} - pubKey := ed25519.PubKey(bytes) - // create validator set with single validator - validator := tmtypes.NewValidator(pubKey, 1) + validatorPubKey := ed25519.PubKey([]byte("12345678901234567890123456789012")) + validator := tmtypes.NewValidator(validatorPubKey, 1) valSet := tmtypes.NewValidatorSet([]*tmtypes.Validator{validator}) - // generate genesis account - senderPrivKey := secp256k1.GenPrivKey() - accs := make([]authtypes.GenesisAccount, 0, len(genAccounts)+1) + // generate sender account + senderPrivKey := secp256k1.GenPrivKeyFromSecret([]byte("09876543210987654321098765432109")) acc := authtypes.NewBaseAccount(senderPrivKey.PubKey().Address().Bytes(), senderPrivKey.PubKey(), 0, 0) + + // genesis and sender accounts will be added to this slice + accs := make([]authtypes.GenesisAccount, 0, len(genAccounts)+1) accs = append(accs, acc) + + // genesis accounts and sender account balances balances := make([]banktypes.Balance, 0, len(genAccounts)+1) balances = append(balances, banktypes.Balance{ Address: acc.GetAddress().String(), Coins: sdk.NewCoins(sdk.NewCoin(app.BondDenom, sdk.NewInt(100000000000000))), }) - fmt.Println(genAccounts, "GEN ACCS GETTING FUNDED") - kr, fundedBankAccs, fundedAuthAccs := testnode.FundKeyringAccounts(genAccounts...) + + // create a new keyring with the generated accounts + kr, addresses := testnode.NewKeyring(genAccounts...) + // fund the accounts + fundedBankAccs, fundedAuthAccs := testnode.FundKeyringAccounts(kr, addresses) accs = append(accs, fundedAuthAccs...) balances = append(balances, fundedBankAccs...) diff --git a/test/util/testnode/utils.go b/test/util/testnode/utils.go index 0d9664b918..780fb5a5e1 100644 --- a/test/util/testnode/utils.go +++ b/test/util/testnode/utils.go @@ -42,6 +42,8 @@ func QueryWithoutProof(clientCtx client.Context, hashHexStr string) (*rpctypes.R } func NewKeyring(accounts ...string) (keyring.Keyring, []sdk.AccAddress) { + fmt.Println(accounts, "accounts FRPM KEYRING") + fmt.Println(len(accounts), "ACCOUNTS LENGTH") cdc := encoding.MakeConfig(app.ModuleEncodingRegisters...).Codec kb := keyring.NewInMemory(cdc) @@ -68,10 +70,10 @@ func RandomAddress() sdk.Address { return addresses[0] } -func FundKeyringAccounts(accounts ...string) (keyring.Keyring, []banktypes.Balance, []authtypes.GenesisAccount) { - kr, addresses := NewKeyring(accounts...) - genAccounts := make([]authtypes.GenesisAccount, len(accounts)) - genBalances := make([]banktypes.Balance, len(accounts)) +func FundKeyringAccounts(kr keyring.Keyring, addresses []sdk.AccAddress) ([]banktypes.Balance, []authtypes.GenesisAccount) { + // kr, addresses := NewKeyring(accounts...) + genAccounts := make([]authtypes.GenesisAccount, len(addresses)) + genBalances := make([]banktypes.Balance, len(addresses)) for i, addr := range addresses { balances := sdk.NewCoins( @@ -81,7 +83,7 @@ func FundKeyringAccounts(accounts ...string) (keyring.Keyring, []banktypes.Balan genBalances[i] = banktypes.Balance{Address: addr.String(), Coins: balances.Sort()} genAccounts[i] = authtypes.NewBaseAccount(addr, nil, uint64(i), 0) } - return kr, genBalances, genAccounts + return genBalances, genAccounts } func GenerateAccounts(count int) []string { From 60861e3fac325392297ae09749b81426fcc7f644 Mon Sep 17 00:00:00 2001 From: Nina Barbakadze Date: Fri, 31 May 2024 19:25:20 +0200 Subject: [PATCH 05/19] test: add apply genesis to testapp and made it deterministic --- app/test/non_determinism_test.go | 84 ++++++++++---------------- app/test/prepare_proposal_test.go | 4 ++ test/util/genesis/genesis.go | 48 ++++++++++++++- test/util/test_app.go | 99 +++++++++++++++++++++++++++++-- test/util/testfactory/common.go | 1 + test/util/testnode/utils.go | 7 +-- 6 files changed, 180 insertions(+), 63 deletions(-) diff --git a/app/test/non_determinism_test.go b/app/test/non_determinism_test.go index 6257a28e60..f147b28bdc 100644 --- a/app/test/non_determinism_test.go +++ b/app/test/non_determinism_test.go @@ -2,57 +2,45 @@ package app_test import ( "fmt" - // "os" - "testing" - "github.com/celestiaorg/celestia-app/v2/app" "github.com/celestiaorg/celestia-app/v2/app/encoding" - "github.com/celestiaorg/celestia-app/v2/pkg/appconsts" - // "github.com/celestiaorg/celestia-app/v2/pkg/user" testutil "github.com/celestiaorg/celestia-app/v2/test/util" "github.com/celestiaorg/celestia-app/v2/test/util/blobfactory" "github.com/celestiaorg/celestia-app/v2/test/util/testfactory" - // "github.com/celestiaorg/celestia-app/v2/test/util/testnode" - - // blobtypes "github.com/celestiaorg/celestia-app/v2/x/blob/types" "github.com/celestiaorg/go-square/blob" - // "github.com/celestiaorg/go-square/namespace" + appns "github.com/celestiaorg/go-square/namespace" "github.com/cosmos/cosmos-sdk/codec" hd "github.com/cosmos/cosmos-sdk/crypto/hd" - - abci "github.com/tendermint/tendermint/abci/types" - - "strconv" - - appns "github.com/celestiaorg/go-square/namespace" keyring "github.com/cosmos/cosmos-sdk/crypto/keyring" - sdk "github.com/cosmos/cosmos-sdk/types" - + "github.com/cosmos/cosmos-sdk/crypto/types" "github.com/stretchr/testify/require" - // tmproto "github.com/tendermint/tendermint/proto/tendermint/types" + abci "github.com/tendermint/tendermint/abci/types" + "testing" ) func TestNonDeterminismBetweenAppVersions(t *testing.T) { // set up testapp with genesis state const ( numBlobTxs, numNormalTxs = 5, 5 - account = "test" ) - accounts := deterministicAccounts(numBlobTxs + numNormalTxs) - - testApp, _ := testutil.SetupTestAppWithGenesisValSet(app.DefaultConsensusParams()) - // ctx := testApp.NewContext(true, tmproto.Header{}) + + testApp := testutil.NewTestApp() + enc := encoding.MakeConfig(app.ModuleEncodingRegisters...) - accounts = append(accounts, account) - kr, _ := DeterministicKeyRing(enc.Codec, accounts...) - // addr := testfactory.GetAddress(kr, account) - // acc := testutil.DirectQueryAccount(testApp, addr) - // signer, err := user.NewSigner(kr, enc.TxConfig, testutil.ChainID, appconsts.LatestVersion, user.NewAccount(account, acc.GetAccountNumber(), acc.GetSequence())) - // require.NoError(t, err) - accinfos := queryAccountInfo(testApp, accounts, kr) - fmt.Println(kr, "kr") + kr, pubKeys := DeterministicKeyRing(enc.Codec) + + var addresses []string + krss, _ := kr.List() + // this is for getting names of accounts + for _, account := range krss { + addresses = append(addresses, account.Name) + } + + _, _, err := testutil.ApplyGenesisState(testApp, pubKeys, 1_000_000_000, app.DefaultConsensusParams()) + require.NoError(t, err) + accinfos := queryAccountInfo(testApp, addresses, kr) // create deterministic set of 10 transactions normalTxs := testutil.SendTxsWithAccounts( @@ -61,16 +49,18 @@ func TestNonDeterminismBetweenAppVersions(t *testing.T) { enc.TxConfig, kr, 1000, - account, - accounts[:numNormalTxs], + addresses[0], + addresses[:numNormalTxs], testutil.ChainID, ) + fmt.Println(len(accinfos[numBlobTxs:]), "ACCINFOS LENGTH") + fmt.Println(len(addresses[numBlobTxs:]), "ADDRESSES LENGTH") + // maybe change this to signer.CreatePFBS - blobTxs := blobfactory.ManyMultiBlobTx(t, enc.TxConfig, kr, testutil.ChainID, accounts[numBlobTxs:], accinfos[numBlobTxs:], testfactory.Repeat([]*blob.Blob{ + blobTxs := blobfactory.ManyMultiBlobTx(t, enc.TxConfig, kr, testutil.ChainID, addresses[numBlobTxs+1:], accinfos[numBlobTxs+1:], testfactory.Repeat([]*blob.Blob{ blob.New(HardcodedNamespace(), []byte{1}, appconsts.DefaultShareVersion), }, numBlobTxs)) - // normal sdk tx // deliver normal txs for _, tx := range normalTxs { @@ -95,15 +85,6 @@ func TestNonDeterminismBetweenAppVersions(t *testing.T) { fmt.Println("AppHash:", appHash) } -func deterministicAccounts(numAccounts uint64) []string { - const charset = "abcdefghijklmnopqrs" - accounts := make([]string, numAccounts) - for i := range accounts { - accounts[i] = charset + strconv.Itoa(i) - } - return accounts -} - func HardcodedNamespace() appns.Namespace { return appns.Namespace{ Version: 0, @@ -111,7 +92,7 @@ func HardcodedNamespace() appns.Namespace { } } -func DeterministicKeyRing(cdc codec.Codec, accounts ...string) (keyring.Keyring, []sdk.AccAddress) { +func DeterministicKeyRing(cdc codec.Codec) (keyring.Keyring, []types.PubKey) { mnemonics := []string{ "great myself congress genuine scale muscle view uncover pipe miracle sausage broccoli lonely swap table foam brand turtle comic gorilla firm mad grunt hazard", "cheap job month trigger flush cactus chest juice dolphin people limit crunch curious secret object beach shield snake hunt group sketch cousin puppy fox", @@ -126,18 +107,17 @@ func DeterministicKeyRing(cdc codec.Codec, accounts ...string) (keyring.Keyring, "charge subway treat loop donate place loan want grief leg message siren joy road exclude match empty enforce vote meadow enlist vintage wool involve", } kb := keyring.NewInMemory(cdc) - addresses := make([]sdk.AccAddress, len(accounts)) - for idx, acc := range accounts { - rec, err := kb.NewAccount(acc, mnemonics[idx], "", "", hd.Secp256k1) + pubKeys := make([]types.PubKey, len(mnemonics)) + for idx, mnemonic := range mnemonics { + rec, err := kb.NewAccount(fmt.Sprintf("account-%d", idx), mnemonic, "", "", hd.Secp256k1) if err != nil { panic(err) } - addr, err := rec.GetAddress() + pubKey, err := rec.GetPubKey() if err != nil { panic(err) } - addresses[idx] = addr - + pubKeys[idx] = pubKey } - return kb, nil + return kb, pubKeys } diff --git a/app/test/prepare_proposal_test.go b/app/test/prepare_proposal_test.go index 04b925e365..7147645b1c 100644 --- a/app/test/prepare_proposal_test.go +++ b/app/test/prepare_proposal_test.go @@ -1,6 +1,8 @@ package app_test import ( + // "fmt" + "fmt" "testing" "time" @@ -209,7 +211,9 @@ func queryAccountInfo(capp *app.App, accs []string, kr keyring.Keyring) []blobfa infos := make([]blobfactory.AccountInfo, len(accs)) for i, acc := range accs { addr := testfactory.GetAddress(kr, acc) + fmt.Println(addr, "ADDR") accI := testutil.DirectQueryAccount(capp, addr) + fmt.Println(accI, "ACC INFO") infos[i] = blobfactory.AccountInfo{ AccountNum: accI.GetAccountNumber(), Sequence: accI.GetSequence(), diff --git a/test/util/genesis/genesis.go b/test/util/genesis/genesis.go index 2292445136..2f55520e34 100644 --- a/test/util/genesis/genesis.go +++ b/test/util/genesis/genesis.go @@ -80,7 +80,7 @@ func (g *Genesis) WithGenesisTime(genesisTime time.Time) *Genesis { func (g *Genesis) WithValidators(vals ...Validator) *Genesis { for _, val := range vals { - err := g.AddValidator(val) + err := g.NewValidator(val) if err != nil { panic(err) } @@ -139,7 +139,7 @@ func (g *Genesis) NewAccount(acc KeyringAccount) error { return nil } -func (g *Genesis) AddValidator(val Validator) error { +func (g *Genesis) NewValidator(val Validator) error { if err := val.ValidateBasic(); err != nil { return err } @@ -161,6 +161,50 @@ func (g *Genesis) AddValidator(val Validator) error { return nil } +// TODO improve this function to imply that we're just adding one validator to make it deterministic +func (g *Genesis) AddValidator(val Validator) error { + 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" + rec, err := g.kr.NewAccount("validator1", mnemo, "", "", hd.Secp256k1) + if err != nil { + return err + } + validatorPubKey, err := rec.GetPubKey() + if err != nil { + return err + } + + if err := val.ValidateBasic(); err != nil { + return err + } + + // make account from keyring account + account := Account{ + PubKey: validatorPubKey, + Balance: val.KeyringAccount.InitialTokens, + } + + if err := g.AddAccount(account); err != nil { + return err + } + + // TODO decide on this + // add validator to genesis keyring + // if _, err := g.kr.Key(val.Name); err == nil { + // return fmt.Errorf("validator with name %s already exists", val.Name) + // } + + // // Add the validator's genesis transaction + gentx, err := val.GenTx(g.ecfg, g.kr, g.ChainID) + if err != nil { + return err + } + + // install the validator + g.genTxs = append(g.genTxs, gentx) + g.validators = append(g.validators, val) + return nil +} + func (g *Genesis) Accounts() []Account { return g.accounts } diff --git a/test/util/test_app.go b/test/util/test_app.go index dde79728e9..512ca74129 100644 --- a/test/util/test_app.go +++ b/test/util/test_app.go @@ -1,7 +1,6 @@ package util import ( - // "bytes" "encoding/json" "fmt" "testing" @@ -12,6 +11,7 @@ import ( "github.com/celestiaorg/celestia-app/v2/pkg/appconsts" v1 "github.com/celestiaorg/celestia-app/v2/pkg/appconsts/v1" v2 "github.com/celestiaorg/celestia-app/v2/pkg/appconsts/v2" + "github.com/celestiaorg/celestia-app/v2/test/util/genesis" "github.com/celestiaorg/celestia-app/v2/test/util/testfactory" "github.com/celestiaorg/celestia-app/v2/test/util/testnode" "github.com/cosmos/cosmos-sdk/codec" @@ -19,15 +19,16 @@ import ( cryptocodec "github.com/cosmos/cosmos-sdk/crypto/codec" "github.com/cosmos/cosmos-sdk/crypto/keyring" "github.com/cosmos/cosmos-sdk/crypto/keys/secp256k1" + cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types" "github.com/cosmos/cosmos-sdk/server" "github.com/cosmos/cosmos-sdk/simapp" - "github.com/tendermint/tendermint/crypto/ed25519" sdk "github.com/cosmos/cosmos-sdk/types" authtypes "github.com/cosmos/cosmos-sdk/x/auth/types" banktypes "github.com/cosmos/cosmos-sdk/x/bank/types" "github.com/spf13/cast" "github.com/stretchr/testify/require" abci "github.com/tendermint/tendermint/abci/types" + "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" @@ -75,8 +76,7 @@ func SetupTestAppWithGenesisValSet(cparams *tmproto.ConsensusParams, genAccounts return testApp, kr } -func NewTestAppWithGenesisSet(cparams *tmproto.ConsensusParams, genAccounts ...string) (*app.App, *tmtypes.ValidatorSet, keyring.Keyring) { - // var cache sdk.MultiStorePersistentCache +func NewTestApp() *app.App { // EmptyAppOptions is a stub implementing AppOptions emptyOpts := EmptyAppOptions{} // var anteOpt = func(bapp *baseapp.BaseApp) { bapp.SetAnteHandler(nil) } @@ -84,14 +84,103 @@ func NewTestAppWithGenesisSet(cparams *tmproto.ConsensusParams, genAccounts ...s encCfg := encoding.MakeConfig(app.ModuleEncodingRegisters...) - testApp := app.New( + return app.New( log.NewNopLogger(), db, nil, cast.ToUint(emptyOpts.Get(server.FlagInvCheckPeriod)), encCfg, 0, emptyOpts, ) +} + +func ApplyGenesisState(testApp *app.App, pubKeys []cryptotypes.PubKey, balance int64, cparams *tmproto.ConsensusParams) (keyring.Keyring, []genesis.Account, error) { + // create genesis + gen := genesis.NewDefaultGenesis().WithChainID("test").WithConsensusParams(cparams).WithGenesisTime(time.Date(2023, 1, 1, 1, 1, 1, 1, time.UTC).UTC()) + + for _, pk := range pubKeys { + err := gen.AddAccount(genesis.Account{ + PubKey: pk, + Balance: balance, + }) + if err != nil { + return nil, nil, err + } + } + + // hardcoding keys to make validator creation deterministic + consensusKey := ed25519.PrivKey(ed25519.GenPrivKeyFromSecret([]byte("12345678901234567890123456389012"))) + networkKey := ed25519.PrivKey(ed25519.GenPrivKeyFromSecret([]byte("12345678901234567890123456786012"))) + + // question: do we want to add validator in the keyring? + err := gen.AddValidator(genesis.Validator{ + KeyringAccount: genesis.KeyringAccount{ + Name: "validator1", + InitialTokens: 1_000_000_000, + }, + Stake: 1_000_000, + ConsensusKey: consensusKey, + NetworkKey: networkKey, + }) + if err != nil { + return nil, nil, err + } + genDoc, err := gen.Export() + + if err != nil { + return nil, nil, err + } + + // initialise test app against genesis + _ = testApp.Info(abci.RequestInfo{}) + + abciParams := &abci.ConsensusParams{ + Block: &abci.BlockParams{ + // choose some value large enough to not bottleneck the max square + // size + MaxBytes: int64(appconsts.DefaultSquareSizeUpperBound*appconsts.DefaultSquareSizeUpperBound) * appconsts.ContinuationSparseShareContentSize, + MaxGas: cparams.Block.MaxGas, + }, + Evidence: &cparams.Evidence, + Validator: &cparams.Validator, + Version: &cparams.Version, + } + + // init chain will set the validator set and initialize the genesis accounts + fmt.Println(genDoc.GenesisTime, "GENDOC APP STATE") + + fmt.Println(abciParams, "ABCI PARAMS") + fmt.Println(genDoc.ChainID, "CHAIN ID") + + // TODO Understand why genDoc.GenesisTime is getting reset + testApp.InitChain( + abci.RequestInitChain{ + Time: gen.GenesisTime, + Validators: []abci.ValidatorUpdate{}, + ConsensusParams: abciParams, + AppStateBytes: genDoc.AppState, + ChainId: genDoc.ChainID, + }, + ) + + // commit genesis changes + testApp.Commit() + testApp.BeginBlock(abci.RequestBeginBlock{Header: tmproto.Header{ + ChainID: ChainID, + Height: testApp.LastBlockHeight() + 1, + AppHash: testApp.LastCommitID().Hash, + ValidatorsHash: genDoc.ValidatorHash(), + NextValidatorsHash: genDoc.ValidatorHash(), + Version: tmversion.Consensus{ + App: cparams.Version.AppVersion, + }, + }}) + + return gen.Keyring(), gen.Accounts(), nil +} + +func NewTestAppWithGenesisSet(cparams *tmproto.ConsensusParams, genAccounts ...string) (*app.App, *tmtypes.ValidatorSet, keyring.Keyring) { + testApp := NewTestApp() genesisState, valSet, kr := GenesisStateWithSingleValidator(testApp, genAccounts...) stateBytes, err := json.MarshalIndent(genesisState, "", " ") diff --git a/test/util/testfactory/common.go b/test/util/testfactory/common.go index 8edb003aa8..689a431fff 100644 --- a/test/util/testfactory/common.go +++ b/test/util/testfactory/common.go @@ -2,6 +2,7 @@ package testfactory import ( "bytes" + // "fmt" "sort" "github.com/celestiaorg/celestia-app/v2/pkg/appconsts" diff --git a/test/util/testnode/utils.go b/test/util/testnode/utils.go index 780fb5a5e1..2fd92f0010 100644 --- a/test/util/testnode/utils.go +++ b/test/util/testnode/utils.go @@ -42,8 +42,6 @@ func QueryWithoutProof(clientCtx client.Context, hashHexStr string) (*rpctypes.R } func NewKeyring(accounts ...string) (keyring.Keyring, []sdk.AccAddress) { - fmt.Println(accounts, "accounts FRPM KEYRING") - fmt.Println(len(accounts), "ACCOUNTS LENGTH") cdc := encoding.MakeConfig(app.ModuleEncodingRegisters...).Codec kb := keyring.NewInMemory(cdc) @@ -71,7 +69,6 @@ func RandomAddress() sdk.Address { } func FundKeyringAccounts(kr keyring.Keyring, addresses []sdk.AccAddress) ([]banktypes.Balance, []authtypes.GenesisAccount) { - // kr, addresses := NewKeyring(accounts...) genAccounts := make([]authtypes.GenesisAccount, len(addresses)) genBalances := make([]banktypes.Balance, len(addresses)) @@ -83,7 +80,9 @@ func FundKeyringAccounts(kr keyring.Keyring, addresses []sdk.AccAddress) ([]bank genBalances[i] = banktypes.Balance{Address: addr.String(), Coins: balances.Sort()} genAccounts[i] = authtypes.NewBaseAccount(addr, nil, uint64(i), 0) } - return genBalances, genAccounts + fmt.Println(genBalances, "genBalances") + fmt.Println(genAccounts, "genAccounts") + return genBalances, genAccounts } func GenerateAccounts(count int) []string { From 0667101015348b25e20bdad6b6c448d3edf6be52 Mon Sep 17 00:00:00 2001 From: Nina Barbakadze Date: Mon, 3 Jun 2024 17:14:19 +0200 Subject: [PATCH 06/19] test: apphash matches needs cleaning up --- app/app.go | 1 - app/test/non_determinism_test.go | 14 ++++++++------ app/test/prepare_proposal_test.go | 4 ---- test/util/blobfactory/payforblob_factory.go | 2 +- test/util/direct_tx_gen.go | 2 +- test/util/genesis/document.go | 3 +++ test/util/genesis/genesis.go | 6 +++--- test/util/test_app.go | 16 ++++++---------- 8 files changed, 22 insertions(+), 26 deletions(-) diff --git a/app/app.go b/app/app.go index 1c3449d0cb..c6e4f15373 100644 --- a/app/app.go +++ b/app/app.go @@ -552,7 +552,6 @@ func (app *App) InitChain(req abci.RequestInitChain) (res abci.ResponseInitChain } res = app.BaseApp.InitChain(req) - fmt.Println(res, "RESPONSE INIT CHAIN") ctx := app.NewContext(false, tmproto.Header{}) if appVersion != v1 { diff --git a/app/test/non_determinism_test.go b/app/test/non_determinism_test.go index f147b28bdc..c1f4409807 100644 --- a/app/test/non_determinism_test.go +++ b/app/test/non_determinism_test.go @@ -2,6 +2,8 @@ package app_test import ( "fmt" + "testing" + "github.com/celestiaorg/celestia-app/v2/app" "github.com/celestiaorg/celestia-app/v2/app/encoding" "github.com/celestiaorg/celestia-app/v2/pkg/appconsts" @@ -16,7 +18,6 @@ import ( "github.com/cosmos/cosmos-sdk/crypto/types" "github.com/stretchr/testify/require" abci "github.com/tendermint/tendermint/abci/types" - "testing" ) func TestNonDeterminismBetweenAppVersions(t *testing.T) { @@ -25,6 +26,8 @@ func TestNonDeterminismBetweenAppVersions(t *testing.T) { numBlobTxs, numNormalTxs = 5, 5 ) + expectedAppHash := []byte{100, 237, 125, 126, 116, 10, 189, 82, 156, 116, 176, 136, 169, 92, 185, 12, 72, 134, 254, 175, 234, 13, 159, 90, 139, 192, 190, 248, 67, 9, 32, 217} + testApp := testutil.NewTestApp() enc := encoding.MakeConfig(app.ModuleEncodingRegisters...) @@ -37,10 +40,11 @@ func TestNonDeterminismBetweenAppVersions(t *testing.T) { addresses = append(addresses, account.Name) } - _, _, err := testutil.ApplyGenesisState(testApp, pubKeys, 1_000_000_000, app.DefaultConsensusParams()) + _, _, err := testutil.ApplyGenesisState(testApp, pubKeys, 1_000_000_000, app.DefaultInitialConsensusParams()) require.NoError(t, err) accinfos := queryAccountInfo(testApp, addresses, kr) + fmt.Println("AccountInfos:", accinfos) // create deterministic set of 10 transactions normalTxs := testutil.SendTxsWithAccounts( @@ -54,9 +58,6 @@ func TestNonDeterminismBetweenAppVersions(t *testing.T) { testutil.ChainID, ) - fmt.Println(len(accinfos[numBlobTxs:]), "ACCINFOS LENGTH") - fmt.Println(len(addresses[numBlobTxs:]), "ADDRESSES LENGTH") - // maybe change this to signer.CreatePFBS blobTxs := blobfactory.ManyMultiBlobTx(t, enc.TxConfig, kr, testutil.ChainID, addresses[numBlobTxs+1:], accinfos[numBlobTxs+1:], testfactory.Repeat([]*blob.Blob{ blob.New(HardcodedNamespace(), []byte{1}, appconsts.DefaultShareVersion), @@ -70,6 +71,7 @@ func TestNonDeterminismBetweenAppVersions(t *testing.T) { // deliver blob txs for _, tx := range blobTxs { + // fmt.Println("BlobTx:", tx) blobTx, ok := blob.UnmarshalBlobTx(tx) require.True(t, ok) resp := testApp.DeliverTx(abci.RequestDeliverTx{Tx: blobTx.Tx}) @@ -82,7 +84,7 @@ func TestNonDeterminismBetweenAppVersions(t *testing.T) { // // Get the app hash appHash := testApp.LastCommitID().Hash - fmt.Println("AppHash:", appHash) + require.Equal(t, expectedAppHash, appHash) } func HardcodedNamespace() appns.Namespace { diff --git a/app/test/prepare_proposal_test.go b/app/test/prepare_proposal_test.go index 7147645b1c..04b925e365 100644 --- a/app/test/prepare_proposal_test.go +++ b/app/test/prepare_proposal_test.go @@ -1,8 +1,6 @@ package app_test import ( - // "fmt" - "fmt" "testing" "time" @@ -211,9 +209,7 @@ func queryAccountInfo(capp *app.App, accs []string, kr keyring.Keyring) []blobfa infos := make([]blobfactory.AccountInfo, len(accs)) for i, acc := range accs { addr := testfactory.GetAddress(kr, acc) - fmt.Println(addr, "ADDR") accI := testutil.DirectQueryAccount(capp, addr) - fmt.Println(accI, "ACC INFO") infos[i] = blobfactory.AccountInfo{ AccountNum: accI.GetAccountNumber(), Sequence: accI.GetSequence(), diff --git a/test/util/blobfactory/payforblob_factory.go b/test/util/blobfactory/payforblob_factory.go index 9860999940..b0ab9706bb 100644 --- a/test/util/blobfactory/payforblob_factory.go +++ b/test/util/blobfactory/payforblob_factory.go @@ -227,7 +227,7 @@ func ManyMultiBlobTx( txs := make([][]byte, len(accounts)) opts := DefaultTxOpts() for i, acc := range accounts { - signer, err := user.NewSigner(kr, enc, chainid, appconsts.LatestVersion, user.NewAccount(acc, accInfos[i].AccountNum, accInfos[i].Sequence)) + signer, err := user.NewSigner(kr, enc, chainid, 1, user.NewAccount(acc, accInfos[i].AccountNum, accInfos[i].Sequence)) require.NoError(t, err) txs[i], _, err = signer.CreatePayForBlobs(acc, blobs[i], opts...) require.NoError(t, err) diff --git a/test/util/direct_tx_gen.go b/test/util/direct_tx_gen.go index ef0b47c24b..f9416d1d47 100644 --- a/test/util/direct_tx_gen.go +++ b/test/util/direct_tx_gen.go @@ -212,7 +212,7 @@ func SendTxWithManualSequence( opts ...user.TxOption, ) coretypes.Tx { fromAddr, toAddr := getAddress(fromAcc, kr), getAddress(toAcc, kr) - signer, err := user.NewSigner(kr, cfg, chainid, appconsts.LatestVersion, user.NewAccount(fromAcc, accountNum, sequence)) + signer, err := user.NewSigner(kr, cfg, chainid, 1, user.NewAccount(fromAcc, accountNum, sequence)) require.NoError(t, err) msg := banktypes.NewMsgSend(fromAddr, toAddr, sdk.NewCoins(sdk.NewCoin(app.BondDenom, sdk.NewIntFromUint64(amount)))) diff --git a/test/util/genesis/document.go b/test/util/genesis/document.go index 842bb92770..af37694891 100644 --- a/test/util/genesis/document.go +++ b/test/util/genesis/document.go @@ -38,6 +38,8 @@ func Document( if err != nil { return nil, fmt.Errorf("packing accounts: %w", err) } + // TODO: these are different come back to this + // fmt.Println("sdkAccounts", sdkAccounts) authGenState := authtypes.DefaultGenesisState() bankGenState := banktypes.DefaultGenesisState() @@ -57,6 +59,7 @@ func Document( } state := app.ModuleBasics.DefaultGenesis(ecfg.Codec) + // fmt.Println(state, "state") state[authtypes.ModuleName] = ecfg.Codec.MustMarshalJSON(authGenState) state[banktypes.ModuleName] = ecfg.Codec.MustMarshalJSON(bankGenState) state[genutiltypes.ModuleName] = ecfg.Codec.MustMarshalJSON(genutilGenState) diff --git a/test/util/genesis/genesis.go b/test/util/genesis/genesis.go index 2f55520e34..31ec00c77c 100644 --- a/test/util/genesis/genesis.go +++ b/test/util/genesis/genesis.go @@ -186,8 +186,8 @@ func (g *Genesis) AddValidator(val Validator) error { if err := g.AddAccount(account); err != nil { return err } - - // TODO decide on this + + // TODO decide on this // add validator to genesis keyring // if _, err := g.kr.Key(val.Name); err == nil { // return fmt.Errorf("validator with name %s already exists", val.Name) @@ -198,7 +198,7 @@ func (g *Genesis) AddValidator(val Validator) error { if err != nil { return err } - + // install the validator g.genTxs = append(g.genTxs, gentx) g.validators = append(g.validators, val) diff --git a/test/util/test_app.go b/test/util/test_app.go index 512ca74129..25881d22e8 100644 --- a/test/util/test_app.go +++ b/test/util/test_app.go @@ -95,7 +95,10 @@ func NewTestApp() *app.App { func ApplyGenesisState(testApp *app.App, pubKeys []cryptotypes.PubKey, balance int64, cparams *tmproto.ConsensusParams) (keyring.Keyring, []genesis.Account, error) { // create genesis - gen := genesis.NewDefaultGenesis().WithChainID("test").WithConsensusParams(cparams).WithGenesisTime(time.Date(2023, 1, 1, 1, 1, 1, 1, time.UTC).UTC()) + gen := genesis.NewDefaultGenesis(). + WithChainID(ChainID). + WithConsensusParams(cparams). + WithGenesisTime(time.Date(2023, 1, 1, 1, 1, 1, 1, time.UTC).UTC()) for _, pk := range pubKeys { err := gen.AddAccount(genesis.Account{ @@ -126,13 +129,12 @@ func ApplyGenesisState(testApp *app.App, pubKeys []cryptotypes.PubKey, balance i } genDoc, err := gen.Export() - if err != nil { return nil, nil, err } // initialise test app against genesis - _ = testApp.Info(abci.RequestInfo{}) + testApp.Info(abci.RequestInfo{}) abciParams := &abci.ConsensusParams{ Block: &abci.BlockParams{ @@ -146,12 +148,6 @@ func ApplyGenesisState(testApp *app.App, pubKeys []cryptotypes.PubKey, balance i Version: &cparams.Version, } - // init chain will set the validator set and initialize the genesis accounts - fmt.Println(genDoc.GenesisTime, "GENDOC APP STATE") - - fmt.Println(abciParams, "ABCI PARAMS") - fmt.Println(genDoc.ChainID, "CHAIN ID") - // TODO Understand why genDoc.GenesisTime is getting reset testApp.InitChain( abci.RequestInitChain{ @@ -202,7 +198,7 @@ func NewTestAppWithGenesisSet(cparams *tmproto.ConsensusParams, genAccounts ...s genesisTime := time.Date(2023, 1, 1, 1, 1, 1, 1, time.UTC).UTC() - _ = testApp.Info(abci.RequestInfo{}) + testApp.Info(abci.RequestInfo{}) // init chain will set the validator set and initialize the genesis accounts testApp.InitChain( From c0452be2354cbfe1cba2b936cb8f1bee6b9a5572 Mon Sep 17 00:00:00 2001 From: Nina Barbakadze Date: Mon, 3 Jun 2024 20:43:37 +0200 Subject: [PATCH 07/19] style: cleanup --- test/e2e/testnet/testnet.go | 2 +- test/util/genesis/document.go | 2 -- test/util/genesis/genesis.go | 42 +++++++++++++++++++++------------ test/util/test_app.go | 28 +++++++--------------- test/util/testfactory/common.go | 1 - test/util/testnode/utils.go | 5 ---- 6 files changed, 37 insertions(+), 43 deletions(-) diff --git a/test/e2e/testnet/testnet.go b/test/e2e/testnet/testnet.go index 4dd0bbe9b3..711fec012b 100644 --- a/test/e2e/testnet/testnet.go +++ b/test/e2e/testnet/testnet.go @@ -73,7 +73,7 @@ func (t *Testnet) CreateGenesisNode(version string, selfDelegation, upgradeHeigh if err != nil { return err } - if err := t.genesis.AddValidator(node.GenesisValidator()); err != nil { + if err := t.genesis.NewValidator(node.GenesisValidator()); err != nil { return err } t.nodes = append(t.nodes, node) diff --git a/test/util/genesis/document.go b/test/util/genesis/document.go index af37694891..8f9a0b3201 100644 --- a/test/util/genesis/document.go +++ b/test/util/genesis/document.go @@ -38,8 +38,6 @@ func Document( if err != nil { return nil, fmt.Errorf("packing accounts: %w", err) } - // TODO: these are different come back to this - // fmt.Println("sdkAccounts", sdkAccounts) authGenState := authtypes.DefaultGenesisState() bankGenState := banktypes.DefaultGenesisState() diff --git a/test/util/genesis/genesis.go b/test/util/genesis/genesis.go index 31ec00c77c..a02c97c6ef 100644 --- a/test/util/genesis/genesis.go +++ b/test/util/genesis/genesis.go @@ -11,6 +11,7 @@ import ( "github.com/cosmos/cosmos-sdk/crypto/hd" "github.com/cosmos/cosmos-sdk/crypto/keyring" sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/tendermint/tendermint/crypto/ed25519" tmrand "github.com/tendermint/tendermint/libs/rand" tmproto "github.com/tendermint/tendermint/proto/tendermint/types" coretypes "github.com/tendermint/tendermint/types" @@ -161,20 +162,36 @@ func (g *Genesis) NewValidator(val Validator) error { return nil } -// TODO improve this function to imply that we're just adding one validator to make it deterministic -func (g *Genesis) AddValidator(val Validator) error { +// AddDeterministicValidator adds a single validator to the genesis deterministically. +func (g *Genesis) AddDeterministicValidator() 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" - rec, err := g.kr.NewAccount("validator1", mnemo, "", "", hd.Secp256k1) + consensusKey := ed25519.PrivKey(ed25519.GenPrivKeyFromSecret([]byte("12345678901234567890123456389012"))) + networkKey := ed25519.PrivKey(ed25519.GenPrivKeyFromSecret([]byte("12345678901234567890123456786012"))) + + val := Validator{ + KeyringAccount: 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.kr.NewAccount(val.Name, mnemo, "", "", hd.Secp256k1) if err != nil { - return err + return fmt.Errorf("failed to create account: %w", err) } + validatorPubKey, err := rec.GetPubKey() if err != nil { - return err + return fmt.Errorf("failed to get pubkey: %w", err) } if err := val.ValidateBasic(); err != nil { - return err + return fmt.Errorf("failed to validate validator: %w", err) } // make account from keyring account @@ -183,20 +200,15 @@ func (g *Genesis) AddValidator(val Validator) error { Balance: val.KeyringAccount.InitialTokens, } + // add the validator's account to the genesis if err := g.AddAccount(account); err != nil { - return err + return fmt.Errorf("failed to add account: %w", err) } - // TODO decide on this - // add validator to genesis keyring - // if _, err := g.kr.Key(val.Name); err == nil { - // return fmt.Errorf("validator with name %s already exists", val.Name) - // } - - // // Add the validator's genesis transaction + // add the validator's genesis transaction gentx, err := val.GenTx(g.ecfg, g.kr, g.ChainID) if err != nil { - return err + return fmt.Errorf("failed to generate tx: %w", err) } // install the validator diff --git a/test/util/test_app.go b/test/util/test_app.go index 25881d22e8..d839d85587 100644 --- a/test/util/test_app.go +++ b/test/util/test_app.go @@ -71,11 +71,11 @@ func SetupTestAppWithGenesisValSet(cparams *tmproto.ConsensusParams, genAccounts App: cparams.Version.AppVersion, }, }}) - fmt.Print(testApp.LastCommitID().Hash, "LAST COMMIT ID HASH") return testApp, kr } +// NewTestApp creates a new app instance with an empty memDB and a no-op logger. func NewTestApp() *app.App { // EmptyAppOptions is a stub implementing AppOptions emptyOpts := EmptyAppOptions{} @@ -93,6 +93,7 @@ func NewTestApp() *app.App { ) } +// ApplyGenesisState sets genesis on initialized testApp with the provided arguments. func ApplyGenesisState(testApp *app.App, pubKeys []cryptotypes.PubKey, balance int64, cparams *tmproto.ConsensusParams) (keyring.Keyring, []genesis.Account, error) { // create genesis gen := genesis.NewDefaultGenesis(). @@ -100,6 +101,7 @@ func ApplyGenesisState(testApp *app.App, pubKeys []cryptotypes.PubKey, balance i WithConsensusParams(cparams). WithGenesisTime(time.Date(2023, 1, 1, 1, 1, 1, 1, time.UTC).UTC()) + // add accounts to genesis for _, pk := range pubKeys { err := gen.AddAccount(genesis.Account{ PubKey: pk, @@ -110,27 +112,15 @@ func ApplyGenesisState(testApp *app.App, pubKeys []cryptotypes.PubKey, balance i } } - // hardcoding keys to make validator creation deterministic - consensusKey := ed25519.PrivKey(ed25519.GenPrivKeyFromSecret([]byte("12345678901234567890123456389012"))) - networkKey := ed25519.PrivKey(ed25519.GenPrivKeyFromSecret([]byte("12345678901234567890123456786012"))) - - // question: do we want to add validator in the keyring? - err := gen.AddValidator(genesis.Validator{ - KeyringAccount: genesis.KeyringAccount{ - Name: "validator1", - InitialTokens: 1_000_000_000, - }, - Stake: 1_000_000, - ConsensusKey: consensusKey, - NetworkKey: networkKey, - }) + // add validator to genesis + err := gen.AddDeterministicValidator() if err != nil { - return nil, nil, err + return nil, nil, fmt.Errorf("failed to add validator: %w", err) } genDoc, err := gen.Export() if err != nil { - return nil, nil, err + return nil, nil, fmt.Errorf("failed to export genesis doc: %w", err) } // initialise test app against genesis @@ -148,7 +138,7 @@ func ApplyGenesisState(testApp *app.App, pubKeys []cryptotypes.PubKey, balance i Version: &cparams.Version, } - // TODO Understand why genDoc.GenesisTime is getting reset + // init chain will set the validator set and initialize the genesis accounts testApp.InitChain( abci.RequestInitChain{ Time: gen.GenesisTime, @@ -288,7 +278,7 @@ func GenesisStateWithSingleValidator(testApp *app.App, genAccounts ...string) (a senderPrivKey := secp256k1.GenPrivKeyFromSecret([]byte("09876543210987654321098765432109")) acc := authtypes.NewBaseAccount(senderPrivKey.PubKey().Address().Bytes(), senderPrivKey.PubKey(), 0, 0) - // genesis and sender accounts will be added to this slice + // append sender account to genesis accounts accs := make([]authtypes.GenesisAccount, 0, len(genAccounts)+1) accs = append(accs, acc) diff --git a/test/util/testfactory/common.go b/test/util/testfactory/common.go index 689a431fff..8edb003aa8 100644 --- a/test/util/testfactory/common.go +++ b/test/util/testfactory/common.go @@ -2,7 +2,6 @@ package testfactory import ( "bytes" - // "fmt" "sort" "github.com/celestiaorg/celestia-app/v2/pkg/appconsts" diff --git a/test/util/testnode/utils.go b/test/util/testnode/utils.go index 2fd92f0010..7784b1e68f 100644 --- a/test/util/testnode/utils.go +++ b/test/util/testnode/utils.go @@ -3,7 +3,6 @@ package testnode import ( "context" "encoding/hex" - "fmt" "github.com/celestiaorg/celestia-app/v2/app" "github.com/celestiaorg/celestia-app/v2/app/encoding" @@ -48,7 +47,6 @@ func NewKeyring(accounts ...string) (keyring.Keyring, []sdk.AccAddress) { addresses := make([]sdk.AccAddress, len(accounts)) for idx, acc := range accounts { rec, _, err := kb.NewMnemonic(acc, keyring.English, "", "", hd.Secp256k1) - fmt.Println(rec.Name, "rec") if err != nil { panic(err) } @@ -57,7 +55,6 @@ func NewKeyring(accounts ...string) (keyring.Keyring, []sdk.AccAddress) { panic(err) } addresses[idx] = addr - fmt.Println("Address", addr) } return kb, addresses } @@ -80,8 +77,6 @@ func FundKeyringAccounts(kr keyring.Keyring, addresses []sdk.AccAddress) ([]bank genBalances[i] = banktypes.Balance{Address: addr.String(), Coins: balances.Sort()} genAccounts[i] = authtypes.NewBaseAccount(addr, nil, uint64(i), 0) } - fmt.Println(genBalances, "genBalances") - fmt.Println(genAccounts, "genAccounts") return genBalances, genAccounts } From 60edfd32ad577d19e0a2d65fa717ed7f0cebc0e9 Mon Sep 17 00:00:00 2001 From: Nina Barbakadze Date: Mon, 3 Jun 2024 23:38:41 +0200 Subject: [PATCH 08/19] refactor: move some logic from genesis to testapp --- app/test/non_determinism_test.go | 39 +++++++----- test/util/genesis/genesis.go | 100 ++++++++++--------------------- test/util/test_app.go | 48 ++++++++++++++- test/util/testnode/utils.go | 2 +- 4 files changed, 101 insertions(+), 88 deletions(-) diff --git a/app/test/non_determinism_test.go b/app/test/non_determinism_test.go index c1f4409807..35ba0f62ee 100644 --- a/app/test/non_determinism_test.go +++ b/app/test/non_determinism_test.go @@ -20,33 +20,38 @@ import ( abci "github.com/tendermint/tendermint/abci/types" ) -func TestNonDeterminismBetweenAppVersions(t *testing.T) { - // set up testapp with genesis state +// TestNonDeterminismBetweenMainAndV1 executes a set of different transactions, +// produces an app hash and compares it with the app hash produced by v1.x +func TestNonDeterminismBetweenMainAndV1(t *testing.T) { const ( numBlobTxs, numNormalTxs = 5, 5 ) expectedAppHash := []byte{100, 237, 125, 126, 116, 10, 189, 82, 156, 116, 176, 136, 169, 92, 185, 12, 72, 134, 254, 175, 234, 13, 159, 90, 139, 192, 190, 248, 67, 9, 32, 217} + // initialize testApp testApp := testutil.NewTestApp() enc := encoding.MakeConfig(app.ModuleEncodingRegisters...) + // create deterministic keys kr, pubKeys := DeterministicKeyRing(enc.Codec) var addresses []string - krss, _ := kr.List() - // this is for getting names of accounts - for _, account := range krss { - addresses = append(addresses, account.Name) + recs, err := kr.List() + require.NoError(t, err) + + // Get the name of the records + for _, rec := range recs { + addresses = append(addresses, rec.Name) } - _, _, err := testutil.ApplyGenesisState(testApp, pubKeys, 1_000_000_000, app.DefaultInitialConsensusParams()) + // Apply genesis state to the app + _, _, err = testutil.ApplyGenesisState(testApp, pubKeys, 1_000_000_000, app.DefaultInitialConsensusParams()) require.NoError(t, err) accinfos := queryAccountInfo(testApp, addresses, kr) - fmt.Println("AccountInfos:", accinfos) - // create deterministic set of 10 transactions + // Create a set of 10 deterministic sdk transactions normalTxs := testutil.SendTxsWithAccounts( t, testApp, @@ -58,20 +63,19 @@ func TestNonDeterminismBetweenAppVersions(t *testing.T) { testutil.ChainID, ) - // maybe change this to signer.CreatePFBS + // Create a set of 5 deterministic blob transactions blobTxs := blobfactory.ManyMultiBlobTx(t, enc.TxConfig, kr, testutil.ChainID, addresses[numBlobTxs+1:], accinfos[numBlobTxs+1:], testfactory.Repeat([]*blob.Blob{ - blob.New(HardcodedNamespace(), []byte{1}, appconsts.DefaultShareVersion), + blob.New(DeterministicNamespace(), []byte{1}, appconsts.DefaultShareVersion), }, numBlobTxs)) - // deliver normal txs + // Deliver sdk txs for _, tx := range normalTxs { resp := testApp.DeliverTx(abci.RequestDeliverTx{Tx: tx}) require.EqualValues(t, 0, resp.Code, resp.Log) } - // deliver blob txs + // Deliver blob txs for _, tx := range blobTxs { - // fmt.Println("BlobTx:", tx) blobTx, ok := blob.UnmarshalBlobTx(tx) require.True(t, ok) resp := testApp.DeliverTx(abci.RequestDeliverTx{Tx: blobTx.Tx}) @@ -81,19 +85,22 @@ func TestNonDeterminismBetweenAppVersions(t *testing.T) { // Commit the state testApp.Commit() - // // Get the app hash + // Get the app hash appHash := testApp.LastCommitID().Hash + // Assert that the app hash is equal to the app hash produced by v1.x require.Equal(t, expectedAppHash, appHash) } -func HardcodedNamespace() appns.Namespace { +// DeterministicNamespace returns a deterministic namespace +func DeterministicNamespace() appns.Namespace { return appns.Namespace{ Version: 0, ID: []byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 37, 67, 154, 200, 228, 130, 74, 147, 162, 11}, } } +// DeterministicKeyRing returns a deterministic keyring and a list of deterministic public keys func DeterministicKeyRing(cdc codec.Codec) (keyring.Keyring, []types.PubKey) { mnemonics := []string{ "great myself congress genuine scale muscle view uncover pipe miracle sausage broccoli lonely swap table foam brand turtle comic gorilla firm mad grunt hazard", diff --git a/test/util/genesis/genesis.go b/test/util/genesis/genesis.go index a02c97c6ef..9472fc0958 100644 --- a/test/util/genesis/genesis.go +++ b/test/util/genesis/genesis.go @@ -45,6 +45,21 @@ type Genesis struct { genOps []Modifier } +// Accounts getter +func (g *Genesis) Accounts() []Account { + return g.accounts +} + +// Keyring getter +func (g *Genesis) Keyring() keyring.Keyring { + return g.kr +} + +// Validators getter +func (g *Genesis) Validators() []Validator { + return g.validators +} + // NewDefaultGenesis creates a new default genesis with no accounts or validators. func NewDefaultGenesis() *Genesis { ecfg := encoding.MakeConfig(app.ModuleBasics) @@ -59,26 +74,31 @@ func NewDefaultGenesis() *Genesis { return g } +// WithModifier adds a genesis modifier to the genesis. func (g *Genesis) WithModifiers(ops ...Modifier) *Genesis { g.genOps = append(g.genOps, ops...) return g } +// WithConsensusParams sets the consensus parameters of the genesis. func (g *Genesis) WithConsensusParams(params *tmproto.ConsensusParams) *Genesis { g.ConsensusParams = params return g } +// WithChainID sets the chain ID of the genesis. func (g *Genesis) WithChainID(chainID string) *Genesis { g.ChainID = chainID return g } +// WithGenesisTime sets the genesis time of the genesis. func (g *Genesis) WithGenesisTime(genesisTime time.Time) *Genesis { g.GenesisTime = genesisTime return g } +// WithAccounts adds the given validators to the genesis. func (g *Genesis) WithValidators(vals ...Validator) *Genesis { for _, val := range vals { err := g.NewValidator(val) @@ -101,6 +121,7 @@ func (g *Genesis) WithKeyringAccounts(accs ...KeyringAccount) *Genesis { return g } +// AddAccount adds an existing account to the genesis. func (g *Genesis) AddAccount(account Account) error { for _, acc := range g.accounts { if bytes.Equal(acc.PubKey.Bytes(), account.PubKey.Bytes()) { @@ -111,6 +132,7 @@ func (g *Genesis) AddAccount(account Account) error { return nil } +// NewAccount creates a new account and adds it to the genesis. func (g *Genesis) NewAccount(acc KeyringAccount) error { if err := acc.ValidateBasic(); err != nil { return err @@ -140,16 +162,12 @@ func (g *Genesis) NewAccount(acc KeyringAccount) error { return nil } -func (g *Genesis) NewValidator(val Validator) error { +// AddValidator verifies and adds a given validator to the genesis. +func (g *Genesis) AddValidator(val Validator) error { if err := val.ValidateBasic(); err != nil { return err } - // Add the validator's genesis account - if err := g.NewAccount(val.KeyringAccount); err != nil { - return err - } - // Add the validator's genesis transaction gentx, err := val.GenTx(g.ecfg, g.kr, g.ChainID) if err != nil { @@ -162,65 +180,17 @@ func (g *Genesis) NewValidator(val Validator) error { return nil } -// AddDeterministicValidator adds a single validator to the genesis deterministically. -func (g *Genesis) AddDeterministicValidator() 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.PrivKey(ed25519.GenPrivKeyFromSecret([]byte("12345678901234567890123456389012"))) - networkKey := ed25519.PrivKey(ed25519.GenPrivKeyFromSecret([]byte("12345678901234567890123456786012"))) - - val := Validator{ - KeyringAccount: 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.kr.NewAccount(val.Name, mnemo, "", "", 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) - } - - if err := val.ValidateBasic(); err != nil { - return fmt.Errorf("failed to validate validator: %w", err) - } - - // make account from keyring account - account := 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 genesis transaction - gentx, err := val.GenTx(g.ecfg, g.kr, g.ChainID) - if err != nil { - return fmt.Errorf("failed to generate tx: %w", err) +// Creates a new validator account and adds it to the genesis. +func (g *Genesis) NewValidator(val Validator) error { + // Add the validator's genesis account + if err := g.NewAccount(val.KeyringAccount); err != nil { + return err } - // install the validator - g.genTxs = append(g.genTxs, gentx) - g.validators = append(g.validators, val) - return nil -} - -func (g *Genesis) Accounts() []Account { - return g.accounts + return g.AddValidator(val) } +// Export returns the genesis document of the network. func (g *Genesis) Export() (*coretypes.GenesisDoc, error) { gentxs := make([]json.RawMessage, 0, len(g.genTxs)) for _, genTx := range g.genTxs { @@ -242,14 +212,6 @@ func (g *Genesis) Export() (*coretypes.GenesisDoc, error) { ) } -func (g *Genesis) Keyring() keyring.Keyring { - return g.kr -} - -func (g *Genesis) Validators() []Validator { - return g.validators -} - // Validator returns the validator at the given index. False is returned if the // index is out of bounds. func (g *Genesis) Validator(i int) (Validator, bool) { diff --git a/test/util/test_app.go b/test/util/test_app.go index d839d85587..951d81e424 100644 --- a/test/util/test_app.go +++ b/test/util/test_app.go @@ -17,6 +17,7 @@ import ( "github.com/cosmos/cosmos-sdk/codec" codectypes "github.com/cosmos/cosmos-sdk/codec/types" cryptocodec "github.com/cosmos/cosmos-sdk/crypto/codec" + hd "github.com/cosmos/cosmos-sdk/crypto/hd" "github.com/cosmos/cosmos-sdk/crypto/keyring" "github.com/cosmos/cosmos-sdk/crypto/keys/secp256k1" cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types" @@ -113,7 +114,7 @@ func ApplyGenesisState(testApp *app.App, pubKeys []cryptotypes.PubKey, balance i } // add validator to genesis - err := gen.AddDeterministicValidator() + err := AddDeterministicValidatorToGenesis(gen) if err != nil { return nil, nil, fmt.Errorf("failed to add validator: %w", err) } @@ -165,6 +166,7 @@ func ApplyGenesisState(testApp *app.App, pubKeys []cryptotypes.PubKey, balance i return gen.Keyring(), gen.Accounts(), nil } +// NewTestAppWithGenesisSet initializes a new app with a validator set and genesis accounts. func NewTestAppWithGenesisSet(cparams *tmproto.ConsensusParams, genAccounts ...string) (*app.App, *tmtypes.ValidatorSet, keyring.Keyring) { testApp := NewTestApp() genesisState, valSet, kr := GenesisStateWithSingleValidator(testApp, genAccounts...) @@ -203,6 +205,48 @@ func NewTestAppWithGenesisSet(cparams *tmproto.ConsensusParams, genAccounts ...s return testApp, valSet, 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) + } + + 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, + } + + // add the validator's account to the genesis + if err := g.AddAccount(account); err != nil { + return fmt.Errorf("failed to add account: %w", err) + } + + return g.AddValidator(val) +} + // AddAccount mimics the cli addAccount command, providing an // account with an allocation of to "token" and "tia" tokens in the genesis // state @@ -292,7 +336,7 @@ func GenesisStateWithSingleValidator(testApp *app.App, genAccounts ...string) (a // create a new keyring with the generated accounts kr, addresses := testnode.NewKeyring(genAccounts...) // fund the accounts - fundedBankAccs, fundedAuthAccs := testnode.FundKeyringAccounts(kr, addresses) + fundedBankAccs, fundedAuthAccs := testnode.FundKeyringAccounts(addresses) accs = append(accs, fundedAuthAccs...) balances = append(balances, fundedBankAccs...) diff --git a/test/util/testnode/utils.go b/test/util/testnode/utils.go index 7784b1e68f..d8c4ebece2 100644 --- a/test/util/testnode/utils.go +++ b/test/util/testnode/utils.go @@ -65,7 +65,7 @@ func RandomAddress() sdk.Address { return addresses[0] } -func FundKeyringAccounts(kr keyring.Keyring, addresses []sdk.AccAddress) ([]banktypes.Balance, []authtypes.GenesisAccount) { +func FundKeyringAccounts(addresses []sdk.AccAddress) ([]banktypes.Balance, []authtypes.GenesisAccount) { genAccounts := make([]authtypes.GenesisAccount, len(addresses)) genBalances := make([]banktypes.Balance, len(addresses)) From e0d91b212af6aab4c6deb1fdd335423163f03d97 Mon Sep 17 00:00:00 2001 From: Nina Barbakadze Date: Mon, 3 Jun 2024 23:59:57 +0200 Subject: [PATCH 09/19] style: more cleaning up --- app/test/non_determinism_test.go | 8 ++++---- app/test/prepare_proposal_test.go | 4 +++- app/test/process_proposal_test.go | 1 + test/util/blobfactory/payforblob_factory.go | 3 ++- test/util/direct_tx_gen.go | 4 +++- test/util/genesis/document.go | 1 - test/util/genesis/genesis.go | 1 - 7 files changed, 13 insertions(+), 9 deletions(-) diff --git a/app/test/non_determinism_test.go b/app/test/non_determinism_test.go index 35ba0f62ee..0c76f0be3c 100644 --- a/app/test/non_determinism_test.go +++ b/app/test/non_determinism_test.go @@ -29,16 +29,16 @@ func TestNonDeterminismBetweenMainAndV1(t *testing.T) { expectedAppHash := []byte{100, 237, 125, 126, 116, 10, 189, 82, 156, 116, 176, 136, 169, 92, 185, 12, 72, 134, 254, 175, 234, 13, 159, 90, 139, 192, 190, 248, 67, 9, 32, 217} - // initialize testApp + // Initialize testApp testApp := testutil.NewTestApp() enc := encoding.MakeConfig(app.ModuleEncodingRegisters...) - // create deterministic keys + // Create deterministic keys kr, pubKeys := DeterministicKeyRing(enc.Codec) - var addresses []string recs, err := kr.List() require.NoError(t, err) + addresses := make([]string, len(recs)) // Get the name of the records for _, rec := range recs { @@ -66,7 +66,7 @@ func TestNonDeterminismBetweenMainAndV1(t *testing.T) { // Create a set of 5 deterministic blob transactions blobTxs := blobfactory.ManyMultiBlobTx(t, enc.TxConfig, kr, testutil.ChainID, addresses[numBlobTxs+1:], accinfos[numBlobTxs+1:], testfactory.Repeat([]*blob.Blob{ blob.New(DeterministicNamespace(), []byte{1}, appconsts.DefaultShareVersion), - }, numBlobTxs)) + }, numBlobTxs), app.DefaultInitialConsensusParams().Version.AppVersion) // Deliver sdk txs for _, tx := range normalTxs { diff --git a/app/test/prepare_proposal_test.go b/app/test/prepare_proposal_test.go index 04b925e365..2da3d79286 100644 --- a/app/test/prepare_proposal_test.go +++ b/app/test/prepare_proposal_test.go @@ -40,6 +40,7 @@ func TestPrepareProposalPutsPFBsAtEnd(t *testing.T) { testfactory.Repeat([]*blob.Blob{ blob.New(appns.RandomBlobNamespace(), []byte{1}, appconsts.DefaultShareVersion), }, numBlobTxs), + app.DefaultConsensusParams().Version.AppVersion, ) normalTxs := testutil.SendTxsWithAccounts( @@ -97,6 +98,7 @@ func TestPrepareProposalFiltering(t *testing.T) { testfactory.RandomBlobNamespaces(tmrand.NewRand(), 3), [][]int{{100}, {1000}, {420}}, ), + app.DefaultConsensusParams().Version.AppVersion, ) // create 3 MsgSend transactions that are signed with valid account numbers @@ -136,7 +138,7 @@ func TestPrepareProposalFiltering(t *testing.T) { nilAccount := "carmon san diego" _, _, err := kr.NewMnemonic(nilAccount, keyring.English, "", "", hd.Secp256k1) require.NoError(t, err) - noAccountTx := []byte(testutil.SendTxWithManualSequence(t, encConf.TxConfig, kr, nilAccount, accounts[0], 1000, "", 0, 6)) + noAccountTx := []byte(testutil.SendTxWithManualSequence(t, encConf.TxConfig, kr, nilAccount, accounts[0], 1000, "", 0, app.DefaultConsensusParams().Version.AppVersion, 6)) type test struct { name string diff --git a/app/test/process_proposal_test.go b/app/test/process_proposal_test.go index fd52936322..9acaec6d99 100644 --- a/app/test/process_proposal_test.go +++ b/app/test/process_proposal_test.go @@ -47,6 +47,7 @@ func TestProcessProposal(t *testing.T) { testfactory.RandomBlobNamespaces(tmrand.NewRand(), 4), [][]int{{100}, {1000}, {420}, {300}}, ), + app.DefaultConsensusParams().Version.AppVersion, ) // create 3 MsgSend transactions that are signed with valid account numbers diff --git a/test/util/blobfactory/payforblob_factory.go b/test/util/blobfactory/payforblob_factory.go index b0ab9706bb..0db4cbca79 100644 --- a/test/util/blobfactory/payforblob_factory.go +++ b/test/util/blobfactory/payforblob_factory.go @@ -222,12 +222,13 @@ func ManyMultiBlobTx( accounts []string, accInfos []AccountInfo, blobs [][]*blob.Blob, + appVersion uint64, ) [][]byte { t.Helper() txs := make([][]byte, len(accounts)) opts := DefaultTxOpts() for i, acc := range accounts { - signer, err := user.NewSigner(kr, enc, chainid, 1, user.NewAccount(acc, accInfos[i].AccountNum, accInfos[i].Sequence)) + signer, err := user.NewSigner(kr, enc, chainid, appVersion, user.NewAccount(acc, accInfos[i].AccountNum, accInfos[i].Sequence)) require.NoError(t, err) txs[i], _, err = signer.CreatePayForBlobs(acc, blobs[i], opts...) require.NoError(t, err) diff --git a/test/util/direct_tx_gen.go b/test/util/direct_tx_gen.go index f9416d1d47..902dc1baf5 100644 --- a/test/util/direct_tx_gen.go +++ b/test/util/direct_tx_gen.go @@ -192,6 +192,7 @@ func SendTxsWithAccounts( chainid, acc.GetSequence(), acc.GetAccountNumber(), + appconsts.LatestVersion, opts..., ) } @@ -209,10 +210,11 @@ func SendTxWithManualSequence( amount uint64, chainid string, sequence, accountNum uint64, + appVersion uint64, opts ...user.TxOption, ) coretypes.Tx { fromAddr, toAddr := getAddress(fromAcc, kr), getAddress(toAcc, kr) - signer, err := user.NewSigner(kr, cfg, chainid, 1, user.NewAccount(fromAcc, accountNum, sequence)) + signer, err := user.NewSigner(kr, cfg, chainid, appVersion, user.NewAccount(fromAcc, accountNum, sequence)) require.NoError(t, err) msg := banktypes.NewMsgSend(fromAddr, toAddr, sdk.NewCoins(sdk.NewCoin(app.BondDenom, sdk.NewIntFromUint64(amount)))) diff --git a/test/util/genesis/document.go b/test/util/genesis/document.go index 8f9a0b3201..842bb92770 100644 --- a/test/util/genesis/document.go +++ b/test/util/genesis/document.go @@ -57,7 +57,6 @@ func Document( } state := app.ModuleBasics.DefaultGenesis(ecfg.Codec) - // fmt.Println(state, "state") state[authtypes.ModuleName] = ecfg.Codec.MustMarshalJSON(authGenState) state[banktypes.ModuleName] = ecfg.Codec.MustMarshalJSON(bankGenState) state[genutiltypes.ModuleName] = ecfg.Codec.MustMarshalJSON(genutilGenState) diff --git a/test/util/genesis/genesis.go b/test/util/genesis/genesis.go index 9472fc0958..ab2e8a93eb 100644 --- a/test/util/genesis/genesis.go +++ b/test/util/genesis/genesis.go @@ -11,7 +11,6 @@ import ( "github.com/cosmos/cosmos-sdk/crypto/hd" "github.com/cosmos/cosmos-sdk/crypto/keyring" sdk "github.com/cosmos/cosmos-sdk/types" - "github.com/tendermint/tendermint/crypto/ed25519" tmrand "github.com/tendermint/tendermint/libs/rand" tmproto "github.com/tendermint/tendermint/proto/tendermint/types" coretypes "github.com/tendermint/tendermint/types" From 11e55a2c0777f02c8069b90008b630304d55186d Mon Sep 17 00:00:00 2001 From: Nina Barbakadze Date: Tue, 4 Jun 2024 00:10:16 +0200 Subject: [PATCH 10/19] test: fix mistake in array allocation --- app/test/non_determinism_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/test/non_determinism_test.go b/app/test/non_determinism_test.go index 0c76f0be3c..4392eb9ff5 100644 --- a/app/test/non_determinism_test.go +++ b/app/test/non_determinism_test.go @@ -38,7 +38,7 @@ func TestNonDeterminismBetweenMainAndV1(t *testing.T) { recs, err := kr.List() require.NoError(t, err) - addresses := make([]string, len(recs)) + addresses := make([]string, 0, len(recs)) // Get the name of the records for _, rec := range recs { From 7b4c1e181ec7b596821a7333ed0dca179d28d9c6 Mon Sep 17 00:00:00 2001 From: Nina Barbakadze Date: Fri, 7 Jun 2024 16:32:27 +0200 Subject: [PATCH 11/19] test: make the test simpler --- app/test/non_determinism_test.go | 132 ++++++++++++++++++++----------- 1 file changed, 88 insertions(+), 44 deletions(-) diff --git a/app/test/non_determinism_test.go b/app/test/non_determinism_test.go index 4392eb9ff5..636b92985f 100644 --- a/app/test/non_determinism_test.go +++ b/app/test/non_determinism_test.go @@ -9,7 +9,9 @@ import ( "github.com/celestiaorg/celestia-app/v2/pkg/appconsts" testutil "github.com/celestiaorg/celestia-app/v2/test/util" "github.com/celestiaorg/celestia-app/v2/test/util/blobfactory" - "github.com/celestiaorg/celestia-app/v2/test/util/testfactory" + + // "github.com/celestiaorg/celestia-app/v2/test/util/blobfactory" + // "github.com/celestiaorg/celestia-app/v2/test/util/testfactory" "github.com/celestiaorg/go-square/blob" appns "github.com/celestiaorg/go-square/namespace" "github.com/cosmos/cosmos-sdk/codec" @@ -17,19 +19,33 @@ import ( keyring "github.com/cosmos/cosmos-sdk/crypto/keyring" "github.com/cosmos/cosmos-sdk/crypto/types" "github.com/stretchr/testify/require" + + "github.com/celestiaorg/celestia-app/v2/pkg/user" + sdk "github.com/cosmos/cosmos-sdk/types" + banktypes "github.com/cosmos/cosmos-sdk/x/bank/types" abci "github.com/tendermint/tendermint/abci/types" + tmproto "github.com/tendermint/tendermint/proto/tendermint/types" + "github.com/tendermint/tendermint/proto/tendermint/version" ) -// TestNonDeterminismBetweenMainAndV1 executes a set of different transactions, -// produces an app hash and compares it with the app hash produced by v1.x -func TestNonDeterminismBetweenMainAndV1(t *testing.T) { - const ( - numBlobTxs, numNormalTxs = 5, 5 - ) +type sdkTxStruct struct { + sdkMsgs []sdk.Msg + txOptions []user.TxOption +} + +type blobTxStruct struct { + author string + blobs []*blob.Blob + txOptions []user.TxOption +} - expectedAppHash := []byte{100, 237, 125, 126, 116, 10, 189, 82, 156, 116, 176, 136, 169, 92, 185, 12, 72, 134, 254, 175, 234, 13, 159, 90, 139, 192, 190, 248, 67, 9, 32, 217} +// TestConsistentAppHash executes transactions, +// produces an app hash and compares it with the app hash produced by v1.x. +// testApp is running v1. +func TestConsistentAppHash(t *testing.T) { + // expectedAppHash := []byte{100, 237, 125, 126, 116, 10, 189, 82, 156, 116, 176, 136, 169, 92, 185, 12, 72, 134, 254, 175, 234, 13, 159, 90, 139, 192, 190, 248, 67, 9, 32, 217} - // Initialize testApp + // Initialize testApp testApp := testutil.NewTestApp() enc := encoding.MakeConfig(app.ModuleEncodingRegisters...) @@ -38,62 +54,90 @@ func TestNonDeterminismBetweenMainAndV1(t *testing.T) { recs, err := kr.List() require.NoError(t, err) - addresses := make([]string, 0, len(recs)) + accountNames := make([]string, 0, len(recs)) // Get the name of the records for _, rec := range recs { - addresses = append(addresses, rec.Name) + accountNames = append(accountNames, rec.Name) } - // Apply genesis state to the app + // Apply genesis state to the app. _, _, err = testutil.ApplyGenesisState(testApp, pubKeys, 1_000_000_000, app.DefaultInitialConsensusParams()) require.NoError(t, err) - accinfos := queryAccountInfo(testApp, addresses, kr) - - // Create a set of 10 deterministic sdk transactions - normalTxs := testutil.SendTxsWithAccounts( - t, - testApp, - enc.TxConfig, - kr, - 1000, - addresses[0], - addresses[:numNormalTxs], - testutil.ChainID, - ) - - // Create a set of 5 deterministic blob transactions - blobTxs := blobfactory.ManyMultiBlobTx(t, enc.TxConfig, kr, testutil.ChainID, addresses[numBlobTxs+1:], accinfos[numBlobTxs+1:], testfactory.Repeat([]*blob.Blob{ - blob.New(DeterministicNamespace(), []byte{1}, appconsts.DefaultShareVersion), - }, numBlobTxs), app.DefaultInitialConsensusParams().Version.AppVersion) - - // Deliver sdk txs - for _, tx := range normalTxs { - resp := testApp.DeliverTx(abci.RequestDeliverTx{Tx: tx}) - require.EqualValues(t, 0, resp.Code, resp.Log) + // Query keyring account infos + accountInfos := queryAccountInfo(testApp, accountNames, kr) + + // Create accounts for the signer + var accounts []*user.Account + for i, accountInfo := range accountInfos { + account := user.NewAccount(accountNames[i], accountInfo.AccountNum, accountInfo.Sequence) + accounts = append(accounts, account) + } + + // Create a signer with keyring accounts + signer, err := user.NewSigner(kr, enc.TxConfig, testutil.ChainID, app.DefaultInitialVersion, accounts...) + require.NoError(t, err) + + amount := sdk.NewCoins(sdk.NewCoin(app.BondDenom, sdk.NewIntFromUint64(1000))) + + // Create an SDK Tx + sdkTx := sdkTxStruct{ + sdkMsgs: []sdk.Msg{ + banktypes.NewMsgSend(signer.Account(accountNames[0]).Address(), + signer.Account(accountNames[1]).Address(), + amount)}, + txOptions: blobfactory.DefaultTxOpts(), + } + + // Create a Blob Tx + blobTx := blobTxStruct{ + author: accountNames[2], + blobs: []*blob.Blob{blob.New(Namespace(), []byte{1}, appconsts.DefaultShareVersion)}, + txOptions: blobfactory.DefaultTxOpts(), } - // Deliver blob txs - for _, tx := range blobTxs { - blobTx, ok := blob.UnmarshalBlobTx(tx) - require.True(t, ok) - resp := testApp.DeliverTx(abci.RequestDeliverTx{Tx: blobTx.Tx}) - require.EqualValues(t, 0, resp.Code, resp.Log) + // Create SDK Tx + rawSdkTx, err := signer.CreateTx(sdkTx.sdkMsgs, sdkTx.txOptions...) + require.NoError(t, err) + + // Create Blob Tx + rawBlobTx, _, err := signer.CreatePayForBlobs(blobTx.author, blobTx.blobs, blobTx.txOptions...) + require.NoError(t, err) + + // BeginBlock + header := tmproto.Header{ + Version: version.Consensus{App: 1}, + Height: testApp.LastBlockHeight() + 1, } + testApp.BeginBlock(abci.RequestBeginBlock{Header: header}) + + // Deliver SDK Tx + resp := testApp.DeliverTx(abci.RequestDeliverTx{Tx: rawSdkTx}) + require.EqualValues(t, 0, resp.Code, resp.Log) + + // Deliver Blob Tx + blob, isBlobTx := blob.UnmarshalBlobTx(rawBlobTx) + require.True(t, isBlobTx) + resp = testApp.DeliverTx(abci.RequestDeliverTx{Tx: blob.Tx}) + require.EqualValues(t, 0, resp.Code, resp.Log) + + // EndBlock + testApp.EndBlock(abci.RequestEndBlock{Height: header.Height}) // Commit the state testApp.Commit() // Get the app hash appHash := testApp.LastCommitID().Hash + fmt.Println(appHash) - // Assert that the app hash is equal to the app hash produced by v1.x - require.Equal(t, expectedAppHash, appHash) + // Require that the app hash is equal to the app hash produced by v1.x + // require.Equal(t, expectedAppHash, appHash) } // DeterministicNamespace returns a deterministic namespace -func DeterministicNamespace() appns.Namespace { +func Namespace() appns.Namespace { return appns.Namespace{ Version: 0, ID: []byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 37, 67, 154, 200, 228, 130, 74, 147, 162, 11}, From 7471b9a97d4e975341131cdf55c65e0793fe46ea Mon Sep 17 00:00:00 2001 From: Nina Barbakadze Date: Fri, 7 Jun 2024 18:00:03 +0200 Subject: [PATCH 12/19] style: lint --- ...ism_test.go => consistent_apphash_test.go} | 29 +++++++++---------- 1 file changed, 13 insertions(+), 16 deletions(-) rename app/test/{non_determinism_test.go => consistent_apphash_test.go} (90%) diff --git a/app/test/non_determinism_test.go b/app/test/consistent_apphash_test.go similarity index 90% rename from app/test/non_determinism_test.go rename to app/test/consistent_apphash_test.go index 636b92985f..03279d1d1f 100644 --- a/app/test/non_determinism_test.go +++ b/app/test/consistent_apphash_test.go @@ -7,22 +7,18 @@ import ( "github.com/celestiaorg/celestia-app/v2/app" "github.com/celestiaorg/celestia-app/v2/app/encoding" "github.com/celestiaorg/celestia-app/v2/pkg/appconsts" + "github.com/celestiaorg/celestia-app/v2/pkg/user" testutil "github.com/celestiaorg/celestia-app/v2/test/util" "github.com/celestiaorg/celestia-app/v2/test/util/blobfactory" - - // "github.com/celestiaorg/celestia-app/v2/test/util/blobfactory" - // "github.com/celestiaorg/celestia-app/v2/test/util/testfactory" "github.com/celestiaorg/go-square/blob" appns "github.com/celestiaorg/go-square/namespace" "github.com/cosmos/cosmos-sdk/codec" hd "github.com/cosmos/cosmos-sdk/crypto/hd" keyring "github.com/cosmos/cosmos-sdk/crypto/keyring" "github.com/cosmos/cosmos-sdk/crypto/types" - "github.com/stretchr/testify/require" - - "github.com/celestiaorg/celestia-app/v2/pkg/user" sdk "github.com/cosmos/cosmos-sdk/types" banktypes "github.com/cosmos/cosmos-sdk/x/bank/types" + "github.com/stretchr/testify/require" abci "github.com/tendermint/tendermint/abci/types" tmproto "github.com/tendermint/tendermint/proto/tendermint/types" "github.com/tendermint/tendermint/proto/tendermint/version" @@ -39,13 +35,14 @@ type blobTxStruct struct { txOptions []user.TxOption } -// TestConsistentAppHash executes transactions, -// produces an app hash and compares it with the app hash produced by v1.x. -// testApp is running v1. +// TestConsistentAppHash executes a set of txs, generates an app hash, +// and compares it against a previously generated hash from the same set of transactions. +// App hashes across different commits should be consistent. func TestConsistentAppHash(t *testing.T) { - // expectedAppHash := []byte{100, 237, 125, 126, 116, 10, 189, 82, 156, 116, 176, 136, 169, 92, 185, 12, 72, 134, 254, 175, 234, 13, 159, 90, 139, 192, 190, 248, 67, 9, 32, 217} + // Expected app hash produced by v1.x - TODO: link to the test producing the hash + expectedAppHash := []byte{9, 208, 117, 101, 108, 61, 146, 58, 26, 190, 199, 124, 76, 178, 84, 74, 54, 159, 76, 187, 2, 169, 128, 87, 70, 78, 8, 192, 28, 144, 116, 117} - // Initialize testApp + // Initialize testApp testApp := testutil.NewTestApp() enc := encoding.MakeConfig(app.ModuleEncodingRegisters...) @@ -69,7 +66,7 @@ func TestConsistentAppHash(t *testing.T) { accountInfos := queryAccountInfo(testApp, accountNames, kr) // Create accounts for the signer - var accounts []*user.Account + accounts := make([]*user.Account, 0, len(accountInfos)) for i, accountInfo := range accountInfos { account := user.NewAccount(accountNames[i], accountInfo.AccountNum, accountInfo.Sequence) accounts = append(accounts, account) @@ -86,7 +83,8 @@ func TestConsistentAppHash(t *testing.T) { sdkMsgs: []sdk.Msg{ banktypes.NewMsgSend(signer.Account(accountNames[0]).Address(), signer.Account(accountNames[1]).Address(), - amount)}, + amount), + }, txOptions: blobfactory.DefaultTxOpts(), } @@ -130,10 +128,9 @@ func TestConsistentAppHash(t *testing.T) { // Get the app hash appHash := testApp.LastCommitID().Hash - fmt.Println(appHash) - // Require that the app hash is equal to the app hash produced by v1.x - // require.Equal(t, expectedAppHash, appHash) + // Require that the app hash is equal to the app hash produced on a different commit + require.Equal(t, expectedAppHash, appHash) } // DeterministicNamespace returns a deterministic namespace From c32926aa91715abebdfa16b2a78c5c40e22a1d91 Mon Sep 17 00:00:00 2001 From: Nina Barbakadze Date: Fri, 7 Jun 2024 18:04:34 +0200 Subject: [PATCH 13/19] chore: revert unnecessary changes --- app/test/prepare_proposal_test.go | 4 +--- app/test/process_proposal_test.go | 1 - test/util/blobfactory/payforblob_factory.go | 3 +-- test/util/direct_tx_gen.go | 4 +--- 4 files changed, 3 insertions(+), 9 deletions(-) diff --git a/app/test/prepare_proposal_test.go b/app/test/prepare_proposal_test.go index 2da3d79286..04b925e365 100644 --- a/app/test/prepare_proposal_test.go +++ b/app/test/prepare_proposal_test.go @@ -40,7 +40,6 @@ func TestPrepareProposalPutsPFBsAtEnd(t *testing.T) { testfactory.Repeat([]*blob.Blob{ blob.New(appns.RandomBlobNamespace(), []byte{1}, appconsts.DefaultShareVersion), }, numBlobTxs), - app.DefaultConsensusParams().Version.AppVersion, ) normalTxs := testutil.SendTxsWithAccounts( @@ -98,7 +97,6 @@ func TestPrepareProposalFiltering(t *testing.T) { testfactory.RandomBlobNamespaces(tmrand.NewRand(), 3), [][]int{{100}, {1000}, {420}}, ), - app.DefaultConsensusParams().Version.AppVersion, ) // create 3 MsgSend transactions that are signed with valid account numbers @@ -138,7 +136,7 @@ func TestPrepareProposalFiltering(t *testing.T) { nilAccount := "carmon san diego" _, _, err := kr.NewMnemonic(nilAccount, keyring.English, "", "", hd.Secp256k1) require.NoError(t, err) - noAccountTx := []byte(testutil.SendTxWithManualSequence(t, encConf.TxConfig, kr, nilAccount, accounts[0], 1000, "", 0, app.DefaultConsensusParams().Version.AppVersion, 6)) + noAccountTx := []byte(testutil.SendTxWithManualSequence(t, encConf.TxConfig, kr, nilAccount, accounts[0], 1000, "", 0, 6)) type test struct { name string diff --git a/app/test/process_proposal_test.go b/app/test/process_proposal_test.go index 9acaec6d99..fd52936322 100644 --- a/app/test/process_proposal_test.go +++ b/app/test/process_proposal_test.go @@ -47,7 +47,6 @@ func TestProcessProposal(t *testing.T) { testfactory.RandomBlobNamespaces(tmrand.NewRand(), 4), [][]int{{100}, {1000}, {420}, {300}}, ), - app.DefaultConsensusParams().Version.AppVersion, ) // create 3 MsgSend transactions that are signed with valid account numbers diff --git a/test/util/blobfactory/payforblob_factory.go b/test/util/blobfactory/payforblob_factory.go index 0db4cbca79..9860999940 100644 --- a/test/util/blobfactory/payforblob_factory.go +++ b/test/util/blobfactory/payforblob_factory.go @@ -222,13 +222,12 @@ func ManyMultiBlobTx( accounts []string, accInfos []AccountInfo, blobs [][]*blob.Blob, - appVersion uint64, ) [][]byte { t.Helper() txs := make([][]byte, len(accounts)) opts := DefaultTxOpts() for i, acc := range accounts { - signer, err := user.NewSigner(kr, enc, chainid, appVersion, user.NewAccount(acc, accInfos[i].AccountNum, accInfos[i].Sequence)) + signer, err := user.NewSigner(kr, enc, chainid, appconsts.LatestVersion, user.NewAccount(acc, accInfos[i].AccountNum, accInfos[i].Sequence)) require.NoError(t, err) txs[i], _, err = signer.CreatePayForBlobs(acc, blobs[i], opts...) require.NoError(t, err) diff --git a/test/util/direct_tx_gen.go b/test/util/direct_tx_gen.go index 902dc1baf5..ef0b47c24b 100644 --- a/test/util/direct_tx_gen.go +++ b/test/util/direct_tx_gen.go @@ -192,7 +192,6 @@ func SendTxsWithAccounts( chainid, acc.GetSequence(), acc.GetAccountNumber(), - appconsts.LatestVersion, opts..., ) } @@ -210,11 +209,10 @@ func SendTxWithManualSequence( amount uint64, chainid string, sequence, accountNum uint64, - appVersion uint64, opts ...user.TxOption, ) coretypes.Tx { fromAddr, toAddr := getAddress(fromAcc, kr), getAddress(toAcc, kr) - signer, err := user.NewSigner(kr, cfg, chainid, appVersion, user.NewAccount(fromAcc, accountNum, sequence)) + signer, err := user.NewSigner(kr, cfg, chainid, appconsts.LatestVersion, user.NewAccount(fromAcc, accountNum, sequence)) require.NoError(t, err) msg := banktypes.NewMsgSend(fromAddr, toAddr, sdk.NewCoins(sdk.NewCoin(app.BondDenom, sdk.NewIntFromUint64(amount)))) From ae87db74f76e5d018de325852db8d58345b855db Mon Sep 17 00:00:00 2001 From: Nina Barbakadze Date: Fri, 7 Jun 2024 18:12:35 +0200 Subject: [PATCH 14/19] style: cleanup imports --- app/test/consistent_apphash_test.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/test/consistent_apphash_test.go b/app/test/consistent_apphash_test.go index 03279d1d1f..a10881d2a7 100644 --- a/app/test/consistent_apphash_test.go +++ b/app/test/consistent_apphash_test.go @@ -13,8 +13,8 @@ import ( "github.com/celestiaorg/go-square/blob" appns "github.com/celestiaorg/go-square/namespace" "github.com/cosmos/cosmos-sdk/codec" - hd "github.com/cosmos/cosmos-sdk/crypto/hd" - keyring "github.com/cosmos/cosmos-sdk/crypto/keyring" + "github.com/cosmos/cosmos-sdk/crypto/hd" + "github.com/cosmos/cosmos-sdk/crypto/keyring" "github.com/cosmos/cosmos-sdk/crypto/types" sdk "github.com/cosmos/cosmos-sdk/types" banktypes "github.com/cosmos/cosmos-sdk/x/bank/types" From 92be77ffe8c83eca05ad373d6c31b6c170ae04bc Mon Sep 17 00:00:00 2001 From: Nina Barbakadze Date: Mon, 10 Jun 2024 13:45:23 +0200 Subject: [PATCH 15/19] refactor: address nits --- app/test/consistent_apphash_test.go | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/app/test/consistent_apphash_test.go b/app/test/consistent_apphash_test.go index a10881d2a7..2759d3a1fc 100644 --- a/app/test/consistent_apphash_test.go +++ b/app/test/consistent_apphash_test.go @@ -24,12 +24,12 @@ import ( "github.com/tendermint/tendermint/proto/tendermint/version" ) -type sdkTxStruct struct { +type sdkTx struct { sdkMsgs []sdk.Msg txOptions []user.TxOption } -type blobTxStruct struct { +type blobTx struct { author string blobs []*blob.Blob txOptions []user.TxOption @@ -47,7 +47,7 @@ func TestConsistentAppHash(t *testing.T) { enc := encoding.MakeConfig(app.ModuleEncodingRegisters...) // Create deterministic keys - kr, pubKeys := DeterministicKeyRing(enc.Codec) + kr, pubKeys := deterministicKeyRing(enc.Codec) recs, err := kr.List() require.NoError(t, err) @@ -79,7 +79,7 @@ func TestConsistentAppHash(t *testing.T) { amount := sdk.NewCoins(sdk.NewCoin(app.BondDenom, sdk.NewIntFromUint64(1000))) // Create an SDK Tx - sdkTx := sdkTxStruct{ + sdkTx := sdkTx{ sdkMsgs: []sdk.Msg{ banktypes.NewMsgSend(signer.Account(accountNames[0]).Address(), signer.Account(accountNames[1]).Address(), @@ -89,9 +89,9 @@ func TestConsistentAppHash(t *testing.T) { } // Create a Blob Tx - blobTx := blobTxStruct{ + blobTx := blobTx{ author: accountNames[2], - blobs: []*blob.Blob{blob.New(Namespace(), []byte{1}, appconsts.DefaultShareVersion)}, + blobs: []*blob.Blob{blob.New(fixedNamespace(), []byte{1}, appconsts.DefaultShareVersion)}, txOptions: blobfactory.DefaultTxOpts(), } @@ -133,16 +133,16 @@ func TestConsistentAppHash(t *testing.T) { require.Equal(t, expectedAppHash, appHash) } -// DeterministicNamespace returns a deterministic namespace -func Namespace() appns.Namespace { +// fixedNamespace returns a hardcoded namespace +func fixedNamespace() appns.Namespace { return appns.Namespace{ Version: 0, ID: []byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 37, 67, 154, 200, 228, 130, 74, 147, 162, 11}, } } -// DeterministicKeyRing returns a deterministic keyring and a list of deterministic public keys -func DeterministicKeyRing(cdc codec.Codec) (keyring.Keyring, []types.PubKey) { +// deterministicKeyRing returns a deterministic keyring and a list of deterministic public keys +func deterministicKeyRing(cdc codec.Codec) (keyring.Keyring, []types.PubKey) { mnemonics := []string{ "great myself congress genuine scale muscle view uncover pipe miracle sausage broccoli lonely swap table foam brand turtle comic gorilla firm mad grunt hazard", "cheap job month trigger flush cactus chest juice dolphin people limit crunch curious secret object beach shield snake hunt group sketch cousin puppy fox", From 7512217ad765d759e3ec3815d2ef0ceb1e6aa95f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?nina=20/=20=E1=83=9C=E1=83=98=E1=83=9C=E1=83=90?= Date: Mon, 10 Jun 2024 16:28:27 +0200 Subject: [PATCH 16/19] Update test/util/test_app.go Co-authored-by: Rootul P --- test/util/test_app.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/util/test_app.go b/test/util/test_app.go index 951d81e424..5805db9d8c 100644 --- a/test/util/test_app.go +++ b/test/util/test_app.go @@ -166,7 +166,7 @@ func ApplyGenesisState(testApp *app.App, pubKeys []cryptotypes.PubKey, balance i return gen.Keyring(), gen.Accounts(), nil } -// NewTestAppWithGenesisSet initializes a new app with a validator set and genesis accounts. +// NewTestAppWithGenesisSet initializes a new app with genesis accounts and returns the testApp, validator set and keyring. func NewTestAppWithGenesisSet(cparams *tmproto.ConsensusParams, genAccounts ...string) (*app.App, *tmtypes.ValidatorSet, keyring.Keyring) { testApp := NewTestApp() genesisState, valSet, kr := GenesisStateWithSingleValidator(testApp, genAccounts...) From d9e3a632dd5172f6d07eb58f05d989e5db5701df Mon Sep 17 00:00:00 2001 From: Nina Barbakadze Date: Mon, 10 Jun 2024 19:16:24 +0200 Subject: [PATCH 17/19] refactor: revert the api breaking change and rename a method --- test/util/test_app.go | 9 ++++----- test/util/testnode/utils.go | 9 +++++---- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/test/util/test_app.go b/test/util/test_app.go index 5805db9d8c..49c7b44368 100644 --- a/test/util/test_app.go +++ b/test/util/test_app.go @@ -94,8 +94,8 @@ func NewTestApp() *app.App { ) } -// ApplyGenesisState sets genesis on initialized testApp with the provided arguments. -func ApplyGenesisState(testApp *app.App, pubKeys []cryptotypes.PubKey, balance int64, cparams *tmproto.ConsensusParams) (keyring.Keyring, []genesis.Account, error) { +// 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) { // create genesis gen := genesis.NewDefaultGenesis(). WithChainID(ChainID). @@ -334,9 +334,8 @@ func GenesisStateWithSingleValidator(testApp *app.App, genAccounts ...string) (a }) // create a new keyring with the generated accounts - kr, addresses := testnode.NewKeyring(genAccounts...) - // fund the accounts - fundedBankAccs, fundedAuthAccs := testnode.FundKeyringAccounts(addresses) + kr, fundedBankAccs, fundedAuthAccs := testnode.FundKeyringAccounts(genAccounts...) + accs = append(accs, fundedAuthAccs...) balances = append(balances, fundedBankAccs...) diff --git a/test/util/testnode/utils.go b/test/util/testnode/utils.go index d8c4ebece2..bcdabcfa55 100644 --- a/test/util/testnode/utils.go +++ b/test/util/testnode/utils.go @@ -65,9 +65,10 @@ func RandomAddress() sdk.Address { return addresses[0] } -func FundKeyringAccounts(addresses []sdk.AccAddress) ([]banktypes.Balance, []authtypes.GenesisAccount) { - genAccounts := make([]authtypes.GenesisAccount, len(addresses)) - genBalances := make([]banktypes.Balance, len(addresses)) +func FundKeyringAccounts(accounts ...string) (keyring.Keyring, []banktypes.Balance, []authtypes.GenesisAccount) { + kr, addresses := NewKeyring(accounts...) + genAccounts := make([]authtypes.GenesisAccount, len(accounts)) + genBalances := make([]banktypes.Balance, len(accounts)) for i, addr := range addresses { balances := sdk.NewCoins( @@ -77,7 +78,7 @@ func FundKeyringAccounts(addresses []sdk.AccAddress) ([]banktypes.Balance, []aut genBalances[i] = banktypes.Balance{Address: addr.String(), Coins: balances.Sort()} genAccounts[i] = authtypes.NewBaseAccount(addr, nil, uint64(i), 0) } - return genBalances, genAccounts + return kr, genBalances, genAccounts } func GenerateAccounts(count int) []string { From 5e49042ce16447f647a7b7154cdd7f02596cda8b Mon Sep 17 00:00:00 2001 From: Nina Barbakadze Date: Mon, 10 Jun 2024 19:19:38 +0200 Subject: [PATCH 18/19] refactor: remove an outdated comment --- test/util/test_app.go | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/test/util/test_app.go b/test/util/test_app.go index 49c7b44368..1fc02f8d06 100644 --- a/test/util/test_app.go +++ b/test/util/test_app.go @@ -17,7 +17,7 @@ import ( "github.com/cosmos/cosmos-sdk/codec" codectypes "github.com/cosmos/cosmos-sdk/codec/types" cryptocodec "github.com/cosmos/cosmos-sdk/crypto/codec" - hd "github.com/cosmos/cosmos-sdk/crypto/hd" + "github.com/cosmos/cosmos-sdk/crypto/hd" "github.com/cosmos/cosmos-sdk/crypto/keyring" "github.com/cosmos/cosmos-sdk/crypto/keys/secp256k1" cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types" @@ -333,7 +333,6 @@ func GenesisStateWithSingleValidator(testApp *app.App, genAccounts ...string) (a Coins: sdk.NewCoins(sdk.NewCoin(app.BondDenom, sdk.NewInt(100000000000000))), }) - // create a new keyring with the generated accounts kr, fundedBankAccs, fundedAuthAccs := testnode.FundKeyringAccounts(genAccounts...) accs = append(accs, fundedAuthAccs...) From 8a6523e3aabe8f841c0c11b7945a79eeafcaf332 Mon Sep 17 00:00:00 2001 From: Nina Barbakadze Date: Mon, 10 Jun 2024 19:21:12 +0200 Subject: [PATCH 19/19] refactor: cleanup test --- app/test/consistent_apphash_test.go | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/app/test/consistent_apphash_test.go b/app/test/consistent_apphash_test.go index 2759d3a1fc..c3133ba98c 100644 --- a/app/test/consistent_apphash_test.go +++ b/app/test/consistent_apphash_test.go @@ -24,12 +24,12 @@ import ( "github.com/tendermint/tendermint/proto/tendermint/version" ) -type sdkTx struct { +type SdkTx struct { sdkMsgs []sdk.Msg txOptions []user.TxOption } -type blobTx struct { +type BlobTx struct { author string blobs []*blob.Blob txOptions []user.TxOption @@ -59,7 +59,7 @@ func TestConsistentAppHash(t *testing.T) { } // Apply genesis state to the app. - _, _, err = testutil.ApplyGenesisState(testApp, pubKeys, 1_000_000_000, app.DefaultInitialConsensusParams()) + _, _, err = testutil.SetupDeterministicGenesisState(testApp, pubKeys, 1_000_000_000, app.DefaultInitialConsensusParams()) require.NoError(t, err) // Query keyring account infos @@ -79,7 +79,7 @@ func TestConsistentAppHash(t *testing.T) { amount := sdk.NewCoins(sdk.NewCoin(app.BondDenom, sdk.NewIntFromUint64(1000))) // Create an SDK Tx - sdkTx := sdkTx{ + sdkTx := SdkTx{ sdkMsgs: []sdk.Msg{ banktypes.NewMsgSend(signer.Account(accountNames[0]).Address(), signer.Account(accountNames[1]).Address(), @@ -89,7 +89,7 @@ func TestConsistentAppHash(t *testing.T) { } // Create a Blob Tx - blobTx := blobTx{ + blobTx := BlobTx{ author: accountNames[2], blobs: []*blob.Blob{blob.New(fixedNamespace(), []byte{1}, appconsts.DefaultShareVersion)}, txOptions: blobfactory.DefaultTxOpts(),