github.com/johnathanhowell/sia@v0.5.1-beta.0.20160524050156-83dcc3d37c94/modules/explorer/info.go (about)

     1  package explorer
     2  
     3  import (
     4  	"github.com/NebulousLabs/Sia/modules"
     5  	"github.com/NebulousLabs/Sia/types"
     6  )
     7  
     8  // Block takes a block ID and finds the corresponding block, provided that the
     9  // block is in the consensus set.
    10  func (e *Explorer) Block(id types.BlockID) (types.Block, types.BlockHeight, bool) {
    11  	var height types.BlockHeight
    12  	err := e.db.View(dbGetAndDecode(bucketBlockIDs, id, &height))
    13  	if err != nil {
    14  		return types.Block{}, 0, false
    15  	}
    16  	block, exists := e.cs.BlockAtHeight(height)
    17  	if !exists {
    18  		return types.Block{}, 0, false
    19  	}
    20  	return block, height, true
    21  }
    22  
    23  // BlockFacts returns a set of statistics about the blockchain as they appeared
    24  // at a given block height, and a bool indicating whether facts exist for the
    25  // given height.
    26  func (e *Explorer) BlockFacts(height types.BlockHeight) (modules.BlockFacts, bool) {
    27  	block, exists := e.cs.BlockAtHeight(height)
    28  	if !exists {
    29  		return modules.BlockFacts{}, false
    30  	}
    31  
    32  	var bf blockFacts
    33  	err := e.db.View(dbGetAndDecode(bucketBlockFacts, block.ID(), &bf))
    34  	if err != nil {
    35  		return modules.BlockFacts{}, false
    36  	}
    37  
    38  	return bf.BlockFacts, true
    39  }
    40  
    41  // Transaction takes a transaction ID and finds the block containing the
    42  // transaction. Because of the miner payouts, the transaction ID might be a
    43  // block ID. To find the transaction, iterate through the block.
    44  func (e *Explorer) Transaction(id types.TransactionID) (types.Block, types.BlockHeight, bool) {
    45  	var height types.BlockHeight
    46  	err := e.db.View(dbGetAndDecode(bucketTransactionIDs, id, &height))
    47  	if err != nil {
    48  		return types.Block{}, 0, false
    49  	}
    50  	block, exists := e.cs.BlockAtHeight(height)
    51  	if !exists {
    52  		return types.Block{}, 0, false
    53  	}
    54  	return block, height, true
    55  }
    56  
    57  // UnlockHash returns the IDs of all the transactions that contain the unlock
    58  // hash. An empty set indicates that the unlock hash does not appear in the
    59  // blockchain.
    60  func (e *Explorer) UnlockHash(uh types.UnlockHash) []types.TransactionID {
    61  	var ids []types.TransactionID
    62  	err := e.db.View(dbGetTransactionIDSet(bucketUnlockHashes, uh, &ids))
    63  	if err != nil {
    64  		ids = nil
    65  	}
    66  	return ids
    67  }
    68  
    69  // SiacoinOutput returns the siacoin output associated with the specified ID.
    70  func (e *Explorer) SiacoinOutput(id types.SiacoinOutputID) (types.SiacoinOutput, bool) {
    71  	var sco types.SiacoinOutput
    72  	err := e.db.View(dbGetAndDecode(bucketSiacoinOutputs, id, &sco))
    73  	if err != nil {
    74  		return types.SiacoinOutput{}, false
    75  	}
    76  	return sco, true
    77  }
    78  
    79  // SiacoinOutputID returns all of the transactions that contain the specified
    80  // siacoin output ID. An empty set indicates that the siacoin output ID does
    81  // not appear in the blockchain.
    82  func (e *Explorer) SiacoinOutputID(id types.SiacoinOutputID) []types.TransactionID {
    83  	var ids []types.TransactionID
    84  	err := e.db.View(dbGetTransactionIDSet(bucketSiacoinOutputIDs, id, &ids))
    85  	if err != nil {
    86  		ids = nil
    87  	}
    88  	return ids
    89  }
    90  
    91  // FileContractHistory returns the history associated with the specified file
    92  // contract ID, which includes the file contract itself and all of the
    93  // revisions that have been submitted to the blockchain. The first bool
    94  // indicates whether the file contract exists, and the second bool indicates
    95  // whether a storage proof was successfully submitted for the file contract.
    96  func (e *Explorer) FileContractHistory(id types.FileContractID) (fc types.FileContract, fcrs []types.FileContractRevision, fcE bool, spE bool) {
    97  	var history fileContractHistory
    98  	err := e.db.View(dbGetAndDecode(bucketFileContractHistories, id, &history))
    99  	fc = history.Contract
   100  	fcrs = history.Revisions
   101  	fcE = err == nil
   102  	spE = history.StorageProof.ParentID == id
   103  	return
   104  }
   105  
   106  // FileContractIDs returns all of the transactions that contain the specified
   107  // file contract ID. An empty set indicates that the file contract ID does not
   108  // appear in the blockchain.
   109  func (e *Explorer) FileContractID(id types.FileContractID) []types.TransactionID {
   110  	var ids []types.TransactionID
   111  	err := e.db.View(dbGetTransactionIDSet(bucketFileContractIDs, id, &ids))
   112  	if err != nil {
   113  		ids = nil
   114  	}
   115  	return ids
   116  }
   117  
   118  // SiafundOutput returns the siafund output associated with the specified ID.
   119  func (e *Explorer) SiafundOutput(id types.SiafundOutputID) (types.SiafundOutput, bool) {
   120  	var sco types.SiafundOutput
   121  	err := e.db.View(dbGetAndDecode(bucketSiafundOutputs, id, &sco))
   122  	if err != nil {
   123  		return types.SiafundOutput{}, false
   124  	}
   125  	return sco, true
   126  }
   127  
   128  // SiafundOutputID returns all of the transactions that contain the specified
   129  // siafund output ID. An empty set indicates that the siafund output ID does
   130  // not appear in the blockchain.
   131  func (e *Explorer) SiafundOutputID(id types.SiafundOutputID) []types.TransactionID {
   132  	var ids []types.TransactionID
   133  	err := e.db.View(dbGetTransactionIDSet(bucketSiafundOutputIDs, id, &ids))
   134  	if err != nil {
   135  		ids = nil
   136  	}
   137  	return ids
   138  }