Skip to content

Commit

Permalink
add feedback from live testing
Browse files Browse the repository at this point in the history
  • Loading branch information
piotr-roslaniec committed Jul 12, 2023
1 parent 7f54da4 commit 2d1aa90
Show file tree
Hide file tree
Showing 13 changed files with 57 additions and 23 deletions.
2 changes: 1 addition & 1 deletion src/agents/coordinator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ export class DkgCoordinatorAgent {
if (!ritualStartEvent) {
throw new Error('Ritual start event not found');
}
return ritualStartEvent.args?.ritualId.toNumber();
return ritualStartEvent.args?.ritualId;
}

public static async getRitual(
Expand Down
6 changes: 3 additions & 3 deletions src/dkg.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,10 +115,10 @@ export class DkgClient {
web3Provider: ethers.providers.Web3Provider,
ursulas: ChecksumAddress[],
waitUntilEnd = false
): Promise<DkgRitual> {
): Promise<number | undefined> {
const ritualId = await DkgCoordinatorAgent.initializeRitual(
web3Provider,
ursulas
ursulas.sort()
);

if (waitUntilEnd) {
Expand All @@ -137,7 +137,7 @@ export class DkgClient {
}
}

return this.getExistingRitual(web3Provider, ritualId);
return ritualId;
}

private static waitUntilRitualEnd = async (
Expand Down
5 changes: 5 additions & 0 deletions src/policies/policy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,11 @@ export class BlockchainPolicy {
public async generatePreEnactedPolicy(
ursulas: readonly Ursula[]
): Promise<PreEnactedPolicy> {
if (ursulas.length != this.verifiedKFrags.length) {
throw new Error(
`Number of ursulas must match number of verified kFrags: ${this.verifiedKFrags.length}`
);
}
const treasureMap = this.makeTreasureMap(ursulas, this.verifiedKFrags);
const encryptedTreasureMap = this.encryptTreasureMap(treasureMap);
// const revocationKit = new RevocationKit(treasureMap, this.publisher.signer);
Expand Down
9 changes: 9 additions & 0 deletions src/sdk/cohort.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,15 @@ export class Cohort {
include: string[] = [],
exclude: string[] = []
) {
if (configuration.threshold > configuration.shares) {
throw new Error('Threshold cannot be greater than the number of shares');
}
// TODO: Remove this limitation after `nucypher-core@0.11.0` deployment
const isMultipleOf2 = (n: number) => n % 2 === 0;
if (!isMultipleOf2(configuration.shares)) {
throw new Error('Number of shares must be a multiple of 2');
}

const porter = new Porter(configuration.porterUri);
const ursulas = await porter.getUrsulas(
configuration.shares,
Expand Down
19 changes: 14 additions & 5 deletions src/sdk/strategy/cbd-strategy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,21 @@ export class CbdStrategy {
}

public async deploy(
web3Provider: ethers.providers.Web3Provider
web3Provider: ethers.providers.Web3Provider,
ritualId?: number
): Promise<DeployedCbdStrategy> {
const dkgRitual = await DkgClient.initializeRitual(
web3Provider,
this.cohort.ursulaAddresses
);
if (ritualId === undefined) {
ritualId = await DkgClient.initializeRitual(
web3Provider,
this.cohort.ursulaAddresses,
true
);
}
if (ritualId === undefined) {
// Given that we just initialized the ritual, this should never happen
throw new Error('Ritual ID is undefined');
}
const dkgRitual = await DkgClient.getExistingRitual(web3Provider, ritualId);
return DeployedCbdStrategy.create(this.cohort, dkgRitual);
}

Expand Down
2 changes: 1 addition & 1 deletion test/acceptance/alice-grants.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ describe('story: alice shares message with bob through policy', () => {
const shares = 3;
const startDate = new Date();
const endDate = new Date(Date.now() + 60 * 1000);
const mockedUrsulas = fakeUrsulas().slice(0, shares);
const mockedUrsulas = fakeUrsulas(shares);

// Intermediate variables used for mocking
let encryptedTreasureMap: EncryptedTreasureMap;
Expand Down
2 changes: 1 addition & 1 deletion test/acceptance/delay-enact.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ describe('story: alice1 creates a policy but alice2 enacts it', () => {
const shares = 3;
const startDate = new Date();
const endDate = new Date(Date.now() + 60 * 1000); // 60s later
const mockedUrsulas = fakeUrsulas().slice(0, shares);
const mockedUrsulas = fakeUrsulas(shares);
const label = 'fake-data-label';

it('alice generates a new policy', async () => {
Expand Down
4 changes: 2 additions & 2 deletions test/docs/cbd.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,8 @@ describe('Get Started (CBD PoC)', () => {

// 2. Build a Cohort
const config = {
threshold: 3,
shares: 5,
threshold: 2,
shares: 4,
porterUri: 'https://porter-tapir.nucypher.community',
};
const newCohort = await Cohort.create(config);
Expand Down
2 changes: 1 addition & 1 deletion test/integration/pre.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ describe('proxy reencryption', () => {
const plaintext = toBytes('plaintext-message');
const threshold = 2;
const shares = 3;
const ursulas = fakeUrsulas().slice(0, shares);
const ursulas = fakeUrsulas(shares);
const label = 'fake-data-label';
const alice = fakeAlice();
const bob = fakeBob();
Expand Down
8 changes: 6 additions & 2 deletions test/unit/cbd-strategy.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
fakeWeb3Provider,
makeCohort,
mockCbdDecrypt,
mockGetExistingRitual,
mockGetParticipants,
mockGetRitualState,
mockGetUrsulas,
Expand All @@ -38,8 +39,9 @@ const ownsNFT = new ERC721Ownership({
chain: 5,
});
const conditionExpr = new ConditionExpression(ownsNFT);
const ursulas = fakeUrsulas().slice(0, 3);
const ursulas = fakeUrsulas();
const variant = FerveoVariant.Precomputed;
const ritualId = 0;

const makeCbdStrategy = async () => {
const cohort = await makeCohort(ursulas);
Expand All @@ -55,11 +57,13 @@ async function makeDeployedCbdStrategy() {
const mockedDkgRitual = fakeDkgRitual(mockedDkg);
const web3Provider = fakeWeb3Provider(aliceSecretKey.toBEBytes());
const getUrsulasSpy = mockGetUrsulas(ursulas);
const initializeRitualSpy = mockInitializeRitual(mockedDkgRitual);
const initializeRitualSpy = mockInitializeRitual(ritualId);
const getExistingRitualSpy = mockGetExistingRitual(mockedDkgRitual);
const deployedStrategy = await strategy.deploy(web3Provider);

expect(getUrsulasSpy).toHaveBeenCalled();
expect(initializeRitualSpy).toHaveBeenCalled();
expect(getExistingRitualSpy).toHaveBeenCalled();

return { mockedDkg, deployedStrategy };
}
Expand Down
2 changes: 1 addition & 1 deletion test/unit/cohort.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Cohort } from '../../src';
import { fakeUrsulas, makeCohort } from '../utils';

describe('Cohort', () => {
const mockedUrsulas = fakeUrsulas().slice(0, 3);
const mockedUrsulas = fakeUrsulas();

it('creates a Cohort', async () => {
const cohort = await makeCohort(mockedUrsulas);
Expand Down
2 changes: 1 addition & 1 deletion test/unit/pre-strategy.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ const ownsNFT = new ERC721Ownership({
chain: 5,
});
const conditionExpr = new ConditionExpression(ownsNFT);
const mockedUrsulas = fakeUrsulas().slice(0, 3);
const mockedUrsulas = fakeUrsulas();

const makePreStrategy = async () => {
const cohort = await makeCohort(mockedUrsulas);
Expand Down
17 changes: 12 additions & 5 deletions test/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,11 +114,12 @@ const genChecksumAddress = (i: number) =>
'0x' + '0'.repeat(40 - i.toString(16).length) + i.toString(16);
const genEthAddr = (i: number) =>
EthereumAddress.fromString(genChecksumAddress(i));
export const fakeUrsulas = (): readonly Ursula[] =>
[0, 1, 2, 3, 4].map((i: number) => ({
export const fakeUrsulas = (n = 4): Ursula[] =>
// 0...n-1
Array.from(Array(n).keys()).map((i: number) => ({
encryptingKey: SecretKey.random().publicKey(),
checksumAddress: genChecksumAddress(i).toLowerCase(),
uri: 'https://example.a.com:9151',
uri: `https://example.${i}.com:9151`,
}));

export const mockGetUrsulas = (ursulas: readonly Ursula[]) => {
Expand Down Expand Up @@ -516,8 +517,14 @@ export const fakeDkgRitual = (ritual: {
);
};

export const mockInitializeRitual = (dkgRitual: DkgRitual) => {
export const mockInitializeRitual = (ritualId: number) => {
return jest.spyOn(DkgClient, 'initializeRitual').mockImplementation(() => {
return Promise.resolve(ritualId);
});
};

export const mockGetExistingRitual = (dkgRitual: DkgRitual) => {
return jest.spyOn(DkgClient, 'getExistingRitual').mockImplementation(() => {
return Promise.resolve(dkgRitual);
});
};
Expand All @@ -526,7 +533,7 @@ export const makeCohort = async (ursulas: Ursula[]) => {
const getUrsulasSpy = mockGetUrsulas(ursulas);
const config = {
threshold: 2,
shares: 3,
shares: ursulas.length,
porterUri: 'https://_this.should.crash',
};
const cohort = await Cohort.create(config);
Expand Down

1 comment on commit 2d1aa90

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bundled size for the package is listed below:

build/module/types/ethers-contracts/factories: 82.03 KB
build/module/types/ethers-contracts: 152.34 KB
build/module/types: 156.25 KB
build/module/src/policies: 19.53 KB
build/module/src/characters: 93.75 KB
build/module/src/agents: 39.06 KB
build/module/src/sdk/strategy: 31.25 KB
build/module/src/sdk: 46.88 KB
build/module/src/conditions/predefined: 19.53 KB
build/module/src/conditions/context: 42.97 KB
build/module/src/conditions/base: 54.69 KB
build/module/src/conditions: 156.25 KB
build/module/src/kits: 19.53 KB
build/module/src: 449.22 KB
build/module/test: 46.88 KB
build/module: 707.03 KB
build/main/types/ethers-contracts/factories: 82.03 KB
build/main/types/ethers-contracts: 152.34 KB
build/main/types: 156.25 KB
build/main/src/policies: 19.53 KB
build/main/src/characters: 93.75 KB
build/main/src/agents: 39.06 KB
build/main/src/sdk/strategy: 31.25 KB
build/main/src/sdk: 46.88 KB
build/main/src/conditions/predefined: 19.53 KB
build/main/src/conditions/context: 42.97 KB
build/main/src/conditions/base: 54.69 KB
build/main/src/conditions: 156.25 KB
build/main/src/kits: 19.53 KB
build/main/src: 453.13 KB
build/main/test: 46.88 KB
build/main: 710.94 KB
build: 1.39 MB

Please sign in to comment.