Implement peer package to send messages between peers (#279)

* Initial implementation of class and discoving other peers

* Implement peer connection and sending messages between peers

* Add react app and use peer package for broadcasting

* Maintain stream for each remote peer

* Refactor code in peer package

* Add serve package for react-app

* Add readme for running react app

* Add peer package readme

* Add logs for events with details

* Add a chat CLI using peer package (#280)

* Add a flag to instantiate Peer for nodejs

* Add a basic chat CLI using peer

* Add a signal server arg to chat CLI

* Add instructions for chat CLI

* Fix typescript and ESM issues after adding peer package (#282)

* Fix build issues in util package

* Update eslint TS plugins

* Scope react app package name

* Convert cli package back to CJS and dynamically import ESM

* Upgrade ts-node version

* Fix tests

* Setup a relay node and pubsub based discovery (#284)

* Add a script to setup a relay node

* Use pubsub based peer discovery

* Add peer multiaddr to connection log

* Catch relay node dial errors

* Increase discovery interval and dial all mutiaddr

* Add UI to display self peer ID and multiaddrs

* Add UI for displaying live remote connections

* Send js objects in peer broadcast messages

* Add react-peer package for using peer in react app

* Reduce disconnect frequency between peers (#287)

* Restrict number of max concurrent dials per peer

* Increase hop relay timeout to 1 day

* Self review changes

* Review changes

* Increase pubsub discovery interval and add steps to create a peer id  (#290)

* Increase pubsub discovery interval

* Disable autodial to avoid peer dials without protocol on a reconnect

* Add steps to create a peer id and use for relay node

* Add back dependency to run signalling server

* Avoid bootstrapping and dial to relay node directly

Co-authored-by: prathamesh0 <42446521+prathamesh0@users.noreply.github.com>
Co-authored-by: prathamesh0 <prathamesh.musale0@gmail.com>
This commit is contained in:
2023-01-10 20:10:27 +05:30
committed by GitHub
co-authored by prathamesh0 prathamesh
parent aa4a954330
commit cd29b47ecc
82 changed files with 17136 additions and 2475 deletions
+6 -5
View File
@@ -13,6 +13,7 @@ import {
FindConditions,
FindManyOptions,
In,
ObjectLiteral,
QueryRunner,
Repository,
SelectQueryBuilder
@@ -427,7 +428,7 @@ export class Database {
return event;
}
async getFrothyEntity<Entity> (queryRunner: QueryRunner, repo: Repository<Entity>, data: { blockHash: string, id: string }): Promise<{ blockHash: string, blockNumber: number, id: string }> {
async getFrothyEntity<Entity extends ObjectLiteral> (queryRunner: QueryRunner, repo: Repository<Entity>, data: { blockHash: string, id: string }): Promise<{ blockHash: string, blockNumber: number, id: string }> {
// Hierarchical query for getting the entity in the frothy region.
const heirerchicalQuery = `
WITH RECURSIVE cte_query AS
@@ -478,7 +479,7 @@ export class Database {
return { blockHash, blockNumber, id };
}
async getPrevEntityVersion<Entity> (queryRunner: QueryRunner, repo: Repository<Entity>, findOptions: { [key: string]: any }): Promise<Entity | undefined> {
async getPrevEntityVersion<Entity extends ObjectLiteral> (queryRunner: QueryRunner, repo: Repository<Entity>, findOptions: { [key: string]: any }): Promise<Entity | undefined> {
const { blockHash, blockNumber, id } = await this.getFrothyEntity(queryRunner, repo, findOptions.where);
if (id) {
@@ -491,7 +492,7 @@ export class Database {
return this.getLatestPrunedEntity(repo, findOptions.where.id, blockNumber + 1);
}
async getLatestPrunedEntity<Entity> (repo: Repository<Entity>, id: string, canonicalBlockNumber: number): Promise<Entity | undefined> {
async getLatestPrunedEntity<Entity extends ObjectLiteral> (repo: Repository<Entity>, id: string, canonicalBlockNumber: number): Promise<Entity | undefined> {
// Filter out latest entity from pruned blocks.
const entityInPrunedRegion = await repo.createQueryBuilder('entity')
.where('entity.id = :id', { id })
@@ -797,7 +798,7 @@ export class Database {
return repo.save(entity);
}
buildQuery<Entity> (
buildQuery<Entity extends ObjectLiteral> (
repo: Repository<Entity>,
selectQueryBuilder: SelectQueryBuilder<Entity>,
where: Where = {},
@@ -867,7 +868,7 @@ export class Database {
return selectQueryBuilder;
}
orderQuery<Entity> (
orderQuery<Entity extends ObjectLiteral> (
repo: Repository<Entity>,
selectQueryBuilder: SelectQueryBuilder<Entity>,
orderOptions: { orderBy?: string, orderDirection?: string },
+3 -3
View File
@@ -3,9 +3,9 @@ import { utils } from 'ethers';
const log = debug('vulcanize:eth');
function decodeInteger(value : string, defaultValue: BigInt): BigInt
function decodeInteger(value : string) : BigInt | undefined
function decodeInteger (value : string, defaultValue?: BigInt): BigInt | undefined {
function decodeInteger(value : string, defaultValue: bigint): bigint
function decodeInteger(value : string) : bigint | undefined
function decodeInteger (value : string, defaultValue?: bigint): bigint | undefined {
if (value === undefined || value === null || value.length === 0) return defaultValue;
if (value === '0x') return BigInt(0);
return BigInt(value);
+17 -16
View File
@@ -13,7 +13,8 @@ import {
QueryRunner,
Repository,
SelectQueryBuilder,
UpdateResult
UpdateResult,
ObjectLiteral
} from 'typeorm';
import { ColumnMetadata } from 'typeorm/metadata/ColumnMetadata';
import { RawSqlResultsToEntityTransformer } from 'typeorm/query-builder/transformer/RawSqlResultsToEntityTransformer';
@@ -93,7 +94,7 @@ export class GraphDatabase {
return this._baseDatabase.createTransactionRunner();
}
async getModelEntity<Entity> (repo: Repository<Entity>, whereOptions: any): Promise<Entity | undefined> {
async getModelEntity<Entity extends ObjectLiteral> (repo: Repository<Entity>, whereOptions: any): Promise<Entity | undefined> {
eventProcessingLoadEntityCount.inc();
const findOptions = {
@@ -165,7 +166,7 @@ export class GraphDatabase {
return repo.findOne(findOptions);
}
async getEntity<Entity> (entityName: string, id: string, blockHash?: string): Promise<Entity | undefined> {
async getEntity<Entity extends ObjectLiteral> (entityName: string, id: string, blockHash?: string): Promise<Entity | undefined> {
const queryRunner = this._conn.createQueryRunner();
try {
@@ -212,7 +213,7 @@ export class GraphDatabase {
return count > 0;
}
async getEntityWithRelations<Entity> (
async getEntityWithRelations<Entity extends ObjectLiteral> (
queryRunner: QueryRunner,
entityType: (new () => Entity),
id: string,
@@ -343,7 +344,7 @@ export class GraphDatabase {
return entityData;
}
async getEntities<Entity> (
async getEntities<Entity extends ObjectLiteral> (
queryRunner: QueryRunner,
entityType: new () => Entity,
relationsMap: Map<any, { [key: string]: any }>,
@@ -411,7 +412,7 @@ export class GraphDatabase {
return entities;
}
async getEntitiesGroupBy<Entity> (
async getEntitiesGroupBy<Entity extends ObjectLiteral> (
queryRunner: QueryRunner,
entityType: new () => Entity,
block: BlockHeight,
@@ -475,7 +476,7 @@ export class GraphDatabase {
return entities;
}
async getEntitiesDistinctOn<Entity> (
async getEntitiesDistinctOn<Entity extends ObjectLiteral> (
queryRunner: QueryRunner,
entityType: new () => Entity,
block: BlockHeight,
@@ -516,7 +517,7 @@ export class GraphDatabase {
`(${subQuery.getQuery()})`,
'latestEntities'
)
.setParameters(subQuery.getParameters());
.setParameters(subQuery.getParameters()) as SelectQueryBuilder<Entity>;
if (queryOptions.orderBy) {
selectQueryBuilder = this._baseDatabase.orderQuery(repo, selectQueryBuilder, queryOptions, 'subTable_');
@@ -539,7 +540,7 @@ export class GraphDatabase {
return entities as Entity[];
}
async getEntitiesSingular<Entity> (
async getEntitiesSingular<Entity extends ObjectLiteral> (
queryRunner: QueryRunner,
entityType: new () => Entity,
block: BlockHeight,
@@ -574,7 +575,7 @@ export class GraphDatabase {
return entities as Entity[];
}
async getEntitiesUnique<Entity> (
async getEntitiesUnique<Entity extends ObjectLiteral> (
queryRunner: QueryRunner,
entityType: new () => Entity,
block: BlockHeight,
@@ -676,7 +677,7 @@ export class GraphDatabase {
return selectQueryBuilder.getMany();
}
async getEntitiesLateral<Entity> (
async getEntitiesLateral<Entity extends ObjectLiteral> (
queryRunner: QueryRunner,
entityType: new () => Entity,
latestEntity: new () => any,
@@ -717,7 +718,7 @@ export class GraphDatabase {
return qb;
},
'result'
);
) as SelectQueryBuilder<Entity>;
selectQueryBuilder = this._baseDatabase.buildQuery(latestEntityRepo, selectQueryBuilder, where, 'latest');
@@ -1040,11 +1041,11 @@ export class GraphDatabase {
}
cacheUpdatedEntityByName (entityName: string, entity: any, pruned = false): void {
const repo = this._conn.getRepository(entityName);
const repo = this._conn.getRepository<ObjectLiteral>(entityName);
this.cacheUpdatedEntity(repo, entity, pruned);
}
cacheUpdatedEntity<Entity> (repo: Repository<Entity>, entity: any, pruned = false): void {
cacheUpdatedEntity<Entity extends ObjectLiteral> (repo: Repository<Entity>, entity: any, pruned = false): void {
const tableName = repo.metadata.tableName;
if (pruned) {
@@ -1204,8 +1205,8 @@ export class GraphDatabase {
}
async canonicalizeLatestEntity (queryRunner: QueryRunner, entityType: any, latestEntityType: any, entities: any[], blockNumber: number): Promise<void> {
const repo = queryRunner.manager.getRepository(entityType);
const latestEntityRepo = queryRunner.manager.getRepository(latestEntityType);
const repo = queryRunner.manager.getRepository<ObjectLiteral>(entityType);
const latestEntityRepo = queryRunner.manager.getRepository<ObjectLiteral>(latestEntityType);
await Promise.all(entities.map(async (entity: any) => {
// Get latest pruned (canonical) version for the given entity
+2 -2
View File
@@ -3,7 +3,7 @@ import path from 'path';
import fs from 'fs-extra';
import debug from 'debug';
import yaml from 'js-yaml';
import { DeepPartial, EntityTarget, InsertEvent, Repository, UpdateEvent } from 'typeorm';
import { DeepPartial, EntityTarget, InsertEvent, ObjectLiteral, Repository, UpdateEvent } from 'typeorm';
import { ColumnMetadata } from 'typeorm/metadata/ColumnMetadata';
import assert from 'assert';
import _ from 'lodash';
@@ -855,7 +855,7 @@ export const afterEntityInsertOrUpdate = async<Entity> (
.execute();
};
export function getLatestEntityFromEntity<Entity> (latestEntityRepo: Repository<Entity>, entity: any): Entity {
export function getLatestEntityFromEntity<Entity extends ObjectLiteral> (latestEntityRepo: Repository<Entity>, entity: any): Entity {
const latestEntityFields = latestEntityRepo.metadata.columns.map(column => column.propertyName);
return latestEntityRepo.create(_.pick(entity, latestEntityFields) as DeepPartial<Entity>);
}
+2 -2
View File
@@ -2,7 +2,7 @@
// Copyright 2021 Vulcanize, Inc.
//
import { Connection, DeepPartial, EntityTarget, FindConditions, FindManyOptions, QueryRunner } from 'typeorm';
import { Connection, DeepPartial, EntityTarget, FindConditions, FindManyOptions, ObjectLiteral, QueryRunner } from 'typeorm';
import { MappingKey, StorageLayout } from '@cerc-io/solidity-mapper';
import { EthClient } from '@cerc-io/ipld-eth-client';
@@ -182,7 +182,7 @@ export interface DatabaseInterface {
}
export interface GraphDatabaseInterface {
getEntity<Entity> (entity: (new () => Entity) | string, id: string, blockHash?: string): Promise<Entity | undefined>;
getEntity<Entity extends ObjectLiteral> (entity: (new () => Entity) | string, id: string, blockHash?: string): Promise<Entity | undefined>;
}
export interface GraphWatcherInterface {