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.

232 lines
6.6 KiB

10 years ago
10 years ago
  1. package blockchain
  2. import (
  3. "bytes"
  4. "encoding/json"
  5. "fmt"
  6. "io"
  7. "github.com/tendermint/tendermint2/binary"
  8. . "github.com/tendermint/tendermint2/common"
  9. dbm "github.com/tendermint/tendermint2/db"
  10. "github.com/tendermint/tendermint2/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 commit votes
  18. Currently the commit 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 uint
  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() uint {
  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 uint) *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 := uint(0); i < meta.Parts.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 uint, index uint) *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 uint) *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. // NOTE: the Commit-vote heights are for the block at `height-1`
  93. // Since these are included in the subsequent block, the height
  94. // is off by 1.
  95. func (bs *BlockStore) LoadBlockValidation(height uint) *types.Validation {
  96. var n int64
  97. var err error
  98. r := bs.GetReader(calcBlockValidationKey(height))
  99. if r == nil {
  100. panic(Fmt("BlockValidation does not exist for height %v", height))
  101. }
  102. validation := binary.ReadBinary(&types.Validation{}, r, &n, &err).(*types.Validation)
  103. if err != nil {
  104. panic(Fmt("Error reading validation: %v", err))
  105. }
  106. return validation
  107. }
  108. // NOTE: the Commit-vote heights are for the block at `height`
  109. func (bs *BlockStore) LoadSeenValidation(height uint) *types.Validation {
  110. var n int64
  111. var err error
  112. r := bs.GetReader(calcSeenValidationKey(height))
  113. if r == nil {
  114. panic(Fmt("SeenValidation does not exist for height %v", height))
  115. }
  116. validation := binary.ReadBinary(&types.Validation{}, r, &n, &err).(*types.Validation)
  117. if err != nil {
  118. panic(Fmt("Error reading validation: %v", err))
  119. }
  120. return validation
  121. }
  122. // blockParts: Must be parts of the block
  123. // seenValidation: The +2/3 commits that were seen which finalized the height.
  124. // If all the nodes restart after committing a block,
  125. // we need this to reload the commits to catch-up nodes to the
  126. // most recent height. Otherwise they'd stall at H-1.
  127. // Also good to have to debug consensus issues & punish wrong-signers
  128. // whose commits weren't included in the block.
  129. func (bs *BlockStore) SaveBlock(block *types.Block, blockParts *types.PartSet, seenValidation *types.Validation) {
  130. height := block.Height
  131. if height != bs.height+1 {
  132. panic(Fmt("BlockStore can only save contiguous blocks. Wanted %v, got %v", bs.height+1, height))
  133. }
  134. if !blockParts.IsComplete() {
  135. panic(Fmt("BlockStore can only save complete block part sets"))
  136. }
  137. // Save block meta
  138. meta := types.NewBlockMeta(block, blockParts)
  139. metaBytes := binary.BinaryBytes(meta)
  140. bs.db.Set(calcBlockMetaKey(height), metaBytes)
  141. // Save block parts
  142. for i := uint(0); i < blockParts.Total(); i++ {
  143. bs.saveBlockPart(height, i, blockParts.GetPart(i))
  144. }
  145. // Save block validation (duplicate and separate from the Block)
  146. blockValidationBytes := binary.BinaryBytes(block.Validation)
  147. bs.db.Set(calcBlockValidationKey(height), blockValidationBytes)
  148. // Save seen validation (seen +2/3 commits)
  149. seenValidationBytes := binary.BinaryBytes(seenValidation)
  150. bs.db.Set(calcSeenValidationKey(height), seenValidationBytes)
  151. // Save new BlockStoreStateJSON descriptor
  152. BlockStoreStateJSON{Height: height}.Save(bs.db)
  153. // Done!
  154. bs.height = height
  155. }
  156. func (bs *BlockStore) saveBlockPart(height uint, index uint, part *types.Part) {
  157. if height != bs.height+1 {
  158. panic(Fmt("BlockStore can only save contiguous blocks. Wanted %v, got %v", bs.height+1, height))
  159. }
  160. partBytes := binary.BinaryBytes(part)
  161. bs.db.Set(calcBlockPartKey(height, index), partBytes)
  162. }
  163. //-----------------------------------------------------------------------------
  164. func calcBlockMetaKey(height uint) []byte {
  165. return []byte(fmt.Sprintf("H:%v", height))
  166. }
  167. func calcBlockPartKey(height uint, partIndex uint) []byte {
  168. return []byte(fmt.Sprintf("P:%v:%v", height, partIndex))
  169. }
  170. func calcBlockValidationKey(height uint) []byte {
  171. return []byte(fmt.Sprintf("V:%v", height))
  172. }
  173. func calcSeenValidationKey(height uint) []byte {
  174. return []byte(fmt.Sprintf("SV:%v", height))
  175. }
  176. //-----------------------------------------------------------------------------
  177. var blockStoreKey = []byte("blockStore")
  178. type BlockStoreStateJSON struct {
  179. Height uint
  180. }
  181. func (bsj BlockStoreStateJSON) Save(db dbm.DB) {
  182. bytes, err := json.Marshal(bsj)
  183. if err != nil {
  184. panic(Fmt("Could not marshal state bytes: %v", err))
  185. }
  186. db.Set(blockStoreKey, bytes)
  187. }
  188. func LoadBlockStoreStateJSON(db dbm.DB) BlockStoreStateJSON {
  189. bytes := db.Get(blockStoreKey)
  190. if bytes == nil {
  191. return BlockStoreStateJSON{
  192. Height: 0,
  193. }
  194. }
  195. bsj := BlockStoreStateJSON{}
  196. err := json.Unmarshal(bytes, &bsj)
  197. if err != nil {
  198. panic(Fmt("Could not unmarshal bytes: %X", bytes))
  199. }
  200. return bsj
  201. }