Skip to content

Commit

Permalink
Merge PR #781 from 'pinheadmz/reveal-ignore-unknown'
Browse files Browse the repository at this point in the history
  • Loading branch information
pinheadmz committed Nov 29, 2022
2 parents 1326351 + d5de2c6 commit e07ba54
Show file tree
Hide file tree
Showing 4 changed files with 175 additions and 8 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ jobs:
node-version: 18.x

- name: Install dependencies
run: sudo apt-get install -y libunbound-dev |
run: sudo apt-get update && sudo apt-get install -y libunbound-dev |
npm install nyc coveralls

- name: Test
Expand Down Expand Up @@ -69,7 +69,7 @@ jobs:

- name: Install libunbound
if: contains(matrix.os, 'ubuntu')
run: sudo apt-get install -y libunbound-dev
run: sudo apt-get update && sudo apt-get install -y libunbound-dev

- name: Install dependencies
run: npm install
Expand Down
6 changes: 4 additions & 2 deletions lib/wallet/txdb.js
Original file line number Diff line number Diff line change
Expand Up @@ -1730,7 +1730,8 @@ class TXDB {
const prevout = tx.inputs[i].prevout;

const bb = await this.getBid(nameHash, prevout);
assert(bb);
if (!bb)
break;

if (height === -1) {
state.ulocked(path, -bb.lockup);
Expand Down Expand Up @@ -1812,7 +1813,8 @@ class TXDB {
const prevout = tx.inputs[i].prevout;

const bb = await this.getBid(nameHash, prevout);
assert(bb);
if (!bb)
break;

if (height === -1) {
state.ulocked(path, bb.lockup);
Expand Down
12 changes: 8 additions & 4 deletions lib/wallet/wallet.js
Original file line number Diff line number Diff line change
Expand Up @@ -1920,8 +1920,10 @@ class Wallet extends EventEmitter {
const blind = coin.covenant.getHash(3);
const bv = await this.getBlind(blind);

if (!bv)
throw new Error(`Blind value not found: ${name}.`);
if (!bv) {
this.logger.warning(`Blind value not found for name: ${name}.`);
continue;
}

const {value, nonce} = bv;

Expand Down Expand Up @@ -2048,8 +2050,10 @@ class Wallet extends EventEmitter {
const blind = coin.covenant.getHash(3);
const bv = await this.getBlind(blind);

if (!bv)
throw new Error(`Blind value not found for name: ${name}.`);
if (!bv) {
this.logger.warning(`Blind value not found for name: ${name}.`);
continue;
}

const {value, nonce} = bv;

Expand Down
161 changes: 161 additions & 0 deletions test/wallet-rescan-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -564,4 +564,165 @@ describe('Wallet rescan with namestate transitions', function() {
assert.strictEqual(ns.revoked, node.chain.height);
});
});

describe('Restore from seed', function() {
const node = new FullNode({
network: network.type,
memory: true,
plugins: [require('../lib/wallet/plugin')]
});

const {wdb} = node.require('walletdb');
let wallet1, wallet2, wallet3;
let addr1;
let heightBeforeReveal;

const name = rules.grindName(4, 4, network);

before(async () => {
await node.open();
});

after(async () => {
await node.close();
});

it('should create and fund wallet 1', async () => {
wallet1 = await wdb.create();
addr1 = (await wallet1.receiveAddress()).toString(network);
await node.rpc.generateToAddress([10, addr1]);
});

it('should open and bid from wallet 1', async () => {
await wallet1.sendOpen(name, false);
await node.rpc.generateToAddress([treeInterval + 1, addr1]);
await wallet1.sendBid(name, 1e6, 1e6);
await node.rpc.generateToAddress([1, addr1]);
});

it('should restore wallet 1 from seed into wallet 2', async () => {
const {mnemonic} = wallet1.master;
wallet2 = await wdb.create({mnemonic});

assert.strictEqual(
wallet1.master.key.xprivkey,
wallet2.master.key.xprivkey
);

// Sanity check
const bal1 = await wallet1.getBalance();
assert(bal1.unconfirmed > 0);
assert(bal1.tx > 0);
assert((await wallet1.getBids()).length);

// Wallet 2 has no history
const bal2 = await wallet2.getBalance();
assert.strictEqual(bal2.unconfirmed, 0);
assert.strictEqual(bal2.tx, 0);
assert(!(await wallet2.getBids()).length);
});

it('should rescan wallet 2', async () => {
await wdb.rescan(0);
const bal2 = await wallet2.getBalance();
assert(bal2.unconfirmed > 0);
assert(bal2.tx > 0);
assert((await wallet2.getBids()).length);
});

it('should bid from wallet 2', async () => {
await wallet2.sendBid(name, 2e6, 2e6);
await node.rpc.generateToAddress([1, addr1]);
});

it('should not have all blinds in either wallet', async () => {
const bids1 = await wallet1.getBids();
assert.strictEqual(bids1.length, 2);
for (const bid of bids1) {
if (bid.lockup === 1e6)
assert(bid.value === 1e6);
else
assert(bid.value === -1); // unknown
}

const bids2 = await wallet2.getBids();
assert.strictEqual(bids2.length, 2);
for (const bid of bids2) {
if (bid.lockup === 2e6)
assert(bid.value === 2e6);
else
assert(bid.value === -1); // unknown
}
});

it('should reveal from each wallet', async () => {
await node.rpc.generateToAddress([biddingPeriod, addr1]);

heightBeforeReveal = node.chain.height;

// Wallet 1 only knows blind for one of the bids
const tx1 = await wallet1.sendReveal(name);
assert.strictEqual(tx1.outputs.length, 2);
assert.strictEqual(tx1.outputs[0].value, 1e6);
assert.strictEqual(tx1.outputs[0].covenant.type, rules.types.REVEAL);
assert.strictEqual(tx1.outputs[1].covenant.type, rules.types.NONE);

// Confirm
await node.rpc.generateToAddress([1, addr1]);

// Wallet 1 knows there's another bid but can't reveal it.
assert.strictEqual(
(await wallet1.getBids()).length,
2
);
await assert.rejects(
wallet1.sendReveal(name),
{message: `No bids to reveal for name: ${name}.`}
);

// Wallet 2 can reveal the second bid
const tx2 = await wallet2.sendReveal(name);
assert.strictEqual(tx2.outputs.length, 2);
assert.strictEqual(tx2.outputs[0].value, 2e6);
assert.strictEqual(tx2.outputs[0].covenant.type, rules.types.REVEAL);

// Confirm
await node.rpc.generateToAddress([1, addr1]);
});

it('should have all reveals in both wallets', async () => {
const reveals1 = await wallet1.getReveals();
const reveals2 = await wallet2.getReveals();

assert.strictEqual(reveals1.length, 2);
assert.strictEqual(reveals2.length, 2);

for (const reveal of reveals1.concat(reveals2)) {
assert(reveal.own);
assert(reveal.value);
}
});

it('should restore wallet 1 from seed into wallet 3', async () => {
const {mnemonic} = wallet1.master;
wallet3 = await wdb.create({mnemonic});
});

it('should just rescan reveal phase', async () => {
await wdb.rescan(heightBeforeReveal);

let bal1 = await wallet1.getBalance();
let bal3 = await wallet3.getBalance();

assert.notDeepStrictEqual(bal1, bal3);

// Complete rescan cleans everything up
await wdb.rescan(0);

bal1 = await wallet1.getBalance();
bal3 = await wallet3.getBalance();

assert.deepStrictEqual(bal1, bal3);
});
});
});

0 comments on commit e07ba54

Please sign in to comment.