Skip to content

Commit

Permalink
Merge pull request #117 from orbs-network/block-time
Browse files Browse the repository at this point in the history
Block time
  • Loading branch information
uv-orbs authored Oct 13, 2024
2 parents e07bbe1 + e5579f5 commit f40fec4
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 2 deletions.
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -69,3 +69,9 @@ typings/

# version
.version

# code generated
status/

# IDE
.vscode/
55 changes: 55 additions & 0 deletions src/ethereum/ethereum-reader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,14 @@ import { Contract, PastEventOptions } from 'web3-eth-contract';
import { toNumber, DailyStats } from '../helpers';
import { EventName, contractByEventName, getAbiForContract } from './types';
import pThrottle from 'p-throttle';
import fetch from 'node-fetch';
import https from 'https';

const HTTP_TIMEOUT_SEC = 20;

const subDomain = 'eth-api'
const domain = 'orbs.network'

export type EthereumConfiguration = {
EthereumEndpoint: string;
EthereumRequestsPerSecondLimit: number;
Expand All @@ -14,9 +19,16 @@ export type EthereumConfiguration = {
export class EthereumReader {
private web3: Web3;
private throttled?: pThrottle.ThrottledFunction<[], void>;
private agent: https.Agent;
private blockTimeSinceFail: number;

public requestStats = new DailyStats();

constructor(config: EthereumConfiguration) {
this.agent = new https.Agent({
maxSockets: 5,
});
this.blockTimeSinceFail = 0;
this.web3 = new Web3(
new Web3.providers.HttpProvider(config.EthereumEndpoint, {
keepAlive: true,
Expand All @@ -34,7 +46,50 @@ export class EthereumReader {
return this.web3.eth.getBlockNumber();
}

// orbs GET api dediated to serve block time from cache
// for faster sync time


async getBlockTime(blockNumber: number): Promise<number | null> {
const url = `https://${subDomain}.${domain}/api/blocktime?block=${blockNumber}`
try {
const res = await fetch(url, {
timeout: HTTP_TIMEOUT_SEC * 1000,
agent: this.agent,
headers:{
"x-module":"management-service"
}
});
if (res.status >= 400){
console.error("getBlockTime fetch Error status", res.status)
return null;
}
const text = await res.text();
const time = Number(text)
if(isNaN(time)){
return null;
}
return time;
}catch(err){
console.error("getBlockTime Error:", err)
return null;
}
}
async getRefTime(blockNumber: number | 'latest'): Promise<number> {

// get from cache first
const shouldTry = (this.blockTimeSinceFail == 0 || this.blockTimeSinceFail > 5)
if (blockNumber !== 'latest' && shouldTry){
this.blockTimeSinceFail = 0;
const blocktime = await this.getBlockTime(blockNumber)
if(blocktime)
return blocktime
}
console.log('getBlockTime failed', blockNumber)
// count calls web3 provider
this.blockTimeSinceFail++;

// fallback to web3
if (this.throttled) await this.throttled();
this.requestStats.add(1);
const block = await this.web3.eth.getBlock(blockNumber);
Expand Down
4 changes: 2 additions & 2 deletions src/ethereum/event-fetcher-lookahead.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ export class LookaheadEventFetcher extends EventFetcher {
protected autoscaleStreak = 0;

static DefaultAutoscaleOptions: AutoscaleOptions = {
initialPageSize: 100000,
maxPageSize: 1000000,
initialPageSize: 500000,
maxPageSize: 5000000,
minPageSize: 1000,
pageGrowFactor: 2,
pageGrowAfter: 5,
Expand Down

0 comments on commit f40fec4

Please sign in to comment.