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