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.

409 lines
10 KiB

abci: Synchronize FinalizeBlock with the updated specification (#7983) This change set implements the most recent version of `FinalizeBlock`. # What does this change actually contain? * This change set is rather large but fear not! The majority of the files touched and changes are renaming `ResponseDeliverTx` to `ExecTxResult`. This should be a pretty inoffensive change since they're effectively the same type but with a different name. * The `execBlockOnProxyApp` was totally removed since it served as just a wrapper around the logic that is now mostly encapsulated within `FinalizeBlock` * The `updateState` helper function has been made a public method on `State`. It was being exposed as a shim through the testing infrastructure, so this seemed innocuous. * Tests already existed to ensure that the application received the `ByzantineValidators` and the `ValidatorUpdates`, but one was fixed up to ensure that `LastCommitInfo` was being sent across. * Tests were removed from the `psql` indexer that seemed to search for an event in the indexer that was not being created. # Questions for reviewers * We store this [ABCIResponses](https://github.com/tendermint/tendermint/blob/5721a13ab1f4479f9807f449f0bf5c536b9a05f2/proto/tendermint/state/types.pb.go#L37) type in the data base as the block results. This type has changed since v0.35 to contain the `FinalizeBlock` response. I'm wondering if we need to do any shimming to keep the old data retrieveable? * Similarly, this change is exposed via the RPC through [ResultBlockResults](https://github.com/tendermint/tendermint/blob/5721a13ab1f4479f9807f449f0bf5c536b9a05f2/rpc/coretypes/responses.go#L69) changing. Should we somehow shim or notify for this change? closes: #7658
3 years ago
abci: Synchronize FinalizeBlock with the updated specification (#7983) This change set implements the most recent version of `FinalizeBlock`. # What does this change actually contain? * This change set is rather large but fear not! The majority of the files touched and changes are renaming `ResponseDeliverTx` to `ExecTxResult`. This should be a pretty inoffensive change since they're effectively the same type but with a different name. * The `execBlockOnProxyApp` was totally removed since it served as just a wrapper around the logic that is now mostly encapsulated within `FinalizeBlock` * The `updateState` helper function has been made a public method on `State`. It was being exposed as a shim through the testing infrastructure, so this seemed innocuous. * Tests already existed to ensure that the application received the `ByzantineValidators` and the `ValidatorUpdates`, but one was fixed up to ensure that `LastCommitInfo` was being sent across. * Tests were removed from the `psql` indexer that seemed to search for an event in the indexer that was not being created. # Questions for reviewers * We store this [ABCIResponses](https://github.com/tendermint/tendermint/blob/5721a13ab1f4479f9807f449f0bf5c536b9a05f2/proto/tendermint/state/types.pb.go#L37) type in the data base as the block results. This type has changed since v0.35 to contain the `FinalizeBlock` response. I'm wondering if we need to do any shimming to keep the old data retrieveable? * Similarly, this change is exposed via the RPC through [ResultBlockResults](https://github.com/tendermint/tendermint/blob/5721a13ab1f4479f9807f449f0bf5c536b9a05f2/rpc/coretypes/responses.go#L69) changing. Should we somehow shim or notify for this change? closes: #7658
3 years ago
abci: Synchronize FinalizeBlock with the updated specification (#7983) This change set implements the most recent version of `FinalizeBlock`. # What does this change actually contain? * This change set is rather large but fear not! The majority of the files touched and changes are renaming `ResponseDeliverTx` to `ExecTxResult`. This should be a pretty inoffensive change since they're effectively the same type but with a different name. * The `execBlockOnProxyApp` was totally removed since it served as just a wrapper around the logic that is now mostly encapsulated within `FinalizeBlock` * The `updateState` helper function has been made a public method on `State`. It was being exposed as a shim through the testing infrastructure, so this seemed innocuous. * Tests already existed to ensure that the application received the `ByzantineValidators` and the `ValidatorUpdates`, but one was fixed up to ensure that `LastCommitInfo` was being sent across. * Tests were removed from the `psql` indexer that seemed to search for an event in the indexer that was not being created. # Questions for reviewers * We store this [ABCIResponses](https://github.com/tendermint/tendermint/blob/5721a13ab1f4479f9807f449f0bf5c536b9a05f2/proto/tendermint/state/types.pb.go#L37) type in the data base as the block results. This type has changed since v0.35 to contain the `FinalizeBlock` response. I'm wondering if we need to do any shimming to keep the old data retrieveable? * Similarly, this change is exposed via the RPC through [ResultBlockResults](https://github.com/tendermint/tendermint/blob/5721a13ab1f4479f9807f449f0bf5c536b9a05f2/rpc/coretypes/responses.go#L69) changing. Should we somehow shim or notify for this change? closes: #7658
3 years ago
  1. package mempool
  2. import (
  3. "context"
  4. "fmt"
  5. "os"
  6. "runtime"
  7. "strings"
  8. "sync"
  9. "testing"
  10. "time"
  11. "github.com/fortytw2/leaktest"
  12. "github.com/stretchr/testify/require"
  13. "github.com/tendermint/tendermint/abci/example/kvstore"
  14. abci "github.com/tendermint/tendermint/abci/types"
  15. "github.com/tendermint/tendermint/config"
  16. "github.com/tendermint/tendermint/internal/p2p"
  17. "github.com/tendermint/tendermint/internal/p2p/p2ptest"
  18. "github.com/tendermint/tendermint/libs/log"
  19. tmrand "github.com/tendermint/tendermint/libs/rand"
  20. protomem "github.com/tendermint/tendermint/proto/tendermint/mempool"
  21. "github.com/tendermint/tendermint/types"
  22. )
  23. type reactorTestSuite struct {
  24. network *p2ptest.Network
  25. logger log.Logger
  26. reactors map[types.NodeID]*Reactor
  27. mempoolChannels map[types.NodeID]*p2p.Channel
  28. mempools map[types.NodeID]*TxMempool
  29. kvstores map[types.NodeID]*kvstore.Application
  30. peerChans map[types.NodeID]chan p2p.PeerUpdate
  31. peerUpdates map[types.NodeID]*p2p.PeerUpdates
  32. nodes []types.NodeID
  33. }
  34. func setupReactors(ctx context.Context, t *testing.T, numNodes int, chBuf uint) *reactorTestSuite {
  35. t.Helper()
  36. cfg, err := config.ResetTestRoot(t.TempDir(), strings.ReplaceAll(t.Name(), "/", "|"))
  37. require.NoError(t, err)
  38. t.Cleanup(func() { os.RemoveAll(cfg.RootDir) })
  39. rts := &reactorTestSuite{
  40. logger: log.NewNopLogger().With("testCase", t.Name()),
  41. network: p2ptest.MakeNetwork(ctx, t, p2ptest.NetworkOptions{NumNodes: numNodes}),
  42. reactors: make(map[types.NodeID]*Reactor, numNodes),
  43. mempoolChannels: make(map[types.NodeID]*p2p.Channel, numNodes),
  44. mempools: make(map[types.NodeID]*TxMempool, numNodes),
  45. kvstores: make(map[types.NodeID]*kvstore.Application, numNodes),
  46. peerChans: make(map[types.NodeID]chan p2p.PeerUpdate, numNodes),
  47. peerUpdates: make(map[types.NodeID]*p2p.PeerUpdates, numNodes),
  48. }
  49. chDesc := getChannelDescriptor(cfg.Mempool)
  50. rts.mempoolChannels = rts.network.MakeChannelsNoCleanup(ctx, t, chDesc)
  51. for nodeID := range rts.network.Nodes {
  52. rts.kvstores[nodeID] = kvstore.NewApplication()
  53. mempool := setup(ctx, t, 0)
  54. rts.mempools[nodeID] = mempool
  55. rts.peerChans[nodeID] = make(chan p2p.PeerUpdate, chBuf)
  56. rts.peerUpdates[nodeID] = p2p.NewPeerUpdates(rts.peerChans[nodeID], 1)
  57. rts.network.Nodes[nodeID].PeerManager.Register(ctx, rts.peerUpdates[nodeID])
  58. chCreator := func(ctx context.Context, chDesc *p2p.ChannelDescriptor) (*p2p.Channel, error) {
  59. return rts.mempoolChannels[nodeID], nil
  60. }
  61. rts.reactors[nodeID], err = NewReactor(
  62. ctx,
  63. rts.logger.With("nodeID", nodeID),
  64. cfg.Mempool,
  65. rts.network.Nodes[nodeID].PeerManager,
  66. mempool,
  67. chCreator,
  68. rts.peerUpdates[nodeID],
  69. )
  70. require.NoError(t, err)
  71. rts.nodes = append(rts.nodes, nodeID)
  72. require.NoError(t, rts.reactors[nodeID].Start(ctx))
  73. require.True(t, rts.reactors[nodeID].IsRunning())
  74. }
  75. require.Len(t, rts.reactors, numNodes)
  76. t.Cleanup(func() {
  77. for nodeID := range rts.reactors {
  78. if rts.reactors[nodeID].IsRunning() {
  79. rts.reactors[nodeID].Stop()
  80. rts.reactors[nodeID].Wait()
  81. require.False(t, rts.reactors[nodeID].IsRunning())
  82. }
  83. }
  84. })
  85. t.Cleanup(leaktest.Check(t))
  86. return rts
  87. }
  88. func (rts *reactorTestSuite) start(ctx context.Context, t *testing.T) {
  89. t.Helper()
  90. rts.network.Start(ctx, t)
  91. require.Len(t,
  92. rts.network.RandomNode().PeerManager.Peers(),
  93. len(rts.nodes)-1,
  94. "network does not have expected number of nodes")
  95. }
  96. func (rts *reactorTestSuite) waitForTxns(t *testing.T, txs []types.Tx, ids ...types.NodeID) {
  97. t.Helper()
  98. // ensure that the transactions get fully broadcast to the
  99. // rest of the network
  100. wg := &sync.WaitGroup{}
  101. for name, pool := range rts.mempools {
  102. if !p2ptest.NodeInSlice(name, ids) {
  103. continue
  104. }
  105. if len(txs) == pool.Size() {
  106. continue
  107. }
  108. wg.Add(1)
  109. go func(pool *TxMempool) {
  110. defer wg.Done()
  111. require.Eventually(t, func() bool { return len(txs) == pool.Size() },
  112. time.Minute,
  113. 250*time.Millisecond,
  114. "ntx=%d, size=%d", len(txs), pool.Size(),
  115. )
  116. }(pool)
  117. }
  118. wg.Wait()
  119. }
  120. func TestReactorBroadcastDoesNotPanic(t *testing.T) {
  121. ctx, cancel := context.WithCancel(context.Background())
  122. defer cancel()
  123. const numNodes = 2
  124. rts := setupReactors(ctx, t, numNodes, 0)
  125. observePanic := func(r interface{}) {
  126. t.Fatal("panic detected in reactor")
  127. }
  128. primary := rts.nodes[0]
  129. secondary := rts.nodes[1]
  130. primaryReactor := rts.reactors[primary]
  131. primaryMempool := primaryReactor.mempool
  132. secondaryReactor := rts.reactors[secondary]
  133. primaryReactor.observePanic = observePanic
  134. secondaryReactor.observePanic = observePanic
  135. firstTx := &WrappedTx{}
  136. primaryMempool.insertTx(firstTx)
  137. // run the router
  138. rts.start(ctx, t)
  139. go primaryReactor.broadcastTxRoutine(ctx, secondary)
  140. wg := &sync.WaitGroup{}
  141. for i := 0; i < 50; i++ {
  142. next := &WrappedTx{}
  143. wg.Add(1)
  144. go func() {
  145. defer wg.Done()
  146. primaryMempool.insertTx(next)
  147. }()
  148. }
  149. primaryReactor.Stop()
  150. wg.Wait()
  151. }
  152. func TestReactorBroadcastTxs(t *testing.T) {
  153. numTxs := 1000
  154. numNodes := 10
  155. ctx, cancel := context.WithCancel(context.Background())
  156. defer cancel()
  157. rts := setupReactors(ctx, t, numNodes, uint(numTxs))
  158. primary := rts.nodes[0]
  159. secondaries := rts.nodes[1:]
  160. txs := checkTxs(ctx, t, rts.reactors[primary].mempool, numTxs, UnknownPeerID)
  161. require.Equal(t, numTxs, rts.reactors[primary].mempool.Size())
  162. rts.start(ctx, t)
  163. // Wait till all secondary suites (reactor) received all mempool txs from the
  164. // primary suite (node).
  165. rts.waitForTxns(t, convertTex(txs), secondaries...)
  166. }
  167. // regression test for https://github.com/tendermint/tendermint/issues/5408
  168. func TestReactorConcurrency(t *testing.T) {
  169. numTxs := 10
  170. numNodes := 2
  171. ctx, cancel := context.WithCancel(context.Background())
  172. defer cancel()
  173. rts := setupReactors(ctx, t, numNodes, 0)
  174. primary := rts.nodes[0]
  175. secondary := rts.nodes[1]
  176. rts.start(ctx, t)
  177. var wg sync.WaitGroup
  178. for i := 0; i < runtime.NumCPU()*2; i++ {
  179. wg.Add(2)
  180. // 1. submit a bunch of txs
  181. // 2. update the whole mempool
  182. txs := checkTxs(ctx, t, rts.reactors[primary].mempool, numTxs, UnknownPeerID)
  183. go func() {
  184. defer wg.Done()
  185. mempool := rts.mempools[primary]
  186. mempool.Lock()
  187. defer mempool.Unlock()
  188. deliverTxResponses := make([]*abci.ExecTxResult, len(txs))
  189. for i := range txs {
  190. deliverTxResponses[i] = &abci.ExecTxResult{Code: 0}
  191. }
  192. require.NoError(t, mempool.Update(ctx, 1, convertTex(txs), deliverTxResponses, nil, nil))
  193. }()
  194. // 1. submit a bunch of txs
  195. // 2. update none
  196. _ = checkTxs(ctx, t, rts.reactors[secondary].mempool, numTxs, UnknownPeerID)
  197. go func() {
  198. defer wg.Done()
  199. mempool := rts.mempools[secondary]
  200. mempool.Lock()
  201. defer mempool.Unlock()
  202. err := mempool.Update(ctx, 1, []types.Tx{}, make([]*abci.ExecTxResult, 0), nil, nil)
  203. require.NoError(t, err)
  204. }()
  205. }
  206. wg.Wait()
  207. }
  208. func TestReactorNoBroadcastToSender(t *testing.T) {
  209. numTxs := 1000
  210. numNodes := 2
  211. ctx, cancel := context.WithCancel(context.Background())
  212. defer cancel()
  213. rts := setupReactors(ctx, t, numNodes, uint(numTxs))
  214. primary := rts.nodes[0]
  215. secondary := rts.nodes[1]
  216. peerID := uint16(1)
  217. _ = checkTxs(ctx, t, rts.mempools[primary], numTxs, peerID)
  218. rts.start(ctx, t)
  219. time.Sleep(100 * time.Millisecond)
  220. require.Eventually(t, func() bool {
  221. return rts.mempools[secondary].Size() == 0
  222. }, time.Minute, 100*time.Millisecond)
  223. }
  224. func TestReactor_MaxTxBytes(t *testing.T) {
  225. numNodes := 2
  226. cfg := config.TestConfig()
  227. ctx, cancel := context.WithCancel(context.Background())
  228. defer cancel()
  229. rts := setupReactors(ctx, t, numNodes, 0)
  230. primary := rts.nodes[0]
  231. secondary := rts.nodes[1]
  232. // Broadcast a tx, which has the max size and ensure it's received by the
  233. // second reactor.
  234. tx1 := tmrand.Bytes(cfg.Mempool.MaxTxBytes)
  235. err := rts.reactors[primary].mempool.CheckTx(
  236. ctx,
  237. tx1,
  238. nil,
  239. TxInfo{
  240. SenderID: UnknownPeerID,
  241. },
  242. )
  243. require.NoError(t, err)
  244. rts.start(ctx, t)
  245. rts.reactors[primary].mempool.Flush()
  246. rts.reactors[secondary].mempool.Flush()
  247. // broadcast a tx, which is beyond the max size and ensure it's not sent
  248. tx2 := tmrand.Bytes(cfg.Mempool.MaxTxBytes + 1)
  249. err = rts.mempools[primary].CheckTx(ctx, tx2, nil, TxInfo{SenderID: UnknownPeerID})
  250. require.Error(t, err)
  251. }
  252. func TestDontExhaustMaxActiveIDs(t *testing.T) {
  253. // we're creating a single node network, but not starting the
  254. // network.
  255. ctx, cancel := context.WithCancel(context.Background())
  256. defer cancel()
  257. rts := setupReactors(ctx, t, 1, MaxActiveIDs+1)
  258. nodeID := rts.nodes[0]
  259. peerID, err := types.NewNodeID("0011223344556677889900112233445566778899")
  260. require.NoError(t, err)
  261. // ensure the reactor does not panic (i.e. exhaust active IDs)
  262. for i := 0; i < MaxActiveIDs+1; i++ {
  263. rts.peerChans[nodeID] <- p2p.PeerUpdate{
  264. Status: p2p.PeerStatusUp,
  265. NodeID: peerID,
  266. }
  267. require.NoError(t, rts.mempoolChannels[nodeID].Send(ctx, p2p.Envelope{
  268. To: peerID,
  269. Message: &protomem.Txs{
  270. Txs: [][]byte{},
  271. },
  272. }))
  273. }
  274. }
  275. func TestMempoolIDsPanicsIfNodeRequestsOvermaxActiveIDs(t *testing.T) {
  276. if testing.Short() {
  277. t.Skip("skipping test in short mode")
  278. }
  279. // 0 is already reserved for UnknownPeerID
  280. ids := NewMempoolIDs()
  281. for i := 0; i < MaxActiveIDs-1; i++ {
  282. peerID, err := types.NewNodeID(fmt.Sprintf("%040d", i))
  283. require.NoError(t, err)
  284. ids.ReserveForPeer(peerID)
  285. }
  286. peerID, err := types.NewNodeID(fmt.Sprintf("%040d", MaxActiveIDs-1))
  287. require.NoError(t, err)
  288. require.Panics(t, func() {
  289. ids.ReserveForPeer(peerID)
  290. })
  291. }
  292. func TestBroadcastTxForPeerStopsWhenPeerStops(t *testing.T) {
  293. if testing.Short() {
  294. t.Skip("skipping test in short mode")
  295. }
  296. ctx, cancel := context.WithCancel(context.Background())
  297. defer cancel()
  298. rts := setupReactors(ctx, t, 2, 2)
  299. primary := rts.nodes[0]
  300. secondary := rts.nodes[1]
  301. rts.start(ctx, t)
  302. // disconnect peer
  303. rts.peerChans[primary] <- p2p.PeerUpdate{
  304. Status: p2p.PeerStatusDown,
  305. NodeID: secondary,
  306. }
  307. time.Sleep(500 * time.Millisecond)
  308. txs := checkTxs(ctx, t, rts.reactors[primary].mempool, 4, UnknownPeerID)
  309. require.Equal(t, 4, len(txs))
  310. require.Equal(t, 4, rts.mempools[primary].Size())
  311. require.Equal(t, 0, rts.mempools[secondary].Size())
  312. }