Skip to content

Commit

Permalink
feat: split data-access read and write for TheGraph (#875)
Browse files Browse the repository at this point in the history
  • Loading branch information
benjlevesque committed Jul 4, 2022
1 parent 9c5ae2b commit 8fdf34d
Show file tree
Hide file tree
Showing 9 changed files with 246 additions and 112 deletions.
5 changes: 4 additions & 1 deletion packages/request-client.js/src/api/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -382,7 +382,10 @@ export default class Request {
}

extensionsData.push(
declarativePaymentNetwork.createExtensionsDataForDeclareSentPayment({ amount, note }),
declarativePaymentNetwork.createExtensionsDataForDeclareSentPayment({
amount,
note,
}),
);

const parameters: RequestLogicTypes.IAddExtensionsDataParameters = {
Expand Down
2 changes: 1 addition & 1 deletion packages/request-node/src/request/getChannelsByTopic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { StatusCodes } from 'http-status-codes';

const GET_CHANNELS_TIMEOUT = 600000;
export default class GetChannelHandler {
constructor(private logger: LogTypes.ILogger, private dataAccess: DataAccessTypes.IDataAccess) {
constructor(private logger: LogTypes.ILogger, private dataAccess: DataAccessTypes.IDataRead) {
this.handler = this.handler.bind(this);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { StatusCodes } from 'http-status-codes';
const GET_TRANSACTIONS_TIMEOUT = 600000;

export default class GetTransactionsByChannelIdHandler {
constructor(private logger: LogTypes.ILogger, private dataAccess: DataAccessTypes.IDataAccess) {
constructor(private logger: LogTypes.ILogger, private dataAccess: DataAccessTypes.IDataRead) {
this.handler = this.handler.bind(this);
}

Expand Down
2 changes: 1 addition & 1 deletion packages/request-node/src/request/persistTransaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export default class PersistTransactionHandler {
*/
constructor(
private confirmedTransactionStore: ConfirmedTransactionStore,
private dataAccess: DataAccessTypes.IDataAccess,
private dataAccess: DataAccessTypes.IDataWrite,
private logger: LogTypes.ILogger,
) {
this.handler = this.handler.bind(this);
Expand Down
44 changes: 44 additions & 0 deletions packages/request-node/src/thegraph/CombinedDataAccess.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { DataAccessTypes } from '@requestnetwork/types';

export abstract class CombinedDataAccess implements DataAccessTypes.IDataAccess {
constructor(
protected reader: DataAccessTypes.IDataRead,
protected writer: DataAccessTypes.IDataWrite,
) {
this.getTransactionsByChannelId = this.reader.getTransactionsByChannelId.bind(this.reader);
this.getChannelsByTopic = this.reader.getChannelsByTopic.bind(this.reader);
this.getChannelsByMultipleTopics = this.reader.getChannelsByMultipleTopics.bind(this.reader);
this.persistTransaction = this.writer.persistTransaction.bind(this.writer);
}

async initialize(): Promise<void> {
await this.reader.initialize();
await this.writer.initialize();
}

async close(): Promise<void> {
await this.writer.close();
await this.reader.close();
}

getTransactionsByChannelId: (
channelId: string,
updatedBetween?: DataAccessTypes.ITimestampBoundaries | undefined,
) => Promise<DataAccessTypes.IReturnGetTransactions>;

getChannelsByTopic: (
topic: string,
updatedBetween?: DataAccessTypes.ITimestampBoundaries | undefined,
) => Promise<DataAccessTypes.IReturnGetChannelsByTopic>;
getChannelsByMultipleTopics: (
topics: string[],
updatedBetween?: DataAccessTypes.ITimestampBoundaries,
) => Promise<DataAccessTypes.IReturnGetChannelsByTopic>;
persistTransaction: (
transactionData: DataAccessTypes.ITransaction,
channelId: string,
topics?: string[] | undefined,
) => Promise<DataAccessTypes.IReturnPersistTransaction>;

abstract _getStatus(): Promise<any>;
}
30 changes: 30 additions & 0 deletions packages/request-node/src/thegraph/PendingStore.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { DataAccessTypes, StorageTypes } from '@requestnetwork/types';

type PendingItem = {
transaction: DataAccessTypes.ITransaction;
storageResult: StorageTypes.IAppendResult;
};
/**
* A simple in-memory store to share state between DataReader and DataWriter
* Useful to retrieve a transaction that was just emitted but is not confirmed yet
**/
export class PendingStore {
private pending: Record<string, PendingItem> = {};

/** Gets a pending tx */
public get(channelId: string): PendingItem {
return this.pending[channelId];
}

public add(
channelId: string,
transaction: DataAccessTypes.ITransaction,
storageResult: StorageTypes.IAppendResult,
): void {
this.pending[channelId] = { transaction, storageResult };
}

public remove(channelId: string): void {
delete this.pending[channelId];
}
}
Loading

0 comments on commit 8fdf34d

Please sign in to comment.