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.

247 lines
6.9 KiB

10 years ago
10 years ago
  1. package block
  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. )
  11. /*
  12. Simple low level store for blocks.
  13. There are three types of information stored:
  14. - BlockMeta: Meta information about each block
  15. - Block part: Parts of each block, aggregated w/ PartSet
  16. - Validation: The Validation part of each block, for gossiping commit votes
  17. Currently the commit signatures are duplicated in the Block parts as
  18. well as the Validation. In the future this may change, perhaps by moving
  19. the Validation data outside the Block.
  20. */
  21. type BlockStore struct {
  22. height uint
  23. db dbm.DB
  24. }
  25. func NewBlockStore(db dbm.DB) *BlockStore {
  26. bsjson := LoadBlockStoreStateJSON(db)
  27. return &BlockStore{
  28. height: bsjson.Height,
  29. db: db,
  30. }
  31. }
  32. // Height() returns the last known contiguous block height.
  33. func (bs *BlockStore) Height() uint {
  34. return bs.height
  35. }
  36. func (bs *BlockStore) GetReader(key []byte) io.Reader {
  37. bytez := bs.db.Get(key)
  38. if bytez == nil {
  39. return nil
  40. }
  41. return bytes.NewReader(bytez)
  42. }
  43. func (bs *BlockStore) LoadBlock(height uint) *Block {
  44. var n int64
  45. var err error
  46. r := bs.GetReader(calcBlockMetaKey(height))
  47. if r == nil {
  48. panic(Fmt("Block does not exist at height %v", height))
  49. }
  50. meta := binary.ReadBinary(&BlockMeta{}, r, &n, &err).(*BlockMeta)
  51. if err != nil {
  52. panic(Fmt("Error reading block meta: %v", err))
  53. }
  54. bytez := []byte{}
  55. for i := uint(0); i < meta.Parts.Total; i++ {
  56. part := bs.LoadBlockPart(height, i)
  57. bytez = append(bytez, part.Bytes...)
  58. }
  59. block := binary.ReadBinary(&Block{}, bytes.NewReader(bytez), &n, &err).(*Block)
  60. if err != nil {
  61. panic(Fmt("Error reading block: %v", err))
  62. }
  63. return block
  64. }
  65. func (bs *BlockStore) LoadBlockPart(height uint, index uint) *Part {
  66. var n int64
  67. var err error
  68. r := bs.GetReader(calcBlockPartKey(height, index))
  69. if r == nil {
  70. panic(Fmt("BlockPart does not exist for height %v index %v", height, index))
  71. }
  72. part := binary.ReadBinary(&Part{}, r, &n, &err).(*Part)
  73. if err != nil {
  74. panic(Fmt("Error reading block part: %v", err))
  75. }
  76. return part
  77. }
  78. func (bs *BlockStore) LoadBlockMeta(height uint) *BlockMeta {
  79. var n int64
  80. var err error
  81. r := bs.GetReader(calcBlockMetaKey(height))
  82. if r == nil {
  83. panic(Fmt("BlockMeta does not exist for height %v", height))
  84. }
  85. meta := binary.ReadBinary(&BlockMeta{}, r, &n, &err).(*BlockMeta)
  86. if err != nil {
  87. panic(Fmt("Error reading block meta: %v", err))
  88. }
  89. return meta
  90. }
  91. // NOTE: the Commit-vote heights are for the block at `height-1`
  92. // Since these are included in the subsequent block, the height
  93. // is off by 1.
  94. func (bs *BlockStore) LoadBlockValidation(height uint) *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(&Validation{}, r, &n, &err).(*Validation)
  102. if err != nil {
  103. panic(Fmt("Error reading validation: %v", err))
  104. }
  105. return validation
  106. }
  107. // NOTE: the Commit-vote heights are for the block at `height`
  108. func (bs *BlockStore) LoadSeenValidation(height uint) *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(&Validation{}, r, &n, &err).(*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 commits that were seen which finalized the height.
  123. // If all the nodes restart after committing a block,
  124. // we need this to reload the commits to catch-up nodes to the
  125. // most recent height. Otherwise they'd stall at H-1.
  126. // Also good to have to debug consensus issues & punish wrong-signers
  127. // whose commits weren't included in the block.
  128. func (bs *BlockStore) SaveBlock(block *Block, blockParts *PartSet, seenValidation *Validation) {
  129. height := block.Height
  130. if height != bs.height+1 {
  131. panic(Fmt("BlockStore can only save contiguous blocks. Wanted %v, got %v", bs.height+1, height))
  132. }
  133. if !blockParts.IsComplete() {
  134. panic(Fmt("BlockStore can only save complete block part sets"))
  135. }
  136. // Save block meta
  137. meta := makeBlockMeta(block, blockParts)
  138. metaBytes := binary.BinaryBytes(meta)
  139. bs.db.Set(calcBlockMetaKey(height), metaBytes)
  140. // Save block parts
  141. for i := uint(0); i < blockParts.Total(); i++ {
  142. bs.saveBlockPart(height, i, blockParts.GetPart(i))
  143. }
  144. // Save block validation (duplicate and separate from the Block)
  145. blockValidationBytes := binary.BinaryBytes(block.Validation)
  146. bs.db.Set(calcBlockValidationKey(height), blockValidationBytes)
  147. // Save seen validation (seen +2/3 commits)
  148. seenValidationBytes := binary.BinaryBytes(seenValidation)
  149. bs.db.Set(calcSeenValidationKey(height), seenValidationBytes)
  150. // Save new BlockStoreStateJSON descriptor
  151. BlockStoreStateJSON{Height: height}.Save(bs.db)
  152. // Done!
  153. bs.height = height
  154. }
  155. func (bs *BlockStore) saveBlockPart(height uint, index uint, part *Part) {
  156. if height != bs.height+1 {
  157. panic(Fmt("BlockStore can only save contiguous blocks. Wanted %v, got %v", bs.height+1, height))
  158. }
  159. partBytes := binary.BinaryBytes(part)
  160. bs.db.Set(calcBlockPartKey(height, index), partBytes)
  161. }
  162. //-----------------------------------------------------------------------------
  163. type BlockMeta struct {
  164. Hash []byte // The block hash
  165. Header *Header // The block's Header
  166. Parts PartSetHeader // The PartSetHeader, for transfer
  167. }
  168. func makeBlockMeta(block *Block, blockParts *PartSet) *BlockMeta {
  169. return &BlockMeta{
  170. Hash: block.Hash(),
  171. Header: block.Header,
  172. Parts: blockParts.Header(),
  173. }
  174. }
  175. //-----------------------------------------------------------------------------
  176. func calcBlockMetaKey(height uint) []byte {
  177. return []byte(fmt.Sprintf("H:%v", height))
  178. }
  179. func calcBlockPartKey(height uint, partIndex uint) []byte {
  180. return []byte(fmt.Sprintf("P:%v:%v", height, partIndex))
  181. }
  182. func calcBlockValidationKey(height uint) []byte {
  183. return []byte(fmt.Sprintf("V:%v", height))
  184. }
  185. func calcSeenValidationKey(height uint) []byte {
  186. return []byte(fmt.Sprintf("SV:%v", height))
  187. }
  188. //-----------------------------------------------------------------------------
  189. var blockStoreKey = []byte("blockStore")
  190. type BlockStoreStateJSON struct {
  191. Height uint
  192. }
  193. func (bsj BlockStoreStateJSON) Save(db dbm.DB) {
  194. bytes, err := json.Marshal(bsj)
  195. if err != nil {
  196. panic(Fmt("Could not marshal state bytes: %v", err))
  197. }
  198. db.Set(blockStoreKey, bytes)
  199. }
  200. func LoadBlockStoreStateJSON(db dbm.DB) BlockStoreStateJSON {
  201. bytes := db.Get(blockStoreKey)
  202. if bytes == nil {
  203. return BlockStoreStateJSON{
  204. Height: 0,
  205. }
  206. }
  207. bsj := BlockStoreStateJSON{}
  208. err := json.Unmarshal(bytes, &bsj)
  209. if err != nil {
  210. panic(Fmt("Could not unmarshal bytes: %X", bytes))
  211. }
  212. return bsj
  213. }