From 3e72022e5ae44010dc08d9c9cdaab5d663be91d4 Mon Sep 17 00:00:00 2001 From: Chris Pacia Date: Sat, 8 Apr 2017 13:15:16 -0400 Subject: [PATCH] Change markasdead txns function to updateheight --- Godeps/Godeps.json | 2 +- bitcoin/listeners/wallet_listener.go | 2 +- repo/db/txns.go | 6 +++--- repo/db/txns_test.go | 6 +++--- .../github.com/OpenBazaar/spvwallet/datastore.go | 4 ++-- .../OpenBazaar/spvwallet/sortsignsend.go | 2 +- vendor/github.com/OpenBazaar/spvwallet/txstore.go | 15 ++++++++++----- 7 files changed, 21 insertions(+), 16 deletions(-) diff --git a/Godeps/Godeps.json b/Godeps/Godeps.json index 8a7d31930e..4151ac66bc 100644 --- a/Godeps/Godeps.json +++ b/Godeps/Godeps.json @@ -17,7 +17,7 @@ }, { "ImportPath": "github.com/OpenBazaar/spvwallet", - "Rev": "0b11739502f86fdf1680e85e4f2d1a56cb453ab7" + "Rev": "5aaab072ece9db11dd775fced4fc4605d3f84c7d" }, { "ImportPath": "github.com/boltdb/bolt", diff --git a/bitcoin/listeners/wallet_listener.go b/bitcoin/listeners/wallet_listener.go index 80f1341a93..4cf1df3ac1 100644 --- a/bitcoin/listeners/wallet_listener.go +++ b/bitcoin/listeners/wallet_listener.go @@ -43,7 +43,7 @@ func (l *WalletListener) OnTransactionReceived(cb spvwallet.TransactionCallback) OrderId: metadata.OrderId, Thumbnail: metadata.Thumbnail, Height: cb.Height, - CanBumpFee: true, + CanBumpFee: cb.Value > 0, } l.broadcast <- n } diff --git a/repo/db/txns.go b/repo/db/txns.go index 1d3f29aa84..24dbf84b14 100644 --- a/repo/db/txns.go +++ b/repo/db/txns.go @@ -125,19 +125,19 @@ func (t *TxnsDB) Delete(txid *chainhash.Hash) error { return nil } -func (t *TxnsDB) MarkAsDead(txid chainhash.Hash) error { +func (t *TxnsDB) UpdateHeight(txid chainhash.Hash, height int) error { t.lock.Lock() defer t.lock.Unlock() tx, err := t.db.Begin() if err != nil { return err } - stmt, err := tx.Prepare("update txns set height=-1 where txid=?") + stmt, err := tx.Prepare("update txns set height=? where txid=?") if err != nil { return err } defer stmt.Close() - _, err = stmt.Exec(txid.String()) + _, err = stmt.Exec(height, txid.String()) if err != nil { tx.Rollback() return err diff --git a/repo/db/txns_test.go b/repo/db/txns_test.go index 596c937a42..361f7a7c45 100644 --- a/repo/db/txns_test.go +++ b/repo/db/txns_test.go @@ -131,7 +131,7 @@ func TestDeleteTxns(t *testing.T) { } } -func TestTxnsDB_MarkAsDead(t *testing.T) { +func TestTxnsDB_UpdateHeight(t *testing.T) { tx := wire.NewMsgTx(wire.TxVersion) txHex := "0100000001cbfe4948ebc9113244b802a96e4940fa063c0455a16ca1f39a1e1db03837d9c701000000da004830450221008994e3dba54cb0ea23ca008d0e361b4339ee7b44b5e9101f6837e6a1a89ce044022051be859c68a547feaf60ffacc43f528cf2963c088bde33424d859274505e3f450147304402206cd4ef92cc7f2862c67810479013330fcafe4d468f1370563d4dff6be5bcbedc02207688a09163e615bc82299a29e987e1d718cb99a91d46a1ab13d18c0f6e616a1601475221024760c9ba5fa6241da6ee8601f0266f0e0592f53735703f0feaae23eda6673ae821038cfa8e97caaafbe21455803043618440c28c501ec32d6ece6865003165a0d4d152aeffffffff029ae2c700000000001976a914f72f20a739ec3c3df1a1fd7eff122d13bd5ca39188acb64784240000000017a9140be09225644b4cfdbb472028d8ccaf6df736025c8700000000" raw, _ := hex.DecodeString(txHex) @@ -142,12 +142,12 @@ func TestTxnsDB_MarkAsDead(t *testing.T) { if err != nil { t.Error(err) } - err = txdb.MarkAsDead(tx.TxHash()) + err = txdb.UpdateHeight(tx.TxHash(), -1) if err != nil { t.Error(err) } _, txn, err := txdb.Get(tx.TxHash()) if txn.Height != -1 { - t.Error("Txn db failed to mark as dead") + t.Error("Txn db failed to update height") } } diff --git a/vendor/github.com/OpenBazaar/spvwallet/datastore.go b/vendor/github.com/OpenBazaar/spvwallet/datastore.go index 20b8e4a3ca..231d9345bf 100644 --- a/vendor/github.com/OpenBazaar/spvwallet/datastore.go +++ b/vendor/github.com/OpenBazaar/spvwallet/datastore.go @@ -50,8 +50,8 @@ type Txns interface { // Fetch all transactions from the db GetAll(includeWatchOnly bool) ([]Txn, error) - // Mark a transaction as dead - MarkAsDead(txid chainhash.Hash) error + // Update the height of a transaction + UpdateHeight(txid chainhash.Hash, height int) error // Delete a transactions from the db Delete(txid *chainhash.Hash) error diff --git a/vendor/github.com/OpenBazaar/spvwallet/sortsignsend.go b/vendor/github.com/OpenBazaar/spvwallet/sortsignsend.go index dfc0998e11..c06ffca1c1 100644 --- a/vendor/github.com/OpenBazaar/spvwallet/sortsignsend.go +++ b/vendor/github.com/OpenBazaar/spvwallet/sortsignsend.go @@ -80,7 +80,7 @@ func (w *SPVWallet) gatherCoins() map[coinset.Coin]*hd.ExtendedKey { utxos, _ := w.txstore.Utxos().GetAll() m := make(map[coinset.Coin]*hd.ExtendedKey) for _, u := range utxos { - if u.WatchOnly { + if u.WatchOnly || u.AtHeight == 0{ continue } var confirmations int32 diff --git a/vendor/github.com/OpenBazaar/spvwallet/txstore.go b/vendor/github.com/OpenBazaar/spvwallet/txstore.go index 8795b7da6b..f40c458dd3 100644 --- a/vendor/github.com/OpenBazaar/spvwallet/txstore.go +++ b/vendor/github.com/OpenBazaar/spvwallet/txstore.go @@ -217,7 +217,7 @@ func (ts *TxStore) Ingest(tx *wire.MsgTx, height int32) (uint32, error) { // Check to see if this is a double spend doubleSpends, err := ts.CheckDoubleSpends(tx) - if err != nil { + if err != nil { return hits, err } if len(doubleSpends) > 0 { @@ -330,20 +330,22 @@ func (ts *TxStore) Ingest(tx *wire.MsgTx, height int32) (uint32, error) { _, txn, err := ts.Txns().Get(tx.TxHash()) shouldCallback := false if err != nil { + cb.Value = value txn.Timestamp = time.Now() shouldCallback = true + ts.Txns().Put(tx, int(value), int(height), txn.Timestamp, hits == 0) } // Let's check the height before committing so we don't allow rogue peers to send us a lose // tx that resets our height to zero. if txn.Height <= 0 { - ts.Txns().Put(tx, int(value), int(height), txn.Timestamp, hits == 0) + ts.Txns().UpdateHeight(tx.TxHash(), int(height)) if height > 0 { + cb.Value = txn.Value shouldCallback = true } } if shouldCallback { // Callback on listeners - cb.Value = value for _, listener := range ts.listeners { listener(cb) } @@ -364,7 +366,7 @@ func (ts *TxStore) markAsDead(txid chainhash.Hash) error { if err != nil { return err } - err = ts.Txns().MarkAsDead(txid) + err = ts.Txns().UpdateHeight(txid, -1) if err != nil { return err } @@ -376,6 +378,9 @@ func (ts *TxStore) markAsDead(txid chainhash.Hash) error { if err := markStxoAsDead(s); err != nil { return err } + if err := ts.Utxos().Put(s.Utxo); err != nil { + return err + } } // If an dependency of the spend is dead then mark the spend as dead if txid.IsEqual(&s.Utxo.Op.Hash) { @@ -398,7 +403,7 @@ func (ts *TxStore) markAsDead(txid chainhash.Hash) error { if err != nil { return err } - ts.Txns().MarkAsDead(txid) + ts.Txns().UpdateHeight(txid, -1) } } return nil