Skip to content

bug: nanoseconds #310

@SgtPooki

Description

@SgtPooki

Nanoseconds are calculated incorrectly. See ipfs/helia-verified-fetch#19 (comment) for more details. Basically,

Some proofs (copy and paste into dev console):

  • nanoseconds are 1e9, or "One billionth of one second".
    • BigInt(1)/*<s>*/ * BigInt(1e9)/*<ns>*/ === (1000000000n)/*<ns>*/ /* 1,000,000,000/*<ns>*/
    • 1000000000/*<ns>*/ * 1e-9/*<ns-to-s>*/ === 1
  • milliseconds are "1e6" or "One millionth of one second"
    • BigInt(1)/*<s>*/ * BigInt(1e6)/*<ms>*/ === 1000000n/*<ms>*/ /* === 1,000,000/*<ms>*/
    • 1000000/*<ms>*/ * 1e-6/*<ms-to-s>*/ === 1
  • nanoseconds are 1e6 or "One millionth of one millisecond"
    • BigInt(1)/*<ms>*/ * BigInt(1e6)/*<ns>*/ === 1000000n/*<ns>*/ /* === 1,000,000/*<ms>*/
    • 1000000/*<ns>*/ * 1e-6/*<ns-to-ms>*/ === 1

const lifetimeNs = (BigInt(ms) * BigInt(100000)) + BigInt(ns ?? '0')
creates lifeTimeNs as:

function lifeTimeNs(ms, ns) {
    // BigInt(100000) === BigInt(1e5)
    return (BigInt(ms) * BigInt(100000)) + BigInt(ns ?? '0')
}
// should match BigInt(1)/*<ms>*/ * BigInt(1e6)/*<ns>*/ === 1000000n/*<ns>*/
lifeTimeNs(1) === BigInt(1e6) // false, 
// returns `100000n` instead of `1000000n`

references:

Metadata

Metadata

Assignees

No one assigned

    Labels

    need/triageNeeds initial labeling and prioritization

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions