github.com/NebulousLabs/Sia@v1.3.7/modules/consensus/consensusdb_helpers_test.go (about) 1 package consensus 2 3 // database_test.go contains a bunch of legacy functions to preserve 4 // compatibility with the test suite. 5 6 import ( 7 "github.com/NebulousLabs/Sia/encoding" 8 "github.com/NebulousLabs/Sia/types" 9 10 "github.com/coreos/bbolt" 11 ) 12 13 // dbBlockHeight is a convenience function allowing blockHeight to be called 14 // without a bolt.Tx. 15 func (cs *ConsensusSet) dbBlockHeight() (bh types.BlockHeight) { 16 dbErr := cs.db.View(func(tx *bolt.Tx) error { 17 bh = blockHeight(tx) 18 return nil 19 }) 20 if dbErr != nil { 21 panic(dbErr) 22 } 23 return bh 24 } 25 26 // dbCurrentProcessedBlock is a convenience function allowing 27 // currentProcessedBlock to be called without a bolt.Tx. 28 func (cs *ConsensusSet) dbCurrentProcessedBlock() (pb *processedBlock) { 29 dbErr := cs.db.View(func(tx *bolt.Tx) error { 30 pb = currentProcessedBlock(tx) 31 return nil 32 }) 33 if dbErr != nil { 34 panic(dbErr) 35 } 36 return pb 37 } 38 39 // dbGetPath is a convenience function allowing getPath to be called without a 40 // bolt.Tx. 41 func (cs *ConsensusSet) dbGetPath(bh types.BlockHeight) (id types.BlockID, err error) { 42 dbErr := cs.db.View(func(tx *bolt.Tx) error { 43 id, err = getPath(tx, bh) 44 return nil 45 }) 46 if dbErr != nil { 47 panic(dbErr) 48 } 49 return id, err 50 } 51 52 // dbPushPath is a convenience function allowing pushPath to be called without a 53 // bolt.Tx. 54 func (cs *ConsensusSet) dbPushPath(bid types.BlockID) { 55 dbErr := cs.db.Update(func(tx *bolt.Tx) error { 56 pushPath(tx, bid) 57 return nil 58 }) 59 if dbErr != nil { 60 panic(dbErr) 61 } 62 } 63 64 // dbGetBlockMap is a convenience function allowing getBlockMap to be called 65 // without a bolt.Tx. 66 func (cs *ConsensusSet) dbGetBlockMap(id types.BlockID) (pb *processedBlock, err error) { 67 dbErr := cs.db.View(func(tx *bolt.Tx) error { 68 pb, err = getBlockMap(tx, id) 69 return nil 70 }) 71 if dbErr != nil { 72 panic(dbErr) 73 } 74 return pb, err 75 } 76 77 // dbGetSiacoinOutput is a convenience function allowing getSiacoinOutput to be 78 // called without a bolt.Tx. 79 func (cs *ConsensusSet) dbGetSiacoinOutput(id types.SiacoinOutputID) (sco types.SiacoinOutput, err error) { 80 dbErr := cs.db.View(func(tx *bolt.Tx) error { 81 sco, err = getSiacoinOutput(tx, id) 82 return nil 83 }) 84 if dbErr != nil { 85 panic(dbErr) 86 } 87 return sco, err 88 } 89 90 // getArbSiacoinOutput is a convenience function fetching a single random 91 // siacoin output from the database. 92 func (cs *ConsensusSet) getArbSiacoinOutput() (scoid types.SiacoinOutputID, sco types.SiacoinOutput, err error) { 93 dbErr := cs.db.View(func(tx *bolt.Tx) error { 94 cursor := tx.Bucket(SiacoinOutputs).Cursor() 95 scoidBytes, scoBytes := cursor.First() 96 copy(scoid[:], scoidBytes) 97 return encoding.Unmarshal(scoBytes, &sco) 98 }) 99 if dbErr != nil { 100 panic(dbErr) 101 } 102 if err != nil { 103 return types.SiacoinOutputID{}, types.SiacoinOutput{}, err 104 } 105 return scoid, sco, nil 106 } 107 108 // dbGetFileContract is a convenience function allowing getFileContract to be 109 // called without a bolt.Tx. 110 func (cs *ConsensusSet) dbGetFileContract(id types.FileContractID) (fc types.FileContract, err error) { 111 dbErr := cs.db.View(func(tx *bolt.Tx) error { 112 fc, err = getFileContract(tx, id) 113 return nil 114 }) 115 if dbErr != nil { 116 panic(dbErr) 117 } 118 return fc, err 119 } 120 121 // dbAddFileContract is a convenience function allowing addFileContract to be 122 // called without a bolt.Tx. 123 func (cs *ConsensusSet) dbAddFileContract(id types.FileContractID, fc types.FileContract) { 124 dbErr := cs.db.Update(func(tx *bolt.Tx) error { 125 addFileContract(tx, id, fc) 126 return nil 127 }) 128 if dbErr != nil { 129 panic(dbErr) 130 } 131 } 132 133 // dbRemoveFileContract is a convenience function allowing removeFileContract 134 // to be called without a bolt.Tx. 135 func (cs *ConsensusSet) dbRemoveFileContract(id types.FileContractID) { 136 dbErr := cs.db.Update(func(tx *bolt.Tx) error { 137 removeFileContract(tx, id) 138 return nil 139 }) 140 if dbErr != nil { 141 panic(dbErr) 142 } 143 } 144 145 // dbGetSiafundOutput is a convenience function allowing getSiafundOutput to be 146 // called without a bolt.Tx. 147 func (cs *ConsensusSet) dbGetSiafundOutput(id types.SiafundOutputID) (sfo types.SiafundOutput, err error) { 148 dbErr := cs.db.View(func(tx *bolt.Tx) error { 149 sfo, err = getSiafundOutput(tx, id) 150 return nil 151 }) 152 if dbErr != nil { 153 panic(dbErr) 154 } 155 return sfo, err 156 } 157 158 // dbAddSiafundOutput is a convenience function allowing addSiafundOutput to be 159 // called without a bolt.Tx. 160 func (cs *ConsensusSet) dbAddSiafundOutput(id types.SiafundOutputID, sfo types.SiafundOutput) { 161 dbErr := cs.db.Update(func(tx *bolt.Tx) error { 162 addSiafundOutput(tx, id, sfo) 163 return nil 164 }) 165 if dbErr != nil { 166 panic(dbErr) 167 } 168 } 169 170 // dbGetSiafundPool is a convenience function allowing getSiafundPool to be 171 // called without a bolt.Tx. 172 func (cs *ConsensusSet) dbGetSiafundPool() (siafundPool types.Currency) { 173 dbErr := cs.db.View(func(tx *bolt.Tx) error { 174 siafundPool = getSiafundPool(tx) 175 return nil 176 }) 177 if dbErr != nil { 178 panic(dbErr) 179 } 180 return siafundPool 181 } 182 183 // dbGetDSCO is a convenience function allowing a delayed siacoin output to be 184 // fetched without a bolt.Tx. An error is returned if the delayed output is not 185 // found at the maturity height indicated by the input. 186 func (cs *ConsensusSet) dbGetDSCO(height types.BlockHeight, id types.SiacoinOutputID) (dsco types.SiacoinOutput, err error) { 187 dbErr := cs.db.View(func(tx *bolt.Tx) error { 188 dscoBucketID := append(prefixDSCO, encoding.Marshal(height)...) 189 dscoBucket := tx.Bucket(dscoBucketID) 190 if dscoBucket == nil { 191 err = errNilItem 192 return nil 193 } 194 dscoBytes := dscoBucket.Get(id[:]) 195 if dscoBytes == nil { 196 err = errNilItem 197 return nil 198 } 199 err = encoding.Unmarshal(dscoBytes, &dsco) 200 if err != nil { 201 panic(err) 202 } 203 return nil 204 }) 205 if dbErr != nil { 206 panic(dbErr) 207 } 208 return dsco, err 209 } 210 211 // dbStorageProofSegment is a convenience function allowing 212 // 'storageProofSegment' to be called during testing without a tx. 213 func (cs *ConsensusSet) dbStorageProofSegment(fcid types.FileContractID) (index uint64, err error) { 214 dbErr := cs.db.View(func(tx *bolt.Tx) error { 215 index, err = storageProofSegment(tx, fcid) 216 return nil 217 }) 218 if dbErr != nil { 219 panic(dbErr) 220 } 221 return index, err 222 } 223 224 // dbValidStorageProofs is a convenience function allowing 'validStorageProofs' 225 // to be called during testing without a tx. 226 func (cs *ConsensusSet) dbValidStorageProofs(t types.Transaction) (err error) { 227 dbErr := cs.db.View(func(tx *bolt.Tx) error { 228 err = validStorageProofs(tx, t) 229 return nil 230 }) 231 if dbErr != nil { 232 panic(dbErr) 233 } 234 return err 235 } 236 237 // dbValidFileContractRevisions is a convenience function allowing 238 // 'validFileContractRevisions' to be called during testing without a tx. 239 func (cs *ConsensusSet) dbValidFileContractRevisions(t types.Transaction) (err error) { 240 dbErr := cs.db.View(func(tx *bolt.Tx) error { 241 err = validFileContractRevisions(tx, t) 242 return nil 243 }) 244 if dbErr != nil { 245 panic(dbErr) 246 } 247 return err 248 }