github.com/pokt-network/tendermint@v0.32.11-0.20230426215212-59310158d3e9/state/helpers_test.go (about)

     1  package state_test
     2  
     3  import (
     4  	"bytes"
     5  	"fmt"
     6  	"time"
     7  
     8  	dbm "github.com/tendermint/tm-db"
     9  
    10  	abci "github.com/tendermint/tendermint/abci/types"
    11  	"github.com/tendermint/tendermint/crypto"
    12  	"github.com/tendermint/tendermint/crypto/ed25519"
    13  	tmrand "github.com/tendermint/tendermint/libs/rand"
    14  	"github.com/tendermint/tendermint/proxy"
    15  	sm "github.com/tendermint/tendermint/state"
    16  	"github.com/tendermint/tendermint/types"
    17  	tmtime "github.com/tendermint/tendermint/types/time"
    18  )
    19  
    20  type paramsChangeTestCase struct {
    21  	height int64
    22  	params types.ConsensusParams
    23  }
    24  
    25  // always returns true if asked if any evidence was already committed.
    26  type mockEvPoolAlwaysCommitted struct{}
    27  
    28  func (m mockEvPoolAlwaysCommitted) PendingEvidence(int64) []types.Evidence { return nil }
    29  func (m mockEvPoolAlwaysCommitted) AddEvidence(types.Evidence) error       { return nil }
    30  func (m mockEvPoolAlwaysCommitted) Update(*types.Block, sm.State)          {}
    31  func (m mockEvPoolAlwaysCommitted) IsCommitted(types.Evidence) bool        { return true }
    32  
    33  func newTestApp() proxy.AppConns {
    34  	app := &testApp{}
    35  	cc := proxy.NewLocalClientCreator(app)
    36  	return proxy.NewAppConns(cc)
    37  }
    38  
    39  func makeAndCommitGoodBlock(
    40  	state sm.State,
    41  	height int64,
    42  	lastCommit *types.Commit,
    43  	proposerAddr []byte,
    44  	blockExec *sm.BlockExecutor,
    45  	privVals map[string]types.PrivValidator,
    46  	evidence []types.Evidence) (sm.State, types.BlockID, *types.Commit, error) {
    47  	// A good block passes
    48  	state, blockID, err := makeAndApplyGoodBlock(state, height, lastCommit, proposerAddr, blockExec, evidence)
    49  	if err != nil {
    50  		return state, types.BlockID{}, nil, err
    51  	}
    52  
    53  	// Simulate a lastCommit for this block from all validators for the next height
    54  	commit, err := makeValidCommit(height, blockID, state.Validators, privVals)
    55  	if err != nil {
    56  		return state, types.BlockID{}, nil, err
    57  	}
    58  	return state, blockID, commit, nil
    59  }
    60  
    61  func makeAndApplyGoodBlock(state sm.State, height int64, lastCommit *types.Commit, proposerAddr []byte,
    62  	blockExec *sm.BlockExecutor, evidence []types.Evidence) (sm.State, types.BlockID, error) {
    63  	block, _ := state.MakeBlock(height, makeTxs(height), lastCommit, evidence, proposerAddr)
    64  	if err := blockExec.ValidateBlock(state, block); err != nil {
    65  		return state, types.BlockID{}, err
    66  	}
    67  	blockID := types.BlockID{Hash: block.Hash(),
    68  		PartsHeader: types.PartSetHeader{Total: 3, Hash: tmrand.Bytes(32)}}
    69  	state, _, err := blockExec.ApplyBlock(state, blockID, block)
    70  	if err != nil {
    71  		return state, types.BlockID{}, err
    72  	}
    73  	return state, blockID, nil
    74  }
    75  
    76  func makeValidCommit(
    77  	height int64,
    78  	blockID types.BlockID,
    79  	vals *types.ValidatorSet,
    80  	privVals map[string]types.PrivValidator,
    81  ) (*types.Commit, error) {
    82  	sigs := make([]*types.CommitSig, 0)
    83  	for i := 0; i < vals.Size(); i++ {
    84  		_, val := vals.GetByIndex(i)
    85  		vote, err := types.MakeVote(height, blockID, vals, privVals[val.Address.String()], chainID, time.Now())
    86  		if err != nil {
    87  			return nil, err
    88  		}
    89  		sigs = append(sigs, vote.CommitSig())
    90  	}
    91  	return types.NewCommit(blockID, sigs), nil
    92  }
    93  
    94  // make some bogus txs
    95  func makeTxs(height int64) (txs []types.Tx) {
    96  	for i := 0; i < nTxsPerBlock; i++ {
    97  		txs = append(txs, types.Tx([]byte{byte(height), byte(i)}))
    98  	}
    99  	return txs
   100  }
   101  
   102  func makeState(nVals, height int) (sm.State, dbm.DB, map[string]types.PrivValidator) {
   103  	vals := make([]types.GenesisValidator, nVals)
   104  	privVals := make(map[string]types.PrivValidator, nVals)
   105  	for i := 0; i < nVals; i++ {
   106  		secret := []byte(fmt.Sprintf("test%d", i))
   107  		pk := ed25519.GenPrivKeyFromSecret(secret)
   108  		valAddr := pk.PubKey().Address()
   109  		vals[i] = types.GenesisValidator{
   110  			Address: valAddr,
   111  			PubKey:  pk.PubKey(),
   112  			Power:   1000,
   113  			Name:    fmt.Sprintf("test%d", i),
   114  		}
   115  		privVals[valAddr.String()] = types.NewMockPVWithParams(pk, false, false)
   116  	}
   117  	s, _ := sm.MakeGenesisState(&types.GenesisDoc{
   118  		ChainID:    chainID,
   119  		Validators: vals,
   120  		AppHash:    nil,
   121  	})
   122  
   123  	stateDB := dbm.NewMemDB()
   124  	sm.SaveState(stateDB, s)
   125  
   126  	for i := 1; i < height; i++ {
   127  		s.LastBlockHeight++
   128  		s.LastValidators = s.Validators.Copy()
   129  		sm.SaveState(stateDB, s)
   130  	}
   131  	return s, stateDB, privVals
   132  }
   133  
   134  func makeBlock(state sm.State, height int64) *types.Block {
   135  	block, _ := state.MakeBlock(
   136  		height,
   137  		makeTxs(state.LastBlockHeight),
   138  		new(types.Commit),
   139  		nil,
   140  		state.Validators.GetProposer().Address,
   141  	)
   142  	return block
   143  }
   144  
   145  func genValSet(size int) *types.ValidatorSet {
   146  	vals := make([]*types.Validator, size)
   147  	for i := 0; i < size; i++ {
   148  		vals[i] = types.NewValidator(ed25519.GenPrivKey().PubKey(), 10)
   149  	}
   150  	return types.NewValidatorSet(vals)
   151  }
   152  
   153  func makeConsensusParams(
   154  	blockBytes, blockGas int64,
   155  	blockTimeIotaMs int64,
   156  	evidenceAge int64,
   157  ) types.ConsensusParams {
   158  	return types.ConsensusParams{
   159  		Block: types.BlockParams{
   160  			MaxBytes:   blockBytes,
   161  			MaxGas:     blockGas,
   162  			TimeIotaMs: blockTimeIotaMs,
   163  		},
   164  		Evidence: types.EvidenceParams{
   165  			MaxAge: evidenceAge,
   166  		},
   167  	}
   168  }
   169  
   170  func makeHeaderPartsResponsesValPubKeyChange(
   171  	state sm.State,
   172  	pubkey crypto.PubKey,
   173  ) (types.Header, types.BlockID, *sm.ABCIResponses) {
   174  
   175  	block := makeBlock(state, state.LastBlockHeight+1)
   176  	abciResponses := &sm.ABCIResponses{
   177  		EndBlock: &abci.ResponseEndBlock{ValidatorUpdates: nil},
   178  	}
   179  
   180  	// If the pubkey is new, remove the old and add the new.
   181  	_, val := state.NextValidators.GetByIndex(0)
   182  	if !bytes.Equal(pubkey.Bytes(), val.PubKey.Bytes()) {
   183  		abciResponses.EndBlock = &abci.ResponseEndBlock{
   184  			ValidatorUpdates: []abci.ValidatorUpdate{
   185  				types.TM2PB.NewValidatorUpdate(val.PubKey, 0),
   186  				types.TM2PB.NewValidatorUpdate(pubkey, 10),
   187  			},
   188  		}
   189  	}
   190  
   191  	return block.Header, types.BlockID{Hash: block.Hash(), PartsHeader: types.PartSetHeader{}}, abciResponses
   192  }
   193  
   194  func makeHeaderPartsResponsesValPowerChange(
   195  	state sm.State,
   196  	power int64,
   197  ) (types.Header, types.BlockID, *sm.ABCIResponses) {
   198  
   199  	block := makeBlock(state, state.LastBlockHeight+1)
   200  	abciResponses := &sm.ABCIResponses{
   201  		EndBlock: &abci.ResponseEndBlock{ValidatorUpdates: nil},
   202  	}
   203  
   204  	// If the pubkey is new, remove the old and add the new.
   205  	_, val := state.NextValidators.GetByIndex(0)
   206  	if val.VotingPower != power {
   207  		abciResponses.EndBlock = &abci.ResponseEndBlock{
   208  			ValidatorUpdates: []abci.ValidatorUpdate{
   209  				types.TM2PB.NewValidatorUpdate(val.PubKey, power),
   210  			},
   211  		}
   212  	}
   213  
   214  	return block.Header, types.BlockID{Hash: block.Hash(), PartsHeader: types.PartSetHeader{}}, abciResponses
   215  }
   216  
   217  func makeHeaderPartsResponsesParams(
   218  	state sm.State,
   219  	params types.ConsensusParams,
   220  ) (types.Header, types.BlockID, *sm.ABCIResponses) {
   221  
   222  	block := makeBlock(state, state.LastBlockHeight+1)
   223  	abciResponses := &sm.ABCIResponses{
   224  		EndBlock: &abci.ResponseEndBlock{ConsensusParamUpdates: types.TM2PB.ConsensusParams(&params)},
   225  	}
   226  	return block.Header, types.BlockID{Hash: block.Hash(), PartsHeader: types.PartSetHeader{}}, abciResponses
   227  }
   228  
   229  func randomGenesisDoc() *types.GenesisDoc {
   230  	pubkey := ed25519.GenPrivKey().PubKey()
   231  	return &types.GenesisDoc{
   232  		GenesisTime: tmtime.Now(),
   233  		ChainID:     "abc",
   234  		Validators: []types.GenesisValidator{
   235  			{
   236  				Address: pubkey.Address(),
   237  				PubKey:  pubkey,
   238  				Power:   10,
   239  				Name:    "myval",
   240  			},
   241  		},
   242  		ConsensusParams: types.DefaultConsensusParams(),
   243  	}
   244  }
   245  
   246  //----------------------------------------------------------------------------
   247  
   248  type testApp struct {
   249  	abci.BaseApplication
   250  
   251  	CommitVotes         []abci.VoteInfo
   252  	ByzantineValidators []abci.Evidence
   253  	ValidatorUpdates    []abci.ValidatorUpdate
   254  }
   255  
   256  var _ abci.Application = (*testApp)(nil)
   257  
   258  func (app *testApp) Info(req abci.RequestInfo) (resInfo abci.ResponseInfo) {
   259  	return abci.ResponseInfo{}
   260  }
   261  
   262  func (app *testApp) BeginBlock(req abci.RequestBeginBlock) abci.ResponseBeginBlock {
   263  	app.CommitVotes = req.LastCommitInfo.Votes
   264  	app.ByzantineValidators = req.ByzantineValidators
   265  	return abci.ResponseBeginBlock{}
   266  }
   267  
   268  func (app *testApp) EndBlock(req abci.RequestEndBlock) abci.ResponseEndBlock {
   269  	return abci.ResponseEndBlock{ValidatorUpdates: app.ValidatorUpdates}
   270  }
   271  
   272  func (app *testApp) DeliverTx(req abci.RequestDeliverTx) abci.ResponseDeliverTx {
   273  	return abci.ResponseDeliverTx{Events: []abci.Event{}}
   274  }
   275  
   276  func (app *testApp) CheckTx(req abci.RequestCheckTx) abci.ResponseCheckTx {
   277  	return abci.ResponseCheckTx{}
   278  }
   279  
   280  func (app *testApp) Commit() abci.ResponseCommit {
   281  	return abci.ResponseCommit{}
   282  }
   283  
   284  func (app *testApp) Query(reqQuery abci.RequestQuery) (resQuery abci.ResponseQuery) {
   285  	return
   286  }