Skip to content

Commit

Permalink
Fixed _sequence
Browse files Browse the repository at this point in the history
Now using a single epoch for group generation
  • Loading branch information
arietrouw committed Dec 21, 2024
1 parent c26e5ed commit 93495cb
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
asId, IdSchema, isId,
} from '@xyo-network/id-payload-plugin'
import { PayloadBuilder } from '@xyo-network/payload-builder'
import { WithStorageMeta } from '@xyo-network/payload-model'
import {
describe, expect, it,
} from 'vitest'
Expand All @@ -34,21 +35,43 @@ describe('MemoryArchivist', () => {
await archivist.clear()
})

it('should return items inserted in the order they were provided in', async () => {
it.only('should return items inserted in the order they were provided in', async () => {
const archivist = await MemoryArchivist.create({ account: 'random' })
const payloads: Id[] = Array.from({ length: 100 }, (_, i) => new PayloadBuilder<Id>({ schema: IdSchema }).fields({ salt: `${i}` }).build())
// Ensure payload was create in order provided
for (const [index, id] of payloads.entries()) {
expect(id?.salt).toBe(`${index}`)
}

const withStorageMeta = await PayloadBuilder.addStorageMeta(payloads)

// Ensure payload was returned in order provided
for (const [index, result] of withStorageMeta.entries()) {
expect(isId(result)).toBe(true)
const id = asId(result)
expect(id).toBeDefined()
expect(id?.salt).toBe(`${index}`)
expect(await PayloadBuilder.dataHash(result)).toEqual(await PayloadBuilder.dataHash(payloads[index]))
}

const results = await archivist.insert(payloads)

// Ensure payload was inserted in order provided
for (const [index, result] of results.entries()) {
expect(isId(result)).toBe(true)
const id = asId(result)
expect(id).toBeDefined()
if (index > 0) {
expect(result._sequence > results[index - 1]._sequence).toBeTrue()
}
if (index < 99) {
expect(result._sequence < results[index + 1]._sequence).toBeTrue()
}
if (id?.salt !== `${index}`) {
console.warn('result-', results[index - 1])
console.warn('result', result)
console.warn('result+', results[index + 1])
}
expect(id?.salt).toBe(`${index}`)
expect(await PayloadBuilder.dataHash(result)).toEqual(await PayloadBuilder.dataHash(payloads[index]))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,10 @@ export class PayloadBuilder<T extends Payload = Payload<AnyObject>, R = T> {
static async addStorageMeta<T extends Payload>(payloads: T | T[], index = 0): Promise<WithStorageMeta<T>[] | WithStorageMeta<T>> {
return Array.isArray(payloads)
? await (async () => {
const timestamp = Date.now()
return await Promise.all(payloads.map(async (payload, i) => await this.addSequencedStorageMeta(
payload,
timestamp,
i,
)))
})()
Expand Down Expand Up @@ -242,9 +244,9 @@ export class PayloadBuilder<T extends Payload = Payload<AnyObject>, R = T> {
}
}

private static async addSequencedStorageMeta<T extends Payload = Payload>(payload: T, index = 0): Promise<WithStorageMeta<T>> {
private static async addSequencedStorageMeta<T extends Payload = Payload>(payload: T, timestamp: number, index = 0): Promise<WithStorageMeta<T>> {
const withHashMeta = await this.addHashMeta(payload)
const _sequence = SequenceParser.from(Date.now(), withHashMeta._hash, index).localSequence
const _sequence = SequenceParser.from(timestamp, withHashMeta._hash, index).localSequence
return {
...withHashMeta,
_sequence,
Expand Down

0 comments on commit 93495cb

Please sign in to comment.