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.

231 lines
6.1 KiB

10 years ago
  1. package blockchain
  2. import (
  3. "bytes"
  4. "encoding/json"
  5. "fmt"
  6. "io"
  7. . "github.com/tendermint/go-common"
  8. dbm "github.com/tendermint/go-db"
  9. "github.com/tendermint/go-wire"
  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. - Commit: The commit part of each block, for gossiping precommit votes
  18. Currently the precommit signatures are duplicated in the Block parts as
  19. well as the Commit. In the future this may change, perhaps by moving
  20. the Commit data outside the Block.
  21. Panics indicate probable corruption in the data
  22. */
  23. type BlockStore struct {
  24. height int
  25. db dbm.DB
  26. }
  27. func NewBlockStore(db dbm.DB) *BlockStore {
  28. bsjson := LoadBlockStoreStateJSON(db)
  29. return &BlockStore{
  30. height: bsjson.Height,
  31. db: db,
  32. }
  33. }
  34. // Height() returns the last known contiguous block height.
  35. func (bs *BlockStore) Height() int {
  36. return bs.height
  37. }
  38. func (bs *BlockStore) GetReader(key []byte) io.Reader {
  39. bytez := bs.db.Get(key)
  40. if bytez == nil {
  41. return nil
  42. }
  43. return bytes.NewReader(bytez)
  44. }
  45. func (bs *BlockStore) LoadBlock(height int) *types.Block {
  46. var n int
  47. var err error
  48. r := bs.GetReader(calcBlockMetaKey(height))
  49. if r == nil {
  50. return nil
  51. }
  52. meta := wire.ReadBinary(&types.BlockMeta{}, r, 0, &n, &err).(*types.BlockMeta)
  53. if err != nil {
  54. PanicCrisis(Fmt("Error reading block meta: %v", err))
  55. }
  56. bytez := []byte{}
  57. for i := 0; i < meta.PartsHeader.Total; i++ {
  58. part := bs.LoadBlockPart(height, i)
  59. bytez = append(bytez, part.Bytes...)
  60. }
  61. block := wire.ReadBinary(&types.Block{}, bytes.NewReader(bytez), 0, &n, &err).(*types.Block)
  62. if err != nil {
  63. PanicCrisis(Fmt("Error reading block: %v", err))
  64. }
  65. return block
  66. }
  67. func (bs *BlockStore) LoadBlockPart(height int, index int) *types.Part {
  68. var n int
  69. var err error
  70. r := bs.GetReader(calcBlockPartKey(height, index))
  71. if r == nil {
  72. return nil
  73. }
  74. part := wire.ReadBinary(&types.Part{}, r, 0, &n, &err).(*types.Part)
  75. if err != nil {
  76. PanicCrisis(Fmt("Error reading block part: %v", err))
  77. }
  78. return part
  79. }
  80. func (bs *BlockStore) LoadBlockMeta(height int) *types.BlockMeta {
  81. var n int
  82. var err error
  83. r := bs.GetReader(calcBlockMetaKey(height))
  84. if r == nil {
  85. return nil
  86. }
  87. meta := wire.ReadBinary(&types.BlockMeta{}, r, 0, &n, &err).(*types.BlockMeta)
  88. if err != nil {
  89. PanicCrisis(Fmt("Error reading block meta: %v", err))
  90. }
  91. return meta
  92. }
  93. // The +2/3 and other Precommit-votes for block at `height`.
  94. // This Commit comes from block.LastCommit for `height+1`.
  95. func (bs *BlockStore) LoadBlockCommit(height int) *types.Commit {
  96. var n int
  97. var err error
  98. r := bs.GetReader(calcBlockCommitKey(height))
  99. if r == nil {
  100. return nil
  101. }
  102. commit := wire.ReadBinary(&types.Commit{}, r, 0, &n, &err).(*types.Commit)
  103. if err != nil {
  104. PanicCrisis(Fmt("Error reading commit: %v", err))
  105. }
  106. return commit
  107. }
  108. // NOTE: the Precommit-vote heights are for the block at `height`
  109. func (bs *BlockStore) LoadSeenCommit(height int) *types.Commit {
  110. var n int
  111. var err error
  112. r := bs.GetReader(calcSeenCommitKey(height))
  113. if r == nil {
  114. return nil
  115. }
  116. commit := wire.ReadBinary(&types.Commit{}, r, 0, &n, &err).(*types.Commit)
  117. if err != nil {
  118. PanicCrisis(Fmt("Error reading commit: %v", err))
  119. }
  120. return commit
  121. }
  122. // blockParts: Must be parts of the block
  123. // seenCommit: The +2/3 precommits that were seen which committed at height.
  124. // If all the nodes restart after committing a block,
  125. // we need this to reload the precommits to catch-up nodes to the
  126. // most recent height. Otherwise they'd stall at H-1.
  127. func (bs *BlockStore) SaveBlock(block *types.Block, blockParts *types.PartSet, seenCommit *types.Commit) {
  128. height := block.Height
  129. if height != bs.height+1 {
  130. PanicSanity(Fmt("BlockStore can only save contiguous blocks. Wanted %v, got %v", bs.height+1, height))
  131. }
  132. if !blockParts.IsComplete() {
  133. PanicSanity(Fmt("BlockStore can only save complete block part sets"))
  134. }
  135. // Save block meta
  136. meta := types.NewBlockMeta(block, blockParts)
  137. metaBytes := wire.BinaryBytes(meta)
  138. bs.db.Set(calcBlockMetaKey(height), metaBytes)
  139. // Save block parts
  140. for i := 0; i < blockParts.Total(); i++ {
  141. bs.saveBlockPart(height, i, blockParts.GetPart(i))
  142. }
  143. // Save block commit (duplicate and separate from the Block)
  144. blockCommitBytes := wire.BinaryBytes(block.LastCommit)
  145. bs.db.Set(calcBlockCommitKey(height-1), blockCommitBytes)
  146. // Save seen commit (seen +2/3 precommits for block)
  147. seenCommitBytes := wire.BinaryBytes(seenCommit)
  148. bs.db.Set(calcSeenCommitKey(height), seenCommitBytes)
  149. // Save new BlockStoreStateJSON descriptor
  150. BlockStoreStateJSON{Height: height}.Save(bs.db)
  151. // Done!
  152. bs.height = height
  153. }
  154. func (bs *BlockStore) saveBlockPart(height int, index int, part *types.Part) {
  155. if height != bs.height+1 {
  156. PanicSanity(Fmt("BlockStore can only save contiguous blocks. Wanted %v, got %v", bs.height+1, height))
  157. }
  158. partBytes := wire.BinaryBytes(part)
  159. bs.db.Set(calcBlockPartKey(height, index), partBytes)
  160. }
  161. //-----------------------------------------------------------------------------
  162. func calcBlockMetaKey(height int) []byte {
  163. return []byte(fmt.Sprintf("H:%v", height))
  164. }
  165. func calcBlockPartKey(height int, partIndex int) []byte {
  166. return []byte(fmt.Sprintf("P:%v:%v", height, partIndex))
  167. }
  168. func calcBlockCommitKey(height int) []byte {
  169. return []byte(fmt.Sprintf("C:%v", height))
  170. }
  171. func calcSeenCommitKey(height int) []byte {
  172. return []byte(fmt.Sprintf("SC:%v", height))
  173. }
  174. //-----------------------------------------------------------------------------
  175. var blockStoreKey = []byte("blockStore")
  176. type BlockStoreStateJSON struct {
  177. Height int
  178. }
  179. func (bsj BlockStoreStateJSON) Save(db dbm.DB) {
  180. bytes, err := json.Marshal(bsj)
  181. if err != nil {
  182. PanicSanity(Fmt("Could not marshal state bytes: %v", err))
  183. }
  184. db.Set(blockStoreKey, bytes)
  185. }
  186. func LoadBlockStoreStateJSON(db dbm.DB) BlockStoreStateJSON {
  187. bytes := db.Get(blockStoreKey)
  188. if bytes == nil {
  189. return BlockStoreStateJSON{
  190. Height: 0,
  191. }
  192. }
  193. bsj := BlockStoreStateJSON{}
  194. err := json.Unmarshal(bytes, &bsj)
  195. if err != nil {
  196. PanicCrisis(Fmt("Could not unmarshal bytes: %X", bytes))
  197. }
  198. return bsj
  199. }