Skip to content

Commit

Permalink
Validate moduleHashByChain
Browse files Browse the repository at this point in the history
fixes: #19
  • Loading branch information
DenisCarriere committed Oct 8, 2023
1 parent 8d0668a commit 9de310a
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 11 deletions.
15 changes: 8 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,14 @@ Send Text Message with the following JSON payloads

## REST API

| Pathname | Description |
|-------------------|-----------------------|
| GET `/health` | Health check
| GET `/metrics` | Prometheus metrics
| GET `/chain` | Returns all available `chain`
| GET `/traceId` | Returns all `traceId` by `chain`
| GET `/moduleHash` | Returns all available `moduleHash` by `chain`
| Pathname | Description |
|---------------------------|-----------------------|
| GET `/health` | Health check
| GET `/metrics` | Prometheus metrics
| GET `/chain` | Returns all available `chain`
| GET `/traceId` | Returns all `traceId` by `chain`
| GET `/moduleHash` | Returns all available `moduleHash`
| GET `/moduleHashByChain` | Returns all available `moduleHash` by `chain`
| POST `/` {timestamp, signature, body} | Webhook HTTP POST (Ed25519 signature)
| POST `/` {"message": "PING"} | Webhook HTTP POST Ping

Expand Down
1 change: 1 addition & 0 deletions src/GET.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export default async function (req: Request, server: Server) {
if ( pathname === "/health") return checkHealth();
if ( pathname === "/metrics") return new Response(await prometheus.registry.metrics());
if ( pathname === "/moduleHash") return toJSON(sqlite.selectAll(db, "moduleHash"));
if ( pathname === "/moduleHashByChain") return toJSON(sqlite.selectAll(db, "moduleHashByChain"));
if ( pathname === "/traceId") return toJSON(sqlite.selectAll(db, "traceId"));
if ( pathname === "/chain") return toJSON(sqlite.selectAll(db, "chain"));
return new Response("Not found", { status: 400 });
Expand Down
5 changes: 3 additions & 2 deletions src/POST.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,10 @@ export default async function (req: Request, server: Server) {
prometheus.trace_id.labels({traceId}).inc(1);

// Upsert moduleHash into SQLite DB
sqlite.replace(db, "moduleHash", moduleHash, timestamp);
sqlite.replace(db, "chain", chain, timestamp);
sqlite.replace(db, "traceId", traceId, timestamp);
sqlite.replace(db, "moduleHash", moduleHash, timestamp);
sqlite.replace(db, "moduleHashByChain", `${chain}:${moduleHash}`, timestamp);
sqlite.replace(db, "traceId", `${chain}:${traceId}`, timestamp);

return new Response("OK");
}
1 change: 1 addition & 0 deletions src/banner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export function banner() {
/health
/metrics
/moduleHash
/moduleHashByChain
/traceId
/chain
Expand Down
1 change: 1 addition & 0 deletions src/sqlite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export function createDb(filename: string) {

// create tables if does not exists
create(db, "moduleHash");
create(db, "moduleHashByChain");
create(db, "traceId");
create(db, "chain");
return db;
Expand Down
12 changes: 10 additions & 2 deletions src/websocket/subscribe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,16 @@ export default function (ws: ServerWebSocket<ServerWebSocketData>, params: {[key
const { moduleHash, chain } = params;
const topic = chain ? `${chain}:${moduleHash}` : moduleHash;
if ( ws.isSubscribed(topic) ) throw new Error(`Already subscribed to [${topic}] topic.`);
if ( !sqlite.exists(db, "moduleHash", moduleHash) ) throw new Error(`ModuleHash [${moduleHash}] not found.`);
if ( chain && !sqlite.exists(db, "chain", chain) ) throw new Error(`Chain [${chain}] not found.`);

// Subscribe to ModuleHash by Chain
if ( chain ) {
if ( !sqlite.exists(db, "chain", chain) ) throw new Error(`Chain [${chain}] not found.`);
if ( !sqlite.exists(db, "moduleHashByChain", topic) ) throw new Error(`ModuleHash [${moduleHash}] from Chain [${chain}] not found.`);

// Subscribe to all ModuleHash
} else {
if ( !sqlite.exists(db, "moduleHash", moduleHash) ) throw new Error(`ModuleHash [${moduleHash}] not found.`);
}
ws.subscribe(topic);
logger.info('subscribed', {id, key: ws.data.key, remoteAddress: ws.remoteAddress, topic, params});
}

0 comments on commit 9de310a

Please sign in to comment.