Skip to content

Commit

Permalink
Feature: Preload application addresses (#22)
Browse files Browse the repository at this point in the history
  • Loading branch information
brunomenezes authored Jan 16, 2024
1 parent 8422d8e commit d37fa6d
Show file tree
Hide file tree
Showing 19 changed files with 335 additions and 77 deletions.
1 change: 1 addition & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ npm-debug.log
/pages
/docs
/.vscode
/preloaders


# OS Files
Expand Down
4 changes: 2 additions & 2 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
"editor.defaultFormatter": "esbenp.prettier-vscode",
"editor.codeActionsOnSave": {
"source.fixAll": true,
"source.organizeImports": true
"source.fixAll": "explicit",
"source.organizeImports": "explicit"
},
"[terraform]": {
"editor.defaultFormatter": "hashicorp.terraform"
Expand Down
1 change: 1 addition & 0 deletions assets/applications-1.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"height":18958008,"hash":"0xd723ba51f35ad8e2e868972669b14f179d7ff451f08687d64d86972f7340f6a7","top":[],"addresses":{"0x7122cd1221c20892234186facfe8615e6743ab02":["0x0974cc873df893b302f6be7ecf4f9d4b1a15c366"]}}
1 change: 1 addition & 0 deletions assets/applications-11155111.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"height":5031033,"hash":"0x60b57b9528dec06fcf6b7038bf7c24e61588234a421be558bcb1c5c7df66a04c","top":[],"addresses":{"0x7122cd1221c20892234186facfe8615e6743ab02":["0x70ac08179605af2d9e75782b8decdd3c22aa4d0c","0x71ab24ee3ddb97dc01a161edf64c8d51102b0cd3","0x3694c82fde031b8462e90e8bfee0377de2b01ecc","0x3adf2a4ba065d4e128e1bdbfd91f489b07877d89","0x7bd3565af78d8457c81ff8b4870a174fa3783eb0","0xb35a7a3d7c1854e0a1196e3c706c4bb5c2b1657c","0xfa18d9738d906813fe81aade5bb2739207162305","0x184436275e033a8d1939849eadb5e744ade197c7","0x331094ebf8ba94790b1f0194291bef4e297b3466","0xc3af72d3c8e8aaa62535c6d5edfe4bd1d594a811","0xf9f432badf8514944b7a22c6ebd9775ef1d10494","0x3f19c60b0d818e0b843c10349111f64e5d5b18d5","0x74608454c71165ac0f2e503d47a00092ff0a48a3","0x87e73a1517e8144e37764192c544896eb4da7a57","0xeca14c01b1eb97dbd5c2e654f0e0f9693f580d42","0x9c3276bbeeee65e07c20acb262e4673dd4bcbce3","0x6fbe45a6b9e32092e2d813133c3904dfc4a92cc4","0x7c92af1ad96a80325d35ad4e505918f1923cf438","0x184b770bb161dcaade158a7347cc084a0c044801","0x674cb14ea404348b3b87c6e629f862014a72b6e9","0x53efed7c0a9e28c93201c71f0fa061ac9384407c","0x9f12d4365806fc000d6555acb85c5371b464e506","0xf4cb3c246203025dcfaf221deb1fff2bb90f2b20","0xb663c083fe2259f0514cf53a832308c1ae31986a","0x59b7b9328f06d5bc2843e8f3aec4a8d5e5419518","0x113dff0952688de6d9a09b2cdd58a5ba3d10d395","0xe5bdcc71da2c1df734cc560d33a8b4036372c27a","0x4ca2f6935200b9a782a78f408f640f17b29809d8","0xbef6f348e398b0fc81a20ecae268cee79ebf0053","0xa0f84501eaf5c85efdba0e44da026239023e531d","0xc6f515b07a8996aa78358ebe48a280cf4eabbed4","0x76e42e81f892ffac5ae7017b4764c8bba26536e1","0x7d7bc8237574fc74f7c6e0577db5526465e428bb","0xfb6de8730ff4a51332233d80c6abb38beed9e586","0x5b8647cd40278ff6504b8ad008f1fe468bcba2ae","0x9a6a74d1ca0b5219b0bea2988d4a9f309ce64a80","0x4906e2fc1383d5e70defb62e74ae7274b535fc65","0x5818d91e461d3120d525464f677ee17455e5c83c","0xd5e518c5bcd79d91fae9911ad8e2e31d95491975","0x7329704939e953242f1dbef51d790b773063601b","0x85280983a0e41533dfa4f8a7853645e8f08ff92a","0xcfcbedb8ceeb3ab21818039d274b59c774549597","0x40ecf0b914d1292bbf9390b80d701cee7c083ff7","0x9fbcd682e86f87420cf9b5f3c8c815f262b54ff4","0xf50ed28a165d368bdfb4a3e31c383917b0eadfea","0x0be010fa7e70d74fa8b6729fe1ae268787298f54","0x393d5ed52d3516ecd9442ca08f735143dee7e1ca","0xde1476c525805af79023fb55eb30ef0e01def6dc","0xe2510c5470fedd84e6fbff29c647b0c883dbc496","0x028367fe226cd9e5699f4288d512fe3a4a4a0012","0x56708c2cd2ee23ac169541013adea91e25e4eab9","0xa7a1fc4971435358718d5e48ddc38482410234b6","0x5874625fa6228bb63d2efdb2657b7f103380e403","0xe495186fde78d3c725079da3859b42da74ebf623","0xa8b4c251feff511d74c314adc893169880919227","0x26bf772117844cbaa3bcef7d038308f59adc92f4","0x87db74dc8f36d1cada0ee7cde5beb898dc41dd12","0x87e5bf4abe7f656a99cd9534badee10605212d24","0xc78fa4e18eb3c79805da64c387192ff0b1089372","0x5e6ed55cc045c5f1d95532edca7dafbcfe7435b4","0x634a235880bcc25844b43d0089b33eac063ada5a","0xc75e497eb836c010d7831696a83867384d46f49a","0x20840b831add95b40bb91b800292293fa8f58906","0xf8d68cfca57096ef665f3f5cb8d311b1fe9b3779","0xba06a9685aac487b9d820fa16ace3682d9999e64","0xc2a25a24304ac9b296657fbdc8dc79f2069e9de2","0xa433773b65ad045d666e9e9b31a32c747bf36bb2","0xa1c977656f68e1ee2733ff43b83529af2a5ae7c9","0xc0bf2492b753c10eb3c7f584f8f5c667e1e5a3f5","0x3950109fa34151b033bbc2990cb5f432f8cabf76","0x0c0a1646e383c3a6adb09299759904c52372a69b","0x1faea00e6859dfbbc80ea6fc768f1ea340d181bc","0x04c9faf00aff05a85f9cf5ee5ede7f6b627a0bad","0xc65d551a0b6c9ba65f07b4a93c930910af2e9b17","0xbe5a16b5e24d144920075a099880c48b89aa9213","0xd85323aeab29047cb22e9cb31967a25c5a26af8c","0x9e147b5a6d118c7ac17d20112c2c81116418d6e8","0xb896cbfac31d07cd6073b28973dfcde66e3d90b3","0x6a77f3cfc57dffb8dc82be3249aa5a6b979626ac","0xac795db158acb80fb14cebfa1167539f93ff0f05","0x211ebaf8002ca3fb894343bab4fdb5ae8fc03ad5","0x82a31cfdf65a9b793416f02735b96cdc8a4a88dd","0x9b81da10ed590aad669122fa8481f4659ae6cfe5","0x19927cb5ce72677297314c70f38416466cf6c781","0x75d26ee81e6642c4ed5df10b03c6649fd3f7629b","0xb76fddfdfa6a0f00213e5ea8491e103729eb1add","0xfdc52064fc8077bc48ad307bae41aebb050a58f2","0xdb84080e7d2b4654a7e384de851a6cf7281643de","0xd3fb544b9ce4725733e1b1e4114ce228b624099f","0x2dc155cb107b0ebe621b927f9e7acc4819e79a0d","0xf9f5c74acf5a20f1e91aeb057fd7f13db305a0e6","0x9b37f2d97fc0e46145a0d011898de40fe1704c3b","0x0e16cc94ed11d3aff0115ce07a1ac3f57e08b67b","0x2871b7d22693ca922eb497b945d84fdd9c786aae","0xd08e09d072ee853b8a6f331d94e0c15ee2d3d8ed","0xfd142fb8e80a95f25ff5e75960781649bb858e39","0xe70f662d7d47acb03c7d733e23af1534eed82733","0xc3f1384c2250075004a731140e481bf1a5b8e0d8","0x51ca152db75570bd7df12f99891129468802c0be","0xbc5ef46af65b986e83609d0ab2c386b4e8f110c0","0x131bfd89a99361fe2bd7078d7e83303f59a818f0","0xa946c3fbd53adc29e56ba32b1a289c135434bff4","0x6e1986c7d405884e5d13031d33ed981b160bac50","0xdd1f9b83507327f29c2c1bb42011fad5fb482dc6"]}}
6 changes: 0 additions & 6 deletions codegen-squid-schema.yaml

This file was deleted.

19 changes: 0 additions & 19 deletions codegen.yml

This file was deleted.

4 changes: 4 additions & 0 deletions commands.json
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,10 @@
"open": {
"description": "Open a local browser window",
"cmd": ["npx", "--yes", "opener"]
},
"preload:apps": {
"description": "Preload the address of applications created by CartesiDAppFactory",
"cmd": ["npx", "--yes", "ts-node", "./preloaders/applicationLoader"]
}
}
}
110 changes: 80 additions & 30 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@
"sqd:deploy": "sqd deploy -o cartesi -m",
"sqd:deploy:mainnet": "run-s \"sqd:deploy -- {@} .\" -- \"squid-mainnet.yaml\"",
"sqd:deploy:sepolia": "run-s \"sqd:deploy -- {@} .\" -- \"squid-sepolia.yaml\"",
"preload:apps": "run-p preload:apps:mainnet preload:apps:sepolia",
"preload:apps:mainnet": "CHAIN_ID=1 sqd preload:apps",
"preload:apps:sepolia": "CHAIN_ID=11155111 sqd preload:apps",
"tsc": "tsc",
"process:prod": "node deploy/run --npmScriptName=sqd:process:prod",
"serve:prod": "node deploy/run --npmScriptName=sqd:graphql:prod"
Expand All @@ -31,6 +34,7 @@
"@cartesi/rollups": "1.0.0",
"@subsquid/archive-registry": "^3.2.0",
"@subsquid/evm-processor": "^1.4.0",
"@subsquid/file-store": "^2.0.0",
"@subsquid/graphql-server": "^4.2.0",
"@subsquid/logger": "^1.3.0",
"@subsquid/typeorm-migration": "^1.2.0",
Expand Down
110 changes: 110 additions & 0 deletions preloaders/ApplicationLoader.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
import { EvmBatchProcessor } from '@subsquid/evm-processor';
import { Database, LocalDest } from '@subsquid/file-store';
import { createLogger } from '@subsquid/logger';
import {
events as CartesiDAppFactory,
events,
} from '../src/abi/CartesiDAppFactory';
import { CartesiDAppFactoryAddress, getConfig } from '../src/config';

type Metadata = {
height: number;
hash: string;
addresses: Record<string, string[]>;
};

const logger = createLogger('sqd:preloader:application');

if (!process.env.CHAIN_ID) {
logger.error(
'Looks like the CHAIN_ID environment is not set. The supported chain ids are [1, 11155111]',
);
throw new Error('ChainId is required to preload the application addresses');
}

const chainId = parseInt(process.env.CHAIN_ID ?? 0);
const config = getConfig(chainId);

logger.info(`Processing chain_id: ${chainId}`);

const processor = new EvmBatchProcessor()
.setDataSource(config.dataSource)
.useArchiveOnly(true)
.setFinalityConfirmation(config.finalityConfirmation ?? 10)
.setFields({
log: {
topics: true,
},
})
.setBlockRange({
from: config.from,
})
.addLog({
address: [CartesiDAppFactoryAddress],
topic0: [CartesiDAppFactory.ApplicationCreated.topic],
});

const appFilename = `applications-${chainId}.json` as const;

let applications: string[] = [];
let isInit = false;

const database = new Database({
tables: {},
dest: new LocalDest('./assets'),
chunkSizeMb: Infinity,
hooks: {
async onStateRead(dest) {
if (await dest.exists(appFilename)) {
let { height, hash, addresses }: Metadata = await dest
.readFile(appFilename)
.then(JSON.parse);

if (!isInit) {
applications = addresses[CartesiDAppFactoryAddress];
isInit = true;
}

return { height, hash };
} else {
return undefined;
}
},
async onStateUpdate(dest, info) {
let metadata: Metadata = {
...info,
addresses: {
[CartesiDAppFactoryAddress]: applications,
},
};

await dest.writeFile(appFilename, JSON.stringify(metadata));
},
},
});

processor.run(database, async (ctx) => {
for (const block of ctx.blocks) {
for (const log of block.logs) {
if (
log.address === CartesiDAppFactoryAddress &&
log.topics[0] === events.ApplicationCreated.topic
) {
const { application } = events.ApplicationCreated.decode(log);
const id = application.toLowerCase();

applications.push(id);
ctx.log.info(`${id} (Application) preloaded`);
}
}
}

ctx.store.setForceFlush(true);

if (ctx.isHead) {
ctx.log.info(
`Block header for chain-id ${chainId} reached. Finishing preloader`,
);
process.exit();
}
});
Loading

0 comments on commit d37fa6d

Please sign in to comment.