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.

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