You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

678 lines
20 KiB

  1. package consensus
  2. import (
  3. "bytes"
  4. "context"
  5. "fmt"
  6. "io"
  7. "io/ioutil"
  8. "os"
  9. "path"
  10. "runtime"
  11. "testing"
  12. "time"
  13. "github.com/stretchr/testify/assert"
  14. "github.com/stretchr/testify/require"
  15. "github.com/tendermint/tendermint/abci/example/kvstore"
  16. abci "github.com/tendermint/tendermint/abci/types"
  17. "github.com/tendermint/tendermint/crypto"
  18. auto "github.com/tendermint/tendermint/libs/autofile"
  19. dbm "github.com/tendermint/tendermint/libs/db"
  20. "github.com/tendermint/tendermint/version"
  21. cfg "github.com/tendermint/tendermint/config"
  22. "github.com/tendermint/tendermint/libs/log"
  23. "github.com/tendermint/tendermint/privval"
  24. "github.com/tendermint/tendermint/proxy"
  25. sm "github.com/tendermint/tendermint/state"
  26. "github.com/tendermint/tendermint/types"
  27. )
  28. var consensusReplayConfig *cfg.Config
  29. func init() {
  30. consensusReplayConfig = ResetConfig("consensus_replay_test")
  31. }
  32. // These tests ensure we can always recover from failure at any part of the consensus process.
  33. // There are two general failure scenarios: failure during consensus, and failure while applying the block.
  34. // Only the latter interacts with the app and store,
  35. // but the former has to deal with restrictions on re-use of priv_validator keys.
  36. // The `WAL Tests` are for failures during the consensus;
  37. // the `Handshake Tests` are for failures in applying the block.
  38. // With the help of the WAL, we can recover from it all!
  39. //------------------------------------------------------------------------------------------
  40. // WAL Tests
  41. // TODO: It would be better to verify explicitly which states we can recover from without the wal
  42. // and which ones we need the wal for - then we'd also be able to only flush the
  43. // wal writer when we need to, instead of with every message.
  44. func startNewConsensusStateAndWaitForBlock(t *testing.T, lastBlockHeight int64, blockDB dbm.DB, stateDB dbm.DB) {
  45. logger := log.TestingLogger()
  46. state, _ := sm.LoadStateFromDBOrGenesisFile(stateDB, consensusReplayConfig.GenesisFile())
  47. privValidator := loadPrivValidator(consensusReplayConfig)
  48. cs := newConsensusStateWithConfigAndBlockStore(consensusReplayConfig, state, privValidator, kvstore.NewKVStoreApplication(), blockDB)
  49. cs.SetLogger(logger)
  50. bytes, _ := ioutil.ReadFile(cs.config.WalFile())
  51. // fmt.Printf("====== WAL: \n\r%s\n", bytes)
  52. t.Logf("====== WAL: \n\r%X\n", bytes)
  53. err := cs.Start()
  54. require.NoError(t, err)
  55. defer cs.Stop()
  56. // This is just a signal that we haven't halted; its not something contained
  57. // in the WAL itself. Assuming the consensus state is running, replay of any
  58. // WAL, including the empty one, should eventually be followed by a new
  59. // block, or else something is wrong.
  60. newBlockCh := make(chan interface{}, 1)
  61. err = cs.eventBus.Subscribe(context.Background(), testSubscriber, types.EventQueryNewBlock, newBlockCh)
  62. require.NoError(t, err)
  63. select {
  64. case <-newBlockCh:
  65. case <-time.After(60 * time.Second):
  66. t.Fatalf("Timed out waiting for new block (see trace above)")
  67. }
  68. }
  69. func sendTxs(cs *ConsensusState, ctx context.Context) {
  70. for i := 0; i < 256; i++ {
  71. select {
  72. case <-ctx.Done():
  73. return
  74. default:
  75. tx := []byte{byte(i)}
  76. cs.mempool.CheckTx(tx, nil)
  77. i++
  78. }
  79. }
  80. }
  81. // TestWALCrash uses crashing WAL to test we can recover from any WAL failure.
  82. func TestWALCrash(t *testing.T) {
  83. testCases := []struct {
  84. name string
  85. initFn func(dbm.DB, *ConsensusState, context.Context)
  86. heightToStop int64
  87. }{
  88. {"empty block",
  89. func(stateDB dbm.DB, cs *ConsensusState, ctx context.Context) {},
  90. 1},
  91. {"many non-empty blocks",
  92. func(stateDB dbm.DB, cs *ConsensusState, ctx context.Context) {
  93. go sendTxs(cs, ctx)
  94. },
  95. 3},
  96. }
  97. for _, tc := range testCases {
  98. t.Run(tc.name, func(t *testing.T) {
  99. crashWALandCheckLiveness(t, tc.initFn, tc.heightToStop)
  100. })
  101. }
  102. }
  103. func crashWALandCheckLiveness(t *testing.T, initFn func(dbm.DB, *ConsensusState, context.Context), heightToStop int64) {
  104. walPaniced := make(chan error)
  105. crashingWal := &crashingWAL{panicCh: walPaniced, heightToStop: heightToStop}
  106. i := 1
  107. LOOP:
  108. for {
  109. // fmt.Printf("====== LOOP %d\n", i)
  110. t.Logf("====== LOOP %d\n", i)
  111. // create consensus state from a clean slate
  112. logger := log.NewNopLogger()
  113. stateDB := dbm.NewMemDB()
  114. state, _ := sm.MakeGenesisStateFromFile(consensusReplayConfig.GenesisFile())
  115. privValidator := loadPrivValidator(consensusReplayConfig)
  116. blockDB := dbm.NewMemDB()
  117. cs := newConsensusStateWithConfigAndBlockStore(consensusReplayConfig, state, privValidator, kvstore.NewKVStoreApplication(), blockDB)
  118. cs.SetLogger(logger)
  119. // start sending transactions
  120. ctx, cancel := context.WithCancel(context.Background())
  121. initFn(stateDB, cs, ctx)
  122. // clean up WAL file from the previous iteration
  123. walFile := cs.config.WalFile()
  124. os.Remove(walFile)
  125. // set crashing WAL
  126. csWal, err := cs.OpenWAL(walFile)
  127. require.NoError(t, err)
  128. crashingWal.next = csWal
  129. // reset the message counter
  130. crashingWal.msgIndex = 1
  131. cs.wal = crashingWal
  132. // start consensus state
  133. err = cs.Start()
  134. require.NoError(t, err)
  135. i++
  136. select {
  137. case err := <-walPaniced:
  138. t.Logf("WAL paniced: %v", err)
  139. // make sure we can make blocks after a crash
  140. startNewConsensusStateAndWaitForBlock(t, cs.Height, blockDB, stateDB)
  141. // stop consensus state and transactions sender (initFn)
  142. cs.Stop()
  143. cancel()
  144. // if we reached the required height, exit
  145. if _, ok := err.(ReachedHeightToStopError); ok {
  146. break LOOP
  147. }
  148. case <-time.After(10 * time.Second):
  149. t.Fatal("WAL did not panic for 10 seconds (check the log)")
  150. }
  151. }
  152. }
  153. // crashingWAL is a WAL which crashes or rather simulates a crash during Save
  154. // (before and after). It remembers a message for which we last panicked
  155. // (lastPanicedForMsgIndex), so we don't panic for it in subsequent iterations.
  156. type crashingWAL struct {
  157. next WAL
  158. panicCh chan error
  159. heightToStop int64
  160. msgIndex int // current message index
  161. lastPanicedForMsgIndex int // last message for which we panicked
  162. }
  163. // WALWriteError indicates a WAL crash.
  164. type WALWriteError struct {
  165. msg string
  166. }
  167. func (e WALWriteError) Error() string {
  168. return e.msg
  169. }
  170. // ReachedHeightToStopError indicates we've reached the required consensus
  171. // height and may exit.
  172. type ReachedHeightToStopError struct {
  173. height int64
  174. }
  175. func (e ReachedHeightToStopError) Error() string {
  176. return fmt.Sprintf("reached height to stop %d", e.height)
  177. }
  178. // Write simulate WAL's crashing by sending an error to the panicCh and then
  179. // exiting the cs.receiveRoutine.
  180. func (w *crashingWAL) Write(m WALMessage) {
  181. if endMsg, ok := m.(EndHeightMessage); ok {
  182. if endMsg.Height == w.heightToStop {
  183. w.panicCh <- ReachedHeightToStopError{endMsg.Height}
  184. runtime.Goexit()
  185. } else {
  186. w.next.Write(m)
  187. }
  188. return
  189. }
  190. if w.msgIndex > w.lastPanicedForMsgIndex {
  191. w.lastPanicedForMsgIndex = w.msgIndex
  192. _, file, line, _ := runtime.Caller(1)
  193. w.panicCh <- WALWriteError{fmt.Sprintf("failed to write %T to WAL (fileline: %s:%d)", m, file, line)}
  194. runtime.Goexit()
  195. } else {
  196. w.msgIndex++
  197. w.next.Write(m)
  198. }
  199. }
  200. func (w *crashingWAL) WriteSync(m WALMessage) {
  201. w.Write(m)
  202. }
  203. func (w *crashingWAL) Group() *auto.Group { return w.next.Group() }
  204. func (w *crashingWAL) SearchForEndHeight(height int64, options *WALSearchOptions) (gr *auto.GroupReader, found bool, err error) {
  205. return w.next.SearchForEndHeight(height, options)
  206. }
  207. func (w *crashingWAL) Start() error { return w.next.Start() }
  208. func (w *crashingWAL) Stop() error { return w.next.Stop() }
  209. func (w *crashingWAL) Wait() { w.next.Wait() }
  210. //------------------------------------------------------------------------------------------
  211. // Handshake Tests
  212. const (
  213. NUM_BLOCKS = 6
  214. )
  215. var (
  216. mempool = sm.MockMempool{}
  217. evpool = sm.MockEvidencePool{}
  218. )
  219. //---------------------------------------
  220. // Test handshake/replay
  221. // 0 - all synced up
  222. // 1 - saved block but app and state are behind
  223. // 2 - save block and committed but state is behind
  224. var modes = []uint{0, 1, 2}
  225. // Sync from scratch
  226. func TestHandshakeReplayAll(t *testing.T) {
  227. for _, m := range modes {
  228. testHandshakeReplay(t, 0, m)
  229. }
  230. }
  231. // Sync many, not from scratch
  232. func TestHandshakeReplaySome(t *testing.T) {
  233. for _, m := range modes {
  234. testHandshakeReplay(t, 1, m)
  235. }
  236. }
  237. // Sync from lagging by one
  238. func TestHandshakeReplayOne(t *testing.T) {
  239. for _, m := range modes {
  240. testHandshakeReplay(t, NUM_BLOCKS-1, m)
  241. }
  242. }
  243. // Sync from caught up
  244. func TestHandshakeReplayNone(t *testing.T) {
  245. for _, m := range modes {
  246. testHandshakeReplay(t, NUM_BLOCKS, m)
  247. }
  248. }
  249. func tempWALWithData(data []byte) string {
  250. walFile, err := ioutil.TempFile("", "wal")
  251. if err != nil {
  252. panic(fmt.Errorf("failed to create temp WAL file: %v", err))
  253. }
  254. _, err = walFile.Write(data)
  255. if err != nil {
  256. panic(fmt.Errorf("failed to write to temp WAL file: %v", err))
  257. }
  258. if err := walFile.Close(); err != nil {
  259. panic(fmt.Errorf("failed to close temp WAL file: %v", err))
  260. }
  261. return walFile.Name()
  262. }
  263. // Make some blocks. Start a fresh app and apply nBlocks blocks. Then restart the app and sync it up with the remaining blocks
  264. func testHandshakeReplay(t *testing.T, nBlocks int, mode uint) {
  265. config := ResetConfig("proxy_test_")
  266. walBody, err := WALWithNBlocks(NUM_BLOCKS)
  267. require.NoError(t, err)
  268. walFile := tempWALWithData(walBody)
  269. config.Consensus.SetWalFile(walFile)
  270. privVal := privval.LoadFilePV(config.PrivValidatorFile())
  271. wal, err := NewWAL(walFile)
  272. require.NoError(t, err)
  273. wal.SetLogger(log.TestingLogger())
  274. err = wal.Start()
  275. require.NoError(t, err)
  276. defer wal.Stop()
  277. chain, commits, err := makeBlockchainFromWAL(wal)
  278. require.NoError(t, err)
  279. stateDB, state, store := stateAndStore(config, privVal.GetPubKey(), kvstore.ProtocolVersion)
  280. store.chain = chain
  281. store.commits = commits
  282. // run the chain through state.ApplyBlock to build up the tendermint state
  283. state = buildTMStateFromChain(config, stateDB, state, chain, mode)
  284. latestAppHash := state.AppHash
  285. // make a new client creator
  286. kvstoreApp := kvstore.NewPersistentKVStoreApplication(path.Join(config.DBDir(), "2"))
  287. clientCreator2 := proxy.NewLocalClientCreator(kvstoreApp)
  288. if nBlocks > 0 {
  289. // run nBlocks against a new client to build up the app state.
  290. // use a throwaway tendermint state
  291. proxyApp := proxy.NewAppConns(clientCreator2)
  292. stateDB, state, _ := stateAndStore(config, privVal.GetPubKey(), kvstore.ProtocolVersion)
  293. buildAppStateFromChain(proxyApp, stateDB, state, chain, nBlocks, mode)
  294. }
  295. // now start the app using the handshake - it should sync
  296. genDoc, _ := sm.MakeGenesisDocFromFile(config.GenesisFile())
  297. handshaker := NewHandshaker(stateDB, state, store, genDoc)
  298. proxyApp := proxy.NewAppConns(clientCreator2)
  299. if err := proxyApp.Start(); err != nil {
  300. t.Fatalf("Error starting proxy app connections: %v", err)
  301. }
  302. defer proxyApp.Stop()
  303. if err := handshaker.Handshake(proxyApp); err != nil {
  304. t.Fatalf("Error on abci handshake: %v", err)
  305. }
  306. // get the latest app hash from the app
  307. res, err := proxyApp.Query().InfoSync(abci.RequestInfo{Version: ""})
  308. if err != nil {
  309. t.Fatal(err)
  310. }
  311. // the app hash should be synced up
  312. if !bytes.Equal(latestAppHash, res.LastBlockAppHash) {
  313. t.Fatalf("Expected app hashes to match after handshake/replay. got %X, expected %X", res.LastBlockAppHash, latestAppHash)
  314. }
  315. expectedBlocksToSync := NUM_BLOCKS - nBlocks
  316. if nBlocks == NUM_BLOCKS && mode > 0 {
  317. expectedBlocksToSync++
  318. } else if nBlocks > 0 && mode == 1 {
  319. expectedBlocksToSync++
  320. }
  321. if handshaker.NBlocks() != expectedBlocksToSync {
  322. t.Fatalf("Expected handshake to sync %d blocks, got %d", expectedBlocksToSync, handshaker.NBlocks())
  323. }
  324. }
  325. func applyBlock(stateDB dbm.DB, st sm.State, blk *types.Block, proxyApp proxy.AppConns) sm.State {
  326. testPartSize := types.BlockPartSizeBytes
  327. blockExec := sm.NewBlockExecutor(stateDB, log.TestingLogger(), proxyApp.Consensus(), mempool, evpool)
  328. blkID := types.BlockID{blk.Hash(), blk.MakePartSet(testPartSize).Header()}
  329. newState, err := blockExec.ApplyBlock(st, blkID, blk)
  330. if err != nil {
  331. panic(err)
  332. }
  333. return newState
  334. }
  335. func buildAppStateFromChain(proxyApp proxy.AppConns, stateDB dbm.DB,
  336. state sm.State, chain []*types.Block, nBlocks int, mode uint) {
  337. // start a new app without handshake, play nBlocks blocks
  338. if err := proxyApp.Start(); err != nil {
  339. panic(err)
  340. }
  341. defer proxyApp.Stop()
  342. validators := types.TM2PB.ValidatorUpdates(state.Validators)
  343. if _, err := proxyApp.Consensus().InitChainSync(abci.RequestInitChain{
  344. Validators: validators,
  345. }); err != nil {
  346. panic(err)
  347. }
  348. switch mode {
  349. case 0:
  350. for i := 0; i < nBlocks; i++ {
  351. block := chain[i]
  352. state = applyBlock(stateDB, state, block, proxyApp)
  353. }
  354. case 1, 2:
  355. for i := 0; i < nBlocks-1; i++ {
  356. block := chain[i]
  357. state = applyBlock(stateDB, state, block, proxyApp)
  358. }
  359. if mode == 2 {
  360. // update the kvstore height and apphash
  361. // as if we ran commit but not
  362. state = applyBlock(stateDB, state, chain[nBlocks-1], proxyApp)
  363. }
  364. }
  365. }
  366. func buildTMStateFromChain(config *cfg.Config, stateDB dbm.DB, state sm.State, chain []*types.Block, mode uint) sm.State {
  367. // run the whole chain against this client to build up the tendermint state
  368. clientCreator := proxy.NewLocalClientCreator(kvstore.NewPersistentKVStoreApplication(path.Join(config.DBDir(), "1")))
  369. proxyApp := proxy.NewAppConns(clientCreator)
  370. if err := proxyApp.Start(); err != nil {
  371. panic(err)
  372. }
  373. defer proxyApp.Stop()
  374. validators := types.TM2PB.ValidatorUpdates(state.Validators)
  375. if _, err := proxyApp.Consensus().InitChainSync(abci.RequestInitChain{
  376. Validators: validators,
  377. }); err != nil {
  378. panic(err)
  379. }
  380. switch mode {
  381. case 0:
  382. // sync right up
  383. for _, block := range chain {
  384. state = applyBlock(stateDB, state, block, proxyApp)
  385. }
  386. case 1, 2:
  387. // sync up to the penultimate as if we stored the block.
  388. // whether we commit or not depends on the appHash
  389. for _, block := range chain[:len(chain)-1] {
  390. state = applyBlock(stateDB, state, block, proxyApp)
  391. }
  392. // apply the final block to a state copy so we can
  393. // get the right next appHash but keep the state back
  394. applyBlock(stateDB, state, chain[len(chain)-1], proxyApp)
  395. }
  396. return state
  397. }
  398. //--------------------------
  399. // utils for making blocks
  400. func makeBlockchainFromWAL(wal WAL) ([]*types.Block, []*types.Commit, error) {
  401. // Search for height marker
  402. gr, found, err := wal.SearchForEndHeight(0, &WALSearchOptions{})
  403. if err != nil {
  404. return nil, nil, err
  405. }
  406. if !found {
  407. return nil, nil, fmt.Errorf("WAL does not contain height %d.", 1)
  408. }
  409. defer gr.Close() // nolint: errcheck
  410. // log.Notice("Build a blockchain by reading from the WAL")
  411. var blocks []*types.Block
  412. var commits []*types.Commit
  413. var thisBlockParts *types.PartSet
  414. var thisBlockCommit *types.Commit
  415. var height int64
  416. dec := NewWALDecoder(gr)
  417. for {
  418. msg, err := dec.Decode()
  419. if err == io.EOF {
  420. break
  421. } else if err != nil {
  422. return nil, nil, err
  423. }
  424. piece := readPieceFromWAL(msg)
  425. if piece == nil {
  426. continue
  427. }
  428. switch p := piece.(type) {
  429. case EndHeightMessage:
  430. // if its not the first one, we have a full block
  431. if thisBlockParts != nil {
  432. var block = new(types.Block)
  433. _, err = cdc.UnmarshalBinaryLengthPrefixedReader(thisBlockParts.GetReader(), block, 0)
  434. if err != nil {
  435. panic(err)
  436. }
  437. if block.Height != height+1 {
  438. panic(fmt.Sprintf("read bad block from wal. got height %d, expected %d", block.Height, height+1))
  439. }
  440. commitHeight := thisBlockCommit.Precommits[0].Height
  441. if commitHeight != height+1 {
  442. panic(fmt.Sprintf("commit doesnt match. got height %d, expected %d", commitHeight, height+1))
  443. }
  444. blocks = append(blocks, block)
  445. commits = append(commits, thisBlockCommit)
  446. height++
  447. }
  448. case *types.PartSetHeader:
  449. thisBlockParts = types.NewPartSetFromHeader(*p)
  450. case *types.Part:
  451. _, err := thisBlockParts.AddPart(p)
  452. if err != nil {
  453. return nil, nil, err
  454. }
  455. case *types.Vote:
  456. if p.Type == types.PrecommitType {
  457. thisBlockCommit = &types.Commit{
  458. BlockID: p.BlockID,
  459. Precommits: []*types.Vote{p},
  460. }
  461. }
  462. }
  463. }
  464. // grab the last block too
  465. var block = new(types.Block)
  466. _, err = cdc.UnmarshalBinaryLengthPrefixedReader(thisBlockParts.GetReader(), block, 0)
  467. if err != nil {
  468. panic(err)
  469. }
  470. if block.Height != height+1 {
  471. panic(fmt.Sprintf("read bad block from wal. got height %d, expected %d", block.Height, height+1))
  472. }
  473. commitHeight := thisBlockCommit.Precommits[0].Height
  474. if commitHeight != height+1 {
  475. panic(fmt.Sprintf("commit doesnt match. got height %d, expected %d", commitHeight, height+1))
  476. }
  477. blocks = append(blocks, block)
  478. commits = append(commits, thisBlockCommit)
  479. return blocks, commits, nil
  480. }
  481. func readPieceFromWAL(msg *TimedWALMessage) interface{} {
  482. // for logging
  483. switch m := msg.Msg.(type) {
  484. case msgInfo:
  485. switch msg := m.Msg.(type) {
  486. case *ProposalMessage:
  487. return &msg.Proposal.BlockID.PartsHeader
  488. case *BlockPartMessage:
  489. return msg.Part
  490. case *VoteMessage:
  491. return msg.Vote
  492. }
  493. case EndHeightMessage:
  494. return m
  495. }
  496. return nil
  497. }
  498. // fresh state and mock store
  499. func stateAndStore(config *cfg.Config, pubKey crypto.PubKey, appVersion version.Protocol) (dbm.DB, sm.State, *mockBlockStore) {
  500. stateDB := dbm.NewMemDB()
  501. state, _ := sm.MakeGenesisStateFromFile(config.GenesisFile())
  502. state.Version.Consensus.App = appVersion
  503. store := NewMockBlockStore(config, state.ConsensusParams)
  504. return stateDB, state, store
  505. }
  506. //----------------------------------
  507. // mock block store
  508. type mockBlockStore struct {
  509. config *cfg.Config
  510. params types.ConsensusParams
  511. chain []*types.Block
  512. commits []*types.Commit
  513. }
  514. // TODO: NewBlockStore(db.NewMemDB) ...
  515. func NewMockBlockStore(config *cfg.Config, params types.ConsensusParams) *mockBlockStore {
  516. return &mockBlockStore{config, params, nil, nil}
  517. }
  518. func (bs *mockBlockStore) Height() int64 { return int64(len(bs.chain)) }
  519. func (bs *mockBlockStore) LoadBlock(height int64) *types.Block { return bs.chain[height-1] }
  520. func (bs *mockBlockStore) LoadBlockMeta(height int64) *types.BlockMeta {
  521. block := bs.chain[height-1]
  522. return &types.BlockMeta{
  523. BlockID: types.BlockID{block.Hash(), block.MakePartSet(types.BlockPartSizeBytes).Header()},
  524. Header: block.Header,
  525. }
  526. }
  527. func (bs *mockBlockStore) LoadBlockPart(height int64, index int) *types.Part { return nil }
  528. func (bs *mockBlockStore) SaveBlock(block *types.Block, blockParts *types.PartSet, seenCommit *types.Commit) {
  529. }
  530. func (bs *mockBlockStore) LoadBlockCommit(height int64) *types.Commit {
  531. return bs.commits[height-1]
  532. }
  533. func (bs *mockBlockStore) LoadSeenCommit(height int64) *types.Commit {
  534. return bs.commits[height-1]
  535. }
  536. //----------------------------------------
  537. func TestInitChainUpdateValidators(t *testing.T) {
  538. val, _ := types.RandValidator(true, 10)
  539. vals := types.NewValidatorSet([]*types.Validator{val})
  540. app := &initChainApp{vals: types.TM2PB.ValidatorUpdates(vals)}
  541. clientCreator := proxy.NewLocalClientCreator(app)
  542. config := ResetConfig("proxy_test_")
  543. privVal := privval.LoadFilePV(config.PrivValidatorFile())
  544. stateDB, state, store := stateAndStore(config, privVal.GetPubKey(), 0x0)
  545. oldValAddr := state.Validators.Validators[0].Address
  546. // now start the app using the handshake - it should sync
  547. genDoc, _ := sm.MakeGenesisDocFromFile(config.GenesisFile())
  548. handshaker := NewHandshaker(stateDB, state, store, genDoc)
  549. proxyApp := proxy.NewAppConns(clientCreator)
  550. if err := proxyApp.Start(); err != nil {
  551. t.Fatalf("Error starting proxy app connections: %v", err)
  552. }
  553. defer proxyApp.Stop()
  554. if err := handshaker.Handshake(proxyApp); err != nil {
  555. t.Fatalf("Error on abci handshake: %v", err)
  556. }
  557. // reload the state, check the validator set was updated
  558. state = sm.LoadState(stateDB)
  559. newValAddr := state.Validators.Validators[0].Address
  560. expectValAddr := val.Address
  561. assert.NotEqual(t, oldValAddr, newValAddr)
  562. assert.Equal(t, newValAddr, expectValAddr)
  563. }
  564. func newInitChainApp(vals []abci.ValidatorUpdate) *initChainApp {
  565. return &initChainApp{
  566. vals: vals,
  567. }
  568. }
  569. // returns the vals on InitChain
  570. type initChainApp struct {
  571. abci.BaseApplication
  572. vals []abci.ValidatorUpdate
  573. }
  574. func (ica *initChainApp) InitChain(req abci.RequestInitChain) abci.ResponseInitChain {
  575. return abci.ResponseInitChain{
  576. Validators: ica.vals,
  577. }
  578. }