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