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.

354 lines
10 KiB

10 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
8 years ago
8 years ago
8 years ago
  1. package state
  2. import (
  3. "bytes"
  4. "errors"
  5. "fmt"
  6. "os"
  7. "time"
  8. "github.com/gogo/protobuf/proto"
  9. tmtime "github.com/tendermint/tendermint/libs/time"
  10. tmstate "github.com/tendermint/tendermint/proto/tendermint/state"
  11. tmversion "github.com/tendermint/tendermint/proto/tendermint/version"
  12. "github.com/tendermint/tendermint/types"
  13. "github.com/tendermint/tendermint/version"
  14. )
  15. //-----------------------------------------------------------------------------
  16. type Version struct {
  17. Consensus version.Consensus ` json:"consensus"`
  18. Software string ` json:"software"`
  19. }
  20. // InitStateVersion sets the Consensus.Block and Software versions,
  21. // but leaves the Consensus.App version blank.
  22. // The Consensus.App version will be set during the Handshake, once
  23. // we hear from the app what protocol version it is running.
  24. var InitStateVersion = Version{
  25. Consensus: version.Consensus{
  26. Block: version.BlockProtocol,
  27. App: 0,
  28. },
  29. Software: version.TMVersion,
  30. }
  31. func (v *Version) ToProto() tmstate.Version {
  32. return tmstate.Version{
  33. Consensus: tmversion.Consensus{
  34. Block: v.Consensus.Block,
  35. App: v.Consensus.App,
  36. },
  37. Software: v.Software,
  38. }
  39. }
  40. func VersionFromProto(v tmstate.Version) Version {
  41. return Version{
  42. Consensus: version.Consensus{
  43. Block: v.Consensus.Block,
  44. App: v.Consensus.App,
  45. },
  46. Software: v.Software,
  47. }
  48. }
  49. //-----------------------------------------------------------------------------
  50. // State is a short description of the latest committed block of the Tendermint consensus.
  51. // It keeps all information necessary to validate new blocks,
  52. // including the last validator set and the consensus params.
  53. // All fields are exposed so the struct can be easily serialized,
  54. // but none of them should be mutated directly.
  55. // Instead, use state.Copy() or updateState(...).
  56. // NOTE: not goroutine-safe.
  57. type State struct {
  58. // FIXME: This can be removed as TMVersion is a constant, and version.Consensus should
  59. // eventually be replaced by VersionParams in ConsensusParams
  60. Version Version
  61. // immutable
  62. ChainID string
  63. InitialHeight int64 // should be 1, not 0, when starting from height 1
  64. // LastBlockHeight=0 at genesis (ie. block(H=0) does not exist)
  65. LastBlockHeight int64
  66. LastBlockID types.BlockID
  67. LastBlockTime time.Time
  68. // LastValidators is used to validate block.LastCommit.
  69. // Validators are persisted to the database separately every time they change,
  70. // so we can query for historical validator sets.
  71. // Note that if s.LastBlockHeight causes a valset change,
  72. // we set s.LastHeightValidatorsChanged = s.LastBlockHeight + 1 + 1
  73. // Extra +1 due to nextValSet delay.
  74. NextValidators *types.ValidatorSet
  75. Validators *types.ValidatorSet
  76. LastValidators *types.ValidatorSet
  77. LastHeightValidatorsChanged int64
  78. // Consensus parameters used for validating blocks.
  79. // Changes returned by FinalizeBlock and updated after Commit.
  80. ConsensusParams types.ConsensusParams
  81. LastHeightConsensusParamsChanged int64
  82. // Merkle root of the results from executing prev block
  83. LastResultsHash []byte
  84. // the latest AppHash we've received from calling abci.Commit()
  85. AppHash []byte
  86. }
  87. // Copy makes a copy of the State for mutating.
  88. func (state State) Copy() State {
  89. return State{
  90. Version: state.Version,
  91. ChainID: state.ChainID,
  92. InitialHeight: state.InitialHeight,
  93. LastBlockHeight: state.LastBlockHeight,
  94. LastBlockID: state.LastBlockID,
  95. LastBlockTime: state.LastBlockTime,
  96. NextValidators: state.NextValidators.Copy(),
  97. Validators: state.Validators.Copy(),
  98. LastValidators: state.LastValidators.Copy(),
  99. LastHeightValidatorsChanged: state.LastHeightValidatorsChanged,
  100. ConsensusParams: state.ConsensusParams,
  101. LastHeightConsensusParamsChanged: state.LastHeightConsensusParamsChanged,
  102. AppHash: state.AppHash,
  103. LastResultsHash: state.LastResultsHash,
  104. }
  105. }
  106. // Equals returns true if the States are identical.
  107. func (state State) Equals(state2 State) (bool, error) {
  108. sbz, err := state.Bytes()
  109. if err != nil {
  110. return false, err
  111. }
  112. s2bz, err := state2.Bytes()
  113. if err != nil {
  114. return false, err
  115. }
  116. return bytes.Equal(sbz, s2bz), nil
  117. }
  118. // Bytes serializes the State using protobuf, propagating marshaling
  119. // errors
  120. func (state State) Bytes() ([]byte, error) {
  121. sm, err := state.ToProto()
  122. if err != nil {
  123. return nil, err
  124. }
  125. bz, err := proto.Marshal(sm)
  126. if err != nil {
  127. return nil, err
  128. }
  129. return bz, nil
  130. }
  131. // IsEmpty returns true if the State is equal to the empty State.
  132. func (state State) IsEmpty() bool {
  133. return state.Validators == nil // XXX can't compare to Empty
  134. }
  135. // ToProto takes the local state type and returns the equivalent proto type
  136. func (state *State) ToProto() (*tmstate.State, error) {
  137. if state == nil {
  138. return nil, errors.New("state is nil")
  139. }
  140. sm := new(tmstate.State)
  141. sm.Version = state.Version.ToProto()
  142. sm.ChainID = state.ChainID
  143. sm.InitialHeight = state.InitialHeight
  144. sm.LastBlockHeight = state.LastBlockHeight
  145. sm.LastBlockID = state.LastBlockID.ToProto()
  146. sm.LastBlockTime = state.LastBlockTime
  147. vals, err := state.Validators.ToProto()
  148. if err != nil {
  149. return nil, err
  150. }
  151. sm.Validators = vals
  152. nVals, err := state.NextValidators.ToProto()
  153. if err != nil {
  154. return nil, err
  155. }
  156. sm.NextValidators = nVals
  157. if state.LastBlockHeight >= 1 { // At Block 1 LastValidators is nil
  158. lVals, err := state.LastValidators.ToProto()
  159. if err != nil {
  160. return nil, err
  161. }
  162. sm.LastValidators = lVals
  163. }
  164. sm.LastHeightValidatorsChanged = state.LastHeightValidatorsChanged
  165. sm.ConsensusParams = state.ConsensusParams.ToProto()
  166. sm.LastHeightConsensusParamsChanged = state.LastHeightConsensusParamsChanged
  167. sm.LastResultsHash = state.LastResultsHash
  168. sm.AppHash = state.AppHash
  169. return sm, nil
  170. }
  171. // FromProto takes a state proto message & returns the local state type
  172. func FromProto(pb *tmstate.State) (*State, error) {
  173. if pb == nil {
  174. return nil, errors.New("nil State")
  175. }
  176. state := new(State)
  177. state.Version = VersionFromProto(pb.Version)
  178. state.ChainID = pb.ChainID
  179. state.InitialHeight = pb.InitialHeight
  180. bi, err := types.BlockIDFromProto(&pb.LastBlockID)
  181. if err != nil {
  182. return nil, err
  183. }
  184. state.LastBlockID = *bi
  185. state.LastBlockHeight = pb.LastBlockHeight
  186. state.LastBlockTime = pb.LastBlockTime
  187. vals, err := types.ValidatorSetFromProto(pb.Validators)
  188. if err != nil {
  189. return nil, err
  190. }
  191. state.Validators = vals
  192. nVals, err := types.ValidatorSetFromProto(pb.NextValidators)
  193. if err != nil {
  194. return nil, err
  195. }
  196. state.NextValidators = nVals
  197. if state.LastBlockHeight >= 1 { // At Block 1 LastValidators is nil
  198. lVals, err := types.ValidatorSetFromProto(pb.LastValidators)
  199. if err != nil {
  200. return nil, err
  201. }
  202. state.LastValidators = lVals
  203. } else {
  204. state.LastValidators = types.NewValidatorSet(nil)
  205. }
  206. state.LastHeightValidatorsChanged = pb.LastHeightValidatorsChanged
  207. state.ConsensusParams = types.ConsensusParamsFromProto(pb.ConsensusParams)
  208. state.LastHeightConsensusParamsChanged = pb.LastHeightConsensusParamsChanged
  209. state.LastResultsHash = pb.LastResultsHash
  210. state.AppHash = pb.AppHash
  211. return state, nil
  212. }
  213. //------------------------------------------------------------------------
  214. // Create a block from the latest state
  215. // MakeBlock builds a block from the current state with the given txs, commit,
  216. // and evidence. Note it also takes a proposerAddress because the state does not
  217. // track rounds, and hence does not know the correct proposer. TODO: fix this!
  218. func (state State) MakeBlock(
  219. height int64,
  220. txs []types.Tx,
  221. commit *types.Commit,
  222. evidence []types.Evidence,
  223. proposerAddress []byte,
  224. ) *types.Block {
  225. // Build base block with block data.
  226. block := types.MakeBlock(height, txs, commit, evidence)
  227. // Fill rest of header with state data.
  228. block.Header.Populate(
  229. state.Version.Consensus, state.ChainID,
  230. tmtime.Now(), state.LastBlockID,
  231. state.Validators.Hash(), state.NextValidators.Hash(),
  232. state.ConsensusParams.HashConsensusParams(), state.AppHash, state.LastResultsHash,
  233. proposerAddress,
  234. )
  235. return block
  236. }
  237. //------------------------------------------------------------------------
  238. // Genesis
  239. // MakeGenesisStateFromFile reads and unmarshals state from the given
  240. // file.
  241. //
  242. // Used during replay and in tests.
  243. func MakeGenesisStateFromFile(genDocFile string) (State, error) {
  244. genDoc, err := MakeGenesisDocFromFile(genDocFile)
  245. if err != nil {
  246. return State{}, err
  247. }
  248. return MakeGenesisState(genDoc)
  249. }
  250. // MakeGenesisDocFromFile reads and unmarshals genesis doc from the given file.
  251. func MakeGenesisDocFromFile(genDocFile string) (*types.GenesisDoc, error) {
  252. genDocJSON, err := os.ReadFile(genDocFile)
  253. if err != nil {
  254. return nil, fmt.Errorf("couldn't read GenesisDoc file: %w", err)
  255. }
  256. genDoc, err := types.GenesisDocFromJSON(genDocJSON)
  257. if err != nil {
  258. return nil, fmt.Errorf("error reading GenesisDoc: %w", err)
  259. }
  260. return genDoc, nil
  261. }
  262. // MakeGenesisState creates state from types.GenesisDoc.
  263. func MakeGenesisState(genDoc *types.GenesisDoc) (State, error) {
  264. err := genDoc.ValidateAndComplete()
  265. if err != nil {
  266. return State{}, fmt.Errorf("error in genesis doc: %w", err)
  267. }
  268. var validatorSet, nextValidatorSet *types.ValidatorSet
  269. if genDoc.Validators == nil || len(genDoc.Validators) == 0 {
  270. validatorSet = types.NewValidatorSet(nil)
  271. nextValidatorSet = types.NewValidatorSet(nil)
  272. } else {
  273. validators := make([]*types.Validator, len(genDoc.Validators))
  274. for i, val := range genDoc.Validators {
  275. validators[i] = types.NewValidator(val.PubKey, val.Power)
  276. }
  277. validatorSet = types.NewValidatorSet(validators)
  278. nextValidatorSet = types.NewValidatorSet(validators).CopyIncrementProposerPriority(1)
  279. }
  280. return State{
  281. Version: InitStateVersion,
  282. ChainID: genDoc.ChainID,
  283. InitialHeight: genDoc.InitialHeight,
  284. LastBlockHeight: 0,
  285. LastBlockID: types.BlockID{},
  286. LastBlockTime: genDoc.GenesisTime,
  287. NextValidators: nextValidatorSet,
  288. Validators: validatorSet,
  289. LastValidators: types.NewValidatorSet(nil),
  290. LastHeightValidatorsChanged: genDoc.InitialHeight,
  291. ConsensusParams: *genDoc.ConsensusParams,
  292. LastHeightConsensusParamsChanged: genDoc.InitialHeight,
  293. AppHash: genDoc.AppHash,
  294. }, nil
  295. }