Skip to content

Commit

Permalink
VUL-120 storage watching: mapping (address=> uint)
Browse files Browse the repository at this point in the history
* VUL-120 Storage watching init commit

* VUL-120 Remove web3.js

* VUL-120 Create table for state

* VUL-120 Sava state to db

* VUL-120 Get contract by address hash

* VUL-126 Watch for fixed lenth slots (sync old data)

* VUL-115 Preimage table in contract watcher

* VUL-115 Preimage table in contract watcher

* Rework test (#29)

* remove old path for tests

* add mock for store

* add tests for dataService

* add full tests for processEvent and processState

Co-authored-by: Ilnur Galiev <[email protected]>

* VUL-120 Save data to db

* add contract parser

* add functions toStructure and toFields

Co-authored-by: Ilnur Galiev <[email protected]>
  • Loading branch information
reenko and n0cte authored Nov 19, 2020
1 parent f89509c commit a550c44
Show file tree
Hide file tree
Showing 31 changed files with 1,651 additions and 215 deletions.
1 change: 1 addition & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,4 @@ GRAPHQL_URI=http://localhost:5000

ENABLE_EVENT_WATCHER=true
ENABLE_HEADER_WATCHER=true
ENABLE_STORAGE_WATCHER=true
40 changes: 0 additions & 40 deletions __tests__/dataServicePgTypeTest .ts

This file was deleted.

140 changes: 0 additions & 140 deletions __tests__/dataServiceTest.ts

This file was deleted.

3 changes: 0 additions & 3 deletions __tests__/zeroTest.ts

This file was deleted.

5 changes: 3 additions & 2 deletions config.example.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,6 @@
uri = "http://localhost:5000"

[watcher]
event = true
header =true
event = true
header = true
storage = true
5 changes: 5 additions & 0 deletions package-lock.json

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

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
"node-fetch": "^2.6.1",
"postgraphile": "^4.9.0",
"react": "^16.13.1",
"solidity-parser-diligence": "^0.4.18",
"subscriptions-transport-ws": "^0.9.18",
"ts-node": "^8.10.2",
"typeorm": "^0.2.26",
Expand Down
83 changes: 83 additions & 0 deletions src/__mocks__/store.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
import Address from 'models/data/address';
import Contract from '../models/contract/contract';

export const contractsByAddrHash = {
"emptyStateLeafKey": null,
"someStateLeafKey": {
contractId: 1,
address: "0xAddress",
}
};

export const eventsByContractId = {
1: [
],
3: [{
name: 'ename'
}],
};

export const statesByContractId = {
1: [{
slot: "slot1",
type: "uint",
}, {
slot: "slot2",
type: "uint",
}]
};

export const mockGetContractByAddressHash = jest.fn().mockImplementation(function (addrHash: string) {
return contractsByAddrHash[addrHash];
});

export const mockGetStatesByContractId = jest.fn().mockImplementation(function (contractId: number) {
return statesByContractId[contractId];
});

export const mockGetContracts = jest.fn().mockImplementation(function (): Contract[] {
return [
{ address: 'address1' } as Contract,
{ address: 'address2' } as Contract,
{
address: 'address3',
contractId: 3,
events: [1],
abi: [{
name: 'ename',
inputs: [],
}],
} as Contract,
]
});

export const mockGetEventsByContractId = jest.fn().mockImplementation(function (contractId: number) {
return eventsByContractId[contractId];
});

export const mockGetAddressById = jest.fn().mockImplementation(function (addressId: number): Address {
return null;
});

export const mockGetAddress = jest.fn().mockImplementation(function (addressString: string): Address {
return {
addressId: 0,
address: addressString,
hash: ''
};
});

export const mockGetStore = jest.fn().mockImplementation(() => {
return {
getContractByAddressHash: mockGetContractByAddressHash,
getStatesByContractId: mockGetStatesByContractId,
getContracts: mockGetContracts,
getEventsByContractId: mockGetEventsByContractId,
getAddressById: mockGetAddressById,
getAddress: mockGetAddress
}
});

export default {
getStore: mockGetStore
};
3 changes: 3 additions & 0 deletions src/env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,9 @@ export default envalid.cleanEnv(
ENABLE_HEADER_WATCHER: envalid.bool({
default: tomlConfig?.watcher?.header
}),
ENABLE_STORAGE_WATCHER: envalid.bool({
default: tomlConfig?.watcher?.storage
}),

},
{ strict: true }
Expand Down
21 changes: 21 additions & 0 deletions src/migrations/1604551153589-StateConfig.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import {MigrationInterface, QueryRunner} from "typeorm";

export class StateConfig1604551153589 implements MigrationInterface {
name = 'StateConfig1604551153589'

public async up(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(`CREATE TABLE "contract"."state" (
"state_id" SERIAL NOT NULL,
"slot" integer NOT NULL,
"type" character varying NOT NULL,
CONSTRAINT "states_pk" PRIMARY KEY ("state_id")
)`);
await queryRunner.query(`ALTER TABLE "contract"."contracts" ADD "states" integer array`);
}

public async down(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(`ALTER TABLE "contract"."contracts" DROP COLUMN "states"`);
await queryRunner.query(`DROP TABLE "contract"."state"`);
}

}
31 changes: 31 additions & 0 deletions src/migrations/1604987750715-StateProgress.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import {MigrationInterface, QueryRunner} from "typeorm";

export class StateProgress1604987750715 implements MigrationInterface {

public async up(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(`
create table if not exists data.state_progress
(
state_progress_id serial not null
constraint state_progress_pk
primary key,
state_id integer not null
constraint state_progress_states_state_id_fk
references contract.state,
contract_id integer not null
constraint state_progress_contracts_contract_id_fk
references contract.contracts,
block_number integer not null
);
comment on table data.state_progress is 'Sync state progress';
comment on column data.state_progress.state_id is 'State ID';
comment on column data.state_progress.contract_id is 'Contract ID';
comment on column data.state_progress.block_number is 'Number of Block';
`);
}

public async down(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(`DROP TABLE "data"."state_progress"`);
}

}
25 changes: 25 additions & 0 deletions src/migrations/1605072677007-Addresses.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import {MigrationInterface, QueryRunner} from "typeorm";

export class Addresses1605072677007 implements MigrationInterface {

public async up(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(`
create table if not exists data.addresses
(
address_id serial not null
constraint addresses_pk
primary key,
address varchar not null,
hash varchar not null
);
comment on column data.addresses.address_id is 'PK';
comment on column data.addresses.address is 'Contract address';
comment on column data.addresses.hash is 'Keccak hash';
`);
}

public async down(queryRunner: QueryRunner): Promise<void> {
await queryRunner.dropTable('data.addresses');
}

}
Loading

0 comments on commit a550c44

Please sign in to comment.