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.

248 lines
7.0 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 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(&BlockMeta{}, r, &n, &err).(*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) *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(&BlockMeta{}, r, &n, &err).(*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 := makeBlockMeta(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. type BlockMeta struct {
  165. Hash []byte // The block hash
  166. Header *types.Header // The block's Header
  167. Parts types.PartSetHeader // The PartSetHeader, for transfer
  168. }
  169. func makeBlockMeta(block *types.Block, blockParts *types.PartSet) *BlockMeta {
  170. return &BlockMeta{
  171. Hash: block.Hash(),
  172. Header: block.Header,
  173. Parts: blockParts.Header(),
  174. }
  175. }
  176. //-----------------------------------------------------------------------------
  177. func calcBlockMetaKey(height uint) []byte {
  178. return []byte(fmt.Sprintf("H:%v", height))
  179. }
  180. func calcBlockPartKey(height uint, partIndex uint) []byte {
  181. return []byte(fmt.Sprintf("P:%v:%v", height, partIndex))
  182. }
  183. func calcBlockValidationKey(height uint) []byte {
  184. return []byte(fmt.Sprintf("V:%v", height))
  185. }
  186. func calcSeenValidationKey(height uint) []byte {
  187. return []byte(fmt.Sprintf("SV:%v", height))
  188. }
  189. //-----------------------------------------------------------------------------
  190. var blockStoreKey = []byte("blockStore")
  191. type BlockStoreStateJSON struct {
  192. Height uint
  193. }
  194. func (bsj BlockStoreStateJSON) Save(db dbm.DB) {
  195. bytes, err := json.Marshal(bsj)
  196. if err != nil {
  197. panic(Fmt("Could not marshal state bytes: %v", err))
  198. }
  199. db.Set(blockStoreKey, bytes)
  200. }
  201. func LoadBlockStoreStateJSON(db dbm.DB) BlockStoreStateJSON {
  202. bytes := db.Get(blockStoreKey)
  203. if bytes == nil {
  204. return BlockStoreStateJSON{
  205. Height: 0,
  206. }
  207. }
  208. bsj := BlockStoreStateJSON{}
  209. err := json.Unmarshal(bytes, &bsj)
  210. if err != nil {
  211. panic(Fmt("Could not unmarshal bytes: %X", bytes))
  212. }
  213. return bsj
  214. }