From 6367e1d2e5712b63fcb846b14291608b55b3884d Mon Sep 17 00:00:00 2001 From: Luis Ricardo Sanchez Date: Thu, 6 Oct 2022 17:47:27 -0500 Subject: [PATCH] fix: pr comments addressed v1 --- README.md | 4 +- src/config.ts | 100 ++++++++++++++++++++++++-------------------- src/stamps.ts | 10 ++--- test/stamps.spec.ts | 2 +- 4 files changed, 61 insertions(+), 55 deletions(-) diff --git a/README.md b/README.md index fa390ea1..c9a74adc 100644 --- a/README.md +++ b/README.md @@ -158,8 +158,8 @@ npm run start | ENS_SUBDOMAINS | false | Enables Bzz.link subdomain translation functionality for ENS. | | REMOVE_PIN_HEADER | true | Removes swarm-pin header on all proxy requests. | | `LOG_LEVEL` | info | Log level that is outputted (values: `critical`, `error`, `warn`, `info`, `verbose`, `debug`) | -| POSTAGE_EXTENDSTTL | false | Enables extends TTL feature. Works along with POSTAGE_AMOUNT | -| POSTAGE_EXTENDS_CAPACITY | false | Enables extends stamp capacity +| POSTAGE_EXTENDSTTL | false | Enables extending TTL feature. Works along with POSTAGE_AMOUNT | +| POSTAGE_EXTENDS_CAPACITY | false | Enables extending stamp capacity feature. | | EXPOSE_HASHED_IDENTITY | false | Exposes `x-bee-node` header, which is the hashed identity of the Bee node for identification purposes | | REUPLOAD_PERIOD | undefined | How frequently are the pinned content checked to be reuploaded. | diff --git a/src/config.ts b/src/config.ts index bff9b305..d5620b9a 100644 --- a/src/config.ts +++ b/src/config.ts @@ -158,66 +158,25 @@ export function getStampsConfig({ // Start in hardcoded mode if (POSTAGE_STAMP) return { mode: 'hardcoded', stamp: POSTAGE_STAMP } // Start autobuy - else { - return validateStampPostageConfig( - BEE_DEBUG_API_URL, + else if (!POSTAGE_EXTENDSTTL && POSTAGE_DEPTH && POSTAGE_AMOUNT && BEE_DEBUG_API_URL) { + return createAutoBuyStampsConfig( POSTAGE_DEPTH, POSTAGE_AMOUNT, POSTAGE_USAGE_THRESHOLD, POSTAGE_USAGE_MAX, POSTAGE_TTL_MIN, - POSTAGE_EXTENDSTTL, - POSTAGE_EXTENDS_CAPACITY, refreshPeriod, beeDebugApiUrl, ) - } -} - -export function validateStampPostageConfig( - BEE_DEBUG_API_URL: string | undefined, - POSTAGE_DEPTH: string | undefined, - POSTAGE_AMOUNT: string | undefined, - POSTAGE_USAGE_THRESHOLD: string | undefined, - POSTAGE_USAGE_MAX: string | undefined, - POSTAGE_TTL_MIN: string | undefined, - POSTAGE_EXTENDSTTL: string | undefined, - POSTAGE_EXTENDS_CAPACITY: string | undefined, - refreshPeriod: number, - beeDebugApiUrl: string, -): StampsConfig | undefined { - if (!POSTAGE_EXTENDSTTL && POSTAGE_DEPTH && POSTAGE_AMOUNT && BEE_DEBUG_API_URL) { - return { - mode: 'autobuy', - depth: Number(POSTAGE_DEPTH), - amount: POSTAGE_AMOUNT, - usageThreshold: Number(POSTAGE_USAGE_THRESHOLD || DEFAULT_POSTAGE_USAGE_THRESHOLD), - usageMax: Number(POSTAGE_USAGE_MAX || DEFAULT_POSTAGE_USAGE_MAX), - ttlMin: Number(POSTAGE_TTL_MIN || (refreshPeriod / 1000) * 5), - refreshPeriod, - beeDebugApiUrl, - } } else if ( POSTAGE_EXTENDSTTL === 'true' && POSTAGE_AMOUNT && POSTAGE_DEPTH && Number(POSTAGE_TTL_MIN) >= MINIMAL_EXTENDS_TTL_VALUE ) { - return { - mode: 'extendsTTL', - depth: Number(POSTAGE_DEPTH), - ttlMin: Number(POSTAGE_TTL_MIN), - amount: POSTAGE_AMOUNT, - refreshPeriod, - beeDebugApiUrl, - } + return createExtendsTTLConfig(POSTAGE_DEPTH, POSTAGE_TTL_MIN, POSTAGE_AMOUNT, refreshPeriod, beeDebugApiUrl) } else if (POSTAGE_EXTENDS_CAPACITY === 'true') { - return { - mode: 'extendsCapacity', - usageThreshold: Number(POSTAGE_USAGE_THRESHOLD || DEFAULT_POSTAGE_USAGE_THRESHOLD), - refreshPeriod, - beeDebugApiUrl, - } + return createExtendsCapacityConfig(POSTAGE_USAGE_THRESHOLD, refreshPeriod, beeDebugApiUrl) } // Missing one of the variables needed for the autobuy or extends TTL else if (POSTAGE_DEPTH || POSTAGE_AMOUNT || POSTAGE_TTL_MIN || BEE_DEBUG_API_URL) { @@ -232,6 +191,57 @@ export function validateStampPostageConfig( return undefined } +export function createAutoBuyStampsConfig( + POSTAGE_DEPTH: string, + POSTAGE_AMOUNT: string, + POSTAGE_USAGE_THRESHOLD: string | undefined, + POSTAGE_USAGE_MAX: string | undefined, + POSTAGE_TTL_MIN: string | undefined, + refreshPeriod: number, + beeDebugApiUrl: string, +): StampsConfigAutobuy { + return { + mode: 'autobuy', + depth: Number(POSTAGE_DEPTH), + amount: POSTAGE_AMOUNT, + usageThreshold: Number(POSTAGE_USAGE_THRESHOLD || DEFAULT_POSTAGE_USAGE_THRESHOLD), + usageMax: Number(POSTAGE_USAGE_MAX || DEFAULT_POSTAGE_USAGE_MAX), + ttlMin: Number(POSTAGE_TTL_MIN || (refreshPeriod / 1000) * 5), + refreshPeriod, + beeDebugApiUrl, + } +} + +export function createExtendsTTLConfig( + POSTAGE_DEPTH: string, + POSTAGE_TTL_MIN: string | undefined, + POSTAGE_AMOUNT: string, + refreshPeriod: number, + beeDebugApiUrl: string, +): StampsConfigExtendsTTL { + return { + mode: 'extendsTTL', + depth: Number(POSTAGE_DEPTH), + ttlMin: Number(POSTAGE_TTL_MIN), + amount: POSTAGE_AMOUNT, + refreshPeriod, + beeDebugApiUrl, + } +} + +export function createExtendsCapacityConfig( + POSTAGE_USAGE_THRESHOLD: string | undefined, + refreshPeriod: number, + beeDebugApiUrl: string, +): StampsConfigExtendsCapacity { + return { + mode: 'extendsCapacity', + usageThreshold: Number(POSTAGE_USAGE_THRESHOLD || DEFAULT_POSTAGE_USAGE_THRESHOLD), + refreshPeriod, + beeDebugApiUrl, + } +} + export function getContentConfig({ BEE_API_URL, REUPLOAD_PERIOD }: EnvironmentVariables = {}): ContentConfig { return { beeApiUrl: BEE_API_URL || DEFAULT_BEE_API_URL, diff --git a/src/stamps.ts b/src/stamps.ts index 33833282..93226c1e 100644 --- a/src/stamps.ts +++ b/src/stamps.ts @@ -120,7 +120,7 @@ export function filterUsableStampsExtendsTTL(stamps: PostageBatch[]): PostageBat */ export function filterUsableStampsExtendsCapacity(stamps: PostageBatch[], usageThreshold: number): PostageBatch[] { const usableStamps = stamps - // filter to get stamps that have the right depth, amount and are not fully used or expired + // filter to get stamps that have been used over the usageThreshold set .filter(s => s.usable && getUsage(s) > usageThreshold) // sort the stamps by usage .sort((a, b) => (getUsage(a) < getUsage(b) ? 1 : -1)) @@ -166,10 +166,6 @@ export async function topUpStamp(beeDebug: BeeDebug, postageBatchId: string, amo return stamp } -export async function diluteStamp(beeDebug: BeeDebug, postageBatchId: string, depth: number) { - await beeDebug.diluteBatch(postageBatchId, depth) -} - export class StampsManager { private stamp?: string private usableStamps?: PostageBatch[] @@ -329,8 +325,8 @@ export class StampsManager { for (const stamp of this.usableStamps) { try { logger.debug(`extending stamp capacity: ${stamp.batchID}`) - await topUpStamp(beeDebug, stamp.batchID, (Number(stamp.amount) * 2).toString()) - await diluteStamp(beeDebug, stamp.batchID, stamp.depth + 1) + await topUpStamp(beeDebug, stamp.batchID, (BigInt(stamp.amount) * BigInt(2)).toString()) + await beeDebug.diluteBatch(stamp.batchID, stamp.depth + 1) logger.info(`capacity extended for stamp ${stamp.batchID}`) } catch (err) { logger.error('failed to extend stamp capacity', err) diff --git a/test/stamps.spec.ts b/test/stamps.spec.ts index 384ffcb7..ae955286 100644 --- a/test/stamps.spec.ts +++ b/test/stamps.spec.ts @@ -249,7 +249,7 @@ describe('extendsCapacity', () => { const stamp = await buyNewStamp(defaultDepth, defaultAmount, beeDebug) await topUpStamp(beeDebug, stamp.batchId, (Number(stamp.stamp.amount) * 2).toString()) - await diluteStamp(beeDebug, stamp.batchId, stamp.stamp.depth + 1) + await beeDebug.diluteBatch(stamp.batchId, stamp.stamp.depth + 1) const stampExtended = await beeDebug.getPostageBatch(stamp.batchId) expect(stampExtended.depth).toBeGreaterThan(defaultDepth) })