Skip to content

Commit

Permalink
fix: undefined message error
Browse files Browse the repository at this point in the history
  • Loading branch information
Tadeuchi committed Jan 24, 2024
1 parent 51e188a commit 67b1148
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 18 deletions.
38 changes: 22 additions & 16 deletions src/PgContractCache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,12 +138,12 @@ export class PgContractCache<V> implements BasicSortKeyCache<EvalStateResult<V>>
const getBenchmark = Benchmark.measure();
const result = await this.connection().query(
`WITH validity_page AS
(SELECT tx_id, valid
(SELECT tx_id, valid, error_message
from warp.validity
where key = $1
and sort_key <= $2
ORDER BY sort_key DESC, id DESC)
select json_object_agg(tx_id, valid) as v, count(*) as count
select json_object_agg(tx_id, valid) as v, json_object_agg(tx_id, error_message) as err, count(*) as count
from validity_page;`,
[cacheKey.key, cacheKey.sortKey]
);
Expand All @@ -155,9 +155,9 @@ export class PgContractCache<V> implements BasicSortKeyCache<EvalStateResult<V>>
});

if (result && result.rows.length > 0) {
return new PgContractValidity(result.rows[0].v, result.rows[0].count);
return new PgContractValidity(result.rows[0].v, result.rows[0].err, result.rows[0].count);
}
return new PgContractValidity({}, 0);
return new PgContractValidity({}, {}, 0);
}

async getLast(key: string): Promise<SortKeyCacheResult<EvalStateResult<V>> | null> {
Expand Down Expand Up @@ -315,20 +315,23 @@ export class PgContractCache<V> implements BasicSortKeyCache<EvalStateResult<V>>
[stateCacheKey.key, stateCacheKey.sortKey, stringifiedState]
);

let insertValues = '';
let sql = '';
let batchCounter = 0;
let valueCounter = 0;
let values = [];
for (const [tx, v] of Object.entries(value.validity)) {
batchCounter++;
insertValues += `${batchCounter > 1 ? ',' : ''} ('${stateCacheKey.key}', '${
stateCacheKey.sortKey
}', '${tx}', ${v}, '${value.errorMessages[tx]}')`;
values.push(stateCacheKey.key, stateCacheKey.sortKey, tx, v, value.errorMessages[tx]);
sql += `${batchCounter > 1 ? ',' : ''} ($${++valueCounter}, $${++valueCounter}, $${++valueCounter}, $${++valueCounter}, $${++valueCounter})`;
if (batchCounter % this.pgCacheOptions.validityBatchSize === 0) {
await this.queryInsertValidity(insertValues);
insertValues = '';
await this.queryInsertValidity(sql, values);
sql = '';
batchCounter = 0;
valueCounter = 0;
values = [];
}
}
await this.queryInsertValidity(insertValues);
await this.queryInsertValidity(sql, values);

insertBenchmark.stop();
this.benchmarkLogger.debug('PG Benchmark', {
Expand All @@ -342,17 +345,18 @@ export class PgContractCache<V> implements BasicSortKeyCache<EvalStateResult<V>>
});
}

private async queryInsertValidity(formattedValues: string): Promise<void> {
if (formattedValues) {
private async queryInsertValidity(sql: string, values: any[]): Promise<void> {
if (sql && values) {
await this.connection().query(
`INSERT INTO warp.validity (key, sort_key, tx_id, valid, error_message)
VALUES
${formattedValues}
${sql}
ON CONFLICT(key, tx_id)
DO UPDATE
SET valid = EXCLUDED.valid,
sort_key = EXCLUDED.sort_key,
error_message = EXCLUDED.error_message`
error_message = EXCLUDED.error_message`,
values
);
}
}
Expand Down Expand Up @@ -444,11 +448,13 @@ export class PgContractCache<V> implements BasicSortKeyCache<EvalStateResult<V>>
}

export class PgContractValidity {
constructor(v: Record<string, boolean>, count: number) {
constructor(v: Record<string, boolean>, err: Record<string, string>, count: number) {
this.validity = v;
this.errorMessages = err;
this.count = count;
}

readonly validity: Record<string, boolean>;
readonly errorMessages: Record<string, string>;
readonly count: number;
}
18 changes: 16 additions & 2 deletions src/__tests__/pg-cache-validity.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,10 @@ describe('Postgres cache', () => {
zz15: true
},
{
zz12: 'bum!!'
zz12: 'bum!!',
zz13: undefined,
zz14: undefined,
zz15: null
}
)
);
Expand Down Expand Up @@ -54,7 +57,8 @@ describe('Postgres cache', () => {
zz18: true
},
{
zz17: 'bum!!'
zz17: 'bum!!',
zz18: undefined
}
)
);
Expand Down Expand Up @@ -97,6 +101,16 @@ describe('Postgres cache', () => {
zz16: true,
zz17: false,
zz18: true
},
errorMessages: {
zz11: null,
zz12: 'bum!!',
zz13: null,
zz14: null,
zz15: null,
zz16: null,
zz17: 'bum!!',
zz18: null
}
});

Expand Down

0 comments on commit 67b1148

Please sign in to comment.