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.

352 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
7 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 {
  108. sbz, s2bz := state.Bytes(), state2.Bytes()
  109. return bytes.Equal(sbz, s2bz)
  110. }
  111. // Bytes serializes the State using protobuf.
  112. // It panics if either casting to protobuf or serialization fails.
  113. func (state State) Bytes() []byte {
  114. sm, err := state.ToProto()
  115. if err != nil {
  116. panic(err)
  117. }
  118. bz, err := proto.Marshal(sm)
  119. if err != nil {
  120. panic(err)
  121. }
  122. return bz
  123. }
  124. // IsEmpty returns true if the State is equal to the empty State.
  125. func (state State) IsEmpty() bool {
  126. return state.Validators == nil // XXX can't compare to Empty
  127. }
  128. // ToProto takes the local state type and returns the equivalent proto type
  129. func (state *State) ToProto() (*tmstate.State, error) {
  130. if state == nil {
  131. return nil, errors.New("state is nil")
  132. }
  133. sm := new(tmstate.State)
  134. sm.Version = state.Version.ToProto()
  135. sm.ChainID = state.ChainID
  136. sm.InitialHeight = state.InitialHeight
  137. sm.LastBlockHeight = state.LastBlockHeight
  138. sm.LastBlockID = state.LastBlockID.ToProto()
  139. sm.LastBlockTime = state.LastBlockTime
  140. vals, err := state.Validators.ToProto()
  141. if err != nil {
  142. return nil, err
  143. }
  144. sm.Validators = vals
  145. nVals, err := state.NextValidators.ToProto()
  146. if err != nil {
  147. return nil, err
  148. }
  149. sm.NextValidators = nVals
  150. if state.LastBlockHeight >= 1 { // At Block 1 LastValidators is nil
  151. lVals, err := state.LastValidators.ToProto()
  152. if err != nil {
  153. return nil, err
  154. }
  155. sm.LastValidators = lVals
  156. }
  157. sm.LastHeightValidatorsChanged = state.LastHeightValidatorsChanged
  158. sm.ConsensusParams = state.ConsensusParams.ToProto()
  159. sm.LastHeightConsensusParamsChanged = state.LastHeightConsensusParamsChanged
  160. sm.LastResultsHash = state.LastResultsHash
  161. sm.AppHash = state.AppHash
  162. return sm, nil
  163. }
  164. // FromProto takes a state proto message & returns the local state type
  165. func FromProto(pb *tmstate.State) (*State, error) {
  166. if pb == nil {
  167. return nil, errors.New("nil State")
  168. }
  169. state := new(State)
  170. state.Version = VersionFromProto(pb.Version)
  171. state.ChainID = pb.ChainID
  172. state.InitialHeight = pb.InitialHeight
  173. bi, err := types.BlockIDFromProto(&pb.LastBlockID)
  174. if err != nil {
  175. return nil, err
  176. }
  177. state.LastBlockID = *bi
  178. state.LastBlockHeight = pb.LastBlockHeight
  179. state.LastBlockTime = pb.LastBlockTime
  180. vals, err := types.ValidatorSetFromProto(pb.Validators)
  181. if err != nil {
  182. return nil, err
  183. }
  184. state.Validators = vals
  185. nVals, err := types.ValidatorSetFromProto(pb.NextValidators)
  186. if err != nil {
  187. return nil, err
  188. }
  189. state.NextValidators = nVals
  190. if state.LastBlockHeight >= 1 { // At Block 1 LastValidators is nil
  191. lVals, err := types.ValidatorSetFromProto(pb.LastValidators)
  192. if err != nil {
  193. return nil, err
  194. }
  195. state.LastValidators = lVals
  196. } else {
  197. state.LastValidators = types.NewValidatorSet(nil)
  198. }
  199. state.LastHeightValidatorsChanged = pb.LastHeightValidatorsChanged
  200. state.ConsensusParams = types.ConsensusParamsFromProto(pb.ConsensusParams)
  201. state.LastHeightConsensusParamsChanged = pb.LastHeightConsensusParamsChanged
  202. state.LastResultsHash = pb.LastResultsHash
  203. state.AppHash = pb.AppHash
  204. return state, nil
  205. }
  206. //------------------------------------------------------------------------
  207. // Create a block from the latest state
  208. // MakeBlock builds a block from the current state with the given txs, commit,
  209. // and evidence. Note it also takes a proposerAddress because the state does not
  210. // track rounds, and hence does not know the correct proposer. TODO: fix this!
  211. func (state State) MakeBlock(
  212. height int64,
  213. txs []types.Tx,
  214. commit *types.Commit,
  215. evidence []types.Evidence,
  216. proposerAddress []byte,
  217. ) (*types.Block, *types.PartSet, error) {
  218. // Build base block with block data.
  219. block := types.MakeBlock(height, txs, commit, evidence)
  220. // Fill rest of header with state data.
  221. block.Header.Populate(
  222. state.Version.Consensus, state.ChainID,
  223. tmtime.Now(), state.LastBlockID,
  224. state.Validators.Hash(), state.NextValidators.Hash(),
  225. state.ConsensusParams.HashConsensusParams(), state.AppHash, state.LastResultsHash,
  226. proposerAddress,
  227. )
  228. bps, err := block.MakePartSet(types.BlockPartSizeBytes)
  229. if err != nil {
  230. return nil, nil, err
  231. }
  232. return block, bps, nil
  233. }
  234. //------------------------------------------------------------------------
  235. // Genesis
  236. // MakeGenesisStateFromFile reads and unmarshals state from the given
  237. // file.
  238. //
  239. // Used during replay and in tests.
  240. func MakeGenesisStateFromFile(genDocFile string) (State, error) {
  241. genDoc, err := MakeGenesisDocFromFile(genDocFile)
  242. if err != nil {
  243. return State{}, err
  244. }
  245. return MakeGenesisState(genDoc)
  246. }
  247. // MakeGenesisDocFromFile reads and unmarshals genesis doc from the given file.
  248. func MakeGenesisDocFromFile(genDocFile string) (*types.GenesisDoc, error) {
  249. genDocJSON, err := os.ReadFile(genDocFile)
  250. if err != nil {
  251. return nil, fmt.Errorf("couldn't read GenesisDoc file: %w", err)
  252. }
  253. genDoc, err := types.GenesisDocFromJSON(genDocJSON)
  254. if err != nil {
  255. return nil, fmt.Errorf("error reading GenesisDoc: %w", err)
  256. }
  257. return genDoc, nil
  258. }
  259. // MakeGenesisState creates state from types.GenesisDoc.
  260. func MakeGenesisState(genDoc *types.GenesisDoc) (State, error) {
  261. err := genDoc.ValidateAndComplete()
  262. if err != nil {
  263. return State{}, fmt.Errorf("error in genesis doc: %w", err)
  264. }
  265. var validatorSet, nextValidatorSet *types.ValidatorSet
  266. if genDoc.Validators == nil || len(genDoc.Validators) == 0 {
  267. validatorSet = types.NewValidatorSet(nil)
  268. nextValidatorSet = types.NewValidatorSet(nil)
  269. } else {
  270. validators := make([]*types.Validator, len(genDoc.Validators))
  271. for i, val := range genDoc.Validators {
  272. validators[i] = types.NewValidator(val.PubKey, val.Power)
  273. }
  274. validatorSet = types.NewValidatorSet(validators)
  275. nextValidatorSet = types.NewValidatorSet(validators).CopyIncrementProposerPriority(1)
  276. }
  277. return State{
  278. Version: InitStateVersion,
  279. ChainID: genDoc.ChainID,
  280. InitialHeight: genDoc.InitialHeight,
  281. LastBlockHeight: 0,
  282. LastBlockID: types.BlockID{},
  283. LastBlockTime: genDoc.GenesisTime,
  284. NextValidators: nextValidatorSet,
  285. Validators: validatorSet,
  286. LastValidators: types.NewValidatorSet(nil),
  287. LastHeightValidatorsChanged: genDoc.InitialHeight,
  288. ConsensusParams: *genDoc.ConsensusParams,
  289. LastHeightConsensusParamsChanged: genDoc.InitialHeight,
  290. AppHash: genDoc.AppHash,
  291. }, nil
  292. }