From f518ad4a3e0b5957f4094530c7e2c63e4679d807 Mon Sep 17 00:00:00 2001 From: Chris Troutner Date: Fri, 7 Feb 2020 16:30:20 -0800 Subject: [PATCH] Refactored for four tiers --- src/middleware/route-ratelimit.js | 14 ++++----- test/v3/rate-limits.js | 47 ++++++++++++++++++++++++++----- 2 files changed, 47 insertions(+), 14 deletions(-) diff --git a/src/middleware/route-ratelimit.js b/src/middleware/route-ratelimit.js index ffa76a6..cbd47fd 100644 --- a/src/middleware/route-ratelimit.js +++ b/src/middleware/route-ratelimit.js @@ -321,28 +321,28 @@ class RateLimits { const apiLevel = jwtInfo.apiLevel const resource = jwtInfo.resource - const level20Routes = ["insight", "bitcore", "blockbook"] - const level30Routes = ["slp"] + const level30Routes = ["insight", "bitcore", "blockbook"] + const level40Routes = ["slp"] // console.log(`apiLevel: ${apiLevel}`) // Only evaluate if user is using a JWT token. if (jwtInfo.id) { // SLP indexer routes - if (level30Routes.includes(resource)) { - if (apiLevel >= 30) retVal = 1 + if (level40Routes.includes(resource)) { + if (apiLevel >= 40) retVal = 1 // else if (apiLevel >= 10) retVal = 10 else retVal = 10 } // Normal indexer routes - else if (level20Routes.includes(resource)) { - if (apiLevel >= 20) retVal = 1 + else if (level30Routes.includes(resource)) { + if (apiLevel >= 30) retVal = 1 else retVal = 10 } // Full node tier - else if (apiLevel >= 10) { + else if (apiLevel >= 20) { retVal = 1 } diff --git a/test/v3/rate-limits.js b/test/v3/rate-limits.js index 98e7ff9..fdf021a 100644 --- a/test/v3/rate-limits.js +++ b/test/v3/rate-limits.js @@ -303,7 +303,7 @@ describe("#route-ratelimits & jwt-auth", () => { assert.equal(result, 10) }) - it("should return 1 point for indexer tier requesting full node access", () => { + it("should return 1 point for full node tier requesting full node access", () => { const jwtInfo = { apiLevel: 20, resource: "blockchain", @@ -314,7 +314,7 @@ describe("#route-ratelimits & jwt-auth", () => { assert.equal(result, 1) }) - it("should return 1 points for indexer tier requesting indexer access", () => { + it("should return 10 points for full-node tier requesting indexer access", () => { const jwtInfo = { apiLevel: 20, resource: "blockbook", @@ -322,10 +322,10 @@ describe("#route-ratelimits & jwt-auth", () => { } const result = rateLimits.calcPoints(jwtInfo) - assert.equal(result, 1) + assert.equal(result, 10) }) - it("should return 10 points for indexer tier requesting SLPDB access", () => { + it("should return 10 points for full node tier requesting SLPDB access", () => { const jwtInfo = { apiLevel: 20, resource: "slp", @@ -336,7 +336,7 @@ describe("#route-ratelimits & jwt-auth", () => { assert.equal(result, 10) }) - it("should return 1 point for SLP tier requesting full node access", () => { + it("should return 1 point for indexer tier requesting full node access", () => { const jwtInfo = { apiLevel: 30, resource: "blockchain", @@ -347,7 +347,7 @@ describe("#route-ratelimits & jwt-auth", () => { assert.equal(result, 1) }) - it("should return 1 points for SLP tier requesting indexer access", () => { + it("should return 1 points for indexer tier requesting indexer access", () => { const jwtInfo = { apiLevel: 30, resource: "blockbook", @@ -358,13 +358,46 @@ describe("#route-ratelimits & jwt-auth", () => { assert.equal(result, 1) }) - it("should return 1 points for SLP tier requesting SLPDB access", () => { + it("should return 10 points for indexer tier requesting SLPDB access", () => { const jwtInfo = { apiLevel: 30, resource: "slp", id: "5e3a0415eb29a962da2708b4" } + const result = rateLimits.calcPoints(jwtInfo) + assert.equal(result, 10) + }) + + it("should return 1 point for SLP tier requesting full node access", () => { + const jwtInfo = { + apiLevel: 40, + resource: "blockchain", + id: "5e3a0415eb29a962da2708b4" + } + + const result = rateLimits.calcPoints(jwtInfo) + assert.equal(result, 1) + }) + + it("should return 1 points for SLP tier requesting indexer access", () => { + const jwtInfo = { + apiLevel: 40, + resource: "blockbook", + id: "5e3a0415eb29a962da2708b4" + } + + const result = rateLimits.calcPoints(jwtInfo) + assert.equal(result, 1) + }) + + it("should return 1 points for SLP tier requesting SLPDB access", () => { + const jwtInfo = { + apiLevel: 40, + resource: "slp", + id: "5e3a0415eb29a962da2708b4" + } + const result = rateLimits.calcPoints(jwtInfo) assert.equal(result, 1) })