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.

353 lines
11 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
7 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
7 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
7 years ago
8 years ago
8 years ago
7 years ago
8 years ago
8 years ago
  1. package state
  2. import (
  3. "bytes"
  4. "errors"
  5. "fmt"
  6. fail "github.com/ebuchman/fail-test"
  7. abci "github.com/tendermint/abci/types"
  8. crypto "github.com/tendermint/go-crypto"
  9. "github.com/tendermint/tendermint/proxy"
  10. "github.com/tendermint/tendermint/types"
  11. cmn "github.com/tendermint/tmlibs/common"
  12. "github.com/tendermint/tmlibs/log"
  13. )
  14. //--------------------------------------------------
  15. // Execute the block
  16. // ValExecBlock executes the block, but does NOT mutate State.
  17. // + validates the block
  18. // + executes block.Txs on the proxyAppConn
  19. func (s *State) ValExecBlock(txEventPublisher types.TxEventPublisher, proxyAppConn proxy.AppConnConsensus, block *types.Block) (*ABCIResponses, error) {
  20. // Validate the block.
  21. if err := s.validateBlock(block); err != nil {
  22. return nil, ErrInvalidBlock(err)
  23. }
  24. // Execute the block txs
  25. abciResponses, err := execBlockOnProxyApp(txEventPublisher, proxyAppConn, block, s.logger, s.LastValidators)
  26. if err != nil {
  27. // There was some error in proxyApp
  28. // TODO Report error and wait for proxyApp to be available.
  29. return nil, ErrProxyAppConn(err)
  30. }
  31. return abciResponses, nil
  32. }
  33. // Executes block's transactions on proxyAppConn.
  34. // Returns a list of transaction results and updates to the validator set
  35. // TODO: Generate a bitmap or otherwise store tx validity in state.
  36. func execBlockOnProxyApp(txEventPublisher types.TxEventPublisher, proxyAppConn proxy.AppConnConsensus, block *types.Block, logger log.Logger, lastValidators *types.ValidatorSet) (*ABCIResponses, error) {
  37. var validTxs, invalidTxs = 0, 0
  38. txIndex := 0
  39. abciResponses := NewABCIResponses(block)
  40. // Execute transactions and get hash
  41. proxyCb := func(req *abci.Request, res *abci.Response) {
  42. switch r := res.Value.(type) {
  43. case *abci.Response_DeliverTx:
  44. // TODO: make use of res.Log
  45. // TODO: make use of this info
  46. // Blocks may include invalid txs.
  47. // reqDeliverTx := req.(abci.RequestDeliverTx)
  48. txResult := r.DeliverTx
  49. if txResult.Code == abci.CodeTypeOK {
  50. validTxs++
  51. } else {
  52. logger.Debug("Invalid tx", "code", txResult.Code, "log", txResult.Log)
  53. invalidTxs++
  54. }
  55. // NOTE: if we count we can access the tx from the block instead of
  56. // pulling it from the req
  57. txEventPublisher.PublishEventTx(types.EventDataTx{types.TxResult{
  58. Height: block.Height,
  59. Index: uint32(txIndex),
  60. Tx: types.Tx(req.GetDeliverTx().Tx),
  61. Result: *txResult,
  62. }})
  63. abciResponses.DeliverTx[txIndex] = txResult
  64. txIndex++
  65. }
  66. }
  67. proxyAppConn.SetResponseCallback(proxyCb)
  68. // determine which validators did not sign last block
  69. absentVals := make([]int32, 0)
  70. for valI, vote := range block.LastCommit.Precommits {
  71. if vote == nil {
  72. absentVals = append(absentVals, int32(valI))
  73. }
  74. }
  75. // Begin block
  76. _, err := proxyAppConn.BeginBlockSync(abci.RequestBeginBlock{
  77. Hash: block.Hash(),
  78. Header: types.TM2PB.Header(block.Header),
  79. AbsentValidators: absentVals,
  80. ByzantineValidators: nil,
  81. })
  82. if err != nil {
  83. logger.Error("Error in proxyAppConn.BeginBlock", "err", err)
  84. return nil, err
  85. }
  86. // Run txs of block
  87. for _, tx := range block.Txs {
  88. proxyAppConn.DeliverTxAsync(tx)
  89. if err := proxyAppConn.Error(); err != nil {
  90. return nil, err
  91. }
  92. }
  93. // End block
  94. abciResponses.EndBlock, err = proxyAppConn.EndBlockSync(abci.RequestEndBlock{block.Height})
  95. if err != nil {
  96. logger.Error("Error in proxyAppConn.EndBlock", "err", err)
  97. return nil, err
  98. }
  99. valUpdates := abciResponses.EndBlock.ValidatorUpdates
  100. logger.Info("Executed block", "height", block.Height, "validTxs", validTxs, "invalidTxs", invalidTxs)
  101. if len(valUpdates) > 0 {
  102. logger.Info("Updates to validators", "updates", abci.ValidatorsString(valUpdates))
  103. }
  104. return abciResponses, nil
  105. }
  106. func updateValidators(validators *types.ValidatorSet, changedValidators []*abci.Validator) error {
  107. // TODO: prevent change of 1/3+ at once
  108. for _, v := range changedValidators {
  109. pubkey, err := crypto.PubKeyFromBytes(v.PubKey) // NOTE: expects go-wire encoded pubkey
  110. if err != nil {
  111. return err
  112. }
  113. address := pubkey.Address()
  114. power := int64(v.Power)
  115. // mind the overflow from int64
  116. if power < 0 {
  117. return errors.New(cmn.Fmt("Power (%d) overflows int64", v.Power))
  118. }
  119. _, val := validators.GetByAddress(address)
  120. if val == nil {
  121. // add val
  122. added := validators.Add(types.NewValidator(pubkey, power))
  123. if !added {
  124. return errors.New(cmn.Fmt("Failed to add new validator %X with voting power %d", address, power))
  125. }
  126. } else if v.Power == 0 {
  127. // remove val
  128. _, removed := validators.Remove(address)
  129. if !removed {
  130. return errors.New(cmn.Fmt("Failed to remove validator %X)"))
  131. }
  132. } else {
  133. // update val
  134. val.VotingPower = power
  135. updated := validators.Update(val)
  136. if !updated {
  137. return errors.New(cmn.Fmt("Failed to update validator %X with voting power %d", address, power))
  138. }
  139. }
  140. }
  141. return nil
  142. }
  143. // return a bit array of validators that signed the last commit
  144. // NOTE: assumes commits have already been authenticated
  145. /* function is currently unused
  146. func commitBitArrayFromBlock(block *types.Block) *cmn.BitArray {
  147. signed := cmn.NewBitArray(len(block.LastCommit.Precommits))
  148. for i, precommit := range block.LastCommit.Precommits {
  149. if precommit != nil {
  150. signed.SetIndex(i, true) // val_.LastCommitHeight = block.Height - 1
  151. }
  152. }
  153. return signed
  154. }
  155. */
  156. //-----------------------------------------------------
  157. // Validate block
  158. // ValidateBlock validates the block against the state.
  159. func (s *State) ValidateBlock(block *types.Block) error {
  160. return s.validateBlock(block)
  161. }
  162. // MakeBlock builds a block with the given txs and commit from the current state.
  163. func (s *State) MakeBlock(height int64, txs []types.Tx, commit *types.Commit) (*types.Block, *types.PartSet) {
  164. // build base block
  165. block := types.MakeBlock(height, txs, commit)
  166. // fill header with state data
  167. block.ChainID = s.ChainID
  168. block.TotalTxs = s.LastBlockTotalTx + block.NumTxs
  169. block.LastBlockID = s.LastBlockID
  170. block.ValidatorsHash = s.Validators.Hash()
  171. block.AppHash = s.AppHash
  172. block.ConsensusHash = s.LastConsensusParams.Hash()
  173. return block, block.MakePartSet(s.ConsensusParams.BlockGossip.BlockPartSizeBytes)
  174. }
  175. func (s *State) validateBlock(b *types.Block) error {
  176. // Basic block validation.
  177. if err := b.ValidateBasic(); err != nil {
  178. return err
  179. }
  180. if b.ChainID != s.ChainID {
  181. return fmt.Errorf("Wrong Block.Header.ChainID. Expected %v, got %v", s.ChainID, b.ChainID)
  182. }
  183. if b.Height != s.LastBlockHeight+1 {
  184. return fmt.Errorf("Wrong Block.Header.Height. Expected %v, got %v", s.LastBlockHeight+1, b.Height)
  185. }
  186. /* TODO: Determine bounds for Time
  187. See blockchain/reactor "stopSyncingDurationMinutes"
  188. if !b.Time.After(lastBlockTime) {
  189. return errors.New("Invalid Block.Header.Time")
  190. }
  191. */
  192. newTxs := int64(len(b.Data.Txs))
  193. if b.TotalTxs != s.LastBlockTotalTx+newTxs {
  194. return fmt.Errorf("Wrong Block.Header.TotalTxs. Expected %v, got %v", s.LastBlockTotalTx+newTxs, b.TotalTxs)
  195. }
  196. if !b.LastBlockID.Equals(s.LastBlockID) {
  197. return fmt.Errorf("Wrong Block.Header.LastBlockID. Expected %v, got %v", s.LastBlockID, b.LastBlockID)
  198. }
  199. if !bytes.Equal(b.AppHash, s.AppHash) {
  200. return fmt.Errorf("Wrong Block.Header.AppHash. Expected %X, got %v", s.AppHash, b.AppHash)
  201. }
  202. if !bytes.Equal(b.ConsensusHash, s.LastConsensusParams.Hash()) {
  203. return fmt.Errorf("Wrong Block.Header.ConsensusHash. Expected %X, got %v", s.LastConsensusParams.Hash(), b.ConsensusHash)
  204. }
  205. // Validate block LastCommit.
  206. if b.Height == 1 {
  207. if len(b.LastCommit.Precommits) != 0 {
  208. return errors.New("Block at height 1 (first block) should have no LastCommit precommits")
  209. }
  210. } else {
  211. if len(b.LastCommit.Precommits) != s.LastValidators.Size() {
  212. return errors.New(cmn.Fmt("Invalid block commit size. Expected %v, got %v",
  213. s.LastValidators.Size(), len(b.LastCommit.Precommits)))
  214. }
  215. err := s.LastValidators.VerifyCommit(
  216. s.ChainID, s.LastBlockID, b.Height-1, b.LastCommit)
  217. if err != nil {
  218. return err
  219. }
  220. }
  221. return nil
  222. }
  223. //-----------------------------------------------------------------------------
  224. // ApplyBlock validates & executes the block, updates state w/ ABCI responses,
  225. // then commits and updates the mempool atomically, then saves state.
  226. // ApplyBlock validates the block against the state, executes it against the app,
  227. // commits it, and saves the block and state. It's the only function that needs to be called
  228. // from outside this package to process and commit an entire block.
  229. func (s *State) ApplyBlock(txEventPublisher types.TxEventPublisher, proxyAppConn proxy.AppConnConsensus,
  230. block *types.Block, partsHeader types.PartSetHeader, mempool types.Mempool) error {
  231. abciResponses, err := s.ValExecBlock(txEventPublisher, proxyAppConn, block)
  232. if err != nil {
  233. return fmt.Errorf("Exec failed for application: %v", err)
  234. }
  235. fail.Fail() // XXX
  236. // save the results before we commit
  237. s.SaveABCIResponses(abciResponses)
  238. fail.Fail() // XXX
  239. // now update the block and validators
  240. err = s.SetBlockAndValidators(block.Header, partsHeader, abciResponses)
  241. if err != nil {
  242. return fmt.Errorf("Commit failed for application: %v", err)
  243. }
  244. // lock mempool, commit state, update mempoool
  245. err = s.CommitStateUpdateMempool(proxyAppConn, block, mempool)
  246. if err != nil {
  247. return fmt.Errorf("Commit failed for application: %v", err)
  248. }
  249. fail.Fail() // XXX
  250. // save the state and the validators
  251. s.Save()
  252. return nil
  253. }
  254. // CommitStateUpdateMempool locks the mempool, runs the ABCI Commit message, and updates the mempool.
  255. // The Mempool must be locked during commit and update because state is typically reset on Commit and old txs must be replayed
  256. // against committed state before new txs are run in the mempool, lest they be invalid.
  257. func (s *State) CommitStateUpdateMempool(proxyAppConn proxy.AppConnConsensus, block *types.Block, mempool types.Mempool) error {
  258. mempool.Lock()
  259. defer mempool.Unlock()
  260. // Commit block, get hash back
  261. res, err := proxyAppConn.CommitSync()
  262. if err != nil {
  263. s.logger.Error("Client error during proxyAppConn.CommitSync", "err", err)
  264. return err
  265. }
  266. if res.IsErr() {
  267. s.logger.Error("Error in proxyAppConn.CommitSync", "err", res)
  268. return res
  269. }
  270. if res.Log != "" {
  271. s.logger.Debug("Commit.Log: " + res.Log)
  272. }
  273. s.logger.Info("Committed state", "height", block.Height, "txs", block.NumTxs, "hash", res.Data)
  274. // Set the state's new AppHash
  275. s.AppHash = res.Data
  276. // Update mempool.
  277. return mempool.Update(block.Height, block.Txs)
  278. }
  279. // ExecCommitBlock executes and commits a block on the proxyApp without validating or mutating the state.
  280. // It returns the application root hash (result of abci.Commit).
  281. func ExecCommitBlock(appConnConsensus proxy.AppConnConsensus, block *types.Block, logger log.Logger, lastValidators *types.ValidatorSet) ([]byte, error) {
  282. _, err := execBlockOnProxyApp(types.NopEventBus{}, appConnConsensus, block, logger, lastValidators)
  283. if err != nil {
  284. logger.Error("Error executing block on proxy app", "height", block.Height, "err", err)
  285. return nil, err
  286. }
  287. // Commit block, get hash back
  288. res, err := appConnConsensus.CommitSync()
  289. if err != nil {
  290. logger.Error("Client error during proxyAppConn.CommitSync", "err", res)
  291. return nil, err
  292. }
  293. if res.IsErr() {
  294. logger.Error("Error in proxyAppConn.CommitSync", "err", res)
  295. return nil, res
  296. }
  297. if res.Log != "" {
  298. logger.Info("Commit.Log: " + res.Log)
  299. }
  300. return res.Data, nil
  301. }