Skip to content

Commit

Permalink
test(v1.x): expand apphash test with all state machine msgs
Browse files Browse the repository at this point in the history
  • Loading branch information
ninabarbakadze committed Jul 5, 2024
1 parent 8236eda commit 8f3e0b7
Show file tree
Hide file tree
Showing 5 changed files with 127 additions and 45 deletions.
26 changes: 19 additions & 7 deletions app/consistent_apphash_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,12 @@ import (
abci "github.com/tendermint/tendermint/abci/types"
tmproto "github.com/tendermint/tendermint/proto/tendermint/types"
"github.com/tendermint/tendermint/proto/tendermint/version"
coretypes "github.com/tendermint/tendermint/types"
)

type BlobTx struct {
author string
blobs []*blob.Blob
blobs []*tmproto.Blob
txOptions []user.TxOption
}

Expand Down Expand Up @@ -74,7 +75,7 @@ func TestConsistentAppHash(t *testing.T) {
accounts := createAccounts(accountInfos, accountNames)

// Create a signer with accounts
signer, err := user.NewSigner(kr, enc.TxConfig, testutil.ChainID, app.DefaultInitialVersion, accounts...)
signer, err := user.NewTxSigner(kr, enc.TxConfig, testutil.ChainID, appconsts.LatestVersion, accounts...)
require.NoError(t, err)

// ------------ Genesis Validator Accounts ------------
Expand All @@ -92,7 +93,7 @@ func TestConsistentAppHash(t *testing.T) {
valAccounts := createAccounts(valAccountInfos, valAccountNames)

// Create a signer with validator accounts
valSigner, err := user.NewSigner(valKeyRing, enc.TxConfig, testutil.ChainID, app.DefaultInitialVersion, valAccounts...)
valSigner, err := user.NewTxSigner(valKeyRing, enc.TxConfig, testutil.ChainID, appconsts.LatestVersion, valAccounts...)
require.NoError(t, err)

// ----------- Create SDK Messages ------------
Expand Down Expand Up @@ -271,7 +272,7 @@ func TestConsistentAppHash(t *testing.T) {
// Create a Blob Tx
blobTx := BlobTx{
author: accountNames[1],
blobs: []*blob.Blob{blob.New(fixedNamespace(), []byte{1}, appconsts.DefaultShareVersion)},
blobs: []*tmproto.Blob{New(fixedNamespace(), []byte{1}, appconsts.DefaultShareVersion)},
txOptions: blobfactory.DefaultTxOpts(),
}
rawBlobTx, _, err := signer.CreatePayForBlobs(blobTx.author, blobTx.blobs, blobTx.txOptions...)
Expand All @@ -290,10 +291,11 @@ func TestConsistentAppHash(t *testing.T) {
require.NoError(t, err)

// Execute the final block and get the data hash alongside the final app hash
_, finalAppHash, err := executeTxs(testApp, []byte{}, validatorRawTxs, abciValidators, secondAppHash)
dataHash, finalAppHash, err := executeTxs(testApp, []byte{}, validatorRawTxs, abciValidators, secondAppHash)
require.NoError(t, err)

fmt.Println(finalAppHash, "APP HASH")
fmt.Println(dataHash, "DATA HASH")

// Require that the app hash is equal to the app hash produced on a different commit
// require.Equal(t, expectedAppHash, appHash)
Expand Down Expand Up @@ -340,7 +342,7 @@ func deterministicKeyRing(cdc codec.Codec) (keyring.Keyring, []types.PubKey) {

// processSdkMessages takes a list of sdk messages, forms transactions, signs them
// and returns a list of raw transactions
func processSdkMessages(signer *user.Signer, sdkMessages []sdk.Msg) ([][]byte, error) {
func processSdkMessages(signer *user.TxSigner, sdkMessages []sdk.Msg) ([][]byte, error) {
rawSdkTxs := make([][]byte, 0, len(sdkMessages))
for _, msg := range sdkMessages {
rawSdkTx, err := signer.CreateTx([]sdk.Msg{msg}, blobfactory.DefaultTxOpts()...)
Expand Down Expand Up @@ -429,7 +431,7 @@ func executeTxs(testApp *app.App, rawBlobTx []byte, rawSdkTxs [][]byte, validato
// Deliver Blob Txs
if len(rawBlobTx) != 0 {
// Deliver Blob Tx
blob, isBlobTx := blob.UnmarshalBlobTx(rawBlobTx)
blob, isBlobTx := coretypes.UnmarshalBlobTx(rawBlobTx)
if !isBlobTx {
return nil, nil, fmt.Errorf("Not a valid BlobTx")
}
Expand Down Expand Up @@ -502,3 +504,13 @@ func convertToABCIValidators(genValidators []stakingtypes.Validator) ([]abci.Val
}
return abciValidators, nil
}

// New creates a new tmproto.Blob from the provided data
func New(ns appns.Namespace, blob []byte, shareVersion uint8) *tmproto.Blob {
return &tmproto.Blob{
NamespaceId: ns.ID,
Data: blob,
ShareVersion: uint32(shareVersion),
NamespaceVersion: uint32(ns.Version),
}
}
28 changes: 28 additions & 0 deletions test/util/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ import (
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
gethcommon "github.com/ethereum/go-ethereum/common"
"github.com/stretchr/testify/require"
tmed "github.com/tendermint/tendermint/crypto/ed25519"
"github.com/tendermint/tendermint/libs/log"
tmproto "github.com/tendermint/tendermint/proto/tendermint/types"
tmversion "github.com/tendermint/tendermint/proto/tendermint/version"
Expand All @@ -58,6 +59,33 @@ var (
MinCommissionRate: sdk.NewDecWithPrec(0, 0),
}

// HardcodedConsensusPrivKeys
FixedConsensusPrivKeys = []tmed.PrivKey{
tmed.GenPrivKeyFromSecret([]byte("12345678901234567890123456389012")),
tmed.GenPrivKeyFromSecret([]byte("12345678901234567890123456389013")),
tmed.GenPrivKeyFromSecret([]byte("12345678901234567890123456389014")),
tmed.GenPrivKeyFromSecret([]byte("12345678901234567890123456389015")),
tmed.GenPrivKeyFromSecret([]byte("12345678901234567890123456389016")),
}

FixedNetWorkPrivKeys = []tmed.PrivKey{
tmed.GenPrivKeyFromSecret([]byte("12345678901234567890123456786012")),
tmed.GenPrivKeyFromSecret([]byte("12345678901234567890123456786013")),
tmed.GenPrivKeyFromSecret([]byte("12345678901234567890123456786014")),
tmed.GenPrivKeyFromSecret([]byte("12345678901234567890123456786015")),
tmed.GenPrivKeyFromSecret([]byte("12345678901234567890123456786016")),
}

// FixedMnemonics is a set of fixed mnemonics for testing.
// Account names are: validator1, validator2, validator3, validator4, validator5
FixedMnemonics = []string{
"body world north giggle crop reduce height copper damp next verify orphan lens loan adjust inform utility theory now ranch motion opinion crowd fun",
"body champion street fat bone above office guess waste vivid gift around approve elevator depth fiber alarm usual skirt like organ space antique silk",
"cheap alpha render punch clap prize duty drive steel situate person radar smooth elegant over chronic wait danger thumb soft letter spatial acquire rough",
"outdoor ramp suspect office disagree world attend vanish small wish capable fall wall soon damp session emotion chest toss viable meat host clerk truth",
"ability evidence casino cram weasel chest brush bridge sister blur onion found glad own mansion amateur expect force fun dragon famous alien appear open",
}

// ConsPrivKeys generate ed25519 ConsPrivKeys to be used for validator operator keys
ConsPrivKeys = []ccrypto.PrivKey{
ed25519.GenPrivKey(),
Expand Down
11 changes: 11 additions & 0 deletions test/util/genesis/modifier.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
govtypes "github.com/cosmos/cosmos-sdk/x/gov/types"
v1 "github.com/cosmos/cosmos-sdk/x/gov/types/v1"
slashingtypes "github.com/cosmos/cosmos-sdk/x/slashing/types"
)

// Modifier allows for arbitrary changes to be made on the genesis state
Expand All @@ -30,6 +31,16 @@ func SetBlobParams(codec codec.Codec, params blobtypes.Params) Modifier {
}
}

// SetSlashingParams will set the provided slashing params as genesis state.
func SetSlashingParams(codec codec.Codec, parans slashingtypes.Params) Modifier {
return func(state map[string]json.RawMessage) map[string]json.RawMessage {
slashingGenState := slashingtypes.DefaultGenesisState()
slashingGenState.Params = parans
state[slashingtypes.ModuleName] = codec.MustMarshalJSON(slashingGenState)
return state
}
}

// ImmediateProposals sets the thresholds for getting a gov proposal to very low
// levels.
func ImmediateProposals(codec codec.Codec) Modifier {
Expand Down
79 changes: 41 additions & 38 deletions test/util/test_app.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,17 @@ import (
"github.com/celestiaorg/celestia-app/test/util/genesis"
"github.com/cosmos/cosmos-sdk/crypto/hd"
cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types"
slashingtypes "github.com/cosmos/cosmos-sdk/x/slashing/types"
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
"github.com/tendermint/tendermint/crypto/ed25519"
tmversion "github.com/tendermint/tendermint/proto/tendermint/version"
)

const (
ChainID = "test-app"
)

var GenesisTime = time.Date(2023, 1, 1, 1, 1, 1, 1, time.UTC).UTC()

// Get flags every time the simulator is run
func init() {
simapp.GetSimulatorFlags()
Expand Down Expand Up @@ -75,11 +77,14 @@ func NewTestApp() *app.App {

// SetupDeterministicGenesisState sets genesis on initialized testApp with the provided arguments.
func SetupDeterministicGenesisState(testApp *app.App, pubKeys []cryptotypes.PubKey, balance int64, cparams *tmproto.ConsensusParams) (keyring.Keyring, []genesis.Account, error) {
slashingParams := slashingtypes.NewParams(2, sdk.OneDec(), time.Minute, sdk.OneDec(), sdk.OneDec())

// Create genesis
gen := genesis.NewDefaultGenesis().
WithChainID(ChainID).
WithConsensusParams(cparams).
WithGenesisTime(time.Date(2023, 1, 1, 1, 1, 1, 1, time.UTC).UTC())
WithModifiers(genesis.SetSlashingParams(testApp.AppCodec(), slashingParams)).
WithGenesisTime(GenesisTime)

// Add accounts to genesis
for _, pk := range pubKeys {
Expand All @@ -93,7 +98,7 @@ func SetupDeterministicGenesisState(testApp *app.App, pubKeys []cryptotypes.PubK
}

// Add validator to genesis
err := AddDeterministicValidatorToGenesis(gen)
err := AddDeterministicValidatorsToGenesis(gen)
if err != nil {
return nil, nil, fmt.Errorf("failed to add validator: %w", err)
}
Expand Down Expand Up @@ -210,46 +215,44 @@ func SetupTestAppWithGenesisValSet(cparams *tmproto.ConsensusParams, genAccounts
return testApp, kr
}

// AddDeterministicValidatorToGenesis adds a single deterministic validator to the genesis.
func AddDeterministicValidatorToGenesis(g *genesis.Genesis) error {
// Hardcoded keys for deterministic account creation
mnemo := "body world north giggle crop reduce height copper damp next verify orphan lens loan adjust inform utility theory now ranch motion opinion crowd fun"
consensusKey := ed25519.GenPrivKeyFromSecret([]byte("12345678901234567890123456389012"))
networkKey := ed25519.GenPrivKeyFromSecret([]byte("12345678901234567890123456786012"))

val := genesis.Validator{
KeyringAccount: genesis.KeyringAccount{
Name: "validator1",
InitialTokens: 1_000_000_000,
},
Stake: 1_000_000,
ConsensusKey: consensusKey,
NetworkKey: networkKey,
}

// Initialize the validator's genesis account in the keyring
rec, err := g.Keyring().NewAccount(val.Name, mnemo, "", "", hd.Secp256k1)
if err != nil {
return fmt.Errorf("failed to create account: %w", err)
}
func AddDeterministicValidatorsToGenesis(g *genesis.Genesis) error {
for i := range FixedMnemonics {
val := genesis.Validator{
KeyringAccount: genesis.KeyringAccount{
Name: "validator" + fmt.Sprint(i),
InitialTokens: 5_000_000_000,
},
Stake: 1_000_000_000,
ConsensusKey: FixedConsensusPrivKeys[i],
NetworkKey: FixedNetWorkPrivKeys[i],
}
// initialize the validator's genesis account in the keyring
rec, err := g.Keyring().NewAccount(val.Name, FixedMnemonics[i], "", "", hd.Secp256k1)
if err != nil {
return fmt.Errorf("failed to create account: %w", err)
}

validatorPubKey, err := rec.GetPubKey()
if err != nil {
return fmt.Errorf("failed to get pubkey: %w", err)
}
validatorPubKey, err := rec.GetPubKey()
if err != nil {
return fmt.Errorf("failed to get pubkey: %w", err)
}

// Make account from keyring account
account := genesis.Account{
PubKey: validatorPubKey,
Balance: val.KeyringAccount.InitialTokens,
}
// make account from keyring account
account := genesis.Account{
PubKey: validatorPubKey,
Balance: val.KeyringAccount.InitialTokens,
}

// Add the validator's account to the genesis
if err := g.AddAccount(account); err != nil {
return fmt.Errorf("failed to add account: %w", err)
// add the validator's account to the genesis
if err := g.AddAccount(account); err != nil {
return fmt.Errorf("failed to add account: %w", err)
}
if err := g.AddValidator(val); err != nil {
return fmt.Errorf("failed to add validator: %w", err)
}
}

return g.AddValidator(val)
return nil
}

// AddGenesisAccount mimics the cli addGenesisAccount command, providing an
Expand Down
28 changes: 28 additions & 0 deletions test/util/testfactory/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,3 +62,31 @@ func GetAddress(keys keyring.Keyring, account string) sdk.AccAddress {
}
return addr
}

func GetAddresses(keys keyring.Keyring) []sdk.AccAddress {
recs, err := keys.List()
if err != nil {
panic(err)
}
addresses := make([]sdk.AccAddress, 0, len(recs))
for _, rec := range recs {
address, err := rec.GetAddress()
if err != nil {
panic(err)
}
addresses = append(addresses, address)
}
return addresses
}

func GetAccountNames(keys keyring.Keyring) []string {
recs, err := keys.List()
if err != nil {
panic(err)
}
names := make([]string, 0, len(recs))
for _, rec := range recs {
names = append(names, rec.Name)
}
return names
}

0 comments on commit 8f3e0b7

Please sign in to comment.