Skip to content

Commit

Permalink
update dre l2 cache with warp cache data
Browse files Browse the repository at this point in the history
  • Loading branch information
Tadeuchi committed Jun 2, 2023
1 parent c8a9e09 commit 4f3b662
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 10 deletions.
28 changes: 27 additions & 1 deletion src/db/nodeDb.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const logger = require('../logger')('node-db');

let eventsDb = null;
let stateDb = null;
let warpStateDb = null;

module.exports = {
createNodeDbEventsTables: async (knex) => {
Expand Down Expand Up @@ -90,6 +91,19 @@ module.exports = {
return stateDb;
},

connectWarpCacheDb: () => {
if (warpStateDb == null) {
warpStateDb = knex({
client: 'better-sqlite3',
connection: {
filename: `cache/warp/sqlite/state.db`
},
useNullAsDefault: true
});
}
return warpStateDb;
},

connectEvents: () => {
if (eventsDb == null) {
eventsDb = knex({
Expand Down Expand Up @@ -207,7 +221,19 @@ module.exports = {
await eventsDb.raw('DELETE FROM events WHERE contract_tx_id = ?;', [contractTxId]);
},

getLastState: async (nodeDb, contractTxId) => {
getLastStateFromWarpCache: async (nodeDb, contractTxId) => {
const result = await warpStateDb.raw(
`select sort_key AS sortKey, value AS cachedValue
from sort_key_cache
where key = ?
ORDER BY sort_key DESC
LIMIT 1;`,
[contractTxId]
);
return result.length === 0 ? null : result[0];
},

getLastStateFromDreCache: async (nodeDb, contractTxId) => {
const result = await nodeDb('states')
.where({
contract_tx_id: contractTxId
Expand Down
4 changes: 3 additions & 1 deletion src/listener.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ const {
hasContract,
connectEvents,
createNodeDbEventsTables,
doBlacklist
doBlacklist,
connectWarpCacheDb
} = require('./db/nodeDb');

const logger = require('./logger')('listener');
Expand Down Expand Up @@ -48,6 +49,7 @@ async function runListener() {

const nodeDb = connect();
const nodeDbEvents = connectEvents();
const nodeWarpState = connectWarpCacheDb();

await createNodeDbTables(nodeDb);
await createNodeDbEventsTables(nodeDbEvents);
Expand Down
26 changes: 20 additions & 6 deletions src/routes/contract.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
const { JSONPath } = require('jsonpath-plus');
const { getLastState, getContractErrors, getFailures, events } = require('../db/nodeDb');
const {
getLastStateFromDreCache,
getContractErrors,
getFailures,
events,
getLastStateFromWarpCache,
insertState
} = require('../db/nodeDb');
const { config } = require('../config');

const registrationStatus = {
Expand Down Expand Up @@ -29,22 +36,29 @@ module.exports = async (ctx) => {
response.status = registrationStatus['blacklisted'];
response.errors = await getContractErrors(nodeDb, contractId);
} else {
const result = await getLastState(nodeDb, contractId);
const warpState = await getLastStateFromWarpCache(nodeDb, contractId);
let result = await getLastStateFromDreCache(nodeDb, contractId);
let parsed = false;
if (warpState && (!result || result.sort_key.localeCompare(warpState.sortKey) < 0)) {
warpState.cachedValue = JSON.parse(warpState.cachedValue);
result = await insertState(nodeDb, contractId, warpState);
parsed = true;
}
if (result) {
response.status = registrationStatus['evaluated'];
response.contractTxId = contractId;
if (query) {
response.result = JSONPath({ path: query, json: JSON.parse(result.state) });
} else {
if (showState) {
response.state = JSON.parse(result.state);
response.state = parsed ? result.state : JSON.parse(result.state);
}
}
if (showValidity) {
response.validity = JSON.parse(result.validity);
response.validity = parsed ? result.validity : JSON.parse(result.validity);
}
if (showErrorMessages) {
response.errorMessages = JSON.parse(result.error_messages);
response.errorMessages = parsed ? result.error_messages : JSON.parse(result.error_messages);
}
if (showErrors) {
response.errors = await getContractErrors(nodeDb, contractId);
Expand All @@ -53,7 +67,7 @@ module.exports = async (ctx) => {
response.timestamp = result.timestamp;
response.signature = result.signature;
response.stateHash = result.state_hash;
response.manifest = JSON.parse(result.manifest);
response.manifest = parsed ? result.manifest : JSON.parse(result.manifest);
} else {
const contractErrors = await getContractErrors(nodeDb, contractId);
if (contractErrors.length) {
Expand Down
4 changes: 2 additions & 2 deletions src/routes/state.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const { JSONPath } = require('jsonpath-plus');
const { getLastState } = require('../db/nodeDb');
const { getLastStateFromDreCache } = require('../db/nodeDb');

module.exports = async (ctx) => {
const contractId = ctx.query.id;
Expand All @@ -10,7 +10,7 @@ module.exports = async (ctx) => {

try {
const response = {};
const result = await getLastState(nodeDb, contractId);
const result = await getLastStateFromDreCache(nodeDb, contractId);
if (result) {
response.contractTxId = contractId;
response.sortKey = result.sort_key;
Expand Down

0 comments on commit 4f3b662

Please sign in to comment.