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.

306 lines
9.5 KiB

8 years ago
new pubsub package comment out failing consensus tests for now rewrite rpc httpclient to use new pubsub package import pubsub as tmpubsub, query as tmquery make event IDs constants EventKey -> EventTypeKey rename EventsPubsub to PubSub mempool does not use pubsub rename eventsSub to pubsub new subscribe API fix channel size issues and consensus tests bugs refactor rpc client add missing discardFromChan method add mutex rename pubsub to eventBus remove IsRunning from WSRPCConnection interface (not needed) add a comment in broadcastNewRoundStepsAndVotes rename registerEventCallbacks to broadcastNewRoundStepsAndVotes See https://dave.cheney.net/2014/03/19/channel-axioms stop eventBuses after reactor tests remove unnecessary Unsubscribe return subscribe helper function move discardFromChan to where it is used subscribe now returns an err this gives us ability to refuse to subscribe if pubsub is at its max capacity. use context for control overflow cache queries handle err when subscribing in replay_test rename testClientID to testSubscriber extract var set channel buffer capacity to 1 in replay_file fix byzantine_test unsubscribe from single event, not all events refactor httpclient to return events to appropriate channels return failing testReplayCrashBeforeWriteVote test fix TestValidatorSetChanges refactor code a bit fix testReplayCrashBeforeWriteVote add comment fix TestValidatorSetChanges fixes from Bucky's review update comment [ci skip] test TxEventBuffer update changelog fix TestValidatorSetChanges (2nd attempt) only do wg.Done when no errors benchmark event bus create pubsub server inside NewEventBus only expose config params (later if needed) set buffer capacity to 0 so we are not testing cache new tx event format: key = "Tx" plus a tag {"tx.hash": XYZ} This should allow to subscribe to all transactions! or a specific one using a query: "tm.events.type = Tx and tx.hash = '013ABF99434...'" use TimeoutCommit instead of afterPublishEventNewBlockTimeout TimeoutCommit is the time a node waits after committing a block, before it goes into the next height. So it will finish everything from the last block, but then wait a bit. The idea is this gives it time to hear more votes from other validators, to strengthen the commit it includes in the next block. But it also gives it time to hear about new transactions. waitForBlockWithUpdatedVals rewrite WAL crash tests Task: test that we can recover from any WAL crash. Solution: the old tests were relying on event hub being run in the same thread (we were injecting the private validator's last signature). when considering a rewrite, we considered two possible solutions: write a "fuzzy" testing system where WAL is crashing upon receiving a new message, or inject failures and trigger them in tests using something like https://github.com/coreos/gofail. remove sleep no cs.Lock around wal.Save test different cases (empty block, non-empty block, ...) comments add comments test 4 cases: empty block, non-empty block, non-empty block with smaller part size, many blocks fixes as per Bucky's last review reset subscriptions on UnsubscribeAll use a simple counter to track message for which we panicked also, set a smaller part size for all test cases
8 years ago
8 years ago
8 years ago
8 years ago
7 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
new pubsub package comment out failing consensus tests for now rewrite rpc httpclient to use new pubsub package import pubsub as tmpubsub, query as tmquery make event IDs constants EventKey -> EventTypeKey rename EventsPubsub to PubSub mempool does not use pubsub rename eventsSub to pubsub new subscribe API fix channel size issues and consensus tests bugs refactor rpc client add missing discardFromChan method add mutex rename pubsub to eventBus remove IsRunning from WSRPCConnection interface (not needed) add a comment in broadcastNewRoundStepsAndVotes rename registerEventCallbacks to broadcastNewRoundStepsAndVotes See https://dave.cheney.net/2014/03/19/channel-axioms stop eventBuses after reactor tests remove unnecessary Unsubscribe return subscribe helper function move discardFromChan to where it is used subscribe now returns an err this gives us ability to refuse to subscribe if pubsub is at its max capacity. use context for control overflow cache queries handle err when subscribing in replay_test rename testClientID to testSubscriber extract var set channel buffer capacity to 1 in replay_file fix byzantine_test unsubscribe from single event, not all events refactor httpclient to return events to appropriate channels return failing testReplayCrashBeforeWriteVote test fix TestValidatorSetChanges refactor code a bit fix testReplayCrashBeforeWriteVote add comment fix TestValidatorSetChanges fixes from Bucky's review update comment [ci skip] test TxEventBuffer update changelog fix TestValidatorSetChanges (2nd attempt) only do wg.Done when no errors benchmark event bus create pubsub server inside NewEventBus only expose config params (later if needed) set buffer capacity to 0 so we are not testing cache new tx event format: key = "Tx" plus a tag {"tx.hash": XYZ} This should allow to subscribe to all transactions! or a specific one using a query: "tm.events.type = Tx and tx.hash = '013ABF99434...'" use TimeoutCommit instead of afterPublishEventNewBlockTimeout TimeoutCommit is the time a node waits after committing a block, before it goes into the next height. So it will finish everything from the last block, but then wait a bit. The idea is this gives it time to hear more votes from other validators, to strengthen the commit it includes in the next block. But it also gives it time to hear about new transactions. waitForBlockWithUpdatedVals rewrite WAL crash tests Task: test that we can recover from any WAL crash. Solution: the old tests were relying on event hub being run in the same thread (we were injecting the private validator's last signature). when considering a rewrite, we considered two possible solutions: write a "fuzzy" testing system where WAL is crashing upon receiving a new message, or inject failures and trigger them in tests using something like https://github.com/coreos/gofail. remove sleep no cs.Lock around wal.Save test different cases (empty block, non-empty block, ...) comments add comments test 4 cases: empty block, non-empty block, non-empty block with smaller part size, many blocks fixes as per Bucky's last review reset subscriptions on UnsubscribeAll use a simple counter to track message for which we panicked also, set a smaller part size for all test cases
8 years ago
new pubsub package comment out failing consensus tests for now rewrite rpc httpclient to use new pubsub package import pubsub as tmpubsub, query as tmquery make event IDs constants EventKey -> EventTypeKey rename EventsPubsub to PubSub mempool does not use pubsub rename eventsSub to pubsub new subscribe API fix channel size issues and consensus tests bugs refactor rpc client add missing discardFromChan method add mutex rename pubsub to eventBus remove IsRunning from WSRPCConnection interface (not needed) add a comment in broadcastNewRoundStepsAndVotes rename registerEventCallbacks to broadcastNewRoundStepsAndVotes See https://dave.cheney.net/2014/03/19/channel-axioms stop eventBuses after reactor tests remove unnecessary Unsubscribe return subscribe helper function move discardFromChan to where it is used subscribe now returns an err this gives us ability to refuse to subscribe if pubsub is at its max capacity. use context for control overflow cache queries handle err when subscribing in replay_test rename testClientID to testSubscriber extract var set channel buffer capacity to 1 in replay_file fix byzantine_test unsubscribe from single event, not all events refactor httpclient to return events to appropriate channels return failing testReplayCrashBeforeWriteVote test fix TestValidatorSetChanges refactor code a bit fix testReplayCrashBeforeWriteVote add comment fix TestValidatorSetChanges fixes from Bucky's review update comment [ci skip] test TxEventBuffer update changelog fix TestValidatorSetChanges (2nd attempt) only do wg.Done when no errors benchmark event bus create pubsub server inside NewEventBus only expose config params (later if needed) set buffer capacity to 0 so we are not testing cache new tx event format: key = "Tx" plus a tag {"tx.hash": XYZ} This should allow to subscribe to all transactions! or a specific one using a query: "tm.events.type = Tx and tx.hash = '013ABF99434...'" use TimeoutCommit instead of afterPublishEventNewBlockTimeout TimeoutCommit is the time a node waits after committing a block, before it goes into the next height. So it will finish everything from the last block, but then wait a bit. The idea is this gives it time to hear more votes from other validators, to strengthen the commit it includes in the next block. But it also gives it time to hear about new transactions. waitForBlockWithUpdatedVals rewrite WAL crash tests Task: test that we can recover from any WAL crash. Solution: the old tests were relying on event hub being run in the same thread (we were injecting the private validator's last signature). when considering a rewrite, we considered two possible solutions: write a "fuzzy" testing system where WAL is crashing upon receiving a new message, or inject failures and trigger them in tests using something like https://github.com/coreos/gofail. remove sleep no cs.Lock around wal.Save test different cases (empty block, non-empty block, ...) comments add comments test 4 cases: empty block, non-empty block, non-empty block with smaller part size, many blocks fixes as per Bucky's last review reset subscriptions on UnsubscribeAll use a simple counter to track message for which we panicked also, set a smaller part size for all test cases
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
  1. package state
  2. import (
  3. "errors"
  4. "fmt"
  5. fail "github.com/ebuchman/fail-test"
  6. abci "github.com/tendermint/abci/types"
  7. crypto "github.com/tendermint/go-crypto"
  8. "github.com/tendermint/tendermint/proxy"
  9. "github.com/tendermint/tendermint/types"
  10. cmn "github.com/tendermint/tmlibs/common"
  11. "github.com/tendermint/tmlibs/log"
  12. )
  13. //--------------------------------------------------
  14. // Execute the block
  15. // ValExecBlock executes the block, but does NOT mutate State.
  16. // + validates the block
  17. // + executes block.Txs on the proxyAppConn
  18. func (s *State) ValExecBlock(txEventPublisher types.TxEventPublisher, proxyAppConn proxy.AppConnConsensus, block *types.Block) (*ABCIResponses, error) {
  19. // Validate the block.
  20. if err := s.validateBlock(block); err != nil {
  21. return nil, ErrInvalidBlock(err)
  22. }
  23. // Execute the block txs
  24. abciResponses, err := execBlockOnProxyApp(txEventPublisher, proxyAppConn, block, s.logger, s.LastValidators)
  25. if err != nil {
  26. // There was some error in proxyApp
  27. // TODO Report error and wait for proxyApp to be available.
  28. return nil, ErrProxyAppConn(err)
  29. }
  30. return abciResponses, nil
  31. }
  32. // Executes block's transactions on proxyAppConn.
  33. // Returns a list of transaction results and updates to the validator set
  34. // TODO: Generate a bitmap or otherwise store tx validity in state.
  35. func execBlockOnProxyApp(txEventPublisher types.TxEventPublisher, proxyAppConn proxy.AppConnConsensus, block *types.Block, logger log.Logger, lastValidators *types.ValidatorSet) (*ABCIResponses, error) {
  36. var validTxs, invalidTxs = 0, 0
  37. txIndex := 0
  38. abciResponses := NewABCIResponses(block)
  39. // Execute transactions and get hash
  40. proxyCb := func(req *abci.Request, res *abci.Response) {
  41. switch r := res.Value.(type) {
  42. case *abci.Response_DeliverTx:
  43. // TODO: make use of res.Log
  44. // TODO: make use of this info
  45. // Blocks may include invalid txs.
  46. // reqDeliverTx := req.(abci.RequestDeliverTx)
  47. txResult := r.DeliverTx
  48. if txResult.Code == abci.CodeTypeOK {
  49. validTxs++
  50. } else {
  51. logger.Debug("Invalid tx", "code", txResult.Code, "log", txResult.Log)
  52. invalidTxs++
  53. }
  54. // NOTE: if we count we can access the tx from the block instead of
  55. // pulling it from the req
  56. txEventPublisher.PublishEventTx(types.EventDataTx{types.TxResult{
  57. Height: block.Height,
  58. Index: uint32(txIndex),
  59. Tx: types.Tx(req.GetDeliverTx().Tx),
  60. Result: *txResult,
  61. }})
  62. abciResponses.DeliverTx[txIndex] = txResult
  63. txIndex++
  64. }
  65. }
  66. proxyAppConn.SetResponseCallback(proxyCb)
  67. // determine which validators did not sign last block
  68. absentVals := make([]int32, 0)
  69. for valI, vote := range block.LastCommit.Precommits {
  70. if vote == nil {
  71. absentVals = append(absentVals, int32(valI))
  72. }
  73. }
  74. // Begin block
  75. _, err := proxyAppConn.BeginBlockSync(abci.RequestBeginBlock{
  76. Hash: block.Hash(),
  77. Header: types.TM2PB.Header(block.Header),
  78. AbsentValidators: absentVals,
  79. ByzantineValidators: nil,
  80. })
  81. if err != nil {
  82. logger.Error("Error in proxyAppConn.BeginBlock", "err", err)
  83. return nil, err
  84. }
  85. // Run txs of block
  86. for _, tx := range block.Txs {
  87. proxyAppConn.DeliverTxAsync(tx)
  88. if err := proxyAppConn.Error(); err != nil {
  89. return nil, err
  90. }
  91. }
  92. // End block
  93. abciResponses.EndBlock, err = proxyAppConn.EndBlockSync(abci.RequestEndBlock{block.Height})
  94. if err != nil {
  95. logger.Error("Error in proxyAppConn.EndBlock", "err", err)
  96. return nil, err
  97. }
  98. valUpdates := abciResponses.EndBlock.ValidatorUpdates
  99. logger.Info("Executed block", "height", block.Height, "validTxs", validTxs, "invalidTxs", invalidTxs)
  100. if len(valUpdates) > 0 {
  101. logger.Info("Updates to validators", "updates", abci.ValidatorsString(valUpdates))
  102. }
  103. return abciResponses, nil
  104. }
  105. func updateValidators(validators *types.ValidatorSet, changedValidators []*abci.Validator) error {
  106. // TODO: prevent change of 1/3+ at once
  107. for _, v := range changedValidators {
  108. pubkey, err := crypto.PubKeyFromBytes(v.PubKey) // NOTE: expects go-wire encoded pubkey
  109. if err != nil {
  110. return err
  111. }
  112. address := pubkey.Address()
  113. power := int64(v.Power)
  114. // mind the overflow from int64
  115. if power < 0 {
  116. return errors.New(cmn.Fmt("Power (%d) overflows int64", v.Power))
  117. }
  118. _, val := validators.GetByAddress(address)
  119. if val == nil {
  120. // add val
  121. added := validators.Add(types.NewValidator(pubkey, power))
  122. if !added {
  123. return errors.New(cmn.Fmt("Failed to add new validator %X with voting power %d", address, power))
  124. }
  125. } else if v.Power == 0 {
  126. // remove val
  127. _, removed := validators.Remove(address)
  128. if !removed {
  129. return errors.New(cmn.Fmt("Failed to remove validator %X)"))
  130. }
  131. } else {
  132. // update val
  133. val.VotingPower = power
  134. updated := validators.Update(val)
  135. if !updated {
  136. return errors.New(cmn.Fmt("Failed to update validator %X with voting power %d", address, power))
  137. }
  138. }
  139. }
  140. return nil
  141. }
  142. // return a bit array of validators that signed the last commit
  143. // NOTE: assumes commits have already been authenticated
  144. /* function is currently unused
  145. func commitBitArrayFromBlock(block *types.Block) *cmn.BitArray {
  146. signed := cmn.NewBitArray(len(block.LastCommit.Precommits))
  147. for i, precommit := range block.LastCommit.Precommits {
  148. if precommit != nil {
  149. signed.SetIndex(i, true) // val_.LastCommitHeight = block.Height - 1
  150. }
  151. }
  152. return signed
  153. }
  154. */
  155. //-----------------------------------------------------
  156. // Validate block
  157. // ValidateBlock validates the block against the state.
  158. func (s *State) ValidateBlock(block *types.Block) error {
  159. return s.validateBlock(block)
  160. }
  161. func (s *State) validateBlock(block *types.Block) error {
  162. // Basic block validation.
  163. err := block.ValidateBasic(s.ChainID, s.LastBlockHeight,
  164. s.LastBlockTotalTx, s.LastBlockID, s.LastBlockTime, s.AppHash, s.LastConsensusHash)
  165. if err != nil {
  166. return err
  167. }
  168. // Validate block LastCommit.
  169. if block.Height == 1 {
  170. if len(block.LastCommit.Precommits) != 0 {
  171. return errors.New("Block at height 1 (first block) should have no LastCommit precommits")
  172. }
  173. } else {
  174. if len(block.LastCommit.Precommits) != s.LastValidators.Size() {
  175. return errors.New(cmn.Fmt("Invalid block commit size. Expected %v, got %v",
  176. s.LastValidators.Size(), len(block.LastCommit.Precommits)))
  177. }
  178. err := s.LastValidators.VerifyCommit(
  179. s.ChainID, s.LastBlockID, block.Height-1, block.LastCommit)
  180. if err != nil {
  181. return err
  182. }
  183. }
  184. return nil
  185. }
  186. //-----------------------------------------------------------------------------
  187. // ApplyBlock validates & executes the block, updates state w/ ABCI responses,
  188. // then commits and updates the mempool atomically, then saves state.
  189. // ApplyBlock validates the block against the state, executes it against the app,
  190. // commits it, and saves the block and state. It's the only function that needs to be called
  191. // from outside this package to process and commit an entire block.
  192. func (s *State) ApplyBlock(txEventPublisher types.TxEventPublisher, proxyAppConn proxy.AppConnConsensus,
  193. block *types.Block, partsHeader types.PartSetHeader, mempool types.Mempool) error {
  194. abciResponses, err := s.ValExecBlock(txEventPublisher, proxyAppConn, block)
  195. if err != nil {
  196. return fmt.Errorf("Exec failed for application: %v", err)
  197. }
  198. fail.Fail() // XXX
  199. // save the results before we commit
  200. s.SaveABCIResponses(abciResponses)
  201. fail.Fail() // XXX
  202. // now update the block and validators
  203. s.SetBlockAndValidators(block.Header, partsHeader, abciResponses)
  204. // lock mempool, commit state, update mempoool
  205. err = s.CommitStateUpdateMempool(proxyAppConn, block, mempool)
  206. if err != nil {
  207. return fmt.Errorf("Commit failed for application: %v", err)
  208. }
  209. fail.Fail() // XXX
  210. // save the state and the validators
  211. s.Save()
  212. return nil
  213. }
  214. // CommitStateUpdateMempool locks the mempool, runs the ABCI Commit message, and updates the mempool.
  215. // The Mempool must be locked during commit and update because state is typically reset on Commit and old txs must be replayed
  216. // against committed state before new txs are run in the mempool, lest they be invalid.
  217. func (s *State) CommitStateUpdateMempool(proxyAppConn proxy.AppConnConsensus, block *types.Block, mempool types.Mempool) error {
  218. mempool.Lock()
  219. defer mempool.Unlock()
  220. // Commit block, get hash back
  221. res, err := proxyAppConn.CommitSync()
  222. if err != nil {
  223. s.logger.Error("Client error during proxyAppConn.CommitSync", "err", err)
  224. return err
  225. }
  226. if res.IsErr() {
  227. s.logger.Error("Error in proxyAppConn.CommitSync", "err", res)
  228. return res
  229. }
  230. if res.Log != "" {
  231. s.logger.Debug("Commit.Log: " + res.Log)
  232. }
  233. s.logger.Info("Committed state", "height", block.Height, "txs", block.NumTxs, "hash", res.Data)
  234. // Set the state's new AppHash
  235. s.AppHash = res.Data
  236. // Update mempool.
  237. return mempool.Update(block.Height, block.Txs)
  238. }
  239. // ExecCommitBlock executes and commits a block on the proxyApp without validating or mutating the state.
  240. // It returns the application root hash (result of abci.Commit).
  241. func ExecCommitBlock(appConnConsensus proxy.AppConnConsensus, block *types.Block, logger log.Logger, lastValidators *types.ValidatorSet) ([]byte, error) {
  242. _, err := execBlockOnProxyApp(types.NopEventBus{}, appConnConsensus, block, logger, lastValidators)
  243. if err != nil {
  244. logger.Error("Error executing block on proxy app", "height", block.Height, "err", err)
  245. return nil, err
  246. }
  247. // Commit block, get hash back
  248. res, err := appConnConsensus.CommitSync()
  249. if err != nil {
  250. logger.Error("Client error during proxyAppConn.CommitSync", "err", res)
  251. return nil, err
  252. }
  253. if res.IsErr() {
  254. logger.Error("Error in proxyAppConn.CommitSync", "err", res)
  255. return nil, res
  256. }
  257. if res.Log != "" {
  258. logger.Info("Commit.Log: " + res.Log)
  259. }
  260. return res.Data, nil
  261. }