Skip to content

Commit

Permalink
history: implement number>hash wrapper
Browse files Browse the repository at this point in the history
  • Loading branch information
ScottyPoi committed Sep 16, 2024
1 parent b4064dc commit dff9d32
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 5 deletions.
3 changes: 1 addition & 2 deletions packages/portalnetwork/src/client/eth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -191,8 +191,7 @@ export class ETH {
}
} else {
// Header not found by number. If block hash is known, search for header by hash
const blockIndex = await this.history!.blockIndex()
const blockHash = blockIndex.get('0x' + blockNumber.toString(16))
const blockHash = await this.history!.blockNumberToHash(BigInt(blockNumber))
if (blockHash !== undefined) {
return this.getBlockByHash(blockHash, includeTransactions)
}
Expand Down
5 changes: 2 additions & 3 deletions packages/portalnetwork/src/networks/history/history.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,8 @@ export class HistoryNetwork extends BaseNetwork {
public findContentLocally = async (contentKey: Uint8Array): Promise<Uint8Array | undefined> => {
const contentType = contentKey[0]
if (contentType === HistoryNetworkContentType.BlockHeaderByNumber) {
const blockNumber = '0x' + decodeHistoryNetworkContentKey(contentKey).keyOpt.toString(16)
const index = await this.blockIndex()
const blockHash = index.get(blockNumber)
const blockNumber = decodeHistoryNetworkContentKey(contentKey).keyOpt
const blockHash = await this.blockNumberToHash(<bigint>blockNumber)
if (blockHash === undefined) {
return undefined
}
Expand Down
11 changes: 11 additions & 0 deletions packages/portalnetwork/src/networks/network.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,17 @@ export abstract class BaseNetwork extends EventEmitter {
}
}

public async blockNumberToHash(blockNumber: bigint): Promise<string | undefined> {
const blockIndex = await this.blockIndex()
return blockIndex.get('0x' + blockNumber.toString(16))
}

public async blockHashToNumber(blockHash: string): Promise<bigint | undefined> {
const blockIndex = await this.blockIndex()
const blockNumber = blockIndex.get(blockHash)
return blockNumber === undefined ? undefined : BigInt(blockNumber)
}

public async put(contentKey: string, content: string) {
await this.db.put(contentKey, content)
}
Expand Down

0 comments on commit dff9d32

Please sign in to comment.