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.

203 lines
5.9 KiB

  1. package state
  2. import (
  3. "bytes"
  4. "fmt"
  5. "path"
  6. "testing"
  7. "github.com/tendermint/tendermint/config/tendermint_test"
  8. // . "github.com/tendermint/go-common"
  9. cfg "github.com/tendermint/go-config"
  10. "github.com/tendermint/go-crypto"
  11. dbm "github.com/tendermint/go-db"
  12. "github.com/tendermint/tendermint/proxy"
  13. "github.com/tendermint/tendermint/types"
  14. "github.com/tendermint/tmsp/example/dummy"
  15. )
  16. var (
  17. privKey = crypto.GenPrivKeyEd25519FromSecret([]byte("handshake_test"))
  18. chainID = "handshake_chain"
  19. nBlocks = 5
  20. mempool = mockMempool{}
  21. testPartSize = 65536
  22. )
  23. func TestExecBlock(t *testing.T) {
  24. // TODO
  25. }
  26. // Sync from scratch
  27. func TestHandshakeReplayAll(t *testing.T) {
  28. testHandshakeReplay(t, 0)
  29. }
  30. // Sync many, not from scratch
  31. func TestHandshakeReplaySome(t *testing.T) {
  32. testHandshakeReplay(t, 1)
  33. }
  34. // Sync from lagging by one
  35. func TestHandshakeReplayOne(t *testing.T) {
  36. testHandshakeReplay(t, nBlocks-1)
  37. }
  38. // Sync from caught up
  39. func TestHandshakeReplayNone(t *testing.T) {
  40. testHandshakeReplay(t, nBlocks)
  41. }
  42. // Make some blocks. Start a fresh app and apply n blocks. Then restart the app and sync it up with the remaining blocks
  43. func testHandshakeReplay(t *testing.T, n int) {
  44. config := tendermint_test.ResetConfig("proxy_test_")
  45. state, store := stateAndStore(config)
  46. clientCreator := proxy.NewLocalClientCreator(dummy.NewPersistentDummyApplication(path.Join(config.GetString("db_dir"), "1")))
  47. clientCreator2 := proxy.NewLocalClientCreator(dummy.NewPersistentDummyApplication(path.Join(config.GetString("db_dir"), "2")))
  48. proxyApp := proxy.NewAppConns(config, clientCreator, NewHandshaker(config, state, store))
  49. if _, err := proxyApp.Start(); err != nil {
  50. t.Fatalf("Error starting proxy app connections: %v", err)
  51. }
  52. chain := makeBlockchain(t, proxyApp, state)
  53. store.chain = chain //
  54. latestAppHash := state.AppHash
  55. proxyApp.Stop()
  56. if n > 0 {
  57. // start a new app without handshake, play n blocks
  58. proxyApp = proxy.NewAppConns(config, clientCreator2, nil)
  59. if _, err := proxyApp.Start(); err != nil {
  60. t.Fatalf("Error starting proxy app connections: %v", err)
  61. }
  62. state2, _ := stateAndStore(config)
  63. for i := 0; i < n; i++ {
  64. block := chain[i]
  65. err := state2.ApplyBlock(nil, proxyApp.Consensus(), block, block.MakePartSet(testPartSize).Header(), mempool)
  66. if err != nil {
  67. t.Fatal(err)
  68. }
  69. }
  70. proxyApp.Stop()
  71. }
  72. // now start it with the handshake
  73. handshaker := NewHandshaker(config, state, store)
  74. proxyApp = proxy.NewAppConns(config, clientCreator2, handshaker)
  75. if _, err := proxyApp.Start(); err != nil {
  76. t.Fatalf("Error starting proxy app connections: %v", err)
  77. }
  78. // get the latest app hash from the app
  79. r, _, blockInfo, _ := proxyApp.Query().InfoSync()
  80. if r.IsErr() {
  81. t.Fatal(r)
  82. }
  83. // the app hash should be synced up
  84. if !bytes.Equal(latestAppHash, blockInfo.AppHash) {
  85. t.Fatalf("Expected app hashes to match after handshake/replay. got %X, expected %X", blockInfo.AppHash, latestAppHash)
  86. }
  87. if handshaker.nBlocks != nBlocks-n {
  88. t.Fatalf("Expected handshake to sync %d blocks, got %d", nBlocks-n, handshaker.nBlocks)
  89. }
  90. }
  91. //--------------------------
  92. // make some bogus txs
  93. func txsFunc(blockNum int) (txs []types.Tx) {
  94. for i := 0; i < 10; i++ {
  95. txs = append(txs, types.Tx([]byte{byte(blockNum), byte(i)}))
  96. }
  97. return txs
  98. }
  99. // sign a commit vote
  100. func signCommit(height, round int, hash []byte, header types.PartSetHeader) *types.Vote {
  101. vote := &types.Vote{
  102. ValidatorIndex: 0,
  103. ValidatorAddress: privKey.PubKey().Address(),
  104. Height: height,
  105. Round: round,
  106. Type: types.VoteTypePrecommit,
  107. BlockID: types.BlockID{hash, header},
  108. }
  109. sig := privKey.Sign(types.SignBytes(chainID, vote))
  110. vote.Signature = sig.(crypto.SignatureEd25519)
  111. return vote
  112. }
  113. // make a blockchain with one validator
  114. func makeBlockchain(t *testing.T, proxyApp proxy.AppConns, state *State) (blockchain []*types.Block) {
  115. prevHash := state.LastBlockID.Hash
  116. lastCommit := new(types.Commit)
  117. prevParts := types.PartSetHeader{}
  118. valHash := state.Validators.Hash()
  119. prevBlockID := types.BlockID{prevHash, prevParts}
  120. for i := 1; i < nBlocks+1; i++ {
  121. block, parts := types.MakeBlock(i, chainID, txsFunc(i), lastCommit,
  122. prevBlockID, valHash, state.AppHash, testPartSize)
  123. fmt.Println(i)
  124. fmt.Println(prevBlockID)
  125. fmt.Println(block.LastBlockID)
  126. err := state.ApplyBlock(nil, proxyApp.Consensus(), block, block.MakePartSet(testPartSize).Header(), mempool)
  127. if err != nil {
  128. t.Fatal(i, err)
  129. }
  130. voteSet := types.NewVoteSet(chainID, i, 0, types.VoteTypePrecommit, state.Validators)
  131. vote := signCommit(i, 0, block.Hash(), parts.Header())
  132. _, err = voteSet.AddVote(vote)
  133. if err != nil {
  134. t.Fatal(err)
  135. }
  136. blockchain = append(blockchain, block)
  137. prevHash = block.Hash()
  138. prevParts = parts.Header()
  139. lastCommit = voteSet.MakeCommit()
  140. prevBlockID = types.BlockID{prevHash, prevParts}
  141. }
  142. return blockchain
  143. }
  144. // fresh state and mock store
  145. func stateAndStore(config cfg.Config) (*State, *mockBlockStore) {
  146. stateDB := dbm.NewMemDB()
  147. return MakeGenesisState(stateDB, &types.GenesisDoc{
  148. ChainID: chainID,
  149. Validators: []types.GenesisValidator{
  150. types.GenesisValidator{privKey.PubKey(), 10000, "test"},
  151. },
  152. AppHash: nil,
  153. }), NewMockBlockStore(config, nil)
  154. }
  155. //----------------------------------
  156. // mock block store
  157. type mockBlockStore struct {
  158. config cfg.Config
  159. chain []*types.Block
  160. }
  161. func NewMockBlockStore(config cfg.Config, chain []*types.Block) *mockBlockStore {
  162. return &mockBlockStore{config, chain}
  163. }
  164. func (bs *mockBlockStore) Height() int { return len(bs.chain) }
  165. func (bs *mockBlockStore) LoadBlock(height int) *types.Block { return bs.chain[height-1] }
  166. func (bs *mockBlockStore) LoadBlockMeta(height int) *types.BlockMeta {
  167. block := bs.chain[height-1]
  168. return &types.BlockMeta{
  169. Hash: block.Hash(),
  170. Header: block.Header,
  171. PartsHeader: block.MakePartSet(bs.config.GetInt("block_part_size")).Header(),
  172. }
  173. }