Beispiel #1
0
// GetAddressTransactions returns the Transactions whose unspents give coins to a cipher.Address.
// This includes unconfirmed txns' predicted unspents.
func (vs *Visor) GetAddressTransactions(a cipher.Address) []Transaction {
	var txns []Transaction
	// Look in the blockchain
	uxs := vs.Blockchain.GetUnspent().AllForAddress(a)
	mxSeq := vs.HeadBkSeq()
	var bk *coin.Block
	for _, ux := range uxs {
		if bk = vs.GetBlockBySeq(ux.Head.BkSeq); bk == nil {
			return txns
		}

		tx, ok := bk.GetTransaction(ux.Body.SrcTransaction)
		if ok {
			h := mxSeq - bk.Head.BkSeq + 1
			txns = append(txns, Transaction{
				Txn:    tx,
				Status: NewConfirmedTransactionStatus(h),
				Time:   bk.Time(),
			})
		}
	}

	// Look in the unconfirmed pool
	uxs = vs.Unconfirmed.Unspent.AllForAddress(a)
	for _, ux := range uxs {
		tx, ok := vs.Unconfirmed.Txns[ux.Body.SrcTransaction]
		if !ok {
			logger.Critical("Unconfirmed unspent missing unconfirmed txn")
			continue
		}
		txns = append(txns, Transaction{
			Txn:    tx.Txn,
			Status: NewUnconfirmedTransactionStatus(),
			Time:   uint64(tx.Received.Unix()),
		})
	}

	return txns
}
Beispiel #2
0
// GetLastTxs returns last confirmed transactions, return nil if empty
func (vs *Visor) GetLastTxs() ([]*Transaction, error) {
	ltxs, err := vs.history.GetLastTxs()
	if err != nil {
		return nil, err
	}

	txs := make([]*Transaction, len(ltxs))
	var confirms uint64
	bh := vs.GetHeadBlock().Seq()
	var b *coin.Block
	for i, tx := range ltxs {
		confirms = bh - tx.BlockSeq + 1
		if b = vs.GetBlockBySeq(tx.BlockSeq); b == nil {
			return nil, fmt.Errorf("found no block in seq %v", tx.BlockSeq)
		}

		txs[i] = &Transaction{
			Txn:    tx.Tx,
			Status: NewConfirmedTransactionStatus(confirms),
			Time:   b.Time(),
		}
	}
	return txs, nil
}