-
Notifications
You must be signed in to change notification settings - Fork 43
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: metadata protocol #1732
Merged
Merged
feat: metadata protocol #1732
Changes from 15 commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
1e476bd
add proto
danisharora099 9b7a027
add rpc and interfaces
danisharora099 7e89ac4
add protocol implementation
danisharora099 e691aa7
update faulty proto def
danisharora099 78a093a
add rpc and interfaces
danisharora099 d76f62f
refactor implementation & write test
danisharora099 6524032
setup the metadata protocol as a service
danisharora099 e711edf
Merge branch 'master' into feat/metadata-protocol
danisharora099 3bc9140
fix cases where metadata service needs to be undefined
danisharora099 61f7e5a
remove redundant catch block
danisharora099 591e36e
remove addressed TODO
danisharora099 7c22554
update import path
danisharora099 0542728
log errors
danisharora099 deef497
remove redundant code from handling incoming metadata request
danisharora099 ca17b30
update tests
danisharora099 d53eddb
add test to check for active connections
danisharora099 cb43460
change expects
danisharora099 d0c9683
save remote peer's shard info after successful connection
danisharora099 ddb8e8e
Merge branch 'master' into feat/metadata-protocol
danisharora099 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
import type { PeerId } from "@libp2p/interface/peer-id"; | ||
import { IncomingStreamData } from "@libp2p/interface/stream-handler"; | ||
import type { IMetadata, Libp2pComponents, ShardInfo } from "@waku/interfaces"; | ||
import { proto_metadata } from "@waku/proto"; | ||
import { Logger } from "@waku/utils"; | ||
import all from "it-all"; | ||
import * as lp from "it-length-prefixed"; | ||
import { pipe } from "it-pipe"; | ||
import { Uint8ArrayList } from "uint8arraylist"; | ||
|
||
import { BaseProtocol } from "../base_protocol.js"; | ||
|
||
const log = new Logger("metadata"); | ||
|
||
export const MetadataCodec = "/vac/waku/metadata/1.0.0"; | ||
|
||
class Metadata extends BaseProtocol { | ||
private readonly shardInfo: ShardInfo; | ||
constructor(shardInfo: ShardInfo, libp2p: Libp2pComponents) { | ||
super(MetadataCodec, libp2p.components); | ||
this.shardInfo = shardInfo; | ||
void libp2p.registrar.handle(MetadataCodec, (streamData) => { | ||
void this.onRequest(streamData); | ||
}); | ||
} | ||
|
||
/** | ||
* Handle an incoming metadata request | ||
*/ | ||
private async onRequest(streamData: IncomingStreamData): Promise<void> { | ||
try { | ||
const encodedResponse = proto_metadata.WakuMetadataResponse.encode( | ||
this.shardInfo | ||
); | ||
|
||
await pipe([encodedResponse], lp.encode, streamData.stream); | ||
} catch (error) { | ||
log.error("Error handling metadata request", error); | ||
} | ||
} | ||
|
||
/** | ||
* Make a metadata query to a peer | ||
*/ | ||
async query(peerId: PeerId): Promise<ShardInfo> { | ||
const request = proto_metadata.WakuMetadataRequest.encode(this.shardInfo); | ||
|
||
const peer = await this.getPeer(peerId); | ||
|
||
const stream = await this.getStream(peer); | ||
|
||
const res = await pipe( | ||
[request], | ||
lp.encode, | ||
stream, | ||
lp.decode, | ||
async (source) => await all(source) | ||
); | ||
|
||
const bytes = new Uint8ArrayList(); | ||
res.forEach((chunk) => { | ||
bytes.append(chunk); | ||
}); | ||
const response = proto_metadata.WakuMetadataResponse.decode(bytes); | ||
if (!response) log.error("Error decoding metadata response"); | ||
|
||
return response as ShardInfo; | ||
} | ||
} | ||
|
||
export function wakuMetadata( | ||
shardInfo: ShardInfo | ||
): (components: Libp2pComponents) => IMetadata { | ||
return (components: Libp2pComponents) => new Metadata(shardInfo, components); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import type { PeerId } from "@libp2p/interface/peer-id"; | ||
|
||
import type { ShardInfo } from "./enr.js"; | ||
import type { IBaseProtocol } from "./protocols.js"; | ||
|
||
export interface IMetadata extends IBaseProtocol { | ||
query(peerId: PeerId): Promise<ShardInfo | undefined>; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
syntax = "proto3"; | ||
|
||
|
||
message WakuMetadataRequest { | ||
optional uint32 cluster_id = 1; | ||
repeated uint32 shards = 2; | ||
} | ||
|
||
message WakuMetadataResponse { | ||
optional uint32 cluster_id = 1; | ||
repeated uint32 shards = 2; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,147 @@ | ||
/* eslint-disable import/export */ | ||
/* eslint-disable complexity */ | ||
/* eslint-disable @typescript-eslint/no-namespace */ | ||
/* eslint-disable @typescript-eslint/no-unnecessary-boolean-literal-compare */ | ||
/* eslint-disable @typescript-eslint/no-empty-interface */ | ||
|
||
import { encodeMessage, decodeMessage, message } from 'protons-runtime' | ||
import type { Codec } from 'protons-runtime' | ||
import type { Uint8ArrayList } from 'uint8arraylist' | ||
|
||
export interface WakuMetadataRequest { | ||
clusterId?: number | ||
shards: number[] | ||
} | ||
|
||
export namespace WakuMetadataRequest { | ||
let _codec: Codec<WakuMetadataRequest> | ||
|
||
export const codec = (): Codec<WakuMetadataRequest> => { | ||
if (_codec == null) { | ||
_codec = message<WakuMetadataRequest>((obj, w, opts = {}) => { | ||
if (opts.lengthDelimited !== false) { | ||
w.fork() | ||
} | ||
|
||
if (obj.clusterId != null) { | ||
w.uint32(8) | ||
w.uint32(obj.clusterId) | ||
} | ||
|
||
if (obj.shards != null) { | ||
for (const value of obj.shards) { | ||
w.uint32(16) | ||
w.uint32(value) | ||
} | ||
} | ||
|
||
if (opts.lengthDelimited !== false) { | ||
w.ldelim() | ||
} | ||
}, (reader, length) => { | ||
const obj: any = { | ||
shards: [] | ||
} | ||
|
||
const end = length == null ? reader.len : reader.pos + length | ||
|
||
while (reader.pos < end) { | ||
const tag = reader.uint32() | ||
|
||
switch (tag >>> 3) { | ||
case 1: | ||
obj.clusterId = reader.uint32() | ||
break | ||
case 2: | ||
obj.shards.push(reader.uint32()) | ||
break | ||
default: | ||
reader.skipType(tag & 7) | ||
break | ||
} | ||
} | ||
|
||
return obj | ||
}) | ||
} | ||
|
||
return _codec | ||
} | ||
|
||
export const encode = (obj: Partial<WakuMetadataRequest>): Uint8Array => { | ||
return encodeMessage(obj, WakuMetadataRequest.codec()) | ||
} | ||
|
||
export const decode = (buf: Uint8Array | Uint8ArrayList): WakuMetadataRequest => { | ||
return decodeMessage(buf, WakuMetadataRequest.codec()) | ||
} | ||
} | ||
|
||
export interface WakuMetadataResponse { | ||
clusterId?: number | ||
shards: number[] | ||
} | ||
|
||
export namespace WakuMetadataResponse { | ||
let _codec: Codec<WakuMetadataResponse> | ||
|
||
export const codec = (): Codec<WakuMetadataResponse> => { | ||
if (_codec == null) { | ||
_codec = message<WakuMetadataResponse>((obj, w, opts = {}) => { | ||
if (opts.lengthDelimited !== false) { | ||
w.fork() | ||
} | ||
|
||
if (obj.clusterId != null) { | ||
w.uint32(8) | ||
w.uint32(obj.clusterId) | ||
} | ||
|
||
if (obj.shards != null) { | ||
for (const value of obj.shards) { | ||
w.uint32(16) | ||
w.uint32(value) | ||
} | ||
} | ||
|
||
if (opts.lengthDelimited !== false) { | ||
w.ldelim() | ||
} | ||
}, (reader, length) => { | ||
const obj: any = { | ||
shards: [] | ||
} | ||
|
||
const end = length == null ? reader.len : reader.pos + length | ||
|
||
while (reader.pos < end) { | ||
const tag = reader.uint32() | ||
|
||
switch (tag >>> 3) { | ||
case 1: | ||
obj.clusterId = reader.uint32() | ||
break | ||
case 2: | ||
obj.shards.push(reader.uint32()) | ||
break | ||
default: | ||
reader.skipType(tag & 7) | ||
break | ||
} | ||
} | ||
|
||
return obj | ||
}) | ||
} | ||
|
||
return _codec | ||
} | ||
|
||
export const encode = (obj: Partial<WakuMetadataResponse>): Uint8Array => { | ||
return encodeMessage(obj, WakuMetadataResponse.codec()) | ||
} | ||
|
||
export const decode = (buf: Uint8Array | Uint8ArrayList): WakuMetadataResponse => { | ||
return decodeMessage(buf, WakuMetadataResponse.codec()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The metadata request contains the shardinfo from the remote node. We should save it locally.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes, that part was intended be tracked part of #1684 but addressed it here and actually makes more sense! thanks:) d0c9683