Comments (2)
Something like this. This allow constant time add and purge block data and usually identify tx data in 1 or 2 lookup.
interface BlockData {
blockNumber: number
block: Block
}
interface BucketData {
bucket: number
blocks: BlockData[]
transactions: Record<string, Tranasction> // txhash => tx
}
const BUCKET_SIZE = 5
export class UnfinalizedBlockCache {
buckets: BucketData[] = []
txpool: Record<string, Tranasction> = {}
addBlock(block: Block) {
const bucket = Math.floor(block.number / BUCKET_SIZE)
const startBucket = this.buckets[0]?.bucket
const idx = bucket - startBucket
this.buckets[idx] = this.buckets[idx] ?? { bucket, blocks: [], transactions: {} }
const bucketData = {
bucket
// other fields
}
this.buckets[idx].blocks[block.number % BUCKET_SIZE] = bucketData
// update this.buckets[idx].transactions
}
updateFinalizedHead(blockNumber: number) {
const bucket = Math.floor(blockNumber / BUCKET_SIZE)
const startBucket = this.buckets[0]?.bucket
if (startBucket != null && bucket > startBucket) {
const idx = bucket - startBucket
this.buckets.splice(0, idx + 1) // make sure this can handle holes
}
}
queryTransactionByHash(hash: string) {
for (const i = this.buckets.length - 1; i >= 0; --i ) {
const bucketData = this.buckets[i] // search from latest bucket
const tx = bucketData.transactions[hash]
if (tx) {
return tx
}
}
if (this.txpool[hash]) {
return this.txpool[hash]
}
// not found
// update txpool and search it again
}
}
function createUnfinalizedBlockCache(api: ApiPromise) {
const cache = new UnfinalizedBlockCache()
let lastProcessedBlock = undefined
// subscribe to new head
// retry on error
// while lastProcessedBlock !== undefiled && lastProcessedBlock < latestBlockNumber
// let block = getBlock(lastProcessedBlock + 1)
// cache.addBlock(block)
// lastProcessedBlock = block.number
// subscribe to finalized haed
// retry on error
// lastProcessedBlock = max(lastProcessedBlock, blockNumber)
// cache.updateFinalizedHead(blockNumber)
return cache
}
from bodhi.js.
closes with #131
from bodhi.js.
Related Issues (20)
- tutorials/examples with ethers v6
- `viem` example
- eth_estimateGas should revert with small gasLimit HOT 1
- polish tests
- getlogs without subql
- throw proper error msg when there is no fallback function
- error msg polish
- make dd-trace optional deps for eth-providers
- use yarn4
- Add timeout parameter HOT 1
- integrate viem example to CI
- decode failed on karura mainnet
- `Error: Cannot read properties of undefined` querying evm rpc endpoint HOT 7
- estimateGas doesn't throw error HOT 1
- blockscout v2
- subql-node fails HOT 1
- Failed to get tx receipt HOT 4
- unable to decode some error msg
- resync the subql to fix the forked data HOT 1
- Update `subql/node` and `subql/query` versions
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from bodhi.js.