github.com/avahowell/sia@v0.5.1-beta.0.20160524050156-83dcc3d37c94/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/NebulousLabs/bolt"
    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  // dbCurrentBlockID is a convenience function allowing currentBlockID to be
    27  // called without a bolt.Tx.
    28  func (cs *ConsensusSet) dbCurrentBlockID() (id types.BlockID) {
    29  	dbErr := cs.db.View(func(tx *bolt.Tx) error {
    30  		id = currentBlockID(tx)
    31  		return nil
    32  	})
    33  	if dbErr != nil {
    34  		panic(dbErr)
    35  	}
    36  	return id
    37  }
    38  
    39  // dbCurrentProcessedBlock is a convenience function allowing
    40  // currentProcessedBlock to be called without a bolt.Tx.
    41  func (cs *ConsensusSet) dbCurrentProcessedBlock() (pb *processedBlock) {
    42  	dbErr := cs.db.View(func(tx *bolt.Tx) error {
    43  		pb = currentProcessedBlock(tx)
    44  		return nil
    45  	})
    46  	if dbErr != nil {
    47  		panic(dbErr)
    48  	}
    49  	return pb
    50  }
    51  
    52  // dbGetPath is a convenience function allowing getPath to be called without a
    53  // bolt.Tx.
    54  func (cs *ConsensusSet) dbGetPath(bh types.BlockHeight) (id types.BlockID, err error) {
    55  	dbErr := cs.db.View(func(tx *bolt.Tx) error {
    56  		id, err = getPath(tx, bh)
    57  		return nil
    58  	})
    59  	if dbErr != nil {
    60  		panic(dbErr)
    61  	}
    62  	return id, err
    63  }
    64  
    65  // dbGetBlockMap is a convenience function allowing getBlockMap to be called
    66  // without a bolt.Tx.
    67  func (cs *ConsensusSet) dbGetBlockMap(id types.BlockID) (pb *processedBlock, err error) {
    68  	dbErr := cs.db.View(func(tx *bolt.Tx) error {
    69  		pb, err = getBlockMap(tx, id)
    70  		return nil
    71  	})
    72  	if dbErr != nil {
    73  		panic(dbErr)
    74  	}
    75  	return pb, err
    76  }
    77  
    78  // dbGetSiacoinOutput is a convenience function allowing getSiacoinOutput to be
    79  // called without a bolt.Tx.
    80  func (cs *ConsensusSet) dbGetSiacoinOutput(id types.SiacoinOutputID) (sco types.SiacoinOutput, err error) {
    81  	dbErr := cs.db.View(func(tx *bolt.Tx) error {
    82  		sco, err = getSiacoinOutput(tx, id)
    83  		return nil
    84  	})
    85  	if dbErr != nil {
    86  		panic(dbErr)
    87  	}
    88  	return sco, err
    89  }
    90  
    91  // dbGetFileContract is a convenience function allowing getFileContract to be
    92  // called without a bolt.Tx.
    93  func (cs *ConsensusSet) dbGetFileContract(id types.FileContractID) (fc types.FileContract, err error) {
    94  	dbErr := cs.db.View(func(tx *bolt.Tx) error {
    95  		fc, err = getFileContract(tx, id)
    96  		return nil
    97  	})
    98  	if dbErr != nil {
    99  		panic(dbErr)
   100  	}
   101  	return fc, err
   102  }
   103  
   104  // dbGetSiafundOutput is a convenience function allowing getSiafundOutput to be
   105  // called without a bolt.Tx.
   106  func (cs *ConsensusSet) dbGetSiafundOutput(id types.SiafundOutputID) (sfo types.SiafundOutput, err error) {
   107  	dbErr := cs.db.View(func(tx *bolt.Tx) error {
   108  		sfo, err = getSiafundOutput(tx, id)
   109  		return nil
   110  	})
   111  	if dbErr != nil {
   112  		panic(dbErr)
   113  	}
   114  	return sfo, err
   115  }
   116  
   117  // dbAddSiafundOutput is a convenience function allowing addSiafundOutput to be
   118  // called without a bolt.Tx.
   119  func (cs *ConsensusSet) dbAddSiafundOutput(id types.SiafundOutputID, sfo types.SiafundOutput) {
   120  	dbErr := cs.db.Update(func(tx *bolt.Tx) error {
   121  		addSiafundOutput(tx, id, sfo)
   122  		return nil
   123  	})
   124  	if dbErr != nil {
   125  		panic(dbErr)
   126  	}
   127  }
   128  
   129  // dbGetSiafundPool is a convenience function allowing getSiafundPool to be
   130  // called without a bolt.Tx.
   131  func (cs *ConsensusSet) dbGetSiafundPool() (siafundPool types.Currency) {
   132  	dbErr := cs.db.View(func(tx *bolt.Tx) error {
   133  		siafundPool = getSiafundPool(tx)
   134  		return nil
   135  	})
   136  	if dbErr != nil {
   137  		panic(dbErr)
   138  	}
   139  	return siafundPool
   140  }
   141  
   142  // dbGetDSCO is a convenience function allowing a delayed siacoin output to be
   143  // fetched without a bolt.Tx. An error is returned if the delayed output is not
   144  // found at the maturity height indicated by the input.
   145  func (cs *ConsensusSet) dbGetDSCO(height types.BlockHeight, id types.SiacoinOutputID) (dsco types.SiacoinOutput, err error) {
   146  	dbErr := cs.db.View(func(tx *bolt.Tx) error {
   147  		dscoBucketID := append(prefixDSCO, encoding.Marshal(height)...)
   148  		dscoBucket := tx.Bucket(dscoBucketID)
   149  		if dscoBucket == nil {
   150  			err = errNilItem
   151  			return nil
   152  		}
   153  		dscoBytes := dscoBucket.Get(id[:])
   154  		if dscoBytes == nil {
   155  			err = errNilItem
   156  			return nil
   157  		}
   158  		err = encoding.Unmarshal(dscoBytes, &dsco)
   159  		if err != nil {
   160  			panic(err)
   161  		}
   162  		return nil
   163  	})
   164  	if dbErr != nil {
   165  		panic(dbErr)
   166  	}
   167  	return dsco, err
   168  }