Skip to content

Commit

Permalink
Contracttest (#47)
Browse files Browse the repository at this point in the history
* fix saving storage for mapping

* increase pg connection limit

* fix merge issue

* add fk to address table from contract_address_slots table to be able to derive mapping storage by address

* fill address table and all related from events. Match addresses with storage

* fix tests

* fix linter warnings

* fix issue with duplicated FK

* check data format correct

* fix db connection leak
  • Loading branch information
ramilexe authored Apr 7, 2021
1 parent b1a917a commit 9da2fb4
Show file tree
Hide file tree
Showing 14 changed files with 560 additions and 276 deletions.
1 change: 1 addition & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ services:
POSTGRES_USER: "vdbm"
POSTGRES_DB: "vulcanize_public"
POSTGRES_PASSWORD: "password"
command: ["postgres", "-c", "log_statement=all", "-N", "500"]
volumes:
- contract_watcher_js_db_data:/var/lib/postgresql/data
ports:
Expand Down
5 changes: 5 additions & 0 deletions ormconfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ module.exports = {
'password': env.DATABASE_PASSWORD,
'database': env.DATABASE_NAME,
'logging': env.DATABASE_LOGGING,
extra: {
max: 100,
idleTimeoutMillis: 5000,
//log: console.log
},
"entities": process.env.NODE_ENV === 'production' ?
[__dirname + "/dist/models/*.js", __dirname + "/dist/models/**/*.js",] :
["src/models/*.ts", "src/models/**/*.ts"],
Expand Down
34 changes: 25 additions & 9 deletions src/repositories/data/addressIdSlotIdRepository.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {EntityRepository, QueryRunner, Table} from 'typeorm';
import { TableOptions } from 'typeorm/schema-builder/options/TableOptions';
import {Structure} from "../../services/dataTypeParser";

@EntityRepository()
export default class AddressIdSlotIdRepository {
Expand All @@ -9,12 +10,16 @@ export default class AddressIdSlotIdRepository {
this.queryRunner = queryRunner;
}

public async createTable(addressId, slotId): Promise<null> {
const tableName = `data.address_id_${addressId}_slot_id_${slotId}`;
private getTableName(contractId: number, slotId: number): string {
return `data.contract_id_${contractId}_address_slot_id_${slotId}`;
}

public async createTable(contractId: number, slotId: number): Promise<null> {
const tableName = this.getTableName(contractId, slotId);
const table = await this.queryRunner.getTable(tableName);

if (table) {
console.log(`Table ${tableName} already exists`);
// console.log(`Table ${tableName} already exists`);
return;
}

Expand All @@ -41,16 +46,16 @@ export default class AddressIdSlotIdRepository {
console.log('create new table', tableName);
}

public async add(cotractAddressId: number, addressId, slotId: number, hash: string): Promise<null> {
const tableName = `data.address_id_${cotractAddressId}_slot_id_${slotId}`;
public async add(contractId: number, addressId, slotId: number, hash: string): Promise<null> {
const tableName = this.getTableName(contractId, slotId);
const sql = `INSERT INTO ${tableName} (address_id, hash) VALUES (${addressId}, '${hash}');`;
console.log(sql);

return this.queryRunner.query(sql);
}

public async isExist(cotractAddressId: number, slotId: number, addressId: number): Promise<boolean> {
const tableName = `data.address_id_${cotractAddressId}_slot_id_${slotId}`;
public async isExist(contractId: number, slotId: number, addressId: number): Promise<boolean> {
const tableName = this.getTableName(contractId, slotId);
const sql = `SELECT * FROM ${tableName} WHERE address_id=${addressId};`;

const data = await this.queryRunner.query(sql);
Expand All @@ -61,8 +66,8 @@ export default class AddressIdSlotIdRepository {
return data[0]?.address_id ? true : false;
}

public async getAddressIdByHash(cotractAddressId: number, slotId: number, hash: string): Promise<number> {
const tableName = `data.address_id_${cotractAddressId}_slot_id_${slotId}`;
public async getAddressIdByHash(contractId: number, slotId: number, hash: string): Promise<number> {
const tableName = this.getTableName(contractId, slotId);
const sql = `SELECT * FROM ${tableName} WHERE hash='${hash}';`;

const data = await this.queryRunner.query(sql);
Expand All @@ -72,4 +77,15 @@ export default class AddressIdSlotIdRepository {

return data[0]?.address_id;
}

public async syncAddressSlotHashes(contractId: number, slotId: number, stateStructure: Structure): Promise<void> {
const sql = `UPDATE data.contract_id_${contractId}_state_id_${slotId} a
SET address_id=b.address_id
FROM data.contract_id_${contractId}_address_slot_id_${slotId} b
WHERE
a.address_id IS NULL
AND a.${stateStructure.name}=b.hash`;

return this.queryRunner.query(sql);
}
}
12 changes: 10 additions & 2 deletions src/repositories/data/addressRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,16 @@ export default class AddressRepository extends Repository<Address> {

public async add(address: string, hash: string): Promise<Address> {
return this.save({
address,
hash,
address: address.toLowerCase(),
hash: hash.toLowerCase(),
});
}

public async get(address: string): Promise<Address> {
return await this.findOne({
where: {
address: address.toLowerCase()
}
});
}
}
3 changes: 2 additions & 1 deletion src/repositories/data/eventRepository.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {EntityRepository, QueryRunner} from 'typeorm';
import {ABIInputData} from "../../types";

@EntityRepository()
export default class EventRepository {
Expand All @@ -8,7 +9,7 @@ export default class EventRepository {
this.queryRunner = queryRunner;
}

public async add(tableName: string, data): Promise<number> {
public async add(tableName: string, data: Array<ABIInputData & {isStrict?}>): Promise<number> {
const sql = `INSERT INTO ${tableName}
(${data.map((line) => line.isStrict ? line.name : 'data_' + line.name.toLowerCase().trim()).join(',')})
VALUES
Expand Down
10 changes: 10 additions & 0 deletions src/repositories/data/slotRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,16 @@ export default class SlotRepository {
this.queryRunner = queryRunner;
}

public async getByValue(table: string, name: string, value: string): Promise<number> {
const sql = `SELECT id FROM ${table} WHERE ${name}=$1`;
const res = await this.queryRunner.query(sql, [value]);
if (!res || !res.length) {
return null;
}

return res[0].id;
}

public async add(tableName: string, name: string[], value): Promise<number> {
const sql = `INSERT INTO ${tableName} (${name.join(',')}) VALUES ('${value.map((v) => v.toString().replace(/\0/g, '')).join('\',\'')}') RETURNING id;`;
console.log(sql);
Expand Down
8 changes: 7 additions & 1 deletion src/run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@ process.on('unhandledRejection', (reason, p) => {
dataService.processState(data.relatedNode)
},
processHeader: (data) => dataService.processHeader(data),
processEvent: (data) => dataService.processEvent(null, data.relatedNode, data.decoded, data.event),
processEvent: (data) => {
if (!data) {
return;
}

dataService.processEvent(null, data.relatedNode, data.decoded, data.event);
},
})
})();
3 changes: 2 additions & 1 deletion src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ const server = async function({
createConnection(connectionOptions).then(async () => {
const app = new App();

Store.init();
// @TODO autoUpdate=true breaks
Store.init(false);

const graphqlClient = new GraphqlClient(env.GRAPHQL_URI, ws);
const graphqlService = new GraphqlService(graphqlClient);
Expand Down
Loading

0 comments on commit 9da2fb4

Please sign in to comment.