Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: add in memory dns cache. #879

Merged
merged 2 commits into from
Dec 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions test/auction-reorg-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ const MemWallet = require('./util/memwallet');
const Network = require('../lib/protocol/network');
const rules = require('../lib/covenants/rules');
const {ownership} = require('../lib/covenants/ownership');
const {CachedStubResolver} = require('./util/stub');

const network = Network.get('regtest');
const {treeInterval} = network.names;
Expand Down Expand Up @@ -73,6 +74,16 @@ function createNode() {
describe('Auction Reorg', function() {
this.timeout(20000);

const originalResolver = ownership.Resolver;

before(() => {
ownership.Resolver = CachedStubResolver;
});

after(() => {
ownership.Resolver = originalResolver;
});

describe('Vickrey Auction Reorg', function() {
const node = createNode();
const orig = createNode();
Expand Down
11 changes: 11 additions & 0 deletions test/auction-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const MemWallet = require('./util/memwallet');
const Network = require('../lib/protocol/network');
const rules = require('../lib/covenants/rules');
const {ownership} = require('../lib/covenants/ownership');
const {CachedStubResolver} = require('./util/stub');

const network = Network.get('regtest');
const GNAME_SIZE = 10;
Expand Down Expand Up @@ -69,6 +70,16 @@ function createNode() {
describe('Auction', function() {
this.timeout(15000);

const originalResolver = ownership.Resolver;

before(() => {
ownership.Resolver = CachedStubResolver;
});

after(() => {
ownership.Resolver = originalResolver;
});

describe('Vickrey Auction', function() {
const node = createNode();
const orig = createNode();
Expand Down
5 changes: 5 additions & 0 deletions test/chain-checkpoints-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ const Network = require('../lib/protocol/network');
const rules = require('../lib/covenants/rules');
const {Resource} = require('../lib/dns/resource');
const AirdropProof = require('../lib/primitives/airdropproof');
const {CachedStubResolver} = require('./util/stub');

const network = Network.get('regtest');

Expand Down Expand Up @@ -91,8 +92,11 @@ async function mineBlocks(n, label) {
}

describe('Checkpoints', function() {
const originalResolver = ownership.Resolver;

before(async () => {
ownership.ignore = true;
ownership.Resolver = CachedStubResolver;

await blocks.open();
await chainGenerator.open();
Expand All @@ -105,6 +109,7 @@ describe('Checkpoints', function() {
await blocks.close();

ownership.ignore = false;
ownership.Resolver = originalResolver;
});

it('should add addrs to miner', async () => {
Expand Down
11 changes: 11 additions & 0 deletions test/chain-icann-lockup-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const chainCommon = require('../lib/blockchain/common');
const {BufferMap} = require('buffer-map');
const {thresholdStates} = chainCommon;
const {isReserved, isLockedUp, hashName} = require('../lib/covenants/rules');
const {CachedStubResolver} = require('./util/stub');

const SOFT_FORK_NAME = 'icannlockup';

Expand Down Expand Up @@ -52,6 +53,16 @@ const ACTUAL_RENEWAL_WINDOW = network.names.renewalWindow;
describe('BIP9 - ICANN lockup (integration)', function() {
this.timeout(20000);

const originalResolver = ownership.Resolver;

before(() => {
ownership.Resolver = CachedStubResolver;
});

after(() => {
ownership.Resolver = originalResolver;
});

const CUSTOM = [
'cloudflare',
'nlnetlabs',
Expand Down
5 changes: 5 additions & 0 deletions test/claim-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const consensus = require('../lib/protocol/consensus');
const {ownership} = require('../lib/covenants/ownership');
const reserved = require('../lib/covenants/reserved');
const {Resource} = require('../lib/dns/resource');
const {CachedStubResolver} = require('./util/stub');

const network = Network.get('regtest');

Expand Down Expand Up @@ -47,15 +48,19 @@ async function mineBlocks(n, addr) {
describe('Reserved Name Claims', function() {
this.timeout(10000);

const originalResolver = ownership.Resolver;

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

wallet = await wdb.create();
addr = await wallet.receiveAddress();
ownership.Resolver = CachedStubResolver;
});

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

// Reset the ownership flag after every test,
Expand Down
11 changes: 11 additions & 0 deletions test/mempool-invalidation-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const rules = require('../lib/covenants/rules');
const {states} = require('../lib/covenants/namestate');
const {Resource} = require('../lib/dns/resource');
const {forEvent} = require('./util/common');
const {CachedStubResolver} = require('./util/stub');

const network = Network.get('regtest');
const {
Expand All @@ -21,6 +22,16 @@ const ACTUAL_CLAIM_PERIOD = claimPeriod;
const ACTUAL_RENEWAL_WINDOW = renewalWindow;

describe('Mempool Invalidation', function() {
const originalResolver = ownership.Resolver;

before(() => {
ownership.Resolver = CachedStubResolver;
});

after(() => {
ownership.Resolver = originalResolver;
});

const NAMES = [
// roots
'nl',
Expand Down
11 changes: 11 additions & 0 deletions test/mempool-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ const rules = require('../lib/covenants/rules');
const NameState = require('../lib/covenants/namestate');
const {states} = NameState;
const {ownership} = require('../lib/covenants/ownership');
const {CachedStubResolver} = require('./util/stub');

const ONE_HASH = Buffer.alloc(32, 0x00);
ONE_HASH[0] = 0x01;
Expand Down Expand Up @@ -75,6 +76,16 @@ async function getMockBlock(chain, txs = [], cb = true) {
describe('Mempool', function() {
this.timeout(5000);

const originalResolver = ownership.Resolver;

before(() => {
ownership.Resolver = CachedStubResolver;
});

after(() => {
ownership.Resolver = originalResolver;
});

describe('Mempool TXs', function() {
let workers, blocks, chain, mempool, wallet;
let cachedTX;
Expand Down
5 changes: 5 additions & 0 deletions test/txstart-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ const Address = require('../lib/primitives/address');
const Script = require('../lib/script/script');
const common = require('../lib/blockchain/common');
const {ownership} = require('../lib/covenants/ownership');
const {CachedStubResolver} = require('./util/stub');
const VERIFY_NONE = common.flags.VERIFY_NONE;

const node = new FullNode({
Expand Down Expand Up @@ -49,18 +50,22 @@ describe('Disable TXs', function() {

let utxo, lastTX;

const originalResolver = ownership.Resolver;

before(async () => {
node.network.txStart = 5;
await node.open();

// Start with one block for the fakeClaim
const block = await node.miner.mineBlock();
assert(await node.chain.add(block));
ownership.Resolver = CachedStubResolver;
});

after(async () => {
await node.close();
node.network.txStart = RESET_TXSTART;
ownership.Resolver = originalResolver;
});

it('should reject tx from mempool before txStart', async () => {
Expand Down
89 changes: 89 additions & 0 deletions test/util/stub.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
'use strict';

const assert = require('bsert');
const path = require('path');
const {StubResolver, wire} = require('bns');
const fs = require('bfile');
const {tmpdir} = require('os');

let CACHE = {};

/**
* Proxy requests if they are not cached.
*/

class CachedStubResolver extends StubResolver {
constructor(options) {
super(options);

this.enabled = true;
this.cacheOnDisk = process.env['HSD_TEST_DNS_FILE_CACHE'] === 'true';
this.cacheDir = path.join(tmpdir(), 'hsd-test');
this.cacheFile = path.join(this.cacheDir, 'dns-cache.json');

this.loadCacheSync();
}

loadCacheSync() {
if (!this.cacheOnDisk)
return;

if (!fs.existsSync(this.cacheDir))
fs.mkdirSync(this.cacheDir);

if (fs.existsSync(this.cacheFile))
CACHE = JSON.parse(fs.readFileSync(this.cacheFile, 'utf8'));
}

saveCacheSync() {
if (!this.cacheOnDisk)
return;

const stringified = JSON.stringify(CACHE, null, 2);
fs.writeFileSync(this.cacheFile, stringified, 'utf8');
}

setCache(qs, res) {
if (!this.enabled)
return;

assert(qs instanceof wire.Question);
assert(res instanceof wire.Message);

CACHE[qs.toString()] = res.toString();
this.saveCacheSync();
}

hasCache(qs) {
if (!this.enabled)
return false;

assert(qs instanceof wire.Question);

return Boolean(CACHE[qs.toString()]);
}

getCache(qs) {
if (!this.enabled)
return null;

assert(qs instanceof wire.Question);

return wire.Message.fromString(CACHE[qs.toString()]);
}

async resolve(qs) {
if (this.hasCache(qs))
return this.getCache(qs);

const resolved = await super.resolve(qs);

if (!resolved)
return null;

this.setCache(qs, resolved);
return resolved;
}
}

exports.CachedStubResolver = CachedStubResolver;