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