diff --git a/.vscode/launch.json b/.vscode/launch.json index 22167df469..c6a1ba6332 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -26,7 +26,7 @@ "--run", "--inspect-brk", "--no-file-parallelism", - "packages/modules/packages/diviner/packages/indexing/packages/temporal/packages/memory/src/StateToIndexCandidateDiviner/spec/Diviner.spec.ts" + "packages/protocol/packages/payload/packages/model/src/StorageMeta/spec/SequenceParser.spec.ts" ], "sourceMaps": true, "resolveSourceMapLocations": [ diff --git a/packages/protocol/packages/payload/packages/model/src/StorageMeta/SequenceParser.ts b/packages/protocol/packages/payload/packages/model/src/StorageMeta/SequenceParser.ts index b531dbee72..4c707310c8 100644 --- a/packages/protocol/packages/payload/packages/model/src/StorageMeta/SequenceParser.ts +++ b/packages/protocol/packages/payload/packages/model/src/StorageMeta/SequenceParser.ts @@ -32,32 +32,32 @@ export class SequenceParser { } get address(): Address { - const start = SequenceConstants.localSequenceBytes * 2 - const end = SequenceConstants.qualifiedSequenceBytes * 2 + const start = SequenceConstants.localSequenceBytes + const end = SequenceConstants.qualifiedSequenceBytes return toHex(this.data.slice(start, end).buffer, { prefix: false }) } get epoch(): Epoch { const start = 0 - const end = SequenceConstants.epochBytes * 2 + const end = SequenceConstants.epochBytes return toHex(this.data.slice(start, end).buffer, { prefix: false }) } get localSequence(): LocalSequence { const start = 0 - const end = SequenceConstants.localSequenceBytes * 2 + const end = SequenceConstants.localSequenceBytes return toHex(this.data.slice(start, end).buffer, { prefix: false }) } get nonce(): Nonce { - const start = SequenceConstants.epochBytes * 2 - const end = SequenceConstants.localSequenceBytes * 2 + const start = SequenceConstants.epochBytes + const end = SequenceConstants.localSequenceBytes return toHex(this.data.slice(start, end).buffer, { prefix: false }) } get qualifiedSequence(): QualifiedSequence { const start = 0 - const end = SequenceConstants.qualifiedSequenceBytes * 2 + const end = SequenceConstants.qualifiedSequenceBytes return toHex(this.data.slice(start, end).buffer, { prefix: false }) } diff --git a/packages/protocol/packages/payload/packages/model/src/StorageMeta/spec/SequenceParser.spec.ts b/packages/protocol/packages/payload/packages/model/src/StorageMeta/spec/SequenceParser.spec.ts index bce176a78c..1d0f11bfd2 100644 --- a/packages/protocol/packages/payload/packages/model/src/StorageMeta/spec/SequenceParser.spec.ts +++ b/packages/protocol/packages/payload/packages/model/src/StorageMeta/spec/SequenceParser.spec.ts @@ -6,46 +6,78 @@ import { describe, expect, it, } from 'vitest' +import { SequenceConstants } from '../Sequence.ts' import { SequenceParser } from '../SequenceParser.ts' describe('SequenceParser', () => { const hash: Hash = toHex('1269b95d3ebf1b1258a82ccca0b365fabf4b8c99bf8fc852e5045e30ad20fbb1') const address: Address = 'b36d327210f67ad98be881ddf6ad1f1b3e2c5137' const timestamp = 1_234_567_890_123 - const wrapper: SequenceParser = SequenceParser.from(timestamp, hash, address) + const parsed: SequenceParser = SequenceParser.from(timestamp, hash, address) describe('epoch', () => { it('converts timestamp to epoch correctly', () => { - expect(wrapper.epoch).toBe(SequenceParser.toEpoch(timestamp)) - expect(wrapper.epoch).toBe('0000011f71fb04cb') + expect(parsed.epoch).toBe(SequenceParser.toEpoch(timestamp)) + expect(parsed.epoch).toBe('0000011f71fb04cb') + }) + }) + + describe('from', () => { + it('parses sequence from timestamp and hash correctly', () => { + const parsed = SequenceParser.from(timestamp, hash) + const timestampHex = SequenceParser.toEpoch(timestamp) + const nonceHex = SequenceParser.toNonce(hash) + expect(parsed.localSequence).toBe(`${timestampHex}${nonceHex}`) + expect(parsed.localSequence).toBe('0000011f71fb04cbe5045e30ad20fbb1') + }) + it('parses sequence from timestamp, hash, and address correctly', () => { + const parsed = SequenceParser.from(timestamp, hash, address) + const timestampHex = SequenceParser.toEpoch(timestamp) + const nonceHex = SequenceParser.toNonce(hash) + expect(parsed.localSequence).toBe(`${timestampHex}${nonceHex}`) + expect(parsed.localSequence).toBe('0000011f71fb04cbe5045e30ad20fbb1') + expect(parsed.qualifiedSequence).toBe(`${timestampHex}${nonceHex}${address}`) + expect(parsed.qualifiedSequence).toBe('0000011f71fb04cbe5045e30ad20fbb1b36d327210f67ad98be881ddf6ad1f1b3e2c5137') + }) + it('equivalently parses two local sequences with and without address', () => { + const withAddress = SequenceParser.from(timestamp, hash, address) + const withoutAddress = SequenceParser.from(timestamp, hash) + expect(withAddress.localSequence).toEqual(withoutAddress.localSequence) + }) + it('equivalently parses two qualified sequences with and without address', () => { + const address = '0'.repeat(SequenceConstants.addressBytes * 2) as Address + const withAddress = SequenceParser.from(timestamp, hash, address) + const withoutAddress = SequenceParser.from(timestamp, hash) + expect(withAddress.qualifiedSequence).toEqual(withoutAddress.qualifiedSequence) }) }) describe('nonce', () => { it('derives nonce from hash correctly', () => { - expect(wrapper.nonce).toBe(SequenceParser.toNonce(hash)) - expect(wrapper.nonce).toBe(hash.slice(-8 * 2)) - expect(wrapper.nonce).toBe('e5045e30ad20fbb1') + expect(parsed.nonce).toBe(SequenceParser.toNonce(hash)) + expect(parsed.nonce).toBe(hash.slice(-8 * 2)) + expect(parsed.nonce).toBe('e5045e30ad20fbb1') }) }) describe('address', () => { it('stores the address correctly', () => { - expect(wrapper.address).toBe(address) + expect(parsed.address).toBe(address) }) }) describe('localSequence', () => { it('concatenates epoch and nonce correctly', () => { - expect(wrapper.localSequence).toBe(`${wrapper.epoch}${wrapper.nonce}`) - expect(wrapper.localSequence).toBe('0000011f71fb04cbe5045e30ad20fbb1') + expect(parsed.localSequence).toBe(`${parsed.epoch}${parsed.nonce}`) + expect(parsed.localSequence).toBe('0000011f71fb04cbe5045e30ad20fbb1') }) }) describe('qualifiedSequence', () => { it('concatenates localSequence and address correctly', () => { - expect(wrapper.qualifiedSequence).toBe(`${wrapper.localSequence}${wrapper.address}`) - expect(wrapper.qualifiedSequence).toBe( + expect(parsed.localSequence).toBe(`${parsed.epoch}${parsed.nonce}${parsed.address}`) + expect(parsed.qualifiedSequence).toBe(`${parsed.localSequence}${parsed.address}`) + expect(parsed.qualifiedSequence).toBe( '0000011f71fb04cbe5045e30ad20fbb1b36d327210f67ad98be881ddf6ad1f1b3e2c5137', ) })