Skip to content

Commit

Permalink
mtx: rename existing inputs.
Browse files Browse the repository at this point in the history
  • Loading branch information
nodech committed Aug 22, 2024
1 parent 7e69a76 commit c9cc309
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 31 deletions.
47 changes: 26 additions & 21 deletions lib/primitives/mtx.js
Original file line number Diff line number Diff line change
Expand Up @@ -1403,7 +1403,7 @@ class MTX extends TX {
/**
* Coin Selector
* @alias module:primitives.CoinSelector
* @property {TX} tx - clone of the original mtx.
* @property {MTX} tx - clone of the original mtx.
* @property {CoinView} view - reference to the original view.
* @property {Coin[]} coins - List of available coins.
* @property {Amount} outputValue - Total output value.
Expand All @@ -1416,21 +1416,22 @@ class MTX extends TX {
* @property {Boolean} subtractFee - whether to subtract fee from the output.
* @property {Number} subtractIndex - index of the output to subtract fee from.
* @property {Number} height - height of the chain. (to check spendability)
* @property {Number} depth - depth or confirmations for the coin.
* @property {Number} depth - minimum confirmation depth of coins to spend.
* @property {Number} hardFee - fixed fee.
* @property {Number} rate - Rate of dollarydoo per kB.
* @property {Number} maxFee - maximum fee we are willing to pay.
* @property {Boolean} round - round to the nearest kilobyte.
* @property {Number} coinbaseMaturity - when do CBs become spendable.
* @property {Function} estimate - Input script size estimator.
* @property {Object[]} inputs - preferred inputs.
* @property {Function?} estimate - Input script size estimator.
* @property {BufferMap<Buffer, Number>} existingInputs - inputs from tx.
* @property {BufferMap<Buffer, Number>} preferredInputs - preferred inputs.
*/

class CoinSelector {
/**
* Create a coin selector.
* @constructor
* @param {TX} tx
* @param {MTX} tx - tx to clone.
* @param {Object?} options
*/

Expand All @@ -1456,7 +1457,7 @@ class CoinSelector {
this.round = false;
this.coinbaseMaturity = 400;
this.changeAddress = null;
this.inputs = new BufferMap();
this.existingInputs = new BufferMap();
this.preferredInputs = new BufferMap();

// Needed for size estimation.
Expand Down Expand Up @@ -1560,10 +1561,10 @@ class CoinSelector {
this.estimate = options.estimate;
}

if (options.inputs) {
assert(Array.isArray(options.inputs));
for (let i = 0; i < options.inputs.length; i++) {
const prevout = options.inputs[i];
if (options.preferredInputs) {
assert(Array.isArray(options.preferredInputs));
for (let i = 0; i < options.preferredInputs.length; i++) {
const prevout = options.preferredInputs[i];
assert(prevout && typeof prevout === 'object');
const {hash, index} = prevout;
this.preferredInputs.set(Outpoint.toKey(hash, index), i);
Expand All @@ -1582,7 +1583,7 @@ class CoinSelector {
if (this.tx.inputs.length > 0) {
for (let i = 0; i < this.tx.inputs.length; i++) {
const {prevout} = this.tx.inputs[i];
this.inputs.set(prevout.toKey(), i);
this.existingInputs.set(prevout.toKey(), i);
}
}
}
Expand Down Expand Up @@ -1698,7 +1699,7 @@ class CoinSelector {
*/

fund() {
this.resolveInputs();
this.resolveExisting();
this.resolvePreferred();

if (this.isFull())
Expand All @@ -1725,44 +1726,46 @@ class CoinSelector {
* Resolve coins for existing inputs.
*/

resolveInputs() {
if (this.inputs.size === 0)
resolveExisting() {
if (this.existingInputs.size === 0)
return;

// Ensure we have coins for the existing inputs.
const view = this.tx.view;
const coins = [];

for (let i = 0; i < this.inputs.size; i++)
for (let i = 0; i < this.existingInputs.size; i++)
coins.push(null);

// check coinview first
for (const [key, index] of this.inputs.entries()) {
for (const [key, index] of this.existingInputs.entries()) {
const prevout = Outpoint.fromKey(key);

if (this.view.hasEntry(prevout)) {
const coinEntry = this.view.getEntry(prevout);

coins[index] = coinEntry.toCoin(prevout);
this.inputs.delete(key);
this.existingInputs.delete(key);
}
}

// skip this if we resolved all inputs.
if (this.inputs.size > 0) {
if (this.existingInputs.size > 0) {
// Costly operations that tries to fill coinview from passed in
// coin list.
for (const coin of this.coins) {
const {hash, index} = coin;
const key = Outpoint.toKey(hash, index);
const i = this.inputs.get(key);
const i = this.existingInputs.get(key);

if (i != null) {
coins[i] = coin;
this.inputs.delete(key);
this.existingInputs.delete(key);
}
}
}

if (this.inputs.size > 0)
if (this.existingInputs.size > 0)
throw new Error('Could not resolve existing inputs.');

for (const coin of coins) {
Expand Down Expand Up @@ -1798,6 +1801,8 @@ class CoinSelector {

// skip this if we resolved all inputs.
if (this.preferredInputs.size > 0) {
// Costly operation that tries to fill coinview from passed in
// coin list.
for (const coin of this.coins) {
const {hash, index} = coin;
const key = Outpoint.toKey(hash, index);
Expand Down
20 changes: 10 additions & 10 deletions test/mtx-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ describe('MTX', function() {

await mtx.fund(coins1, {
changeAddress: wallet1.getChange(),
inputs: [{
preferredInputs: [{
hash: coin.hash,
index: coin.index
}]
Expand All @@ -229,7 +229,7 @@ describe('MTX', function() {

await mtx.fund(coins1, {
changeAddress: wallet1.getChange(),
inputs: [{
preferredInputs: [{
hash: coin.hash,
index: coin.index
}]
Expand Down Expand Up @@ -257,7 +257,7 @@ describe('MTX', function() {

await mtx.fund(coins1, {
changeAddress: wallet1.getChange(),
inputs: [{
preferredInputs: [{
hash: viewCoin.hash,
index: viewCoin.index
}, {
Expand Down Expand Up @@ -296,7 +296,7 @@ describe('MTX', function() {
try {
await mtx.fund(coins1, {
changeAddress: wallet1.getChange(),
inputs: [{
preferredInputs: [{
hash: coin.hash,
index: coin.index
}]
Expand Down Expand Up @@ -458,7 +458,7 @@ describe('MTX', function() {

await mtx.fund(coins1, {
changeAddress: wallet1.getChange(),
inputs: [{
preferredInputs: [{
hash: coin2.hash,
index: coin2.index
}]
Expand Down Expand Up @@ -501,7 +501,7 @@ describe('MTX', function() {

await mtx.fund(coins1, {
changeAddress: wallet1.getChange(),
inputs: [{
preferredInputs: [{
hash: coin2.hash,
index: coin2.index
}]
Expand Down Expand Up @@ -555,7 +555,7 @@ describe('MTX', function() {

await mtx.fund(coins1, {
changeAddress: wallet1.getChange(),
inputs: [{
preferredInputs: [{
hash: coin2.hash,
index: coin2.index
}, {
Expand Down Expand Up @@ -629,7 +629,7 @@ describe('MTX', function() {
try {
await mtx.fund(coins1, {
changeAddress: wallet1.getChange(),
inputs: [{
preferredInputs: [{
hash: coin2.hash,
index: coin2.index
}, {
Expand Down Expand Up @@ -676,7 +676,7 @@ describe('MTX', function() {
try {
await mtx.fund(coins1, {
changeAddress: wallet1.getChange(),
inputs: [{
preferredInputs: [{
hash: coin2.hash,
index: coin2.index
}, {
Expand Down Expand Up @@ -723,7 +723,7 @@ describe('MTX', function() {
try {
await mtx.fund(coins1, {
changeAddress: wallet1.getChange(),
inputs: [{
preferredInputs: [{
hash: coin2.hash,
index: coin2.index
}, {
Expand Down

0 comments on commit c9cc309

Please sign in to comment.