Skip to content

Commit

Permalink
fix: pr comments addressed v1
Browse files Browse the repository at this point in the history
  • Loading branch information
luissanchez0305 committed Oct 6, 2022
1 parent 2551b7c commit 6367e1d
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 55 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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. |
Expand Down
100 changes: 55 additions & 45 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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,
Expand Down
10 changes: 3 additions & 7 deletions src/stamps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down Expand Up @@ -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[]
Expand Down Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion test/stamps.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
Expand Down

0 comments on commit 6367e1d

Please sign in to comment.