Skip to content
This repository has been archived by the owner on Jun 11, 2024. It is now read-only.

Commit

Permalink
Merge pull request #6650 from LiskHQ/6589-lisk-codec-spec
Browse files Browse the repository at this point in the history
Improve lisk-codec protocol specs to be more deterministic - Closes #6589
  • Loading branch information
shuse2 authored Aug 23, 2021
2 parents 876e133 + 2376618 commit f8105dd
Show file tree
Hide file tree
Showing 79 changed files with 4,624 additions and 5,215 deletions.
1 change: 1 addition & 0 deletions elements/lisk-codec/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
"eslint-config-lisk-base": "2.0.0",
"eslint-plugin-import": "2.22.1",
"eslint-plugin-jest": "24.3.2",
"glob": "7.1.7",
"jest": "26.6.3",
"jest-extended": "0.11.5",
"jest-when": "3.2.1",
Expand Down
18 changes: 8 additions & 10 deletions elements/lisk-codec/test/add_schema.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,23 +13,21 @@
*/

import { objects } from '@liskhq/lisk-utils';
import { testCases as objectTestCases } from '../fixtures/objects_encodings.json';
import { testCases as objectsTestCases } from '../fixtures/objects_encodings.json';
import { codec } from '../src/codec';

describe('addSchema', () => {
// Arrange
const objectFixtureInput = objectTestCases[0].input;
const objectFixtureInput = objectsTestCases[0].input;

it('should add schema and keep it in cache', () => {
const message = objectFixtureInput.object;
// Replace the JSON representation of buffer with an actual buffer
(message as any).address = Buffer.from((message as any).address.data);
// Fix number not being bigint
(message as any).balance = BigInt(message.balance);
const object = {
...objectFixtureInput.object,
balance: BigInt((objectFixtureInput.object.balance as unknown) as string),
address: Buffer.from((objectFixtureInput.object.address as unknown) as string, 'hex'),
};

const { schema } = objectFixtureInput;

codec.encode(schema as any, message as any);
codec.encode(objectFixtureInput.schema, object);

expect((codec as any)._compileSchemas.object11).toMatchSnapshot();
});
Expand Down
49 changes: 24 additions & 25 deletions elements/lisk-codec/test/bytes.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,40 +13,39 @@
*/

import { writeBytes, readBytes } from '../src/bytes';
import { testCases } from '../fixtures/bytes_encodings.json';

describe('bytes', () => {
describe('writer', () => {
it('should encode bytes', () => {
const testCaseOneInput = testCases[0].input.object;
const testCaseOneOutput = testCases[0].output.value;
expect(writeBytes(Buffer.from(testCaseOneInput.address.data)).toString('hex')).toEqual(
testCaseOneOutput.slice(2, testCaseOneOutput.length),
); // Ignoring the key part

const testCaseSecondInput = testCases[1].input.object;
const testCaseSecondOutput = testCases[1].output.value;
expect(writeBytes(Buffer.from(testCaseSecondInput.address.data)).toString('hex')).toEqual(
testCaseSecondOutput.slice(2, testCaseOneOutput.length),
); // Ignoring the key part
const bytes = Buffer.from('abc0', 'hex');
const lengthInBuffer = Buffer.from([2]);

expect(writeBytes(bytes)).toEqual(Buffer.concat([lengthInBuffer, bytes]));
});

it('should encode empty bytes', () => {
const bytes = Buffer.alloc(0);
const lengthInBuffer = Buffer.from([0]);

expect(writeBytes(bytes)).toEqual(Buffer.concat([lengthInBuffer, bytes]));
});
});

describe('reader', () => {
it('should decode bytes', () => {
const testCaseOneInput = testCases[0].input.object;
const firstResult = Buffer.from(testCaseOneInput.address.data);
expect(
readBytes(writeBytes(firstResult), 0),
// Result length + varint length referring to the size
).toEqual([firstResult, firstResult.length + 1]);

const testCaseSecondInput = testCases[0].input.object;
const secondResult = Buffer.from(testCaseSecondInput.address.data);
expect(
readBytes(writeBytes(secondResult), 0),
// Result length + varint length referring to the size
).toEqual([secondResult, secondResult.length + 1]);
const bytes = Buffer.from('abc0', 'hex');
const lengthInBuffer = Buffer.from([2]);
const data = Buffer.concat([lengthInBuffer, bytes]);

expect(readBytes(data, 0)).toEqual([bytes, 2 + 1]);
});

it('should decode empty bytes', () => {
const bytes = Buffer.alloc(0);
const lengthInBuffer = Buffer.from([0]);
const data = Buffer.concat([lengthInBuffer, bytes]);

expect(readBytes(data, 0)).toEqual([bytes, 0 + 1]);
});
});
});
Loading

0 comments on commit f8105dd

Please sign in to comment.