Skip to content

Commit dec9cb2

Browse files
authored
Merge pull request #21 from ora-io/dev
Dev
2 parents a304564 + 3b0a478 commit dec9cb2

File tree

15 files changed

+3755
-4062
lines changed

15 files changed

+3755
-4062
lines changed

.github/workflows/ci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,4 +87,4 @@ jobs:
8787
run: nr build
8888

8989
- name: Test
90-
run: nr test
90+
run: npx vitest

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "ora-stack",
3-
"version": "0.2.0",
3+
"version": "0.2.1",
44
"private": true,
55
"packageManager": "[email protected]",
66
"description": "",
@@ -47,7 +47,7 @@
4747
"publish": "nr prepublishOnly && esno scripts/publish.ts",
4848
"release": "esno scripts/release.ts",
4949
"start": "esno src/index.ts",
50-
"test": "vitest",
50+
"test": "DEBUG=DEBUG:ora-stack:* vitest",
5151
"typecheck": "tsc --noEmit"
5252
},
5353
"devDependencies": {

packages/orap/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "@ora-io/orap",
33
"type": "module",
4-
"version": "0.2.0",
4+
"version": "0.2.1",
55
"packageManager": "[email protected]",
66
"description": "",
77
"author": "Norman (nom4dv3), MuRong",

packages/orap/src/debug.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { debug as debugFn } from '@ora-io/utils'
2+
3+
export function debug(formatter: any, ...args: any[]) {
4+
return debugFn('orap')(formatter, ...args)
5+
}

packages/orap/src/flow/orap.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import type { Fn } from '@ora-io/utils'
33
import type { Interface, InterfaceAbi } from 'ethers'
44
import { OrapVerse } from '../verse/orap'
55
import type { EventSignalRegisterParams } from '../signal'
6+
import { debug } from '../debug'
67
import { EventFlow } from './event'
78
import type { Flow, HandleFn } from './interface'
89

@@ -35,6 +36,7 @@ export class OrapFlow implements Flow {
3536

3637
const eventFlow = new EventFlow(this, params, handler)
3738
this.subflows.event.push(eventFlow)
39+
debug('The event %s is registered, address: %s', params.eventName, params.address)
3840
return eventFlow
3941
}
4042

packages/orap/src/signal/event.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,8 +124,7 @@ export class EventSignal implements Signal {
124124
&& !(provider instanceof ethers.WebSocketProvider)
125125
)
126126
throw new Error('crosscheckProvider must be an instance of RekuProviderManager or ethers.JsonRpcProvider or ethers.WebSocketProvider')
127-
128127
this.crosschecker = new AutoCrossChecker(provider)
129-
await this.crosschecker.start(this.crosscheckerOptions)
128+
this.crosschecker.start(this.crosscheckerOptions)
130129
}
131130
}

packages/reku/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "@ora-io/reku",
33
"type": "module",
4-
"version": "0.2.0",
4+
"version": "0.2.1",
55
"packageManager": "[email protected]",
66
"description": "",
77
"author": "Norman (nom4dv3), MuRong",

packages/reku/src/debug.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { debug as debugFn } from '@ora-io/utils'
2+
3+
export function debug(formatter: any, ...args: any[]) {
4+
return debugFn('reku')(formatter, ...args)
5+
}

packages/reku/src/event/crosschecker/autochecker.ts

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import type { ethers } from 'ethers'
2-
import { polling, sleep, timeoutWithRetry } from '@ora-io/utils'
2+
import { polling, timeoutWithRetry } from '@ora-io/utils'
33
import { ETH_BLOCK_INTERVAL } from '../../constants'
44
import type { Providers } from '../../types/w3'
5+
import { debug } from '../../debug'
56
import { CrossCheckerCacheManager } from './cache/manager'
67
import type { AutoCrossCheckParam, CrossCheckRangeParam, SimpleLog } from './interface'
78
import { BaseCrossChecker } from './basechecker'
@@ -52,20 +53,21 @@ export class AutoCrossChecker extends BaseCrossChecker {
5253
* @param options
5354
*/
5455
async start(options: AutoCrossCheckParam) {
56+
debug('auto crosscheck start with options: %O', options)
5557
this.validate(options)
5658

5759
this.cache = new CrossCheckerCacheManager(options?.store, { keyPrefix: options?.storeKeyPrefix, ttl: options?.storeTtl })
5860

59-
const latestblocknum = await timeoutWithRetry(() => this.provider.provider?.getBlockNumber(), 15 * 1000, 3)
61+
let latestblocknum = await timeoutWithRetry(() => this.provider.provider?.getBlockNumber(), 15 * 1000, 3)
6062

6163
// resume checkpoint priority: options.fromBlock > cache > latestblocknum + 1
62-
const defaultInitCheckpoint = await this.cache.getCheckpoint() ?? (latestblocknum + 1)
64+
const defaultInitCheckpoint = await this.cache.getCheckpoint() ?? (latestblocknum)
6365

6466
const {
6567
fromBlock = defaultInitCheckpoint,
6668
batchBlocksCount = 10,
6769
pollingInterval = 3000,
68-
blockInterval = ETH_BLOCK_INTERVAL,
70+
// blockInterval = ETH_BLOCK_INTERVAL,
6971
delayBlockFromLatest = 1,
7072
toBlock,
7173
} = options
@@ -85,17 +87,20 @@ export class AutoCrossChecker extends BaseCrossChecker {
8587
}
8688

8789
const waitNextCrosscheck = async (): Promise<boolean> => {
88-
const latestblocknum = await timeoutWithRetry(() => this.provider.provider?.getBlockNumber(), 15 * 1000, 3)
90+
latestblocknum = await timeoutWithRetry(() => this.provider.provider?.getBlockNumber(), 15 * 1000, 3)
8991
if (ccrOptions.toBlock + delayBlockFromLatest > latestblocknum) {
9092
// sleep until the toBlock
91-
await sleep((ccrOptions.toBlock + delayBlockFromLatest - latestblocknum) * blockInterval)
93+
// await sleep((ccrOptions.toBlock + delayBlockFromLatest - latestblocknum) * blockInterval)
9294
return false
9395
}
9496
return true
9597
}
9698

9799
const waitOrUpdateToBlock = toBlock
98-
? () => { ccrOptions.toBlock = Math.min(ccrOptions.toBlock, toBlock); return true }
100+
? () => {
101+
ccrOptions.toBlock = Math.min(ccrOptions.toBlock, toBlock)
102+
return true
103+
}
99104
: waitNextCrosscheck
100105

101106
const updateCCROptions = async (ccrOptions: CrossCheckRangeParam) => {
@@ -113,13 +118,20 @@ export class AutoCrossChecker extends BaseCrossChecker {
113118
// never ends if options.toBlock is not provided
114119
: () => false
115120

121+
debug('crosscheck running')
122+
116123
// TODO: replace polling with schedule cron
117124
await polling(async () => {
118-
if (await waitOrUpdateToBlock()) {
125+
const wait = await waitOrUpdateToBlock()
126+
debug('polling interval: %d, wait: %s, from block: %d, to block: %d', pollingInterval, wait, ccrOptions.fromBlock, ccrOptions.toBlock)
127+
if (wait) {
119128
await this.crossCheckRange(ccrOptions)
120129
// only update options after cc succ
121130
await updateCCROptions(ccrOptions)
122131
}
132+
else {
133+
debug('Because the latest block %d is too old, skip this cross check', latestblocknum)
134+
}
123135
return endingCondition()
124136
}, pollingInterval)
125137
}

packages/reku/src/event/crosschecker/basechecker.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import type { ethers } from 'ethers'
22
import { timeoutWithRetry } from '@ora-io/utils'
33
import { ETH_BLOCK_COUNT_ONE_HOUR } from '../../constants'
44
import type { Providers } from '../../types/w3'
5+
import { debug } from '../../debug'
56
import type { CrossCheckFromParam, CrossCheckRangeParam, CrossCheckRetroParam, SimpleLog } from './interface'
67

78
export class BaseCrossChecker {
@@ -77,6 +78,13 @@ export class BaseCrossChecker {
7778
return block.number
7879
}
7980

81+
/**
82+
* @deprecated
83+
* @param logs
84+
* @param txHashList
85+
* @param logIndexList
86+
* @returns
87+
*/
8088
async diff_old(logs: ethers.Log[], txHashList: string[], logIndexList: number[][]): Promise<ethers.Log[]> {
8189
const missing = (log: ethers.Log) => {
8290
const txIndex = txHashList?.indexOf(log.transactionHash) || -1
@@ -105,6 +113,7 @@ export class BaseCrossChecker {
105113
async _crossCheck(options: CrossCheckRangeParam) {
106114
// get period logs
107115
const { fromBlock, toBlock, address, topics } = options
116+
debug('start crosscheck from %d to %d', fromBlock, toBlock)
108117
const params = {
109118
fromBlock,
110119
toBlock,
@@ -120,8 +129,10 @@ export class BaseCrossChecker {
120129
const missingLogs = ignoreLogs ? await this.diff(logs, ignoreLogs) : logs
121130

122131
// callback on missing logs
123-
for (const log of missingLogs)
132+
for (const log of missingLogs) {
133+
debug('missing tx hash: %s', log.transactionHash)
124134
await options.onMissingLog(log)
135+
}
125136
}
126137
}
127138
}

0 commit comments

Comments
 (0)