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.

229 lines
6.5 KiB

10 years ago
  1. package blockchain
  2. import (
  3. "bytes"
  4. "encoding/json"
  5. "fmt"
  6. "io"
  7. "github.com/tendermint/tendermint/binary"
  8. . "github.com/tendermint/tendermint/common"
  9. dbm "github.com/tendermint/tendermint/db"
  10. "github.com/tendermint/tendermint/types"
  11. )
  12. /*
  13. Simple low level store for blocks.
  14. There are three types of information stored:
  15. - BlockMeta: Meta information about each block
  16. - Block part: Parts of each block, aggregated w/ PartSet
  17. - Validation: The Validation part of each block, for gossiping precommit votes
  18. Currently the precommit signatures are duplicated in the Block parts as
  19. well as the Validation. In the future this may change, perhaps by moving
  20. the Validation data outside the Block.
  21. */
  22. type BlockStore struct {
  23. height int
  24. db dbm.DB
  25. }
  26. func NewBlockStore(db dbm.DB) *BlockStore {
  27. bsjson := LoadBlockStoreStateJSON(db)
  28. return &BlockStore{
  29. height: bsjson.Height,
  30. db: db,
  31. }
  32. }
  33. // Height() returns the last known contiguous block height.
  34. func (bs *BlockStore) Height() int {
  35. return bs.height
  36. }
  37. func (bs *BlockStore) GetReader(key []byte) io.Reader {
  38. bytez := bs.db.Get(key)
  39. if bytez == nil {
  40. return nil
  41. }
  42. return bytes.NewReader(bytez)
  43. }
  44. func (bs *BlockStore) LoadBlock(height int) *types.Block {
  45. var n int64
  46. var err error
  47. r := bs.GetReader(calcBlockMetaKey(height))
  48. if r == nil {
  49. panic(Fmt("Block does not exist at height %v", height))
  50. }
  51. meta := binary.ReadBinary(&types.BlockMeta{}, r, &n, &err).(*types.BlockMeta)
  52. if err != nil {
  53. panic(Fmt("Error reading block meta: %v", err))
  54. }
  55. bytez := []byte{}
  56. for i := 0; i < meta.PartsHeader.Total; i++ {
  57. part := bs.LoadBlockPart(height, i)
  58. bytez = append(bytez, part.Bytes...)
  59. }
  60. block := binary.ReadBinary(&types.Block{}, bytes.NewReader(bytez), &n, &err).(*types.Block)
  61. if err != nil {
  62. panic(Fmt("Error reading block: %v", err))
  63. }
  64. return block
  65. }
  66. func (bs *BlockStore) LoadBlockPart(height int, index int) *types.Part {
  67. var n int64
  68. var err error
  69. r := bs.GetReader(calcBlockPartKey(height, index))
  70. if r == nil {
  71. panic(Fmt("BlockPart does not exist for height %v index %v", height, index))
  72. }
  73. part := binary.ReadBinary(&types.Part{}, r, &n, &err).(*types.Part)
  74. if err != nil {
  75. panic(Fmt("Error reading block part: %v", err))
  76. }
  77. return part
  78. }
  79. func (bs *BlockStore) LoadBlockMeta(height int) *types.BlockMeta {
  80. var n int64
  81. var err error
  82. r := bs.GetReader(calcBlockMetaKey(height))
  83. if r == nil {
  84. panic(Fmt("BlockMeta does not exist for height %v", height))
  85. }
  86. meta := binary.ReadBinary(&types.BlockMeta{}, r, &n, &err).(*types.BlockMeta)
  87. if err != nil {
  88. panic(Fmt("Error reading block meta: %v", err))
  89. }
  90. return meta
  91. }
  92. // The +2/3 and other Precommit-votes for block at `height`.
  93. // This Validation comes from block.LastValidation for `height+1`.
  94. func (bs *BlockStore) LoadBlockValidation(height int) *types.Validation {
  95. var n int64
  96. var err error
  97. r := bs.GetReader(calcBlockValidationKey(height))
  98. if r == nil {
  99. panic(Fmt("BlockValidation does not exist for height %v", height))
  100. }
  101. validation := binary.ReadBinary(&types.Validation{}, r, &n, &err).(*types.Validation)
  102. if err != nil {
  103. panic(Fmt("Error reading validation: %v", err))
  104. }
  105. return validation
  106. }
  107. // NOTE: the Precommit-vote heights are for the block at `height`
  108. func (bs *BlockStore) LoadSeenValidation(height int) *types.Validation {
  109. var n int64
  110. var err error
  111. r := bs.GetReader(calcSeenValidationKey(height))
  112. if r == nil {
  113. panic(Fmt("SeenValidation does not exist for height %v", height))
  114. }
  115. validation := binary.ReadBinary(&types.Validation{}, r, &n, &err).(*types.Validation)
  116. if err != nil {
  117. panic(Fmt("Error reading validation: %v", err))
  118. }
  119. return validation
  120. }
  121. // blockParts: Must be parts of the block
  122. // seenValidation: The +2/3 precommits that were seen which committed at height.
  123. // If all the nodes restart after committing a block,
  124. // we need this to reload the precommits to catch-up nodes to the
  125. // most recent height. Otherwise they'd stall at H-1.
  126. func (bs *BlockStore) SaveBlock(block *types.Block, blockParts *types.PartSet, seenValidation *types.Validation) {
  127. height := block.Height
  128. if height != bs.height+1 {
  129. panic(Fmt("BlockStore can only save contiguous blocks. Wanted %v, got %v", bs.height+1, height))
  130. }
  131. if !blockParts.IsComplete() {
  132. panic(Fmt("BlockStore can only save complete block part sets"))
  133. }
  134. // Save block meta
  135. meta := types.NewBlockMeta(block, blockParts)
  136. metaBytes := binary.BinaryBytes(meta)
  137. bs.db.Set(calcBlockMetaKey(height), metaBytes)
  138. // Save block parts
  139. for i := 0; i < blockParts.Total(); i++ {
  140. bs.saveBlockPart(height, i, blockParts.GetPart(i))
  141. }
  142. // Save block validation (duplicate and separate from the Block)
  143. blockValidationBytes := binary.BinaryBytes(block.LastValidation)
  144. bs.db.Set(calcBlockValidationKey(height-1), blockValidationBytes)
  145. // Save seen validation (seen +2/3 precommits for block)
  146. seenValidationBytes := binary.BinaryBytes(seenValidation)
  147. bs.db.Set(calcSeenValidationKey(height), seenValidationBytes)
  148. // Save new BlockStoreStateJSON descriptor
  149. BlockStoreStateJSON{Height: height}.Save(bs.db)
  150. // Done!
  151. bs.height = height
  152. }
  153. func (bs *BlockStore) saveBlockPart(height int, index int, part *types.Part) {
  154. if height != bs.height+1 {
  155. panic(Fmt("BlockStore can only save contiguous blocks. Wanted %v, got %v", bs.height+1, height))
  156. }
  157. partBytes := binary.BinaryBytes(part)
  158. bs.db.Set(calcBlockPartKey(height, index), partBytes)
  159. }
  160. //-----------------------------------------------------------------------------
  161. func calcBlockMetaKey(height int) []byte {
  162. return []byte(fmt.Sprintf("H:%v", height))
  163. }
  164. func calcBlockPartKey(height int, partIndex int) []byte {
  165. return []byte(fmt.Sprintf("P:%v:%v", height, partIndex))
  166. }
  167. func calcBlockValidationKey(height int) []byte {
  168. return []byte(fmt.Sprintf("V:%v", height))
  169. }
  170. func calcSeenValidationKey(height int) []byte {
  171. return []byte(fmt.Sprintf("SV:%v", height))
  172. }
  173. //-----------------------------------------------------------------------------
  174. var blockStoreKey = []byte("blockStore")
  175. type BlockStoreStateJSON struct {
  176. Height int
  177. }
  178. func (bsj BlockStoreStateJSON) Save(db dbm.DB) {
  179. bytes, err := json.Marshal(bsj)
  180. if err != nil {
  181. panic(Fmt("Could not marshal state bytes: %v", err))
  182. }
  183. db.Set(blockStoreKey, bytes)
  184. }
  185. func LoadBlockStoreStateJSON(db dbm.DB) BlockStoreStateJSON {
  186. bytes := db.Get(blockStoreKey)
  187. if bytes == nil {
  188. return BlockStoreStateJSON{
  189. Height: 0,
  190. }
  191. }
  192. bsj := BlockStoreStateJSON{}
  193. err := json.Unmarshal(bytes, &bsj)
  194. if err != nil {
  195. panic(Fmt("Could not unmarshal bytes: %X", bytes))
  196. }
  197. return bsj
  198. }