From 7bcb61159c8bc05ece4cf7ed309173803898e1d8 Mon Sep 17 00:00:00 2001 From: jowparks Date: Thu, 31 Aug 2023 11:26:17 -0700 Subject: [PATCH] chore: minor changes to client and server logic (#32) --- example/.env.example | 3 ++- example/src/Client/Client.ts | 2 +- example/src/index.ts | 4 ++-- src/services/LightStreamer.ts | 23 ++++++++++++++++++++--- test/server.test.slow.ts | 2 +- 5 files changed, 26 insertions(+), 8 deletions(-) diff --git a/example/.env.example b/example/.env.example index 54d42d0..5fb7b9c 100644 --- a/example/.env.example +++ b/example/.env.example @@ -1 +1,2 @@ -SPENDING_KEY="" \ No newline at end of file +SPENDING_KEY="" +WALLET_SERVER_HOST="walletserver.ironfish.network:50051" \ No newline at end of file diff --git a/example/src/Client/Client.ts b/example/src/Client/Client.ts index 5a42455..d1915c5 100644 --- a/example/src/Client/Client.ts +++ b/example/src/Client/Client.ts @@ -5,7 +5,7 @@ import { AccountsManager } from "./utils/AccountsManager"; import { BlockCache } from "./utils/BlockCache"; const client = new LightStreamerClient( - "localhost:50051", + process.env["WALLET_SERVER_HOST"] ?? "localhost:50051", credentials.createInsecure(), ); diff --git a/example/src/index.ts b/example/src/index.ts index 46ee8a6..59a72a8 100644 --- a/example/src/index.ts +++ b/example/src/index.ts @@ -1,8 +1,8 @@ import { config } from "dotenv"; -import { Client } from "./Client/Client"; - config(); +import { Client } from "./Client/Client"; + async function main() { const client = new Client(); await client.start(); diff --git a/src/services/LightStreamer.ts b/src/services/LightStreamer.ts index 79763c1..e0a0dbc 100644 --- a/src/services/LightStreamer.ts +++ b/src/services/LightStreamer.ts @@ -95,7 +95,10 @@ class LightStreamer implements LightStreamerServer { public getBlockRange: handleServerStreamingCall = async (call) => { - if (!call.request.start?.sequence || !call.request.end?.sequence) { + if ( + (!call.request.start?.sequence && call.request.start?.sequence !== 0) || + (!call.request.end?.sequence && call.request.end?.sequence !== 0) + ) { call.emit("error", { code: status.INVALID_ARGUMENT, details: "Must provide sequence for start and end", @@ -103,14 +106,28 @@ class LightStreamer implements LightStreamerServer { call.end(); return; } - if (call.request.start.sequence >= call.request.end.sequence) { + + if (call.request.start.sequence > call.request.end.sequence) { call.emit("error", { code: status.INVALID_ARGUMENT, - details: "End sequence must be greater than start sequence", + details: + "End sequence must be greater than or equal to start sequence", }); call.end(); return; } + + const requested_block_amount = + call.request.end.sequence - call.request.start.sequence; + if ( + process.env["BLOCK_RANGE_MAX"] && + requested_block_amount > Number(process.env["BLOCK_RANGE_MAX"]) + ) { + call.emit("error", { + code: status.INVALID_ARGUMENT, + details: `Maximum block range exceeded for server, requested: ${requested_block_amount}, allowed: ${process.env["BLOCK_RANGE_MAX"]}}`, + }); + } try { for ( let i = call.request.start.sequence; diff --git a/test/server.test.slow.ts b/test/server.test.slow.ts index 23383ff..e83d00a 100644 --- a/test/server.test.slow.ts +++ b/test/server.test.slow.ts @@ -184,7 +184,7 @@ describe("getBlock", () => { }); }); expect(promise).rejects.toThrow( - "INVALID_ARGUMENT: End sequence must be greater than start sequence", + "INVALID_ARGUMENT: End sequence must be greater than or equal to start sequence", ); });