-
Notifications
You must be signed in to change notification settings - Fork 285
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Wallet.fund() messes up existing inputs #639
Changes from all commits
961645a
c9d0d8d
3506db5
76b06e2
43bf26c
edc1f25
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -100,15 +100,21 @@ class MTX extends TX { | |
} | ||
|
||
/** | ||
* Clone the transaction. Note that | ||
* this will not carry over the view. | ||
* Clone the transaction and it's input coins. | ||
* @returns {MTX} | ||
*/ | ||
|
||
inject(mtx) { | ||
assert(mtx instanceof this.constructor); | ||
super.inject(mtx); | ||
this.changeIndex = mtx.changeIndex; | ||
|
||
// CoinView has other properties like UndoCoins and BitView | ||
// that an MTX doesn't care about. We just want a copy of the | ||
// coins spent by the MTX. | ||
for (const [key, value] of mtx.view.map.entries()) | ||
this.view.map.set(key, value); | ||
|
||
return this; | ||
} | ||
|
||
|
@@ -158,11 +164,19 @@ class MTX extends TX { | |
addCoin(coin) { | ||
assert(coin instanceof Coin, 'Cannot add non-coin.'); | ||
|
||
const input = Input.fromCoin(coin); | ||
// Avoid duplicate inputs | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
for (const input of this.inputs) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Another note is this is inefficient way of checking this |
||
const {hash, index} = input.prevout; | ||
if (hash.equals(coin.hash) && index === coin.index) { | ||
this.view.addCoin(coin); | ||
return input; | ||
} | ||
} | ||
|
||
// Coin represents a new input. | ||
const input = Input.fromCoin(coin); | ||
this.inputs.push(input); | ||
this.view.addCoin(coin); | ||
|
||
return input; | ||
} | ||
|
||
|
@@ -1122,12 +1136,31 @@ class MTX extends TX { | |
assert(options, 'Options are required.'); | ||
assert(options.changeAddress, 'Change address is required.'); | ||
|
||
// Ensure backward compatibility with the old API: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we really want to remove old API? Other than incorrectly managing preferred inputs, was there another thing wrong with it? As far as I can see, what we want to fix is - when |
||
// Inputs to MTX were added with mtx.addOutpoint() and then later | ||
// CoinSelector.fund() was expected to match those outpoints with coins | ||
// from the wallet. The problem with this is that when CoinSelector | ||
// initialized, it wiped out all the inputs, including some that may have | ||
// already been signed outside the wallet for custom scripts or CoinJoins. | ||
// With the new API, mtx.addCoin() can be used to either fund existing | ||
// (pre-signed) inputs OR add new "preferred" inputs for covenants. | ||
// The logic below was moved here from CoinSelector.fund() in case | ||
// the old method was used and "preferred" coins are not yet in the view. | ||
for (const input of this.inputs) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This will become totally unnecessary, if we do the thing mentioned above. Also Complexity of this will be off charts. We already have issues with the |
||
const {prevout} = input; | ||
if (this.view.hasEntry(prevout)) | ||
continue; | ||
|
||
for (const coin of coins) { | ||
if (prevout.hash.equals(coin.hash) && prevout.index === coin.index) { | ||
this.view.addCoin(coin); | ||
} | ||
} | ||
} | ||
|
||
// Select necessary coins. | ||
const select = await this.selectCoins(coins, options); | ||
|
||
// Make sure we empty the input array. | ||
this.inputs.length = 0; | ||
|
||
// Add coins to transaction. | ||
for (const coin of select.chosen) | ||
this.addCoin(coin); | ||
|
@@ -1585,7 +1618,6 @@ class CoinSelector { | |
this.chosen = []; | ||
this.change = 0; | ||
this.fee = CoinSelector.MIN_FEE; | ||
this.tx.inputs.length = 0; | ||
|
||
switch (this.selection) { | ||
case 'all': | ||
|
@@ -1688,33 +1720,6 @@ class CoinSelector { | |
*/ | ||
|
||
fund() { | ||
// Ensure all preferred inputs first. | ||
if (this.inputs.size > 0) { | ||
const coins = []; | ||
|
||
for (let i = 0; i < this.inputs.size; i++) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Here instead of pushing blindly, we check if |
||
coins.push(null); | ||
|
||
for (const coin of this.coins) { | ||
const {hash, index} = coin; | ||
const key = Outpoint.toKey(hash, index); | ||
const i = this.inputs.get(key); | ||
|
||
if (i != null) { | ||
coins[i] = coin; | ||
this.inputs.delete(key); | ||
} | ||
} | ||
|
||
if (this.inputs.size > 0) | ||
throw new Error('Could not resolve preferred inputs.'); | ||
|
||
for (const coin of coins) { | ||
this.tx.addCoin(coin); | ||
this.chosen.push(coin); | ||
} | ||
} | ||
|
||
if (this.isFull()) | ||
return; | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -38,7 +38,6 @@ const {types} = rules; | |
const {Mnemonic} = HD; | ||
const {BufferSet} = require('buffer-map'); | ||
const Coin = require('../primitives/coin'); | ||
const Outpoint = require('../primitives/outpoint'); | ||
|
||
/* | ||
* Constants | ||
|
@@ -1839,7 +1838,7 @@ class Wallet extends EventEmitter { | |
output.covenant.pushHash(nameHash); | ||
output.covenant.pushU32(height); | ||
output.covenant.pushHash(nonce); | ||
reveal.addOutpoint(Outpoint.fromTX(bid, bidOuputIndex)); | ||
reveal.addCoin(bidCoin); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These are technically not necessary because the Coin would attempted to be added to CoinView if it's already not there. But this is cleaner. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So I think now with this logic: edc1f25 using There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. addCoin will still stay an optimization if we use my suggestion. |
||
reveal.outputs.push(output); | ||
|
||
await this.fill(reveal, { ...options, coins: coins }); | ||
|
@@ -1926,7 +1925,7 @@ class Wallet extends EventEmitter { | |
output.covenant.pushU32(ns.height); | ||
output.covenant.pushHash(nonce); | ||
|
||
mtx.addOutpoint(prevout); | ||
mtx.addCoin(coin); | ||
mtx.outputs.push(output); | ||
} | ||
|
||
|
@@ -2051,7 +2050,7 @@ class Wallet extends EventEmitter { | |
output.covenant.pushU32(ns.height); | ||
output.covenant.pushHash(nonce); | ||
|
||
mtx.addOutpoint(prevout); | ||
mtx.addCoin(coin); | ||
mtx.outputs.push(output); | ||
} | ||
|
||
|
@@ -2176,7 +2175,7 @@ class Wallet extends EventEmitter { | |
if (coin.height < ns.height) | ||
continue; | ||
|
||
mtx.addOutpoint(prevout); | ||
mtx.addCoin(coin); | ||
|
||
const output = new Output(); | ||
output.address = coin.address; | ||
|
@@ -2298,7 +2297,7 @@ class Wallet extends EventEmitter { | |
if (coin.height < ns.height) | ||
continue; | ||
|
||
mtx.addOutpoint(prevout); | ||
mtx.addCoin(coin); | ||
|
||
const output = new Output(); | ||
output.address = coin.address; | ||
|
@@ -2444,7 +2443,7 @@ class Wallet extends EventEmitter { | |
output.covenant.pushHash(await this.wdb.getRenewalBlock()); | ||
|
||
const mtx = new MTX(); | ||
mtx.addOutpoint(ns.owner); | ||
mtx.addCoin(coin); | ||
mtx.outputs.push(output); | ||
|
||
return mtx; | ||
|
@@ -2524,7 +2523,7 @@ class Wallet extends EventEmitter { | |
output.covenant.push(raw); | ||
|
||
const mtx = new MTX(); | ||
mtx.addOutpoint(ns.owner); | ||
mtx.addCoin(coin); | ||
mtx.outputs.push(output); | ||
|
||
return mtx; | ||
|
@@ -2663,7 +2662,7 @@ class Wallet extends EventEmitter { | |
output.covenant.pushHash(await this.wdb.getRenewalBlock()); | ||
|
||
const mtx = new MTX(); | ||
mtx.addOutpoint(ns.owner); | ||
mtx.addCoin(coin); | ||
mtx.outputs.push(output); | ||
|
||
return mtx; | ||
|
@@ -2797,7 +2796,7 @@ class Wallet extends EventEmitter { | |
output.covenant.push(address.hash); | ||
|
||
const mtx = new MTX(); | ||
mtx.addOutpoint(ns.owner); | ||
mtx.addCoin(coin); | ||
mtx.outputs.push(output); | ||
|
||
return mtx; | ||
|
@@ -2933,7 +2932,7 @@ class Wallet extends EventEmitter { | |
output.covenant.push(EMPTY); | ||
|
||
const mtx = new MTX(); | ||
mtx.addOutpoint(ns.owner); | ||
mtx.addCoin(coin); | ||
mtx.outputs.push(output); | ||
|
||
return mtx; | ||
|
@@ -3077,7 +3076,7 @@ class Wallet extends EventEmitter { | |
output.covenant.pushHash(await this.wdb.getRenewalBlock()); | ||
|
||
const mtx = new MTX(); | ||
mtx.addOutpoint(ns.owner); | ||
mtx.addCoin(coin); | ||
mtx.outputs.push(output); | ||
|
||
return mtx; | ||
|
@@ -3208,7 +3207,7 @@ class Wallet extends EventEmitter { | |
output.covenant.pushU32(ns.height); | ||
|
||
const mtx = new MTX(); | ||
mtx.addOutpoint(ns.owner); | ||
mtx.addCoin(coin); | ||
mtx.outputs.push(output); | ||
|
||
return mtx; | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The reason behind not cloning the view is that in most cases it's not necessary and it's complex.
Also we have couple of variations on the view. Notably, the new
WalletCoinView
needs different approach to be properly cloned.So in most cases
mtx
clone is done like:Referencing the old view. It needs double checking if there's a case where old way messes things up, but I doubt. Most views in the wallet side are short lived.