*Refactor wisdom query to extract setupRelay to be a functional api
This commit is contained in:
parent
a8b0f36d16
commit
271a0c2911
|
@ -1,4 +1,5 @@
|
||||||
import {
|
import {
|
||||||
|
ClientRPCRequest,
|
||||||
RPCBroadcastRequest,
|
RPCBroadcastRequest,
|
||||||
RPCBroadcastResponse,
|
RPCBroadcastResponse,
|
||||||
RPCRequest,
|
RPCRequest,
|
||||||
|
@ -12,10 +13,10 @@ import {
|
||||||
validateResponse,
|
validateResponse,
|
||||||
validateTimestampedResponse,
|
validateTimestampedResponse,
|
||||||
} from "../util.js";
|
} from "../util.js";
|
||||||
import RPC from "@lumeweb/rpc";
|
|
||||||
import { blake2b } from "libskynet";
|
import { blake2b } from "libskynet";
|
||||||
import { ERR_INVALID_SIGNATURE, ERR_NO_RELAYS } from "../error.js";
|
import { ERR_INVALID_SIGNATURE, ERR_NO_RELAYS } from "../error.js";
|
||||||
import RpcQueryBase from "./base.js";
|
import RpcQueryBase from "./base.js";
|
||||||
|
import { getActiveRelay, setupRelay } from "../sharedRelay.js";
|
||||||
|
|
||||||
function flatHash(data: any) {
|
function flatHash(data: any) {
|
||||||
const flattenedData = flatten(data).sort();
|
const flattenedData = flatten(data).sort();
|
||||||
|
@ -26,18 +27,13 @@ function flatHash(data: any) {
|
||||||
|
|
||||||
export default class WisdomRpcQuery extends RpcQueryBase {
|
export default class WisdomRpcQuery extends RpcQueryBase {
|
||||||
protected declare _response?: RPCBroadcastResponse;
|
protected declare _response?: RPCBroadcastResponse;
|
||||||
private static _activeRelay: any;
|
protected declare _query: ClientRPCRequest;
|
||||||
|
|
||||||
static get activeRelay(): any {
|
|
||||||
return this._activeRelay;
|
|
||||||
}
|
|
||||||
|
|
||||||
get result(): Promise<RPCResponse> {
|
get result(): Promise<RPCResponse> {
|
||||||
return this._promise as Promise<RPCResponse>;
|
return this._promise as Promise<RPCResponse>;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected async _run(): Promise<void> {
|
protected async _run(): Promise<void> {
|
||||||
await this.setupRelay();
|
await setupRelay(this._network);
|
||||||
await this.queryRelay();
|
await this.queryRelay();
|
||||||
await this.checkResponse();
|
await this.checkResponse();
|
||||||
}
|
}
|
||||||
|
@ -57,7 +53,7 @@ export default class WisdomRpcQuery extends RpcQueryBase {
|
||||||
}
|
}
|
||||||
|
|
||||||
protected async queryRelay(): Promise<any> {
|
protected async queryRelay(): Promise<any> {
|
||||||
let activeRelay = WisdomRpcQuery.activeRelay;
|
let activeRelay = getActiveRelay();
|
||||||
let relays = this.getRelays();
|
let relays = this.getRelays();
|
||||||
|
|
||||||
if (!relays.length) {
|
if (!relays.length) {
|
||||||
|
@ -82,7 +78,8 @@ export default class WisdomRpcQuery extends RpcQueryBase {
|
||||||
|
|
||||||
if (
|
if (
|
||||||
!validateResponse(
|
!validateResponse(
|
||||||
WisdomRpcQuery.activeRelay.stream.remotePublicKey,
|
// @ts-ignore
|
||||||
|
getActiveRelay().stream.remotePublicKey,
|
||||||
this._response as RPCResponse
|
this._response as RPCResponse
|
||||||
)
|
)
|
||||||
) {
|
) {
|
||||||
|
@ -146,7 +143,7 @@ export default class WisdomRpcQuery extends RpcQueryBase {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
protected getRelays(): string[] | [] {
|
protected getRelays(): string[] {
|
||||||
if (
|
if (
|
||||||
this._network.maxRelays === 0 ||
|
this._network.maxRelays === 0 ||
|
||||||
this._network.relays.length <= this._network.maxRelays
|
this._network.relays.length <= this._network.maxRelays
|
||||||
|
@ -165,27 +162,4 @@ export default class WisdomRpcQuery extends RpcQueryBase {
|
||||||
|
|
||||||
return list;
|
return list;
|
||||||
}
|
}
|
||||||
|
|
||||||
private async setupRelay() {
|
|
||||||
let active = WisdomRpcQuery.activeRelay;
|
|
||||||
let relays = this._network.relays;
|
|
||||||
|
|
||||||
if (!active) {
|
|
||||||
if (!relays.length) {
|
|
||||||
throw new Error(ERR_NO_RELAYS);
|
|
||||||
}
|
|
||||||
|
|
||||||
let relay = relays[Math.floor(Math.random() * relays.length)];
|
|
||||||
let socket = this._network.dht.connect(b4a.from(relay, "hex"));
|
|
||||||
if (isPromise(socket)) {
|
|
||||||
socket = await socket;
|
|
||||||
}
|
|
||||||
await socket.opened;
|
|
||||||
|
|
||||||
WisdomRpcQuery._activeRelay = new RPC(socket);
|
|
||||||
socket.once("close", () => {
|
|
||||||
WisdomRpcQuery._activeRelay = undefined;
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,34 @@
|
||||||
|
import { ERR_NO_RELAYS } from "./error.js";
|
||||||
|
import b4a from "b4a";
|
||||||
|
import { isPromise } from "./util.js";
|
||||||
|
import RPC from "@lumeweb/rpc";
|
||||||
|
import RpcNetwork from "./network.js";
|
||||||
|
|
||||||
|
let activeRelay: RPC | undefined;
|
||||||
|
|
||||||
|
export async function setupRelay(network: RpcNetwork) {
|
||||||
|
const relays = network.relays;
|
||||||
|
|
||||||
|
if (!activeRelay) {
|
||||||
|
if (!relays.length) {
|
||||||
|
throw new Error(ERR_NO_RELAYS);
|
||||||
|
}
|
||||||
|
|
||||||
|
let relay = relays[Math.floor(Math.random() * relays.length)];
|
||||||
|
let socket = network.dht.connect(b4a.from(relay, "hex"));
|
||||||
|
if (isPromise(socket)) {
|
||||||
|
socket = await socket;
|
||||||
|
}
|
||||||
|
|
||||||
|
await socket.opened;
|
||||||
|
|
||||||
|
activeRelay = new RPC(socket);
|
||||||
|
socket.once("close", () => {
|
||||||
|
activeRelay = undefined;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export function getActiveRelay(): RPC {
|
||||||
|
return activeRelay as RPC;
|
||||||
|
}
|
Loading…
Reference in New Issue