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

     1  package wallet
     2  
     3  import (
     4  	"errors"
     5  
     6  	"github.com/NebulousLabs/Sia/modules"
     7  	"github.com/NebulousLabs/Sia/types"
     8  )
     9  
    10  var (
    11  	errOutOfBounds      = errors.New("requesting transactions at unknown confirmation heights")
    12  	errNoHistoryForAddr = errors.New("no history found for provided address")
    13  )
    14  
    15  // AddressTransactions returns all of the wallet transactions associated with a
    16  // single unlock hash.
    17  func (w *Wallet) AddressTransactions(uh types.UnlockHash) (pts []modules.ProcessedTransaction) {
    18  	w.mu.Lock()
    19  	defer w.mu.Unlock()
    20  
    21  	for _, pt := range w.processedTransactions {
    22  		relevant := false
    23  		for _, input := range pt.Inputs {
    24  			if input.RelatedAddress == uh {
    25  				relevant = true
    26  				break
    27  			}
    28  		}
    29  		for _, output := range pt.Outputs {
    30  			if output.RelatedAddress == uh {
    31  				relevant = true
    32  				break
    33  			}
    34  		}
    35  		if relevant {
    36  			pts = append(pts, pt)
    37  		}
    38  	}
    39  	return pts
    40  }
    41  
    42  // AddressUnconfirmedHistory returns all of the unconfirmed wallet transactions
    43  // related to a specific address.
    44  func (w *Wallet) AddressUnconfirmedTransactions(uh types.UnlockHash) (pts []modules.ProcessedTransaction) {
    45  	w.mu.Lock()
    46  	defer w.mu.Unlock()
    47  
    48  	// Scan the full list of unconfirmed transactions to see if there are any
    49  	// related transactions.
    50  	for _, pt := range w.unconfirmedProcessedTransactions {
    51  		relevant := false
    52  		for _, input := range pt.Inputs {
    53  			if input.RelatedAddress == uh {
    54  				relevant = true
    55  				break
    56  			}
    57  		}
    58  		for _, output := range pt.Outputs {
    59  			if output.RelatedAddress == uh {
    60  				relevant = true
    61  				break
    62  			}
    63  		}
    64  		if relevant {
    65  			pts = append(pts, pt)
    66  		}
    67  	}
    68  	return pts
    69  }
    70  
    71  // Transaction returns the transaction with the given id. 'False' is returned
    72  // if the transaction does not exist.
    73  func (w *Wallet) Transaction(txid types.TransactionID) (modules.ProcessedTransaction, bool) {
    74  	w.mu.Lock()
    75  	defer w.mu.Unlock()
    76  	pt, exists := w.processedTransactionMap[txid]
    77  	if !exists {
    78  		return modules.ProcessedTransaction{}, exists
    79  	}
    80  	return *pt, exists
    81  }
    82  
    83  // Transactions returns all transactions relevant to the wallet that were
    84  // confirmed in the range [startHeight, endHeight].
    85  func (w *Wallet) Transactions(startHeight, endHeight types.BlockHeight) (pts []modules.ProcessedTransaction, err error) {
    86  	w.mu.Lock()
    87  	defer w.mu.Unlock()
    88  
    89  	if startHeight > w.consensusSetHeight || startHeight > endHeight {
    90  		return nil, errOutOfBounds
    91  	}
    92  	if len(w.processedTransactions) == 0 {
    93  		return nil, nil
    94  	}
    95  
    96  	for _, pt := range w.processedTransactions {
    97  		if pt.ConfirmationHeight > endHeight {
    98  			break
    99  		}
   100  		if pt.ConfirmationHeight >= startHeight {
   101  			pts = append(pts, pt)
   102  		}
   103  	}
   104  	return pts, nil
   105  }
   106  
   107  // UnconfirmedTransactions returns the set of unconfirmed transactions that are
   108  // relevant to the wallet.
   109  func (w *Wallet) UnconfirmedTransactions() []modules.ProcessedTransaction {
   110  	w.mu.Lock()
   111  	defer w.mu.Unlock()
   112  	return w.unconfirmedProcessedTransactions
   113  }