wallet: processTx -> processConfirmedTx, processPendingTx

This commit is contained in:
Wei Lu 2014-06-12 15:42:10 +08:00
parent e064e9d29d
commit d265b53b03
2 changed files with 89 additions and 76 deletions

View file

@ -137,7 +137,15 @@ function Wallet(seed, network) {
return value == undefined return value == undefined
} }
this.processTx = function(tx, isPending) { this.processPendingTx = function(tx){
processTx(tx, true)
}
this.processConfirmedTx = function(tx){
processTx(tx, false)
}
function processTx(tx, isPending) {
var txhash = tx.getHash() var txhash = tx.getHash()
tx.outs.forEach(function(txOut, i){ tx.outs.forEach(function(txOut, i){

View file

@ -252,7 +252,7 @@ describe('Wallet', function() {
}) })
}) })
describe('processTx', function(){ describe('Process transaction', function(){
var addresses var addresses
var tx var tx
@ -266,6 +266,17 @@ describe('Wallet', function() {
tx = Transaction.fromHex(fixtureTx1Hex) tx = Transaction.fromHex(fixtureTx1Hex)
}) })
describe("processPendingTx", function(){
it("sets the pending flag on output", function(){
wallet.addresses = [addresses[0]]
wallet.processPendingTx(tx)
verifyOutputAdded(0, true)
})
})
describe('processConfirmedTx', function(){
it('does not fail on scripts with no corresponding Address', function() { it('does not fail on scripts with no corresponding Address', function() {
var pubKey = wallet.getPrivateKey(0).pub var pubKey = wallet.getPrivateKey(0).pub
var script = Script.createPubKeyScriptPubKey(pubKey) var script = Script.createPubKeyScriptPubKey(pubKey)
@ -278,7 +289,7 @@ describe('Wallet', function() {
tx2.outs[0].script = script tx2.outs[0].script = script
tx2.outs[0].address = undefined tx2.outs[0].address = undefined
wallet.processTx(tx2) wallet.processConfirmedTx(tx2)
}) })
describe("when tx outs contains an address owned by the wallet, an 'output' gets added to wallet.outputs", function(){ describe("when tx outs contains an address owned by the wallet, an 'output' gets added to wallet.outputs", function(){
@ -286,35 +297,60 @@ describe('Wallet', function() {
var totalOuts = outputCount() var totalOuts = outputCount()
wallet.addresses = [addresses[0]] wallet.addresses = [addresses[0]]
wallet.processTx(tx) wallet.processConfirmedTx(tx)
assert.equal(outputCount(), totalOuts + 1) assert.equal(outputCount(), totalOuts + 1)
verifyOutputAdded(0) verifyOutputAdded(0, false)
}) })
it("works for change address", function(){ it("works for change address", function(){
var totalOuts = outputCount() var totalOuts = outputCount()
wallet.changeAddresses = [addresses[1]] wallet.changeAddresses = [addresses[1]]
wallet.processTx(tx) wallet.processConfirmedTx(tx)
assert.equal(outputCount(), totalOuts + 1) assert.equal(outputCount(), totalOuts + 1)
verifyOutputAdded(1) verifyOutputAdded(1, false)
})
describe("when the pending flag is set", function(){
it("sets the pending flag on output", function(){
wallet.addresses = [addresses[0]]
wallet.processTx(tx, true)
verifyOutputAdded(0, true)
})
}) })
function outputCount(){ function outputCount(){
return Object.keys(wallet.outputs).length return Object.keys(wallet.outputs).length
} }
})
describe("when tx ins outpoint contains a known txhash:i, the corresponding 'output' gets updated", function(){
beforeEach(function(){
wallet.addresses = [addresses[0]] // the address fixtureTx2 used as input
wallet.processConfirmedTx(tx)
tx = Transaction.fromHex(fixtureTx2Hex)
})
it("does not add to wallet.outputs", function(){
var outputs = wallet.outputs
wallet.processConfirmedTx(tx)
assert.deepEqual(wallet.outputs, outputs)
})
it("sets spend with the transaction hash and input index", function(){
wallet.processConfirmedTx(tx)
var txIn = tx.ins[0]
var key = txIn.outpoint.hash + ":" + txIn.outpoint.index
var output = wallet.outputs[key]
assert.equal(output.spend, tx.getHash() + ':' + 0)
})
})
it("does nothing when none of the involved addresses belong to the wallet", function(){
var outputs = wallet.outputs
wallet.processConfirmedTx(tx)
assert.deepEqual(wallet.outputs, outputs)
})
})
function verifyOutputAdded(index, pending) { function verifyOutputAdded(index, pending) {
var txOut = tx.outs[index] var txOut = tx.outs[index]
var key = tx.getHash() + ":" + index var key = tx.getHash() + ":" + index
@ -328,37 +364,6 @@ describe('Wallet', function() {
} }
}) })
describe("when tx ins outpoint contains a known txhash:i, the corresponding 'output' gets updated", function(){
beforeEach(function(){
wallet.addresses = [addresses[0]] // the address fixtureTx2 used as input
wallet.processTx(tx)
tx = Transaction.fromHex(fixtureTx2Hex)
})
it("does not add to wallet.outputs", function(){
var outputs = wallet.outputs
wallet.processTx(tx)
assert.deepEqual(wallet.outputs, outputs)
})
it("sets spend with the transaction hash and input index", function(){
wallet.processTx(tx)
var txIn = tx.ins[0]
var key = txIn.outpoint.hash + ":" + txIn.outpoint.index
var output = wallet.outputs[key]
assert.equal(output.spend, tx.getHash() + ':' + 0)
})
})
it("does nothing when none of the involved addresses belong to the wallet", function(){
var outputs = wallet.outputs
wallet.processTx(tx)
assert.deepEqual(wallet.outputs, outputs)
})
})
describe('createTx', function(){ describe('createTx', function(){
var to, value var to, value