Skip to content

Commit

Permalink
dns: send REFUSED until chain sync
Browse files Browse the repository at this point in the history
  • Loading branch information
pinheadmz committed Sep 14, 2022
1 parent ce05f3c commit c7ffc46
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 11 deletions.
23 changes: 15 additions & 8 deletions lib/dns/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,9 @@ class RootServer extends DNSServer {
if (!this.lookup)
throw new Error('Tree not available.');

if (!this.chain.safeEntry)
throw new Error('Chain is not safe for name resolution.');

const {treeRoot} = this.chain.safeEntry;
const hash = rules.hashName(name);
const data = await this.lookup(hash, treeRoot);
Expand Down Expand Up @@ -385,11 +388,8 @@ class RootServer extends DNSServer {
// useless proofs for invalid TLDs
// (These requests are most
// likely bad anyways)
if (!rules.verifyName(tld)) {
const res = new Message();
res.code = codes.REFUSED;
return res;
}
if (!rules.verifyName(tld))
throw new Error('Invalid name.');

// Ask the urkel tree for the name data.
const data = !this.blacklist.has(tld)
Expand Down Expand Up @@ -535,10 +535,17 @@ class RootServer extends DNSServer {
if (cache)
return cache;

const res = await this.response(req, rinfo);
let res;
try {
res = await this.response(req, rinfo);

if (!util.equal(tld, '_synth.'))
this.cache.set(name, type, res);
if (!util.equal(tld, '_synth.'))
this.cache.set(name, type, res);
} catch (e) {
this.logger.error(e);
res = new Message();
res.code = codes.REFUSED;
}

return res;
}
Expand Down
40 changes: 37 additions & 3 deletions test/dns-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,10 @@ describe('DNS Servers', function() {
const resource = Resource.fromJSON({
records: [{type: 'TXT', txt: [string]}]
});
const maxTipAge = network.block.maxTipAge;

before(async () => {
network.block.maxTipAge = 12 * 60 * 60;
await miner.open();
await miner.connect();
await node.open();
Expand All @@ -132,19 +134,51 @@ describe('DNS Servers', function() {

await forValue(miner.pool.peers.list, 'size', 1);
await forValue(node.pool.peers.list, 'size', 1);

name = await miner.rpc.grindName([4]);

miner.miner.addresses.length = 0;
miner.miner.addAddress(wallet.getReceive());
});

after(async () => {
await node.close();
await miner.close();
network.block.maxTipAge = maxTipAge;
});

it('should refuse to resolve before chain sync', async () => {
await assert.rejects(
rootResolver.resolveTxt(name),
{message: `queryTxt EREFUSED ${name}`}
);
});

it('should not refuse to resolve after chain sync', async () => {
await mineBlocks(2);
await assert.rejects(
rootResolver.resolveTxt(name),
{message: `queryTxt ENOTFOUND ${name}`}
);
});

it('should refuse to resolve invalid name', async () => {
await assert.rejects(
rootResolver.resolveTxt('com\\\\000'),
{message: 'queryTxt EREFUSED com\\\\000'}
);
});

it('should not refuse to resolve valid name', async () => {
await assert.rejects(
rootResolver.resolveTxt('com\\000'),
{message: 'queryTxt ENOTFOUND com\\000'}
);
});

it('should fund wallet and win name', async () => {
miner.miner.addresses.length = 0;
miner.miner.addAddress(wallet.getReceive());
await mineBlocks(20);

name = await miner.rpc.grindName([4]);
await miner.mempool.addTX((await wallet.sendOpen(name)).toTX());
await mineBlocks(treeInterval + 1);
await miner.mempool.addTX((await wallet.sendBid(name, 10000, 10000)).toTX());
Expand Down

0 comments on commit c7ffc46

Please sign in to comment.