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.

235 lines
6.2 KiB

10 years ago
8 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. // NOTE: we can delete this at a later height
  148. seenCommitBytes := wire.BinaryBytes(seenCommit)
  149. bs.db.Set(calcSeenCommitKey(height), seenCommitBytes)
  150. // Save new BlockStoreStateJSON descriptor
  151. BlockStoreStateJSON{Height: height}.Save(bs.db)
  152. // Done!
  153. bs.height = height
  154. // Flush
  155. bs.db.SetSync(nil, nil)
  156. }
  157. func (bs *BlockStore) saveBlockPart(height int, index int, part *types.Part) {
  158. if height != bs.height+1 {
  159. PanicSanity(Fmt("BlockStore can only save contiguous blocks. Wanted %v, got %v", bs.height+1, height))
  160. }
  161. partBytes := wire.BinaryBytes(part)
  162. bs.db.Set(calcBlockPartKey(height, index), partBytes)
  163. }
  164. //-----------------------------------------------------------------------------
  165. func calcBlockMetaKey(height int) []byte {
  166. return []byte(fmt.Sprintf("H:%v", height))
  167. }
  168. func calcBlockPartKey(height int, partIndex int) []byte {
  169. return []byte(fmt.Sprintf("P:%v:%v", height, partIndex))
  170. }
  171. func calcBlockCommitKey(height int) []byte {
  172. return []byte(fmt.Sprintf("C:%v", height))
  173. }
  174. func calcSeenCommitKey(height int) []byte {
  175. return []byte(fmt.Sprintf("SC:%v", height))
  176. }
  177. //-----------------------------------------------------------------------------
  178. var blockStoreKey = []byte("blockStore")
  179. type BlockStoreStateJSON struct {
  180. Height int
  181. }
  182. func (bsj BlockStoreStateJSON) Save(db dbm.DB) {
  183. bytes, err := json.Marshal(bsj)
  184. if err != nil {
  185. PanicSanity(Fmt("Could not marshal state bytes: %v", err))
  186. }
  187. db.SetSync(blockStoreKey, bytes)
  188. }
  189. func LoadBlockStoreStateJSON(db dbm.DB) BlockStoreStateJSON {
  190. bytes := db.Get(blockStoreKey)
  191. if bytes == nil {
  192. return BlockStoreStateJSON{
  193. Height: 0,
  194. }
  195. }
  196. bsj := BlockStoreStateJSON{}
  197. err := json.Unmarshal(bytes, &bsj)
  198. if err != nil {
  199. PanicCrisis(Fmt("Could not unmarshal bytes: %X", bytes))
  200. }
  201. return bsj
  202. }