Skip to content
This repository has been archived by the owner on Mar 7, 2023. It is now read-only.

Commit

Permalink
Merge pull request #119 from blockchain/duplicate-txs
Browse files Browse the repository at this point in the history
Check for duplicate transactions
  • Loading branch information
Sjors committed Feb 17, 2016
2 parents 6c3b86b + 4e798a2 commit 0e1bf07
Show file tree
Hide file tree
Showing 6 changed files with 124 additions and 5 deletions.
1 change: 1 addition & 0 deletions karma.conf.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ module.exports = function(karma) {
'tests/transaction_spec.js.coffee', //(OK)
'tests/transaction_spend_spec.js.coffee', //(OK)
'tests/wallet_spec.js.coffee', //(PARTIAL)
'tests/wallet_store_spec.js.coffee',
'tests/bip38_spec.js.coffee', //(OK)
'tests/address_spec.js.coffee',
'tests/keychain_spec.js.coffee',
Expand Down
8 changes: 6 additions & 2 deletions src/transaction-list.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,17 @@ TransactionList.prototype.fetchTxs = function (amount) {
};

TransactionList.prototype.pushTxs = function (txs) {
txs = Helpers.toArrayFormat(txs).map(Tx.factory);
txs = Helpers.toArrayFormat(txs).map(Tx.factory).filter(function (tx) {
return !this.transaction(tx.hash);
}.bind(this));
this._transactions = this._transactions.concat(txs);
this._events.emit('update');
};

TransactionList.prototype.shiftTxs = function (txs) {
txs = Helpers.toArrayFormat(txs).map(Tx.factory);
txs = Helpers.toArrayFormat(txs).map(Tx.factory).filter(function (tx) {
return !this.transaction(tx.hash);
}.bind(this));
this._transactions = txs.concat(this._transactions);
this._events.emit('update');
};
Expand Down
3 changes: 3 additions & 0 deletions src/wallet-store.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,9 @@ var WalletStore = (function() {
getCurrencies: function() {
return currencyCodeToCurrency;
},
getTransaction: function(hash) {
return transactions.filter(function(tx){return tx.hash === hash})[0]
},
getTransactions: function() {
return transactions;
},
Expand Down
49 changes: 48 additions & 1 deletion tests/blockchain_wallet_spec.js.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ MyWallet = undefined
Address = undefined
Wallet = undefined
HDWallet = undefined
WalletStore = undefined

describe "HDWallet", ->
wallet = undefined
Expand Down Expand Up @@ -77,11 +78,21 @@ describe "HDWallet", ->
isInstanceOf: (candidate, theClass) ->
candidate.label != undefined || typeof(candidate) == "object"

walletStoreTxs = []
WalletStore = {
pushTransaction: (tx) -> walletStoreTxs.push(tx)
getTransactions: () -> walletStoreTxs
getTransaction: (hash) -> walletStoreTxs.filter(
(tx) -> tx.hash == hash
)[0]
}

stubs = {
'./wallet' : MyWallet,
'./address' : Address,
'./helpers' : Helpers,
'./hd-wallet': HDWallet
'./hd-wallet': HDWallet,
'./wallet-store' : WalletStore
}
Wallet = proxyquire('../src/blockchain-wallet', stubs)

Expand Down Expand Up @@ -446,6 +457,42 @@ describe "HDWallet", ->
it ".getPrivateKeyForAddress", ->
pending()

describe "_updateWalletInfo()", ->
multiaddr = {
wallet:
total_sent: 1
total_received: 0
final_balance: 0
n_tx: 1
addresses: [
{
address:"1CzYCAAi46b8CFiybd3CcGbUykCAeqaocj",
n_tx:1,
total_received: 0,
total_sent: 1,
final_balance: 0
}
]
txs: [
{
hash: "1234"
}
]
info:
latest_block: 300000
}
beforeEach ->
spyOn(WalletStore, "pushTransaction").and.callThrough()

it "should add a new transaction", ->
wallet._updateWalletInfo(multiaddr)
expect(WalletStore.pushTransaction).toHaveBeenCalled()

it "should not add a duplicate transaction", ->
wallet._updateWalletInfo(multiaddr)
wallet._updateWalletInfo(multiaddr)
expect(WalletStore.getTransactions().length).toEqual(1)

describe "JSON serialization", ->

it 'should hold: fromJSON . toJSON = id', ->
Expand Down
24 changes: 22 additions & 2 deletions tests/transaction_list_spec.js.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,33 @@ describe 'TransactionList', ->
expect(txList.transactions().length).toEqual(1)
done()

it 'should not add duplicate tx to the list', (done) ->
txList.fetchTxs().then (numFetched1) ->

txList.fetchTxs().then (numFetched2) ->
expect(numFetched1).toEqual(1)
expect(numFetched2).toEqual(1)

expect(txList.transactions().length).toEqual(1)
done()

it 'should add txs to the tx list', ->
txList.pushTxs({ txType: 'sent' })
expect(txList.transactions.length).toEqual(1)

it 'should prepend txs to the tx list', ->
txList.shiftTxs({ txType: 'sent' })
expect(txList.transactions.length).toEqual(1)
expect(txList.transactions().length).toEqual(0)

txList.shiftTxs({ txType: 'sent', hash: "1234" })
expect(txList.transactions().length).toEqual(1)

it 'should not prepend duplicate txs to the tx list', ->
txList.shiftTxs({ txType: 'sent', hash: "1234"})
txList.shiftTxs({ txType: 'sent', hash: "1234"})

expect(txList.transactions().length).toEqual(1)



describe 'events', ->
spy = undefined
Expand Down
44 changes: 44 additions & 0 deletions tests/wallet_store_spec.js.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
proxyquire = require('proxyquireify')(require)
MyWallet = {
wallet: {
getHistory: () ->
syncWallet: () ->
}
}

stubs = {
'./wallet': MyWallet,
}

WalletStore = proxyquire('../src/wallet-store', stubs)

describe "WalletStore", ->

beforeEach ->
spyOn(MyWallet, "syncWallet")
spyOn(MyWallet.wallet, "getHistory")

describe "instance", ->
beforeEach ->

describe "getTransactions()", ->
it "should return nothing initially", ->
expect(WalletStore.getTransactions()).toEqual([])

describe "pushTransaction()", ->

it "add a transaction", ->
tx = {hash: "1234"}
WalletStore.pushTransaction(tx)
expect(WalletStore.getTransactions()).toEqual([tx])

describe "getTransaction()", ->
it "should return a transaction with the right hash", ->
tx1 = {hash: "1234"}
tx2 = {hash: "5678"}

WalletStore.pushTransaction(tx1)
WalletStore.pushTransaction(tx2)

expect(WalletStore.getTransaction("1234")).toEqual(tx1)
expect(WalletStore.getTransaction("5678")).toEqual(tx2)

0 comments on commit 0e1bf07

Please sign in to comment.