Wallet: rename outputs to unspentsMap

This commit is contained in:
Daniel Cousens 2014-08-18 15:18:20 +10:00
parent d24fdef585
commit 06f13db8d7
2 changed files with 24 additions and 24 deletions

View file

@ -24,7 +24,7 @@ function Wallet(seed, network) {
this.addresses = [] this.addresses = []
this.changeAddresses = [] this.changeAddresses = []
this.network = network this.network = network
this.outputs = {} this.unspentMap = {}
// FIXME: remove in 2.x.y // FIXME: remove in 2.x.y
var me = this var me = this
@ -41,7 +41,7 @@ function Wallet(seed, network) {
me.addresses = [] me.addresses = []
me.changeAddresses = [] me.changeAddresses = []
me.outputs = {} me.unspentMap = {}
} }
this.getMasterKey = function() { return masterKey } this.getMasterKey = function() { return masterKey }
@ -53,7 +53,7 @@ function Wallet(seed, network) {
Wallet.prototype.createTx = function(to, value, fixedFee, changeAddress) { Wallet.prototype.createTx = function(to, value, fixedFee, changeAddress) {
assert(value > this.network.dustThreshold, value + ' must be above dust threshold (' + this.network.dustThreshold + ' Satoshis)') assert(value > this.network.dustThreshold, value + ' must be above dust threshold (' + this.network.dustThreshold + ' Satoshis)')
var utxos = getCandidateOutputs(this.outputs, value) var utxos = getCandidateOutputs(this.unspentMap, value)
var accum = 0 var accum = 0
var subTotal = value var subTotal = value
var addresses = [] var addresses = []
@ -112,7 +112,7 @@ Wallet.prototype.__processTx = function(tx, isPending) {
if (myAddresses.indexOf(address) > -1) { if (myAddresses.indexOf(address) > -1) {
var output = txId + ':' + i var output = txId + ':' + i
this.outputs[output] = { this.unspentMap[output] = {
hash: txHash, hash: txHash,
index: i, index: i,
value: txOut.value, value: txOut.value,
@ -127,14 +127,14 @@ Wallet.prototype.__processTx = function(tx, isPending) {
var txinId = bufferutils.reverse(txIn.hash).toString('hex') var txinId = bufferutils.reverse(txIn.hash).toString('hex')
var output = txinId + ':' + txIn.index var output = txinId + ':' + txIn.index
if (!(output in this.outputs)) return if (!(output in this.unspentMap)) return
if (isPending) { if (isPending) {
this.outputs[output].pending = true this.unspentMap[output].pending = true
this.outputs[output].spent = true this.unspentMap[output].spent = true
} else { } else {
delete this.outputs[output] delete this.unspentMap[output]
} }
}, this) }, this)
} }
@ -204,8 +204,8 @@ Wallet.prototype.getReceiveAddress = function() {
Wallet.prototype.getUnspentOutputs = function() { Wallet.prototype.getUnspentOutputs = function() {
var utxos = [] var utxos = []
for (var key in this.outputs) { for (var key in this.unspentMap) {
var output = this.outputs[key] var output = this.unspentMap[key]
// Don't include pending spent outputs // Don't include pending spent outputs
if (!output.spent) { if (!output.spent) {
@ -245,7 +245,7 @@ Wallet.prototype.setUnspentOutputs = function(utxos) {
var output = txId + ':' + index var output = txId + ':' + index
this.outputs[output] = { this.unspentMap[output] = {
address: address, address: address,
hash: hash, hash: hash,
index: index, index: index,

View file

@ -217,7 +217,7 @@ describe('Wallet', function() {
}) })
it('matches the expected behaviour', function() { it('matches the expected behaviour', function() {
var output = wallet.outputs[expectedOutputKey] var output = wallet.unspentMap[expectedOutputKey]
assert(output) assert(output)
assert.equal(output.value, utxo.value) assert.equal(output.value, utxo.value)
@ -250,7 +250,7 @@ describe('Wallet', function() {
}) })
it("ignores pending spending outputs (outputs with 'spent' property)", function() { it("ignores pending spending outputs (outputs with 'spent' property)", function() {
var output = wallet.outputs[expectedOutputKey] var output = wallet.unspentMap[expectedOutputKey]
output.pending = true output.pending = true
output.spent = true output.spent = true
assert.deepEqual(wallet.getUnspentOutputs(), []) assert.deepEqual(wallet.getUnspentOutputs(), [])
@ -279,7 +279,7 @@ describe('Wallet', function() {
it('matches the expected behaviour', function() { it('matches the expected behaviour', function() {
wallet.setUnspentOutputs([utxo]) wallet.setUnspentOutputs([utxo])
var output = wallet.outputs[expectedOutputKey] var output = wallet.unspentMap[expectedOutputKey]
assert(output) assert(output)
assert.equal(output.value, utxo.value) assert.equal(output.value, utxo.value)
assert.equal(output.address, utxo.address) assert.equal(output.address, utxo.address)
@ -341,7 +341,7 @@ describe('Wallet', function() {
txInId = txInId.toString('hex') txInId = txInId.toString('hex')
var key = txInId + ':' + txIn.index var key = txInId + ':' + txIn.index
var output = wallet.outputs[key] var output = wallet.unspentMap[key]
assert(!output.pending) assert(!output.pending)
wallet.processPendingTx(spendTx) wallet.processPendingTx(spendTx)
@ -363,7 +363,7 @@ describe('Wallet', function() {
wallet.processConfirmedTx(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.unspentMap", function() {
it("works for receive address", function() { it("works for receive address", function() {
var totalOuts = outputCount() var totalOuts = outputCount()
@ -385,7 +385,7 @@ describe('Wallet', function() {
}) })
function outputCount() { function outputCount() {
return Object.keys(wallet.outputs).length return Object.keys(wallet.unspentMap).length
} }
}) })
@ -398,9 +398,9 @@ describe('Wallet', function() {
spendTx = Transaction.fromHex(fixtureTx2Hex) spendTx = Transaction.fromHex(fixtureTx2Hex)
}) })
it("does not add to wallet.outputs", function() { it("does not add to wallet.unspentMap", function() {
wallet.processConfirmedTx(spendTx) wallet.processConfirmedTx(spendTx)
assert.deepEqual(wallet.outputs, {}) assert.deepEqual(wallet.unspentMap, {})
}) })
it("deletes corresponding 'output'", function() { it("deletes corresponding 'output'", function() {
@ -410,16 +410,16 @@ describe('Wallet', function() {
txInId = txInId.toString('hex') txInId = txInId.toString('hex')
var expected = txInId + ':' + txIn.index var expected = txInId + ':' + txIn.index
assert(expected in wallet.outputs) assert(expected in wallet.unspentMap)
wallet.processConfirmedTx(spendTx) wallet.processConfirmedTx(spendTx)
assert(!(expected in wallet.outputs)) assert(!(expected in wallet.unspentMap))
}) })
}) })
it("does nothing when none of the involved addresses belong to the wallet", function() { it("does nothing when none of the involved addresses belong to the wallet", function() {
wallet.processConfirmedTx(tx) wallet.processConfirmedTx(tx)
assert.deepEqual(wallet.outputs, {}) assert.deepEqual(wallet.unspentMap, {})
}) })
}) })
@ -427,7 +427,7 @@ describe('Wallet', function() {
var txOut = tx.outs[index] var txOut = tx.outs[index]
var key = tx.getId() + ":" + index var key = tx.getId() + ":" + index
var output = wallet.outputs[key] var output = wallet.unspentMap[key]
assert.deepEqual(output.hash, tx.getHash()) assert.deepEqual(output.hash, tx.getHash())
assert.equal(output.value, txOut.value) assert.equal(output.value, txOut.value)
assert.equal(output.pending, pending) assert.equal(output.pending, pending)
@ -514,7 +514,7 @@ describe('Wallet', function() {
function getFee(wallet, tx) { function getFee(wallet, tx) {
var inputValue = tx.ins.reduce(function(memo, input){ var inputValue = tx.ins.reduce(function(memo, input){
var id = Array.prototype.reverse.call(input.hash).toString('hex') var id = Array.prototype.reverse.call(input.hash).toString('hex')
return memo + wallet.outputs[id + ':' + input.index].value return memo + wallet.unspentMap[id + ':' + input.index].value
}, 0) }, 0)
return tx.outs.reduce(function(memo, output){ return tx.outs.reduce(function(memo, output){