Skip to content

Commit

Permalink
refactor block confirm alg (#86)
Browse files Browse the repository at this point in the history
* refactor: refactor eth block confirm

* refactor: refactor ckbHandler block confirm

* fix: fix eth init right bound of getLogs

* fix: fix code merge

* fix: add limit to confirm

* fix: fix bug

* chore: close typeorm log

* fix: fix eth getLogs bound

* fix: fix updateCkbBurnConfirmStatus

* fix: fix watchNewBlock miss await bug

Co-authored-by: wangbing <wangbing@cryptape.com>
  • Loading branch information
solargatsby and wangbing committed May 24, 2021
1 parent 10cdd02 commit d6f248c
Show file tree
Hide file tree
Showing 13 changed files with 522 additions and 202 deletions.
5 changes: 3 additions & 2 deletions offchain-modules/config.json.example
Expand Up @@ -16,7 +16,7 @@
],
"multiSignThreshold": 2,
"contractAddress": "0x8326e1d621Cd32752920ed2A44B49bB1a96c7391",
"confirmNumber": 5,
"confirmNumber": 1,
"startBlockHeight": 1
},
"eos": {
Expand Down Expand Up @@ -100,7 +100,8 @@
}
}
},
"startBlockHeight": 1
"startBlockHeight": 1,
"confirmNumber": 1
},
"tron": {
"tronGridUrl": "https://api.shasta.trongrid.io",
Expand Down
1 change: 1 addition & 0 deletions offchain-modules/packages/x/src/config.ts
Expand Up @@ -28,6 +28,7 @@ export interface CkbConfig {
sudtType: ConfigItem;
};
startBlockHeight: number;
confirmNumber: number;
}

export interface EthConfig {
Expand Down
44 changes: 43 additions & 1 deletion offchain-modules/packages/x/src/db/ckb.ts
@@ -1,6 +1,7 @@
// invoke in ckb handler
import { Connection } from 'typeorm';
import { Connection, DeleteResult, UpdateResult } from 'typeorm';
import { ForceBridgeCore } from '../core';
import { dbTxStatus } from './entity/CkbMint';
import {
BtcUnlock,
CkbBurn,
Expand Down Expand Up @@ -57,6 +58,16 @@ export class CkbDb {
await this.connection.manager.save(records);
}

async updateCkbMintStatus(mintTxHash: string, status: dbTxStatus) {
return this.connection
.getRepository(CkbMint)
.createQueryBuilder()
.update()
.set({ status: status })
.where('mintHash = :mintTxHash', { mintTxHash: mintTxHash })
.execute();
}

async createCkbBurn(records: ICkbBurn[]): Promise<void> {
const ckbBurnRepo = this.connection.getRepository(CkbBurn);
const dbRecords = records.map((r) => ckbBurnRepo.create(r));
Expand Down Expand Up @@ -87,4 +98,35 @@ export class CkbDb {
const dbRecords = records.map((r) => btcUnlockRepo.create(r));
await btcUnlockRepo.save(dbRecords);
}

async removeUnconfirmedCkbBurn(confirmedBlockHeight: number): Promise<DeleteResult> {
return this.connection
.getRepository(CkbBurn)
.createQueryBuilder()
.delete()
.where('block_number > :blockNumber', { blockNumber: confirmedBlockHeight })
.execute();
}

async getUnconfirmedCkbBurnToConfirm(confirmedBlockHeight: number, limit = 100): Promise<CkbBurn[]> {
return this.connection
.getRepository(CkbBurn)
.createQueryBuilder()
.select()
.where('block_number <= :confirmedHeight And confirm_status = "unconfirmed"', {
confirmedHeight: confirmedBlockHeight,
})
.limit(limit)
.getMany();
}

async updateCkbBurnConfirmStatus(txHashes: string[]): Promise<UpdateResult> {
return this.connection
.getRepository(CkbBurn)
.createQueryBuilder()
.update()
.set({ confirmStatus: 'confirmed' })
.where('ckb_tx_hash in (:txHashes)', { txHashes: txHashes })
.execute();
}
}
4 changes: 4 additions & 0 deletions offchain-modules/packages/x/src/db/entity/CkbBurn.ts
@@ -1,4 +1,5 @@
import { Column, CreateDateColumn, Entity, Index, PrimaryColumn, UpdateDateColumn } from 'typeorm';
import { txConfirmStatus } from './EthLock';

@Entity()
export class CkbBurn {
Expand All @@ -25,6 +26,9 @@ export class CkbBurn {
@Column()
blockNumber: number;

@Column({ default: 'unconfirmed' })
confirmStatus: txConfirmStatus;

@CreateDateColumn()
createdAt: string;

Expand Down
5 changes: 5 additions & 0 deletions offchain-modules/packages/x/src/db/entity/EthLock.ts
@@ -1,5 +1,7 @@
import { Column, CreateDateColumn, Entity, Index, PrimaryColumn, UpdateDateColumn } from 'typeorm';

export type txConfirmStatus = 'unconfirmed' | 'confirmed';

@Entity()
export class EthLock {
@PrimaryColumn()
Expand Down Expand Up @@ -30,6 +32,9 @@ export class EthLock {
@Column()
blockHash: string;

@Column({ default: 'unconfirmed' })
confirmStatus: txConfirmStatus;

@CreateDateColumn()
createdAt: string;

Expand Down
14 changes: 14 additions & 0 deletions offchain-modules/packages/x/src/db/entity/kv.ts
@@ -0,0 +1,14 @@
import { Column, PrimaryGeneratedColumn, Entity, Index } from 'typeorm';

@Entity()
export class KV {
@PrimaryGeneratedColumn()
id: number;

@Index({ unique: true })
@Column()
key: string;

@Column()
value: string;
}
32 changes: 30 additions & 2 deletions offchain-modules/packages/x/src/db/eth.ts
@@ -1,5 +1,5 @@
// invoke in eth handler
import { Connection, Repository } from 'typeorm';
import { Connection, DeleteResult, Repository, UpdateResult } from 'typeorm';
import { ForceBridgeCore } from '../core';
import { EthUnlockStatus } from './entity/EthUnlock';
import { CkbBurn, CkbMint, EthLock, EthUnlock, ICkbMint, IEthLock, IQuery, LockRecord, UnlockRecord } from './model';
Expand Down Expand Up @@ -34,7 +34,35 @@ export class EthDb implements IQuery {
await this.ethLockRepository.save(dbRecords);
}

async getEthUnlockRecordsToUnlock(status: EthUnlockStatus, take = 100): Promise<EthUnlock[]> {
async removeUnconfirmedLocks(confirmedBlockHeight: number): Promise<DeleteResult> {
return this.ethLockRepository
.createQueryBuilder()
.delete()
.where('block_number > :blockNumber', { blockNumber: confirmedBlockHeight })
.execute();
}

async getUnconfirmedLocksToConfirm(confirmedBlockHeight: number, limit = 100): Promise<EthLock[]> {
return this.ethLockRepository
.createQueryBuilder()
.select()
.where('block_number <= :confirmedHeight And confirm_status = "unconfirmed"', {
confirmedHeight: confirmedBlockHeight,
})
.limit(limit)
.getMany();
}

async updateLockConfirmStatus(txHashes: string[]): Promise<UpdateResult> {
return this.ethLockRepository
.createQueryBuilder()
.update()
.set({ confirmStatus: 'confirmed' })
.where('tx_hash in (:txHashes)', { txHashes: txHashes })
.execute();
}

async getEthUnlockRecordsToUnlock(status: EthUnlockStatus, take = 1): Promise<EthUnlock[]> {
return this.ethUnlockRepository.find({
where: {
status,
Expand Down
3 changes: 3 additions & 0 deletions offchain-modules/packages/x/src/db/index.ts
@@ -1,3 +1,6 @@
export * from './ckb';
export * from './eth';
export * from './tron';
export * from './btc';
export * from './eos';
export * from './kv';
31 changes: 31 additions & 0 deletions offchain-modules/packages/x/src/db/kv.ts
@@ -0,0 +1,31 @@
import { Connection, Repository } from 'typeorm';
import { KV } from './entity/kv';

export class KVDb {
private kvDb: Repository<KV>;
constructor(private connection: Connection) {
this.kvDb = connection.getRepository(KV);
}
async get(key: string): Promise<string | undefined> {
const records = await this.kvDb.find({
where: {
key,
},
take: 1,
});
if (!records || records.length === 0) {
return undefined;
}
return records[0].value;
}

async set(key: string, value: string) {
return this.connection
.createQueryBuilder()
.insert()
.into(KV)
.values({ key: key, value: value })
.orUpdate({ conflict_target: ['key'], overwrite: ['value'] })
.execute();
}
}

0 comments on commit d6f248c

Please sign in to comment.