Skip to content

Commit

Permalink
add chopsticks provider (#456)
Browse files Browse the repository at this point in the history
  • Loading branch information
qiweiii authored Oct 20, 2023
1 parent b642649 commit c0714a0
Show file tree
Hide file tree
Showing 15 changed files with 678 additions and 521 deletions.
4 changes: 4 additions & 0 deletions packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,5 +47,9 @@
"default": "./lib/*.js"
},
"./package.json": "./package.json"
},
"browser": {
"./lib/wasm-executor/node-wasm-executor.mjs": "./lib/wasm-executor/browser-wasm-executor.mjs",
"./lib/wasm-executor/node-worker.js": "./lib/wasm-executor/browser-worker.js"
}
}
10 changes: 5 additions & 5 deletions packages/core/src/blockchain/head-state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import _ from 'lodash'
import { Block } from './block'
import { defaultLogger } from '../logger'

type Callback = (block: Block, pairs: [string, string][]) => void | Promise<void>
type Callback = (block: Block, pairs: [string, string | null][]) => void | Promise<void>

export const randomId = () => Math.random().toString(36).substring(2)

Expand All @@ -13,7 +13,7 @@ const logger = defaultLogger.child({ name: 'head-state' })
export class HeadState {
#headListeners: Record<string, (block: Block) => void | Promise<void>> = {}
#storageListeners: Record<string, [string[], Callback]> = {}
#oldValues: Record<string, string | undefined> = {}
#oldValues: Record<string, string | null> = {}

#head: Block

Expand All @@ -36,7 +36,7 @@ export class HeadState {
this.#storageListeners[id] = [keys, cb]

for (const key of keys) {
this.#oldValues[key] = await this.#head.get(key)
this.#oldValues[key] = await this.#head.get(key).then((val) => val || null)
}

return id
Expand All @@ -50,7 +50,7 @@ export class HeadState {
const id = randomId()
const codeKey = stringToHex(':code')
this.#storageListeners[id] = [[codeKey], cb]
this.#oldValues[codeKey] = await this.#head.get(codeKey)
this.#oldValues[codeKey] = await this.#head.get(codeKey).then((val) => val || null)
return id
}

Expand All @@ -72,7 +72,7 @@ export class HeadState {
const diff = await this.#head.storageDiff()

for (const [keys, cb] of Object.values(this.#storageListeners)) {
const changed = keys.filter((key) => diff[key]).map((key) => [key, diff[key]] as [string, string])
const changed = keys.filter((key) => diff[key]).map((key) => [key, diff[key]] as [string, string | null])
if (changed.length > 0) {
try {
await cb(head, changed)
Expand Down
1 change: 1 addition & 0 deletions packages/core/src/blockchain/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -515,6 +515,7 @@ export class Blockchain {
*/
async close() {
await releaseWorker()
await this.api.disconnect()
await this.db?.destroy()
}
}
Loading

0 comments on commit c0714a0

Please sign in to comment.