From dd7902906ea6996550c344a985bf18de8cf7ccc0 Mon Sep 17 00:00:00 2001 From: Junxiao Shi Date: Sun, 10 Mar 2024 02:58:58 +0000 Subject: [PATCH] svs: decouple Component from StateVector codec --- pkg/svs/src/state-vector.ts | 28 ++++++++++++++++++---- pkg/svs/src/sync.ts | 46 ++++++++++++++++++------------------- 2 files changed, 46 insertions(+), 28 deletions(-) diff --git a/pkg/svs/src/state-vector.ts b/pkg/svs/src/state-vector.ts index 74de5450..8bdb4343 100644 --- a/pkg/svs/src/state-vector.ts +++ b/pkg/svs/src/state-vector.ts @@ -83,7 +83,7 @@ export class StateVector { return o; } - /** Encode TLV-VALUE of name component. */ + /** Encode TLV-VALUE only. */ public encodeTo(encoder: Encoder): void { const list = Array.from(this); list.sort(([a], [b]) => -a.compare(b)); @@ -95,12 +95,15 @@ export class StateVector { } } - /** Encode to name component. */ + /** + * Encode to name component. + * @deprecated No longer supported. + */ public toComponent(): Component { return new Component(TT.StateVector, Encoder.encode(this)); } - /** Decode TLV-VALUE of name component. */ + /** Decode TLV-VALUE only. */ public static decodeFrom(decoder: Decoder): StateVector { const vv = new StateVector(); while (!decoder.eof) { @@ -115,7 +118,10 @@ export class StateVector { return vv; } - /** Decode from name component. */ + /** + * Decode from name component. + * @deprecated No longer supported. + */ public static fromComponent(comp: Component): StateVector { if (comp.type !== TT.StateVector) { throw new Error("unexpected NameComponent TLV-TYPE"); @@ -125,7 +131,19 @@ export class StateVector { } export namespace StateVector { - /** TLV-TYPE of name component. */ + /** + * StateVector TLV-TYPE. + * + * @remarks + * SVS v1 encodes StateVector as a name component of this type. + * SVS v2 encodes StateVector as a sub-element of this type within AppParameters. + */ + export const Type = TT.StateVector; + + /** + * TLV-TYPE of name component. + * @deprecated Use {@link Type}. + */ export const NameComponentType = TT.StateVector; export interface DiffEntry { diff --git a/pkg/svs/src/sync.ts b/pkg/svs/src/sync.ts index e72931ab..b18477de 100644 --- a/pkg/svs/src/sync.ts +++ b/pkg/svs/src/sync.ts @@ -1,8 +1,9 @@ import { consume, type ConsumerOptions, type Endpoint, produce, type Producer, type ProducerHandler } from "@ndn/endpoint"; import { Forwarder } from "@ndn/fw"; -import { Interest, Name, type NameLike, nullSigner, type Signer, type Verifier } from "@ndn/packet"; +import { Component, Interest, Name, type NameLike, nullSigner, type Signer, type Verifier } from "@ndn/packet"; import { type SyncNode, type SyncProtocol, SyncUpdate } from "@ndn/sync-api"; -import { CustomEvent, randomJitter, trackEventListener } from "@ndn/util"; +import { Decoder, Encoder } from "@ndn/tlv"; +import { assert, CustomEvent, randomJitter, trackEventListener } from "@ndn/util"; import type { Promisable } from "type-fest"; import { TypedEventTarget } from "typescript-event-target"; @@ -25,25 +26,22 @@ type EventMap = SyncProtocol.EventMap & { /** StateVectorSync participant. */ export class SvSync extends TypedEventTarget implements SyncProtocol { public static async create({ - fw, - describe, + syncPrefix, endpoint, // eslint-disable-line etc/no-deprecated + fw = endpoint?.fw ?? Forwarder.getDefault(), + describe = `SvSync(${syncPrefix})`, initialStateVector = new StateVector(), initialize, - syncPrefix, syncInterestLifetime = 1000, steadyTimer = [30000, 0.1], suppressionTimer = [200, 0.5], signer = nullSigner, verifier, }: SvSync.Options): Promise { - fw ??= endpoint?.fw ?? Forwarder.getDefault(); - describe ??= `SvSync(${syncPrefix})`; - const sync = new SvSync( + syncPrefix, describe, initialStateVector, - syncPrefix, Interest.makeModifyFunc({ canBePrefix: true, mustBeFresh: true, @@ -57,18 +55,18 @@ export class SvSync extends TypedEventTarget implements SyncProtocol number, @@ -153,7 +151,9 @@ export class SvSync extends TypedEventTarget implements SyncProtocol { await this.verifier?.verify(interest); - const recv = StateVector.fromComponent(interest.name.at(this.syncPrefix.length)); + const vComp = interest.name.at(this.syncPrefix.length); + assert(vComp.type === StateVector.Type, "name component is not a StateVector"); + const recv = new Decoder(vComp.value).decode(StateVector); const ourOlder = this.own.listOlderThan(recv); const ourNewer = recv.listOlderThan(this.own); @@ -208,7 +208,7 @@ export class SvSync extends TypedEventTarget implements SyncProtocol Promisable; - /** Sync group prefix. */ - syncPrefix: Name; - /** * Sync Interest lifetime in milliseconds. * @defaultValue 1000