wallet: handle multiple conflicting inputs
This commit is contained in:
parent
af19d802f5
commit
eba7bbeddc
@ -886,6 +886,9 @@ class TXDB {
|
|||||||
async removeRecursive(wtx) {
|
async removeRecursive(wtx) {
|
||||||
const {tx, hash} = wtx;
|
const {tx, hash} = wtx;
|
||||||
|
|
||||||
|
if (!await this.hasTX(hash))
|
||||||
|
return null;
|
||||||
|
|
||||||
for (let i = 0; i < tx.outputs.length; i++) {
|
for (let i = 0; i < tx.outputs.length; i++) {
|
||||||
const spent = await this.getSpent(hash, i);
|
const spent = await this.getSpent(hash, i);
|
||||||
|
|
||||||
|
|||||||
@ -519,6 +519,36 @@ describe('Wallet', function() {
|
|||||||
assert.strictEqual((await wallet.getBalance()).unconfirmed, 0);
|
assert.strictEqual((await wallet.getBalance()).unconfirmed, 0);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('should handle double-spend (multiple inputs)', async () => {
|
||||||
|
const wallet = await wdb.create();
|
||||||
|
const address = await wallet.receiveAddress();
|
||||||
|
|
||||||
|
const hash = random.randomBytes(32);
|
||||||
|
const input0 = Input.fromOutpoint(new Outpoint(hash, 0));
|
||||||
|
const input1 = Input.fromOutpoint(new Outpoint(hash, 1));
|
||||||
|
|
||||||
|
const txa = new MTX();
|
||||||
|
txa.addInput(input0);
|
||||||
|
txa.addInput(input1);
|
||||||
|
txa.addOutput(address, 50000);
|
||||||
|
await wdb.addTX(txa.toTX());
|
||||||
|
assert.strictEqual((await wallet.getBalance()).unconfirmed, 50000);
|
||||||
|
|
||||||
|
let conflict = false;
|
||||||
|
wallet.on('conflict', () => {
|
||||||
|
conflict = true;
|
||||||
|
});
|
||||||
|
|
||||||
|
const txb = new MTX();
|
||||||
|
txb.addInput(input0);
|
||||||
|
txb.addInput(input1);
|
||||||
|
txb.addOutput(address, 49000);
|
||||||
|
await wdb.addTX(txb.toTX());
|
||||||
|
|
||||||
|
assert(conflict);
|
||||||
|
assert.strictEqual((await wallet.getBalance()).unconfirmed, 49000);
|
||||||
|
});
|
||||||
|
|
||||||
it('should handle more missed txs', async () => {
|
it('should handle more missed txs', async () => {
|
||||||
const alice = await wdb.create();
|
const alice = await wdb.create();
|
||||||
const bob = await wdb.create();
|
const bob = await wdb.create();
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user