diff --git a/packages/modules/packages/archivist/packages/memory/src/spec/MemoryArchivist.spec.ts b/packages/modules/packages/archivist/packages/memory/src/spec/MemoryArchivist.spec.ts index b0d5de1cd1..8689019ade 100644 --- a/packages/modules/packages/archivist/packages/memory/src/spec/MemoryArchivist.spec.ts +++ b/packages/modules/packages/archivist/packages/memory/src/spec/MemoryArchivist.spec.ts @@ -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' @@ -34,7 +35,7 @@ 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({ schema: IdSchema }).fields({ salt: `${i}` }).build()) // Ensure payload was create in order provided @@ -42,6 +43,17 @@ describe('MemoryArchivist', () => { 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 @@ -49,6 +61,17 @@ describe('MemoryArchivist', () => { 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])) } diff --git a/packages/protocol/packages/payload/packages/builder/src/Builder.ts b/packages/protocol/packages/payload/packages/builder/src/Builder.ts index ca2337fc66..ea38c7addd 100644 --- a/packages/protocol/packages/payload/packages/builder/src/Builder.ts +++ b/packages/protocol/packages/payload/packages/builder/src/Builder.ts @@ -47,8 +47,10 @@ export class PayloadBuilder, R = T> { static async addStorageMeta(payloads: T | T[], index = 0): Promise[] | WithStorageMeta> { 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, ))) })() @@ -242,9 +244,9 @@ export class PayloadBuilder, R = T> { } } - private static async addSequencedStorageMeta(payload: T, index = 0): Promise> { + private static async addSequencedStorageMeta(payload: T, timestamp: number, index = 0): Promise> { 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,