Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

improve references #468

Merged
merged 2 commits into from
Oct 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 30 additions & 12 deletions packages/core/src/blockchain/block.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export class Block {
#chain: Blockchain

#header?: Header | Promise<Header>
#parentBlock?: Block | Promise<Block | undefined>
#parentBlock?: WeakRef<Block> | Promise<Block | undefined>
#extrinsics?: HexString[] | Promise<HexString[]>

#wasm?: Promise<HexString>
Expand Down Expand Up @@ -70,21 +70,27 @@ export class Block {
},
) {
this.#chain = chain
this.#parentBlock = parentBlock
this.#parentBlock = parentBlock ? new WeakRef(parentBlock) : undefined
this.#header = block?.header
this.#extrinsics = block?.extrinsics
this.#baseStorage = block?.storage ?? new RemoteStorageLayer(chain.api, hash, chain.db)
this.#storages = []

this.#runtimeVersion = parentBlock?.runtimeVersion
this.#metadata = parentBlock?.metadata
this.#registry = parentBlock?.registry
this.#meta = parentBlock?.meta

const storageDiff = block?.storageDiff

if (storageDiff) {
// if code doesn't change then reuse parent block's meta
if (!storageDiff?.[stringToHex(':code')]) {
this.#runtimeVersion = parentBlock?.runtimeVersion
this.#metadata = parentBlock?.metadata
this.#registry = parentBlock?.registry
this.#meta = parentBlock?.meta
// if code doesn't change then keep parent block's meta
// otherwise reset meta
if (storageDiff[stringToHex(':code')]) {
this.#runtimeVersion = undefined
this.#metadata = undefined
this.#registry = undefined
this.#meta = undefined
}

this.pushStorageLayer().setAll(storageDiff)
Expand Down Expand Up @@ -116,12 +122,24 @@ export class Block {
return this.#extrinsics
}

get parentBlock(): undefined | Block | Promise<Block | undefined> {
get parentBlock(): Promise<Block | undefined> {
if (this.number === 0) {
return undefined
return Promise.resolve(undefined)
}

const getBlock = async (header: Header | Promise<Header>) => {
const _header = await header
const block = await this.#chain.getBlock(_header.parentHash.toHex())
if (block) this.#parentBlock = new WeakRef(block)
return block
}
if (!this.#parentBlock) {
this.#parentBlock = Promise.resolve(this.header).then((h) => this.#chain.getBlock(h.parentHash.toHex()))

if (this.#parentBlock instanceof WeakRef) {
const block = this.#parentBlock.deref()
if (block) return Promise.resolve(block)
this.#parentBlock = getBlock(this.header)
} else if (!this.#parentBlock) {
this.#parentBlock = getBlock(this.header)
}
return this.#parentBlock
}
Expand Down
17 changes: 9 additions & 8 deletions packages/core/src/blockchain/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ export class Blockchain {

#head: Block
readonly #blocksByNumber: Map<number, Block> = new Map()
readonly #blocksByHash: Record<string, Block> = {}
readonly #blocksByHash: Map<string, Block> = new Map()
readonly #loadingBlocks: Record<string, Promise<void>> = {}

/** For subscribing and managing the head state. */
Expand Down Expand Up @@ -137,11 +137,12 @@ export class Blockchain {
#registerBlock(block: Block) {
// if exceed max memory block count, delete the oldest block
if (this.#blocksByNumber.size === this.#maxMemoryBlockCount) {
const firstKey = this.#blocksByNumber.keys().next().value
this.#blocksByNumber.delete(firstKey)
const { hash, number }: Block = this.#blocksByNumber.values().next().value
this.#blocksByNumber.delete(number)
this.#blocksByHash.delete(hash)
}
this.#blocksByNumber.set(block.number, block)
this.#blocksByHash[block.hash] = block
this.#blocksByHash.set(block.hash, block)
}

get head(): Block {
Expand Down Expand Up @@ -189,7 +190,7 @@ export class Blockchain {
if (blockData) {
const { hash, number, header, extrinsics } = blockData
const parentHash = blockData.parentHash || undefined
let parentBlock = parentHash ? this.#blocksByHash[parentHash] : undefined
let parentBlock = parentHash ? this.#blocksByHash.get(parentHash) : undefined
if (!parentBlock) {
parentBlock = await this.getBlock(parentHash)
}
Expand Down Expand Up @@ -241,7 +242,7 @@ export class Blockchain {
if (hash == null) {
hash = this.head.hash
}
if (!this.#blocksByHash[hash]) {
if (!this.#blocksByHash.has(hash)) {
const loadingBlock = this.#loadingBlocks[hash]
if (loadingBlock) {
await loadingBlock
Expand All @@ -266,7 +267,7 @@ export class Blockchain {
delete this.#loadingBlocks[hash]
}
}
return this.#blocksByHash[hash]
return this.#blocksByHash.get(hash)
}

/**
Expand All @@ -286,7 +287,7 @@ export class Blockchain {
if (this.#blocksByNumber.get(block.number)?.hash === block.hash) {
this.#blocksByNumber.delete(block.number)
}
delete this.#blocksByHash[block.hash]
this.#blocksByHash.delete(block.hash)
// delete from db
if (this.db) {
await this.db.deleteBlock(block.hash)
Expand Down
3 changes: 1 addition & 2 deletions packages/core/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@
"compilerOptions": {
"outDir": "lib",
"rootDir": "src",
"target": "es2016",
"lib": ["es6", "dom", "dom.iterable"],
"lib": ["es2021", "dom", "dom.iterable"],
"isolatedModules": true
},
"include": ["src/**/*"],
Expand Down
3 changes: 1 addition & 2 deletions packages/db/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@
"compilerOptions": {
"outDir": "lib",
"rootDir": "src",
"target": "es2016",
"lib": ["es6", "dom", "dom.iterable"],
"lib": ["es2021", "dom", "dom.iterable"],
"isolatedModules": true
},
"include": ["src/**/*.ts"],
Expand Down
4 changes: 2 additions & 2 deletions packages/web-test/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
"jsx": "react-jsx",
"outDir": "lib",
"rootDir": "src",
"target": "es2016",
"target": "es2021",
"module": "ESNext",
"lib": ["es6", "dom", "dom.iterable"],
"lib": ["es2021", "dom", "dom.iterable"],
"sourceMap": true
},
"include": ["src/**/*"],
Expand Down
4 changes: 2 additions & 2 deletions tsconfig.base.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@
"compilerOptions": {
"allowSyntheticDefaultImports": true,
"esModuleInterop": true,
"lib": ["esnext"],
"lib": ["es2021"],
"module": "CommonJS",
"moduleResolution": "node",
"noUnusedLocals": true,
"noUnusedParameters": true,
"noImplicitAny": false,
"target": "esnext",
"target": "es2021",
"skipLibCheck": true,
"strict": true,
"declaration": true,
Expand Down
3 changes: 1 addition & 2 deletions tsconfig.lint.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
{
"extends": "./tsconfig.json",
"compilerOptions": {
"module": "es2020",
"jsx": "react-jsx",
"lib": ["dom"]
"lib": ["es2021", "dom", "dom.iterable"]
}
}