Skip to content
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

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 18 additions & 31 deletions lib/primitives/mtx.js
Original file line number Diff line number Diff line change
Expand Up @@ -1122,12 +1122,20 @@ class MTX extends TX {
assert(options, 'Options are required.');
assert(options.changeAddress, 'Change address is required.');

// Hack in place to ensure backward compataibility with previous hack
if (this.inputs.length > 0) {
const inputPrevoutKeys = this.inputs.map(input => input.prevout.toKey());
for (const coin of coins) {
const {hash, index} = coin;
const key = Outpoint.toKey(hash, index);
if ( inputPrevoutKeys.some(prevout => prevout.equals(key)) ) {
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);
Expand Down Expand Up @@ -1424,6 +1432,7 @@ class CoinSelector {
*/

constructor(tx, options) {
this.parent = tx;
this.tx = tx.clone();
this.coins = [];
this.outputValue = 0;
Expand Down Expand Up @@ -1569,6 +1578,12 @@ class CoinSelector {
for (let i = 0; i < this.tx.inputs.length; i++) {
const {prevout} = this.tx.inputs[i];
this.inputs.set(prevout.toKey(), i);
const coin = this.parent.view.getCoin(prevout);
// If the coin is not in the view, it's value cannot be known,
// therefore it can't be funded correctly.
if(!coin)
throw new Error('Could not resolve input coin value');
this.tx.view.addCoin(coin);
}
}
}
Expand All @@ -1585,7 +1600,6 @@ class CoinSelector {
this.chosen = [];
this.change = 0;
this.fee = CoinSelector.MIN_FEE;
this.tx.inputs.length = 0;

switch (this.selection) {
case 'all':
Expand Down Expand Up @@ -1688,33 +1702,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++)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here instead of pushing blindly, we check if view has it, if it already has it we don't push this. Everything else can remain same.

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;

Expand Down
25 changes: 12 additions & 13 deletions lib/wallet/wallet.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);
Copy link
Contributor Author

Choose a reason for hiding this comment

The 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.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So I think now with this logic: edc1f25 using addCoin() is actually an optimization!

Copy link
Contributor

Choose a reason for hiding this comment

The 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 });
Expand Down Expand Up @@ -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);
}

Expand Down Expand Up @@ -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);
}

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
18 changes: 9 additions & 9 deletions test/util/memwallet.js
Original file line number Diff line number Diff line change
Expand Up @@ -1155,7 +1155,7 @@ class MemWallet {
output.covenant.pushU32(ns.height);
output.covenant.pushHash(nonce);

mtx.addOutpoint(prevout);
mtx.addCoin(coin);
mtx.outputs.push(output);
}

Expand Down Expand Up @@ -1207,7 +1207,7 @@ class MemWallet {
if (coin.height < ns.height)
continue;

mtx.addOutpoint(prevout);
mtx.addCoin(coin);

const output = new Output();
output.address = coin.address;
Expand Down Expand Up @@ -1286,7 +1286,7 @@ class MemWallet {
output.covenant.pushHash(this.getRenewalBlock());

const mtx = new MTX();
mtx.addOutpoint(ns.owner);
mtx.addCoin(coin);
mtx.outputs.push(output);

return this._create(mtx, options);
Expand Down Expand Up @@ -1348,7 +1348,7 @@ class MemWallet {
output.covenant.push(resource);

const mtx = new MTX();
mtx.addOutpoint(ns.owner);
mtx.addCoin(coin);
mtx.outputs.push(output);

return this._create(mtx, options);
Expand Down Expand Up @@ -1405,7 +1405,7 @@ class MemWallet {
output.covenant.pushHash(this.getRenewalBlock());

const mtx = new MTX();
mtx.addOutpoint(ns.owner);
mtx.addCoin(coin);
mtx.outputs.push(output);

return this._create(mtx, options);
Expand Down Expand Up @@ -1461,7 +1461,7 @@ class MemWallet {
output.covenant.push(address.hash);

const mtx = new MTX();
mtx.addOutpoint(ns.owner);
mtx.addCoin(coin);
mtx.outputs.push(output);

return this._create(mtx, options);
Expand Down Expand Up @@ -1516,7 +1516,7 @@ class MemWallet {
output.covenant.push(EMPTY);

const mtx = new MTX();
mtx.addOutpoint(ns.owner);
mtx.addCoin(coin);
mtx.outputs.push(output);

return this._create(mtx, options);
Expand Down Expand Up @@ -1582,7 +1582,7 @@ class MemWallet {
output.covenant.pushHash(this.getRenewalBlock());

const mtx = new MTX();
mtx.addOutpoint(ns.owner);
mtx.addCoin(coin);
mtx.outputs.push(output);

return this._create(mtx, options);
Expand Down Expand Up @@ -1635,7 +1635,7 @@ class MemWallet {
output.covenant.pushU32(ns.height);

const mtx = new MTX();
mtx.addOutpoint(ns.owner);
mtx.addCoin(coin);
mtx.outputs.push(output);

return this._create(mtx, options);
Expand Down