Giter Site home page Giter Site logo

Comments (2)

xlc avatar xlc commented on June 22, 2024 1

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.

shunjizhan avatar shunjizhan commented on June 22, 2024

closes with #131

from bodhi.js.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.