Skip to content

Commit

Permalink
Bump dwn-sdk-js version. (#113)
Browse files Browse the repository at this point in the history
- bump `dwn-sdk-js` to `0.2.17` which include the new `@web5/dids`
package.
- Injecting the `DidResolver` from `@web5/dids` without caching in tests
is to prevent LevelDB from locking.
  • Loading branch information
LiranCohen authored Feb 16, 2024
1 parent d984edb commit f688d32
Show file tree
Hide file tree
Showing 6 changed files with 202 additions and 71 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,5 @@ node_modules
tmp
dwn.db
coverage

RESOLVERCACHE
142 changes: 128 additions & 14 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
"url": "https://github.com/TBD54566975/dwn-server/issues"
},
"dependencies": {
"@tbd54566975/dwn-sdk-js": "0.2.16",
"@tbd54566975/dwn-sdk-js": "0.2.17",
"@tbd54566975/dwn-sql-store": "0.2.9",
"better-sqlite3": "^8.5.0",
"body-parser": "^1.20.2",
Expand Down Expand Up @@ -58,6 +58,7 @@
"@types/ws": "8.5.4",
"@typescript-eslint/eslint-plugin": "5.59.0",
"@typescript-eslint/parser": "5.59.0",
"@web5/dids": "0.4.0",
"c8": "8.0.1",
"chai": "4.3.6",
"chai-as-promised": "7.1.1",
Expand Down
26 changes: 4 additions & 22 deletions tests/dwn-server.spec.ts
Original file line number Diff line number Diff line change
@@ -1,36 +1,18 @@
import { expect } from 'chai';

import type { DwnServerConfig } from '../src/config.js';
import { config } from '../src/config.js';
import { DwnServer } from '../src/dwn-server.js';
import { randomBytes } from 'crypto';
import { getTestDwn } from './test-dwn.js';

describe('DwnServer', function () {
const dwnServerConfig = { ...config };

before(async function () {
// NOTE: using SQL to workaround an issue where multiple instances of DwnServer can be started using LevelDB in the same test run,
// and dwn-server.spec.ts already uses LevelDB.
dwnServerConfig.messageStore = 'sqlite://';
dwnServerConfig.dataStore = 'sqlite://';
dwnServerConfig.eventLog = 'sqlite://';
});

after(async function () {
});

it('should initialize ProofOfWorkManager with challenge nonce seed if given.', async function () {
const registrationProofOfWorkSeed = randomBytes(32).toString('hex');
const configWithProofOfWorkSeed: DwnServerConfig = {
...dwnServerConfig,
registrationStoreUrl: 'sqlite://',
registrationProofOfWorkEnabled: true,
registrationProofOfWorkSeed
};
it('starts with injected dwn', async function () {
const testDwn = await getTestDwn();

const dwnServer = new DwnServer({ config: configWithProofOfWorkSeed });
const dwnServer = new DwnServer({ config: dwnServerConfig, dwn: testDwn });
await dwnServer.start();
expect(dwnServer.registrationManager['proofOfWorkManager']['challengeSeed']).to.equal(registrationProofOfWorkSeed);

dwnServer.stop(() => console.log('server Stop'));
expect(dwnServer.httpServer.listening).to.be.false;
Expand Down
90 changes: 57 additions & 33 deletions tests/scenarios/registration.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { webcrypto } from 'node:crypto';
import { useFakeTimers } from 'sinon';
import { v4 as uuidv4 } from 'uuid';

import type { DwnServerConfig } from '../../src/config.js';
import { config } from '../../src/config.js';
import type {
JsonRpcRequest,
Expand All @@ -27,6 +28,7 @@ import type { RegistrationManager } from '../../src/registration/registration-ma
import { DwnServerErrorCode } from '../../src/dwn-error.js';
import { ProofOfWorkManager } from '../../src/registration/proof-of-work-manager.js';
import { DwnServer } from '../../src/dwn-server.js';
import { randomBytes } from 'crypto';

if (!globalThis.crypto) {
// @ts-ignore
Expand Down Expand Up @@ -67,45 +69,17 @@ describe('Registration scenarios', function () {
registrationManager = dwnServer.registrationManager;
});

beforeEach(async function () {
});

afterEach(async function () {
});

after(function () {
dwnServer.stop(() => { });
clock.restore();
});

it('should allow tenant registration to be turned off to allow all DWN messages through.', async () => {
// Scenario:
// 1. There is a DWN that does not require tenant registration.
// 2. Alice can write to the DWN without registering as a tenant.

const configClone = {
...dwnServerConfig,
registrationStoreUrl: '', // set to empty to disable tenant registration
port: 3001,
registrationProofOfWorkEnabled: false,
termsOfServiceFilePath: undefined,
};
const dwnServer = new DwnServer({ config: configClone });
await dwnServer.start();

const { jsonRpcRequest, dataBytes } = await generateRecordsWriteJsonRpcRequest(alice);
const writeResponse = await fetch('http://localhost:3001', {
method: 'POST',
headers: {
'dwn-request': JSON.stringify(jsonRpcRequest),
},
body: new Blob([dataBytes]),
});
const writeResponseBody = await writeResponse.json() as JsonRpcResponse;
expect(writeResponse.status).to.equal(200);
expect(writeResponseBody.result.reply.status.code).to.equal(202);
beforeEach(function () {
dwnServer.start();
});

dwnServer.stop(() => { });
afterEach(function () {
dwnServer.stop(() => {});
});

it('should facilitate tenant registration with terms-of-service and proof-or-work turned on', async () => {
Expand Down Expand Up @@ -557,6 +531,56 @@ describe('Registration scenarios', function () {
expect(write3ResponseBody.result.reply.status.code).to.equal(202);

});

/**
* NOTE: The tests below instantiate their own server configs and should should take care to stop the `dwnServer`
* This is done to avoid LevelDB locking for the default `DidResolver` cache.
*/

it('should initialize ProofOfWorkManager with challenge nonce seed if given.', async function () {
dwnServer.stop(() => {});

const registrationProofOfWorkSeed = randomBytes(32).toString('hex');
const configWithProofOfWorkSeed: DwnServerConfig = {
...dwnServerConfig,
registrationStoreUrl: 'sqlite://',
registrationProofOfWorkEnabled: true,
registrationProofOfWorkSeed,
};

dwnServer = new DwnServer({ config: configWithProofOfWorkSeed });
await dwnServer.start();
expect(dwnServer.registrationManager['proofOfWorkManager']['challengeSeed']).to.equal(registrationProofOfWorkSeed);
});

it('should allow tenant registration to be turned off to allow all DWN messages through.', async () => {
dwnServer.stop(() => {});

// Scenario:
// 1. There is a DWN that does not require tenant registration.
// 2. Alice can write to the DWN without registering as a tenant.
const configClone = {
...dwnServerConfig,
registrationStoreUrl: '', // set to empty to disable tenant registration
port: 3002,
registrationProofOfWorkEnabled: false,
termsOfServiceFilePath: undefined,
};
dwnServer = new DwnServer({ config: configClone });
await dwnServer.start();

const { jsonRpcRequest, dataBytes } = await generateRecordsWriteJsonRpcRequest(alice);
const writeResponse = await fetch(dwnMessageEndpoint, {
method: 'POST',
headers: {
'dwn-request': JSON.stringify(jsonRpcRequest),
},
body: new Blob([dataBytes]),
});
const writeResponseBody = await writeResponse.json() as JsonRpcResponse;
expect(writeResponse.status).to.equal(200);
expect(writeResponseBody.result.reply.status.code).to.equal(202);
});
});

async function generateRecordsWriteJsonRpcRequest(persona: Persona): Promise<{ jsonRpcRequest: JsonRpcRequest, dataBytes: Uint8Array }> {
Expand Down
Loading

0 comments on commit f688d32

Please sign in to comment.