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.

213 lines
5.4 KiB

10 years ago
10 years ago
  1. package block
  2. import (
  3. "bytes"
  4. "encoding/json"
  5. "fmt"
  6. . "github.com/tendermint/tendermint/binary"
  7. . "github.com/tendermint/tendermint/common"
  8. db_ "github.com/tendermint/tendermint/db"
  9. )
  10. /*
  11. Simple low level store for blocks.
  12. There are three types of information stored:
  13. - BlockMeta: Meta information about each block
  14. - Block part: Parts of each block, aggregated w/ PartSet
  15. - Validation: The Validation part of each block, for gossiping commit votes
  16. Currently the commit signatures are duplicated in the Block parts as
  17. well as the Validation. In the future this may change, perhaps by moving
  18. the Validation data outside the Block.
  19. */
  20. type BlockStore struct {
  21. height uint
  22. db db_.DB
  23. }
  24. func NewBlockStore(db db_.DB) *BlockStore {
  25. bsjson := LoadBlockStoreStateJSON(db)
  26. return &BlockStore{
  27. height: bsjson.Height,
  28. db: db,
  29. }
  30. }
  31. // Height() returns the last known contiguous block height.
  32. func (bs *BlockStore) Height() uint {
  33. return bs.height
  34. }
  35. func (bs *BlockStore) GetReader(key []byte) Unreader {
  36. bytez := bs.db.Get(key)
  37. if bytez == nil {
  38. return nil
  39. }
  40. return bytes.NewReader(bytez)
  41. }
  42. func (bs *BlockStore) LoadBlock(height uint) *Block {
  43. var n int64
  44. var err error
  45. r := bs.GetReader(calcBlockMetaKey(height))
  46. if r == nil {
  47. panic(Fmt("Block does not exist at height %v", height))
  48. }
  49. meta := ReadBinary(&BlockMeta{}, r, &n, &err).(*BlockMeta)
  50. if err != nil {
  51. panic(Fmt("Error reading block meta: %v", err))
  52. }
  53. bytez := []byte{}
  54. for i := uint(0); i < meta.Parts.Total; i++ {
  55. part := bs.LoadBlockPart(height, i)
  56. bytez = append(bytez, part.Bytes...)
  57. }
  58. block := ReadBinary(&Block{}, bytes.NewReader(bytez), &n, &err).(*Block)
  59. if err != nil {
  60. panic(Fmt("Error reading block: %v", err))
  61. }
  62. return block
  63. }
  64. func (bs *BlockStore) LoadBlockPart(height uint, index uint) *Part {
  65. var n int64
  66. var err error
  67. r := bs.GetReader(calcBlockPartKey(height, index))
  68. if r == nil {
  69. panic(Fmt("BlockPart does not exist for height %v index %v", height, index))
  70. }
  71. part := ReadBinary(&Part{}, r, &n, &err).(*Part)
  72. if err != nil {
  73. panic(Fmt("Error reading block part: %v", err))
  74. }
  75. return part
  76. }
  77. func (bs *BlockStore) LoadBlockMeta(height uint) *BlockMeta {
  78. var n int64
  79. var err error
  80. r := bs.GetReader(calcBlockMetaKey(height))
  81. if r == nil {
  82. panic(Fmt("BlockMeta does not exist for height %v", height))
  83. }
  84. meta := ReadBinary(&BlockMeta{}, r, &n, &err).(*BlockMeta)
  85. if err != nil {
  86. panic(Fmt("Error reading block meta: %v", err))
  87. }
  88. return meta
  89. }
  90. func (bs *BlockStore) LoadBlockValidation(height uint) *Validation {
  91. var n int64
  92. var err error
  93. r := bs.GetReader(calcBlockValidationKey(height))
  94. if r == nil {
  95. panic(Fmt("Validation does not exist for height %v", height))
  96. }
  97. validation := ReadBinary(&Validation{}, r, &n, &err).(*Validation)
  98. if err != nil {
  99. panic(Fmt("Error reading validation: %v", err))
  100. }
  101. return validation
  102. }
  103. func (bs *BlockStore) SaveBlock(block *Block, blockParts *PartSet) {
  104. height := block.Height
  105. if height != bs.height+1 {
  106. panic(Fmt("BlockStore can only save contiguous blocks. Wanted %v, got %v", bs.height+1, height))
  107. }
  108. if !blockParts.IsComplete() {
  109. panic(Fmt("BlockStore can only save complete block part sets"))
  110. }
  111. // Save block meta
  112. meta := makeBlockMeta(block, blockParts)
  113. metaBytes := BinaryBytes(meta)
  114. bs.db.Set(calcBlockMetaKey(height), metaBytes)
  115. // Save block parts
  116. for i := uint(0); i < blockParts.Total(); i++ {
  117. bs.saveBlockPart(height, i, blockParts.GetPart(i))
  118. }
  119. // Save block validation (duplicate and separate)
  120. validationBytes := BinaryBytes(block.Validation)
  121. bs.db.Set(calcBlockValidationKey(height), validationBytes)
  122. // Save new BlockStoreStateJSON descriptor
  123. BlockStoreStateJSON{Height: height}.Save(bs.db)
  124. // Done!
  125. bs.height = height
  126. }
  127. func (bs *BlockStore) saveBlockPart(height uint, index uint, part *Part) {
  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. partBytes := BinaryBytes(part)
  132. bs.db.Set(calcBlockPartKey(height, index), partBytes)
  133. }
  134. //-----------------------------------------------------------------------------
  135. type BlockMeta struct {
  136. Hash []byte // The block hash
  137. Header *Header // The block's Header
  138. Parts PartSetHeader // The PartSetHeader, for transfer
  139. }
  140. func makeBlockMeta(block *Block, blockParts *PartSet) *BlockMeta {
  141. return &BlockMeta{
  142. Hash: block.Hash(),
  143. Header: block.Header,
  144. Parts: blockParts.Header(),
  145. }
  146. }
  147. //-----------------------------------------------------------------------------
  148. func calcBlockMetaKey(height uint) []byte {
  149. return []byte(fmt.Sprintf("H:%v", height))
  150. }
  151. func calcBlockPartKey(height uint, partIndex uint) []byte {
  152. return []byte(fmt.Sprintf("P:%v:%v", height, partIndex))
  153. }
  154. func calcBlockValidationKey(height uint) []byte {
  155. return []byte(fmt.Sprintf("V:%v", height))
  156. }
  157. //-----------------------------------------------------------------------------
  158. var blockStoreKey = []byte("blockStore")
  159. type BlockStoreStateJSON struct {
  160. Height uint
  161. }
  162. func (bsj BlockStoreStateJSON) Save(db db_.DB) {
  163. bytes, err := json.Marshal(bsj)
  164. if err != nil {
  165. panic(Fmt("Could not marshal state bytes: %v", err))
  166. }
  167. db.Set(blockStoreKey, bytes)
  168. }
  169. func LoadBlockStoreStateJSON(db db_.DB) BlockStoreStateJSON {
  170. bytes := db.Get(blockStoreKey)
  171. if bytes == nil {
  172. return BlockStoreStateJSON{
  173. Height: 0,
  174. }
  175. }
  176. bsj := BlockStoreStateJSON{}
  177. err := json.Unmarshal(bytes, &bsj)
  178. if err != nil {
  179. panic(Fmt("Could not unmarshal bytes: %X", bytes))
  180. }
  181. return bsj
  182. }