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

refactor(request-logic): event emitter #1133

Merged
merged 8 commits into from
Aug 3, 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
215 changes: 42 additions & 173 deletions packages/request-logic/src/request-logic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,30 +53,7 @@ export default class RequestLogic implements RequestLogicTypes.IRequestLogic {
// Validate the action, the apply will throw in case of error
RequestLogicCore.applyActionToRequest(null, action, Date.now(), this.advancedLogic);

const resultPersistTx = await this.transactionManager.persistTransaction(
JSON.stringify(action),
requestId,
hashedTopics,
);

const result = Object.assign(new EventEmitter(), {
meta: { transactionManagerMeta: resultPersistTx.meta },
result: { requestId },
});

// When receive the confirmation from transaction manager propagate it
resultPersistTx
.on('confirmed', (resultPersistTxConfirmed: TransactionTypes.IReturnPersistTransaction) => {
result.emit('confirmed', {
meta: { transactionManagerMeta: resultPersistTxConfirmed.meta },
result: { requestId },
});
})
.on('error', (error) => {
result.emit('error', error);
});

return result;
return this.persistTransaction(requestId, action, { requestId }, hashedTopics);
}

/**
Expand Down Expand Up @@ -110,31 +87,13 @@ export default class RequestLogic implements RequestLogicTypes.IRequestLogic {
// Validate the action, the apply will throw in case of error
RequestLogicCore.applyActionToRequest(null, action, Date.now(), this.advancedLogic);

const resultPersistTx = await this.transactionManager.persistTransaction(
JSON.stringify(action),
return this.persistTransaction(
requestId,
action,
{ requestId },
hashedTopics,
encryptionParams,
);

const result = Object.assign(new EventEmitter(), {
meta: { transactionManagerMeta: resultPersistTx.meta },
result: { requestId },
});

// When receive the confirmation from transaction manager propagate it
resultPersistTx
.on('confirmed', (resultPersistTxConfirmed: TransactionTypes.IReturnPersistTransaction) => {
result.emit('confirmed', {
meta: { transactionManagerMeta: resultPersistTxConfirmed.meta },
result: { requestId },
});
})
.on('error', (error) => {
result.emit('error', error);
});

return result;
}

/**
Expand Down Expand Up @@ -192,27 +151,7 @@ export default class RequestLogic implements RequestLogicTypes.IRequestLogic {
await this.validateAction(requestId, action);
}

const resultPersistTx = await this.transactionManager.persistTransaction(
JSON.stringify(action),
requestId,
);

const result = Object.assign(new EventEmitter(), {
meta: { transactionManagerMeta: resultPersistTx.meta },
});

// When receive the confirmation from transaction manager propagate it
resultPersistTx
.on('confirmed', (resultPersistTxConfirmed: TransactionTypes.IReturnPersistTransaction) => {
result.emit('confirmed', {
meta: { transactionManagerMeta: resultPersistTxConfirmed.meta },
});
})
.on('error', (error) => {
result.emit('error', error);
});

return result;
return this.persistTransaction(requestId, action, undefined);
}

/**
Expand Down Expand Up @@ -242,27 +181,7 @@ export default class RequestLogic implements RequestLogicTypes.IRequestLogic {
await this.validateAction(requestId, action);
}

const resultPersistTx = await this.transactionManager.persistTransaction(
JSON.stringify(action),
requestId,
);

const result = Object.assign(new EventEmitter(), {
meta: { transactionManagerMeta: resultPersistTx.meta },
});

// When receive the confirmation from transaction manager propagate it
resultPersistTx
.on('confirmed', (resultPersistTxConfirmed: TransactionTypes.IReturnPersistTransaction) => {
result.emit('confirmed', {
meta: { transactionManagerMeta: resultPersistTxConfirmed.meta },
});
})
.on('error', (error) => {
result.emit('error', error);
});

return result;
return this.persistTransaction(requestId, action, undefined);
}

/**
Expand Down Expand Up @@ -292,27 +211,7 @@ export default class RequestLogic implements RequestLogicTypes.IRequestLogic {
await this.validateAction(requestId, action);
}

const resultPersistTx = await this.transactionManager.persistTransaction(
JSON.stringify(action),
requestId,
);

const result = Object.assign(new EventEmitter(), {
meta: { transactionManagerMeta: resultPersistTx.meta },
});

// When receive the confirmation from transaction manager propagate it
resultPersistTx
.on('confirmed', (resultPersistTxConfirmed: TransactionTypes.IReturnPersistTransaction) => {
result.emit('confirmed', {
meta: { transactionManagerMeta: resultPersistTxConfirmed.meta },
});
})
.on('error', (error) => {
result.emit('error', error);
});

return result;
return this.persistTransaction(requestId, action, undefined);
}

/**
Expand Down Expand Up @@ -342,27 +241,7 @@ export default class RequestLogic implements RequestLogicTypes.IRequestLogic {
await this.validateAction(requestId, action);
}

const resultPersistTx = await this.transactionManager.persistTransaction(
JSON.stringify(action),
requestId,
);

const result = Object.assign(new EventEmitter(), {
meta: { transactionManagerMeta: resultPersistTx.meta },
});

// When receive the confirmation from transaction manager propagate it
resultPersistTx
.on('confirmed', (resultPersistTxConfirmed: TransactionTypes.IReturnPersistTransaction) => {
result.emit('confirmed', {
meta: { transactionManagerMeta: resultPersistTxConfirmed.meta },
});
})
.on('error', (error) => {
result.emit('error', error);
});

return result;
return this.persistTransaction(requestId, action, undefined);
}

/**
Expand Down Expand Up @@ -394,29 +273,7 @@ export default class RequestLogic implements RequestLogicTypes.IRequestLogic {
await this.validateAction(requestId, action);
}

const resultPersistTx = await this.transactionManager.persistTransaction(
JSON.stringify(action),
requestId,
undefined,
encryptionParams,
);

const result = Object.assign(new EventEmitter(), {
meta: { transactionManagerMeta: resultPersistTx.meta },
});

// When receive the confirmation from transaction manager propagate it
resultPersistTx
.on('confirmed', (resultPersistTxConfirmed: TransactionTypes.IReturnPersistTransaction) => {
result.emit('confirmed', {
meta: { transactionManagerMeta: resultPersistTxConfirmed.meta },
});
})
.on('error', (error) => {
result.emit('error', error);
});

return result;
return this.persistTransaction(requestId, action, undefined, [], encryptionParams);
}

/**
Expand Down Expand Up @@ -447,27 +304,7 @@ export default class RequestLogic implements RequestLogicTypes.IRequestLogic {
await this.validateAction(requestId, action);
}

const resultPersistTx = await this.transactionManager.persistTransaction(
JSON.stringify(action),
requestId,
);

const result = Object.assign(new EventEmitter(), {
meta: { transactionManagerMeta: resultPersistTx.meta },
});

// When receive the confirmation from transaction manager propagate it
resultPersistTx
.on('confirmed', (resultPersistTxConfirmed: TransactionTypes.IReturnPersistTransaction) => {
result.emit('confirmed', {
meta: { transactionManagerMeta: resultPersistTxConfirmed.meta },
});
})
.on('error', (error) => {
result.emit('error', error);
});

return result;
return this.persistTransaction(requestId, action, undefined);
}

/**
Expand Down Expand Up @@ -940,4 +777,36 @@ export default class RequestLogic implements RequestLogicTypes.IRequestLogic {

return { ignoredTransactions, keptTransactions };
}

private async persistTransaction<T = unknown>(
requestId: string,
action: RequestLogicTypes.IAction,
result: T,
topics: string[] = [],
encryptionParams: EncryptionTypes.IEncryptionParameters[] = [],
) {
const resultPersistTx = await this.transactionManager.persistTransaction(
JSON.stringify(action),
requestId,
topics,
encryptionParams,
);

const eventEmitter = new EventEmitter();

// When receive the confirmation from transaction manager propagate it
resultPersistTx
.on('confirmed', (resultPersistTxConfirmed) =>
eventEmitter.emit('confirmed', {
meta: { transactionManagerMeta: resultPersistTxConfirmed.meta },
result,
}),
)
.on('error', (error) => eventEmitter.emit('error', error));

return Object.assign(eventEmitter, {
meta: { transactionManagerMeta: resultPersistTx.meta },
result,
});
}
}
15 changes: 14 additions & 1 deletion packages/request-logic/test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ describe('index', () => {
JSON.stringify(action),
requestId,
[],
[],
);
});

Expand All @@ -166,6 +167,7 @@ describe('index', () => {
MultiFormat.serialize(normalizeKeccak256Hash(TestData.payeeRaw.identity)),
MultiFormat.serialize(normalizeKeccak256Hash(TestData.payerRaw.identity)),
],
[],
);
});

Expand Down Expand Up @@ -212,6 +214,7 @@ describe('index', () => {
JSON.stringify(action),
requestId,
[],
[],
);
jest.useRealTimers();
});
Expand Down Expand Up @@ -478,6 +481,8 @@ describe('index', () => {
expect(fakeTransactionManager.persistTransaction).toHaveBeenCalledWith(
JSON.stringify(actionExpected),
requestId,
[],
[],
);
});
it('cannot acceptRequest without signature provider', async () => {
Expand Down Expand Up @@ -574,6 +579,8 @@ describe('index', () => {
expect(fakeTransactionManager.persistTransaction).toHaveBeenCalledWith(
JSON.stringify(actionExpected),
requestId,
[],
[],
);
});
it('cannot cancelRequest without signature provider', async () => {
Expand Down Expand Up @@ -675,6 +682,8 @@ describe('index', () => {
expect(fakeTransactionManager.persistTransaction).toHaveBeenCalledWith(
JSON.stringify(actionExpected),
requestId,
[],
[],
);
});
it('cannot increaseExpectedAmountRequest without signature provider', async () => {
Expand Down Expand Up @@ -781,6 +790,8 @@ describe('index', () => {
expect(fakeTransactionManager.persistTransaction).toHaveBeenCalledWith(
JSON.stringify(actionExpected),
requestId,
[],
[],
);
});
it('cannot reduceExpectedAmountRequest without signature provider', async () => {
Expand Down Expand Up @@ -882,7 +893,7 @@ describe('index', () => {
expect(fakeTransactionManager.persistTransaction).toHaveBeenCalledWith(
JSON.stringify(actionExpected),
requestId,
undefined,
[],
[TestData.otherIdRaw.encryptionParams],
);
});
Expand Down Expand Up @@ -928,6 +939,8 @@ describe('index', () => {
expect(fakeTransactionManager.persistTransaction).toHaveBeenCalledWith(
JSON.stringify(actionExpected),
requestId,
[],
[],
);
});
it('cannot addExtensionsDataRequest without signature provider', async () => {
Expand Down