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

feat: add sessions to trustless gateways #459

Merged
merged 17 commits into from
Apr 4, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
chore: pr comments
  • Loading branch information
achingbrain committed Feb 6, 2024
commit 5836dcdec0c26fd62a64d8c4776df2dbd53a33c9
2 changes: 1 addition & 1 deletion packages/block-brokers/src/bitswap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ class BitswapBlockBroker implements BlockBroker<ProgressOptions<BitswapWantBlock
this.started = false
}

announce (cid: CID, block: Uint8Array, options?: ProgressOptions<BitswapNotifyProgressEvents>): void {
async announce (cid: CID, block: Uint8Array, options?: ProgressOptions<BitswapNotifyProgressEvents>): Promise<void> {
this.bitswap.notify(cid, block, options)
}

Expand Down
2 changes: 1 addition & 1 deletion packages/interface/src/blocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ export interface BlockBroker<GetProgressOptions extends ProgressOptions = Progre
/**
* Make a new block available to peers
*/
announce?(cid: CID, block: Uint8Array, options?: NotifyProgressOptions): void
announce?(cid: CID, block: Uint8Array, options?: NotifyProgressOptions): Promise<void>

/**
* Create a new session
Expand Down
34 changes: 19 additions & 15 deletions packages/utils/src/utils/networked-storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import type { AwaitIterable } from 'interface-store'
import type { CID } from 'multiformats/cid'
import type { MultihashHasher } from 'multiformats/hashes/interface'

export interface NetworkedStorageStorageInit {
export interface NetworkedStorageInit {
root?: CID
}

Expand Down Expand Up @@ -41,7 +41,7 @@ export class NetworkedStorage implements Blocks, Startable {
/**
* Create a new BlockStorage
*/
constructor (components: NetworkedStorageComponents, init: NetworkedStorageStorageInit = {}) {
constructor (components: NetworkedStorageComponents, init: NetworkedStorageInit = {}) {
this.log = components.logger.forComponent(`helia:networked-storage${init.root == null ? '' : `:${init.root}`}`)
this.logger = components.logger
this.child = components.blockstore
Expand Down Expand Up @@ -79,9 +79,13 @@ export class NetworkedStorage implements Blocks, Startable {

options.onProgress?.(new CustomProgressEvent<CID>('blocks:put:providers:notify', cid))

this.blockBrokers.forEach(broker => {
broker.announce?.(cid, block, options)
})
await Promise.all(
this.blockBrokers.map(broker => broker.announce?.(cid, block, options))
)

await Promise.all(
this.blockBrokers.map(broker => broker.announce?.(cid, block, options))
)

options.onProgress?.(new CustomProgressEvent<CID>('blocks:put:blockstore:put', cid))

Expand All @@ -102,11 +106,11 @@ export class NetworkedStorage implements Blocks, Startable {
return !has
})

const notifyEach = forEach(missingBlocks, ({ cid, block }): void => {
const notifyEach = forEach(missingBlocks, async ({ cid, block }): Promise<void> => {
options.onProgress?.(new CustomProgressEvent<CID>('blocks:put-many:providers:notify', cid))
this.blockBrokers.forEach(broker => {
broker.announce?.(cid, block, options)
})
await Promise.all(
this.blockBrokers.map(broker => broker.announce?.(cid, block, options))
)
})

options.onProgress?.(new CustomProgressEvent('blocks:put-many:blockstore:put-many'))
Expand All @@ -129,9 +133,9 @@ export class NetworkedStorage implements Blocks, Startable {

// notify other block providers of the new block
options.onProgress?.(new CustomProgressEvent<CID>('blocks:get:providers:notify', cid))
this.blockBrokers.forEach(broker => {
broker.announce?.(cid, block, options)
})
await Promise.all(
this.blockBrokers.map(broker => broker.announce?.(cid, block, options))
)

return block
}
Expand Down Expand Up @@ -160,9 +164,9 @@ export class NetworkedStorage implements Blocks, Startable {

// notify other block providers of the new block
options.onProgress?.(new CustomProgressEvent<CID>('blocks:get-many:providers:notify', cid))
this.blockBrokers.forEach(broker => {
broker.announce?.(cid, block, options)
})
await Promise.all(
this.blockBrokers.map(broker => broker.announce?.(cid, block, options))
)
}
}))
}
Expand Down