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.

197 lines
5.1 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. meta := ReadBinary(&BlockMeta{}, bs.GetReader(calcBlockMetaKey(height)), &n, &err).(*BlockMeta)
  46. if err != nil {
  47. panic(Fmt("Error reading block meta: %v", err))
  48. }
  49. bytez := []byte{}
  50. for i := uint(0); i < meta.Parts.Total; i++ {
  51. part := bs.LoadBlockPart(height, i)
  52. bytez = append(bytez, part.Bytes...)
  53. }
  54. block := ReadBinary(&Block{}, bytes.NewReader(bytez), &n, &err).(*Block)
  55. if err != nil {
  56. panic(Fmt("Error reading block: %v", err))
  57. }
  58. return block
  59. }
  60. func (bs *BlockStore) LoadBlockPart(height uint, index uint) *Part {
  61. var n int64
  62. var err error
  63. part := ReadBinary(&Part{}, bs.GetReader(calcBlockPartKey(height, index)), &n, &err).(*Part)
  64. if err != nil {
  65. panic(Fmt("Error reading block part: %v", err))
  66. }
  67. return part
  68. }
  69. func (bs *BlockStore) LoadBlockMeta(height uint) *BlockMeta {
  70. var n int64
  71. var err error
  72. meta := ReadBinary(&BlockMeta{}, bs.GetReader(calcBlockMetaKey(height)), &n, &err).(*BlockMeta)
  73. if err != nil {
  74. panic(Fmt("Error reading block meta: %v", err))
  75. }
  76. return meta
  77. }
  78. func (bs *BlockStore) LoadBlockValidation(height uint) *Validation {
  79. var n int64
  80. var err error
  81. validation := ReadBinary(&Validation{}, bs.GetReader(calcBlockValidationKey(height)), &n, &err).(*Validation)
  82. if err != nil {
  83. panic(Fmt("Error reading validation: %v", err))
  84. }
  85. return validation
  86. }
  87. func (bs *BlockStore) SaveBlock(block *Block, blockParts *PartSet) {
  88. height := block.Height
  89. if height != bs.height+1 {
  90. panic(Fmt("BlockStore can only save contiguous blocks. Wanted %v, got %v", bs.height+1, height))
  91. }
  92. if !blockParts.IsComplete() {
  93. panic(Fmt("BlockStore can only save complete block part sets"))
  94. }
  95. // Save block meta
  96. meta := makeBlockMeta(block, blockParts)
  97. metaBytes := BinaryBytes(meta)
  98. bs.db.Set(calcBlockMetaKey(height), metaBytes)
  99. // Save block parts
  100. for i := uint(0); i < blockParts.Total(); i++ {
  101. bs.saveBlockPart(height, i, blockParts.GetPart(i))
  102. }
  103. // Save block validation (duplicate and separate)
  104. validationBytes := BinaryBytes(block.Validation)
  105. bs.db.Set(calcBlockValidationKey(height), validationBytes)
  106. // Save new BlockStoreStateJSON descriptor
  107. BlockStoreStateJSON{Height: height}.Save(bs.db)
  108. // Done!
  109. bs.height = height
  110. }
  111. func (bs *BlockStore) saveBlockPart(height uint, index uint, part *Part) {
  112. if height != bs.height+1 {
  113. panic(Fmt("BlockStore can only save contiguous blocks. Wanted %v, got %v", bs.height+1, height))
  114. }
  115. partBytes := BinaryBytes(part)
  116. bs.db.Set(calcBlockPartKey(height, index), partBytes)
  117. }
  118. //-----------------------------------------------------------------------------
  119. type BlockMeta struct {
  120. Hash []byte // The block hash
  121. Header *Header // The block's Header
  122. Parts PartSetHeader // The PartSetHeader, for transfer
  123. }
  124. func makeBlockMeta(block *Block, blockParts *PartSet) *BlockMeta {
  125. return &BlockMeta{
  126. Hash: block.Hash(),
  127. Header: block.Header,
  128. Parts: blockParts.Header(),
  129. }
  130. }
  131. //-----------------------------------------------------------------------------
  132. func calcBlockMetaKey(height uint) []byte {
  133. return []byte(fmt.Sprintf("H:%v", height))
  134. }
  135. func calcBlockPartKey(height uint, partIndex uint) []byte {
  136. return []byte(fmt.Sprintf("P:%v:%v", height, partIndex))
  137. }
  138. func calcBlockValidationKey(height uint) []byte {
  139. return []byte(fmt.Sprintf("V:%v", height))
  140. }
  141. //-----------------------------------------------------------------------------
  142. var blockStoreKey = []byte("blockStore")
  143. type BlockStoreStateJSON struct {
  144. Height uint
  145. }
  146. func (bsj BlockStoreStateJSON) Save(db db_.DB) {
  147. bytes, err := json.Marshal(bsj)
  148. if err != nil {
  149. panic(Fmt("Could not marshal state bytes: %v", err))
  150. }
  151. db.Set(blockStoreKey, bytes)
  152. }
  153. func LoadBlockStoreStateJSON(db db_.DB) BlockStoreStateJSON {
  154. bytes := db.Get(blockStoreKey)
  155. if bytes == nil {
  156. return BlockStoreStateJSON{
  157. Height: 0,
  158. }
  159. }
  160. bsj := BlockStoreStateJSON{}
  161. err := json.Unmarshal(bytes, &bsj)
  162. if err != nil {
  163. panic(Fmt("Could not unmarshal bytes: %X", bytes))
  164. }
  165. return bsj
  166. }