From fc100921e8915331e270de54e4838449319ab10f Mon Sep 17 00:00:00 2001
From: Tadge Dryja <tadge@lightning.network>
Date: Thu, 21 Jan 2016 17:59:50 -0800
Subject: [PATCH] add pushtx, and outpoint comparitor

---
 uspv/eight333.go   | 14 ++++++++++++++
 uspv/msghandler.go |  2 +-
 uspv/txstore.go    | 13 +++++++++++--
 3 files changed, 26 insertions(+), 3 deletions(-)

diff --git a/uspv/eight333.go b/uspv/eight333.go
index c3082a77d..045ec7422 100644
--- a/uspv/eight333.go
+++ b/uspv/eight333.go
@@ -346,6 +346,20 @@ func NewRootAndHeight(r wire.ShaHash, h int32) (rah RootAndHeight) {
 	return
 }
 
+func (s *SPVCon) PushTx(tx *wire.MsgTx) error {
+	txid := tx.TxSha()
+	err := s.TS.AddTxid(&txid, 0)
+	if err != nil {
+		return err
+	}
+	err = s.TS.AckTx(tx)
+	if err != nil {
+		return err
+	}
+	s.outMsgQueue <- tx
+	return nil
+}
+
 // AskForMerkBlocks requests blocks from current to last
 // right now this asks for 1 block per getData message.
 // Maybe it's faster to ask for many in a each message?
diff --git a/uspv/msghandler.go b/uspv/msghandler.go
index 505dff3b8..3d072832f 100644
--- a/uspv/msghandler.go
+++ b/uspv/msghandler.go
@@ -66,7 +66,7 @@ func (s *SPVCon) incomingMessageHandler() {
 				s.AskForHeaders()
 			}
 		case *wire.MsgTx:
-			err := s.TS.IngestTx(m)
+			err := s.TS.AckTx(m)
 			if err != nil {
 				log.Printf("Incoming Tx error: %s\n", err.Error())
 			}
diff --git a/uspv/txstore.go b/uspv/txstore.go
index 09fd09f11..c7bd604ae 100644
--- a/uspv/txstore.go
+++ b/uspv/txstore.go
@@ -86,7 +86,7 @@ func (t *TxStore) GimmeFilter() (*bloom.Filter, error) {
 }
 
 // Ingest a tx into wallet, dealing with both gains and losses
-func (t *TxStore) IngestTx(tx *wire.MsgTx) error {
+func (t *TxStore) AckTx(tx *wire.MsgTx) error {
 	inTxid := tx.TxSha()
 	height, ok := t.OKTxids[inTxid]
 	if !ok {
@@ -158,7 +158,7 @@ func (t *TxStore) ExpellTx(tx *wire.MsgTx, height int32) error {
 
 	for _, in := range tx.TxIn {
 		for i, myutxo := range t.Utxos {
-			if myutxo.Op == in.PreviousOutPoint {
+			if OutPointsEqual(myutxo.Op, in.PreviousOutPoint) {
 				hits++
 				loss += myutxo.Value
 				err := t.MarkSpent(&myutxo.Op, height, tx)
@@ -175,6 +175,15 @@ func (t *TxStore) ExpellTx(tx *wire.MsgTx, height int32) error {
 	return nil
 }
 
+// need this because before I was comparing pointers maybe?
+// so they were the same outpoint but stored in 2 places so false negative?
+func OutPointsEqual(a, b wire.OutPoint) bool {
+	if !a.Hash.IsEqual(&b.Hash) {
+		return false
+	}
+	return a.Index == b.Index
+}
+
 // TxToString prints out some info about a transaction. for testing / debugging
 func TxToString(tx *wire.MsgTx) string {
 	str := "\t\t\t - Tx - \n"