From e10a23fb3e93a6d08c9cf2e2288e9263fc431524 Mon Sep 17 00:00:00 2001 From: Daniel Cousens Date: Thu, 13 Oct 2016 17:01:08 +1100 Subject: [PATCH] integration: avoid multiple faucet calls --- test/integration/_blockchain.js | 30 +++++++++++++++++------------- test/integration/basic.js | 31 ++++++++++++++++++------------- 2 files changed, 35 insertions(+), 26 deletions(-) diff --git a/test/integration/_blockchain.js b/test/integration/_blockchain.js index 5d0f91b..9460709 100644 --- a/test/integration/_blockchain.js +++ b/test/integration/_blockchain.js @@ -12,12 +12,8 @@ var kpNetwork = bitcoin.networks.testnet var keyPair = bitcoin.ECPair.fromWIF(process.env.BITCOINJS_TESTNET_WIF, kpNetwork) var kpAddress = keyPair.getAddress() -function fundAddress (unspents, address, amount, callback) { - var result = coinSelect(unspents, [{ - address: address, - value: amount - }], 10) - +function fundAddress (unspents, outputs, callback) { + var result = coinSelect(unspents, outputs, 10) if (!result.inputs) return callback(new Error('Faucet empty')) var txb = new bitcoin.TransactionBuilder(kpNetwork) @@ -34,24 +30,32 @@ function fundAddress (unspents, address, amount, callback) { }) var tx = txb.build() + var txId = tx.getId() + testnet.transactions.propagate(tx.toHex(), function (err) { - callback(err, { - txId: tx.getId(), - vout: 0 - }, 0) + callback(err, outputs.map(function (x, i) { + return { txId: txId, vout: i } + })) }) } -testnet.faucet = function faucet (address, amount, done) { +testnet.faucetMany = function faucetMany (outputs, callback) { testnet.addresses.unspents(kpAddress, function (err, unspents) { - if (err) return done(err) + if (err) return callback(err) + typeforce([{ txId: types.Hex, vout: types.UInt32, value: types.Satoshi }], unspents) - fundAddress(unspents, address, amount, done) + fundAddress(unspents, outputs, callback) + }) +} + +testnet.faucet = function faucet (address, value, callback) { + testnet.faucetMany([{ address: address, value: value }], function (err, unspents) { + callback(err, unspents && unspents[0]) }) } diff --git a/test/integration/basic.js b/test/integration/basic.js index b8b2561..5e48757 100644 --- a/test/integration/basic.js +++ b/test/integration/basic.js @@ -68,22 +68,27 @@ describe('bitcoinjs-lib (basic)', function () { var alicesAddress = alice.getAddress() var bobsAddress = bob.getAddress() - blockchain.t.faucet(alicesAddress, 2e4, function (err, unspentA) { + blockchain.t.faucetMany([ + { + address: alicesAddress, + value: 2e4 + }, + { + address: bobsAddress, + value: 2e4 + } + ], function (err, unspents) { if (err) return done(err) - blockchain.t.faucet(bobsAddress, 2e4, function (err, unspentB) { - if (err) return done(err) + var tx = new bitcoin.TransactionBuilder(network) + tx.addInput(unspents[0].txId, unspents[0].vout) + tx.addInput(unspents[1].txId, unspents[1].vout) + tx.addOutput('n2eMqTT929pb1RDNuqEnxdaLau1rxy3efi', 1e4) + tx.addOutput('mvGVHWi6gbkBZZPaqBVRcxvKVPYd9r3fp7', 1e4) + tx.sign(0, alice) + tx.sign(1, bob) - var tx = new bitcoin.TransactionBuilder(network) - tx.addInput(unspentA.txId, unspentA.vout) - tx.addInput(unspentB.txId, unspentB.vout) - tx.addOutput('n2eMqTT929pb1RDNuqEnxdaLau1rxy3efi', 1e4) - tx.addOutput('mvGVHWi6gbkBZZPaqBVRcxvKVPYd9r3fp7', 1e4) - tx.sign(0, alice) - tx.sign(1, bob) - - blockchain.t.transactions.propagate(tx.build().toHex(), done) - }) + blockchain.t.transactions.propagate(tx.build().toHex(), done) }) }) })