Skip to content

Commit

Permalink
implemented tilecache
Browse files Browse the repository at this point in the history
  • Loading branch information
Caspar Oostendorp committed Jun 19, 2024
1 parent 4cec902 commit 7f485ce
Show file tree
Hide file tree
Showing 22 changed files with 740 additions and 141 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# dev
assets/tiles

# App specific
web/
.idea
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# server
NodeExpress backend for "bots" and other optimizations

Version 0.3.10
Version 0.3.11
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.3.10
0.3.11
Binary file added assets/drawing.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
11 changes: 7 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
{
"name": "pixelaw-server",
"description": "NodeExpress backend for \"bots\" and other optimizations",
"version": "0.3.10",
"version": "0.3.11",
"main": "index.js",
"license": "MIT",
"scripts": {
"old": "ts-node src/old.ts",
"tilegen": "ts-node src/cli/generateTiles.ts assets/drawing.png 100 1",
"server": "ts-node src/index.ts",
"queuehandler": "ts-node src/QueueHandler/index.ts",
"selector": "tsx utils/selector.ts",
Expand All @@ -15,7 +15,10 @@
"@apollo/client": "^3.8.4",
"@aws-sdk/client-s3": "^3.418.0",
"@types/fs-extra": "^11.0.4",
"@types/pngjs": "^6.0.5",
"@types/ws": "^8.5.10",
"abi-wan-kanabi": "^2.2.2",
"cors": "^2.8.5",
"dotenv": "^16.3.1",
"express": "^4.19.2",
"fs-extra": "^11.2.0",
Expand All @@ -28,8 +31,8 @@
"postinstall-postinstall": "^2.1.0",
"sqlite": "^5.1.1",
"sqlite3": "^5.1.7",
"starknet": "5.24.3",
"ws": "^8.15.0"
"starknet": "^6.10.0",
"ws": "^8.17.0"
},
"devDependencies": {
"@types/node": "^20.7.0",
Expand Down
50 changes: 0 additions & 50 deletions patches/starknet+5.24.3.patch

This file was deleted.

6 changes: 3 additions & 3 deletions src/db.ts → src/QueueHandler/db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,17 @@ import fs from 'fs';
import path from 'path';

import {open} from 'sqlite'
import {QueueItem} from "./QueueHandler";
import {QueueItem} from "./index";


export class SqliteDb {
private db: Database;

constructor(private dbDir: string) {
constructor(private dbFile: string) {
}

async open(): Promise<void> {
const filename= `${this.dbDir}/server.sqlite`
const filename= `${this.dbFile}`

if (!fs.existsSync(filename)) {
this.db = await open({
Expand Down
42 changes: 19 additions & 23 deletions src/QueueHandler/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,16 @@ import {
num,
RpcProvider,
events, BigNumberish} from 'starknet';
import {SqliteDb} from "../db";
import {getAddresses} from "./getAddresses";
import {SqliteDb} from "./db";
import {getAddresses} from "../utils/getAddresses";
import { sleep } from '../utils/sleep';
import {Message} from "../types";

// Approx Blocktime so we can minimize submitting items early
const BLOCKTIME_MS = 3000
export const QUEUE_STARTED_KEY_EVENT = "0x1c4fa7f75d1ea055adccbf8f86b75224181a3036d672762185805e0b999ad65"
export const QUEUE_FINISHED_KEY_EVENT = "0x16c4dd771da9a5cb32846fbb15c1b614da08fb5267af2fcce902a4c416e76cf"

export type Message = {
cmd: string;
data: string
};

export interface QueueItem {
id: string,
timestamp: number,
Expand All @@ -33,37 +29,37 @@ let running = true
class QueueHandler {
nodeUrl: string
toriiUrl: string
dbName: string
storageDir: string
provider: RpcProvider
account: Account
coreAddress: string
worldAddress: string
coreContract: Contract
db: SqliteDb

constructor(
nodeUrl: string,
toriiUrl: string,
address: string,
private_key: string,
dbName: string
storageDir: string
) {
this.nodeUrl = nodeUrl
this.toriiUrl = toriiUrl
this.provider = new RpcProvider({nodeUrl})

this.toriiUrl = toriiUrl
this.storageDir = storageDir
this.account = new Account(
this.provider,
address,
private_key);
this.dbName = dbName
this.db = new SqliteDb(`${storageDir}/QueueHandler.sqlite` )
}


async getEvents() {

const db = new SqliteDb(this.dbName)
await db.open()
const lastProcessedBlocknumber = await db.getLastBlockNumber()
await this.db.open()
const lastProcessedBlocknumber = await this.db.getLastBlockNumber()
const {block_number: lastBlocknumber} = await this.provider.getBlockLatestAccepted()

if (lastProcessedBlocknumber == lastBlocknumber) return
Expand Down Expand Up @@ -93,7 +89,7 @@ class QueueHandler {
for(let event of parsedEvents){
if (event.hasOwnProperty("QueueScheduled")) {

await db.setQueueItemPending({
await this.db.setQueueItemPending({
id: num.toHex(<BigNumberish>event.QueueScheduled.id),
timestamp: Number(event.QueueScheduled.timestamp),
called_system: num.toHex(<BigNumberish>event.QueueScheduled.called_system),
Expand All @@ -102,11 +98,11 @@ class QueueHandler {
calldata: event.QueueScheduled.calldata.map(e => num.toHex(e))
})
} else if (event.hasOwnProperty("QueueProcessed")) {
await db.removeQueueItemPending(num.toHex(<BigNumberish>event.QueueProcessed.id))
await this.db.removeQueueItemPending(num.toHex(<BigNumberish>event.QueueProcessed.id))
}
}

const pendingFromDb = await db.getQueueItemPending(now)
const pendingFromDb = await this.db.getQueueItemPending(now)

const testedItems = []
for (const item of pendingFromDb) {
Expand All @@ -130,7 +126,7 @@ class QueueHandler {
} else {
try {
// This one fails, move it to error
await db.movePendingToError(item.id, e.message)
await this.db.movePendingToError(item.id, e.message)
} catch (ea) {
log(`movePendingToError db error: ${ea.message}`)
}
Expand Down Expand Up @@ -162,14 +158,14 @@ class QueueHandler {
}

// Mark the retrieved latest blocknumber as done
await db.setLastBlockNumber(lastBlocknumber)
await this.db.setLastBlockNumber(lastBlocknumber)

} catch (e) {
// TODO Handle this general error
console.error(e)
log(`getEvents failed: ${e.message}`)
} finally {
await db.close()
await this.db.close()
}
}

Expand All @@ -179,9 +175,9 @@ class QueueHandler {
toriiUrl: string,
address: string,
private_key: string,
dbName: string,
storageDir: string,
): Promise<QueueHandler> {
const handler = new QueueHandler(nodeUrl, toriiUrl, address, private_key, dbName);
const handler = new QueueHandler(nodeUrl, toriiUrl, address, private_key, storageDir);

const {worldAddress, coreAddress} = await getAddresses(toriiUrl)
handler.worldAddress = worldAddress
Expand Down
File renamed without changes.
50 changes: 50 additions & 0 deletions src/TileCacher/db.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import {Database} from 'sqlite';
import sqlite3 from 'sqlite3';
import fs from 'fs';
import path from 'path';

import {open} from 'sqlite'

export class SqliteDb {
private db: Database;

constructor(private dbFile: string) {
}

async open(): Promise<void> {
const filename= `${this.dbFile}`

if (!fs.existsSync(filename)) {
this.db = await open({
filename,
driver: sqlite3.Database
});

const schema = fs.readFileSync(path.join(__dirname, 'schema.sql'), 'utf-8');
await this.db.exec(schema);
} else {
this.db = await open({
filename,
driver: sqlite3.Database
});
}
}

async setLastBlockNumber(blockNumber: number): Promise<void> {
const query = 'UPDATE system SET data_number = ? WHERE name = ?';
await this.db.run(query, [blockNumber, 'last_blocknumber']);
}


async getLastBlockNumber(): Promise<number> {
const query = 'SELECT data_number FROM system WHERE name = ?';
const row = await this.db.get(query, ['last_blocknumber']);
if(!row) throw "last_blocknumber does not exist in db"
return row.data_number;
}


async close(): Promise<void> {
await this.db.close();
}
}
Loading

0 comments on commit 7f485ce

Please sign in to comment.