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.

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