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.

268 lines
7.9 KiB

10 years ago
8 years ago
  1. package blockchain
  2. import (
  3. "bytes"
  4. "encoding/json"
  5. "fmt"
  6. "io"
  7. "sync"
  8. wire "github.com/tendermint/go-wire"
  9. cmn "github.com/tendermint/tmlibs/common"
  10. dbm "github.com/tendermint/tmlibs/db"
  11. "github.com/tendermint/tendermint/types"
  12. )
  13. /*
  14. BlockStore is a simple low level store for blocks.
  15. There are three types of information stored:
  16. - BlockMeta: Meta information about each block
  17. - Block part: Parts of each block, aggregated w/ PartSet
  18. - Commit: The commit part of each block, for gossiping precommit votes
  19. Currently the precommit signatures are duplicated in the Block parts as
  20. well as the Commit. In the future this may change, perhaps by moving
  21. the Commit data outside the Block. (TODO)
  22. // NOTE: BlockStore methods will panic if they encounter errors
  23. // deserializing loaded data, indicating probable corruption on disk.
  24. */
  25. type BlockStore struct {
  26. db dbm.DB
  27. mtx sync.RWMutex
  28. height int64
  29. }
  30. // NewBlockStore returns a new BlockStore with the given DB,
  31. // initialized to the last height that was committed to the DB.
  32. func NewBlockStore(db dbm.DB) *BlockStore {
  33. bsjson := LoadBlockStoreStateJSON(db)
  34. return &BlockStore{
  35. height: bsjson.Height,
  36. db: db,
  37. }
  38. }
  39. // Height returns the last known contiguous block height.
  40. func (bs *BlockStore) Height() int64 {
  41. bs.mtx.RLock()
  42. defer bs.mtx.RUnlock()
  43. return bs.height
  44. }
  45. // GetReader returns the value associated with the given key wrapped in an io.Reader.
  46. // If no value is found, it returns nil.
  47. // It's mainly for use with wire.ReadBinary.
  48. func (bs *BlockStore) GetReader(key []byte) io.Reader {
  49. bytez := bs.db.Get(key)
  50. if bytez == nil {
  51. return nil
  52. }
  53. return bytes.NewReader(bytez)
  54. }
  55. // LoadBlock returns the block with the given height.
  56. // If no block is found for that height, it returns nil.
  57. func (bs *BlockStore) LoadBlock(height int64) *types.Block {
  58. var n int
  59. var err error
  60. r := bs.GetReader(calcBlockMetaKey(height))
  61. if r == nil {
  62. return nil
  63. }
  64. blockMeta := wire.ReadBinary(&types.BlockMeta{}, r, 0, &n, &err).(*types.BlockMeta)
  65. if err != nil {
  66. panic(fmt.Sprintf("Error reading block meta: %v", err))
  67. }
  68. bytez := []byte{}
  69. for i := 0; i < blockMeta.BlockID.PartsHeader.Total; i++ {
  70. part := bs.LoadBlockPart(height, i)
  71. bytez = append(bytez, part.Bytes...)
  72. }
  73. block := wire.ReadBinary(&types.Block{}, bytes.NewReader(bytez), 0, &n, &err).(*types.Block)
  74. if err != nil {
  75. panic(fmt.Sprintf("Error reading block: %v", err))
  76. }
  77. return block
  78. }
  79. // LoadBlockPart returns the Part at the given index
  80. // from the block at the given height.
  81. // If no part is found for the given height and index, it returns nil.
  82. func (bs *BlockStore) LoadBlockPart(height int64, index int) *types.Part {
  83. var n int
  84. var err error
  85. r := bs.GetReader(calcBlockPartKey(height, index))
  86. if r == nil {
  87. return nil
  88. }
  89. part := wire.ReadBinary(&types.Part{}, r, 0, &n, &err).(*types.Part)
  90. if err != nil {
  91. panic(fmt.Sprintf("Error reading block part: %v", err))
  92. }
  93. return part
  94. }
  95. // LoadBlockMeta returns the BlockMeta for the given height.
  96. // If no block is found for the given height, it returns nil.
  97. func (bs *BlockStore) LoadBlockMeta(height int64) *types.BlockMeta {
  98. var n int
  99. var err error
  100. r := bs.GetReader(calcBlockMetaKey(height))
  101. if r == nil {
  102. return nil
  103. }
  104. blockMeta := wire.ReadBinary(&types.BlockMeta{}, r, 0, &n, &err).(*types.BlockMeta)
  105. if err != nil {
  106. panic(fmt.Sprintf("Error reading block meta: %v", err))
  107. }
  108. return blockMeta
  109. }
  110. // LoadBlockCommit returns the Commit for the given height.
  111. // This commit consists of the +2/3 and other Precommit-votes for block at `height`,
  112. // and it comes from the block.LastCommit for `height+1`.
  113. // If no commit is found for the given height, it returns nil.
  114. func (bs *BlockStore) LoadBlockCommit(height int64) *types.Commit {
  115. var n int
  116. var err error
  117. r := bs.GetReader(calcBlockCommitKey(height))
  118. if r == nil {
  119. return nil
  120. }
  121. commit := wire.ReadBinary(&types.Commit{}, r, 0, &n, &err).(*types.Commit)
  122. if err != nil {
  123. panic(fmt.Sprintf("Error reading commit: %v", err))
  124. }
  125. return commit
  126. }
  127. // LoadSeenCommit returns the locally seen Commit for the given height.
  128. // This is useful when we've seen a commit, but there has not yet been
  129. // a new block at `height + 1` that includes this commit in its block.LastCommit.
  130. func (bs *BlockStore) LoadSeenCommit(height int64) *types.Commit {
  131. var n int
  132. var err error
  133. r := bs.GetReader(calcSeenCommitKey(height))
  134. if r == nil {
  135. return nil
  136. }
  137. commit := wire.ReadBinary(&types.Commit{}, r, 0, &n, &err).(*types.Commit)
  138. if err != nil {
  139. panic(fmt.Sprintf("Error reading commit: %v", err))
  140. }
  141. return commit
  142. }
  143. // SaveBlock persists the given block, blockParts, and seenCommit to the underlying db.
  144. // blockParts: Must be parts of the block
  145. // seenCommit: The +2/3 precommits that were seen which committed at height.
  146. // If all the nodes restart after committing a block,
  147. // we need this to reload the precommits to catch-up nodes to the
  148. // most recent height. Otherwise they'd stall at H-1.
  149. func (bs *BlockStore) SaveBlock(block *types.Block, blockParts *types.PartSet, seenCommit *types.Commit) {
  150. if block == nil {
  151. cmn.PanicSanity("BlockStore can only save a non-nil block")
  152. }
  153. height := block.Height
  154. if g, w := height, bs.Height()+1; g != w {
  155. cmn.PanicSanity(cmn.Fmt("BlockStore can only save contiguous blocks. Wanted %v, got %v", w, g))
  156. }
  157. if !blockParts.IsComplete() {
  158. cmn.PanicSanity(cmn.Fmt("BlockStore can only save complete block part sets"))
  159. }
  160. // Save block meta
  161. blockMeta := types.NewBlockMeta(block, blockParts)
  162. metaBytes := wire.BinaryBytes(blockMeta)
  163. bs.db.Set(calcBlockMetaKey(height), metaBytes)
  164. // Save block parts
  165. for i := 0; i < blockParts.Total(); i++ {
  166. bs.saveBlockPart(height, i, blockParts.GetPart(i))
  167. }
  168. // Save block commit (duplicate and separate from the Block)
  169. blockCommitBytes := wire.BinaryBytes(block.LastCommit)
  170. bs.db.Set(calcBlockCommitKey(height-1), blockCommitBytes)
  171. // Save seen commit (seen +2/3 precommits for block)
  172. // NOTE: we can delete this at a later height
  173. seenCommitBytes := wire.BinaryBytes(seenCommit)
  174. bs.db.Set(calcSeenCommitKey(height), seenCommitBytes)
  175. // Save new BlockStoreStateJSON descriptor
  176. BlockStoreStateJSON{Height: height}.Save(bs.db)
  177. // Done!
  178. bs.mtx.Lock()
  179. bs.height = height
  180. bs.mtx.Unlock()
  181. // Flush
  182. bs.db.SetSync(nil, nil)
  183. }
  184. func (bs *BlockStore) saveBlockPart(height int64, index int, part *types.Part) {
  185. if height != bs.Height()+1 {
  186. cmn.PanicSanity(cmn.Fmt("BlockStore can only save contiguous blocks. Wanted %v, got %v", bs.Height()+1, height))
  187. }
  188. partBytes := wire.BinaryBytes(part)
  189. bs.db.Set(calcBlockPartKey(height, index), partBytes)
  190. }
  191. //-----------------------------------------------------------------------------
  192. func calcBlockMetaKey(height int64) []byte {
  193. return []byte(fmt.Sprintf("H:%v", height))
  194. }
  195. func calcBlockPartKey(height int64, partIndex int) []byte {
  196. return []byte(fmt.Sprintf("P:%v:%v", height, partIndex))
  197. }
  198. func calcBlockCommitKey(height int64) []byte {
  199. return []byte(fmt.Sprintf("C:%v", height))
  200. }
  201. func calcSeenCommitKey(height int64) []byte {
  202. return []byte(fmt.Sprintf("SC:%v", height))
  203. }
  204. //-----------------------------------------------------------------------------
  205. var blockStoreKey = []byte("blockStore")
  206. type BlockStoreStateJSON struct {
  207. Height int64
  208. }
  209. // Save persists the blockStore state to the database as JSON.
  210. func (bsj BlockStoreStateJSON) Save(db dbm.DB) {
  211. bytes, err := json.Marshal(bsj)
  212. if err != nil {
  213. cmn.PanicSanity(cmn.Fmt("Could not marshal state bytes: %v", err))
  214. }
  215. db.SetSync(blockStoreKey, bytes)
  216. }
  217. // LoadBlockStoreStateJSON returns the BlockStoreStateJSON as loaded from disk.
  218. // If no BlockStoreStateJSON was previously persisted, it returns the zero value.
  219. func LoadBlockStoreStateJSON(db dbm.DB) BlockStoreStateJSON {
  220. bytes := db.Get(blockStoreKey)
  221. if len(bytes) == 0 {
  222. return BlockStoreStateJSON{
  223. Height: 0,
  224. }
  225. }
  226. bsj := BlockStoreStateJSON{}
  227. err := json.Unmarshal(bytes, &bsj)
  228. if err != nil {
  229. panic(fmt.Sprintf("Could not unmarshal bytes: %X", bytes))
  230. }
  231. return bsj
  232. }