-
Notifications
You must be signed in to change notification settings - Fork 375
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add mock signer interfaces
- Loading branch information
Showing
8 changed files
with
251 additions
and
213 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
123 changes: 60 additions & 63 deletions
123
packages/phone-number-privacy/signer/src/common/database/wrappers/account.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,81 +1,78 @@ | ||
// import { ErrorMessage } from '@celo/phone-number-privacy-common' | ||
import { ErrorMessage } from '@celo/phone-number-privacy-common' | ||
import Logger from 'bunyan' | ||
import { Knex } from 'knex' | ||
// import { config } from '../../../config' | ||
// import { AccountRecord, ACCOUNTS_COLUMNS, ACCOUNTS_TABLE, toAccountRecord } from '../models/account' | ||
// import { doMeteredSql } from '../utils' | ||
import { config } from '../../../config' | ||
import { AccountRecord, ACCOUNTS_COLUMNS, ACCOUNTS_TABLE, toAccountRecord } from '../models/account' | ||
import { doMeteredSql } from '../utils' | ||
|
||
/* | ||
* Returns how many queries the account has already performed. | ||
*/ | ||
export async function getPerformedQueryCount( | ||
_db: Knex, | ||
_account: string, | ||
_logger: Logger | ||
db: Knex, | ||
account: string, | ||
logger: Logger | ||
): Promise<number> { | ||
return Promise.resolve(0) | ||
// logger.debug({ account }, 'Getting performed query count') | ||
// return doMeteredSql( | ||
// 'getPerformedQueryCount', | ||
// ErrorMessage.DATABASE_GET_FAILURE, | ||
// logger, | ||
// async () => { | ||
// const queryCounts = await db<AccountRecord>(ACCOUNTS_TABLE) | ||
// .where(ACCOUNTS_COLUMNS.address, account) | ||
// .select(ACCOUNTS_COLUMNS.numLookups) | ||
// .first() | ||
// .timeout(config.db.timeout) | ||
// return queryCounts === undefined ? 0 : queryCounts[ACCOUNTS_COLUMNS.numLookups] | ||
// } | ||
// ) | ||
logger.debug({ account }, 'Getting performed query count') | ||
return doMeteredSql( | ||
'getPerformedQueryCount', | ||
ErrorMessage.DATABASE_GET_FAILURE, | ||
logger, | ||
async () => { | ||
const queryCounts = await db<AccountRecord>(ACCOUNTS_TABLE) | ||
.where(ACCOUNTS_COLUMNS.address, account) | ||
.select(ACCOUNTS_COLUMNS.numLookups) | ||
.first() | ||
.timeout(config.db.timeout) | ||
return queryCounts === undefined ? 0 : queryCounts[ACCOUNTS_COLUMNS.numLookups] | ||
} | ||
) | ||
} | ||
|
||
// async function getAccountExists( | ||
// db: Knex, | ||
// account: string, | ||
// logger: Logger, | ||
// trx?: Knex.Transaction | ||
// ): Promise<boolean> { | ||
// return Promise.resolve(true) | ||
// return doMeteredSql('getAccountExists', ErrorMessage.DATABASE_GET_FAILURE, logger, async () => { | ||
// const sql = db<AccountRecord>(ACCOUNTS_TABLE) | ||
// .where(ACCOUNTS_COLUMNS.address, account) | ||
// .first() | ||
// .timeout(config.db.timeout) | ||
async function getAccountExists( | ||
db: Knex, | ||
account: string, | ||
logger: Logger, | ||
trx?: Knex.Transaction | ||
): Promise<boolean> { | ||
return doMeteredSql('getAccountExists', ErrorMessage.DATABASE_GET_FAILURE, logger, async () => { | ||
const sql = db<AccountRecord>(ACCOUNTS_TABLE) | ||
.where(ACCOUNTS_COLUMNS.address, account) | ||
.first() | ||
.timeout(config.db.timeout) | ||
|
||
// const accountRecord = await (trx != null ? sql.transacting(trx) : sql) | ||
// return !!accountRecord | ||
// }) | ||
// } | ||
const accountRecord = await (trx != null ? sql.transacting(trx) : sql) | ||
return !!accountRecord | ||
}) | ||
} | ||
|
||
/* | ||
* Increments query count in database. If record doesn't exist, create one. | ||
*/ | ||
export async function incrementQueryCount( | ||
_db: Knex, | ||
_account: string, | ||
_logger: Logger, | ||
_trx?: Knex.Transaction | ||
db: Knex, | ||
account: string, | ||
logger: Logger, | ||
trx?: Knex.Transaction | ||
): Promise<void> { | ||
return | ||
// logger.debug({ account }, 'Incrementing query count') | ||
// return doMeteredSql( | ||
// 'incrementQueryCount', | ||
// ErrorMessage.DATABASE_INSERT_FAILURE, | ||
// logger, | ||
// async () => { | ||
// if (await getAccountExists(db, account, logger, trx)) { | ||
// const sql = db<AccountRecord>(ACCOUNTS_TABLE) | ||
// .where(ACCOUNTS_COLUMNS.address, account) | ||
// .increment(ACCOUNTS_COLUMNS.numLookups, 1) | ||
// .timeout(config.db.timeout) | ||
// await (trx != null ? sql.transacting(trx) : sql) | ||
// } else { | ||
// const sql = db<AccountRecord>(ACCOUNTS_TABLE) | ||
// .insert(toAccountRecord(account, 1)) | ||
// .timeout(config.db.timeout) | ||
// await (trx != null ? sql.transacting(trx) : sql) | ||
// } | ||
// } | ||
// ) | ||
logger.debug({ account }, 'Incrementing query count') | ||
return doMeteredSql( | ||
'incrementQueryCount', | ||
ErrorMessage.DATABASE_INSERT_FAILURE, | ||
logger, | ||
async () => { | ||
if (await getAccountExists(db, account, logger, trx)) { | ||
const sql = db<AccountRecord>(ACCOUNTS_TABLE) | ||
.where(ACCOUNTS_COLUMNS.address, account) | ||
.increment(ACCOUNTS_COLUMNS.numLookups, 1) | ||
.timeout(config.db.timeout) | ||
await (trx != null ? sql.transacting(trx) : sql) | ||
} else { | ||
const sql = db<AccountRecord>(ACCOUNTS_TABLE) | ||
.insert(toAccountRecord(account, 1)) | ||
.timeout(config.db.timeout) | ||
await (trx != null ? sql.transacting(trx) : sql) | ||
} | ||
} | ||
) | ||
} |
74 changes: 36 additions & 38 deletions
74
packages/phone-number-privacy/signer/src/common/database/wrappers/request.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,48 +1,46 @@ | ||
// import { ErrorMessage } from '@celo/phone-number-privacy-common' | ||
import { ErrorMessage } from '@celo/phone-number-privacy-common' | ||
import Logger from 'bunyan' | ||
import { Knex } from 'knex' | ||
// import { config } from '../../../config' | ||
// import { | ||
// PnpSignRequestRecord, | ||
// // REQUESTS_COLUMNS, | ||
// REQUESTS_TABLE, | ||
// toPnpSignRequestRecord, | ||
// } from '../models/request' | ||
// import { doMeteredSql } from '../utils' | ||
import { config } from '../../../config' | ||
import { | ||
PnpSignRequestRecord, | ||
REQUESTS_COLUMNS, | ||
REQUESTS_TABLE, | ||
toPnpSignRequestRecord, | ||
} from '../models/request' | ||
import { doMeteredSql } from '../utils' | ||
|
||
export async function getRequestExists( // TODO try insert, if primary key error, then duplicate request | ||
_db: Knex, | ||
_account: string, | ||
_blindedQuery: string, | ||
_logger: Logger | ||
db: Knex, | ||
account: string, | ||
blindedQuery: string, | ||
logger: Logger | ||
): Promise<boolean> { | ||
return Promise.resolve(false) | ||
// logger.debug(`Checking if request exists for account: ${account}, blindedQuery: ${blindedQuery}`) | ||
// return doMeteredSql('getRequestExists', ErrorMessage.DATABASE_GET_FAILURE, logger, async () => { | ||
// const existingRequest = await db<PnpSignRequestRecord>(REQUESTS_TABLE) | ||
// .where({ | ||
// [REQUESTS_COLUMNS.address]: account, | ||
// [REQUESTS_COLUMNS.blindedQuery]: blindedQuery, // TODO are we using the primary key correctly?? | ||
// }) | ||
// .first() | ||
// .timeout(config.db.timeout) | ||
// return !!existingRequest // TODO use EXISTS query?? | ||
// }) | ||
logger.debug(`Checking if request exists for account: ${account}, blindedQuery: ${blindedQuery}`) | ||
return doMeteredSql('getRequestExists', ErrorMessage.DATABASE_GET_FAILURE, logger, async () => { | ||
const existingRequest = await db<PnpSignRequestRecord>(REQUESTS_TABLE) | ||
.where({ | ||
[REQUESTS_COLUMNS.address]: account, | ||
[REQUESTS_COLUMNS.blindedQuery]: blindedQuery, // TODO are we using the primary key correctly?? | ||
}) | ||
.first() | ||
.timeout(config.db.timeout) | ||
return !!existingRequest // TODO use EXISTS query?? | ||
}) | ||
} | ||
|
||
export async function insertRequest( | ||
_db: Knex, | ||
_account: string, | ||
_blindedQuery: string, | ||
_logger: Logger, | ||
_trx?: Knex.Transaction | ||
db: Knex, | ||
account: string, | ||
blindedQuery: string, | ||
logger: Logger, | ||
trx?: Knex.Transaction | ||
): Promise<void> { | ||
return | ||
// logger.debug(`Storing salt request for: ${account}, blindedQuery: ${blindedQuery}`) | ||
// return doMeteredSql('insertRequest', ErrorMessage.DATABASE_INSERT_FAILURE, logger, async () => { | ||
// const sql = db<PnpSignRequestRecord>(REQUESTS_TABLE) | ||
// .insert(toPnpSignRequestRecord(account, blindedQuery)) | ||
// .timeout(config.db.timeout) | ||
// await (trx != null ? sql.transacting(trx) : sql) | ||
// }) | ||
logger.debug(`Storing salt request for: ${account}, blindedQuery: ${blindedQuery}`) | ||
return doMeteredSql('insertRequest', ErrorMessage.DATABASE_INSERT_FAILURE, logger, async () => { | ||
const sql = db<PnpSignRequestRecord>(REQUESTS_TABLE) | ||
.insert(toPnpSignRequestRecord(account, blindedQuery)) | ||
.timeout(config.db.timeout) | ||
await (trx != null ? sql.transacting(trx) : sql) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.