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.

243 lines
6.6 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. Panics indicate probable corruption in the data
  22. */
  23. type BlockStore struct {
  24. height int
  25. db dbm.DB
  26. }
  27. func NewBlockStore(db dbm.DB) *BlockStore {
  28. bsjson := LoadBlockStoreStateJSON(db)
  29. return &BlockStore{
  30. height: bsjson.Height,
  31. db: db,
  32. }
  33. }
  34. // Height() returns the last known contiguous block height.
  35. func (bs *BlockStore) Height() int {
  36. return bs.height
  37. }
  38. func (bs *BlockStore) GetReader(key []byte) io.Reader {
  39. bytez := bs.db.Get(key)
  40. if bytez == nil {
  41. return nil
  42. }
  43. return bytes.NewReader(bytez)
  44. }
  45. func (bs *BlockStore) LoadBlock(height int) *types.Block {
  46. var n int64
  47. var err error
  48. r := bs.GetReader(calcBlockMetaKey(height))
  49. if r == nil {
  50. return nil
  51. }
  52. meta := binary.ReadBinary(&types.BlockMeta{}, r, &n, &err).(*types.BlockMeta)
  53. if err != nil {
  54. // SOMETHING HAS GONE HORRIBLY WRONG
  55. panic(Fmt("Error reading block meta: %v", err))
  56. }
  57. bytez := []byte{}
  58. for i := 0; i < meta.PartsHeader.Total; i++ {
  59. part := bs.LoadBlockPart(height, i)
  60. bytez = append(bytez, part.Bytes...)
  61. }
  62. block := binary.ReadBinary(&types.Block{}, bytes.NewReader(bytez), &n, &err).(*types.Block)
  63. if err != nil {
  64. // SOMETHING HAS GONE HORRIBLY WRONG
  65. panic(Fmt("Error reading block: %v", err))
  66. }
  67. return block
  68. }
  69. func (bs *BlockStore) LoadBlockPart(height int, index int) *types.Part {
  70. var n int64
  71. var err error
  72. r := bs.GetReader(calcBlockPartKey(height, index))
  73. if r == nil {
  74. return nil
  75. }
  76. part := binary.ReadBinary(&types.Part{}, r, &n, &err).(*types.Part)
  77. if err != nil {
  78. // SOMETHING HAS GONE HORRIBLY WRONG
  79. panic(Fmt("Error reading block part: %v", err))
  80. }
  81. return part
  82. }
  83. func (bs *BlockStore) LoadBlockMeta(height int) *types.BlockMeta {
  84. var n int64
  85. var err error
  86. r := bs.GetReader(calcBlockMetaKey(height))
  87. if r == nil {
  88. return nil
  89. }
  90. meta := binary.ReadBinary(&types.BlockMeta{}, r, &n, &err).(*types.BlockMeta)
  91. if err != nil {
  92. // SOMETHING HAS GONE HORRIBLY WRONG
  93. panic(Fmt("Error reading block meta: %v", err))
  94. }
  95. return meta
  96. }
  97. // The +2/3 and other Precommit-votes for block at `height`.
  98. // This Validation comes from block.LastValidation for `height+1`.
  99. func (bs *BlockStore) LoadBlockValidation(height int) *types.Validation {
  100. var n int64
  101. var err error
  102. r := bs.GetReader(calcBlockValidationKey(height))
  103. if r == nil {
  104. return nil
  105. }
  106. validation := binary.ReadBinary(&types.Validation{}, r, &n, &err).(*types.Validation)
  107. if err != nil {
  108. // SOMETHING HAS GONE HORRIBLY WRONG
  109. panic(Fmt("Error reading validation: %v", err))
  110. }
  111. return validation
  112. }
  113. // NOTE: the Precommit-vote heights are for the block at `height`
  114. func (bs *BlockStore) LoadSeenValidation(height int) *types.Validation {
  115. var n int64
  116. var err error
  117. r := bs.GetReader(calcSeenValidationKey(height))
  118. if r == nil {
  119. return nil
  120. }
  121. validation := binary.ReadBinary(&types.Validation{}, r, &n, &err).(*types.Validation)
  122. if err != nil {
  123. // SOMETHING HAS GONE HORRIBLY WRONG
  124. panic(Fmt("Error reading validation: %v", err))
  125. }
  126. return validation
  127. }
  128. // blockParts: Must be parts of the block
  129. // seenValidation: The +2/3 precommits that were seen which committed at height.
  130. // If all the nodes restart after committing a block,
  131. // we need this to reload the precommits to catch-up nodes to the
  132. // most recent height. Otherwise they'd stall at H-1.
  133. func (bs *BlockStore) SaveBlock(block *types.Block, blockParts *types.PartSet, seenValidation *types.Validation) {
  134. height := block.Height
  135. if height != bs.height+1 {
  136. // SANITY CHECK
  137. panic(Fmt("BlockStore can only save contiguous blocks. Wanted %v, got %v", bs.height+1, height))
  138. }
  139. if !blockParts.IsComplete() {
  140. // SANITY CHECK
  141. panic(Fmt("BlockStore can only save complete block part sets"))
  142. }
  143. // Save block meta
  144. meta := types.NewBlockMeta(block, blockParts)
  145. metaBytes := binary.BinaryBytes(meta)
  146. bs.db.Set(calcBlockMetaKey(height), metaBytes)
  147. // Save block parts
  148. for i := 0; i < blockParts.Total(); i++ {
  149. bs.saveBlockPart(height, i, blockParts.GetPart(i))
  150. }
  151. // Save block validation (duplicate and separate from the Block)
  152. blockValidationBytes := binary.BinaryBytes(block.LastValidation)
  153. bs.db.Set(calcBlockValidationKey(height-1), blockValidationBytes)
  154. // Save seen validation (seen +2/3 precommits for block)
  155. seenValidationBytes := binary.BinaryBytes(seenValidation)
  156. bs.db.Set(calcSeenValidationKey(height), seenValidationBytes)
  157. // Save new BlockStoreStateJSON descriptor
  158. BlockStoreStateJSON{Height: height}.Save(bs.db)
  159. // Done!
  160. bs.height = height
  161. }
  162. func (bs *BlockStore) saveBlockPart(height int, index int, part *types.Part) {
  163. // SANITY CHECK
  164. if height != bs.height+1 {
  165. panic(Fmt("BlockStore can only save contiguous blocks. Wanted %v, got %v", bs.height+1, height))
  166. }
  167. // SANITY CHECK END
  168. partBytes := binary.BinaryBytes(part)
  169. bs.db.Set(calcBlockPartKey(height, index), partBytes)
  170. }
  171. //-----------------------------------------------------------------------------
  172. func calcBlockMetaKey(height int) []byte {
  173. return []byte(fmt.Sprintf("H:%v", height))
  174. }
  175. func calcBlockPartKey(height int, partIndex int) []byte {
  176. return []byte(fmt.Sprintf("P:%v:%v", height, partIndex))
  177. }
  178. func calcBlockValidationKey(height int) []byte {
  179. return []byte(fmt.Sprintf("V:%v", height))
  180. }
  181. func calcSeenValidationKey(height int) []byte {
  182. return []byte(fmt.Sprintf("SV:%v", height))
  183. }
  184. //-----------------------------------------------------------------------------
  185. var blockStoreKey = []byte("blockStore")
  186. type BlockStoreStateJSON struct {
  187. Height int
  188. }
  189. func (bsj BlockStoreStateJSON) Save(db dbm.DB) {
  190. bytes, err := json.Marshal(bsj)
  191. if err != nil {
  192. // SANITY CHECK
  193. panic(Fmt("Could not marshal state bytes: %v", err))
  194. }
  195. db.Set(blockStoreKey, bytes)
  196. }
  197. func LoadBlockStoreStateJSON(db dbm.DB) BlockStoreStateJSON {
  198. bytes := db.Get(blockStoreKey)
  199. if bytes == nil {
  200. return BlockStoreStateJSON{
  201. Height: 0,
  202. }
  203. }
  204. bsj := BlockStoreStateJSON{}
  205. err := json.Unmarshal(bytes, &bsj)
  206. if err != nil {
  207. // SOMETHING HAS GONE HORRIBLY WRONG
  208. panic(Fmt("Could not unmarshal bytes: %X", bytes))
  209. }
  210. return bsj
  211. }