Return integer instead of string for both algos

This commit is contained in:
arthurfranca 2023-07-18 16:03:44 -03:00
parent b48a4d0ca7
commit 6fac0f53f5
3 changed files with 9 additions and 56 deletions

14
34.md
View File

@ -29,11 +29,7 @@ It is a backward-compatible proposal because non-supporting `relays` ignore the
Different algorithms are required to support diverse apps' event sorting needs.
However, they should be presented as optional and backward-compatible features that respect
dumb `relays`/smart `clients` nostr philosophy so that any `relay` can easily adopt it.
This NIP also takes into account the `relay` database software diversity, as explained ahead,
so to not rule out as many `relays` as possible.
However, they should be presented as optional and backward-compatible features that are easy to support.
Considering the request example above, we expect most `relays` to simply swap the `created_at` field
on a regular query for the `nip34a` one. If a `relay` implementation uses a SQL DB, the above filter would turn this:
@ -71,15 +67,11 @@ For example, [NIP-34asc](34asc.md) extension teaches `relays` the math used to u
The NIP extension MUST have atleast one example in any programming language, preferably with inline comments.
All algorithms' field values MUST be **unique strings** for each event.
This is needed to support the most rudimentary databases that `relays` may be using such as file DBs.
The event id may be appended to the string to make it unique, similar to NIP-34asc solution.
Bugfixes and small updates to embrace new event kinds may be submitted at any time by the algorithm author(s).
## Available Algorithms
| Extension | Name | Description | Modification Date |
| ------------------- | ----------| -------------------------------------------------- | ----------------- |
| [34asc](34asc.md) | Ascending | Events with older `created_at` are retrieved first | 27 / jun / 2023 |
| [34seen](34seen.md) | Seen At | Events are desc sorted by first seen at timestamp | 27 / jun / 2023 |
| [34asc](34asc.md) | Ascending | Events with older `created_at` are retrieved first | 18 / jul / 2023 |
| [34seen](34seen.md) | Seen At | Events are desc sorted by first seen at timestamp | 18 / jul / 2023 |

View File

@ -22,32 +22,13 @@ The lower the `created_at`, the higher `nip34asc` will be.
### Javascript
```js
// Use this same value even if your programming language allows a higher one
const maxDateNowSeconds = 8640000000000 // 8.64e15 ms / 1000
// Equals 17280000000000. It is ok because it is lower than Number.MAX_SAFE_INTEGER
// We multiply by 2 because of "ts = seconds + maxDateNowSeconds" below
const maxTs = maxDateNowSeconds * 2
const maxSecondsLength = maxTs.toString().length
function getNip34asc (createdAt) {
const maxDateNowSeconds = 8640000000000 // 8.64e15 ms / 1000
function getNip34asc (createdAt, id = '') {
// The id length must always be the same to not affect sorting
// All relays must use the same length
if (id.length !== 64) { throw new Error('Wrong id length') }
let seconds = Math.trunc(createdAt) // Make sure it is int instead of float
if (Number.isNaN(seconds)) { throw new Error('Wrong createdAt') }
seconds = Math.max(seconds, -maxDateNowSeconds) // Keep min limit
seconds = Math.min(seconds, maxDateNowSeconds) // Keep max limit
// Allow negative createdAt but remove minus sign
// because '-2' string timestamp is wrongly higher than '-1'
let ts = seconds + maxDateNowSeconds
// Make it lower the greater the ts is (the newer the createdAt is)
ts = maxTs - ts
// '10' is wrongly lower then '2' while '10' is higher than padded '02'
const paddedTsSeconds = ts.toString().padStart(maxSecondsLength, '0')
// Event id is used as a fixed length unique identifier
return `${paddedTsSeconds}${id}`
// maxDateNowSeconds - -maxDateNowSeconds equals 17280000000000 and is lower than Number.MAX_SAFE_INTEGER
return maxDateNowSeconds - createdAt
}
event.nip34asc = getNip34asc(event.created_at, event.id)
event.nip34asc = getNip34asc(event.created_at)
```

View File

@ -22,25 +22,5 @@ The event field is set with the timestamp of the moment the `relay` first became
### Javascript
```js
// Use this same value even if your programming language allows a higher one
// It is ok because it is lower than Number.MAX_SAFE_INTEGER
const maxDateNowSeconds = 8640000000000 // 8.64e15 ms / 1000
const maxSecondsLength = maxDateNowSeconds.toString().length
function getNip34seen (id = '') {
// The id length must always be the same to not affect sorting
// All relays must use the same length
if (id.length !== 64) { throw new Error('Wrong id length') }
let seconds = Math.trunc(Date.now() / 1000) // Make sure it is int instead of float
// don't allow negative timestamp as new Date(-1) is on year 1969
if (seconds < 0) { throw new Error('Wrong server clock') }
seconds = Math.min(seconds, maxDateNowSeconds) // Keep max limit
// '10' is wrongly lower then '2' while '10' is higher than padded '02'
const paddedTsSeconds = seconds.toString().padStart(maxSecondsLength, '0')
// Event id is used as a fixed length unique identifier
return `${paddedTsSeconds}${id}`
}
event.nip34seen = getNip34seen(event.id)
event.nip34seen = Math.floor(Date.now() / 1000)
```