diff --git a/elements/lisk-codec/package.json b/elements/lisk-codec/package.json index e999f7ac767..c9f08513819 100644 --- a/elements/lisk-codec/package.json +++ b/elements/lisk-codec/package.json @@ -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", diff --git a/elements/lisk-codec/test/add_schema.spec.ts b/elements/lisk-codec/test/add_schema.spec.ts index 44f08c154df..252af5dfa98 100644 --- a/elements/lisk-codec/test/add_schema.spec.ts +++ b/elements/lisk-codec/test/add_schema.spec.ts @@ -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(); }); diff --git a/elements/lisk-codec/test/bytes.spec.ts b/elements/lisk-codec/test/bytes.spec.ts index 88edfe4dccf..2a0587ccda2 100644 --- a/elements/lisk-codec/test/bytes.spec.ts +++ b/elements/lisk-codec/test/bytes.spec.ts @@ -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]); }); }); }); diff --git a/elements/lisk-codec/test/decode.spec.ts b/elements/lisk-codec/test/decode.spec.ts index 556aef55a5b..88f18e2023f 100644 --- a/elements/lisk-codec/test/decode.spec.ts +++ b/elements/lisk-codec/test/decode.spec.ts @@ -12,388 +12,287 @@ * Removal or modification of this copyright notice is prohibited. */ -import { Codec } from '../src/codec'; -import * as booleanDecoding from '../fixtures/boolean_encodings.json'; -import * as numberDecoding from '../fixtures/number_encodings.json'; -import * as bytesDecoding from '../fixtures/bytes_encodings.json'; -import * as stringDecoding from '../fixtures/string_encodings.json'; -import * as objectDecoding from '../fixtures/objects_encodings.json'; -import * as arrayDecoding from '../fixtures/arrays_encodings.json'; -import * as blockDecoding from '../fixtures/block_encodings.json'; -import * as blockHeaderDecoding from '../fixtures/block_header_encodings.json'; -import * as blockAssetDecoding from '../fixtures/block_asset_encodings.json'; -import * as genesisBlockAssetDecoding from '../fixtures/genesis_block_encodings.json'; -import * as accountDecoding from '../fixtures/account_encodings.json'; -import * as transactionDecoding from '../fixtures/transaction_encodings.json'; -import * as peerInfoDecoding from '../fixtures/peer_info_sample_encoding.json'; -import * as nestedArrayDecoding from '../fixtures/nested_array_encoding.json'; +import { codec } from '../src/codec'; +import { buildTestCases, getAccountFromJSON } from './utils'; + +import { testCases as accountTestCases } from '../fixtures/account_decodings.json'; +import { testCases as arrayTestCases } from '../fixtures/arrays_decodings.json'; +import { testCases as blockAssetTestCases } from '../fixtures/block_asset_decodings.json'; +import { testCases as blockTestCases } from '../fixtures/block_decodings.json'; +import { testCases as blockHeaderTestCases } from '../fixtures/block_header_decodings.json'; +import { testCases as booleanTestCases } from '../fixtures/boolean_decodings.json'; +import { testCases as bytesTestCases } from '../fixtures/bytes_decodings.json'; +import { testCases as cartSampleTestCases } from '../fixtures/cart_sample_decodings.json'; +import { testCases as genesisBlockTestCases } from '../fixtures/genesis_block_decodings.json'; +import { testCases as nestedArrayTestCases } from '../fixtures/nested_array_decodings.json'; +import { testCases as numberTestCases } from '../fixtures/number_decodings.json'; +import { testCases as objectsTestCases } from '../fixtures/objects_decodings.json'; +import { testCases as peerInfoTestCases } from '../fixtures/peer_info_sample_decodings.json'; +import { testCases as stringTestCases } from '../fixtures/string_decodings.json'; +import { testCases as transactionTestCases } from '../fixtures/transaction_decodings.json'; describe('decode', () => { - describe('boolean decoding', () => { - for (const testCase of booleanDecoding.testCases) { - it(testCase.description, () => { - const codec = new Codec(); - const result = codec.decode( - testCase.input.schema, - Buffer.from(testCase.output.value, 'hex'), - ); - expect(result).toEqual(testCase.input.object); - }); - } + describe('account', () => { + it.each(buildTestCases(accountTestCases))('%s', ({ input, output }) => { + const object = getAccountFromJSON(output.object); + + const result = codec.decode(input.schema, Buffer.from(input.value, 'hex')); + + expect(result).toEqual(object); + }); }); - describe('number decoding', () => { - describe('uint32/sint32 decoding', () => { - for (const testCase of numberDecoding.testCases.slice(0, 2)) { - it(testCase.description, () => { - const codec = new Codec(); - const result = codec.decode( - testCase.input.schema, - Buffer.from(testCase.output.value, 'hex'), - ); - expect(result).toEqual(testCase.input.object); - }); + describe('array', () => { + const isObjectArray = (x: any): x is { address: string; amount: string }[] => + typeof x[0] === 'object' && x[0].address; + + it.each(buildTestCases(arrayTestCases))('%s', ({ input, output }) => { + let object: any = { ...output.object }; + + if (isObjectArray(object.list)) { + object = { + list: object.list.map( + (o: { address: string; amount: string | number | bigint | boolean }) => ({ + ...o, + amount: BigInt(o.amount), + }), + ), + }; } + + const result = codec.decode(input.schema, Buffer.from(input.value, 'hex')); + + expect(result).toEqual(object); }); + }); - describe('uint64/sint64 decoding', () => { - for (const testCase of numberDecoding.testCases.slice(2, 4)) { - it(testCase.description, () => { - const codec = new Codec(); - const result = codec.decode( - testCase.input.schema, - Buffer.from(testCase.output.value, 'hex'), - ); - expect(result).toEqual({ - ...testCase.input.object, - number: BigInt(testCase.input.object.number), - }); - }); - } + describe('block_asset', () => { + it.each(buildTestCases(blockAssetTestCases))('%s', ({ input, output }) => { + const object = { + ...output.object, + seedReveal: Buffer.from(output.object.seedReveal, 'hex'), + }; + + const result = codec.decode(input.schema, Buffer.from(input.value, 'hex')); + + expect(result).toEqual(object); }); }); - describe('bytes decoding', () => { - for (const testCase of bytesDecoding.testCases) { - it(testCase.description, () => { - const codec = new Codec(); - const result = codec.decode( - testCase.input.schema, - Buffer.from(testCase.output.value, 'hex'), - ); - expect(result).toEqual({ - ...testCase.input.object, - address: Buffer.from(testCase.input.object.address.data), - }); - }); - } + describe('block', () => { + it.each(buildTestCases(blockTestCases))('%s', ({ input, output }) => { + const object = { + header: Buffer.from(output.object.header, 'hex'), + payload: output.object.payload.map(p => Buffer.from(p, 'hex')), + }; + + const result = codec.decode(input.schema, Buffer.from(input.value, 'hex')); + + expect(result).toEqual(object); + }); }); - describe('string decoding', () => { - for (const testCase of stringDecoding.testCases) { - it(testCase.description, () => { - const codec = new Codec(); - const result = codec.decode( - testCase.input.schema, - Buffer.from(testCase.output.value, 'hex'), - ); - expect(result).toEqual(testCase.input.object); - }); - } + describe('block_header', () => { + it.each(buildTestCases(blockHeaderTestCases))('%s', ({ input, output }) => { + const object = { + ...output.object, + previousBlockID: Buffer.from(output.object.previousBlockID, 'hex'), + transactionRoot: Buffer.from(output.object.transactionRoot, 'hex'), + generatorPublicKey: Buffer.from(output.object.generatorPublicKey, 'hex'), + reward: BigInt(output.object.reward), + asset: Buffer.from(output.object.asset, 'hex'), + signature: Buffer.from(output.object.signature, 'hex'), + }; + + const result = codec.decode(input.schema, Buffer.from(input.value, 'hex')); + + expect(result).toEqual(object); + }); }); - describe('object decoding', () => { - it('Encoding of object', () => { - const testCase = objectDecoding.testCases[0]; - const codec = new Codec(); - const result = codec.decode( - testCase.input.schema as any, - Buffer.from(testCase.output.value, 'hex'), - ); - expect(result).toEqual({ - ...testCase.input.object, - balance: BigInt(testCase.input.object.balance), - address: Buffer.from(testCase.input.object.address?.data as number[]), - }); + describe('boolean', () => { + it.each(buildTestCases(booleanTestCases))('%s', ({ input, output }) => { + const result = codec.decode(input.schema, Buffer.from(input.value, 'hex')); + + expect(result).toEqual(output.object); }); + }); - it('Encoding of object with optional property', () => { - const testCase = objectDecoding.testCases[1]; - const codec = new Codec(); - const result = codec.decode( - testCase.input.schema as any, - Buffer.from(testCase.output.value, 'hex'), - ); - expect(result).toEqual({ - ...testCase.input.object, - value: BigInt(testCase.input.object.value), - data: Buffer.alloc(0), - }); + describe('bytes', () => { + it.each(buildTestCases(bytesTestCases))('%s', ({ input, output }) => { + const object = { + ...output.object, + address: Buffer.from(output.object.address, 'hex'), + }; + + const result = codec.decode(input.schema, Buffer.from(input.value, 'hex')); + + expect(result).toEqual(object); }); }); - describe('array decoding', () => { - describe('array decoding except object', () => { - // Index 3 is the object test, which needs special handling - const testCases = [ - ...arrayDecoding.testCases.slice(0, 3), - ...arrayDecoding.testCases.slice(4), - ]; - for (const testCase of testCases) { - it(testCase.description, () => { - const codec = new Codec(); - const result = codec.decode( - testCase.input.schema as any, - Buffer.from(testCase.output.value, 'hex'), - ); - expect(result).toEqual(testCase.input.object); - }); - } + describe('cart_sample', () => { + it.each(buildTestCases(cartSampleTestCases))('%s', ({ input, output }) => { + const result = codec.decode(input.schema, Buffer.from(input.value, 'hex')); + + expect(result).toEqual(output.object); }); + }); - it('Encoding of array of object', () => { - const testCase = arrayDecoding.testCases[3]; - const codec = new Codec(); - const result = codec.decode( - testCase.input.schema as any, - Buffer.from(testCase.output.value, 'hex'), - ); - expect(result).toEqual({ - ...testCase.input.object, - myArray: testCase.input.object.myArray?.map(l => ({ - ...l, - amount: BigInt(l.amount), - })), - }); + describe('genesis_block', () => { + it.each(buildTestCases(genesisBlockTestCases))('%s', ({ input, output }) => { + const object = { + ...output.object, + initDelegates: output.object.initDelegates.map(d => Buffer.from(d, 'hex')), + accounts: output.object.accounts.map(a => getAccountFromJSON(a)), + }; + + const result = codec.decode(input.schema, Buffer.from(input.value, 'hex')); + + expect(result).toEqual(object); }); }); - describe('block decoding', () => { - for (const testCase of blockDecoding.testCases) { - it(testCase.description, () => { - const codec = new Codec(); - const result = codec.decode( - testCase.input.schema, - Buffer.from(testCase.output.value, 'hex'), - ); - expect(result).toEqual({ - ...testCase.input.object, - header: Buffer.from(testCase.input.object.header.data), - payload: testCase.input.object.payload.map(p => Buffer.from(p.data)), - }); - }); - } + describe('nested_array', () => { + it.each(buildTestCases(nestedArrayTestCases))('%s', ({ input, output }) => { + const result = codec.decode(input.schema, Buffer.from(input.value, 'hex')); + + expect(result).toEqual(output.object); + }); }); - describe('block header decoding', () => { - for (const testCase of blockHeaderDecoding.testCases) { - it(testCase.description, () => { - const codec = new Codec(); - const result = codec.decode( - testCase.input.schema, - Buffer.from(testCase.output.value, 'hex'), - ); - expect(result).toEqual({ - ...testCase.input.object, - reward: BigInt(testCase.input.object.reward), - asset: Buffer.from(testCase.input.object.asset.data), - transactionRoot: Buffer.from(testCase.input.object.transactionRoot.data), - signature: Buffer.from(testCase.input.object.signature.data), - previousBlockID: Buffer.from(testCase.input.object.previousBlockID.data), - generatorPublicKey: Buffer.from(testCase.input.object.generatorPublicKey.data), - }); - }); - } + describe('number', () => { + it.each(buildTestCases(numberTestCases))('%s', ({ input, output }) => { + const object = { + ...output.object, + number: + typeof output.object.number === 'string' + ? BigInt(output.object.number) + : output.object.number, + }; + + const result = codec.decode(input.schema, Buffer.from(input.value, 'hex')); + + expect(result).toEqual(object); + }); }); - describe('block asset decoding', () => { - for (const testCase of blockAssetDecoding.testCases) { - it(testCase.description, () => { - const codec = new Codec(); - const result = codec.decode( - testCase.input.schema, - Buffer.from(testCase.output.value, 'hex'), - ); - expect(result).toEqual({ - ...testCase.input.object, - seedReveal: Buffer.from(testCase.input.object.seedReveal.data), - }); - }); - } + describe('objects', () => { + it(objectsTestCases[0].description, () => { + const testCase = objectsTestCases[0]; + const output = testCase.output as any; + + const object = { + ...output.object, + address: Buffer.from(output.object.address, 'hex'), + balance: BigInt(output.object.balance), + }; + + const result = codec.decode(testCase.input.schema, Buffer.from(testCase.input.value, 'hex')); + + expect(result).toEqual(object); + }); + + it(objectsTestCases[1].description, () => { + const testCase = objectsTestCases[1]; + const output = testCase.output as any; + + const object = { + ...output.object, + value: BigInt(output.object.value), + data: Buffer.from(output.object.data, 'hex'), + }; + + const result = codec.decode(testCase.input.schema, Buffer.from(testCase.input.value, 'hex')); + + expect(result).toEqual(object); + }); }); - describe('genesis block asset decoding', () => { - for (const testCase of genesisBlockAssetDecoding.testCases) { - it(testCase.description, () => { - const codec = new Codec(); - const result = codec.decode( - testCase.input.schema, - Buffer.from(testCase.output.value, 'hex'), - ); - expect(result).toEqual({ - ...testCase.input.object, - initDelegates: testCase.input.object.initDelegates.map(d => Buffer.from(d.data)), - accounts: testCase.input.object.accounts.map(acc => ({ - ...acc, - address: Buffer.from(acc.address.data), - balance: BigInt(acc.balance), - publicKey: Buffer.from(acc.publicKey.data), - nonce: BigInt(acc.nonce), - keys: { - ...acc.keys, - mandatoryKeys: acc.keys.mandatoryKeys.map((b: any) => Buffer.from(b.data)), - optionalKeys: acc.keys.optionalKeys.map((b: any) => Buffer.from(b.data)), - }, - asset: { - ...acc.asset, - delegate: { - ...acc.asset.delegate, - totalVotesReceived: BigInt(acc.asset.delegate.totalVotesReceived), - }, - sentVotes: acc.asset.sentVotes.map(v => ({ - ...v, - delegateAddress: Buffer.from(v.delegateAddress.data), - amount: BigInt(v.amount), - })), - unlocking: acc.asset.unlocking.map((v: any) => ({ - ...v, - delegateAddress: Buffer.from(v.delegateAddress.data), - amount: BigInt(v.amount), - })), - }, - })), - }); - }); - } + describe('peer info', () => { + it.each(buildTestCases(peerInfoTestCases))('%s', ({ input, output }) => { + const result = codec.decode(input.schema, Buffer.from(input.value, 'hex')); + + expect(result).toEqual(output.object); + }); }); - describe('account decoding', () => { - for (const testCase of accountDecoding.testCases) { - it(testCase.description, () => { - const codec = new Codec(); - const result = codec.decode( - testCase.input.schema as any, - Buffer.from(testCase.output.value, 'hex'), - ); - expect(result).toEqual({ - ...testCase.input.object, - address: Buffer.from(testCase.input.object.address.data), - balance: BigInt(testCase.input.object.balance), - publicKey: Buffer.from(testCase.input.object.publicKey.data), - nonce: BigInt(testCase.input.object.nonce), - keys: { - ...testCase.input.object.keys, - mandatoryKeys: testCase.input.object.keys.mandatoryKeys.map(b => Buffer.from(b.data)), - optionalKeys: testCase.input.object.keys.optionalKeys.map((b: any) => - Buffer.from(b.data), - ), - }, - asset: { - ...testCase.input.object.asset, - delegate: { - ...testCase.input.object.asset.delegate, - totalVotesReceived: BigInt(testCase.input.object.asset.delegate.totalVotesReceived), - }, - sentVotes: testCase.input.object.asset.sentVotes.map(v => ({ - ...v, - delegateAddress: Buffer.from(v.delegateAddress.data), - amount: BigInt(v.amount), - })), - unlocking: testCase.input.object.asset.unlocking.map(v => ({ - ...v, - delegateAddress: Buffer.from(v.delegateAddress.data), - amount: BigInt(v.amount), - })), - }, - }); - }); - } + describe('string', () => { + it.each(buildTestCases(stringTestCases))('%s', ({ input, output }) => { + const result = codec.decode(input.schema, Buffer.from(input.value, 'hex')); + + expect(result).toEqual(output.object); + }); }); - describe('transaction decoding', () => { - it('Encoding of base transaction', () => { - const testCase = transactionDecoding.testCases[0]; - const codec = new Codec(); - const result = codec.decode( - testCase.input.schema as any, - Buffer.from(testCase.output.value, 'hex'), - ); - expect(result).toEqual({ - ...testCase.input.object, - nonce: BigInt(testCase.input.object.nonce), - fee: BigInt(testCase.input.object.fee), - senderPublicKey: Buffer.from((testCase.input.object.senderPublicKey as any).data), - signatures: testCase.input.object.signatures?.map(v => Buffer.from(v.data)), - asset: Buffer.from((testCase.input.object.asset as any).data), - }); + describe('transaction', () => { + // Base transaction + it(transactionTestCases[0].description, () => { + const testCase = transactionTestCases[0]; + const output = testCase.output as any; + + const object = { + ...output.object, + nonce: BigInt(output.object.nonce), + fee: BigInt(output.object.fee), + senderPublicKey: Buffer.from(output.object.senderPublicKey, 'hex'), + asset: Buffer.from(output.object.asset, 'hex'), + signatures: output.object.signatures.map((s: string) => Buffer.from(s, 'hex')), + }; + + const result = codec.decode(testCase.input.schema, Buffer.from(testCase.input.value, 'hex')); + + expect(result).toEqual(object); }); - it('Encoding of vote transaction asset', () => { - const testCase = transactionDecoding.testCases[1]; - const codec = new Codec(); - const result = codec.decode( - testCase.input.schema as any, - Buffer.from(testCase.output.value, 'hex'), - ); - expect(result).toEqual({ - votes: testCase.input.object.votes?.map(v => ({ - delegateAddress: Buffer.from(v.delegateAddress.data), + // vote asset + it(transactionTestCases[1].description, () => { + const testCase = transactionTestCases[1]; + const output = testCase.output as any; + + const object = { + ...output.object, + votes: output.object.votes.map((v: any) => ({ + delegateAddress: Buffer.from(v.delegateAddress, 'hex'), amount: BigInt(v.amount), })), - }); - }); + }; - describe('Encoding of multi signature transaction asset', () => { - const testCases = transactionDecoding.testCases.slice(2, 4); - for (const testCase of testCases) { - it(testCase.description, () => { - const codec = new Codec(); - const result = codec.decode( - testCase.input.schema as any, - Buffer.from(testCase.output.value, 'hex'), - ); - expect(result).toEqual({ - ...testCase.input.object, - mandatoryKeys: testCase.input.object.mandatoryKeys?.map(k => Buffer.from(k.data)), - optionalKeys: testCase.input.object.optionalKeys?.map(k => Buffer.from(k.data)), - }); - }); - } - }); - }); + const result = codec.decode(testCase.input.schema, Buffer.from(testCase.input.value, 'hex')); - describe('peer info decoding', () => { - it('should decode object without options', () => { - const testCase = peerInfoDecoding.testCases[0]; - const codec = new Codec(); - const result = codec.decode(testCase.input.schema, Buffer.from(testCase.output.value, 'hex')); - expect(result).toEqual(testCase.input.object); + expect(result).toEqual(object); }); - it('should decode object without optional fields', () => { - const testCase = peerInfoDecoding.testCases[1]; - const codec = new Codec(); - const result = codec.decode(testCase.input.schema, Buffer.from(testCase.output.value, 'hex')); - expect(result).toEqual({ - ...testCase.input.object, - height: 0, - networkIdentifier: '', - nonce: '', - networkVersion: '', - }); + // multisignature asset + it(transactionTestCases[2].description, () => { + const testCase = transactionTestCases[2]; + const output = testCase.output as any; + + const object = { + ...output.object, + mandatoryKeys: output.object.mandatoryKeys.map((v: string) => Buffer.from(v, 'hex')), + optionalKeys: output.object.optionalKeys.map((v: string) => Buffer.from(v, 'hex')), + }; + + const result = codec.decode(testCase.input.schema, Buffer.from(testCase.input.value, 'hex')); + + expect(result).toEqual(object); }); - }); - describe('nested array decoding', () => { - for (const testCase of nestedArrayDecoding.testCases.slice(1)) { - it(testCase.description, () => { - const codec = new Codec(); - const result = codec.decode( - testCase.input.schema, - Buffer.from(testCase.output.value, 'hex'), - ); - expect(result).toEqual(testCase.input.object); - }); - } + // multisignature asset + it(transactionTestCases[3].description, () => { + const testCase = transactionTestCases[3]; + const output = testCase.output as any; + + const object = { + ...output.object, + mandatoryKeys: output.object.mandatoryKeys.map((v: string) => Buffer.from(v, 'hex')), + optionalKeys: output.object.optionalKeys.map((v: string) => Buffer.from(v, 'hex')), + }; + + const result = codec.decode(testCase.input.schema, Buffer.from(testCase.input.value, 'hex')); + + expect(result).toEqual(object); + }); }); }); diff --git a/elements/lisk-codec/test/decode_json.spec.ts b/elements/lisk-codec/test/decode_json.spec.ts index f5e259d56a5..7fd0f5a4eb2 100644 --- a/elements/lisk-codec/test/decode_json.spec.ts +++ b/elements/lisk-codec/test/decode_json.spec.ts @@ -12,413 +12,30 @@ * Removal or modification of this copyright notice is prohibited. */ -import { Codec } from '../src/index'; -import * as booleanDecoding from '../fixtures/boolean_encodings.json'; -import * as numberDecoding from '../fixtures/number_encodings.json'; -import * as bytesDecoding from '../fixtures/bytes_encodings.json'; -import * as stringDecoding from '../fixtures/string_encodings.json'; -import * as objectDecoding from '../fixtures/objects_encodings.json'; -import * as arrayDecoding from '../fixtures/arrays_encodings.json'; -import * as blockDecoding from '../fixtures/block_encodings.json'; -import * as blockHeaderDecoding from '../fixtures/block_header_encodings.json'; -import * as blockAssetDecoding from '../fixtures/block_asset_encodings.json'; -import * as genesisBlockAssetDecoding from '../fixtures/genesis_block_encodings.json'; -import * as accountDecoding from '../fixtures/account_encodings.json'; -import * as transactionDecoding from '../fixtures/transaction_encodings.json'; -import * as peerInfoDecoding from '../fixtures/peer_info_sample_encoding.json'; -import * as nestedArrayDecoding from '../fixtures/nested_array_encoding.json'; +import { sync as globSync } from 'glob'; +import { join } from 'path'; +import { readFileSync } from 'fs'; +import { codec } from '../src/codec'; +import { buildTestCases } from './utils'; + +const files = globSync(join(__dirname, '..', 'fixtures', '*_decodings.json')).map( + p => + JSON.parse(readFileSync(p, 'utf8')) as { + title: string; + testCases: { + description: string; + input: { value: string; schema: any }; + output: { object: object }; + }[]; + }, +); describe('decodeJSON', () => { - describe('boolean decoding', () => { - for (const testCase of booleanDecoding.testCases) { - it(testCase.description, () => { - const codec = new Codec(); - const result = codec.decodeJSON( - testCase.input.schema, - Buffer.from(testCase.output.value, 'hex'), - ); - expect(result).toEqual(testCase.input.object); - }); - } - }); - - describe('number decoding', () => { - describe('uint32/sint32 decoding', () => { - for (const testCase of numberDecoding.testCases.slice(0, 2)) { - it(testCase.description, () => { - const codec = new Codec(); - const result = codec.decodeJSON( - testCase.input.schema, - Buffer.from(testCase.output.value, 'hex'), - ); - expect(result).toEqual(testCase.input.object); - }); - } - }); - - describe('uint64/sint64 decoding', () => { - for (const testCase of numberDecoding.testCases.slice(2, 4)) { - it(testCase.description, () => { - const codec = new Codec(); - const result = codec.decodeJSON( - testCase.input.schema, - Buffer.from(testCase.output.value, 'hex'), - ); - expect(result).toEqual({ - number: testCase.input.object.number.toString(), - }); - }); - } - }); - }); - - describe('bytes decoding', () => { - for (const testCase of bytesDecoding.testCases) { - it(testCase.description, () => { - const codec = new Codec(); - const result = codec.decodeJSON( - testCase.input.schema, - Buffer.from(testCase.output.value, 'hex'), - ); - expect(result).toEqual({ - ...testCase.input.object, - address: Buffer.from(testCase.input.object.address.data).toString('hex'), - }); - }); - } - }); - - describe('string decoding', () => { - for (const testCase of stringDecoding.testCases) { - it(testCase.description, () => { - const codec = new Codec(); - const result = codec.decodeJSON( - testCase.input.schema, - Buffer.from(testCase.output.value, 'hex'), - ); - expect(result).toEqual(testCase.input.object); - }); - } - }); - - describe('object decoding', () => { - it('Encoding of object', () => { - const testCase = objectDecoding.testCases[0]; - const codec = new Codec(); - const result = codec.decodeJSON( - testCase.input.schema as any, - Buffer.from(testCase.output.value, 'hex'), - ); - expect(result).toEqual({ - ...testCase.input.object, - balance: BigInt(testCase.input.object.balance).toString(), - address: Buffer.from(testCase.input.object.address?.data as number[]).toString('hex'), - }); - }); - - it('Encoding of object with optional property', () => { - const testCase = objectDecoding.testCases[1]; - const codec = new Codec(); - const result = codec.decodeJSON( - testCase.input.schema as any, - Buffer.from(testCase.output.value, 'hex'), - ); - expect(result).toEqual({ - ...testCase.input.object, - value: BigInt(testCase.input.object.value).toString(), - data: Buffer.alloc(0).toString('hex'), - }); - }); - }); - - describe('array decoding', () => { - describe('array decoding except object', () => { - // Index 3 is the object test, which needs special handling - const testCases = [ - ...arrayDecoding.testCases.slice(0, 3), - ...arrayDecoding.testCases.slice(4), - ]; - for (const testCase of testCases) { - it(testCase.description, () => { - const codec = new Codec(); - const result = codec.decodeJSON( - testCase.input.schema as any, - Buffer.from(testCase.output.value, 'hex'), - ); - expect(result).toEqual(testCase.input.object); - }); - } - }); - - it('Encoding of array of object', () => { - const testCase = arrayDecoding.testCases[3]; - const codec = new Codec(); - const result = codec.decodeJSON( - testCase.input.schema as any, - Buffer.from(testCase.output.value, 'hex'), - ); - - expect(result).toEqual({ - ...testCase.input.object, - myArray: testCase.input.object.myArray?.map(l => ({ - ...l, - amount: BigInt(l.amount).toString(), - })), - }); - }); - }); - - describe('block decoding', () => { - for (const testCase of blockDecoding.testCases) { - it(testCase.description, () => { - const codec = new Codec(); - const result = codec.decodeJSON( - testCase.input.schema, - Buffer.from(testCase.output.value, 'hex'), - ); - - expect(result).toEqual({ - ...testCase.input.object, - header: Buffer.from(testCase.input.object.header.data).toString('hex'), - payload: testCase.input.object.payload.map(p => Buffer.from(p.data).toString('hex')), - }); - }); - } - }); - - describe('block header decoding', () => { - for (const testCase of blockHeaderDecoding.testCases) { - it(testCase.description, () => { - const codec = new Codec(); - const result = codec.decodeJSON( - testCase.input.schema, - Buffer.from(testCase.output.value, 'hex'), - ); - expect(result).toEqual({ - ...testCase.input.object, - reward: BigInt(testCase.input.object.reward).toString(), - asset: Buffer.from(testCase.input.object.asset.data).toString('hex'), - transactionRoot: Buffer.from(testCase.input.object.transactionRoot.data).toString('hex'), - signature: Buffer.from(testCase.input.object.signature.data).toString('hex'), - previousBlockID: Buffer.from(testCase.input.object.previousBlockID.data).toString('hex'), - generatorPublicKey: Buffer.from(testCase.input.object.generatorPublicKey.data).toString( - 'hex', - ), - }); - }); - } - }); - - describe('block asset decoding', () => { - for (const testCase of blockAssetDecoding.testCases) { - it(testCase.description, () => { - const codec = new Codec(); - const result = codec.decodeJSON( - testCase.input.schema, - Buffer.from(testCase.output.value, 'hex'), - ); - expect(result).toEqual({ - ...testCase.input.object, - seedReveal: Buffer.from(testCase.input.object.seedReveal.data).toString('hex'), - }); - }); - } - }); - - describe('genesis block asset decoding', () => { - for (const testCase of genesisBlockAssetDecoding.testCases) { - it(testCase.description, () => { - const codec = new Codec(); - const result = codec.decodeJSON( - testCase.input.schema, - Buffer.from(testCase.output.value, 'hex'), - ); - expect(result).toEqual({ - ...testCase.input.object, - initDelegates: testCase.input.object.initDelegates.map(d => - Buffer.from(d.data).toString('hex'), - ), - accounts: testCase.input.object.accounts.map(acc => ({ - ...acc, - address: Buffer.from(acc.address.data).toString('hex'), - balance: BigInt(acc.balance).toString(), - publicKey: Buffer.from(acc.publicKey.data).toString('hex'), - nonce: BigInt(acc.nonce).toString(), - keys: { - ...acc.keys, - mandatoryKeys: acc.keys.mandatoryKeys.map((b: any) => - Buffer.from(b.data).toString('hex'), - ), - optionalKeys: acc.keys.optionalKeys.map((b: any) => - Buffer.from(b.data).toString('hex'), - ), - }, - asset: { - ...acc.asset, - delegate: { - ...acc.asset.delegate, - totalVotesReceived: BigInt(acc.asset.delegate.totalVotesReceived).toString(), - }, - sentVotes: acc.asset.sentVotes.map(v => ({ - ...v, - delegateAddress: Buffer.from(v.delegateAddress.data).toString('hex'), - amount: BigInt(v.amount).toString(), - })), - unlocking: acc.asset.unlocking.map((v: any) => ({ - ...v, - delegateAddress: Buffer.from(v.delegateAddress.data).toString('hex'), - amount: BigInt(v.amount).toString(), - })), - }, - })), - }); - }); - } - }); - - describe('account decoding', () => { - for (const testCase of accountDecoding.testCases) { - it(testCase.description, () => { - const codec = new Codec(); - const result = codec.decodeJSON( - testCase.input.schema as any, - Buffer.from(testCase.output.value, 'hex'), - ); - expect(result).toEqual({ - ...testCase.input.object, - address: Buffer.from(testCase.input.object.address.data).toString('hex'), - balance: BigInt(testCase.input.object.balance).toString(), - publicKey: Buffer.from(testCase.input.object.publicKey.data).toString('hex'), - nonce: BigInt(testCase.input.object.nonce).toString(), - keys: { - ...testCase.input.object.keys, - mandatoryKeys: testCase.input.object.keys.mandatoryKeys.map(b => - Buffer.from(b.data).toString('hex'), - ), - optionalKeys: testCase.input.object.keys.optionalKeys.map((b: any) => - Buffer.from(b.data).toString('hex'), - ), - }, - asset: { - ...testCase.input.object.asset, - delegate: { - ...testCase.input.object.asset.delegate, - totalVotesReceived: BigInt( - testCase.input.object.asset.delegate.totalVotesReceived, - ).toString(), - }, - sentVotes: testCase.input.object.asset.sentVotes.map(v => ({ - ...v, - delegateAddress: Buffer.from(v.delegateAddress.data).toString('hex'), - amount: BigInt(v.amount).toString(), - })), - unlocking: testCase.input.object.asset.unlocking.map(v => ({ - ...v, - delegateAddress: Buffer.from(v.delegateAddress.data).toString('hex'), - amount: BigInt(v.amount).toString(), - })), - }, - }); - }); - } - }); - - describe('transaction decoding', () => { - it('Encoding of base transaction', () => { - const testCase = transactionDecoding.testCases[0]; - const codec = new Codec(); - const result = codec.decodeJSON( - testCase.input.schema as any, - Buffer.from(testCase.output.value, 'hex'), + describe.each(buildTestCases(files))('%s', file => { + it.each(buildTestCases(file.testCases))('%s', ({ input, output }) => { + expect(codec.decodeJSON(input.schema, Buffer.from(input.value, 'hex'))).toEqual( + output.object, ); - expect(result).toEqual({ - ...testCase.input.object, - nonce: BigInt(testCase.input.object.nonce).toString(), - fee: BigInt(testCase.input.object.fee).toString(), - senderPublicKey: Buffer.from((testCase.input.object.senderPublicKey as any).data).toString( - 'hex', - ), - signatures: testCase.input.object.signatures?.map(v => Buffer.from(v.data).toString('hex')), - asset: Buffer.from((testCase.input.object.asset as any).data).toString('hex'), - }); }); - - it('Encoding of vote transaction asset', () => { - const testCase = transactionDecoding.testCases[1]; - const codec = new Codec(); - const result = codec.decodeJSON( - testCase.input.schema as any, - Buffer.from(testCase.output.value, 'hex'), - ); - expect(result).toEqual({ - votes: testCase.input.object.votes?.map(v => ({ - delegateAddress: Buffer.from(v.delegateAddress.data).toString('hex'), - amount: BigInt(v.amount).toString(), - })), - }); - }); - - describe('Encoding of multi signature transaction asset', () => { - const testCases = transactionDecoding.testCases.slice(2, 4); - for (const testCase of testCases) { - it(testCase.description, () => { - const codec = new Codec(); - const result = codec.decodeJSON( - testCase.input.schema as any, - Buffer.from(testCase.output.value, 'hex'), - ); - expect(result).toEqual({ - ...testCase.input.object, - mandatoryKeys: testCase.input.object.mandatoryKeys?.map(k => - Buffer.from(k.data).toString('hex'), - ), - optionalKeys: testCase.input.object.optionalKeys?.map(k => - Buffer.from(k.data).toString('hex'), - ), - }); - }); - } - }); - }); - - describe('peer info decoding', () => { - it('should decode object without options', () => { - const testCase = peerInfoDecoding.testCases[0]; - const codec = new Codec(); - const result = codec.decodeJSON( - testCase.input.schema, - Buffer.from(testCase.output.value, 'hex'), - ); - expect(result).toEqual(testCase.input.object); - }); - - it('should decode object without optional fields', () => { - const testCase = peerInfoDecoding.testCases[1]; - const codec = new Codec(); - const result = codec.decodeJSON( - testCase.input.schema, - Buffer.from(testCase.output.value, 'hex'), - ); - expect(result).toEqual({ - ...testCase.input.object, - height: 0, - networkIdentifier: '', - nonce: '', - networkVersion: '', - }); - }); - }); - - describe('nested array decoding', () => { - for (const testCase of nestedArrayDecoding.testCases.slice(1)) { - it(testCase.description, () => { - const codec = new Codec(); - const result = codec.decodeJSON( - testCase.input.schema, - Buffer.from(testCase.output.value, 'hex'), - ); - expect(result).toEqual(testCase.input.object); - }); - } }); }); diff --git a/elements/lisk-codec/test/encode.spec.ts b/elements/lisk-codec/test/encode.spec.ts index d04b39125c6..503d50ea37d 100644 --- a/elements/lisk-codec/test/encode.spec.ts +++ b/elements/lisk-codec/test/encode.spec.ts @@ -12,442 +12,285 @@ * Removal or modification of this copyright notice is prohibited. */ import { codec } from '../src/codec'; +import { buildTestCases, getAccountFromJSON } from './utils'; -import { testCases as objectTestCases } from '../fixtures/objects_encodings.json'; -import { testCases as bytesTestCases } from '../fixtures/bytes_encodings.json'; -import { testCases as stringTestCases } from '../fixtures/string_encodings.json'; +import { testCases as accountTestCases } from '../fixtures/account_encodings.json'; +import { testCases as arrayTestCases } from '../fixtures/arrays_encodings.json'; +import { testCases as blockAssetTestCases } from '../fixtures/block_asset_encodings.json'; +import { testCases as blockTestCases } from '../fixtures/block_encodings.json'; +import { testCases as blockHeaderTestCases } from '../fixtures/block_header_encodings.json'; import { testCases as booleanTestCases } from '../fixtures/boolean_encodings.json'; +import { testCases as bytesTestCases } from '../fixtures/bytes_encodings.json'; +import { testCases as cartSampleTestCases } from '../fixtures/cart_sample_encodings.json'; +import { testCases as genesisBlockTestCases } from '../fixtures/genesis_block_encodings.json'; +import { testCases as nestedArrayTestCases } from '../fixtures/nested_array_encoding.json'; import { testCases as numberTestCases } from '../fixtures/number_encodings.json'; -import { testCases as CartTestCases } from '../fixtures/cart_sample_encoding.json'; -import { testCases as arrayTestCases } from '../fixtures/arrays_encodings.json'; -import * as blockEncoding from '../fixtures/block_encodings.json'; -import * as blockHeaderEncoding from '../fixtures/block_header_encodings.json'; -import * as blockAssetEncoding from '../fixtures/block_asset_encodings.json'; -import * as genesisBlockAssetEncoding from '../fixtures/genesis_block_encodings.json'; -import * as accountEncoding from '../fixtures/account_encodings.json'; -import * as transactionEncoding from '../fixtures/transaction_encodings.json'; -import * as peerInfoEncoding from '../fixtures/peer_info_sample_encoding.json'; -import * as nestedArrayEncoding from '../fixtures/nested_array_encoding.json'; +import { testCases as objectsTestCases } from '../fixtures/objects_encodings.json'; +import { testCases as peerInfoTestCases } from '../fixtures/peer_info_sample_encoding.json'; +import { testCases as stringTestCases } from '../fixtures/string_encodings.json'; +import { testCases as transactionTestCases } from '../fixtures/transaction_encodings.json'; describe('encode', () => { - describe('objects', () => { - it('should encode an object with nested objects to Buffer', () => { - const objectFixtureInput = objectTestCases[0].input; - const objectFixtureOutput = objectTestCases[0].output; - 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); + describe('account', () => { + it.each(buildTestCases(accountTestCases))('%s', ({ input, output }) => { + const message = getAccountFromJSON(input.object); - const { schema } = objectFixtureInput; + const result = codec.encode(input.schema, message); - const { value: expectedOutput } = objectFixtureOutput; - - const liskBinaryMessage = codec.encode(schema as any, message as any); - expect(liskBinaryMessage.toString('hex')).toEqual(expectedOutput); + expect(result.toString('hex')).toEqual(output.value); }); + }); - it('should not encode missing properties of an object to Buffer', () => { - const objectFixtureInput = objectTestCases[1].input; - const objectFixtureOutput = objectTestCases[1].output; - const message = objectFixtureInput.object; - const { schema } = objectFixtureInput; - const { value: expectedOutput } = objectFixtureOutput; - - const liskBinaryMessage = codec.encode(schema as any, message as any); - expect(liskBinaryMessage.toString('hex')).toEqual(expectedOutput); - }); + describe('array', () => { + const isObjectArray = (x: any): x is { address: string; amount: string }[] => + typeof x[0] === 'object' && x[0].address; + + it.each(buildTestCases(arrayTestCases))('%s', ({ input, output }) => { + let message: any = { ...input.object }; + + if (isObjectArray(message.list)) { + message = { + list: message.list.map( + (o: { address: string; amount: string | number | bigint | boolean }) => ({ + ...o, + amount: BigInt(o.amount), + }), + ), + }; + } - it('should encode array of objects containing array of objects', () => { - const objectFixtureInput = CartTestCases[0].input; - const objectFixtureOutput = CartTestCases[0].output; - const message = objectFixtureInput.object; - const { schema } = objectFixtureInput; - const { value: expectedOutput } = objectFixtureOutput; + const result = codec.encode(input.schema, message); - const liskBinaryMessage = codec.encode(schema as any, message as any); - expect(liskBinaryMessage.toString('hex')).toEqual(expectedOutput); + expect(result.toString('hex')).toEqual(output.value); }); }); - describe('bytes', () => { - it('should encode a chunk of bytes as bytes with no changes', () => { - const bytesFixtureInput = bytesTestCases[0].input; - const bytesFixtureOutput = bytesTestCases[0].output; - const message = bytesFixtureInput.object; - - const originalMessageBytes = Buffer.from(bytesFixtureInput.object.address.data).toString( - 'hex', - ); - // Replace the JSON representation of buffer with an actual buffer - (message as any).address = Buffer.from(message.address.data); - const { schema } = bytesFixtureInput; - const { value: expectedOutput } = bytesFixtureOutput; - - const liskBinaryMessage = codec.encode(schema as any, message as any); - const liskBinaryMessageAsHex = liskBinaryMessage.toString('hex'); + describe('block_asset', () => { + it.each(buildTestCases(blockAssetTestCases))('%s', ({ input, output }) => { + const message = { + ...input.object, + seedReveal: Buffer.from(input.object.seedReveal, 'hex'), + }; - expect(liskBinaryMessage.toString('hex')).toEqual(expectedOutput); + const result = codec.encode(input.schema, message); - expect(liskBinaryMessageAsHex.substring(4)).toEqual(originalMessageBytes); + expect(result.toString('hex')).toEqual(output.value); }); + }); - it('should encode empty bytes', () => { - const bytesFixtureInput = bytesTestCases[1].input; - const bytesFixtureOutput = bytesTestCases[1].output; - const message = bytesFixtureInput.object; - const { schema } = bytesFixtureInput; + describe('block', () => { + it.each(buildTestCases(blockTestCases))('%s', ({ input, output }) => { + const object = { + header: Buffer.from(input.object.header, 'hex'), + payload: input.object.payload.map(p => Buffer.from(p, 'hex')), + }; - (message as any).address = Buffer.from(message.address.data); - const { value: expectedOutput } = bytesFixtureOutput; - const liskBinaryMessage = codec.encode(schema as any, message as any); + const result = codec.encode(input.schema, object); - expect(liskBinaryMessage.toString('hex')).toEqual(expectedOutput); + expect(result.toString('hex')).toEqual(output.value); }); }); - describe('strings', () => { - it('should encode a regular strings', () => { - const stringFixtureInput = stringTestCases[0].input; - const stringFixtureOutput = stringTestCases[0].output; - const { object: message, schema } = stringFixtureInput; - const { value: expectedOutput } = stringFixtureOutput; - const liskBinaryMessage = codec.encode(schema as any, message as any); - - expect(liskBinaryMessage.toString('hex')).toEqual(expectedOutput); - }); + describe('block_header', () => { + it.each(buildTestCases(blockHeaderTestCases))('%s', ({ input, output }) => { + const object = { + ...input.object, + previousBlockID: Buffer.from(input.object.previousBlockID, 'hex'), + transactionRoot: Buffer.from(input.object.transactionRoot, 'hex'), + generatorPublicKey: Buffer.from(input.object.generatorPublicKey, 'hex'), + reward: BigInt(input.object.reward), + asset: Buffer.from(input.object.asset, 'hex'), + signature: Buffer.from(input.object.signature, 'hex'), + }; - it('should encode empty string', () => { - const stringFixtureInput = stringTestCases[1].input; - const stringFixtureOutput = stringTestCases[1].output; - const { object: message, schema } = stringFixtureInput; - const { value: expectedOutput } = stringFixtureOutput; - const liskBinaryMessage = codec.encode(schema as any, message as any); + const result = codec.encode(input.schema, object); - expect(liskBinaryMessage.toString('hex')).toEqual(expectedOutput); + expect(result.toString('hex')).toEqual(output.value); }); + }); - it('should encode symbols string', () => { - const stringFixtureInput = stringTestCases[2].input; - const stringFixtureOutput = stringTestCases[2].output; - const { object: message, schema } = stringFixtureInput; - const { value: expectedOutput } = stringFixtureOutput; - const liskBinaryMessage = codec.encode(schema as any, message as any); + describe('boolean', () => { + it.each(buildTestCases(booleanTestCases))('%s', ({ input, output }) => { + const result = codec.encode(input.schema, input.object); - expect(liskBinaryMessage.toString('hex')).toEqual(expectedOutput); + expect(result.toString('hex')).toEqual(output.value); }); }); - describe('booleans', () => { - it('should encode boolean true', () => { - const booleanFixtureInput = booleanTestCases[0].input; - const booleanFixtureOutput = booleanTestCases[0].output; - const { object: message, schema } = booleanFixtureInput; - const { value: expectedOutput } = booleanFixtureOutput; - const liskBinaryMessage = codec.encode(schema as any, message as any); - - expect(liskBinaryMessage.toString('hex')).toEqual(expectedOutput); - }); + describe('bytes', () => { + it.each(buildTestCases(bytesTestCases))('%s', ({ input, output }) => { + const object = { + ...input.object, + address: Buffer.from(input.object.address, 'hex'), + }; - it('should encode boolean false', () => { - const booleanFixtureInput = booleanTestCases[1].input; - const booleanFixtureOutput = booleanTestCases[1].output; - const { object: message, schema } = booleanFixtureInput; - const { value: expectedOutput } = booleanFixtureOutput; - const liskBinaryMessage = codec.encode(schema as any, message as any); + const result = codec.encode(input.schema, object); - expect(liskBinaryMessage.toString('hex')).toEqual(expectedOutput); + expect(result.toString('hex')).toEqual(output.value); }); }); - describe('numbers', () => { - it('should encode unsigned 32', () => { - const numberFixtureInput = numberTestCases[0].input; - const numberFixtureOutput = numberTestCases[0].output; - const { object: message, schema } = numberFixtureInput; - const { value: expectedOutput } = numberFixtureOutput; + describe('cart_sample', () => { + it.each(buildTestCases(cartSampleTestCases))('%s', ({ input, output }) => { + const result = codec.encode(input.schema, input.object); - const liskBinaryMessage = codec.encode(schema as any, message as any); - expect(liskBinaryMessage.toString('hex')).toEqual(expectedOutput); + expect(result.toString('hex')).toEqual(output.value); }); + }); - it('should encode signed 32', () => { - const numberFixtureInput = numberTestCases[1].input; - const numberFixtureOutput = numberTestCases[1].output; - const { object: message, schema } = numberFixtureInput; - const { value: expectedOutput } = numberFixtureOutput; - - const liskBinaryMessage = codec.encode(schema as any, message as any); - expect(liskBinaryMessage.toString('hex')).toEqual(expectedOutput); - }); + describe('genesis_block', () => { + it.each(buildTestCases(genesisBlockTestCases))('%s', ({ input, output }) => { + const object = { + ...input.object, + initDelegates: input.object.initDelegates.map(d => Buffer.from(d, 'hex')), + accounts: input.object.accounts.map(a => getAccountFromJSON(a)), + }; - it('should encode unsigned 64', () => { - const numberFixtureInput = numberTestCases[2].input; - const numberFixtureOutput = numberTestCases[2].output; - const { object: message, schema } = numberFixtureInput; - (message as any).number = BigInt(message.number); - const { value: expectedOutput } = numberFixtureOutput; + const result = codec.encode(input.schema, object); - const liskBinaryMessage = codec.encode(schema as any, message as any); - expect(liskBinaryMessage.toString('hex')).toEqual(expectedOutput); + expect(result.toString('hex')).toEqual(output.value); }); + }); - it('should encode signed 64', () => { - const numberFixtureInput = numberTestCases[3].input; - const numberFixtureOutput = numberTestCases[3].output; - const { object: message, schema } = numberFixtureInput; - (message as any).number = BigInt(message.number); - const { value: expectedOutput } = numberFixtureOutput; + describe('nested_array', () => { + it.each(buildTestCases(nestedArrayTestCases))('%s', ({ input, output }) => { + const result = codec.encode(input.schema, input.object); - const liskBinaryMessage = codec.encode(schema as any, message as any); - expect(liskBinaryMessage.toString('hex')).toEqual(expectedOutput); + expect(result.toString('hex')).toEqual(output.value); }); }); - describe('arrays', () => { - it('should encode array of integers', () => { - const arrayFixtureInput = arrayTestCases[0].input; - const arrayFixtureOutput = arrayTestCases[0].output; - const { object: message, schema } = arrayFixtureInput; - const { value: expectedOutput } = arrayFixtureOutput; - - const liskBinaryMessage = codec.encode(schema as any, message as any); - expect(liskBinaryMessage.toString('hex')).toEqual(expectedOutput); - }); + describe('number', () => { + it.each(buildTestCases(numberTestCases))('%s', ({ input, output }) => { + const object = { + ...input.object, + number: + typeof input.object.number === 'string' + ? BigInt(input.object.number) + : input.object.number, + }; - it('should encode array of booleans', () => { - const arrayFixtureInput = arrayTestCases[1].input; - const arrayFixtureOutput = arrayTestCases[1].output; - const { object: message, schema } = arrayFixtureInput; - const { value: expectedOutput } = arrayFixtureOutput; + const result = codec.encode(input.schema, object); - const liskBinaryMessage = codec.encode(schema as any, message as any); - expect(liskBinaryMessage.toString('hex')).toEqual(expectedOutput); + expect(result.toString('hex')).toEqual(output.value); }); + }); + + describe('objects', () => { + it(objectsTestCases[0].description, () => { + const testCase = objectsTestCases[0]; + const input = testCase.input as any; + + const object = { + ...input.object, + address: Buffer.from(input.object.address, 'hex'), + balance: BigInt(input.object.balance), + }; - it('should encode array of strings', () => { - const arrayFixtureInput = arrayTestCases[2].input; - const arrayFixtureOutput = arrayTestCases[2].output; - const { object: message, schema } = arrayFixtureInput; - const { value: expectedOutput } = arrayFixtureOutput; + const result = codec.encode(input.schema, object); - const liskBinaryMessage = codec.encode(schema as any, message as any); - expect(liskBinaryMessage.toString('hex')).toEqual(expectedOutput); + expect(result.toString('hex')).toEqual(testCase.output.value); }); - it('should encode array of objects', () => { - const arrayFixtureInput = arrayTestCases[3].input; - const arrayFixtureOutput = arrayTestCases[3].output; - const { object: message, schema } = arrayFixtureInput; - (message as any).myArray.forEach((element: { amount: any }) => { - // eslint-disable-next-line no-param-reassign - (element as any).amount = BigInt(element.amount); - }); + it(objectsTestCases[1].description, () => { + const testCase = objectsTestCases[1]; + const input = testCase.input as any; + + const object = { + ...input.object, + value: BigInt(input.object.value), + }; - const { value: expectedOutput } = arrayFixtureOutput; + const result = codec.encode(input.schema, object); - const liskBinaryMessage = codec.encode(schema as any, message as any); - expect(liskBinaryMessage.toString('hex')).toEqual(expectedOutput); + expect(result.toString('hex')).toEqual(testCase.output.value); }); }); - describe('block encoding', () => { - for (const testCase of blockEncoding.testCases) { - it(testCase.description, () => { - const message = { - header: Buffer.from(testCase.input.object.header.data), - payload: testCase.input.object.payload.map(payloadItem => Buffer.from(payloadItem.data)), - }; + describe('peer info', () => { + it.each(buildTestCases(peerInfoTestCases))('%s', ({ input, output }) => { + const result = codec.encode(input.schema, input.object); - const result = codec.encode(testCase.input.schema, message); - expect(result.toString('hex')).toEqual(testCase.output.value); - }); - } + expect(result.toString('hex')).toEqual(output.value); + }); }); - describe('block header encoding', () => { - for (const testCase of blockHeaderEncoding.testCases) { - it(testCase.description, () => { - const message = { - ...testCase.input.object, - reward: BigInt(testCase.input.object.reward), - asset: Buffer.from(testCase.input.object.asset.data), - signature: Buffer.from(testCase.input.object.signature.data), - transactionRoot: Buffer.from(testCase.input.object.transactionRoot.data), - previousBlockID: Buffer.from(testCase.input.object.previousBlockID.data), - generatorPublicKey: Buffer.from(testCase.input.object.generatorPublicKey.data), - }; + describe('string', () => { + it.each(buildTestCases(stringTestCases))('%s', ({ input, output }) => { + const result = codec.encode(input.schema, input.object); - const result = codec.encode(testCase.input.schema, message); - expect(result.toString('hex')).toEqual(testCase.output.value); - }); - } + expect(result.toString('hex')).toEqual(output.value); + }); }); - describe('block asset encoding', () => { - for (const testCase of blockAssetEncoding.testCases) { - it(testCase.description, () => { - const message = { - ...testCase.input.object, - seedReveal: Buffer.from(testCase.input.object.seedReveal.data), - }; + describe('transaction', () => { + // Base transaction + it(transactionTestCases[0].description, () => { + const testCase = transactionTestCases[0]; + const input = testCase.input as any; + + const object = { + ...input.object, + nonce: BigInt(input.object.nonce), + fee: BigInt(input.object.fee), + senderPublicKey: Buffer.from(input.object.senderPublicKey, 'hex'), + asset: Buffer.from(input.object.asset, 'hex'), + signatures: input.object.signatures.map((s: string) => Buffer.from(s, 'hex')), + }; - const result = codec.encode(testCase.input.schema, message); + const result = codec.encode(input.schema, object); - expect(result.toString('hex')).toEqual(testCase.output.value); - }); - } - }); - - describe('genesis block asset encoding', () => { - for (const testCase of genesisBlockAssetEncoding.testCases) { - it(testCase.description, () => { - const message = { - ...testCase.input.object, - initDelegates: testCase.input.object.initDelegates.map(d => Buffer.from(d.data)), - accounts: testCase.input.object.accounts.map(acc => ({ - ...acc, - address: Buffer.from(acc.address.data), - balance: BigInt(acc.balance), - publicKey: Buffer.from(acc.publicKey.data), - nonce: BigInt(acc.nonce), - keys: { - ...acc.keys, - mandatoryKeys: acc.keys.mandatoryKeys.map((b: any) => Buffer.from(b.data)), - optionalKeys: acc.keys.optionalKeys.map((b: any) => Buffer.from(b.data)), - }, - asset: { - ...acc.asset, - delegate: { - ...acc.asset.delegate, - totalVotesReceived: BigInt(acc.asset.delegate.totalVotesReceived), - }, - sentVotes: acc.asset.sentVotes.map(v => ({ - ...v, - delegateAddress: Buffer.from(v.delegateAddress.data), - amount: BigInt(v.amount), - })), - unlocking: acc.asset.unlocking.map((v: any) => ({ - ...v, - delegateAddress: Buffer.from(v.delegateAddress.data), - amount: BigInt(v.amount), - })), - }, - })), - }; + expect(result.toString('hex')).toEqual(testCase.output.value); + }); - const result = codec.encode(testCase.input.schema, message); + // vote asset + it(transactionTestCases[1].description, () => { + const testCase = transactionTestCases[1]; + const input = testCase.input as any; - expect(result.toString('hex')).toEqual(testCase.output.value); - }); - } - }); + const object = { + ...input.object, + votes: input.object.votes.map((v: any) => ({ + delegateAddress: Buffer.from(v.delegateAddress, 'hex'), + amount: BigInt(v.amount), + })), + }; - describe('account encoding', () => { - for (const testCase of accountEncoding.testCases) { - it(testCase.description, () => { - const message = { - ...testCase.input.object, - address: Buffer.from(testCase.input.object.address.data), - balance: BigInt(testCase.input.object.balance), - publicKey: Buffer.from(testCase.input.object.publicKey.data), - nonce: BigInt(testCase.input.object.nonce), - keys: { - ...testCase.input.object.keys, - mandatoryKeys: testCase.input.object.keys.mandatoryKeys.map(b => Buffer.from(b.data)), - optionalKeys: testCase.input.object.keys.optionalKeys.map((b: any) => - Buffer.from(b.data), - ), - }, - asset: { - ...testCase.input.object.asset, - delegate: { - ...testCase.input.object.asset.delegate, - totalVotesReceived: BigInt(testCase.input.object.asset.delegate.totalVotesReceived), - }, - sentVotes: testCase.input.object.asset.sentVotes.map(v => ({ - ...v, - delegateAddress: Buffer.from(v.delegateAddress.data), - amount: BigInt(v.amount), - })), - unlocking: testCase.input.object.asset.unlocking.map(v => ({ - ...v, - delegateAddress: Buffer.from(v.delegateAddress.data), - amount: BigInt(v.amount), - })), - }, - }; + const result = codec.encode(input.schema, object); - const result = codec.encode(testCase.input.schema as any, message); + expect(result.toString('hex')).toEqual(testCase.output.value); + }); - expect(result.toString('hex')).toEqual(testCase.output.value); - }); - } - }); + // multisignature asset + it(transactionTestCases[2].description, () => { + const testCase = transactionTestCases[2]; + const input = testCase.input as any; - describe('transaction encoding', () => { - it('Encoding of base transaction', () => { - const testCase = transactionEncoding.testCases[0]; - const message = { - ...testCase.input.object, - nonce: BigInt(testCase.input.object.nonce), - fee: BigInt(testCase.input.object.fee), - senderPublicKey: Buffer.from((testCase.input.object.senderPublicKey as any).data), - signatures: testCase.input.object.signatures?.map(v => Buffer.from(v.data)), - asset: Buffer.from((testCase.input.object.asset as any).data), + const object = { + ...input.object, + mandatoryKeys: input.object.mandatoryKeys.map((v: string) => Buffer.from(v, 'hex')), + optionalKeys: input.object.optionalKeys.map((v: string) => Buffer.from(v, 'hex')), }; - const result = codec.encode(testCase.input.schema as any, message as any); + const result = codec.encode(input.schema, object); + expect(result.toString('hex')).toEqual(testCase.output.value); }); - it('Encoding of vote transaction asset', () => { - const testCase = transactionEncoding.testCases[1]; - const message = { - votes: testCase.input.object.votes?.map(v => ({ - delegateAddress: Buffer.from(v.delegateAddress.data), - amount: BigInt(v.amount), - })), + // multisignature asset + it(transactionTestCases[3].description, () => { + const testCase = transactionTestCases[3]; + const input = testCase.input as any; + + const object = { + ...input.object, + mandatoryKeys: input.object.mandatoryKeys.map((v: string) => Buffer.from(v, 'hex')), + optionalKeys: input.object.optionalKeys.map((v: string) => Buffer.from(v, 'hex')), }; - const result = codec.encode(testCase.input.schema as any, message as any); - expect(result.toString('hex')).toEqual(testCase.output.value); - }); + const result = codec.encode(input.schema, object); - describe('Encoding of multi signature transaction asset', () => { - const testCases = transactionEncoding.testCases.slice(2, 4); - for (const testCase of testCases) { - it(testCase.description, () => { - const message = { - ...testCase.input.object, - mandatoryKeys: testCase.input.object.mandatoryKeys?.map(k => Buffer.from(k.data)), - optionalKeys: testCase.input.object.optionalKeys?.map(k => Buffer.from(k.data)), - }; - - const result = codec.encode(testCase.input.schema as any, message as any); - expect(result.toString('hex')).toEqual(testCase.output.value); - }); - } + expect(result.toString('hex')).toEqual(testCase.output.value); }); }); - - describe('peer info encoding', () => { - for (const testCase of peerInfoEncoding.testCases) { - it(testCase.description, () => { - const result = codec.encode(testCase.input.schema, testCase.input.object); - expect(result.toString('hex')).toEqual(testCase.output.value); - }); - } - }); - - describe('nested array encoding', () => { - for (const testCase of nestedArrayEncoding.testCases) { - it(testCase.description, () => { - const result = codec.encode(testCase.input.schema, testCase.input.object); - expect(result.toString('hex')).toEqual(testCase.output.value); - }); - } - }); }); diff --git a/elements/lisk-codec/test/encode_json.spec.ts b/elements/lisk-codec/test/encode_json.spec.ts index 0f54251741a..a31f9f82bdb 100644 --- a/elements/lisk-codec/test/encode_json.spec.ts +++ b/elements/lisk-codec/test/encode_json.spec.ts @@ -11,461 +11,28 @@ * * Removal or modification of this copyright notice is prohibited. */ +import { sync as globSync } from 'glob'; +import { join } from 'path'; +import { readFileSync } from 'fs'; import { codec } from '../src/codec'; - -import { testCases as objectTestCases } from '../fixtures/objects_encodings.json'; -import { testCases as bytesTestCases } from '../fixtures/bytes_encodings.json'; -import { testCases as stringTestCases } from '../fixtures/string_encodings.json'; -import { testCases as booleanTestCases } from '../fixtures/boolean_encodings.json'; -import { testCases as numberTestCases } from '../fixtures/number_encodings.json'; -import { testCases as CartTestCases } from '../fixtures/cart_sample_encoding.json'; -import { testCases as arrayTestCases } from '../fixtures/arrays_encodings.json'; -import * as blockEncoding from '../fixtures/block_encodings.json'; -import * as blockHeaderEncoding from '../fixtures/block_header_encodings.json'; -import * as blockAssetEncoding from '../fixtures/block_asset_encodings.json'; -import * as genesisBlockAssetEncoding from '../fixtures/genesis_block_encodings.json'; -import * as accountEncoding from '../fixtures/account_encodings.json'; -import * as transactionEncoding from '../fixtures/transaction_encodings.json'; -import * as peerInfoEncoding from '../fixtures/peer_info_sample_encoding.json'; -import * as nestedArrayEncoding from '../fixtures/nested_array_encoding.json'; - -describe('encode', () => { - describe('objects', () => { - it('should encode an object with nested objects to Buffer', () => { - const objectFixtureInput = objectTestCases[0].input; - const objectFixtureOutput = objectTestCases[0].output; - const message = objectFixtureInput.object; - - (message as any).address = Buffer.from((message as any).address.data).toString('hex'); - (message as any).balance = BigInt(message.balance).toString(); - - const { schema } = objectFixtureInput; - - const { value: expectedOutput } = objectFixtureOutput; - - const liskBinaryMessage = codec.encodeJSON(schema as any, message as any); - expect(liskBinaryMessage.toString('hex')).toEqual(expectedOutput); - }); - - it('should not encode missing properties of an object to Buffer', () => { - const objectFixtureInput = objectTestCases[1].input; - const objectFixtureOutput = objectTestCases[1].output; - const message = objectFixtureInput.object; - const { schema } = objectFixtureInput; - const { value: expectedOutput } = objectFixtureOutput; - - const liskBinaryMessage = codec.encode(schema as any, message as any); - expect(liskBinaryMessage.toString('hex')).toEqual(expectedOutput); - }); - - it('should encode array of objects containing array of objects', () => { - const objectFixtureInput = CartTestCases[0].input; - const objectFixtureOutput = CartTestCases[0].output; - const message = objectFixtureInput.object; - const { schema } = objectFixtureInput; - const { value: expectedOutput } = objectFixtureOutput; - - const liskBinaryMessage = codec.encode(schema as any, message as any); - expect(liskBinaryMessage.toString('hex')).toEqual(expectedOutput); - }); - }); - - describe('bytes', () => { - it('should encode a chunk of bytes as bytes with no changes', () => { - const bytesFixtureInput = bytesTestCases[0].input; - const bytesFixtureOutput = bytesTestCases[0].output; - const message = bytesFixtureInput.object; - - const originalMessageBytes = Buffer.from(bytesFixtureInput.object.address.data).toString( - 'hex', - ); - - (message as any).address = Buffer.from(message.address.data).toString('hex'); - const { schema } = bytesFixtureInput; - const { value: expectedOutput } = bytesFixtureOutput; - const liskBinaryMessage = codec.encodeJSON(schema as any, message as any); - const liskBinaryMessageAsHex = liskBinaryMessage.toString('hex'); - - expect(liskBinaryMessage.toString('hex')).toEqual(expectedOutput); - - expect(liskBinaryMessageAsHex.substring(4)).toEqual(originalMessageBytes); - }); - - it('should encode empty bytes', () => { - const bytesFixtureInput = bytesTestCases[1].input; - const bytesFixtureOutput = bytesTestCases[1].output; - const message = bytesFixtureInput.object; - const { schema } = bytesFixtureInput; - - (message as any).address = Buffer.from(message.address.data); - const { value: expectedOutput } = bytesFixtureOutput; - const liskBinaryMessage = codec.encode(schema as any, message as any); - - expect(liskBinaryMessage.toString('hex')).toEqual(expectedOutput); - }); - }); - - describe('strings', () => { - it('should encode a regular strings', () => { - const stringFixtureInput = stringTestCases[0].input; - const stringFixtureOutput = stringTestCases[0].output; - const { object: message, schema } = stringFixtureInput; - const { value: expectedOutput } = stringFixtureOutput; - - const liskBinaryMessage = codec.encodeJSON(schema as any, message as any); - - expect(liskBinaryMessage.toString('hex')).toEqual(expectedOutput); - }); - - it('should encode empty string', () => { - const stringFixtureInput = stringTestCases[1].input; - const stringFixtureOutput = stringTestCases[1].output; - const { object: message, schema } = stringFixtureInput; - const { value: expectedOutput } = stringFixtureOutput; - const liskBinaryMessage = codec.encodeJSON(schema as any, message as any); - - expect(liskBinaryMessage.toString('hex')).toEqual(expectedOutput); - }); - - it('should encode symbols string', () => { - const stringFixtureInput = stringTestCases[2].input; - const stringFixtureOutput = stringTestCases[2].output; - const { object: message, schema } = stringFixtureInput; - const { value: expectedOutput } = stringFixtureOutput; - const liskBinaryMessage = codec.encodeJSON(schema as any, message as any); - - expect(liskBinaryMessage.toString('hex')).toEqual(expectedOutput); - }); - }); - - describe('booleans', () => { - it('should encode boolean true', () => { - const booleanFixtureInput = booleanTestCases[0].input; - const booleanFixtureOutput = booleanTestCases[0].output; - const { object: message, schema } = booleanFixtureInput; - const { value: expectedOutput } = booleanFixtureOutput; - const liskBinaryMessage = codec.encodeJSON(schema as any, message as any); - - expect(liskBinaryMessage.toString('hex')).toEqual(expectedOutput); - }); - - it('should encode boolean false', () => { - const booleanFixtureInput = booleanTestCases[1].input; - const booleanFixtureOutput = booleanTestCases[1].output; - const { object: message, schema } = booleanFixtureInput; - const { value: expectedOutput } = booleanFixtureOutput; - const liskBinaryMessage = codec.encodeJSON(schema as any, message as any); - - expect(liskBinaryMessage.toString('hex')).toEqual(expectedOutput); - }); - }); - - describe('numbers', () => { - it('should encode unsigned 32', () => { - const numberFixtureInput = numberTestCases[0].input; - const numberFixtureOutput = numberTestCases[0].output; - const { object: message, schema } = numberFixtureInput; - const { value: expectedOutput } = numberFixtureOutput; - - const liskBinaryMessage = codec.encodeJSON(schema as any, message as any); - expect(liskBinaryMessage.toString('hex')).toEqual(expectedOutput); - }); - - it('should encode signed 32', () => { - const numberFixtureInput = numberTestCases[1].input; - const numberFixtureOutput = numberTestCases[1].output; - const { object: message, schema } = numberFixtureInput; - const { value: expectedOutput } = numberFixtureOutput; - - const liskBinaryMessage = codec.encodeJSON(schema as any, message as any); - expect(liskBinaryMessage.toString('hex')).toEqual(expectedOutput); - }); - - it('should encode unsigned 64', () => { - const numberFixtureInput = numberTestCases[2].input; - const numberFixtureOutput = numberTestCases[2].output; - const { object: message, schema } = numberFixtureInput; - (message as any).number = BigInt(message.number).toString(); - const { value: expectedOutput } = numberFixtureOutput; - - const liskBinaryMessage = codec.encodeJSON(schema as any, message as any); - expect(liskBinaryMessage.toString('hex')).toEqual(expectedOutput); - }); - - it('should encode signed 64', () => { - const numberFixtureInput = numberTestCases[3].input; - const numberFixtureOutput = numberTestCases[3].output; - const { object: message, schema } = numberFixtureInput; - (message as any).number = BigInt(message.number).toString(); - const { value: expectedOutput } = numberFixtureOutput; - - const liskBinaryMessage = codec.encodeJSON(schema as any, message as any); - expect(liskBinaryMessage.toString('hex')).toEqual(expectedOutput); - }); - }); - - describe('arrays', () => { - it('should encode array of integers', () => { - const arrayFixtureInput = arrayTestCases[0].input; - const arrayFixtureOutput = arrayTestCases[0].output; - const { object: message, schema } = arrayFixtureInput; - const { value: expectedOutput } = arrayFixtureOutput; - - const liskBinaryMessage = codec.encodeJSON(schema as any, message as any); - expect(liskBinaryMessage.toString('hex')).toEqual(expectedOutput); - }); - - it('should encode array of booleans', () => { - const arrayFixtureInput = arrayTestCases[1].input; - const arrayFixtureOutput = arrayTestCases[1].output; - const { object: message, schema } = arrayFixtureInput; - const { value: expectedOutput } = arrayFixtureOutput; - - const liskBinaryMessage = codec.encodeJSON(schema as any, message as any); - expect(liskBinaryMessage.toString('hex')).toEqual(expectedOutput); - }); - - it('should encode array of strings', () => { - const arrayFixtureInput = arrayTestCases[2].input; - const arrayFixtureOutput = arrayTestCases[2].output; - const { object: message, schema } = arrayFixtureInput; - const { value: expectedOutput } = arrayFixtureOutput; - - const liskBinaryMessage = codec.encodeJSON(schema as any, message as any); - expect(liskBinaryMessage.toString('hex')).toEqual(expectedOutput); - }); - - it('should encode array of objects', () => { - const arrayFixtureInput = arrayTestCases[3].input; - const arrayFixtureOutput = arrayTestCases[3].output; - const { object: message, schema } = arrayFixtureInput; - (message as any).myArray.forEach((element: { amount: any }) => { - // eslint-disable-next-line no-param-reassign - (element as any).amount = BigInt(element.amount).toString(); - }); - - const { value: expectedOutput } = arrayFixtureOutput; - - const liskBinaryMessage = codec.encodeJSON(schema as any, message as any); - expect(liskBinaryMessage.toString('hex')).toEqual(expectedOutput); - }); - }); - - describe('block encoding', () => { - for (const testCase of blockEncoding.testCases) { - it(testCase.description, () => { - const message = { - header: Buffer.from(testCase.input.object.header.data).toString('hex'), - payload: testCase.input.object.payload.map(payloadItem => - Buffer.from(payloadItem.data).toString('hex'), - ), - }; - - const result = codec.encodeJSON(testCase.input.schema, message as any); - expect(result.toString('hex')).toEqual(testCase.output.value); - }); - } - }); - - describe('block header encoding', () => { - for (const testCase of blockHeaderEncoding.testCases) { - it(testCase.description, () => { - const message = { - ...testCase.input.object, - reward: BigInt(testCase.input.object.reward).toString(), - asset: Buffer.from(testCase.input.object.asset.data).toString('hex'), - signature: Buffer.from(testCase.input.object.signature.data).toString('hex'), - transactionRoot: Buffer.from(testCase.input.object.transactionRoot.data).toString('hex'), - previousBlockID: Buffer.from(testCase.input.object.previousBlockID.data).toString('hex'), - generatorPublicKey: Buffer.from(testCase.input.object.generatorPublicKey.data).toString( - 'hex', - ), - }; - - const result = codec.encodeJSON(testCase.input.schema, message as any); - expect(result.toString('hex')).toEqual(testCase.output.value); - }); - } - }); - - describe('block asset encoding', () => { - for (const testCase of blockAssetEncoding.testCases) { - it(testCase.description, () => { - const message = { - ...testCase.input.object, - seedReveal: Buffer.from(testCase.input.object.seedReveal.data).toString('hex'), - }; - - const result = codec.encodeJSON(testCase.input.schema, message as any); - - expect(result.toString('hex')).toEqual(testCase.output.value); - }); - } - }); - - describe('genesis block asset encoding', () => { - for (const testCase of genesisBlockAssetEncoding.testCases) { - it(testCase.description, () => { - const message = { - ...testCase.input.object, - initDelegates: testCase.input.object.initDelegates.map(d => - Buffer.from(d.data).toString('hex'), - ), - accounts: testCase.input.object.accounts.map(acc => ({ - ...acc, - address: Buffer.from(acc.address.data).toString('hex'), - balance: BigInt(acc.balance).toString(), - publicKey: Buffer.from(acc.publicKey.data).toString('hex'), - nonce: BigInt(acc.nonce).toString(), - keys: { - ...acc.keys, - mandatoryKeys: acc.keys.mandatoryKeys.map((b: any) => - Buffer.from(b.data).toString('hex'), - ), - optionalKeys: acc.keys.optionalKeys.map((b: any) => - Buffer.from(b.data).toString('hex'), - ), - }, - asset: { - ...acc.asset, - delegate: { - ...acc.asset.delegate, - totalVotesReceived: BigInt(acc.asset.delegate.totalVotesReceived).toString(), - }, - sentVotes: acc.asset.sentVotes.map(v => ({ - ...v, - delegateAddress: Buffer.from(v.delegateAddress.data).toString('hex'), - amount: BigInt(v.amount).toString(), - })), - unlocking: acc.asset.unlocking.map((v: any) => ({ - ...v, - delegateAddress: Buffer.from(v.delegateAddress.data).toString('hex'), - amount: BigInt(v.amount).toString(), - })), - }, - })), - }; - - const result = codec.encodeJSON(testCase.input.schema, message as any); - - expect(result.toString('hex')).toEqual(testCase.output.value); - }); - } - }); - - describe('account encoding', () => { - for (const testCase of accountEncoding.testCases) { - it(testCase.description, () => { - const message = { - ...testCase.input.object, - address: Buffer.from(testCase.input.object.address.data).toString('hex'), - balance: BigInt(testCase.input.object.balance).toString(), - publicKey: Buffer.from(testCase.input.object.publicKey.data).toString('hex'), - nonce: BigInt(testCase.input.object.nonce).toString(), - keys: { - ...testCase.input.object.keys, - mandatoryKeys: testCase.input.object.keys.mandatoryKeys.map(b => - Buffer.from(b.data).toString('hex'), - ), - optionalKeys: testCase.input.object.keys.optionalKeys.map((b: any) => - Buffer.from(b.data).toString('hex'), - ), - }, - asset: { - ...testCase.input.object.asset, - delegate: { - ...testCase.input.object.asset.delegate, - totalVotesReceived: BigInt( - testCase.input.object.asset.delegate.totalVotesReceived, - ).toString(), - }, - sentVotes: testCase.input.object.asset.sentVotes.map(v => ({ - ...v, - delegateAddress: Buffer.from(v.delegateAddress.data).toString('hex'), - amount: BigInt(v.amount).toString(), - })), - unlocking: testCase.input.object.asset.unlocking.map(v => ({ - ...v, - delegateAddress: Buffer.from(v.delegateAddress.data).toString('hex'), - amount: BigInt(v.amount.toString()), - })), - }, - }; - - const result = codec.encodeJSON(testCase.input.schema as any, message as any); - - expect(result.toString('hex')).toEqual(testCase.output.value); - }); - } - }); - - describe('transaction encoding', () => { - it('Encoding of base transaction', () => { - const testCase = transactionEncoding.testCases[0]; - const message = { - ...testCase.input.object, - nonce: BigInt(testCase.input.object.nonce).toString(), - fee: BigInt(testCase.input.object.fee).toString(), - senderPublicKey: Buffer.from((testCase.input.object.senderPublicKey as any).data).toString( - 'hex', - ), - signatures: testCase.input.object.signatures?.map(v => Buffer.from(v.data).toString('hex')), - asset: Buffer.from((testCase.input.object.asset as any).data).toString('hex'), - }; - - const result = codec.encodeJSON(testCase.input.schema as any, message as any); - expect(result.toString('hex')).toEqual(testCase.output.value); - }); - - it('Encoding of vote transaction asset', () => { - const testCase = transactionEncoding.testCases[1]; - const message = { - votes: testCase.input.object.votes?.map(v => ({ - delegateAddress: Buffer.from(v.delegateAddress.data).toString('hex'), - amount: BigInt(v.amount).toString(), - })), - }; - - const result = codec.encodeJSON(testCase.input.schema as any, message as any); - expect(result.toString('hex')).toEqual(testCase.output.value); - }); - - describe('Encoding of multi signature transaction asset', () => { - const testCases = transactionEncoding.testCases.slice(2, 4); - for (const testCase of testCases) { - it(testCase.description, () => { - const message = { - ...testCase.input.object, - mandatoryKeys: testCase.input.object.mandatoryKeys?.map(k => - Buffer.from(k.data).toString('hex'), - ), - optionalKeys: testCase.input.object.optionalKeys?.map(k => - Buffer.from(k.data).toString('hex'), - ), - }; - - const result = codec.encodeJSON(testCase.input.schema as any, message as any); - expect(result.toString('hex')).toEqual(testCase.output.value); - }); - } +import { buildTestCases } from './utils'; + +const files = globSync(join(__dirname, '..', 'fixtures', '*_encodings.json')).map( + p => + JSON.parse(readFileSync(p, 'utf8')) as { + title: string; + testCases: { + description: string; + input: { object: object; schema: any }; + output: { value: string }; + }[]; + }, +); + +describe('encodeJSON', () => { + describe.each(buildTestCases(files))('%s', file => { + it.each(buildTestCases(file.testCases))('%s', ({ input, output }) => { + expect(codec.encodeJSON(input.schema, input.object).toString('hex')).toEqual(output.value); }); }); - - describe('peer info encoding', () => { - for (const testCase of peerInfoEncoding.testCases) { - it(testCase.description, () => { - const result = codec.encodeJSON(testCase.input.schema, testCase.input.object as any); - expect(result.toString('hex')).toEqual(testCase.output.value); - }); - } - }); - - describe('nested array encoding', () => { - for (const testCase of nestedArrayEncoding.testCases) { - it(testCase.description, () => { - const result = codec.encodeJSON(testCase.input.schema, testCase.input.object as any); - expect(result.toString('hex')).toEqual(testCase.output.value); - }); - } - }); }); diff --git a/elements/lisk-codec/test/fromJSON.spec.ts b/elements/lisk-codec/test/fromJSON.spec.ts deleted file mode 100644 index c3461e9f69b..00000000000 --- a/elements/lisk-codec/test/fromJSON.spec.ts +++ /dev/null @@ -1,141 +0,0 @@ -/* - * Copyright © 2020 Lisk Foundation - * - * See the LICENSE file at the top-level directory of this distribution - * for licensing information. - * - * Unless otherwise agreed in a custom licensing agreement with the Lisk Foundation, - * no part of this software, including this file, may be copied, modified, - * propagated, or distributed except according to the terms contained in the - * LICENSE file. - * - * Removal or modification of this copyright notice is prohibited. - */ -import { codec } from '../src/codec'; -import * as accountEncoding from '../fixtures/account_encodings.json'; - -describe('toJSON', () => { - it('should return JSON like object from JS object', () => { - const { schema, object } = accountEncoding.testCases[0].input; - - const jsObject = { - ...object, - address: Buffer.from(object.address.data), - balance: BigInt(object.balance), - publicKey: Buffer.from(object.publicKey.data), - nonce: BigInt(object.nonce), - keys: { - ...object.keys, - mandatoryKeys: object.keys.mandatoryKeys.map(mKey => Buffer.from(mKey.data)), - optionalKeys: object.keys.optionalKeys.map((oKey: any) => Buffer.from(oKey.data)), - }, - asset: { - ...object.asset, - delegate: { - ...object.asset.delegate, - totalVotesReceived: BigInt(object.asset.delegate.totalVotesReceived), - }, - sentVotes: object.asset.sentVotes.map(v => ({ - ...v, - delegateAddress: Buffer.from(v.delegateAddress.data), - amount: BigInt(v.amount), - })), - unlocking: object.asset.unlocking.map(v => ({ - ...v, - delegateAddress: Buffer.from(v.delegateAddress.data), - amount: BigInt(v.amount.toString()), - })), - }, - }; - - const jsonLikeObject = { - ...object, - address: Buffer.from(object.address.data).toString('hex'), - balance: BigInt(object.balance).toString(), - publicKey: Buffer.from(object.publicKey.data).toString('hex'), - nonce: BigInt(object.nonce).toString(), - keys: { - ...object.keys, - mandatoryKeys: object.keys.mandatoryKeys.map(mKey => - Buffer.from(mKey.data).toString('hex'), - ), - optionalKeys: object.keys.optionalKeys.map((oKey: any) => - Buffer.from(oKey.data).toString('hex'), - ), - }, - asset: { - ...object.asset, - delegate: { - ...object.asset.delegate, - totalVotesReceived: BigInt(object.asset.delegate.totalVotesReceived).toString(), - }, - sentVotes: object.asset.sentVotes.map(v => ({ - ...v, - delegateAddress: Buffer.from(v.delegateAddress.data).toString('hex'), - amount: BigInt(v.amount).toString(), - })), - unlocking: object.asset.unlocking.map(v => ({ - ...v, - delegateAddress: Buffer.from(v.delegateAddress.data).toString('hex'), - amount: BigInt(v.amount.toString()).toString(), - })), - }, - }; - - const res = codec.fromJSON(schema, jsonLikeObject); - expect(res).toEqual(jsObject); - }); - - it('should ignore extra properties', () => { - const schema = { - $id: '/schema', - type: 'object', - required: ['rootProp', 'objectValue', 'arrayValue'], - properties: { - rootProp: { - dataType: 'uint64', - fieldNumber: 1, - }, - objectValue: { - type: 'object', - fieldNumber: 2, - properties: { - objectProp: { - dataType: 'uint64', - fieldNumber: 1, - }, - }, - }, - arrayValue: { - type: 'array', - fieldNumber: 3, - items: { - type: 'object', - properties: { - arrayProp: { - dataType: 'uint64', - fieldNumber: 1, - }, - }, - }, - }, - }, - }; - - const result = codec.fromJSON(schema, { - rootProp: '123', - extra3: true, - objectValue: { objectProp: '456', extra1: 'my-value', extra2: 'my-value' }, - arrayValue: [ - { arrayProp: '999', extra4: true }, - { arrayProp: '879', extra5: 987 }, - ], - }); - - expect(result).toEqual({ - rootProp: BigInt(123), - objectValue: { objectProp: BigInt(456) }, - arrayValue: [{ arrayProp: BigInt(999) }, { arrayProp: BigInt(879) }], - }); - }); -}); diff --git a/elements/lisk-codec/test/from_json.spec.ts b/elements/lisk-codec/test/from_json.spec.ts new file mode 100644 index 00000000000..d0f12e15daa --- /dev/null +++ b/elements/lisk-codec/test/from_json.spec.ts @@ -0,0 +1,79 @@ +/* + * Copyright © 2020 Lisk Foundation + * + * See the LICENSE file at the top-level directory of this distribution + * for licensing information. + * + * Unless otherwise agreed in a custom licensing agreement with the Lisk Foundation, + * no part of this software, including this file, may be copied, modified, + * propagated, or distributed except according to the terms contained in the + * LICENSE file. + * + * Removal or modification of this copyright notice is prohibited. + */ +import { codec } from '../src/codec'; +import { testCases as accountTestCases } from '../fixtures/account_encodings.json'; +import { getAccountFromJSON } from './utils'; + +describe('fromJSON', () => { + it('should return JSON like object from JS object', () => { + const { schema, object } = accountTestCases[0].input; + + const jsObject = getAccountFromJSON(object); + + expect(codec.fromJSON(schema, object)).toEqual(jsObject); + }); + + it('should ignore extra properties', () => { + const schema = { + $id: '/schema', + type: 'object', + required: ['rootProp', 'objectValue', 'arrayValue'], + properties: { + rootProp: { + dataType: 'uint64', + fieldNumber: 1, + }, + objectValue: { + type: 'object', + fieldNumber: 2, + properties: { + objectProp: { + dataType: 'uint64', + fieldNumber: 1, + }, + }, + }, + arrayValue: { + type: 'array', + fieldNumber: 3, + items: { + type: 'object', + properties: { + arrayProp: { + dataType: 'uint64', + fieldNumber: 1, + }, + }, + }, + }, + }, + }; + + const result = codec.fromJSON(schema, { + rootProp: '123', + extra3: true, + objectValue: { objectProp: '456', extra1: 'my-value', extra2: 'my-value' }, + arrayValue: [ + { arrayProp: '999', extra4: true }, + { arrayProp: '879', extra5: 987 }, + ], + }); + + expect(result).toEqual({ + rootProp: BigInt(123), + objectValue: { objectProp: BigInt(456) }, + arrayValue: [{ arrayProp: BigInt(999) }, { arrayProp: BigInt(879) }], + }); + }); +}); diff --git a/elements/lisk-codec/test/string.spec.ts b/elements/lisk-codec/test/string.spec.ts index 34870f492cf..01340ff005f 100644 --- a/elements/lisk-codec/test/string.spec.ts +++ b/elements/lisk-codec/test/string.spec.ts @@ -13,61 +13,41 @@ */ import { writeString, readString } from '../src/string'; -import { testCases } from '../fixtures/string_encodings.json'; describe('string', () => { describe('writer', () => { it('should encode string', () => { - const { - input: { - object: { data: message }, - }, - output: { value }, - } = testCases[0]; - const binaryData = writeString(message); + const message = Buffer.from('my-string', 'utf8'); + const lengthInBuffer = Buffer.from([message.length]); + const data = Buffer.concat([lengthInBuffer, message]); - // Same encoding of string - expect(binaryData.toString('hex')).toEqual(value.substring(2)); - // No change of string - expect(binaryData.toString().substring(1)).toEqual(message); + expect(writeString('my-string')).toEqual(data); }); it('should encode empty string', () => { - const { - input: { - object: { data: message }, - }, - output: { value }, - } = testCases[1]; - const binaryData = writeString(message); + const message = Buffer.from('', 'utf8'); + const lengthInBuffer = Buffer.from([message.length]); + const data = Buffer.concat([lengthInBuffer, message]); - expect(binaryData.toString('hex')).toBe(value.substring(2)); + expect(writeString('')).toEqual(data); }); }); describe('reader', () => { it('should decode string', () => { - const { - input: { - object: { data: message }, - }, - } = testCases[0]; - expect(readString(writeString(message), 0)).toEqual([ - message, - message.length + 1, // Add 1 for the size of this string - ]); + const message = Buffer.from('my-string', 'utf8'); + const lengthInBuffer = Buffer.from([message.length]); + const data = Buffer.concat([lengthInBuffer, message]); + + expect(readString(data, 0)).toEqual(['my-string', message.length + 1]); }); - }); - it('should decode empty string', () => { - const { - input: { - object: { data: message }, - }, - } = testCases[1]; - expect(readString(writeString(message), 0)).toEqual([ - message, - message.length + 1, // Add 1 for the size of this string - ]); + it('should decode empty string', () => { + const message = Buffer.from('', 'utf8'); + const lengthInBuffer = Buffer.from([message.length]); + const data = Buffer.concat([lengthInBuffer, message]); + + expect(readString(data, 0)).toEqual(['', message.length + 1]); + }); }); }); diff --git a/elements/lisk-codec/test/toJSON.spec.ts b/elements/lisk-codec/test/toJSON.spec.ts deleted file mode 100644 index ce49500c633..00000000000 --- a/elements/lisk-codec/test/toJSON.spec.ts +++ /dev/null @@ -1,141 +0,0 @@ -/* - * Copyright © 2020 Lisk Foundation - * - * See the LICENSE file at the top-level directory of this distribution - * for licensing information. - * - * Unless otherwise agreed in a custom licensing agreement with the Lisk Foundation, - * no part of this software, including this file, may be copied, modified, - * propagated, or distributed except according to the terms contained in the - * LICENSE file. - * - * Removal or modification of this copyright notice is prohibited. - */ -import { codec } from '../src/codec'; -import * as accountEncoding from '../fixtures/account_encodings.json'; - -describe('toJSON', () => { - it('should return JSON like object from JS object', () => { - const { schema, object } = accountEncoding.testCases[0].input; - - const jsObject = { - ...object, - address: Buffer.from(object.address.data), - balance: BigInt(object.balance), - publicKey: Buffer.from(object.publicKey.data), - nonce: BigInt(object.nonce), - keys: { - ...object.keys, - mandatoryKeys: object.keys.mandatoryKeys.map(mKey => Buffer.from(mKey.data)), - optionalKeys: object.keys.optionalKeys.map((oKey: any) => Buffer.from(oKey.data)), - }, - asset: { - ...object.asset, - delegate: { - ...object.asset.delegate, - totalVotesReceived: BigInt(object.asset.delegate.totalVotesReceived), - }, - sentVotes: object.asset.sentVotes.map(v => ({ - ...v, - delegateAddress: Buffer.from(v.delegateAddress.data), - amount: BigInt(v.amount), - })), - unlocking: object.asset.unlocking.map(v => ({ - ...v, - delegateAddress: Buffer.from(v.delegateAddress.data), - amount: BigInt(v.amount.toString()), - })), - }, - }; - - const jsonLikeObject = { - ...object, - address: Buffer.from(object.address.data).toString('hex'), - balance: BigInt(object.balance).toString(), - publicKey: Buffer.from(object.publicKey.data).toString('hex'), - nonce: BigInt(object.nonce).toString(), - keys: { - ...object.keys, - mandatoryKeys: object.keys.mandatoryKeys.map(mKey => - Buffer.from(mKey.data).toString('hex'), - ), - optionalKeys: object.keys.optionalKeys.map((oKey: any) => - Buffer.from(oKey.data).toString('hex'), - ), - }, - asset: { - ...object.asset, - delegate: { - ...object.asset.delegate, - totalVotesReceived: BigInt(object.asset.delegate.totalVotesReceived).toString(), - }, - sentVotes: object.asset.sentVotes.map(v => ({ - ...v, - delegateAddress: Buffer.from(v.delegateAddress.data).toString('hex'), - amount: BigInt(v.amount).toString(), - })), - unlocking: object.asset.unlocking.map(v => ({ - ...v, - delegateAddress: Buffer.from(v.delegateAddress.data).toString('hex'), - amount: BigInt(v.amount.toString()).toString(), - })), - }, - }; - - const res = codec.toJSON(schema, jsObject); - expect(res).toEqual(jsonLikeObject); - }); - - it('should ignore extra properties', () => { - const schema = { - $id: '/schema', - type: 'object', - required: ['rootProp', 'objectValue', 'arrayValue'], - properties: { - rootProp: { - dataType: 'uint64', - fieldNumber: 1, - }, - objectValue: { - type: 'object', - fieldNumber: 2, - properties: { - objectProp: { - dataType: 'uint64', - fieldNumber: 1, - }, - }, - }, - arrayValue: { - type: 'array', - fieldNumber: 3, - items: { - type: 'object', - properties: { - arrayProp: { - dataType: 'uint64', - fieldNumber: 1, - }, - }, - }, - }, - }, - }; - - const result = codec.toJSON(schema, { - rootProp: BigInt('123'), - extra3: true, - objectValue: { objectProp: BigInt('456'), extra1: 'my-value', extra2: 'my-value' }, - arrayValue: [ - { arrayProp: BigInt('999'), extra4: true }, - { arrayProp: BigInt('879'), extra5: 987 }, - ], - }); - - expect(result).toEqual({ - rootProp: '123', - objectValue: { objectProp: '456' }, - arrayValue: [{ arrayProp: '999' }, { arrayProp: '879' }], - }); - }); -}); diff --git a/elements/lisk-codec/test/to_json.spec.ts b/elements/lisk-codec/test/to_json.spec.ts new file mode 100644 index 00000000000..dfb1f9008e2 --- /dev/null +++ b/elements/lisk-codec/test/to_json.spec.ts @@ -0,0 +1,79 @@ +/* + * Copyright © 2020 Lisk Foundation + * + * See the LICENSE file at the top-level directory of this distribution + * for licensing information. + * + * Unless otherwise agreed in a custom licensing agreement with the Lisk Foundation, + * no part of this software, including this file, may be copied, modified, + * propagated, or distributed except according to the terms contained in the + * LICENSE file. + * + * Removal or modification of this copyright notice is prohibited. + */ +import { codec } from '../src/codec'; +import { testCases as accountTestCases } from '../fixtures/account_encodings.json'; +import { getAccountFromJSON } from './utils'; + +describe('toJSON', () => { + it('should return JSON like object from JS object', () => { + const { schema, object } = accountTestCases[0].input; + + const jsObject = getAccountFromJSON(object); + + expect(codec.toJSON(schema, jsObject)).toEqual(object); + }); + + it('should ignore extra properties', () => { + const schema = { + $id: '/schema', + type: 'object', + required: ['rootProp', 'objectValue', 'arrayValue'], + properties: { + rootProp: { + dataType: 'uint64', + fieldNumber: 1, + }, + objectValue: { + type: 'object', + fieldNumber: 2, + properties: { + objectProp: { + dataType: 'uint64', + fieldNumber: 1, + }, + }, + }, + arrayValue: { + type: 'array', + fieldNumber: 3, + items: { + type: 'object', + properties: { + arrayProp: { + dataType: 'uint64', + fieldNumber: 1, + }, + }, + }, + }, + }, + }; + + const result = codec.toJSON(schema, { + rootProp: BigInt('123'), + extra3: true, + objectValue: { objectProp: BigInt('456'), extra1: 'my-value', extra2: 'my-value' }, + arrayValue: [ + { arrayProp: BigInt('999'), extra4: true }, + { arrayProp: BigInt('879'), extra5: 987 }, + ], + }); + + expect(result).toEqual({ + rootProp: '123', + objectValue: { objectProp: '456' }, + arrayValue: [{ arrayProp: '999' }, { arrayProp: '879' }], + }); + }); +}); diff --git a/elements/lisk-codec/test/utils.ts b/elements/lisk-codec/test/utils.ts new file mode 100644 index 00000000000..16c588a7b01 --- /dev/null +++ b/elements/lisk-codec/test/utils.ts @@ -0,0 +1,53 @@ +/* + * Copyright © 2021 Lisk Foundation + * + * See the LICENSE file at the top-level directory of this distribution + * for licensing information. + * + * Unless otherwise agreed in a custom licensing agreement with the Lisk Foundation, + * no part of this software, including this file, may be copied, modified, + * propagated, or distributed except according to the terms contained in the + * LICENSE file. + * + * Removal or modification of this copyright notice is prohibited. + */ + +export const buildTestCases = < + T extends { title?: string; description: string } | { title: string; description?: string } +>( + testCases: T[], +): T[] => + testCases.map(t => ({ + ...t, + toString: () => t.title ?? t.description, + })); + +export const getAccountFromJSON = (account: any) => ({ + ...account, + address: Buffer.from(account.address, 'hex'), + balance: BigInt(account.balance), + publicKey: Buffer.from(account.publicKey, 'hex'), + nonce: BigInt(account.nonce), + keys: { + ...account.keys, + mandatoryKeys: account.keys.mandatoryKeys.map((b: string) => Buffer.from(b, 'hex')), + optionalKeys: account.keys.optionalKeys.map((b: any) => Buffer.from(b.data, 'hex')), + }, + asset: { + ...account.asset, + delegate: { + ...account.asset.delegate, + totalVotesReceived: BigInt(account.asset.delegate.totalVotesReceived), + }, + sentVotes: account.asset.sentVotes.map((v: any) => ({ + ...v, + delegateAddress: Buffer.from(v.delegateAddress, 'hex'), + amount: BigInt(v.amount), + })), + unlocking: account.asset.unlocking.map((v: any) => ({ + ...v, + delegateAddress: Buffer.from(v.delegateAddress, 'hex'), + amount: BigInt(v.amount), + })), + }, +}); diff --git a/protocol-specs/generator_outputs/lisk_codec/account_decodings.json b/protocol-specs/generator_outputs/lisk_codec/account_decodings.json new file mode 100644 index 00000000000..7cd16633148 --- /dev/null +++ b/protocol-specs/generator_outputs/lisk_codec/account_decodings.json @@ -0,0 +1,361 @@ +{ + "title": "Decoding for account types supported by lisk-codec", + "summary": "Examples of encoding account with lisk-codec", + "config": { + "network": "devnet" + }, + "runner": "lisk_codec", + "handler": "account_decodings", + "testCases": [ + { + "description": "Decoding of valid account 1", + "input": { + "value": "0a14e11a11364738225813f86ea85214400e5db08d6e100a1a200fd3c50a6d3bd17ea806c0566cf6cf10f6e3697d9bda1820b00cb14746bcccef20052a4608021220c8b8fbe474a2b63ccb9744a409569b0a465ee1803f80435aec1c5e7fc2d4ee1812206115424fec0ce9c3bac5a81b5c782827d1f956fb95f1ccfa36c566d04e4d72673299010a180a07436174756c6c6f1201551820204028003080c6868f0112270a20cd32c73e9851c7137980063b8af64aa5a31651f8dcad258b682d2ddf091029e41080c2d72f12270a209d86ad24a3f030e5522b6598115bb4d70c1692c9d8995ddfccb377379a2d86c61080e59a771a2b0a20655e665765e3c42712d9a425b5b720d10457a5e45de0d4420e7c53ad73b02ef5108088debe01188001", + "schema": { + "$id": "accountSchema", + "type": "object", + "properties": { + "address": { + "dataType": "bytes", + "fieldNumber": 1 + }, + "balance": { + "dataType": "uint64", + "fieldNumber": 2 + }, + "publicKey": { + "dataType": "bytes", + "fieldNumber": 3 + }, + "nonce": { + "dataType": "uint64", + "fieldNumber": 4 + }, + "keys": { + "fieldNumber": 5, + "type": "object", + "properties": { + "numberOfSignatures": { + "dataType": "uint32", + "fieldNumber": 1 + }, + "mandatoryKeys": { + "type": "array", + "items": { + "dataType": "bytes" + }, + "fieldNumber": 2 + }, + "optionalKeys": { + "type": "array", + "items": { + "dataType": "bytes" + }, + "fieldNumber": 3 + } + }, + "required": ["numberOfSignatures", "mandatoryKeys", "optionalKeys"] + }, + "asset": { + "type": "object", + "fieldNumber": 6, + "properties": { + "delegate": { + "type": "object", + "fieldNumber": 1, + "properties": { + "username": { + "dataType": "string", + "fieldNumber": 1 + }, + "pomHeights": { + "type": "array", + "items": { + "dataType": "uint32" + }, + "fieldNumber": 2 + }, + "consecutiveMissedBlocks": { + "dataType": "uint32", + "fieldNumber": 3 + }, + "lastForgedHeight": { + "dataType": "uint32", + "fieldNumber": 4 + }, + "isBanned": { + "dataType": "boolean", + "fieldNumber": 5 + }, + "totalVotesReceived": { + "dataType": "uint64", + "fieldNumber": 6 + } + }, + "required": [ + "username", + "pomHeights", + "consecutiveMissedBlocks", + "lastForgedHeight", + "isBanned", + "totalVotesReceived" + ] + }, + "sentVotes": { + "type": "array", + "fieldNumber": 2, + "items": { + "type": "object", + "properties": { + "delegateAddress": { + "dataType": "bytes", + "fieldNumber": 1 + }, + "amount": { + "dataType": "uint64", + "fieldNumber": 2 + } + }, + "required": ["delegateAddress", "amount"] + } + }, + "unlocking": { + "type": "array", + "fieldNumber": 3, + "items": { + "type": "object", + "properties": { + "delegateAddress": { + "dataType": "bytes", + "fieldNumber": 1 + }, + "amount": { + "dataType": "uint64", + "fieldNumber": 2 + }, + "unvoteHeight": { + "dataType": "uint32", + "fieldNumber": 3 + } + }, + "required": ["delegateAddress", "amount", "unvoteHeight"] + } + } + } + } + }, + "required": ["address", "balance", "publicKey", "nonce", "keys", "asset"] + } + }, + "output": { + "object": { + "address": "e11a11364738225813f86ea85214400e5db08d6e", + "balance": "10", + "publicKey": "0fd3c50a6d3bd17ea806c0566cf6cf10f6e3697d9bda1820b00cb14746bcccef", + "nonce": "5", + "keys": { + "numberOfSignatures": 2, + "mandatoryKeys": [ + "c8b8fbe474a2b63ccb9744a409569b0a465ee1803f80435aec1c5e7fc2d4ee18", + "6115424fec0ce9c3bac5a81b5c782827d1f956fb95f1ccfa36c566d04e4d7267" + ], + "optionalKeys": [] + }, + "asset": { + "delegate": { + "username": "Catullo", + "pomHeights": [85], + "consecutiveMissedBlocks": 32, + "lastForgedHeight": 64, + "isBanned": false, + "totalVotesReceived": "300000000" + }, + "sentVotes": [ + { + "delegateAddress": "cd32c73e9851c7137980063b8af64aa5a31651f8dcad258b682d2ddf091029e4", + "amount": "100000000" + }, + { + "delegateAddress": "9d86ad24a3f030e5522b6598115bb4d70c1692c9d8995ddfccb377379a2d86c6", + "amount": "250000000" + } + ], + "unlocking": [ + { + "delegateAddress": "655e665765e3c42712d9a425b5b720d10457a5e45de0d4420e7c53ad73b02ef5", + "amount": "400000000", + "unvoteHeight": 128 + } + ] + } + } + } + }, + { + "description": "Decoding of valid default account", + "input": { + "value": "0a14cd32c73e9851c7137980063b8af64aa5a31651f810001a0020002a020800320c0a0a0a001800200028003000", + "schema": { + "$id": "accountSchema", + "type": "object", + "properties": { + "address": { + "dataType": "bytes", + "fieldNumber": 1 + }, + "balance": { + "dataType": "uint64", + "fieldNumber": 2 + }, + "publicKey": { + "dataType": "bytes", + "fieldNumber": 3 + }, + "nonce": { + "dataType": "uint64", + "fieldNumber": 4 + }, + "keys": { + "fieldNumber": 5, + "type": "object", + "properties": { + "numberOfSignatures": { + "dataType": "uint32", + "fieldNumber": 1 + }, + "mandatoryKeys": { + "type": "array", + "items": { + "dataType": "bytes" + }, + "fieldNumber": 2 + }, + "optionalKeys": { + "type": "array", + "items": { + "dataType": "bytes" + }, + "fieldNumber": 3 + } + }, + "required": ["numberOfSignatures", "mandatoryKeys", "optionalKeys"] + }, + "asset": { + "type": "object", + "fieldNumber": 6, + "properties": { + "delegate": { + "type": "object", + "fieldNumber": 1, + "properties": { + "username": { + "dataType": "string", + "fieldNumber": 1 + }, + "pomHeights": { + "type": "array", + "items": { + "dataType": "uint32" + }, + "fieldNumber": 2 + }, + "consecutiveMissedBlocks": { + "dataType": "uint32", + "fieldNumber": 3 + }, + "lastForgedHeight": { + "dataType": "uint32", + "fieldNumber": 4 + }, + "isBanned": { + "dataType": "boolean", + "fieldNumber": 5 + }, + "totalVotesReceived": { + "dataType": "uint64", + "fieldNumber": 6 + } + }, + "required": [ + "username", + "pomHeights", + "consecutiveMissedBlocks", + "lastForgedHeight", + "isBanned", + "totalVotesReceived" + ] + }, + "sentVotes": { + "type": "array", + "fieldNumber": 2, + "items": { + "type": "object", + "properties": { + "delegateAddress": { + "dataType": "bytes", + "fieldNumber": 1 + }, + "amount": { + "dataType": "uint64", + "fieldNumber": 2 + } + }, + "required": ["delegateAddress", "amount"] + } + }, + "unlocking": { + "type": "array", + "fieldNumber": 3, + "items": { + "type": "object", + "properties": { + "delegateAddress": { + "dataType": "bytes", + "fieldNumber": 1 + }, + "amount": { + "dataType": "uint64", + "fieldNumber": 2 + }, + "unvoteHeight": { + "dataType": "uint32", + "fieldNumber": 3 + } + }, + "required": ["delegateAddress", "amount", "unvoteHeight"] + } + } + } + } + }, + "required": ["address", "balance", "publicKey", "nonce", "keys", "asset"] + } + }, + "output": { + "object": { + "address": "cd32c73e9851c7137980063b8af64aa5a31651f8", + "balance": "0", + "publicKey": "", + "nonce": "0", + "keys": { + "numberOfSignatures": 0, + "mandatoryKeys": [], + "optionalKeys": [] + }, + "asset": { + "delegate": { + "username": "", + "pomHeights": [], + "consecutiveMissedBlocks": 0, + "lastForgedHeight": 0, + "isBanned": false, + "totalVotesReceived": "0" + }, + "sentVotes": [], + "unlocking": [] + } + } + } + } + ] +} diff --git a/protocol-specs/generator_outputs/lisk_codec/account_encodings.json b/protocol-specs/generator_outputs/lisk_codec/account_encodings.json index 6bae00c1fe5..ecac37159eb 100644 --- a/protocol-specs/generator_outputs/lisk_codec/account_encodings.json +++ b/protocol-specs/generator_outputs/lisk_codec/account_encodings.json @@ -1,5 +1,5 @@ { - "title": "Encondings for account types supported by lisk-codec", + "title": "Encoding for account types supported by lisk-codec", "summary": "Examples of encoding account with lisk-codec", "config": { "network": "devnet" @@ -11,147 +11,15 @@ "description": "Encoding of valid account 1", "input": { "object": { - "address": { - "type": "Buffer", - "data": [ - 225, - 26, - 17, - 54, - 71, - 56, - 34, - 88, - 19, - 248, - 110, - 168, - 82, - 20, - 64, - 14, - 93, - 176, - 141, - 110 - ] - }, - "balance": 10, - "publicKey": { - "type": "Buffer", - "data": [ - 15, - 211, - 197, - 10, - 109, - 59, - 209, - 126, - 168, - 6, - 192, - 86, - 108, - 246, - 207, - 16, - 246, - 227, - 105, - 125, - 155, - 218, - 24, - 32, - 176, - 12, - 177, - 71, - 70, - 188, - 204, - 239 - ] - }, - "nonce": 5, + "address": "e11a11364738225813f86ea85214400e5db08d6e", + "balance": "10", + "publicKey": "0fd3c50a6d3bd17ea806c0566cf6cf10f6e3697d9bda1820b00cb14746bcccef", + "nonce": "5", "keys": { "numberOfSignatures": 2, "mandatoryKeys": [ - { - "type": "Buffer", - "data": [ - 200, - 184, - 251, - 228, - 116, - 162, - 182, - 60, - 203, - 151, - 68, - 164, - 9, - 86, - 155, - 10, - 70, - 94, - 225, - 128, - 63, - 128, - 67, - 90, - 236, - 28, - 94, - 127, - 194, - 212, - 238, - 24 - ] - }, - { - "type": "Buffer", - "data": [ - 97, - 21, - 66, - 79, - 236, - 12, - 233, - 195, - 186, - 197, - 168, - 27, - 92, - 120, - 40, - 39, - 209, - 249, - 86, - 251, - 149, - 241, - 204, - 250, - 54, - 197, - 102, - 208, - 78, - 77, - 114, - 103 - ] - } + "c8b8fbe474a2b63ccb9744a409569b0a465ee1803f80435aec1c5e7fc2d4ee18", + "6115424fec0ce9c3bac5a81b5c782827d1f956fb95f1ccfa36c566d04e4d7267" ], "optionalKeys": [] }, @@ -162,130 +30,22 @@ "consecutiveMissedBlocks": 32, "lastForgedHeight": 64, "isBanned": false, - "totalVotesReceived": 300000000 + "totalVotesReceived": "300000000" }, "sentVotes": [ { - "delegateAddress": { - "type": "Buffer", - "data": [ - 205, - 50, - 199, - 62, - 152, - 81, - 199, - 19, - 121, - 128, - 6, - 59, - 138, - 246, - 74, - 165, - 163, - 22, - 81, - 248, - 220, - 173, - 37, - 139, - 104, - 45, - 45, - 223, - 9, - 16, - 41, - 228 - ] - }, - "amount": 100000000 + "delegateAddress": "cd32c73e9851c7137980063b8af64aa5a31651f8dcad258b682d2ddf091029e4", + "amount": "100000000" }, { - "delegateAddress": { - "type": "Buffer", - "data": [ - 157, - 134, - 173, - 36, - 163, - 240, - 48, - 229, - 82, - 43, - 101, - 152, - 17, - 91, - 180, - 215, - 12, - 22, - 146, - 201, - 216, - 153, - 93, - 223, - 204, - 179, - 119, - 55, - 154, - 45, - 134, - 198 - ] - }, - "amount": 250000000 + "delegateAddress": "9d86ad24a3f030e5522b6598115bb4d70c1692c9d8995ddfccb377379a2d86c6", + "amount": "250000000" } ], "unlocking": [ { - "delegateAddress": { - "type": "Buffer", - "data": [ - 101, - 94, - 102, - 87, - 101, - 227, - 196, - 39, - 18, - 217, - 164, - 37, - 181, - 183, - 32, - 209, - 4, - 87, - 165, - 228, - 93, - 224, - 212, - 66, - 14, - 124, - 83, - 173, - 115, - 176, - 46, - 245 - ] - }, - "amount": 400000000, + "delegateAddress": "655e665765e3c42712d9a425b5b720d10457a5e45de0d4420e7c53ad73b02ef5", + "amount": "400000000", "unvoteHeight": 128 } ] @@ -435,37 +195,10 @@ "description": "Encoding of valid default account", "input": { "object": { - "address": { - "type": "Buffer", - "data": [ - 205, - 50, - 199, - 62, - 152, - 81, - 199, - 19, - 121, - 128, - 6, - 59, - 138, - 246, - 74, - 165, - 163, - 22, - 81, - 248 - ] - }, - "balance": 0, - "publicKey": { - "type": "Buffer", - "data": [] - }, - "nonce": 0, + "address": "cd32c73e9851c7137980063b8af64aa5a31651f8", + "balance": "0", + "publicKey": "", + "nonce": "0", "keys": { "numberOfSignatures": 0, "mandatoryKeys": [], @@ -478,7 +211,7 @@ "consecutiveMissedBlocks": 0, "lastForgedHeight": 0, "isBanned": false, - "totalVotesReceived": 0 + "totalVotesReceived": "0" }, "sentVotes": [], "unlocking": [] diff --git a/protocol-specs/generator_outputs/lisk_codec/arrays_decodings.json b/protocol-specs/generator_outputs/lisk_codec/arrays_decodings.json new file mode 100644 index 00000000000..dbf2ad97b3a --- /dev/null +++ b/protocol-specs/generator_outputs/lisk_codec/arrays_decodings.json @@ -0,0 +1,150 @@ +{ + "title": "Decoding for arrays types supported by lisk-codec", + "summary": "Examples of encoding arrays with lisk-codec", + "config": { + "network": "devnet" + }, + "runner": "lisk_codec", + "handler": "arrays_decodings", + "testCases": [ + { + "description": "Decoding of integers array", + "input": { + "value": "0a09030104010509020605", + "schema": { + "type": "object", + "$id": "array-schema-uint32", + "properties": { + "list": { + "type": "array", + "items": { + "dataType": "uint32" + }, + "fieldNumber": 1 + } + } + } + }, + "output": { + "object": { + "list": [3, 1, 4, 1, 5, 9, 2, 6, 5] + } + } + }, + { + "description": "Decoding of booleans array", + "input": { + "value": "0a06010100010000", + "schema": { + "type": "object", + "$id": "array-schema-boolean", + "properties": { + "list": { + "type": "array", + "items": { + "dataType": "boolean" + }, + "fieldNumber": 1 + } + } + } + }, + "output": { + "object": { + "list": [true, true, false, true, false, false] + } + } + }, + { + "description": "Decoding of strings array", + "input": { + "value": "0a046c69736b0a000a07676f676f676f67", + "schema": { + "type": "object", + "$id": "array-schema-string", + "properties": { + "list": { + "type": "array", + "items": { + "dataType": "string" + }, + "fieldNumber": 1 + } + } + } + }, + "output": { + "object": { + "list": ["lisk", "", "gogogog"] + } + } + }, + { + "description": "Decoding of objects array", + "input": { + "value": "0a2e0a286531316131313336343733383232353831336638366561383532313434303065356462303864366510a08d060a2e0a286161326131313336343733383232353831336638366561383532313434303065356462303866666610e0a712", + "schema": { + "type": "object", + "$id": "array-schema-object", + "properties": { + "list": { + "type": "array", + "items": { + "type": "object", + "properties": { + "address": { + "dataType": "string", + "fieldNumber": 1 + }, + "amount": { + "dataType": "uint64", + "fieldNumber": 2 + } + } + }, + "fieldNumber": 1 + } + } + } + }, + "output": { + "object": { + "list": [ + { + "address": "e11a11364738225813f86ea85214400e5db08d6e", + "amount": "100000" + }, + { + "address": "aa2a11364738225813f86ea85214400e5db08fff", + "amount": "300000" + } + ] + } + } + }, + { + "description": "Decoding of empty array", + "input": { + "value": "", + "schema": { + "type": "object", + "$id": "array-schema-uint32", + "properties": { + "list": { + "type": "array", + "items": { + "dataType": "uint32" + }, + "fieldNumber": 1 + } + } + } + }, + "output": { + "object": { + "list": [] + } + } + } + ] +} diff --git a/protocol-specs/generator_outputs/lisk_codec/arrays_encodings.json b/protocol-specs/generator_outputs/lisk_codec/arrays_encodings.json index 0abac4ab80d..2fe123a565b 100644 --- a/protocol-specs/generator_outputs/lisk_codec/arrays_encodings.json +++ b/protocol-specs/generator_outputs/lisk_codec/arrays_encodings.json @@ -1,5 +1,5 @@ { - "title": "Encondings for arrays types supported by lisk-codec", + "title": "Encoding for arrays types supported by lisk-codec", "summary": "Examples of encoding arrays with lisk-codec", "config": { "network": "devnet" @@ -15,7 +15,7 @@ }, "schema": { "type": "object", - "$id": "arrayUint32", + "$id": "array-schema-uint32", "properties": { "list": { "type": "array", @@ -39,7 +39,7 @@ }, "schema": { "type": "object", - "$id": "arrayBoolean", + "$id": "array-schema-boolean", "properties": { "list": { "type": "array", @@ -56,14 +56,14 @@ } }, { - "description": "Encoding of strings array", + "description": "arrayStrings of strings array", "input": { "object": { "list": ["lisk", "", "gogogog"] }, "schema": { "type": "object", - "$id": "arrayStrings", + "$id": "array-schema-string", "properties": { "list": { "type": "array", @@ -83,24 +83,23 @@ "description": "Encoding of objects array", "input": { "object": { - "myArray": [ + "list": [ { "address": "e11a11364738225813f86ea85214400e5db08d6e", - "amount": 100000 + "amount": "100000" }, { "address": "aa2a11364738225813f86ea85214400e5db08fff", - "amount": 300000 + "amount": "300000" } ] }, "schema": { - "$id": "arrayObject", "type": "object", + "$id": "array-schema-object", "properties": { - "myArray": { + "list": { "type": "array", - "fieldNumber": 1, "items": { "type": "object", "properties": { @@ -113,7 +112,8 @@ "fieldNumber": 2 } } - } + }, + "fieldNumber": 1 } } } @@ -130,7 +130,7 @@ }, "schema": { "type": "object", - "$id": "emptyArray", + "$id": "array-schema-uint32", "properties": { "list": { "type": "array", diff --git a/protocol-specs/generator_outputs/lisk_codec/block_asset_decodings.json b/protocol-specs/generator_outputs/lisk_codec/block_asset_decodings.json new file mode 100644 index 00000000000..a183bf98608 --- /dev/null +++ b/protocol-specs/generator_outputs/lisk_codec/block_asset_decodings.json @@ -0,0 +1,75 @@ +{ + "title": "Decoding for block asset types supported by lisk-codec", + "summary": "Examples of encoding block asset with lisk-codec", + "config": { + "network": "devnet" + }, + "runner": "lisk_codec", + "handler": "block_asset_decodings", + "testCases": [ + { + "description": "Decoding of valid block asset", + "input": { + "value": "08990810b9ff361a10d59386e0ae435e292fbe0ebcdb954b75", + "schema": { + "$id": "blockAssetSchema", + "type": "object", + "properties": { + "maxHeightPreviouslyForged": { + "dataType": "uint32", + "fieldNumber": 1 + }, + "maxHeightPrevoted": { + "dataType": "uint32", + "fieldNumber": 2 + }, + "seedReveal": { + "dataType": "bytes", + "fieldNumber": 3 + } + }, + "required": ["maxHeightPreviouslyForged", "maxHeightPrevoted", "seedReveal"] + } + }, + "output": { + "object": { + "maxHeightPreviouslyForged": 1049, + "maxHeightPrevoted": 901049, + "seedReveal": "d59386e0ae435e292fbe0ebcdb954b75" + } + } + }, + { + "description": "Decoding of valid block asset with zero previously forged", + "input": { + "value": "08001099081a10eaaf9d4c65cb501c811ef812847a5551", + "schema": { + "$id": "blockAssetSchema", + "type": "object", + "properties": { + "maxHeightPreviouslyForged": { + "dataType": "uint32", + "fieldNumber": 1 + }, + "maxHeightPrevoted": { + "dataType": "uint32", + "fieldNumber": 2 + }, + "seedReveal": { + "dataType": "bytes", + "fieldNumber": 3 + } + }, + "required": ["maxHeightPreviouslyForged", "maxHeightPrevoted", "seedReveal"] + } + }, + "output": { + "object": { + "maxHeightPreviouslyForged": 0, + "maxHeightPrevoted": 1049, + "seedReveal": "eaaf9d4c65cb501c811ef812847a5551" + } + } + } + ] +} diff --git a/protocol-specs/generator_outputs/lisk_codec/block_asset_encodings.json b/protocol-specs/generator_outputs/lisk_codec/block_asset_encodings.json index e9af9c0c7a2..b8e766a425b 100644 --- a/protocol-specs/generator_outputs/lisk_codec/block_asset_encodings.json +++ b/protocol-specs/generator_outputs/lisk_codec/block_asset_encodings.json @@ -1,5 +1,5 @@ { - "title": "Encondings for block asset types supported by lisk-codec", + "title": "Encoding for block asset types supported by lisk-codec", "summary": "Examples of encoding block asset with lisk-codec", "config": { "network": "devnet" @@ -13,10 +13,7 @@ "object": { "maxHeightPreviouslyForged": 1049, "maxHeightPrevoted": 901049, - "seedReveal": { - "type": "Buffer", - "data": [213, 147, 134, 224, 174, 67, 94, 41, 47, 190, 14, 188, 219, 149, 75, 117] - } + "seedReveal": "d59386e0ae435e292fbe0ebcdb954b75" }, "schema": { "$id": "blockAssetSchema", @@ -48,10 +45,7 @@ "object": { "maxHeightPreviouslyForged": 0, "maxHeightPrevoted": 1049, - "seedReveal": { - "type": "Buffer", - "data": [234, 175, 157, 76, 101, 203, 80, 28, 129, 30, 248, 18, 132, 122, 85, 81] - } + "seedReveal": "eaaf9d4c65cb501c811ef812847a5551" }, "schema": { "$id": "blockAssetSchema", diff --git a/protocol-specs/generator_outputs/lisk_codec/block_decodings.json b/protocol-specs/generator_outputs/lisk_codec/block_decodings.json new file mode 100644 index 00000000000..f170ef09416 --- /dev/null +++ b/protocol-specs/generator_outputs/lisk_codec/block_decodings.json @@ -0,0 +1,74 @@ +{ + "title": "Decoding for block types supported by lisk-codec", + "summary": "Examples of encoding block with lisk-codec", + "config": { + "network": "devnet" + }, + "runner": "lisk_codec", + "handler": "block_decodings", + "testCases": [ + { + "description": "Decoding of valid block with payload", + "input": { + "value": "0a20ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad1220a665a45920422f9d417e4867efdc4fb8a04a1f3fff1fa07e998e86f7f7a27ae3122068a751863fe73b8ede8d832be628ff680d617fa15c74d00142f9025d5f37dd50", + "schema": { + "$id": "blockSchema", + "type": "object", + "properties": { + "header": { + "dataType": "bytes", + "fieldNumber": 1 + }, + "payload": { + "type": "array", + "items": { + "dataType": "bytes" + }, + "fieldNumber": 2 + } + }, + "required": ["header", "payload"] + } + }, + "output": { + "object": { + "header": "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad", + "payload": [ + "a665a45920422f9d417e4867efdc4fb8a04a1f3fff1fa07e998e86f7f7a27ae3", + "68a751863fe73b8ede8d832be628ff680d617fa15c74d00142f9025d5f37dd50" + ] + } + } + }, + { + "description": "Decoding of valid block block without payload", + "input": { + "value": "0a20e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", + "schema": { + "$id": "blockSchema", + "type": "object", + "properties": { + "header": { + "dataType": "bytes", + "fieldNumber": 1 + }, + "payload": { + "type": "array", + "items": { + "dataType": "bytes" + }, + "fieldNumber": 2 + } + }, + "required": ["header", "payload"] + } + }, + "output": { + "object": { + "header": "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", + "payload": [] + } + } + } + ] +} diff --git a/protocol-specs/generator_outputs/lisk_codec/block_encodings.json b/protocol-specs/generator_outputs/lisk_codec/block_encodings.json index a054260fc4f..12bd39c8ae6 100644 --- a/protocol-specs/generator_outputs/lisk_codec/block_encodings.json +++ b/protocol-specs/generator_outputs/lisk_codec/block_encodings.json @@ -1,5 +1,5 @@ { - "title": "Encondings for block types supported by lisk-codec", + "title": "Encoding for block types supported by lisk-codec", "summary": "Examples of encoding block with lisk-codec", "config": { "network": "devnet" @@ -11,118 +11,10 @@ "description": "Encoding of valid block with payload", "input": { "object": { - "header": { - "type": "Buffer", - "data": [ - 186, - 120, - 22, - 191, - 143, - 1, - 207, - 234, - 65, - 65, - 64, - 222, - 93, - 174, - 34, - 35, - 176, - 3, - 97, - 163, - 150, - 23, - 122, - 156, - 180, - 16, - 255, - 97, - 242, - 0, - 21, - 173 - ] - }, + "header": "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad", "payload": [ - { - "type": "Buffer", - "data": [ - 166, - 101, - 164, - 89, - 32, - 66, - 47, - 157, - 65, - 126, - 72, - 103, - 239, - 220, - 79, - 184, - 160, - 74, - 31, - 63, - 255, - 31, - 160, - 126, - 153, - 142, - 134, - 247, - 247, - 162, - 122, - 227 - ] - }, - { - "type": "Buffer", - "data": [ - 104, - 167, - 81, - 134, - 63, - 231, - 59, - 142, - 222, - 141, - 131, - 43, - 230, - 40, - 255, - 104, - 13, - 97, - 127, - 161, - 92, - 116, - 208, - 1, - 66, - 249, - 2, - 93, - 95, - 55, - 221, - 80 - ] - } + "a665a45920422f9d417e4867efdc4fb8a04a1f3fff1fa07e998e86f7f7a27ae3", + "68a751863fe73b8ede8d832be628ff680d617fa15c74d00142f9025d5f37dd50" ] }, "schema": { @@ -152,43 +44,7 @@ "description": "Encoding of valid block block without payload", "input": { "object": { - "header": { - "type": "Buffer", - "data": [ - 227, - 176, - 196, - 66, - 152, - 252, - 28, - 20, - 154, - 251, - 244, - 200, - 153, - 111, - 185, - 36, - 39, - 174, - 65, - 228, - 100, - 155, - 147, - 76, - 164, - 149, - 153, - 27, - 120, - 82, - 184, - 85 - ] - }, + "header": "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", "payload": [] }, "schema": { diff --git a/protocol-specs/generator_outputs/lisk_codec/block_header_decodings.json b/protocol-specs/generator_outputs/lisk_codec/block_header_decodings.json new file mode 100644 index 00000000000..7ddedd3018a --- /dev/null +++ b/protocol-specs/generator_outputs/lisk_codec/block_header_decodings.json @@ -0,0 +1,153 @@ +{ + "title": "Decoding for block header types supported by lisk-codec", + "summary": "Examples of encoding block header with lisk-codec", + "config": { + "network": "devnet" + }, + "runner": "lisk_codec", + "handler": "block_header_decodings", + "testCases": [ + { + "description": "Decoding of valid block header 1", + "input": { + "value": "08011085f6b7f60518c3faf3052220ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad2a20a665a45920422f9d417e4867efdc4fb8a04a1f3fff1fa07e998e86f7f7a27ae3322068a751863fe73b8ede8d832be628ff680d617fa15c74d00142f9025d5f37dd50388088debe014220d59386e0ae435e292fbe0ebcdb954b75ed5fb3922091277cb19f798fc5d507184a408331b5123cac056e2ec8361c56e642db0ca0e13abe33696d23d4d00ad6de844919296e87abe8e172f67fd882b4c0b1c1804b7d9075ecf975cf2631d8d7efef0c", + "schema": { + "$id": "blockHeaderSchema", + "type": "object", + "properties": { + "version": { + "dataType": "uint32", + "fieldNumber": 1 + }, + "timestamp": { + "dataType": "uint32", + "fieldNumber": 2 + }, + "height": { + "dataType": "uint32", + "fieldNumber": 3 + }, + "previousBlockID": { + "dataType": "bytes", + "fieldNumber": 4 + }, + "transactionRoot": { + "dataType": "bytes", + "fieldNumber": 5 + }, + "generatorPublicKey": { + "dataType": "bytes", + "fieldNumber": 6 + }, + "reward": { + "dataType": "uint64", + "fieldNumber": 7 + }, + "asset": { + "dataType": "bytes", + "fieldNumber": 8 + }, + "signature": { + "dataType": "bytes", + "fieldNumber": 9 + } + }, + "required": [ + "version", + "timestamp", + "height", + "previousBlockID", + "transactionRoot", + "generatorPublicKey", + "reward", + "asset" + ] + } + }, + "output": { + "object": { + "version": 1, + "timestamp": 1590557445, + "height": 12385603, + "previousBlockID": "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad", + "transactionRoot": "a665a45920422f9d417e4867efdc4fb8a04a1f3fff1fa07e998e86f7f7a27ae3", + "generatorPublicKey": "68a751863fe73b8ede8d832be628ff680d617fa15c74d00142f9025d5f37dd50", + "reward": "400000000", + "asset": "d59386e0ae435e292fbe0ebcdb954b75ed5fb3922091277cb19f798fc5d50718", + "signature": "8331b5123cac056e2ec8361c56e642db0ca0e13abe33696d23d4d00ad6de844919296e87abe8e172f67fd882b4c0b1c1804b7d9075ecf975cf2631d8d7efef0c" + } + } + }, + { + "description": "Decoding of valid block header 2", + "input": { + "value": "080310ecf8b7f60518b9ff362220e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b8552a20e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b8553220acc56344dea609e80cf5d4165e46917104fe701927847fc2a5d40e37574b2b38388088debe014220eaaf9d4c65cb501c811ef812847a55513181474d734ead1b95b7e1e5b574d2234a401e65032943af975c3cdef94b1fce639645bddb29265321e0277a0f48143ef7f6f6daa1046234a09cc593969ff04d8d082edd15a4a9b90a7b8865fcd9dac44300", + "schema": { + "$id": "blockHeaderSchema", + "type": "object", + "properties": { + "version": { + "dataType": "uint32", + "fieldNumber": 1 + }, + "timestamp": { + "dataType": "uint32", + "fieldNumber": 2 + }, + "height": { + "dataType": "uint32", + "fieldNumber": 3 + }, + "previousBlockID": { + "dataType": "bytes", + "fieldNumber": 4 + }, + "transactionRoot": { + "dataType": "bytes", + "fieldNumber": 5 + }, + "generatorPublicKey": { + "dataType": "bytes", + "fieldNumber": 6 + }, + "reward": { + "dataType": "uint64", + "fieldNumber": 7 + }, + "asset": { + "dataType": "bytes", + "fieldNumber": 8 + }, + "signature": { + "dataType": "bytes", + "fieldNumber": 9 + } + }, + "required": [ + "version", + "timestamp", + "height", + "previousBlockID", + "transactionRoot", + "generatorPublicKey", + "reward", + "asset" + ] + } + }, + "output": { + "object": { + "version": 3, + "timestamp": 1590557804, + "height": 901049, + "previousBlockID": "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", + "transactionRoot": "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", + "generatorPublicKey": "acc56344dea609e80cf5d4165e46917104fe701927847fc2a5d40e37574b2b38", + "reward": "400000000", + "asset": "eaaf9d4c65cb501c811ef812847a55513181474d734ead1b95b7e1e5b574d223", + "signature": "1e65032943af975c3cdef94b1fce639645bddb29265321e0277a0f48143ef7f6f6daa1046234a09cc593969ff04d8d082edd15a4a9b90a7b8865fcd9dac44300" + } + } + } + ] +} diff --git a/protocol-specs/generator_outputs/lisk_codec/block_header_encodings.json b/protocol-specs/generator_outputs/lisk_codec/block_header_encodings.json index f60081b8e85..0ab2d62341e 100644 --- a/protocol-specs/generator_outputs/lisk_codec/block_header_encodings.json +++ b/protocol-specs/generator_outputs/lisk_codec/block_header_encodings.json @@ -1,5 +1,5 @@ { - "title": "Encondings for block header types supported by lisk-codec", + "title": "Encoding for block header types supported by lisk-codec", "summary": "Examples of encoding block header with lisk-codec", "config": { "network": "devnet" @@ -14,224 +14,12 @@ "version": 1, "timestamp": 1590557445, "height": 12385603, - "previousBlockID": { - "type": "Buffer", - "data": [ - 186, - 120, - 22, - 191, - 143, - 1, - 207, - 234, - 65, - 65, - 64, - 222, - 93, - 174, - 34, - 35, - 176, - 3, - 97, - 163, - 150, - 23, - 122, - 156, - 180, - 16, - 255, - 97, - 242, - 0, - 21, - 173 - ] - }, - "transactionRoot": { - "type": "Buffer", - "data": [ - 166, - 101, - 164, - 89, - 32, - 66, - 47, - 157, - 65, - 126, - 72, - 103, - 239, - 220, - 79, - 184, - 160, - 74, - 31, - 63, - 255, - 31, - 160, - 126, - 153, - 142, - 134, - 247, - 247, - 162, - 122, - 227 - ] - }, - "generatorPublicKey": { - "type": "Buffer", - "data": [ - 104, - 167, - 81, - 134, - 63, - 231, - 59, - 142, - 222, - 141, - 131, - 43, - 230, - 40, - 255, - 104, - 13, - 97, - 127, - 161, - 92, - 116, - 208, - 1, - 66, - 249, - 2, - 93, - 95, - 55, - 221, - 80 - ] - }, - "reward": 400000000, - "asset": { - "type": "Buffer", - "data": [ - 213, - 147, - 134, - 224, - 174, - 67, - 94, - 41, - 47, - 190, - 14, - 188, - 219, - 149, - 75, - 117, - 237, - 95, - 179, - 146, - 32, - 145, - 39, - 124, - 177, - 159, - 121, - 143, - 197, - 213, - 7, - 24 - ] - }, - "signature": { - "type": "Buffer", - "data": [ - 131, - 49, - 181, - 18, - 60, - 172, - 5, - 110, - 46, - 200, - 54, - 28, - 86, - 230, - 66, - 219, - 12, - 160, - 225, - 58, - 190, - 51, - 105, - 109, - 35, - 212, - 208, - 10, - 214, - 222, - 132, - 73, - 25, - 41, - 110, - 135, - 171, - 232, - 225, - 114, - 246, - 127, - 216, - 130, - 180, - 192, - 177, - 193, - 128, - 75, - 125, - 144, - 117, - 236, - 249, - 117, - 207, - 38, - 49, - 216, - 215, - 239, - 239, - 12 - ] - } + "previousBlockID": "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad", + "transactionRoot": "a665a45920422f9d417e4867efdc4fb8a04a1f3fff1fa07e998e86f7f7a27ae3", + "generatorPublicKey": "68a751863fe73b8ede8d832be628ff680d617fa15c74d00142f9025d5f37dd50", + "reward": "400000000", + "asset": "d59386e0ae435e292fbe0ebcdb954b75ed5fb3922091277cb19f798fc5d50718", + "signature": "8331b5123cac056e2ec8361c56e642db0ca0e13abe33696d23d4d00ad6de844919296e87abe8e172f67fd882b4c0b1c1804b7d9075ecf975cf2631d8d7efef0c" }, "schema": { "$id": "blockHeaderSchema", @@ -297,224 +85,12 @@ "version": 3, "timestamp": 1590557804, "height": 901049, - "previousBlockID": { - "type": "Buffer", - "data": [ - 227, - 176, - 196, - 66, - 152, - 252, - 28, - 20, - 154, - 251, - 244, - 200, - 153, - 111, - 185, - 36, - 39, - 174, - 65, - 228, - 100, - 155, - 147, - 76, - 164, - 149, - 153, - 27, - 120, - 82, - 184, - 85 - ] - }, - "transactionRoot": { - "type": "Buffer", - "data": [ - 227, - 176, - 196, - 66, - 152, - 252, - 28, - 20, - 154, - 251, - 244, - 200, - 153, - 111, - 185, - 36, - 39, - 174, - 65, - 228, - 100, - 155, - 147, - 76, - 164, - 149, - 153, - 27, - 120, - 82, - 184, - 85 - ] - }, - "generatorPublicKey": { - "type": "Buffer", - "data": [ - 172, - 197, - 99, - 68, - 222, - 166, - 9, - 232, - 12, - 245, - 212, - 22, - 94, - 70, - 145, - 113, - 4, - 254, - 112, - 25, - 39, - 132, - 127, - 194, - 165, - 212, - 14, - 55, - 87, - 75, - 43, - 56 - ] - }, - "reward": 400000000, - "asset": { - "type": "Buffer", - "data": [ - 234, - 175, - 157, - 76, - 101, - 203, - 80, - 28, - 129, - 30, - 248, - 18, - 132, - 122, - 85, - 81, - 49, - 129, - 71, - 77, - 115, - 78, - 173, - 27, - 149, - 183, - 225, - 229, - 181, - 116, - 210, - 35 - ] - }, - "signature": { - "type": "Buffer", - "data": [ - 30, - 101, - 3, - 41, - 67, - 175, - 151, - 92, - 60, - 222, - 249, - 75, - 31, - 206, - 99, - 150, - 69, - 189, - 219, - 41, - 38, - 83, - 33, - 224, - 39, - 122, - 15, - 72, - 20, - 62, - 247, - 246, - 246, - 218, - 161, - 4, - 98, - 52, - 160, - 156, - 197, - 147, - 150, - 159, - 240, - 77, - 141, - 8, - 46, - 221, - 21, - 164, - 169, - 185, - 10, - 123, - 136, - 101, - 252, - 217, - 218, - 196, - 67, - 0 - ] - } + "previousBlockID": "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", + "transactionRoot": "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", + "generatorPublicKey": "acc56344dea609e80cf5d4165e46917104fe701927847fc2a5d40e37574b2b38", + "reward": "400000000", + "asset": "eaaf9d4c65cb501c811ef812847a55513181474d734ead1b95b7e1e5b574d223", + "signature": "1e65032943af975c3cdef94b1fce639645bddb29265321e0277a0f48143ef7f6f6daa1046234a09cc593969ff04d8d082edd15a4a9b90a7b8865fcd9dac44300" }, "schema": { "$id": "blockHeaderSchema", diff --git a/protocol-specs/generator_outputs/lisk_codec/boolean_decodings.json b/protocol-specs/generator_outputs/lisk_codec/boolean_decodings.json new file mode 100644 index 00000000000..63978441a2d --- /dev/null +++ b/protocol-specs/generator_outputs/lisk_codec/boolean_decodings.json @@ -0,0 +1,53 @@ +{ + "title": "Decoding for boolean types supported by lisk-codec", + "summary": "Examples of encoding booleans with lisk-codec", + "config": { + "network": "devnet" + }, + "runner": "lisk_codec", + "handler": "boolean_decodings", + "testCases": [ + { + "description": "Decoding of boolean with value true", + "input": { + "value": "0801", + "schema": { + "$id": "object5", + "type": "object", + "properties": { + "state": { + "dataType": "boolean", + "fieldNumber": 1 + } + } + } + }, + "output": { + "object": { + "state": true + } + } + }, + { + "description": "Decoding of boolean with value false", + "input": { + "value": "0800", + "schema": { + "$id": "object5", + "type": "object", + "properties": { + "state": { + "dataType": "boolean", + "fieldNumber": 1 + } + } + } + }, + "output": { + "object": { + "state": false + } + } + } + ] +} diff --git a/protocol-specs/generator_outputs/lisk_codec/boolean_encodings.json b/protocol-specs/generator_outputs/lisk_codec/boolean_encodings.json index 4eb3466ba67..c1e5a1c7f42 100644 --- a/protocol-specs/generator_outputs/lisk_codec/boolean_encodings.json +++ b/protocol-specs/generator_outputs/lisk_codec/boolean_encodings.json @@ -1,5 +1,5 @@ { - "title": "Encondings for boolean types supported by lisk-codec", + "title": "Encoding for boolean types supported by lisk-codec", "summary": "Examples of encoding booleans with lisk-codec", "config": { "network": "devnet" @@ -35,7 +35,7 @@ "state": false }, "schema": { - "$id": "object6", + "$id": "object5", "type": "object", "properties": { "state": { diff --git a/protocol-specs/generator_outputs/lisk_codec/bytes_decodings.json b/protocol-specs/generator_outputs/lisk_codec/bytes_decodings.json new file mode 100644 index 00000000000..f0a2497257f --- /dev/null +++ b/protocol-specs/generator_outputs/lisk_codec/bytes_decodings.json @@ -0,0 +1,53 @@ +{ + "title": "Decoding for bytes types supported by lisk-codec", + "summary": "Examples of encoding bytes with lisk-codec", + "config": { + "network": "devnet" + }, + "runner": "lisk_codec", + "handler": "bytes_decodings", + "testCases": [ + { + "description": "Decoding of chunk of bytes", + "input": { + "value": "0a14e11a11364738225813f86ea85214400e5db08d6e", + "schema": { + "$id": "object9", + "type": "object", + "properties": { + "address": { + "dataType": "bytes", + "fieldNumber": 1 + } + } + } + }, + "output": { + "object": { + "address": "e11a11364738225813f86ea85214400e5db08d6e" + } + } + }, + { + "description": "Decoding of empty bytes", + "input": { + "value": "0a00", + "schema": { + "$id": "object9", + "type": "object", + "properties": { + "address": { + "dataType": "bytes", + "fieldNumber": 1 + } + } + } + }, + "output": { + "object": { + "address": "" + } + } + } + ] +} diff --git a/protocol-specs/generator_outputs/lisk_codec/bytes_encodings.json b/protocol-specs/generator_outputs/lisk_codec/bytes_encodings.json index d41d0377e3d..3b3a9b30212 100644 --- a/protocol-specs/generator_outputs/lisk_codec/bytes_encodings.json +++ b/protocol-specs/generator_outputs/lisk_codec/bytes_encodings.json @@ -1,5 +1,5 @@ { - "title": "Encondings for bytes types supported by lisk-codec", + "title": "Encoding for bytes types supported by lisk-codec", "summary": "Examples of encoding bytes with lisk-codec", "config": { "network": "devnet" @@ -11,31 +11,7 @@ "description": "Encoding of chunk of bytes", "input": { "object": { - "address": { - "type": "Buffer", - "data": [ - 225, - 26, - 17, - 54, - 71, - 56, - 34, - 88, - 19, - 248, - 110, - 168, - 82, - 20, - 64, - 14, - 93, - 176, - 141, - 110 - ] - } + "address": "e11a11364738225813f86ea85214400e5db08d6e" }, "schema": { "$id": "object9", @@ -56,13 +32,10 @@ "description": "Encoding of empty bytes", "input": { "object": { - "address": { - "type": "Buffer", - "data": [] - } + "address": "" }, "schema": { - "$id": "object10", + "$id": "object9", "type": "object", "properties": { "address": { diff --git a/protocol-specs/generator_outputs/lisk_codec/cart_sample_decodings.json b/protocol-specs/generator_outputs/lisk_codec/cart_sample_decodings.json new file mode 100644 index 00000000000..03a7e1c447a --- /dev/null +++ b/protocol-specs/generator_outputs/lisk_codec/cart_sample_decodings.json @@ -0,0 +1,110 @@ +{ + "title": "Decoding for a complex object", + "summary": "Example of encoding a complex object that might exist in custom apps", + "config": { + "network": "devnet" + }, + "runner": "lisk_codec", + "handler": "cart_sample_decodings", + "testCases": [ + { + "description": "Decoding of object with multiple arrays", + "input": { + "value": "08d2091080acb8f6051864221c089edbb10210d7041801221008d70410061a09537461746520546178221c089eeda50410bf0c1801221008bf0c10071a09537461746520546178", + "schema": { + "$id": "cart_sample", + "type": "object", + "properties": { + "orderId": { + "dataType": "uint32", + "fieldNumber": 1 + }, + "createdAt": { + "dataType": "uint32", + "fieldNumber": 2 + }, + "customerId": { + "dataType": "uint32", + "fieldNumber": 3 + }, + "lineItems": { + "type": "array", + "fieldNumber": 4, + "items": { + "type": "object", + "properties": { + "productId": { + "dataType": "uint32", + "fieldNumber": 1 + }, + "price": { + "dataType": "uint32", + "fieldNumber": 2 + }, + "quantity": { + "dataType": "uint32", + "fieldNumber": 3 + }, + "taxLines": { + "type": "array", + "fieldNumber": 4, + "items": { + "type": "object", + "properties": { + "price": { + "dataType": "uint32", + "fieldNumber": 1 + }, + "rate": { + "dataType": "uint32", + "fieldNumber": 2 + }, + "title": { + "dataType": "string", + "fieldNumber": 3 + } + } + } + } + } + } + } + } + } + }, + "output": { + "object": { + "orderId": 1234, + "createdAt": 1590564352, + "customerId": 100, + "lineItems": [ + { + "productId": 5008798, + "price": 599, + "quantity": 1, + "taxLines": [ + { + "price": 599, + "rate": 6, + "title": "State Tax" + } + ] + }, + { + "productId": 9008798, + "price": 1599, + "quantity": 1, + "taxLines": [ + { + "price": 1599, + "rate": 7, + "title": "State Tax" + } + ] + } + ] + } + } + } + ] +} diff --git a/protocol-specs/generator_outputs/lisk_codec/cart_sample_encoding.json b/protocol-specs/generator_outputs/lisk_codec/cart_sample_encodings.json similarity index 82% rename from protocol-specs/generator_outputs/lisk_codec/cart_sample_encoding.json rename to protocol-specs/generator_outputs/lisk_codec/cart_sample_encodings.json index cab6784078e..2ad62bc650b 100644 --- a/protocol-specs/generator_outputs/lisk_codec/cart_sample_encoding.json +++ b/protocol-specs/generator_outputs/lisk_codec/cart_sample_encodings.json @@ -1,40 +1,40 @@ { - "title": "Encondings for a complex object", + "title": "Encoding for a complex object", "summary": "Example of encoding a complex object that might exist in custom apps", "config": { "network": "devnet" }, "runner": "lisk_codec", - "handler": "cart_sample_encoding", + "handler": "cart_sample_encodings", "testCases": [ { "description": "Encoding of object with multiple arrays", "input": { "object": { - "orderId": "1234", - "createdAt": "1590564352", - "customerId": "100", + "orderId": 1234, + "createdAt": 1590564352, + "customerId": 100, "lineItems": [ { - "productId": "5008798", - "price": "599", - "quantity": "1", + "productId": 5008798, + "price": 599, + "quantity": 1, "taxLines": [ { - "price": "599", - "rate": "6", + "price": 599, + "rate": 6, "title": "State Tax" } ] }, { - "productId": "9008798", - "price": "1599", - "quantity": "1", + "productId": 9008798, + "price": 1599, + "quantity": 1, "taxLines": [ { - "price": "1599", - "rate": "7", + "price": 1599, + "rate": 7, "title": "State Tax" } ] diff --git a/protocol-specs/generator_outputs/lisk_codec/genesis_block_decodings.json b/protocol-specs/generator_outputs/lisk_codec/genesis_block_decodings.json new file mode 100644 index 00000000000..6ce34c9b42d --- /dev/null +++ b/protocol-specs/generator_outputs/lisk_codec/genesis_block_decodings.json @@ -0,0 +1,265 @@ +{ + "title": "Decoding for genesis block types supported by lisk-codec", + "summary": "Examples of encoding block with lisk-codec", + "config": { + "network": "devnet" + }, + "runner": "lisk_codec", + "handler": "genesis_block_decodings", + "testCases": [ + { + "description": "Decoding of valid genesis block asset", + "input": { + "value": "0a7c0a1403f6d90b7dbd0497dc3a52d1c27e23bb8c75897f10001a20fc65777c1d4c00f1af5880c23ba7f60cd3bf84d1bf5c697abc4ffe17cf7acac020002a020800323a0a190a0a67656e657369735f33341800200028003080a094a58d1d121d0a1403f6d90b7dbd0497dc3a52d1c27e23bb8c75897f1080a094a58d1d0a5d0a140903f4c5cb599a7928aef27e314e98291d1e388810001a203f571324e9dc7b2481b71a7dc56637f1234504158986a242e90c33d8d20fdd9220002a020800321b0a190a0a67656e657369735f37341800200028003080a094a58d1d0a7c0a140ada6a2f6c8f891769366fc9aa6fd9f1facb36cf10001a20c69698ef30012964aafacfbe637bb63854b6109cc5c5f22aa4b3dc3e8dca821720002a020800323a0a190a0a67656e657369735f39381800200028003080a094a58d1d121d0a140ada6a2f6c8f891769366fc9aa6fd9f1facb36cf1080a094a58d1d121403f6d90b7dbd0497dc3a52d1c27e23bb8c75897f12140903f4c5cb599a7928aef27e314e98291d1e388812140ada6a2f6c8f891769366fc9aa6fd9f1facb36cf1803", + "schema": { + "$id": "genesisBlockAssetSchema", + "type": "object", + "required": ["accounts", "initDelegates", "initRounds"], + "properties": { + "accounts": { + "type": "array", + "items": { + "type": "object", + "properties": { + "address": { + "dataType": "bytes", + "fieldNumber": 1 + }, + "balance": { + "dataType": "uint64", + "fieldNumber": 2 + }, + "publicKey": { + "dataType": "bytes", + "fieldNumber": 3 + }, + "nonce": { + "dataType": "uint64", + "fieldNumber": 4 + }, + "keys": { + "fieldNumber": 5, + "type": "object", + "properties": { + "numberOfSignatures": { + "dataType": "uint32", + "fieldNumber": 1 + }, + "mandatoryKeys": { + "type": "array", + "items": { + "dataType": "bytes" + }, + "fieldNumber": 2 + }, + "optionalKeys": { + "type": "array", + "items": { + "dataType": "bytes" + }, + "fieldNumber": 3 + } + }, + "required": ["numberOfSignatures", "mandatoryKeys", "optionalKeys"] + }, + "asset": { + "fieldNumber": 6, + "type": "object", + "properties": { + "delegate": { + "type": "object", + "fieldNumber": 1, + "properties": { + "username": { + "dataType": "string", + "fieldNumber": 1 + }, + "pomHeights": { + "type": "array", + "items": { + "dataType": "uint32" + }, + "fieldNumber": 2 + }, + "consecutiveMissedBlocks": { + "dataType": "uint32", + "fieldNumber": 3 + }, + "lastForgedHeight": { + "dataType": "uint32", + "fieldNumber": 4 + }, + "isBanned": { + "dataType": "boolean", + "fieldNumber": 5 + }, + "totalVotesReceived": { + "dataType": "uint64", + "fieldNumber": 6 + } + }, + "required": [ + "username", + "pomHeights", + "consecutiveMissedBlocks", + "lastForgedHeight", + "isBanned", + "totalVotesReceived" + ] + }, + "sentVotes": { + "type": "array", + "fieldNumber": 2, + "items": { + "type": "object", + "properties": { + "delegateAddress": { + "dataType": "bytes", + "fieldNumber": 1 + }, + "amount": { + "dataType": "uint64", + "fieldNumber": 2 + } + }, + "required": ["delegateAddress", "amount"] + } + }, + "unlocking": { + "type": "array", + "fieldNumber": 3, + "items": { + "type": "object", + "properties": { + "delegateAddress": { + "dataType": "bytes", + "fieldNumber": 1 + }, + "amount": { + "dataType": "uint64", + "fieldNumber": 2 + }, + "unvoteHeight": { + "dataType": "uint32", + "fieldNumber": 3 + } + }, + "required": ["delegateAddress", "amount", "unvoteHeight"] + } + } + } + } + }, + "required": ["address", "balance", "publicKey", "nonce", "keys", "asset"] + }, + "fieldNumber": 1 + }, + "initDelegates": { + "type": "array", + "items": { + "dataType": "bytes" + }, + "fieldNumber": 2 + }, + "initRounds": { + "dataType": "uint32", + "fieldNumber": 3, + "minimum": 3 + } + } + } + }, + "output": { + "object": { + "initDelegates": [ + "03f6d90b7dbd0497dc3a52d1c27e23bb8c75897f", + "0903f4c5cb599a7928aef27e314e98291d1e3888", + "0ada6a2f6c8f891769366fc9aa6fd9f1facb36cf" + ], + "initRounds": 3, + "accounts": [ + { + "address": "03f6d90b7dbd0497dc3a52d1c27e23bb8c75897f", + "publicKey": "fc65777c1d4c00f1af5880c23ba7f60cd3bf84d1bf5c697abc4ffe17cf7acac0", + "balance": "0", + "nonce": "0", + "keys": { + "mandatoryKeys": [], + "optionalKeys": [], + "numberOfSignatures": 0 + }, + "asset": { + "delegate": { + "username": "genesis_34", + "pomHeights": [], + "consecutiveMissedBlocks": 0, + "lastForgedHeight": 0, + "isBanned": false, + "totalVotesReceived": "1000000000000" + }, + "sentVotes": [ + { + "delegateAddress": "03f6d90b7dbd0497dc3a52d1c27e23bb8c75897f", + "amount": "1000000000000" + } + ], + "unlocking": [] + } + }, + { + "address": "0903f4c5cb599a7928aef27e314e98291d1e3888", + "publicKey": "3f571324e9dc7b2481b71a7dc56637f1234504158986a242e90c33d8d20fdd92", + "balance": "0", + "nonce": "0", + "keys": { + "mandatoryKeys": [], + "optionalKeys": [], + "numberOfSignatures": 0 + }, + "asset": { + "delegate": { + "username": "genesis_74", + "pomHeights": [], + "consecutiveMissedBlocks": 0, + "lastForgedHeight": 0, + "isBanned": false, + "totalVotesReceived": "1000000000000" + }, + "sentVotes": [], + "unlocking": [] + } + }, + { + "address": "0ada6a2f6c8f891769366fc9aa6fd9f1facb36cf", + "publicKey": "c69698ef30012964aafacfbe637bb63854b6109cc5c5f22aa4b3dc3e8dca8217", + "balance": "0", + "nonce": "0", + "keys": { + "mandatoryKeys": [], + "optionalKeys": [], + "numberOfSignatures": 0 + }, + "asset": { + "delegate": { + "username": "genesis_98", + "pomHeights": [], + "consecutiveMissedBlocks": 0, + "lastForgedHeight": 0, + "isBanned": false, + "totalVotesReceived": "1000000000000" + }, + "sentVotes": [ + { + "delegateAddress": "0ada6a2f6c8f891769366fc9aa6fd9f1facb36cf", + "amount": "1000000000000" + } + ], + "unlocking": [] + } + } + ] + } + } + } + ] +} diff --git a/protocol-specs/generator_outputs/lisk_codec/genesis_block_encodings.json b/protocol-specs/generator_outputs/lisk_codec/genesis_block_encodings.json index fe52ed99287..9d88ca458c7 100644 --- a/protocol-specs/generator_outputs/lisk_codec/genesis_block_encodings.json +++ b/protocol-specs/generator_outputs/lisk_codec/genesis_block_encodings.json @@ -1,5 +1,5 @@ { - "title": "Encondings for genesis block types supported by lisk-codec", + "title": "Encoding for genesis block types supported by lisk-codec", "summary": "Examples of encoding block with lisk-codec", "config": { "network": "devnet" @@ -8,151 +8,19 @@ "handler": "genesis_block_encodings", "testCases": [ { - "description": "Encoding of valid block asset", + "description": "Encoding of valid genesis block asset", "input": { "object": { "initDelegates": [ - { - "type": "Buffer", - "data": [ - 3, - 246, - 217, - 11, - 125, - 189, - 4, - 151, - 220, - 58, - 82, - 209, - 194, - 126, - 35, - 187, - 140, - 117, - 137, - 127 - ] - }, - { - "type": "Buffer", - "data": [ - 9, - 3, - 244, - 197, - 203, - 89, - 154, - 121, - 40, - 174, - 242, - 126, - 49, - 78, - 152, - 41, - 29, - 30, - 56, - 136 - ] - }, - { - "type": "Buffer", - "data": [ - 10, - 218, - 106, - 47, - 108, - 143, - 137, - 23, - 105, - 54, - 111, - 201, - 170, - 111, - 217, - 241, - 250, - 203, - 54, - 207 - ] - } + "03f6d90b7dbd0497dc3a52d1c27e23bb8c75897f", + "0903f4c5cb599a7928aef27e314e98291d1e3888", + "0ada6a2f6c8f891769366fc9aa6fd9f1facb36cf" ], "initRounds": 3, "accounts": [ { - "address": { - "type": "Buffer", - "data": [ - 3, - 246, - 217, - 11, - 125, - 189, - 4, - 151, - 220, - 58, - 82, - 209, - 194, - 126, - 35, - 187, - 140, - 117, - 137, - 127 - ] - }, - "publicKey": { - "type": "Buffer", - "data": [ - 252, - 101, - 119, - 124, - 29, - 76, - 0, - 241, - 175, - 88, - 128, - 194, - 59, - 167, - 246, - 12, - 211, - 191, - 132, - 209, - 191, - 92, - 105, - 122, - 188, - 79, - 254, - 23, - 207, - 122, - 202, - 192 - ] - }, + "address": "03f6d90b7dbd0497dc3a52d1c27e23bb8c75897f", + "publicKey": "fc65777c1d4c00f1af5880c23ba7f60cd3bf84d1bf5c697abc4ffe17cf7acac0", "balance": "0", "nonce": "0", "keys": { @@ -171,31 +39,7 @@ }, "sentVotes": [ { - "delegateAddress": { - "type": "Buffer", - "data": [ - 3, - 246, - 217, - 11, - 125, - 189, - 4, - 151, - 220, - 58, - 82, - 209, - 194, - 126, - 35, - 187, - 140, - 117, - 137, - 127 - ] - }, + "delegateAddress": "03f6d90b7dbd0497dc3a52d1c27e23bb8c75897f", "amount": "1000000000000" } ], @@ -203,68 +47,8 @@ } }, { - "address": { - "type": "Buffer", - "data": [ - 9, - 3, - 244, - 197, - 203, - 89, - 154, - 121, - 40, - 174, - 242, - 126, - 49, - 78, - 152, - 41, - 29, - 30, - 56, - 136 - ] - }, - "publicKey": { - "type": "Buffer", - "data": [ - 63, - 87, - 19, - 36, - 233, - 220, - 123, - 36, - 129, - 183, - 26, - 125, - 197, - 102, - 55, - 241, - 35, - 69, - 4, - 21, - 137, - 134, - 162, - 66, - 233, - 12, - 51, - 216, - 210, - 15, - 221, - 146 - ] - }, + "address": "0903f4c5cb599a7928aef27e314e98291d1e3888", + "publicKey": "3f571324e9dc7b2481b71a7dc56637f1234504158986a242e90c33d8d20fdd92", "balance": "0", "nonce": "0", "keys": { @@ -286,68 +70,8 @@ } }, { - "address": { - "type": "Buffer", - "data": [ - 10, - 218, - 106, - 47, - 108, - 143, - 137, - 23, - 105, - 54, - 111, - 201, - 170, - 111, - 217, - 241, - 250, - 203, - 54, - 207 - ] - }, - "publicKey": { - "type": "Buffer", - "data": [ - 198, - 150, - 152, - 239, - 48, - 1, - 41, - 100, - 170, - 250, - 207, - 190, - 99, - 123, - 182, - 56, - 84, - 182, - 16, - 156, - 197, - 197, - 242, - 42, - 164, - 179, - 220, - 62, - 141, - 202, - 130, - 23 - ] - }, + "address": "0ada6a2f6c8f891769366fc9aa6fd9f1facb36cf", + "publicKey": "c69698ef30012964aafacfbe637bb63854b6109cc5c5f22aa4b3dc3e8dca8217", "balance": "0", "nonce": "0", "keys": { @@ -366,31 +90,7 @@ }, "sentVotes": [ { - "delegateAddress": { - "type": "Buffer", - "data": [ - 10, - 218, - 106, - 47, - 108, - 143, - 137, - 23, - 105, - 54, - 111, - 201, - 170, - 111, - 217, - 241, - 250, - 203, - 54, - 207 - ] - }, + "delegateAddress": "0ada6a2f6c8f891769366fc9aa6fd9f1facb36cf", "amount": "1000000000000" } ], diff --git a/protocol-specs/generator_outputs/lisk_codec/nested_array_decodings.json b/protocol-specs/generator_outputs/lisk_codec/nested_array_decodings.json new file mode 100644 index 00000000000..fb7d08d2071 --- /dev/null +++ b/protocol-specs/generator_outputs/lisk_codec/nested_array_decodings.json @@ -0,0 +1,136 @@ +{ + "title": "Decoding for a nested array", + "summary": "Example of encoding a nested array", + "config": { + "network": "devnet" + }, + "runner": "lisk_codec", + "handler": "nested_array_decodings", + "testCases": [ + { + "description": "Decoding of nested array object sample", + "input": { + "value": "0a4a0a396163636f756e74733a616464726573733a6164343266386538363764363138313731626634393832653634323639343432313438663665313112050a013d100112060a012b10cd02120f636861696e3a64656c656761746573120d636f6e73656e7375733a626674", + "schema": { + "$id": "/state/diff", + "type": "object", + "required": ["updated", "created"], + "properties": { + "updated": { + "type": "array", + "fieldNumber": 1, + "items": { + "type": "object", + "properties": { + "key": { + "dataType": "string", + "fieldNumber": 1 + }, + "value": { + "type": "array", + "fieldNumber": 2, + "items": { + "type": "object", + "properties": { + "code": { + "dataType": "string", + "fieldNumber": 1 + }, + "line": { + "dataType": "uint32", + "fieldNumber": 2 + } + } + } + } + } + } + }, + "created": { + "type": "array", + "fieldNumber": 2, + "items": { + "dataType": "string" + } + } + } + } + }, + "output": { + "object": { + "updated": [ + { + "key": "accounts:address:ad42f8e867d618171bf4982e64269442148f6e11", + "value": [ + { + "code": "=", + "line": 1 + }, + { + "code": "+", + "line": 333 + } + ] + } + ], + "created": ["chain:delegates", "consensus:bft"] + } + } + }, + { + "description": "Decoding of nested array string sample", + "input": { + "value": "0a490a396163636f756e74733a616464726573733a6164343266386538363764363138313731626634393832653634323639343432313438663665313112056469666631120564696666320a550a376163636f756e74733a616464726573733a363961366261313966353836303563366664323630623939303961353130383532336462383412056469666635120564696666361205646966663712056469666635120f636861696e3a64656c656761746573120d636f6e73656e7375733a626674", + "schema": { + "$id": "/state/diff-string", + "type": "object", + "required": ["updated", "created"], + "properties": { + "updated": { + "type": "array", + "fieldNumber": 1, + "items": { + "type": "object", + "properties": { + "key": { + "dataType": "string", + "fieldNumber": 1 + }, + "value": { + "type": "array", + "fieldNumber": 2, + "items": { + "dataType": "string" + } + } + } + } + }, + "created": { + "type": "array", + "fieldNumber": 2, + "items": { + "dataType": "string" + } + } + } + } + }, + "output": { + "object": { + "updated": [ + { + "key": "accounts:address:ad42f8e867d618171bf4982e64269442148f6e11", + "value": ["diff1", "diff2"] + }, + { + "key": "accounts:address:69a6ba19f58605c6fd260b9909a5108523db84", + "value": ["diff5", "diff6", "diff7", "diff5"] + } + ], + "created": ["chain:delegates", "consensus:bft"] + } + } + } + ] +} diff --git a/protocol-specs/generator_outputs/lisk_codec/nested_array_encoding.json b/protocol-specs/generator_outputs/lisk_codec/nested_array_encoding.json index d682747cc45..c0d3a8982df 100644 --- a/protocol-specs/generator_outputs/lisk_codec/nested_array_encoding.json +++ b/protocol-specs/generator_outputs/lisk_codec/nested_array_encoding.json @@ -13,7 +13,7 @@ "object": { "updated": [ { - "key": "accounts:address:rUL46GfWGBcb9JguZCaUQhSPbhE=", + "key": "accounts:address:ad42f8e867d618171bf4982e64269442148f6e11", "value": [ { "code": "=", @@ -74,7 +74,7 @@ } }, "output": { - "value": "0a3e0a2d6163636f756e74733a616464726573733a72554c343647665747426362394a67755a436155516853506268453d12050a013d100112060a012b10cd02120f636861696e3a64656c656761746573120d636f6e73656e7375733a626674" + "value": "0a4a0a396163636f756e74733a616464726573733a6164343266386538363764363138313731626634393832653634323639343432313438663665313112050a013d100112060a012b10cd02120f636861696e3a64656c656761746573120d636f6e73656e7375733a626674" } }, { @@ -83,11 +83,11 @@ "object": { "updated": [ { - "key": "accounts:address:rUL46GfWGBcb9JguZCaUQhSPbhE=", + "key": "accounts:address:ad42f8e867d618171bf4982e64269442148f6e11", "value": ["diff1", "diff2"] }, { - "key": "accounts:address:aaa6GfWGBcb9JguZCaUQhSPbhE=", + "key": "accounts:address:69a6ba19f58605c6fd260b9909a5108523db84", "value": ["diff5", "diff6", "diff7", "diff5"] } ], @@ -129,7 +129,7 @@ } }, "output": { - "value": "0a3d0a2d6163636f756e74733a616464726573733a72554c343647665747426362394a67755a436155516853506268453d12056469666631120564696666320a4a0a2c6163636f756e74733a616464726573733a6161613647665747426362394a67755a436155516853506268453d12056469666635120564696666361205646966663712056469666635120f636861696e3a64656c656761746573120d636f6e73656e7375733a626674" + "value": "0a490a396163636f756e74733a616464726573733a6164343266386538363764363138313731626634393832653634323639343432313438663665313112056469666631120564696666320a550a376163636f756e74733a616464726573733a363961366261313966353836303563366664323630623939303961353130383532336462383412056469666635120564696666361205646966663712056469666635120f636861696e3a64656c656761746573120d636f6e73656e7375733a626674" } } ] diff --git a/protocol-specs/generator_outputs/lisk_codec/nested_array_encodings.json b/protocol-specs/generator_outputs/lisk_codec/nested_array_encodings.json new file mode 100644 index 00000000000..70b03cec3a7 --- /dev/null +++ b/protocol-specs/generator_outputs/lisk_codec/nested_array_encodings.json @@ -0,0 +1,136 @@ +{ + "title": "Encoding for a nested array", + "summary": "Example of encoding a nested array", + "config": { + "network": "devnet" + }, + "runner": "lisk_codec", + "handler": "nested_array_encodings", + "testCases": [ + { + "description": "Encoding of nested array object sample", + "input": { + "object": { + "updated": [ + { + "key": "accounts:address:ad42f8e867d618171bf4982e64269442148f6e11", + "value": [ + { + "code": "=", + "line": 1 + }, + { + "code": "+", + "line": 333 + } + ] + } + ], + "created": ["chain:delegates", "consensus:bft"] + }, + "schema": { + "$id": "/state/diff", + "type": "object", + "required": ["updated", "created"], + "properties": { + "updated": { + "type": "array", + "fieldNumber": 1, + "items": { + "type": "object", + "properties": { + "key": { + "dataType": "string", + "fieldNumber": 1 + }, + "value": { + "type": "array", + "fieldNumber": 2, + "items": { + "type": "object", + "properties": { + "code": { + "dataType": "string", + "fieldNumber": 1 + }, + "line": { + "dataType": "uint32", + "fieldNumber": 2 + } + } + } + } + } + } + }, + "created": { + "type": "array", + "fieldNumber": 2, + "items": { + "dataType": "string" + } + } + } + } + }, + "output": { + "value": "0a4a0a396163636f756e74733a616464726573733a6164343266386538363764363138313731626634393832653634323639343432313438663665313112050a013d100112060a012b10cd02120f636861696e3a64656c656761746573120d636f6e73656e7375733a626674" + } + }, + { + "description": "Encoding of nested array string sample", + "input": { + "object": { + "updated": [ + { + "key": "accounts:address:ad42f8e867d618171bf4982e64269442148f6e11", + "value": ["diff1", "diff2"] + }, + { + "key": "accounts:address:69a6ba19f58605c6fd260b9909a5108523db84", + "value": ["diff5", "diff6", "diff7", "diff5"] + } + ], + "created": ["chain:delegates", "consensus:bft"] + }, + "schema": { + "$id": "/state/diff-string", + "type": "object", + "required": ["updated", "created"], + "properties": { + "updated": { + "type": "array", + "fieldNumber": 1, + "items": { + "type": "object", + "properties": { + "key": { + "dataType": "string", + "fieldNumber": 1 + }, + "value": { + "type": "array", + "fieldNumber": 2, + "items": { + "dataType": "string" + } + } + } + } + }, + "created": { + "type": "array", + "fieldNumber": 2, + "items": { + "dataType": "string" + } + } + } + } + }, + "output": { + "value": "0a490a396163636f756e74733a616464726573733a6164343266386538363764363138313731626634393832653634323639343432313438663665313112056469666631120564696666320a550a376163636f756e74733a616464726573733a363961366261313966353836303563366664323630623939303961353130383532336462383412056469666635120564696666361205646966663712056469666635120f636861696e3a64656c656761746573120d636f6e73656e7375733a626674" + } + } + ] +} diff --git a/protocol-specs/generator_outputs/lisk_codec/number_decodings.json b/protocol-specs/generator_outputs/lisk_codec/number_decodings.json new file mode 100644 index 00000000000..4a4ea8c8924 --- /dev/null +++ b/protocol-specs/generator_outputs/lisk_codec/number_decodings.json @@ -0,0 +1,95 @@ +{ + "title": "Decoding for number types supported by lisk-codec", + "summary": "Examples of encoding numbers with lisk-codec", + "config": { + "network": "devnet" + }, + "runner": "lisk_codec", + "handler": "number_decodings", + "testCases": [ + { + "description": "Decoding 32 bit unsigned number", + "input": { + "value": "080a", + "schema": { + "$id": "number-schema-uint32", + "type": "object", + "properties": { + "number": { + "dataType": "uint32", + "fieldNumber": 1 + } + } + } + }, + "output": { + "object": { + "number": 10 + } + } + }, + { + "description": "Decoding 32 bit signed number", + "input": { + "value": "0813", + "schema": { + "$id": "number-schema-sint32", + "type": "object", + "properties": { + "number": { + "dataType": "sint32", + "fieldNumber": 1 + } + } + } + }, + "output": { + "object": { + "number": -10 + } + } + }, + { + "description": "Decoding 64 bit unsigned number", + "input": { + "value": "08ffffc9a4d9cb54", + "schema": { + "$id": "number-schema-uint64", + "type": "object", + "properties": { + "number": { + "dataType": "uint64", + "fieldNumber": 1 + } + } + } + }, + "output": { + "object": { + "number": "372036854775807" + } + } + }, + { + "description": "Decoding 64 bit signed number", + "input": { + "value": "08fdffffffffffff1f", + "schema": { + "$id": "number-schema-sint64", + "type": "object", + "properties": { + "number": { + "dataType": "sint64", + "fieldNumber": 1 + } + } + } + }, + "output": { + "object": { + "number": "-9007199254740991" + } + } + } + ] +} diff --git a/protocol-specs/generator_outputs/lisk_codec/number_encodings.json b/protocol-specs/generator_outputs/lisk_codec/number_encodings.json index df35e5713a1..c9b6e9e9a27 100644 --- a/protocol-specs/generator_outputs/lisk_codec/number_encodings.json +++ b/protocol-specs/generator_outputs/lisk_codec/number_encodings.json @@ -1,5 +1,5 @@ { - "title": "Encondings for number types supported by lisk-codec", + "title": "Encoding for number types supported by lisk-codec", "summary": "Examples of encoding numbers with lisk-codec", "config": { "network": "devnet" @@ -14,7 +14,7 @@ "number": 10 }, "schema": { - "$id": "object1", + "$id": "number-schema-uint32", "type": "object", "properties": { "number": { @@ -35,7 +35,7 @@ "number": -10 }, "schema": { - "$id": "object2", + "$id": "number-schema-sint32", "type": "object", "properties": { "number": { @@ -53,10 +53,10 @@ "description": "Encoding 64 bit unsigned number", "input": { "object": { - "number": 372036854775807 + "number": "372036854775807" }, "schema": { - "$id": "object3", + "$id": "number-schema-uint64", "type": "object", "properties": { "number": { @@ -74,10 +74,10 @@ "description": "Encoding 64 bit signed number", "input": { "object": { - "number": -9007199254740991 + "number": "-9007199254740991" }, "schema": { - "$id": "object4", + "$id": "number-schema-sint64", "type": "object", "properties": { "number": { diff --git a/protocol-specs/generator_outputs/lisk_codec/objects_decodings.json b/protocol-specs/generator_outputs/lisk_codec/objects_decodings.json new file mode 100644 index 00000000000..ce32c68bdac --- /dev/null +++ b/protocol-specs/generator_outputs/lisk_codec/objects_decodings.json @@ -0,0 +1,109 @@ +{ + "title": "Decoding for objects types supported by lisk-codec", + "summary": "Examples of encoding objects with lisk-codec", + "config": { + "network": "devnet" + }, + "runner": "lisk_codec", + "handler": "objects_decodings", + "testCases": [ + { + "description": "Decoding of object", + "input": { + "value": "0a14e11a11364738225813f86ea85214400e5db08d6e1080ade2041801220864656c65676174652a2d0a25436865636b206f757420746865204c69736b2053444b206e6f7720696e2062696e61727921120408091009", + "schema": { + "$id": "object11", + "type": "object", + "properties": { + "address": { + "dataType": "bytes", + "fieldNumber": 1 + }, + "balance": { + "dataType": "uint64", + "fieldNumber": 2 + }, + "isDelegate": { + "dataType": "boolean", + "fieldNumber": 3 + }, + "name": { + "dataType": "string", + "fieldNumber": 4 + }, + "asset": { + "type": "object", + "fieldNumber": 5, + "properties": { + "data": { + "dataType": "string", + "fieldNumber": 1 + }, + "fooBar": { + "type": "object", + "fieldNumber": 2, + "properties": { + "foo": { + "dataType": "uint32", + "fieldNumber": 1 + }, + "bar": { + "dataType": "uint32", + "fieldNumber": 2 + } + } + } + } + } + } + } + }, + "output": { + "object": { + "address": "e11a11364738225813f86ea85214400e5db08d6e", + "balance": "10000000", + "isDelegate": true, + "name": "delegate", + "asset": { + "data": "Check out the Lisk SDK now in binary!", + "fooBar": { + "foo": 9, + "bar": 9 + } + } + } + } + }, + { + "description": "Decoding of object with optional property", + "input": { + "value": "08011801", + "schema": { + "$id": "object12", + "type": "object", + "properties": { + "isActive": { + "dataType": "boolean", + "fieldNumber": 1 + }, + "data": { + "dataType": "bytes", + "fieldNumber": 2 + }, + "value": { + "dataType": "uint64", + "fieldNumber": 3 + } + } + } + }, + "output": { + "object": { + "isActive": true, + "value": "1", + "data": "" + } + } + } + ] +} diff --git a/protocol-specs/generator_outputs/lisk_codec/objects_encodings.json b/protocol-specs/generator_outputs/lisk_codec/objects_encodings.json index fbd7f1096be..292e51ba6cf 100644 --- a/protocol-specs/generator_outputs/lisk_codec/objects_encodings.json +++ b/protocol-specs/generator_outputs/lisk_codec/objects_encodings.json @@ -1,5 +1,5 @@ { - "title": "Encondings for objects types supported by lisk-codec", + "title": "Encoding for objects types supported by lisk-codec", "summary": "Examples of encoding objects with lisk-codec", "config": { "network": "devnet" @@ -11,32 +11,8 @@ "description": "Encoding of object", "input": { "object": { - "address": { - "type": "Buffer", - "data": [ - 225, - 26, - 17, - 54, - 71, - 56, - 34, - 88, - 19, - 248, - 110, - 168, - 82, - 20, - 64, - 14, - 93, - 176, - 141, - 110 - ] - }, - "balance": 10000000, + "address": "e11a11364738225813f86ea85214400e5db08d6e", + "balance": "10000000", "isDelegate": true, "name": "delegate", "asset": { @@ -103,7 +79,7 @@ "input": { "object": { "isActive": true, - "value": 1 + "value": "1" }, "schema": { "$id": "object12", diff --git a/protocol-specs/generator_outputs/lisk_codec/peer_info_sample_decodings.json b/protocol-specs/generator_outputs/lisk_codec/peer_info_sample_decodings.json new file mode 100644 index 00000000000..685d755c83b --- /dev/null +++ b/protocol-specs/generator_outputs/lisk_codec/peer_info_sample_decodings.json @@ -0,0 +1,115 @@ +{ + "title": "Decoding for a peer info object", + "summary": "Example of encoding a peer info object for p2p", + "config": { + "network": "devnet" + }, + "runner": "lisk_codec", + "handler": "peer_info_sample_decodings", + "testCases": [ + { + "description": "Decoding of peer info sample", + "input": { + "value": "0a07312e312e312e3110d7081a40663866653765636333653239663538663339643861353338663961333562383062346236616239363734663033303065323565333366663431323734616533322203322e302a11694e496744304d6233732f524d61586273320664617277696e387b", + "schema": { + "$id": "peerInfo", + "type": "object", + "properties": { + "ipAddress": { + "dataType": "string", + "fieldNumber": 1 + }, + "wsPort": { + "dataType": "uint32", + "fieldNumber": 2 + }, + "networkIdentifier": { + "dataType": "string", + "fieldNumber": 3 + }, + "networkVersion": { + "dataType": "string", + "fieldNumber": 4 + }, + "nonce": { + "dataType": "string", + "fieldNumber": 5 + }, + "os": { + "dataType": "string", + "fieldNumber": 6 + }, + "height": { + "dataType": "uint32", + "fieldNumber": 7 + } + }, + "required": ["ipAddress", "wsPort"] + } + }, + "output": { + "object": { + "ipAddress": "1.1.1.1", + "wsPort": 1111, + "networkIdentifier": "f8fe7ecc3e29f58f39d8a538f9a35b80b4b6ab9674f0300e25e33ff41274ae32", + "networkVersion": "2.0", + "nonce": "iNIgD0Mb3s/RMaXbs", + "os": "darwin", + "height": 123 + } + } + }, + { + "description": "Decoding of peer info sample with optional property", + "input": { + "value": "0a07312e312e312e3110d708320664617277696e", + "schema": { + "$id": "peerInfo", + "type": "object", + "properties": { + "ipAddress": { + "dataType": "string", + "fieldNumber": 1 + }, + "wsPort": { + "dataType": "uint32", + "fieldNumber": 2 + }, + "networkIdentifier": { + "dataType": "string", + "fieldNumber": 3 + }, + "networkVersion": { + "dataType": "string", + "fieldNumber": 4 + }, + "nonce": { + "dataType": "string", + "fieldNumber": 5 + }, + "os": { + "dataType": "string", + "fieldNumber": 6 + }, + "height": { + "dataType": "uint32", + "fieldNumber": 7 + } + }, + "required": ["ipAddress", "wsPort"] + } + }, + "output": { + "object": { + "ipAddress": "1.1.1.1", + "wsPort": 1111, + "os": "darwin", + "networkIdentifier": "", + "networkVersion": "", + "nonce": "", + "height": 0 + } + } + } + ] +} diff --git a/protocol-specs/generator_outputs/lisk_codec/peer_info_sample_encoding.json b/protocol-specs/generator_outputs/lisk_codec/peer_info_sample_encoding.json index 7c087aa2944..11424fd279b 100644 --- a/protocol-specs/generator_outputs/lisk_codec/peer_info_sample_encoding.json +++ b/protocol-specs/generator_outputs/lisk_codec/peer_info_sample_encoding.json @@ -13,7 +13,7 @@ "object": { "ipAddress": "1.1.1.1", "wsPort": 1111, - "networkIdentifier": "+P5+zD4p9Y852KU4+aNbgLS2q5Z08DAOJeM/9BJ0rjI=", + "networkIdentifier": "f8fe7ecc3e29f58f39d8a538f9a35b80b4b6ab9674f0300e25e33ff41274ae32", "networkVersion": "2.0", "nonce": "iNIgD0Mb3s/RMaXbs", "os": "darwin", @@ -56,7 +56,7 @@ } }, "output": { - "value": "0a07312e312e312e3110d7081a2c2b50352b7a44347039593835324b55342b614e62674c533271355a303844414f4a654d2f39424a30726a493d2203322e302a11694e496744304d6233732f524d61586273320664617277696e387b" + "value": "0a07312e312e312e3110d7081a40663866653765636333653239663538663339643861353338663961333562383062346236616239363734663033303065323565333366663431323734616533322203322e302a11694e496744304d6233732f524d61586273320664617277696e387b" } }, { diff --git a/protocol-specs/generator_outputs/lisk_codec/peer_info_sample_encodings.json b/protocol-specs/generator_outputs/lisk_codec/peer_info_sample_encodings.json new file mode 100644 index 00000000000..f02768e711f --- /dev/null +++ b/protocol-specs/generator_outputs/lisk_codec/peer_info_sample_encodings.json @@ -0,0 +1,111 @@ +{ + "title": "Encoding for a peer info object", + "summary": "Example of encoding a peer info object for p2p", + "config": { + "network": "devnet" + }, + "runner": "lisk_codec", + "handler": "peer_info_sample_encodings", + "testCases": [ + { + "description": "Encoding of peer info sample", + "input": { + "object": { + "ipAddress": "1.1.1.1", + "wsPort": 1111, + "networkIdentifier": "f8fe7ecc3e29f58f39d8a538f9a35b80b4b6ab9674f0300e25e33ff41274ae32", + "networkVersion": "2.0", + "nonce": "iNIgD0Mb3s/RMaXbs", + "os": "darwin", + "height": 123 + }, + "schema": { + "$id": "peerInfo", + "type": "object", + "properties": { + "ipAddress": { + "dataType": "string", + "fieldNumber": 1 + }, + "wsPort": { + "dataType": "uint32", + "fieldNumber": 2 + }, + "networkIdentifier": { + "dataType": "string", + "fieldNumber": 3 + }, + "networkVersion": { + "dataType": "string", + "fieldNumber": 4 + }, + "nonce": { + "dataType": "string", + "fieldNumber": 5 + }, + "os": { + "dataType": "string", + "fieldNumber": 6 + }, + "height": { + "dataType": "uint32", + "fieldNumber": 7 + } + }, + "required": ["ipAddress", "wsPort"] + } + }, + "output": { + "value": "0a07312e312e312e3110d7081a40663866653765636333653239663538663339643861353338663961333562383062346236616239363734663033303065323565333366663431323734616533322203322e302a11694e496744304d6233732f524d61586273320664617277696e387b" + } + }, + { + "description": "Encoding of peer info sample with optional property", + "input": { + "object": { + "ipAddress": "1.1.1.1", + "wsPort": 1111, + "os": "darwin" + }, + "schema": { + "$id": "peerInfo", + "type": "object", + "properties": { + "ipAddress": { + "dataType": "string", + "fieldNumber": 1 + }, + "wsPort": { + "dataType": "uint32", + "fieldNumber": 2 + }, + "networkIdentifier": { + "dataType": "string", + "fieldNumber": 3 + }, + "networkVersion": { + "dataType": "string", + "fieldNumber": 4 + }, + "nonce": { + "dataType": "string", + "fieldNumber": 5 + }, + "os": { + "dataType": "string", + "fieldNumber": 6 + }, + "height": { + "dataType": "uint32", + "fieldNumber": 7 + } + }, + "required": ["ipAddress", "wsPort"] + } + }, + "output": { + "value": "0a07312e312e312e3110d708320664617277696e" + } + } + ] +} diff --git a/protocol-specs/generator_outputs/lisk_codec/string_decodings.json b/protocol-specs/generator_outputs/lisk_codec/string_decodings.json new file mode 100644 index 00000000000..bfcfcec334d --- /dev/null +++ b/protocol-specs/generator_outputs/lisk_codec/string_decodings.json @@ -0,0 +1,74 @@ +{ + "title": "Decoding for string types supported by lisk-codec", + "summary": "Examples of encoding strings with lisk-codec", + "config": { + "network": "devnet" + }, + "runner": "lisk_codec", + "handler": "string_decodings", + "testCases": [ + { + "description": "Decoding of string", + "input": { + "value": "0a12436865636b6f7574204c69736b2053444b21", + "schema": { + "$id": "string-schema", + "type": "object", + "properties": { + "data": { + "dataType": "string", + "fieldNumber": 1 + } + } + } + }, + "output": { + "object": { + "data": "Checkout Lisk SDK!" + } + } + }, + { + "description": "Encoding of empty string", + "input": { + "value": "0a00", + "schema": { + "$id": "string-schema", + "type": "object", + "properties": { + "data": { + "dataType": "string", + "fieldNumber": 1 + } + } + } + }, + "output": { + "object": { + "data": "" + } + } + }, + { + "description": "Encoding of some utf symbols string", + "input": { + "value": "0a2ee282ac2ec6922ee280b02ec5922ec2a32ec2a92ec2ae2ec2b52ec3862ec3bc2ec3bd2ec3b82ec3872ec2a52ec39f", + "schema": { + "$id": "string-schema", + "type": "object", + "properties": { + "data": { + "dataType": "string", + "fieldNumber": 1 + } + } + } + }, + "output": { + "object": { + "data": "€.ƒ.‰.Œ.£.©.®.µ.Æ.ü.ý.ø.Ç.¥.ß" + } + } + } + ] +} diff --git a/protocol-specs/generator_outputs/lisk_codec/string_encodings.json b/protocol-specs/generator_outputs/lisk_codec/string_encodings.json index e5b625a7a55..b16d9a3e3d2 100644 --- a/protocol-specs/generator_outputs/lisk_codec/string_encodings.json +++ b/protocol-specs/generator_outputs/lisk_codec/string_encodings.json @@ -1,5 +1,5 @@ { - "title": "Encondings for string types supported by lisk-codec", + "title": "Encoding for string types supported by lisk-codec", "summary": "Examples of encoding strings with lisk-codec", "config": { "network": "devnet" @@ -14,7 +14,7 @@ "data": "Checkout Lisk SDK!" }, "schema": { - "$id": "object7", + "$id": "string-schema", "type": "object", "properties": { "data": { @@ -35,7 +35,7 @@ "data": "" }, "schema": { - "$id": "object8", + "$id": "string-schema", "type": "object", "properties": { "data": { @@ -56,7 +56,7 @@ "data": "€.ƒ.‰.Œ.£.©.®.µ.Æ.ü.ý.ø.Ç.¥.ß" }, "schema": { - "$id": "object8", + "$id": "string-schema", "type": "object", "properties": { "data": { diff --git a/protocol-specs/generator_outputs/lisk_codec/transaction_decodings.json b/protocol-specs/generator_outputs/lisk_codec/transaction_decodings.json new file mode 100644 index 00000000000..34e7df7be60 --- /dev/null +++ b/protocol-specs/generator_outputs/lisk_codec/transaction_decodings.json @@ -0,0 +1,207 @@ +{ + "title": "Decoding for transaction types supported by lisk-codec", + "summary": "Examples of encoding transaction with lisk-codec", + "config": { + "network": "devnet" + }, + "runner": "lisk_codec", + "handler": "transaction_decodings", + "testCases": [ + { + "description": "Decoding of valid base transaction", + "input": { + "value": "0814100118d284cb8d9f82e50220eb9a89e10b2a208f057d088a585d938c20d63e430a068d4cea384e588aa0b758c68fca21644dbc3214f214d75bbc4b2ea89e433f3a45af803725416ec33a40204514eb1152355799ece36d17037e5feb4871472c60763bdafe67eb6a38bec632a8e2e62f84a32cf764342a4708a65fbad194e37feec03940f0ff84d3df2a053a400b6730e5898ca56fe0dc1c73de9363f6fc8b335592ef10725a8463bff101a4943e60311f0b1a439a2c9e02cca1379b80a822f4ec48cf212bff1f1c757e92ec02", + "schema": { + "$id": "baseTransactionSchema", + "type": "object", + "properties": { + "moduleID": { + "dataType": "uint32", + "fieldNumber": 1 + }, + "assetID": { + "dataType": "uint32", + "fieldNumber": 2 + }, + "nonce": { + "dataType": "uint64", + "fieldNumber": 3 + }, + "fee": { + "dataType": "uint64", + "fieldNumber": 4 + }, + "senderPublicKey": { + "dataType": "bytes", + "fieldNumber": 5 + }, + "asset": { + "dataType": "bytes", + "fieldNumber": 6 + }, + "signatures": { + "type": "array", + "items": { + "dataType": "bytes" + }, + "fieldNumber": 7 + } + }, + "required": [ + "moduleID", + "assetID", + "nonce", + "fee", + "senderPublicKey", + "asset", + "signatures" + ] + } + }, + "output": { + "object": { + "moduleID": 20, + "assetID": 1, + "nonce": "1570179673932370", + "fee": "3156364651", + "senderPublicKey": "8f057d088a585d938c20d63e430a068d4cea384e588aa0b758c68fca21644dbc", + "asset": "f214d75bbc4b2ea89e433f3a45af803725416ec3", + "signatures": [ + "204514eb1152355799ece36d17037e5feb4871472c60763bdafe67eb6a38bec632a8e2e62f84a32cf764342a4708a65fbad194e37feec03940f0ff84d3df2a05", + "0b6730e5898ca56fe0dc1c73de9363f6fc8b335592ef10725a8463bff101a4943e60311f0b1a439a2c9e02cca1379b80a822f4ec48cf212bff1f1c757e92ec02" + ] + } + } + }, + { + "description": "Decoding of valid vote asset", + "input": { + "value": "0a1c0a14cd32c73e9851c7137980063b8af64aa5a31651f810ffdf8bb4590a1d0a149d86ad24a3f030e5522b6598115bb4d70c1692c91080c0bebbc51a", + "schema": { + "$id": "voteAssetSchema", + "type": "object", + "properties": { + "votes": { + "type": "array", + "fieldNumber": 1, + "items": { + "type": "object", + "properties": { + "delegateAddress": { + "dataType": "bytes", + "fieldNumber": 1 + }, + "amount": { + "dataType": "sint64", + "fieldNumber": 2 + } + }, + "required": ["delegateAddress", "amount"] + } + } + }, + "required": ["votes"] + } + }, + "output": { + "object": { + "votes": [ + { + "delegateAddress": "cd32c73e9851c7137980063b8af64aa5a31651f8", + "amount": "-12000000000" + }, + { + "delegateAddress": "9d86ad24a3f030e5522b6598115bb4d70c1692c9", + "amount": "456000000000" + } + ] + } + } + }, + { + "description": "Decoding of valid multisignature asset", + "input": { + "value": "0802122007d6389be6e2109613699c02e78253148989515c3867e4f490eafd004a95b2b412203e754d00815b6b248a981520afbaf913153a26d25e2d5283964779c65ceee7e81a20c8b8fbe474a2b63ccb9744a409569b0a465ee1803f80435aec1c5e7fc2d4ee181a206115424fec0ce9c3bac5a81b5c782827d1f956fb95f1ccfa36c566d04e4d7267", + "schema": { + "$id": "multisigAssetSchema", + "type": "object", + "properties": { + "numberOfSignatures": { + "dataType": "uint32", + "fieldNumber": 1 + }, + "mandatoryKeys": { + "type": "array", + "items": { + "dataType": "bytes" + }, + "fieldNumber": 2 + }, + "optionalKeys": { + "type": "array", + "items": { + "dataType": "bytes" + }, + "fieldNumber": 3 + } + }, + "required": ["numberOfSignatures", "mandatoryKeys", "optionalKeys"] + } + }, + "output": { + "object": { + "numberOfSignatures": 2, + "mandatoryKeys": [ + "07d6389be6e2109613699c02e78253148989515c3867e4f490eafd004a95b2b4", + "3e754d00815b6b248a981520afbaf913153a26d25e2d5283964779c65ceee7e8" + ], + "optionalKeys": [ + "c8b8fbe474a2b63ccb9744a409569b0a465ee1803f80435aec1c5e7fc2d4ee18", + "6115424fec0ce9c3bac5a81b5c782827d1f956fb95f1ccfa36c566d04e4d7267" + ] + } + } + }, + { + "description": "Decoding of valid multisignature asset with empty array", + "input": { + "value": "08021220c8b8fbe474a2b63ccb9744a409569b0a465ee1803f80435aec1c5e7fc2d4ee1812206115424fec0ce9c3bac5a81b5c782827d1f956fb95f1ccfa36c566d04e4d7267", + "schema": { + "$id": "multisigAssetSchema", + "type": "object", + "properties": { + "numberOfSignatures": { + "dataType": "uint32", + "fieldNumber": 1 + }, + "mandatoryKeys": { + "type": "array", + "items": { + "dataType": "bytes" + }, + "fieldNumber": 2 + }, + "optionalKeys": { + "type": "array", + "items": { + "dataType": "bytes" + }, + "fieldNumber": 3 + } + }, + "required": ["numberOfSignatures", "mandatoryKeys", "optionalKeys"] + } + }, + "output": { + "object": { + "numberOfSignatures": 2, + "mandatoryKeys": [ + "c8b8fbe474a2b63ccb9744a409569b0a465ee1803f80435aec1c5e7fc2d4ee18", + "6115424fec0ce9c3bac5a81b5c782827d1f956fb95f1ccfa36c566d04e4d7267" + ], + "optionalKeys": [] + } + } + } + ] +} diff --git a/protocol-specs/generator_outputs/lisk_codec/transaction_encodings.json b/protocol-specs/generator_outputs/lisk_codec/transaction_encodings.json index 22fc23b6f7c..12577e01f91 100644 --- a/protocol-specs/generator_outputs/lisk_codec/transaction_encodings.json +++ b/protocol-specs/generator_outputs/lisk_codec/transaction_encodings.json @@ -1,5 +1,5 @@ { - "title": "Encondings for transaction types supported by lisk-codec", + "title": "Encoding for transaction types supported by lisk-codec", "summary": "Examples of encoding transaction with lisk-codec", "config": { "network": "devnet" @@ -13,209 +13,13 @@ "object": { "moduleID": 20, "assetID": 1, - "nonce": 1570179673932370, - "fee": 3156364651, - "senderPublicKey": { - "type": "Buffer", - "data": [ - 143, - 5, - 125, - 8, - 138, - 88, - 93, - 147, - 140, - 32, - 214, - 62, - 67, - 10, - 6, - 141, - 76, - 234, - 56, - 78, - 88, - 138, - 160, - 183, - 88, - 198, - 143, - 202, - 33, - 100, - 77, - 188 - ] - }, - "asset": { - "type": "Buffer", - "data": [ - 242, - 20, - 215, - 91, - 188, - 75, - 46, - 168, - 158, - 67, - 63, - 58, - 69, - 175, - 128, - 55, - 37, - 65, - 110, - 195 - ] - }, + "nonce": "1570179673932370", + "fee": "3156364651", + "senderPublicKey": "8f057d088a585d938c20d63e430a068d4cea384e588aa0b758c68fca21644dbc", + "asset": "f214d75bbc4b2ea89e433f3a45af803725416ec3", "signatures": [ - { - "type": "Buffer", - "data": [ - 32, - 69, - 20, - 235, - 17, - 82, - 53, - 87, - 153, - 236, - 227, - 109, - 23, - 3, - 126, - 95, - 235, - 72, - 113, - 71, - 44, - 96, - 118, - 59, - 218, - 254, - 103, - 235, - 106, - 56, - 190, - 198, - 50, - 168, - 226, - 230, - 47, - 132, - 163, - 44, - 247, - 100, - 52, - 42, - 71, - 8, - 166, - 95, - 186, - 209, - 148, - 227, - 127, - 238, - 192, - 57, - 64, - 240, - 255, - 132, - 211, - 223, - 42, - 5 - ] - }, - { - "type": "Buffer", - "data": [ - 11, - 103, - 48, - 229, - 137, - 140, - 165, - 111, - 224, - 220, - 28, - 115, - 222, - 147, - 99, - 246, - 252, - 139, - 51, - 85, - 146, - 239, - 16, - 114, - 90, - 132, - 99, - 191, - 241, - 1, - 164, - 148, - 62, - 96, - 49, - 31, - 11, - 26, - 67, - 154, - 44, - 158, - 2, - 204, - 161, - 55, - 155, - 128, - 168, - 34, - 244, - 236, - 72, - 207, - 33, - 43, - 255, - 31, - 28, - 117, - 126, - 146, - 236, - 2 - ] - } + "204514eb1152355799ece36d17037e5feb4871472c60763bdafe67eb6a38bec632a8e2e62f84a32cf764342a4708a65fbad194e37feec03940f0ff84d3df2a05", + "0b6730e5898ca56fe0dc1c73de9363f6fc8b335592ef10725a8463bff101a4943e60311f0b1a439a2c9e02cca1379b80a822f4ec48cf212bff1f1c757e92ec02" ] }, "schema": { @@ -275,60 +79,12 @@ "object": { "votes": [ { - "delegateAddress": { - "type": "Buffer", - "data": [ - 205, - 50, - 199, - 62, - 152, - 81, - 199, - 19, - 121, - 128, - 6, - 59, - 138, - 246, - 74, - 165, - 163, - 22, - 81, - 248 - ] - }, - "amount": -12000000000 + "delegateAddress": "cd32c73e9851c7137980063b8af64aa5a31651f8", + "amount": "-12000000000" }, { - "delegateAddress": { - "type": "Buffer", - "data": [ - 157, - 134, - 173, - 36, - 163, - 240, - 48, - 229, - 82, - 43, - 101, - 152, - 17, - 91, - 180, - 215, - 12, - 22, - 146, - 201 - ] - }, - "amount": 456000000000 + "delegateAddress": "9d86ad24a3f030e5522b6598115bb4d70c1692c9", + "amount": "456000000000" } ] }, @@ -368,156 +124,12 @@ "object": { "numberOfSignatures": 2, "mandatoryKeys": [ - { - "type": "Buffer", - "data": [ - 7, - 214, - 56, - 155, - 230, - 226, - 16, - 150, - 19, - 105, - 156, - 2, - 231, - 130, - 83, - 20, - 137, - 137, - 81, - 92, - 56, - 103, - 228, - 244, - 144, - 234, - 253, - 0, - 74, - 149, - 178, - 180 - ] - }, - { - "type": "Buffer", - "data": [ - 62, - 117, - 77, - 0, - 129, - 91, - 107, - 36, - 138, - 152, - 21, - 32, - 175, - 186, - 249, - 19, - 21, - 58, - 38, - 210, - 94, - 45, - 82, - 131, - 150, - 71, - 121, - 198, - 92, - 238, - 231, - 232 - ] - } + "07d6389be6e2109613699c02e78253148989515c3867e4f490eafd004a95b2b4", + "3e754d00815b6b248a981520afbaf913153a26d25e2d5283964779c65ceee7e8" ], "optionalKeys": [ - { - "type": "Buffer", - "data": [ - 200, - 184, - 251, - 228, - 116, - 162, - 182, - 60, - 203, - 151, - 68, - 164, - 9, - 86, - 155, - 10, - 70, - 94, - 225, - 128, - 63, - 128, - 67, - 90, - 236, - 28, - 94, - 127, - 194, - 212, - 238, - 24 - ] - }, - { - "type": "Buffer", - "data": [ - 97, - 21, - 66, - 79, - 236, - 12, - 233, - 195, - 186, - 197, - 168, - 27, - 92, - 120, - 40, - 39, - 209, - 249, - 86, - 251, - 149, - 241, - 204, - 250, - 54, - 197, - 102, - 208, - 78, - 77, - 114, - 103 - ] - } + "c8b8fbe474a2b63ccb9744a409569b0a465ee1803f80435aec1c5e7fc2d4ee18", + "6115424fec0ce9c3bac5a81b5c782827d1f956fb95f1ccfa36c566d04e4d7267" ] }, "schema": { @@ -556,80 +168,8 @@ "object": { "numberOfSignatures": 2, "mandatoryKeys": [ - { - "type": "Buffer", - "data": [ - 200, - 184, - 251, - 228, - 116, - 162, - 182, - 60, - 203, - 151, - 68, - 164, - 9, - 86, - 155, - 10, - 70, - 94, - 225, - 128, - 63, - 128, - 67, - 90, - 236, - 28, - 94, - 127, - 194, - 212, - 238, - 24 - ] - }, - { - "type": "Buffer", - "data": [ - 97, - 21, - 66, - 79, - 236, - 12, - 233, - 195, - 186, - 197, - 168, - 27, - 92, - 120, - 40, - 39, - 209, - 249, - 86, - 251, - 149, - 241, - 204, - 250, - 54, - 197, - 102, - 208, - 78, - 77, - 114, - 103 - ] - } + "c8b8fbe474a2b63ccb9744a409569b0a465ee1803f80435aec1c5e7fc2d4ee18", + "6115424fec0ce9c3bac5a81b5c782827d1f956fb95f1ccfa36c566d04e4d7267" ], "optionalKeys": [] }, diff --git a/protocol-specs/generators/address_generation/index.js b/protocol-specs/generators/address_generation/index.js index e1570be87d9..f9dc284cf87 100644 --- a/protocol-specs/generators/address_generation/index.js +++ b/protocol-specs/generators/address_generation/index.js @@ -91,7 +91,7 @@ const generateTestCasesForAddressFromPubKey = publicKey => ({ description: 'Generate valid address from a valid public key', input: { publicKey }, output: { - binaryAddress: getBinaryAddress(publicKey).toString('hex'), + binaryAddress: getBinaryAddress(publicKey), base32Address: getBase32Address(publicKey), }, }); diff --git a/protocol-specs/generators/base_generator.js b/protocol-specs/generators/base_generator.js index 7d3bbe5711a..98a771eeabf 100644 --- a/protocol-specs/generators/base_generator.js +++ b/protocol-specs/generators/base_generator.js @@ -16,6 +16,7 @@ const fs = require('fs'); const path = require('path'); +const { jsonStringify } = require('../utils'); const runGenerator = (generatorName, suiteCreators) => { // eslint-disable-next-line no-restricted-syntax @@ -30,7 +31,7 @@ const runGenerator = (generatorName, suiteCreators) => { const fullPath = `${dirPath}/${suiteResult.handler}.json`; - fs.writeFileSync(fullPath, `${JSON.stringify(suiteResult, null, '\t')}\n`); + fs.writeFileSync(fullPath, `${jsonStringify(suiteResult, '\t')}\n`); } }; diff --git a/protocol-specs/generators/dpos_delegate_shuffling/index.js b/protocol-specs/generators/dpos_delegate_shuffling/index.js index b38c2dd01d5..80d1e83db42 100644 --- a/protocol-specs/generators/dpos_delegate_shuffling/index.js +++ b/protocol-specs/generators/dpos_delegate_shuffling/index.js @@ -42,7 +42,7 @@ const generateShuffledDelegateList = () => { delegateList: previousDelegateList.map(delegate => delegate.address), }, output: { - delegateList: delegateList.map(delegate => delegate.address.toString('hex')), + delegateList: delegateList.map(delegate => delegate.address), }, }; }; diff --git a/protocol-specs/generators/dpos_delegate_shuffling/sample_generator.js b/protocol-specs/generators/dpos_delegate_shuffling/sample_generator.js index 007a2ffd6cc..26b63a10a4c 100644 --- a/protocol-specs/generators/dpos_delegate_shuffling/sample_generator.js +++ b/protocol-specs/generators/dpos_delegate_shuffling/sample_generator.js @@ -25,7 +25,7 @@ const generateDelegates = num => { for (let i = 0; i < num; i += 1) { const passphrase = Mnemonic.generateMnemonic(); const { publicKey } = getKeys(passphrase); - const address = hash(Buffer.from(publicKey, 'hex')).slice(0, 20).toString('hex'); + const address = hash(Buffer.from(publicKey, 'hex')).slice(0, 20); delegateList.push({ address, diff --git a/protocol-specs/generators/dpos_forger_selection/index.js b/protocol-specs/generators/dpos_forger_selection/index.js index 8a3a4abc295..aaee3b5ec2a 100644 --- a/protocol-specs/generators/dpos_forger_selection/index.js +++ b/protocol-specs/generators/dpos_forger_selection/index.js @@ -94,7 +94,7 @@ const generateForgerSelectionWithMoreThan2EligibleStandBy = () => { voteWeights: delegateWeightsWithMoreThan2EligibleStandBy.list, }, output: { - selectedForgers: result.map(vw => vw.address.toString('hex')), + selectedForgers: result.map(vw => vw.address), }, }; }; @@ -110,7 +110,7 @@ const generateForgerSelectionWithExactly1EligibleStandBy = () => { }, output: { selectedForgers: copyAndSort(delegateWeightsWithExactly1EligibleStandBy.list) - .map(dw => dw.address.toString('hex')) + .map(dw => dw.address) .slice(0, 103), }, }; @@ -127,7 +127,7 @@ const generateForgerSelectionWithExactly2EligibleStandBy = () => { }, output: { selectedForgers: copyAndSort(delegateWeightsWithExactly2EligibleStandBy.list) - .map(dw => dw.address.toString('hex')) + .map(dw => dw.address) .slice(0, 103), }, }; @@ -144,7 +144,7 @@ const generateForgerSelectionWithLessThan103Delegates = () => { }, output: { selectedForgers: copyAndSort(delegateWeightsLessThan103.list) - .map(dw => dw.address.toString('hex')) + .map(dw => dw.address) .slice(0, 103), }, }; @@ -161,7 +161,7 @@ const generateForgerSelectionWithExactly0EligibleStandBy = () => { }, output: { selectedForgers: copyAndSort(delegateWeightsWith0EligibleStandBy.list) - .map(dw => dw.address.toString('hex')) + .map(dw => dw.address) .slice(0, 103), }, }; diff --git a/protocol-specs/generators/dpos_forger_selection/sample_generator.js b/protocol-specs/generators/dpos_forger_selection/sample_generator.js index 7a5f425ffa4..25f8fb85b30 100644 --- a/protocol-specs/generators/dpos_forger_selection/sample_generator.js +++ b/protocol-specs/generators/dpos_forger_selection/sample_generator.js @@ -26,7 +26,7 @@ const generateDelegates = (num, fixedNum) => { for (let i = 0; i < num; i += 1) { const passphrase = Mnemonic.generateMnemonic(); const { publicKey } = getKeys(passphrase); - const address = hash(Buffer.from(publicKey, 'hex')).slice(0, 20).toString('hex'); + const address = hash(Buffer.from(publicKey, 'hex')).slice(0, 20); const buf = crypto.randomBytes(8); const randomNumber = buf.readBigUInt64BE() / BigInt(10) ** BigInt(8); const voteWeight = fixedValue @@ -34,8 +34,8 @@ const generateDelegates = (num, fixedNum) => { : randomNumber - (randomNumber % BigInt(10) ** BigInt(9)); delegateList.push({ address, - voteWeight: voteWeight.toString(), - // lsk: (voteWeight / (BigInt(10) ** BigInt(8))).toString(), + voteWeight, + // lsk: (voteWeight / (BigInt(10) ** BigInt(8))), }); } diff --git a/protocol-specs/generators/dpos_random_seed_generation/index.js b/protocol-specs/generators/dpos_random_seed_generation/index.js index 5b67e17f1c0..3562dd9b506 100644 --- a/protocol-specs/generators/dpos_random_seed_generation/index.js +++ b/protocol-specs/generators/dpos_random_seed_generation/index.js @@ -219,8 +219,8 @@ const generateRandomSeed = (blocks, blocksPerRound) => { ]); return { - randomSeed1: randomSeed1.toString('hex'), - randomSeed2: randomSeed2.toString('hex'), + randomSeed1, + randomSeed2, }; }; diff --git a/protocol-specs/generators/lisk_codec/index.js b/protocol-specs/generators/lisk_codec/index.js index 639625c975b..575c596e273 100644 --- a/protocol-specs/generators/lisk_codec/index.js +++ b/protocol-specs/generators/lisk_codec/index.js @@ -15,187 +15,196 @@ 'use strict'; const BaseGenerator = require('../base_generator'); -const typesGenerators = require('./types_generators'); - -const numberEncodingsSuite = () => ({ - title: 'Encondings for number types supported by lisk-codec', - summary: 'Examples of encoding numbers with lisk-codec', - config: { - network: 'devnet', - }, - runner: 'lisk_codec', - handler: 'number_encodings', - testCases: [...typesGenerators.generateValidNumberEncodings()], -}); - -const booleanEncodingsSuite = () => ({ - title: 'Encondings for boolean types supported by lisk-codec', - summary: 'Examples of encoding booleans with lisk-codec', - config: { - network: 'devnet', - }, - runner: 'lisk_codec', - handler: 'boolean_encodings', - testCases: [...typesGenerators.generateValidBooleanEncodings()], -}); - -const stringEncodingsSuite = () => ({ - title: 'Encondings for string types supported by lisk-codec', - summary: 'Examples of encoding strings with lisk-codec', - config: { - network: 'devnet', - }, - runner: 'lisk_codec', - handler: 'string_encodings', - testCases: [...typesGenerators.generateValidStringEncodings()], -}); - -const bytesEncodingsSuite = () => ({ - title: 'Encondings for bytes types supported by lisk-codec', - summary: 'Examples of encoding bytes with lisk-codec', - config: { - network: 'devnet', - }, - runner: 'lisk_codec', - handler: 'bytes_encodings', - testCases: [...typesGenerators.generateValidBytesEncodings()], -}); - -const objectEncodingsSuite = () => ({ - title: 'Encondings for objects types supported by lisk-codec', - summary: 'Examples of encoding objects with lisk-codec', - config: { - network: 'devnet', - }, - runner: 'lisk_codec', - handler: 'objects_encodings', - testCases: [...typesGenerators.generateValidObjectEncodings()], -}); - -const arrayEncodingsSuite = () => ({ - title: 'Encondings for arrays types supported by lisk-codec', - summary: 'Examples of encoding arrays with lisk-codec', - config: { - network: 'devnet', - }, - runner: 'lisk_codec', - handler: 'arrays_encodings', - testCases: [...typesGenerators.generateValidArrayEncodings()], -}); - -const blockEncodingsSuite = () => ({ - title: 'Encondings for block types supported by lisk-codec', - summary: 'Examples of encoding block with lisk-codec', - config: { - network: 'devnet', - }, - runner: 'lisk_codec', - handler: 'block_encodings', - testCases: [...typesGenerators.generateValidBlock()], -}); - -const genesisBlockEncodingsSuite = () => ({ - title: 'Encondings for genesis block types supported by lisk-codec', - summary: 'Examples of encoding block with lisk-codec', - config: { - network: 'devnet', - }, - runner: 'lisk_codec', - handler: 'genesis_block_encodings', - testCases: [...typesGenerators.generateValidGenesisBlock()], -}); - -const blockHeaderEncodingsSuite = () => ({ - title: 'Encondings for block header types supported by lisk-codec', - summary: 'Examples of encoding block header with lisk-codec', - config: { - network: 'devnet', - }, - runner: 'lisk_codec', - handler: 'block_header_encodings', - testCases: [...typesGenerators.generateValidBlockHeader()], -}); - -const blockAssetEncodingsSuite = () => ({ - title: 'Encondings for block asset types supported by lisk-codec', - summary: 'Examples of encoding block asset with lisk-codec', - config: { - network: 'devnet', - }, - runner: 'lisk_codec', - handler: 'block_asset_encodings', - testCases: [...typesGenerators.generateValidBlockAsset()], -}); - -const accountEncodingsSuite = () => ({ - title: 'Encondings for account types supported by lisk-codec', - summary: 'Examples of encoding account with lisk-codec', - config: { - network: 'devnet', - }, - runner: 'lisk_codec', - handler: 'account_encodings', - testCases: [...typesGenerators.generateValidAccount()], -}); - -const transactionEncodingsSuite = () => ({ - title: 'Encondings for transaction types supported by lisk-codec', - summary: 'Examples of encoding transaction with lisk-codec', - config: { - network: 'devnet', - }, - runner: 'lisk_codec', - handler: 'transaction_encodings', - testCases: [...typesGenerators.generateValidTransaction()], -}); - -const cartSampleEncodingSuite = () => ({ - title: 'Encondings for a complex object', - summary: 'Example of encoding a complex object that might exist in custom apps', - config: { - network: 'devnet', - }, - runner: 'lisk_codec', - handler: 'cart_sample_encoding', - testCases: [...typesGenerators.generateCartEncodings()], -}); - -const peerInfoSampleEncodingSuite = () => ({ - title: 'Encondings for a peer info object', - summary: 'Example of encoding a peer info object for p2p', - config: { - network: 'devnet', - }, - runner: 'lisk_codec', - handler: 'peer_info_sample_encoding', - testCases: [...typesGenerators.generatePeerInfoEncodings()], -}); - -const nestedArrayEncodingSuite = () => ({ - title: 'Encondings for a nested array', - summary: 'Example of encoding a nested array', - config: { - network: 'devnet', - }, - runner: 'lisk_codec', - handler: 'nested_array_encoding', - testCases: [...typesGenerators.generateNestedArrayEncodings()], -}); +const { + validNumberEncodingsTestCases, + validNumberDecodingsTestCases, + validBooleanEncodingsTestCases, + validBooleanDecodingsTestCases, + validStringsEncodingTestCases, + validStringsDecodingTestCases, + validBytesEncodingsTestCases, + validBytesDecodingsTestCases, + validObjectEncodingsTestCases, + validObjectDecodingsTestCases, + validArrayEncodingsTestCases, + validArrayDecodingsTestCases, + validBlockEncodingsTestCases, + validBlockDecodingsTestCases, + validGenesisBlockAssetEncodingsTestCases, + validGenesisBlockAssetDecodingsTestCases, + validBlockHeaderEncodingsTestCases, + validBlockHeaderDecodingsTestCases, + validBlockAssetEncodingsTestCases, + validBlockAssetDecodingsTestCases, + validAccountEncodingTestCases, + validAccountDecodingTestCases, + validTransactionEncodingsTestCases, + validTransactionDecodingsTestCases, + cartSampleEncodingsTestCases, + cartSampleDecodingsTestCases, + validPeerInfoEncodingsTestCases, + validPeerInfoDecodingsTestCases, + validNestedArrayEncodingsTestCases, + validNestedArrayDecodingsTestCases, +} = require('./types_generators'); + +const generateTestSuite = (data, handler, encodingTestCases, decodingTestCases) => [ + () => ({ + ...data, + title: `Encoding ${data.title}`, + config: { + network: 'devnet', + }, + runner: 'lisk_codec', + handler: `${handler}_encodings`, + testCases: encodingTestCases, + }), + () => ({ + ...data, + title: `Decoding ${data.title}`, + config: { + network: 'devnet', + }, + runner: 'lisk_codec', + handler: `${handler}_decodings`, + testCases: decodingTestCases, + }), +]; module.exports = BaseGenerator.runGenerator('lisk_codec', [ - numberEncodingsSuite, - booleanEncodingsSuite, - stringEncodingsSuite, - bytesEncodingsSuite, - objectEncodingsSuite, - arrayEncodingsSuite, - blockEncodingsSuite, - blockHeaderEncodingsSuite, - genesisBlockEncodingsSuite, - blockAssetEncodingsSuite, - accountEncodingsSuite, - transactionEncodingsSuite, - cartSampleEncodingSuite, - peerInfoSampleEncodingSuite, - nestedArrayEncodingSuite, + ...generateTestSuite( + { + title: 'for number types supported by lisk-codec', + summary: 'Examples of encoding numbers with lisk-codec', + }, + 'number', + validNumberEncodingsTestCases, + validNumberDecodingsTestCases, + ), + ...generateTestSuite( + { + title: 'for boolean types supported by lisk-codec', + summary: 'Examples of encoding booleans with lisk-codec', + }, + 'boolean', + validBooleanEncodingsTestCases, + validBooleanDecodingsTestCases, + ), + ...generateTestSuite( + { + title: 'for string types supported by lisk-codec', + summary: 'Examples of encoding strings with lisk-codec', + }, + 'string', + validStringsEncodingTestCases, + validStringsDecodingTestCases, + ), + ...generateTestSuite( + { + title: 'for bytes types supported by lisk-codec', + summary: 'Examples of encoding bytes with lisk-codec', + }, + 'bytes', + validBytesEncodingsTestCases, + validBytesDecodingsTestCases, + ), + ...generateTestSuite( + { + title: 'for objects types supported by lisk-codec', + summary: 'Examples of encoding objects with lisk-codec', + }, + 'objects', + validObjectEncodingsTestCases, + validObjectDecodingsTestCases, + ), + ...generateTestSuite( + { + title: 'for arrays types supported by lisk-codec', + summary: 'Examples of encoding arrays with lisk-codec', + }, + 'arrays', + validArrayEncodingsTestCases, + validArrayDecodingsTestCases, + ), + ...generateTestSuite( + { + title: 'for block types supported by lisk-codec', + summary: 'Examples of encoding block with lisk-codec', + }, + 'block', + validBlockEncodingsTestCases, + validBlockDecodingsTestCases, + ), + ...generateTestSuite( + { + title: 'for genesis block types supported by lisk-codec', + summary: 'Examples of encoding block with lisk-codec', + }, + 'genesis_block', + validGenesisBlockAssetEncodingsTestCases, + validGenesisBlockAssetDecodingsTestCases, + ), + ...generateTestSuite( + { + title: 'for block header types supported by lisk-codec', + summary: 'Examples of encoding block header with lisk-codec', + }, + 'block_header', + validBlockHeaderEncodingsTestCases, + validBlockHeaderDecodingsTestCases, + ), + ...generateTestSuite( + { + title: 'for block asset types supported by lisk-codec', + summary: 'Examples of encoding block asset with lisk-codec', + }, + 'block_asset', + validBlockAssetEncodingsTestCases, + validBlockAssetDecodingsTestCases, + ), + ...generateTestSuite( + { + title: 'for account types supported by lisk-codec', + summary: 'Examples of encoding account with lisk-codec', + }, + 'account', + validAccountEncodingTestCases, + validAccountDecodingTestCases, + ), + ...generateTestSuite( + { + title: 'for transaction types supported by lisk-codec', + summary: 'Examples of encoding transaction with lisk-codec', + }, + 'transaction', + validTransactionEncodingsTestCases, + validTransactionDecodingsTestCases, + ), + ...generateTestSuite( + { + title: 'for a complex object', + summary: 'Example of encoding a complex object that might exist in custom apps', + }, + 'cart_sample', + cartSampleEncodingsTestCases, + cartSampleDecodingsTestCases, + ), + ...generateTestSuite( + { + title: 'for a peer info object', + summary: 'Example of encoding a peer info object for p2p', + }, + 'peer_info_sample', + validPeerInfoEncodingsTestCases, + validPeerInfoDecodingsTestCases, + ), + ...generateTestSuite( + { + title: 'for a nested array', + summary: 'Example of encoding a nested array', + }, + 'nested_array', + validNestedArrayEncodingsTestCases, + validNestedArrayDecodingsTestCases, + ), ]); diff --git a/protocol-specs/generators/lisk_codec/proto_files/arrays.proto b/protocol-specs/generators/lisk_codec/proto_files/arrays.proto index 08dd1454949..323f11f580d 100644 --- a/protocol-specs/generators/lisk_codec/proto_files/arrays.proto +++ b/protocol-specs/generators/lisk_codec/proto_files/arrays.proto @@ -18,7 +18,7 @@ message Object { } message ArrayObjects { - repeated Object myArray = 1; + repeated Object list = 1; } diff --git a/protocol-specs/generators/lisk_codec/types_generators/account.js b/protocol-specs/generators/lisk_codec/types_generators/account.js index 187cabccb0c..fa8410224f1 100644 --- a/protocol-specs/generators/lisk_codec/types_generators/account.js +++ b/protocol-specs/generators/lisk_codec/types_generators/account.js @@ -104,107 +104,108 @@ const accountSchema = { required: ['address', 'balance', 'publicKey', 'nonce', 'keys', 'asset'], }; -const generateValidAccountEncodings = () => { - const input = { - validAccount1: { - object: { - address: Buffer.from('e11a11364738225813f86ea85214400e5db08d6e', 'hex'), - balance: 10, - publicKey: Buffer.from( - '0fd3c50a6d3bd17ea806c0566cf6cf10f6e3697d9bda1820b00cb14746bcccef', +const validAccount1 = { + address: Buffer.from('e11a11364738225813f86ea85214400e5db08d6e', 'hex'), + balance: '10', + publicKey: Buffer.from('0fd3c50a6d3bd17ea806c0566cf6cf10f6e3697d9bda1820b00cb14746bcccef', 'hex'), + nonce: '5', + keys: { + numberOfSignatures: 2, + mandatoryKeys: [ + Buffer.from('c8b8fbe474a2b63ccb9744a409569b0a465ee1803f80435aec1c5e7fc2d4ee18', 'hex'), + Buffer.from('6115424fec0ce9c3bac5a81b5c782827d1f956fb95f1ccfa36c566d04e4d7267', 'hex'), + ], + optionalKeys: [], + }, + asset: { + delegate: { + username: 'Catullo', + pomHeights: [85], + consecutiveMissedBlocks: 32, + lastForgedHeight: 64, + isBanned: false, + totalVotesReceived: '300000000', + }, + sentVotes: [ + { + delegateAddress: Buffer.from( + 'cd32c73e9851c7137980063b8af64aa5a31651f8dcad258b682d2ddf091029e4', 'hex', ), - nonce: 5, - keys: { - numberOfSignatures: 2, - mandatoryKeys: [ - Buffer.from('c8b8fbe474a2b63ccb9744a409569b0a465ee1803f80435aec1c5e7fc2d4ee18', 'hex'), - Buffer.from('6115424fec0ce9c3bac5a81b5c782827d1f956fb95f1ccfa36c566d04e4d7267', 'hex'), - ], - optionalKeys: [], - }, - asset: { - delegate: { - username: 'Catullo', - pomHeights: [85], - consecutiveMissedBlocks: 32, - lastForgedHeight: 64, - isBanned: false, - totalVotesReceived: 300000000, - }, - sentVotes: [ - { - delegateAddress: Buffer.from( - 'cd32c73e9851c7137980063b8af64aa5a31651f8dcad258b682d2ddf091029e4', - 'hex', - ), - amount: 100000000, - }, - { - delegateAddress: Buffer.from( - '9d86ad24a3f030e5522b6598115bb4d70c1692c9d8995ddfccb377379a2d86c6', - 'hex', - ), - amount: 250000000, - }, - ], - unlocking: [ - { - delegateAddress: Buffer.from( - '655e665765e3c42712d9a425b5b720d10457a5e45de0d4420e7c53ad73b02ef5', - 'hex', - ), - amount: 400000000, - unvoteHeight: 128, - }, - ], - }, + amount: '100000000', }, - schema: accountSchema, - }, - validAccount2: { - object: { - address: Buffer.from('cd32c73e9851c7137980063b8af64aa5a31651f8', 'hex'), - balance: 0, - publicKey: Buffer.alloc(0), - nonce: 0, - keys: { - numberOfSignatures: 0, - mandatoryKeys: [], - optionalKeys: [], - }, - asset: { - delegate: { - username: '', - pomHeights: [], - consecutiveMissedBlocks: 0, - lastForgedHeight: 0, - isBanned: false, - totalVotesReceived: 0, - }, - sentVotes: [], - unlocking: [], - }, + { + delegateAddress: Buffer.from( + '9d86ad24a3f030e5522b6598115bb4d70c1692c9d8995ddfccb377379a2d86c6', + 'hex', + ), + amount: '250000000', + }, + ], + unlocking: [ + { + delegateAddress: Buffer.from( + '655e665765e3c42712d9a425b5b720d10457a5e45de0d4420e7c53ad73b02ef5', + 'hex', + ), + amount: '400000000', + unvoteHeight: 128, }, - schema: accountSchema, + ], + }, +}; + +const validAccount2 = { + address: Buffer.from('cd32c73e9851c7137980063b8af64aa5a31651f8', 'hex'), + balance: '0', + publicKey: Buffer.alloc(0), + nonce: '0', + keys: { + numberOfSignatures: 0, + mandatoryKeys: [], + optionalKeys: [], + }, + asset: { + delegate: { + username: '', + pomHeights: [], + consecutiveMissedBlocks: 0, + lastForgedHeight: 0, + isBanned: false, + totalVotesReceived: '0', }, - }; + sentVotes: [], + unlocking: [], + }, +}; - const validAccount1Encoded = Account.encode(input.validAccount1.object).finish(); - const validAccount2Encoded = Account.encode(input.validAccount2.object).finish(); +const validAccount1Encoded = Account.encode(validAccount1).finish(); +const validAccount2Encoded = Account.encode(validAccount2).finish(); - return [ +module.exports = { + validAccountEncodingTestCases: [ { description: 'Encoding of valid account 1', - input: input.validAccount1, - output: { value: validAccount1Encoded.toString('hex') }, + input: { object: validAccount1, schema: accountSchema }, + output: { value: validAccount1Encoded }, }, { description: 'Encoding of valid default account', - input: input.validAccount2, - output: { value: validAccount2Encoded.toString('hex') }, + input: { object: validAccount2, schema: accountSchema }, + output: { value: validAccount2Encoded }, }, - ]; -}; + ], -module.exports = generateValidAccountEncodings; + validAccountDecodingTestCases: [ + { + description: 'Decoding of valid account 1', + input: { value: validAccount1Encoded, schema: accountSchema }, + output: { object: validAccount1 }, + }, + { + description: 'Decoding of valid default account', + input: { value: validAccount2Encoded, schema: accountSchema }, + output: { object: validAccount2 }, + }, + ], +}; diff --git a/protocol-specs/generators/lisk_codec/types_generators/arrays.js b/protocol-specs/generators/lisk_codec/types_generators/arrays.js index cfe836de301..0f85f07e468 100644 --- a/protocol-specs/generators/lisk_codec/types_generators/arrays.js +++ b/protocol-specs/generators/lisk_codec/types_generators/arrays.js @@ -19,152 +19,120 @@ const prepareProtobuffersArrays = () => const { ArrayOfIntegers, ArrayBools, ArrayString, ArrayObjects } = prepareProtobuffersArrays(); -const generateValidArrayEncodings = () => { - const input = { - ArrayOfIntegers: { - object: { - list: [3, 1, 4, 1, 5, 9, 2, 6, 5], - }, - schema: { - type: 'object', - $id: 'arrayUint32', - properties: { - list: { - type: 'array', - items: { - dataType: 'uint32', - }, - fieldNumber: 1, - }, - }, - }, +const getArraySchemaFor = type => ({ + type: 'object', + $id: `array-schema-${typeof type === 'string' ? type : typeof type}`, + properties: { + list: { + type: 'array', + items: + typeof type === 'object' + ? type + : { + dataType: type, + // eslint-disable-next-line no-mixed-spaces-and-tabs + }, + fieldNumber: 1, }, - arrayBools: { - object: { - list: [true, true, false, true, false, false], - }, - schema: { - type: 'object', - $id: 'arrayBoolean', - properties: { - list: { - type: 'array', - items: { - dataType: 'boolean', - }, - fieldNumber: 1, - }, - }, - }, + }, +}); + +const integerSchema = getArraySchemaFor('uint32'); +const arrayOfIntegers = { list: [3, 1, 4, 1, 5, 9, 2, 6, 5] }; +const emptyArray = { + list: [], +}; +const arrayOfIntegersEncoded = ArrayOfIntegers.encode(arrayOfIntegers).finish(); +const emptyArrayEncoded = ArrayOfIntegers.encode(emptyArray).finish(); + +const booleanSchema = getArraySchemaFor('boolean'); +const arrayOfBooleans = { list: [true, true, false, true, false, false] }; +const arrayBoolsEncoded = ArrayBools.encode(arrayOfBooleans).finish(); + +const stringSchema = getArraySchemaFor('string'); +const arrayOfStrings = { list: ['lisk', '', 'gogogog'] }; +const arrayStringsEncoded = ArrayString.encode(arrayOfStrings).finish(); + +const objectSchema = getArraySchemaFor({ + type: 'object', + properties: { + address: { + dataType: 'string', + fieldNumber: 1, }, - arrayStrings: { - object: { - list: ['lisk', '', 'gogogog'], - }, - schema: { - type: 'object', - $id: 'arrayStrings', - properties: { - list: { - type: 'array', - items: { - dataType: 'string', - }, - fieldNumber: 1, - }, - }, - }, + amount: { + dataType: 'uint64', + fieldNumber: 2, }, - arrayObjects: { - object: { - myArray: [ - { - address: 'e11a11364738225813f86ea85214400e5db08d6e', - amount: 100000, - }, - { - address: 'aa2a11364738225813f86ea85214400e5db08fff', - amount: 300000, - }, - ], - }, - schema: { - $id: 'arrayObject', - type: 'object', - properties: { - myArray: { - type: 'array', - fieldNumber: 1, - items: { - type: 'object', - properties: { - address: { - dataType: 'string', - fieldNumber: 1, - }, - amount: { - dataType: 'uint64', - fieldNumber: 2, - }, - }, - }, - }, - }, - }, + }, +}); +const arrayOfObjects = { + list: [ + { + address: 'e11a11364738225813f86ea85214400e5db08d6e', + amount: '100000', }, - emptyArray: { - object: { - list: [], - }, - schema: { - type: 'object', - $id: 'emptyArray', - properties: { - list: { - type: 'array', - items: { - dataType: 'uint32', - }, - fieldNumber: 1, - }, - }, - }, + { + address: 'aa2a11364738225813f86ea85214400e5db08fff', + amount: '300000', }, - }; - - const arrayOfIntegersEncoded = ArrayOfIntegers.encode(input.ArrayOfIntegers.object).finish(); - const arrayBoolsEncoded = ArrayBools.encode(input.arrayBools.object).finish(); - const arrayStringsEncoded = ArrayString.encode(input.arrayStrings.object).finish(); - const arrayOfObjectsEncoded = ArrayObjects.encode(input.arrayObjects.object).finish(); - const emptyArrayEncoded = ArrayBools.encode(input.emptyArray.object).finish(); + ], +}; +const arrayOfObjectsEncoded = ArrayObjects.encode(arrayOfObjects).finish(); - return [ +module.exports = { + validArrayEncodingsTestCases: [ { description: 'Encoding of integers array', - input: input.ArrayOfIntegers, - output: { value: arrayOfIntegersEncoded.toString('hex') }, + input: { object: arrayOfIntegers, schema: integerSchema }, + output: { value: arrayOfIntegersEncoded }, }, { description: 'Encoding of booleans array', - input: input.arrayBools, - output: { value: arrayBoolsEncoded.toString('hex') }, + input: { object: arrayOfBooleans, schema: booleanSchema }, + output: { value: arrayBoolsEncoded }, }, { - description: 'Encoding of strings array', - input: input.arrayStrings, - output: { value: arrayStringsEncoded.toString('hex') }, + description: 'arrayStrings of strings array', + input: { object: arrayOfStrings, schema: stringSchema }, + output: { value: arrayStringsEncoded }, }, { description: 'Encoding of objects array', - input: input.arrayObjects, - output: { value: arrayOfObjectsEncoded.toString('hex') }, + input: { object: arrayOfObjects, schema: objectSchema }, + output: { value: arrayOfObjectsEncoded }, }, { description: 'Encoding of empty array', - input: input.emptyArray, - output: { value: emptyArrayEncoded.toString('hex') }, + input: { object: emptyArray, schema: integerSchema }, + output: { value: emptyArrayEncoded }, + }, + ], + validArrayDecodingsTestCases: [ + { + description: 'Decoding of integers array', + input: { value: arrayOfIntegersEncoded, schema: integerSchema }, + output: { object: arrayOfIntegers }, + }, + { + description: 'Decoding of booleans array', + input: { value: arrayBoolsEncoded, schema: booleanSchema }, + output: { object: arrayOfBooleans }, + }, + { + description: 'Decoding of strings array', + input: { value: arrayStringsEncoded, schema: stringSchema }, + output: { object: arrayOfStrings }, }, - ]; + { + description: 'Decoding of objects array', + input: { value: arrayOfObjectsEncoded, schema: objectSchema }, + output: { object: arrayOfObjects }, + }, + { + description: 'Decoding of empty array', + input: { value: emptyArrayEncoded, schema: integerSchema }, + output: { object: emptyArray }, + }, + ], }; - -module.exports = generateValidArrayEncodings; diff --git a/protocol-specs/generators/lisk_codec/types_generators/block.js b/protocol-specs/generators/lisk_codec/types_generators/block.js index 39b40d5865d..5590dd6b79c 100644 --- a/protocol-specs/generators/lisk_codec/types_generators/block.js +++ b/protocol-specs/generators/lisk_codec/types_generators/block.js @@ -29,48 +29,46 @@ const blockSchema = { required: ['header', 'payload'], }; -const generateValidBlockEncodings = () => { - const input = { - validBlock1: { - object: { - header: Buffer.from( - 'ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad', - 'hex', - ), - payload: [ - Buffer.from('a665a45920422f9d417e4867efdc4fb8a04a1f3fff1fa07e998e86f7f7a27ae3', 'hex'), - Buffer.from('68a751863fe73b8ede8d832be628ff680d617fa15c74d00142f9025d5f37dd50', 'hex'), - ], - }, - schema: blockSchema, - }, - validBlock2: { - object: { - header: Buffer.from( - 'e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855', - 'hex', - ), - payload: [], - }, - schema: blockSchema, - }, - }; +const validBlock1 = { + header: Buffer.from('ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad', 'hex'), + payload: [ + Buffer.from('a665a45920422f9d417e4867efdc4fb8a04a1f3fff1fa07e998e86f7f7a27ae3', 'hex'), + Buffer.from('68a751863fe73b8ede8d832be628ff680d617fa15c74d00142f9025d5f37dd50', 'hex'), + ], +}; - const validBlock1Encoded = Block.encode(input.validBlock1.object).finish(); - const validBlock2Encoded = Block.encode(input.validBlock2.object).finish(); +const validBlock2 = { + header: Buffer.from('e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855', 'hex'), + payload: [], +}; + +const validBlock1Encoded = Block.encode(validBlock1).finish(); +const validBlock2Encoded = Block.encode(validBlock2).finish(); - return [ +module.exports = { + validBlockEncodingsTestCases: [ { description: 'Encoding of valid block with payload', - input: input.validBlock1, - output: { value: validBlock1Encoded.toString('hex') }, + input: { object: validBlock1, schema: blockSchema }, + output: { value: validBlock1Encoded }, }, { description: 'Encoding of valid block block without payload', - input: input.validBlock2, - output: { value: validBlock2Encoded.toString('hex') }, + input: { object: validBlock2, schema: blockSchema }, + output: { value: validBlock2Encoded }, }, - ]; -}; + ], -module.exports = generateValidBlockEncodings; + validBlockDecodingsTestCases: [ + { + description: 'Decoding of valid block with payload', + input: { value: validBlock1Encoded, schema: blockSchema }, + output: { object: validBlock1 }, + }, + { + description: 'Decoding of valid block block without payload', + input: { value: validBlock2Encoded, schema: blockSchema }, + output: { object: validBlock2 }, + }, + ], +}; diff --git a/protocol-specs/generators/lisk_codec/types_generators/block_asset.js b/protocol-specs/generators/lisk_codec/types_generators/block_asset.js index af1771db263..b5c4572add6 100644 --- a/protocol-specs/generators/lisk_codec/types_generators/block_asset.js +++ b/protocol-specs/generators/lisk_codec/types_generators/block_asset.js @@ -30,41 +30,45 @@ const blockAssetSchema = { required: ['maxHeightPreviouslyForged', 'maxHeightPrevoted', 'seedReveal'], }; -const generateValidBlockAssetEncodings = () => { - const input = { - validBlockAsset1: { - object: { - maxHeightPreviouslyForged: 1049, - maxHeightPrevoted: 901049, - seedReveal: Buffer.from('d59386e0ae435e292fbe0ebcdb954b75', 'hex'), - }, - schema: blockAssetSchema, - }, - validBlockAsset2: { - object: { - maxHeightPreviouslyForged: 0, - maxHeightPrevoted: 1049, - seedReveal: Buffer.from('eaaf9d4c65cb501c811ef812847a5551', 'hex'), - }, - schema: blockAssetSchema, - }, - }; +const validBlockAsset1 = { + maxHeightPreviouslyForged: 1049, + maxHeightPrevoted: 901049, + seedReveal: Buffer.from('d59386e0ae435e292fbe0ebcdb954b75', 'hex'), +}; - const validBlockAsset1Encoded = BlockAsset.encode(input.validBlockAsset1.object).finish(); - const validBlockAsset2Encoded = BlockAsset.encode(input.validBlockAsset2.object).finish(); +const validBlockAsset2 = { + maxHeightPreviouslyForged: 0, + maxHeightPrevoted: 1049, + seedReveal: Buffer.from('eaaf9d4c65cb501c811ef812847a5551', 'hex'), +}; + +const validBlockAsset1Encoded = BlockAsset.encode(validBlockAsset1).finish(); +const validBlockAsset2Encoded = BlockAsset.encode(validBlockAsset2).finish(); - return [ +module.exports = { + validBlockAssetEncodingsTestCases: [ { description: 'Encoding of valid block asset', - input: input.validBlockAsset1, - output: { value: validBlockAsset1Encoded.toString('hex') }, + input: { object: validBlockAsset1, schema: blockAssetSchema }, + output: { value: validBlockAsset1Encoded }, }, { description: 'Encoding of valid block asset with zero previously forged', - input: input.validBlockAsset2, - output: { value: validBlockAsset2Encoded.toString('hex') }, + input: { object: validBlockAsset2, schema: blockAssetSchema }, + output: { value: validBlockAsset2Encoded }, }, - ]; -}; + ], -module.exports = generateValidBlockAssetEncodings; + validBlockAssetDecodingsTestCases: [ + { + description: 'Decoding of valid block asset', + input: { value: validBlockAsset1Encoded, schema: blockAssetSchema }, + output: { object: validBlockAsset1 }, + }, + { + description: 'Decoding of valid block asset with zero previously forged', + input: { value: validBlockAsset2Encoded, schema: blockAssetSchema }, + output: { object: validBlockAsset2 }, + }, + ], +}; diff --git a/protocol-specs/generators/lisk_codec/types_generators/block_header.js b/protocol-specs/generators/lisk_codec/types_generators/block_header.js index 5f4577ae1ef..feba0cb2d4c 100644 --- a/protocol-specs/generators/lisk_codec/types_generators/block_header.js +++ b/protocol-specs/generators/lisk_codec/types_generators/block_header.js @@ -45,83 +45,81 @@ const blockHeaderSchema = { ], }; -const generateValidBlockHeaderEncodings = () => { - const input = { - validBlockHeader1: { - object: { - version: 1, - timestamp: 1590557445, - height: 12385603, - previousBlockID: Buffer.from( - 'ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad', - 'hex', - ), - transactionRoot: Buffer.from( - 'a665a45920422f9d417e4867efdc4fb8a04a1f3fff1fa07e998e86f7f7a27ae3', - 'hex', - ), - generatorPublicKey: Buffer.from( - '68a751863fe73b8ede8d832be628ff680d617fa15c74d00142f9025d5f37dd50', - 'hex', - ), - reward: 400000000, - asset: Buffer.from( - 'd59386e0ae435e292fbe0ebcdb954b75ed5fb3922091277cb19f798fc5d50718', - 'hex', - ), - signature: Buffer.from( - '8331b5123cac056e2ec8361c56e642db0ca0e13abe33696d23d4d00ad6de844919296e87abe8e172f67fd882b4c0b1c1804b7d9075ecf975cf2631d8d7efef0c', - 'hex', - ), - }, - schema: blockHeaderSchema, - }, - validBlockHeader2: { - object: { - version: 3, - timestamp: 1590557804, - height: 901049, - previousBlockID: Buffer.from( - 'e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855', - 'hex', - ), - transactionRoot: Buffer.from( - 'e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855', - 'hex', - ), - generatorPublicKey: Buffer.from( - 'acc56344dea609e80cf5d4165e46917104fe701927847fc2a5d40e37574b2b38', - 'hex', - ), - reward: 400000000, - asset: Buffer.from( - 'eaaf9d4c65cb501c811ef812847a55513181474d734ead1b95b7e1e5b574d223', - 'hex', - ), - signature: Buffer.from( - '1e65032943af975c3cdef94b1fce639645bddb29265321e0277a0f48143ef7f6f6daa1046234a09cc593969ff04d8d082edd15a4a9b90a7b8865fcd9dac44300', - 'hex', - ), - }, - schema: blockHeaderSchema, - }, - }; +const validBlockHeader1 = { + version: 1, + timestamp: 1590557445, + height: 12385603, + previousBlockID: Buffer.from( + 'ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad', + 'hex', + ), + transactionRoot: Buffer.from( + 'a665a45920422f9d417e4867efdc4fb8a04a1f3fff1fa07e998e86f7f7a27ae3', + 'hex', + ), + generatorPublicKey: Buffer.from( + '68a751863fe73b8ede8d832be628ff680d617fa15c74d00142f9025d5f37dd50', + 'hex', + ), + reward: '400000000', + asset: Buffer.from('d59386e0ae435e292fbe0ebcdb954b75ed5fb3922091277cb19f798fc5d50718', 'hex'), + signature: Buffer.from( + '8331b5123cac056e2ec8361c56e642db0ca0e13abe33696d23d4d00ad6de844919296e87abe8e172f67fd882b4c0b1c1804b7d9075ecf975cf2631d8d7efef0c', + 'hex', + ), +}; - const validBlockHeader1Encoded = BlockHeader.encode(input.validBlockHeader1.object).finish(); - const validBlockHeader2Encoded = BlockHeader.encode(input.validBlockHeader2.object).finish(); +const validBlockHeader2 = { + version: 3, + timestamp: 1590557804, + height: 901049, + previousBlockID: Buffer.from( + 'e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855', + 'hex', + ), + transactionRoot: Buffer.from( + 'e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855', + 'hex', + ), + generatorPublicKey: Buffer.from( + 'acc56344dea609e80cf5d4165e46917104fe701927847fc2a5d40e37574b2b38', + 'hex', + ), + reward: '400000000', + asset: Buffer.from('eaaf9d4c65cb501c811ef812847a55513181474d734ead1b95b7e1e5b574d223', 'hex'), + signature: Buffer.from( + '1e65032943af975c3cdef94b1fce639645bddb29265321e0277a0f48143ef7f6f6daa1046234a09cc593969ff04d8d082edd15a4a9b90a7b8865fcd9dac44300', + 'hex', + ), +}; - return [ +const validBlockHeader1Encoded = BlockHeader.encode(validBlockHeader1).finish(); +const validBlockHeader2Encoded = BlockHeader.encode(validBlockHeader2).finish(); + +module.exports = { + validBlockHeaderEncodingsTestCases: [ { description: 'Encoding of valid block header 1', - input: input.validBlockHeader1, - output: { value: validBlockHeader1Encoded.toString('hex') }, + input: { object: validBlockHeader1, schema: blockHeaderSchema }, + output: { value: validBlockHeader1Encoded }, }, { description: 'Encoding of valid block header 2', - input: input.validBlockHeader2, - output: { value: validBlockHeader2Encoded.toString('hex') }, + input: { object: validBlockHeader2, schema: blockHeaderSchema }, + output: { value: validBlockHeader2Encoded }, }, - ]; -}; + ], -module.exports = generateValidBlockHeaderEncodings; + validBlockHeaderDecodingsTestCases: [ + { + description: 'Decoding of valid block header 1', + input: { value: validBlockHeader1Encoded, schema: blockHeaderSchema }, + output: { object: validBlockHeader1 }, + }, + { + description: 'Decoding of valid block header 2', + input: { value: validBlockHeader2Encoded, schema: blockHeaderSchema }, + output: { object: validBlockHeader2 }, + }, + ], +}; diff --git a/protocol-specs/generators/lisk_codec/types_generators/booleans.js b/protocol-specs/generators/lisk_codec/types_generators/booleans.js index 5fe754b3ad2..81b1557af92 100644 --- a/protocol-specs/generators/lisk_codec/types_generators/booleans.js +++ b/protocol-specs/generators/lisk_codec/types_generators/booleans.js @@ -18,55 +18,43 @@ const prepareProtobuffersBooleans = () => protobuf.loadSync('./generators/lisk_codec/proto_files/booleans.proto'); const { Boolean } = prepareProtobuffersBooleans(); -const generateValidBooleanEncodings = () => { - const input = { - booleanTrue: { - object: { - state: true, - }, - schema: { - $id: 'object5', - type: 'object', - properties: { - state: { - dataType: 'boolean', - fieldNumber: 1, - }, - }, - }, +const schema = { + $id: 'object5', + type: 'object', + properties: { + state: { + dataType: 'boolean', + fieldNumber: 1, }, - booleanFalse: { - object: { - state: false, - }, - schema: { - $id: 'object6', - type: 'object', - properties: { - state: { - dataType: 'boolean', - fieldNumber: 1, - }, - }, - }, - }, - }; + }, +}; - const booleanTrueEncoded = Boolean.encode(input.booleanTrue.object).finish(); - const booleanFalseEncoded = Boolean.encode(input.booleanFalse.object).finish(); +const booleanTrueEncoded = Boolean.encode({ state: true }).finish(); +const booleanFalseEncoded = Boolean.encode({ state: false }).finish(); - return [ +module.exports = { + validBooleanEncodingsTestCases: [ { description: 'Encoding of boolean with value true', - input: input.booleanTrue, - output: { value: booleanTrueEncoded.toString('hex') }, + input: { object: { state: true }, schema }, + output: { value: booleanTrueEncoded }, }, { description: 'Encoding of boolean with value false', - input: input.booleanFalse, - output: { value: booleanFalseEncoded.toString('hex') }, + input: { object: { state: false }, schema }, + output: { value: booleanFalseEncoded }, + }, + ], + validBooleanDecodingsTestCases: [ + { + description: 'Decoding of boolean with value true', + input: { value: booleanTrueEncoded, schema }, + output: { object: { state: true } }, }, - ]; + { + description: 'Decoding of boolean with value false', + input: { value: booleanFalseEncoded, schema }, + output: { object: { state: false } }, + }, + ], }; - -module.exports = generateValidBooleanEncodings; diff --git a/protocol-specs/generators/lisk_codec/types_generators/bytes.js b/protocol-specs/generators/lisk_codec/types_generators/bytes.js index c765a0fc6cd..42c470a6a9f 100644 --- a/protocol-specs/generators/lisk_codec/types_generators/bytes.js +++ b/protocol-specs/generators/lisk_codec/types_generators/bytes.js @@ -19,55 +19,51 @@ const prepareProtobuffersBytes = () => const { Bytes } = prepareProtobuffersBytes(); -const generateValidBytesEncodings = () => { - const input = { - bytes: { - object: { - address: Buffer.from('e11a11364738225813f86ea85214400e5db08d6e', 'hex'), - }, - schema: { - $id: 'object9', - type: 'object', - properties: { - address: { - dataType: 'bytes', - fieldNumber: 1, - }, - }, - }, +const schema = { + $id: 'object9', + type: 'object', + properties: { + address: { + dataType: 'bytes', + fieldNumber: 1, }, - emptyBytes: { - object: { - address: Buffer.from(''), - }, - schema: { - $id: 'object10', - type: 'object', - properties: { - address: { - dataType: 'bytes', - fieldNumber: 1, - }, - }, - }, - }, - }; + }, +}; + +const bytes = { + address: Buffer.from('e11a11364738225813f86ea85214400e5db08d6e', 'hex'), +}; + +const emptyBytes = { + address: Buffer.from(''), +}; - const bytesEncoded = Bytes.encode(input.bytes.object).finish(); - const emptyBytesEncoded = Bytes.encode(input.emptyBytes.object).finish(); +const bytesEncoded = Bytes.encode(bytes).finish(); +const emptyBytesEncoded = Bytes.encode(emptyBytes).finish(); - return [ +module.exports = { + validBytesEncodingsTestCases: [ { description: 'Encoding of chunk of bytes', - input: input.bytes, - output: { value: bytesEncoded.toString('hex') }, + input: { object: bytes, schema }, + output: { value: bytesEncoded }, }, { description: 'Encoding of empty bytes', - input: input.emptyBytes, - output: { value: emptyBytesEncoded.toString('hex') }, + input: { object: emptyBytes, schema }, + output: { value: emptyBytesEncoded }, }, - ]; + ], + validBytesDecodingsTestCases: [ + { + description: 'Decoding of chunk of bytes', + input: { value: bytesEncoded, schema }, + output: { object: bytes }, + }, + { + description: 'Decoding of empty bytes', + input: { value: emptyBytesEncoded, schema }, + output: { object: emptyBytes }, + }, + ], }; - -module.exports = generateValidBytesEncodings; diff --git a/protocol-specs/generators/lisk_codec/types_generators/cart_sample.js b/protocol-specs/generators/lisk_codec/types_generators/cart_sample.js index 4c2d04153e7..9035bab71b5 100644 --- a/protocol-specs/generators/lisk_codec/types_generators/cart_sample.js +++ b/protocol-specs/generators/lisk_codec/types_generators/cart_sample.js @@ -19,91 +19,89 @@ const prepareProtobuffersObjects = () => const { Cart } = prepareProtobuffersObjects(); -const generateCartEncodings = () => { - const object = { - orderId: '1234', - createdAt: '1590564352', - customerId: '100', - lineItems: [ - { - productId: '5008798', - price: '599', - quantity: '1', - taxLines: [ - { - price: '599', - rate: '6', - title: 'State Tax', +const object = { + orderId: 1234, + createdAt: 1590564352, + customerId: 100, + lineItems: [ + { + productId: 5008798, + price: 599, + quantity: 1, + taxLines: [ + { + price: 599, + rate: 6, + title: 'State Tax', + }, + ], + }, + { + productId: 9008798, + price: 1599, + quantity: 1, + taxLines: [ + { + price: 1599, + rate: 7, + title: 'State Tax', + }, + ], + }, + ], +}; + +const schema = { + $id: 'cart_sample', + type: 'object', + properties: { + orderId: { + dataType: 'uint32', + fieldNumber: 1, + }, + createdAt: { + dataType: 'uint32', + fieldNumber: 2, + }, + customerId: { + dataType: 'uint32', + fieldNumber: 3, + }, + lineItems: { + type: 'array', + fieldNumber: 4, + items: { + type: 'object', + properties: { + productId: { + dataType: 'uint32', + fieldNumber: 1, }, - ], - }, - { - productId: '9008798', - price: '1599', - quantity: '1', - taxLines: [ - { - price: '1599', - rate: '7', - title: 'State Tax', + price: { + dataType: 'uint32', + fieldNumber: 2, }, - ], - }, - ], - }; - - const schema = { - $id: 'cart_sample', - type: 'object', - properties: { - orderId: { - dataType: 'uint32', - fieldNumber: 1, - }, - createdAt: { - dataType: 'uint32', - fieldNumber: 2, - }, - customerId: { - dataType: 'uint32', - fieldNumber: 3, - }, - lineItems: { - type: 'array', - fieldNumber: 4, - items: { - type: 'object', - properties: { - productId: { - dataType: 'uint32', - fieldNumber: 1, - }, - price: { - dataType: 'uint32', - fieldNumber: 2, - }, - quantity: { - dataType: 'uint32', - fieldNumber: 3, - }, - taxLines: { - type: 'array', - fieldNumber: 4, - items: { - type: 'object', - properties: { - price: { - dataType: 'uint32', - fieldNumber: 1, - }, - rate: { - dataType: 'uint32', - fieldNumber: 2, - }, - title: { - dataType: 'string', - fieldNumber: 3, - }, + quantity: { + dataType: 'uint32', + fieldNumber: 3, + }, + taxLines: { + type: 'array', + fieldNumber: 4, + items: { + type: 'object', + properties: { + price: { + dataType: 'uint32', + fieldNumber: 1, + }, + rate: { + dataType: 'uint32', + fieldNumber: 2, + }, + title: { + dataType: 'string', + fieldNumber: 3, }, }, }, @@ -111,20 +109,30 @@ const generateCartEncodings = () => { }, }, }, - }; + }, +}; - const objectEncoded = Cart.encode(object).finish(); +const objectEncoded = Cart.encode(object).finish(); - return [ +module.exports = { + cartSampleEncodingsTestCases: [ { description: 'Encoding of object with multiple arrays', input: { object, schema, }, - output: { value: objectEncoded.toString('hex') }, + output: { value: objectEncoded }, }, - ]; + ], + cartSampleDecodingsTestCases: [ + { + description: 'Decoding of object with multiple arrays', + input: { + value: objectEncoded, + schema, + }, + output: { object }, + }, + ], }; - -module.exports = generateCartEncodings; diff --git a/protocol-specs/generators/lisk_codec/types_generators/genesis_block_asset.js b/protocol-specs/generators/lisk_codec/types_generators/genesis_block_asset.js index 62efc9b5837..c2ac726bb01 100644 --- a/protocol-specs/generators/lisk_codec/types_generators/genesis_block_asset.js +++ b/protocol-specs/generators/lisk_codec/types_generators/genesis_block_asset.js @@ -19,7 +19,7 @@ const prepareProtobuffersBlock = () => const { GenesisBlockAsset } = prepareProtobuffersBlock(); -const blockAssetSchema = { +const genesisBlockAssetSchema = { $id: 'genesisBlockAssetSchema', type: 'object', required: ['accounts', 'initDelegates', 'initRounds'], @@ -123,122 +123,121 @@ const blockAssetSchema = { }, }; -const generateValidGenesisBlockAssetEncodings = () => { - const input = { - validGenesisBlockAsset1: { - object: { - initDelegates: [ - Buffer.from('03f6d90b7dbd0497dc3a52d1c27e23bb8c75897f', 'hex'), - Buffer.from('0903f4c5cb599a7928aef27e314e98291d1e3888', 'hex'), - Buffer.from('0ada6a2f6c8f891769366fc9aa6fd9f1facb36cf', 'hex'), - ], - initRounds: 3, - accounts: [ - { - address: Buffer.from('03f6d90b7dbd0497dc3a52d1c27e23bb8c75897f', 'hex'), - publicKey: Buffer.from( - 'fc65777c1d4c00f1af5880c23ba7f60cd3bf84d1bf5c697abc4ffe17cf7acac0', - 'hex', - ), - balance: '0', - nonce: '0', - keys: { - mandatoryKeys: [], - optionalKeys: [], - numberOfSignatures: 0, - }, - asset: { - delegate: { - username: 'genesis_34', - pomHeights: [], - consecutiveMissedBlocks: 0, - lastForgedHeight: 0, - isBanned: false, - totalVotesReceived: '1000000000000', - }, - sentVotes: [ - { - delegateAddress: Buffer.from('03f6d90b7dbd0497dc3a52d1c27e23bb8c75897f', 'hex'), - amount: '1000000000000', - }, - ], - unlocking: [], - }, - }, +const validGenesisBlockAsset1 = { + initDelegates: [ + Buffer.from('03f6d90b7dbd0497dc3a52d1c27e23bb8c75897f', 'hex'), + Buffer.from('0903f4c5cb599a7928aef27e314e98291d1e3888', 'hex'), + Buffer.from('0ada6a2f6c8f891769366fc9aa6fd9f1facb36cf', 'hex'), + ], + initRounds: 3, + accounts: [ + { + address: Buffer.from('03f6d90b7dbd0497dc3a52d1c27e23bb8c75897f', 'hex'), + publicKey: Buffer.from( + 'fc65777c1d4c00f1af5880c23ba7f60cd3bf84d1bf5c697abc4ffe17cf7acac0', + 'hex', + ), + balance: '0', + nonce: '0', + keys: { + mandatoryKeys: [], + optionalKeys: [], + numberOfSignatures: 0, + }, + asset: { + delegate: { + username: 'genesis_34', + pomHeights: [], + consecutiveMissedBlocks: 0, + lastForgedHeight: 0, + isBanned: false, + totalVotesReceived: '1000000000000', + }, + sentVotes: [ { - address: Buffer.from('0903f4c5cb599a7928aef27e314e98291d1e3888', 'hex'), - publicKey: Buffer.from( - '3f571324e9dc7b2481b71a7dc56637f1234504158986a242e90c33d8d20fdd92', - 'hex', - ), - balance: '0', - nonce: '0', - keys: { - mandatoryKeys: [], - optionalKeys: [], - numberOfSignatures: 0, - }, - asset: { - delegate: { - username: 'genesis_74', - pomHeights: [], - consecutiveMissedBlocks: 0, - lastForgedHeight: 0, - isBanned: false, - totalVotesReceived: '1000000000000', - }, - sentVotes: [], - unlocking: [], - }, + delegateAddress: Buffer.from('03f6d90b7dbd0497dc3a52d1c27e23bb8c75897f', 'hex'), + amount: '1000000000000', }, + ], + unlocking: [], + }, + }, + { + address: Buffer.from('0903f4c5cb599a7928aef27e314e98291d1e3888', 'hex'), + publicKey: Buffer.from( + '3f571324e9dc7b2481b71a7dc56637f1234504158986a242e90c33d8d20fdd92', + 'hex', + ), + balance: '0', + nonce: '0', + keys: { + mandatoryKeys: [], + optionalKeys: [], + numberOfSignatures: 0, + }, + asset: { + delegate: { + username: 'genesis_74', + pomHeights: [], + consecutiveMissedBlocks: 0, + lastForgedHeight: 0, + isBanned: false, + totalVotesReceived: '1000000000000', + }, + sentVotes: [], + unlocking: [], + }, + }, + { + address: Buffer.from('0ada6a2f6c8f891769366fc9aa6fd9f1facb36cf', 'hex'), + publicKey: Buffer.from( + 'c69698ef30012964aafacfbe637bb63854b6109cc5c5f22aa4b3dc3e8dca8217', + 'hex', + ), + balance: '0', + nonce: '0', + keys: { + mandatoryKeys: [], + optionalKeys: [], + numberOfSignatures: 0, + }, + asset: { + delegate: { + username: 'genesis_98', + pomHeights: [], + consecutiveMissedBlocks: 0, + lastForgedHeight: 0, + isBanned: false, + totalVotesReceived: '1000000000000', + }, + sentVotes: [ { - address: Buffer.from('0ada6a2f6c8f891769366fc9aa6fd9f1facb36cf', 'hex'), - publicKey: Buffer.from( - 'c69698ef30012964aafacfbe637bb63854b6109cc5c5f22aa4b3dc3e8dca8217', - 'hex', - ), - balance: '0', - nonce: '0', - keys: { - mandatoryKeys: [], - optionalKeys: [], - numberOfSignatures: 0, - }, - asset: { - delegate: { - username: 'genesis_98', - pomHeights: [], - consecutiveMissedBlocks: 0, - lastForgedHeight: 0, - isBanned: false, - totalVotesReceived: '1000000000000', - }, - sentVotes: [ - { - delegateAddress: Buffer.from('0ada6a2f6c8f891769366fc9aa6fd9f1facb36cf', 'hex'), - amount: '1000000000000', - }, - ], - unlocking: [], - }, + delegateAddress: Buffer.from('0ada6a2f6c8f891769366fc9aa6fd9f1facb36cf', 'hex'), + amount: '1000000000000', }, ], + unlocking: [], }, - schema: blockAssetSchema, }, - }; + ], +}; - const validBlockAssetEncoded = GenesisBlockAsset.encode( - input.validGenesisBlockAsset1.object, - ).finish(); +const validBlockAssetEncoded = GenesisBlockAsset.encode(validGenesisBlockAsset1).finish(); - return [ +module.exports = { + validGenesisBlockAssetEncodingsTestCases: [ { - description: 'Encoding of valid block asset', - input: input.validGenesisBlockAsset1, - output: { value: validBlockAssetEncoded.toString('hex') }, + description: 'Encoding of valid genesis block asset', + input: { object: validGenesisBlockAsset1, schema: genesisBlockAssetSchema }, + output: { value: validBlockAssetEncoded }, }, - ]; -}; + ], -module.exports = generateValidGenesisBlockAssetEncodings; + validGenesisBlockAssetDecodingsTestCases: [ + { + description: 'Decoding of valid genesis block asset', + input: { value: validBlockAssetEncoded, schema: genesisBlockAssetSchema }, + output: { object: validGenesisBlockAsset1 }, + }, + ], +}; diff --git a/protocol-specs/generators/lisk_codec/types_generators/index.js b/protocol-specs/generators/lisk_codec/types_generators/index.js index d191bfdcb05..df9a7a3f3a8 100644 --- a/protocol-specs/generators/lisk_codec/types_generators/index.js +++ b/protocol-specs/generators/lisk_codec/types_generators/index.js @@ -11,36 +11,36 @@ * * Removal or modification of this copyright notice is prohibited. */ -const generateValidBooleanEncodings = require('./booleans'); -const generateValidNumberEncodings = require('./numbers'); -const generateValidStringEncodings = require('./strings'); -const generateValidBytesEncodings = require('./bytes'); -const generateValidObjectEncodings = require('./objects'); -const generateValidArrayEncodings = require('./arrays'); -const generateValidBlock = require('./block'); -const generateValidGenesisBlock = require('./genesis_block_asset'); -const generateValidBlockHeader = require('./block_header'); -const generateValidBlockAsset = require('./block_asset'); -const generateValidAccount = require('./account'); -const generateValidTransaction = require('./transaction'); -const generateCartEncodings = require('./cart_sample'); -const generatePeerInfoEncodings = require('./peer_sample'); -const generateNestedArrayEncodings = require('./nestest_array'); +const booleans = require('./booleans'); +const numbers = require('./numbers'); +const bytes = require('./bytes'); +const objects = require('./objects'); +const block = require('./block'); +const genesisBlockAsset = require('./genesis_block_asset'); +const blockHeader = require('./block_header'); +const transaction = require('./transaction'); +const cartSample = require('./cart_sample'); +const peerInfo = require('./peer_info'); +const nestedArray = require('./nested_array'); +const strings = require('./strings'); +const account = require('./account'); +const arrays = require('./arrays'); +const blockAsset = require('./block_asset'); module.exports = { - generateValidBooleanEncodings, - generateValidNumberEncodings, - generateValidStringEncodings, - generateValidBytesEncodings, - generateValidObjectEncodings, - generateValidArrayEncodings, - generateValidBlock, - generateValidGenesisBlock, - generateValidBlockHeader, - generateValidBlockAsset, - generateValidAccount, - generateValidTransaction, - generateCartEncodings, - generatePeerInfoEncodings, - generateNestedArrayEncodings, + ...strings, + ...account, + ...arrays, + ...blockAsset, + ...blockHeader, + ...block, + ...booleans, + ...bytes, + ...cartSample, + ...nestedArray, + ...genesisBlockAsset, + ...numbers, + ...objects, + ...peerInfo, + ...transaction, }; diff --git a/protocol-specs/generators/lisk_codec/types_generators/nested_array.js b/protocol-specs/generators/lisk_codec/types_generators/nested_array.js new file mode 100644 index 00000000000..9c34db8eda0 --- /dev/null +++ b/protocol-specs/generators/lisk_codec/types_generators/nested_array.js @@ -0,0 +1,162 @@ +/* + * Copyright © 2020 Lisk Foundation + * + * See the LICENSE file at the top-level directory of this distribution + * for licensing information. + * + * Unless otherwise agreed in a custom licensing agreement with the Lisk Foundation, + * no part of this software, including this file, may be copied, modified, + * propagated, or distributed except according to the terms contained in the + * LICENSE file. + * + * Removal or modification of this copyright notice is prohibited. + */ + +const protobuf = require('protobufjs'); + +const prepareProtobuffersObjects = () => + protobuf.loadSync('./generators/lisk_codec/proto_files/nested_array.proto'); + +const { StateDiff, StateDiffString } = prepareProtobuffersObjects(); + +const diff = { + updated: [ + { + key: 'accounts:address:ad42f8e867d618171bf4982e64269442148f6e11', + value: [ + { + code: '=', + line: 1, + }, + { + code: '+', + line: 333, + }, + ], + }, + ], + created: ['chain:delegates', 'consensus:bft'], +}; +const diffSchema = { + $id: '/state/diff', + type: 'object', + required: ['updated', 'created'], + properties: { + updated: { + type: 'array', + fieldNumber: 1, + items: { + type: 'object', + properties: { + key: { + dataType: 'string', + fieldNumber: 1, + }, + value: { + type: 'array', + fieldNumber: 2, + items: { + type: 'object', + properties: { + code: { + dataType: 'string', + fieldNumber: 1, + }, + line: { + dataType: 'uint32', + fieldNumber: 2, + }, + }, + }, + }, + }, + }, + }, + created: { + type: 'array', + fieldNumber: 2, + items: { + dataType: 'string', + }, + }, + }, +}; + +const diffString = { + updated: [ + { + key: 'accounts:address:ad42f8e867d618171bf4982e64269442148f6e11', + value: ['diff1', 'diff2'], + }, + { + key: 'accounts:address:69a6ba19f58605c6fd260b9909a5108523db84', + value: ['diff5', 'diff6', 'diff7', 'diff5'], + }, + ], + created: ['chain:delegates', 'consensus:bft'], +}; +const diffStringSchema = { + $id: '/state/diff-string', + type: 'object', + required: ['updated', 'created'], + properties: { + updated: { + type: 'array', + fieldNumber: 1, + items: { + type: 'object', + properties: { + key: { + dataType: 'string', + fieldNumber: 1, + }, + value: { + type: 'array', + fieldNumber: 2, + items: { + dataType: 'string', + }, + }, + }, + }, + }, + created: { + type: 'array', + fieldNumber: 2, + items: { + dataType: 'string', + }, + }, + }, +}; + +const objectEncoded = StateDiff.encode(diff).finish(); +const objectStringEncoded = StateDiffString.encode(diffString).finish(); + +module.exports = { + validNestedArrayEncodingsTestCases: [ + { + description: 'Encoding of nested array object sample', + input: { object: diff, schema: diffSchema }, + output: { value: objectEncoded }, + }, + { + description: 'Encoding of nested array string sample', + input: { object: diffString, schema: diffStringSchema }, + output: { value: objectStringEncoded }, + }, + ], + + validNestedArrayDecodingsTestCases: [ + { + description: 'Decoding of nested array object sample', + input: { value: objectEncoded, schema: diffSchema }, + output: { object: diff }, + }, + { + description: 'Decoding of nested array string sample', + input: { value: objectStringEncoded, schema: diffStringSchema }, + output: { object: diffString }, + }, + ], +}; diff --git a/protocol-specs/generators/lisk_codec/types_generators/nestest_array.js b/protocol-specs/generators/lisk_codec/types_generators/nestest_array.js deleted file mode 100644 index 4fe3886f55e..00000000000 --- a/protocol-specs/generators/lisk_codec/types_generators/nestest_array.js +++ /dev/null @@ -1,156 +0,0 @@ -/* - * Copyright © 2020 Lisk Foundation - * - * See the LICENSE file at the top-level directory of this distribution - * for licensing information. - * - * Unless otherwise agreed in a custom licensing agreement with the Lisk Foundation, - * no part of this software, including this file, may be copied, modified, - * propagated, or distributed except according to the terms contained in the - * LICENSE file. - * - * Removal or modification of this copyright notice is prohibited. - */ - -const protobuf = require('protobufjs'); - -const prepareProtobuffersObjects = () => - protobuf.loadSync('./generators/lisk_codec/proto_files/nested_array.proto'); - -const { StateDiff, StateDiffString } = prepareProtobuffersObjects(); - -const generateValidPeerInfoEncodings = () => { - const input = { - diff: { - object: { - updated: [ - { - key: 'accounts:address:ad42f8e867d618171bf4982e64269442148f6e11', - value: [ - { - code: '=', - line: 1, - }, - { - code: '+', - line: 333, - }, - ], - }, - ], - created: ['chain:delegates', 'consensus:bft'], - }, - schema: { - $id: '/state/diff', - type: 'object', - required: ['updated', 'created'], - properties: { - updated: { - type: 'array', - fieldNumber: 1, - items: { - type: 'object', - properties: { - key: { - dataType: 'string', - fieldNumber: 1, - }, - value: { - type: 'array', - fieldNumber: 2, - items: { - type: 'object', - properties: { - code: { - dataType: 'string', - fieldNumber: 1, - }, - line: { - dataType: 'uint32', - fieldNumber: 2, - }, - }, - }, - }, - }, - }, - }, - created: { - type: 'array', - fieldNumber: 2, - items: { - dataType: 'string', - }, - }, - }, - }, - }, - diffString: { - object: { - updated: [ - { - key: 'accounts:address:ad42f8e867d618171bf4982e64269442148f6e11', - value: ['diff1', 'diff2'], - }, - { - key: 'accounts:address:69a6ba19f58605c6fd260b9909a5108523db84', - value: ['diff5', 'diff6', 'diff7', 'diff5'], - }, - ], - created: ['chain:delegates', 'consensus:bft'], - }, - schema: { - $id: '/state/diff-string', - type: 'object', - required: ['updated', 'created'], - properties: { - updated: { - type: 'array', - fieldNumber: 1, - items: { - type: 'object', - properties: { - key: { - dataType: 'string', - fieldNumber: 1, - }, - value: { - type: 'array', - fieldNumber: 2, - items: { - dataType: 'string', - }, - }, - }, - }, - }, - created: { - type: 'array', - fieldNumber: 2, - items: { - dataType: 'string', - }, - }, - }, - }, - }, - }; - - const objectEncoded = StateDiff.encode(input.diff.object).finish(); - const objectStringEncoded = StateDiffString.encode(input.diffString.object).finish(); - - return [ - { - description: 'Encoding of nested array object sample', - input: input.diff, - output: { value: objectEncoded.toString('hex') }, - }, - { - description: 'Encoding of nested array string sample', - input: input.diffString, - output: { value: objectStringEncoded.toString('hex') }, - }, - ]; -}; - -module.exports = generateValidPeerInfoEncodings; diff --git a/protocol-specs/generators/lisk_codec/types_generators/numbers.js b/protocol-specs/generators/lisk_codec/types_generators/numbers.js index d0cec87cb39..db3b1fd4b27 100644 --- a/protocol-specs/generators/lisk_codec/types_generators/numbers.js +++ b/protocol-specs/generators/lisk_codec/types_generators/numbers.js @@ -19,97 +19,75 @@ const prepareProtobuffersNumbers = () => const { Number32, SignedNumber32, Number64, SignedNumber64 } = prepareProtobuffersNumbers(); -const generateValidNumberEncodings = () => { - const input = { - message32: { - object: { - number: 10, - }, - schema: { - $id: 'object1', - type: 'object', - properties: { - number: { - dataType: 'uint32', - fieldNumber: 1, - }, - }, - }, +const getNumberSchema = type => ({ + $id: `number-schema-${type}`, + type: 'object', + properties: { + number: { + dataType: type, + fieldNumber: 1, }, - messageSigned32: { - object: { - number: -10, - }, - schema: { - $id: 'object2', - type: 'object', - properties: { - number: { - dataType: 'sint32', - fieldNumber: 1, - }, - }, - }, - }, - message64: { - object: { - number: 372036854775807, - }, - schema: { - $id: 'object3', - type: 'object', - properties: { - number: { - dataType: 'uint64', - fieldNumber: 1, - }, - }, - }, - }, - messageSigned64: { - object: { - number: -9007199254740991, - }, - schema: { - $id: 'object4', - type: 'object', - properties: { - number: { - dataType: 'sint64', - fieldNumber: 1, - }, - }, - }, - }, - }; + }, +}); + +const number32Schema = getNumberSchema('uint32'); +const number32 = { number: 10 }; +const signedNumber32Schema = getNumberSchema('sint32'); +const signedNumber32 = { number: -10 }; +const number64Schema = getNumberSchema('uint64'); +const number64 = { number: '372036854775807' }; +const signedNumber64Schema = getNumberSchema('sint64'); +const signedNumber64 = { number: '-9007199254740991' }; - const numberEncoded32 = Number32.encode(input.message32.object).finish(); - const signedNumberEncoded32 = SignedNumber32.encode(input.messageSigned32.object).finish(); - const numberEncoded64 = Number64.encode(input.message64.object).finish(); - const signedNumberEncoded64 = SignedNumber64.encode(input.messageSigned64.object).finish(); +const numberEncoded32 = Number32.encode(number32).finish(); +const signedNumberEncoded32 = SignedNumber32.encode(signedNumber32).finish(); +const numberEncoded64 = Number64.encode(number64).finish(); +const signedNumberEncoded64 = SignedNumber64.encode(signedNumber64).finish(); - return [ +module.exports = { + validNumberEncodingsTestCases: [ { description: 'Encoding 32 bit unsigned number', - input: input.message32, - output: { value: numberEncoded32.toString('hex') }, + input: { object: number32, schema: number32Schema }, + output: { value: numberEncoded32 }, }, { description: 'Encoding 32 bit signed number', - input: input.messageSigned32, - output: { value: signedNumberEncoded32.toString('hex') }, + input: { object: signedNumber32, schema: signedNumber32Schema }, + output: { value: signedNumberEncoded32 }, }, { description: 'Encoding 64 bit unsigned number', - input: input.message64, - output: { value: numberEncoded64.toString('hex') }, + input: { object: { number: BigInt(number64.number) }, schema: number64Schema }, + output: { value: numberEncoded64 }, }, { description: 'Encoding 64 bit signed number', - input: input.messageSigned64, - output: { value: signedNumberEncoded64.toString('hex') }, + input: { object: { number: BigInt(signedNumber64.number) }, schema: signedNumber64Schema }, + output: { value: signedNumberEncoded64 }, }, - ]; -}; + ], -module.exports = generateValidNumberEncodings; + validNumberDecodingsTestCases: [ + { + description: 'Decoding 32 bit unsigned number', + input: { value: numberEncoded32, schema: number32Schema }, + output: { object: number32 }, + }, + { + description: 'Decoding 32 bit signed number', + input: { value: signedNumberEncoded32, schema: signedNumber32Schema }, + output: { object: signedNumber32 }, + }, + { + description: 'Decoding 64 bit unsigned number', + input: { value: numberEncoded64, schema: number64Schema }, + output: { object: { number: BigInt(number64.number) } }, + }, + { + description: 'Decoding 64 bit signed number', + input: { value: signedNumberEncoded64, schema: signedNumber64Schema }, + output: { object: { number: BigInt(signedNumber64.number) } }, + }, + ], +}; diff --git a/protocol-specs/generators/lisk_codec/types_generators/objects.js b/protocol-specs/generators/lisk_codec/types_generators/objects.js index 9378b925363..480a07cfc3a 100644 --- a/protocol-specs/generators/lisk_codec/types_generators/objects.js +++ b/protocol-specs/generators/lisk_codec/types_generators/objects.js @@ -19,114 +19,121 @@ const prepareProtobuffersObjects = () => const { Objects, ObjectWithOptionalProp } = prepareProtobuffersObjects(); -const generateValidObjectEncodings = () => { - const object = { - address: Buffer.from('e11a11364738225813f86ea85214400e5db08d6e', 'hex'), - balance: 10000000, - isDelegate: true, - name: 'delegate', - asset: { - data: 'Check out the Lisk SDK now in binary!', - fooBar: { - foo: 9, - bar: 9, - }, +const object = { + address: Buffer.from('e11a11364738225813f86ea85214400e5db08d6e', 'hex'), + balance: '10000000', + isDelegate: true, + name: 'delegate', + asset: { + data: 'Check out the Lisk SDK now in binary!', + fooBar: { + foo: 9, + bar: 9, }, - }; - - const input = { - object: { - object, - schema: { - $id: 'object11', - type: 'object', - properties: { - address: { - dataType: 'bytes', - fieldNumber: 1, - }, - balance: { - dataType: 'uint64', - fieldNumber: 2, - }, - isDelegate: { - dataType: 'boolean', - fieldNumber: 3, - }, - name: { - dataType: 'string', - fieldNumber: 4, - }, - asset: { - type: 'object', - fieldNumber: 5, - properties: { - data: { - dataType: 'string', - fieldNumber: 1, - }, - fooBar: { - type: 'object', - fieldNumber: 2, - properties: { - foo: { - dataType: 'uint32', - fieldNumber: 1, - }, - bar: { - dataType: 'uint32', - fieldNumber: 2, - }, - }, - }, + }, +}; +const objectSchema = { + $id: 'object11', + type: 'object', + properties: { + address: { + dataType: 'bytes', + fieldNumber: 1, + }, + balance: { + dataType: 'uint64', + fieldNumber: 2, + }, + isDelegate: { + dataType: 'boolean', + fieldNumber: 3, + }, + name: { + dataType: 'string', + fieldNumber: 4, + }, + asset: { + type: 'object', + fieldNumber: 5, + properties: { + data: { + dataType: 'string', + fieldNumber: 1, + }, + fooBar: { + type: 'object', + fieldNumber: 2, + properties: { + foo: { + dataType: 'uint32', + fieldNumber: 1, + }, + bar: { + dataType: 'uint32', + fieldNumber: 2, }, }, }, }, }, - objectOptionalProp: { - object: { - isActive: true, - value: 1, - }, - schema: { - $id: 'object12', - type: 'object', - properties: { - isActive: { - dataType: 'boolean', - fieldNumber: 1, - }, - data: { - dataType: 'bytes', - fieldNumber: 2, - }, - value: { - dataType: 'uint64', - fieldNumber: 3, - }, - }, - }, + }, +}; + +const objectWithOptionalProps = { + isActive: true, + value: '1', +}; + +const objectWithOptionalPropsSchema = { + $id: 'object12', + type: 'object', + properties: { + isActive: { + dataType: 'boolean', + fieldNumber: 1, + }, + data: { + dataType: 'bytes', + fieldNumber: 2, + }, + value: { + dataType: 'uint64', + fieldNumber: 3, }, - }; + }, +}; - const objectEncoded = Objects.encode(input.object.object).finish(); - const objectOptionalPropEncoded = ObjectWithOptionalProp.encode( - input.objectOptionalProp.object, - ).finish(); +const objectEncoded = Objects.encode(object).finish(); +const objectOptionalPropEncoded = ObjectWithOptionalProp.encode(objectWithOptionalProps).finish(); - return [ +module.exports = { + validObjectEncodingsTestCases: [ { description: 'Encoding of object', - input: input.object, - output: { value: objectEncoded.toString('hex') }, + input: { object, schema: objectSchema }, + output: { value: objectEncoded }, }, { description: 'Encoding of object with optional property', - input: input.objectOptionalProp, - output: { value: objectOptionalPropEncoded.toString('hex') }, + input: { object: objectWithOptionalProps, schema: objectWithOptionalPropsSchema }, + output: { value: objectOptionalPropEncoded }, + }, + ], + validObjectDecodingsTestCases: [ + { + description: 'Decoding of object', + input: { value: objectEncoded, schema: objectSchema }, + output: { object }, + }, + { + description: 'Decoding of object with optional property', + input: { value: objectOptionalPropEncoded, schema: objectWithOptionalPropsSchema }, + output: { + object: { + ...objectWithOptionalProps, + data: Buffer.alloc(0), + }, + }, }, - ]; + ], }; - -module.exports = generateValidObjectEncodings; diff --git a/protocol-specs/generators/lisk_codec/types_generators/peer_info.js b/protocol-specs/generators/lisk_codec/types_generators/peer_info.js new file mode 100644 index 00000000000..1412a64ab56 --- /dev/null +++ b/protocol-specs/generators/lisk_codec/types_generators/peer_info.js @@ -0,0 +1,110 @@ +/* + * Copyright © 2020 Lisk Foundation + * + * See the LICENSE file at the top-level directory of this distribution + * for licensing information. + * + * Unless otherwise agreed in a custom licensing agreement with the Lisk Foundation, + * no part of this software, including this file, may be copied, modified, + * propagated, or distributed except according to the terms contained in the + * LICENSE file. + * + * Removal or modification of this copyright notice is prohibited. + */ + +const protobuf = require('protobufjs'); + +const prepareProtobuffersObjects = () => + protobuf.loadSync('./generators/lisk_codec/proto_files/peer_sample.proto'); + +const { PeerInfo } = prepareProtobuffersObjects(); + +const peerInfo = { + ipAddress: '1.1.1.1', + wsPort: 1111, + networkIdentifier: 'f8fe7ecc3e29f58f39d8a538f9a35b80b4b6ab9674f0300e25e33ff41274ae32', + networkVersion: '2.0', + nonce: 'iNIgD0Mb3s/RMaXbs', + os: 'darwin', + height: 123, +}; +const peerInfoSchema = { + $id: 'peerInfo', + type: 'object', + properties: { + ipAddress: { + dataType: 'string', + fieldNumber: 1, + }, + wsPort: { + dataType: 'uint32', + fieldNumber: 2, + }, + networkIdentifier: { + dataType: 'string', + fieldNumber: 3, + }, + networkVersion: { + dataType: 'string', + fieldNumber: 4, + }, + nonce: { + dataType: 'string', + fieldNumber: 5, + }, + os: { + dataType: 'string', + fieldNumber: 6, + }, + height: { + dataType: 'uint32', + fieldNumber: 7, + }, + }, + required: ['ipAddress', 'wsPort'], +}; + +const peerInfoWithOptionalProps = { + ipAddress: '1.1.1.1', + wsPort: 1111, + os: 'darwin', +}; + +const objectEncoded = PeerInfo.encode(peerInfo).finish(); +const objectOptionalPropEncoded = PeerInfo.encode(peerInfoWithOptionalProps).finish(); + +module.exports = { + validPeerInfoEncodingsTestCases: [ + { + description: 'Encoding of peer info sample', + input: { object: peerInfo, schema: peerInfoSchema }, + output: { value: objectEncoded }, + }, + { + description: 'Encoding of peer info sample with optional property', + input: { object: peerInfoWithOptionalProps, schema: peerInfoSchema }, + output: { value: objectOptionalPropEncoded }, + }, + ], + + validPeerInfoDecodingsTestCases: [ + { + description: 'Decoding of peer info sample', + input: { value: objectEncoded, schema: peerInfoSchema }, + output: { object: peerInfo }, + }, + { + description: 'Decoding of peer info sample with optional property', + input: { value: objectOptionalPropEncoded, schema: peerInfoSchema }, + output: { + object: { + ...peerInfoWithOptionalProps, + networkIdentifier: '', + networkVersion: '', + nonce: '', + height: 0, + }, + }, + }, + ], +}; diff --git a/protocol-specs/generators/lisk_codec/types_generators/peer_sample.js b/protocol-specs/generators/lisk_codec/types_generators/peer_sample.js deleted file mode 100644 index 529e785c1cd..00000000000 --- a/protocol-specs/generators/lisk_codec/types_generators/peer_sample.js +++ /dev/null @@ -1,131 +0,0 @@ -/* - * Copyright © 2020 Lisk Foundation - * - * See the LICENSE file at the top-level directory of this distribution - * for licensing information. - * - * Unless otherwise agreed in a custom licensing agreement with the Lisk Foundation, - * no part of this software, including this file, may be copied, modified, - * propagated, or distributed except according to the terms contained in the - * LICENSE file. - * - * Removal or modification of this copyright notice is prohibited. - */ - -const protobuf = require('protobufjs'); - -const prepareProtobuffersObjects = () => - protobuf.loadSync('./generators/lisk_codec/proto_files/peer_sample.proto'); - -const { PeerInfo } = prepareProtobuffersObjects(); - -const generateValidPeerInfoEncodings = () => { - const input = { - object: { - object: { - ipAddress: '1.1.1.1', - wsPort: 1111, - networkIdentifier: 'f8fe7ecc3e29f58f39d8a538f9a35b80b4b6ab9674f0300e25e33ff41274ae32', - networkVersion: '2.0', - nonce: 'iNIgD0Mb3s/RMaXbs', - os: 'darwin', - height: 123, - }, - schema: { - $id: 'peerInfo', - type: 'object', - properties: { - ipAddress: { - dataType: 'string', - fieldNumber: 1, - }, - wsPort: { - dataType: 'uint32', - fieldNumber: 2, - }, - networkIdentifier: { - dataType: 'string', - fieldNumber: 3, - }, - networkVersion: { - dataType: 'string', - fieldNumber: 4, - }, - nonce: { - dataType: 'string', - fieldNumber: 5, - }, - os: { - dataType: 'string', - fieldNumber: 6, - }, - height: { - dataType: 'uint32', - fieldNumber: 7, - }, - }, - required: ['ipAddress', 'wsPort'], - }, - }, - objectOptionalProp: { - object: { - ipAddress: '1.1.1.1', - wsPort: 1111, - os: 'darwin', - }, - schema: { - $id: 'peerInfo', - type: 'object', - properties: { - ipAddress: { - dataType: 'string', - fieldNumber: 1, - }, - wsPort: { - dataType: 'uint32', - fieldNumber: 2, - }, - networkIdentifier: { - dataType: 'string', - fieldNumber: 3, - }, - networkVersion: { - dataType: 'string', - fieldNumber: 4, - }, - nonce: { - dataType: 'string', - fieldNumber: 5, - }, - os: { - dataType: 'string', - fieldNumber: 6, - }, - height: { - dataType: 'uint32', - fieldNumber: 7, - }, - }, - required: ['ipAddress', 'wsPort'], - }, - }, - }; - - const objectEncoded = PeerInfo.encode(input.object.object).finish(); - const objectOptionalPropEncoded = PeerInfo.encode(input.objectOptionalProp.object).finish(); - - return [ - { - description: 'Encoding of peer info sample', - input: input.object, - output: { value: objectEncoded.toString('hex') }, - }, - { - description: 'Encoding of peer info sample with optional property', - input: input.objectOptionalProp, - output: { value: objectOptionalPropEncoded.toString('hex') }, - }, - ]; -}; - -module.exports = generateValidPeerInfoEncodings; diff --git a/protocol-specs/generators/lisk_codec/types_generators/strings.js b/protocol-specs/generators/lisk_codec/types_generators/strings.js index 76dd7614ca2..1cacf63678f 100644 --- a/protocol-specs/generators/lisk_codec/types_generators/strings.js +++ b/protocol-specs/generators/lisk_codec/types_generators/strings.js @@ -19,76 +19,59 @@ const prepareProtobuffersStrings = () => const { String } = prepareProtobuffersStrings(); -const generateValidStringEncodings = () => { - const input = { - string: { - object: { - data: 'Checkout Lisk SDK!', - }, - schema: { - $id: 'object7', - type: 'object', - properties: { - data: { - dataType: 'string', - fieldNumber: 1, - }, - }, - }, +const schema = { + $id: 'string-schema', + type: 'object', + properties: { + data: { + dataType: 'string', + fieldNumber: 1, }, - emptyString: { - object: { - data: '', - }, - schema: { - $id: 'object8', - type: 'object', - properties: { - data: { - dataType: 'string', - fieldNumber: 1, - }, - }, - }, - }, - symbols: { - object: { - data: '€.ƒ.‰.Œ.£.©.®.µ.Æ.ü.ý.ø.Ç.¥.ß', - }, - schema: { - $id: 'object8', - type: 'object', - properties: { - data: { - dataType: 'string', - fieldNumber: 1, - }, - }, - }, - }, - }; + }, +}; + +const normal = { data: 'Checkout Lisk SDK!' }; +const emptyString = { data: '' }; +const symbols = { data: '€.ƒ.‰.Œ.£.©.®.µ.Æ.ü.ý.ø.Ç.¥.ß' }; - const stringEncoded = String.encode(input.string.object).finish(); - const emptyStringEncoded = String.encode(input.emptyString.object).finish(); - const symbolsStringEncoded = String.encode(input.symbols.object).finish(); +const stringEncoded = String.encode(normal).finish(); +const emptyStringEncoded = String.encode(emptyString).finish(); +const symbolsStringEncoded = String.encode(symbols).finish(); - return [ +module.exports = { + validStringsEncodingTestCases: [ { description: 'Encoding of string', - input: input.string, - output: { value: stringEncoded.toString('hex') }, + input: { object: normal, schema }, + output: { value: stringEncoded }, }, { description: 'Encoding of empty string', - input: input.emptyString, - output: { value: emptyStringEncoded.toString('hex') }, + input: { object: emptyString, schema }, + output: { value: emptyStringEncoded }, }, { description: 'Encoding of some utf symbols string', - input: input.symbols, - output: { value: symbolsStringEncoded.toString('hex') }, + input: { object: symbols, schema }, + output: { value: symbolsStringEncoded }, }, - ]; -}; + ], -module.exports = generateValidStringEncodings; + validStringsDecodingTestCases: [ + { + description: 'Decoding of string', + input: { value: stringEncoded, schema }, + output: { object: normal }, + }, + { + description: 'Encoding of empty string', + input: { value: emptyStringEncoded, schema }, + output: { object: emptyString }, + }, + { + description: 'Encoding of some utf symbols string', + input: { value: symbolsStringEncoded, schema }, + output: { object: symbols }, + }, + ], +}; diff --git a/protocol-specs/generators/lisk_codec/types_generators/transaction.js b/protocol-specs/generators/lisk_codec/types_generators/transaction.js index 8a389632669..d0fbc695639 100644 --- a/protocol-specs/generators/lisk_codec/types_generators/transaction.js +++ b/protocol-specs/generators/lisk_codec/types_generators/transaction.js @@ -69,107 +69,113 @@ const multisigAssetSchema = { required: ['numberOfSignatures', 'mandatoryKeys', 'optionalKeys'], }; -const generateValidTransactionEncodings = () => { - const input = { - validBaseTransaction: { - object: { - moduleID: 20, - assetID: 1, - nonce: 1570179673932370, - fee: 3156364651, - senderPublicKey: Buffer.from( - '8f057d088a585d938c20d63e430a068d4cea384e588aa0b758c68fca21644dbc', - 'hex', - ), - asset: Buffer.from('f214d75bbc4b2ea89e433f3a45af803725416ec3', 'hex'), - signatures: [ - Buffer.from( - '204514eb1152355799ece36d17037e5feb4871472c60763bdafe67eb6a38bec632a8e2e62f84a32cf764342a4708a65fbad194e37feec03940f0ff84d3df2a05', - 'hex', - ), - Buffer.from( - '0b6730e5898ca56fe0dc1c73de9363f6fc8b335592ef10725a8463bff101a4943e60311f0b1a439a2c9e02cca1379b80a822f4ec48cf212bff1f1c757e92ec02', - 'hex', - ), - ], - }, - schema: baseTransactionSchema, - }, - validVoteAsset: { - object: { - votes: [ - { - delegateAddress: Buffer.from('cd32c73e9851c7137980063b8af64aa5a31651f8', 'hex'), - amount: -12000000000, - }, - { - delegateAddress: Buffer.from('9d86ad24a3f030e5522b6598115bb4d70c1692c9', 'hex'), - amount: 456000000000, - }, - ], - }, - schema: voteAssetSchema, - }, - validMultiSigAsset: { - object: { - numberOfSignatures: 2, - mandatoryKeys: [ - Buffer.from('07d6389be6e2109613699c02e78253148989515c3867e4f490eafd004a95b2b4', 'hex'), - Buffer.from('3e754d00815b6b248a981520afbaf913153a26d25e2d5283964779c65ceee7e8', 'hex'), - ], - optionalKeys: [ - Buffer.from('c8b8fbe474a2b63ccb9744a409569b0a465ee1803f80435aec1c5e7fc2d4ee18', 'hex'), - Buffer.from('6115424fec0ce9c3bac5a81b5c782827d1f956fb95f1ccfa36c566d04e4d7267', 'hex'), - ], - }, - schema: multisigAssetSchema, +const validBaseTransaction = { + moduleID: 20, + assetID: 1, + nonce: '1570179673932370', + fee: '3156364651', + senderPublicKey: Buffer.from( + '8f057d088a585d938c20d63e430a068d4cea384e588aa0b758c68fca21644dbc', + 'hex', + ), + asset: Buffer.from('f214d75bbc4b2ea89e433f3a45af803725416ec3', 'hex'), + signatures: [ + Buffer.from( + '204514eb1152355799ece36d17037e5feb4871472c60763bdafe67eb6a38bec632a8e2e62f84a32cf764342a4708a65fbad194e37feec03940f0ff84d3df2a05', + 'hex', + ), + Buffer.from( + '0b6730e5898ca56fe0dc1c73de9363f6fc8b335592ef10725a8463bff101a4943e60311f0b1a439a2c9e02cca1379b80a822f4ec48cf212bff1f1c757e92ec02', + 'hex', + ), + ], +}; + +const validVoteAsset = { + votes: [ + { + delegateAddress: Buffer.from('cd32c73e9851c7137980063b8af64aa5a31651f8', 'hex'), + amount: '-12000000000', }, - validMultiSigAssetWithEmpty: { - object: { - numberOfSignatures: 2, - mandatoryKeys: [ - Buffer.from('c8b8fbe474a2b63ccb9744a409569b0a465ee1803f80435aec1c5e7fc2d4ee18', 'hex'), - Buffer.from('6115424fec0ce9c3bac5a81b5c782827d1f956fb95f1ccfa36c566d04e4d7267', 'hex'), - ], - optionalKeys: [], - }, - schema: multisigAssetSchema, + { + delegateAddress: Buffer.from('9d86ad24a3f030e5522b6598115bb4d70c1692c9', 'hex'), + amount: '456000000000', }, - }; + ], +}; - const validBaseTransactionEncoded = BaseTransaction.encode( - input.validBaseTransaction.object, - ).finish(); - const validVoteTransactionEncoded = VoteTransaction.encode(input.validVoteAsset.object).finish(); - const validMultisigTransactionEncoded = MultisigTransaction.encode( - input.validMultiSigAsset.object, - ).finish(); - const validMultisigTransactionWithEmptyEncoded = MultisigTransaction.encode( - input.validMultiSigAssetWithEmpty.object, - ).finish(); +const validMultiSigAsset = { + numberOfSignatures: 2, + mandatoryKeys: [ + Buffer.from('07d6389be6e2109613699c02e78253148989515c3867e4f490eafd004a95b2b4', 'hex'), + Buffer.from('3e754d00815b6b248a981520afbaf913153a26d25e2d5283964779c65ceee7e8', 'hex'), + ], + optionalKeys: [ + Buffer.from('c8b8fbe474a2b63ccb9744a409569b0a465ee1803f80435aec1c5e7fc2d4ee18', 'hex'), + Buffer.from('6115424fec0ce9c3bac5a81b5c782827d1f956fb95f1ccfa36c566d04e4d7267', 'hex'), + ], +}; - return [ +const validMultiSigAssetWithEmpty = { + numberOfSignatures: 2, + mandatoryKeys: [ + Buffer.from('c8b8fbe474a2b63ccb9744a409569b0a465ee1803f80435aec1c5e7fc2d4ee18', 'hex'), + Buffer.from('6115424fec0ce9c3bac5a81b5c782827d1f956fb95f1ccfa36c566d04e4d7267', 'hex'), + ], + optionalKeys: [], +}; + +const validBaseTransactionEncoded = BaseTransaction.encode(validBaseTransaction).finish(); +const validVoteTransactionEncoded = VoteTransaction.encode(validVoteAsset).finish(); +const validMultisigTransactionEncoded = MultisigTransaction.encode(validMultiSigAsset).finish(); +const validMultisigTransactionWithEmptyEncoded = MultisigTransaction.encode( + validMultiSigAssetWithEmpty, +).finish(); + +module.exports = { + validTransactionEncodingsTestCases: [ { description: 'Encoding of valid base transaction', - input: input.validBaseTransaction, - output: { value: validBaseTransactionEncoded.toString('hex') }, + input: { object: validBaseTransaction, schema: baseTransactionSchema }, + output: { value: validBaseTransactionEncoded }, }, { description: 'Encoding of valid vote asset', - input: input.validVoteAsset, - output: { value: validVoteTransactionEncoded.toString('hex') }, + input: { object: validVoteAsset, schema: voteAssetSchema }, + output: { value: validVoteTransactionEncoded }, }, { description: 'Encoding of valid multisignature asset', - input: input.validMultiSigAsset, - output: { value: validMultisigTransactionEncoded.toString('hex') }, + input: { object: validMultiSigAsset, schema: multisigAssetSchema }, + output: { value: validMultisigTransactionEncoded }, }, { description: 'Encoding of valid multisignature asset with empty array', - input: input.validMultiSigAssetWithEmpty, - output: { value: validMultisigTransactionWithEmptyEncoded.toString('hex') }, + input: { object: validMultiSigAssetWithEmpty, schema: multisigAssetSchema }, + output: { value: validMultisigTransactionWithEmptyEncoded }, }, - ]; -}; + ], -module.exports = generateValidTransactionEncodings; + validTransactionDecodingsTestCases: [ + { + description: 'Decoding of valid base transaction', + input: { value: validBaseTransactionEncoded, schema: baseTransactionSchema }, + output: { object: validBaseTransaction }, + }, + { + description: 'Decoding of valid vote asset', + input: { value: validVoteTransactionEncoded, schema: voteAssetSchema }, + output: { object: validVoteAsset }, + }, + { + description: 'Decoding of valid multisignature asset', + input: { value: validMultisigTransactionEncoded, schema: multisigAssetSchema }, + output: { object: validMultiSigAsset }, + }, + { + description: 'Decoding of valid multisignature asset with empty array', + input: { value: validMultisigTransactionWithEmptyEncoded, schema: multisigAssetSchema }, + output: { object: validMultiSigAssetWithEmpty }, + }, + ], +}; diff --git a/protocol-specs/generators/multisignature_registration/index.js b/protocol-specs/generators/multisignature_registration/index.js index 588c783a0b5..40251c5af41 100644 --- a/protocol-specs/generators/multisignature_registration/index.js +++ b/protocol-specs/generators/multisignature_registration/index.js @@ -92,9 +92,9 @@ const accounts = { const outputHexAccount = account => ({ ...account, - privateKey: account.privateKey.toString('hex'), - publicKey: account.publicKey.toString('hex'), - address: account.address.toString('hex'), + privateKey: account.privateKey, + publicKey: account.publicKey, + address: account.address, }); const multisigRegAsset = { @@ -196,7 +196,7 @@ const generateValidMultisignatureRegistrationTransaction = () => { description: 'Both mandatory and optional member group', input: { account: outputHexAccount(accounts.targetAccount), - networkIdentifier: networkIdentifier.toString('hex'), + networkIdentifier, members: { mandatoryOne: outputHexAccount(accounts.mandatoryOne), mandatoryTwo: outputHexAccount(accounts.mandatoryTwo), @@ -205,7 +205,7 @@ const generateValidMultisignatureRegistrationTransaction = () => { }, }, output: { - transaction: encodedTx.toString('hex'), + transaction: encodedTx, }, }; }; @@ -264,7 +264,7 @@ const generateValidMultisignatureRegistrationSenderIsMemberTransaction = () => { description: 'Sender is a member of the group', input: { account: outputHexAccount(accounts.targetAccount), - networkIdentifier: networkIdentifier.toString('hex'), + networkIdentifier, members: { targetAccount: outputHexAccount(accounts.targetAccount), mandatoryOne: outputHexAccount(accounts.mandatoryOne), @@ -274,7 +274,7 @@ const generateValidMultisignatureRegistrationSenderIsMemberTransaction = () => { }, }, output: { - transaction: encodedTx.toString('hex'), + transaction: encodedTx, }, }; }; @@ -324,14 +324,14 @@ const generateValidMultisignatureRegistrationOnlyOptionalMembersTransaction = () description: 'Only optional members', input: { account: outputHexAccount(accounts.targetAccount), - networkIdentifier: networkIdentifier.toString('hex'), + networkIdentifier, members: { optionalOne: outputHexAccount(accounts.optionalOne), optionalTwo: outputHexAccount(accounts.optionalTwo), }, }, output: { - transaction: encodedTx.toString('hex'), + transaction: encodedTx, }, }; }; @@ -381,14 +381,14 @@ const generateValidMultisignatureRegistrationOnlyMandatoryMembersTransaction = ( description: 'Only mandatory members', input: { account: outputHexAccount(accounts.targetAccount), - networkIdentifier: networkIdentifier.toString('hex'), + networkIdentifier, members: { mandatoryOne: outputHexAccount(accounts.mandatoryOne), mandatoryTwo: outputHexAccount(accounts.mandatoryTwo), }, }, output: { - transaction: encodedTx.toString('hex'), + transaction: encodedTx, }, }; }; @@ -452,14 +452,14 @@ const generateFormerSecondSignatureTransactioon = () => { description: 'Second signature case', input: { account: outputHexAccount(accounts.targetAccount), - networkIdentifier: networkIdentifier.toString('hex'), + networkIdentifier, members: { mandatoryOne: outputHexAccount(accounts.targetAccount), mandatoryTwo: outputHexAccount(secondSignature), }, }, output: { - transaction: encodedTx.toString('hex'), + transaction: encodedTx, }, }; }; diff --git a/protocol-specs/generators/proof_of_misbehavior_transaction/index.js b/protocol-specs/generators/proof_of_misbehavior_transaction/index.js index bb902b64115..21608899f80 100644 --- a/protocol-specs/generators/proof_of_misbehavior_transaction/index.js +++ b/protocol-specs/generators/proof_of_misbehavior_transaction/index.js @@ -178,9 +178,9 @@ const accounts = { const getHexAccount = account => ({ ...account, - address: account.address.toString('hex'), - publicKey: account.publicKey.toString('hex'), - balance: account.balance.toString(), + address: account.address, + publicKey: account.publicKey, + balance: account.balance, }); const forgerKeyPair = getPrivateAndPublicKeyBytesFromPassphrase(accounts.forger.passphrase); @@ -268,10 +268,10 @@ const generateValidProofOfMisbehaviorTransactionForScenario1 = () => { input: { reportingAccount: getHexAccount(accounts.reporter), targetAccount: getHexAccount(accounts.forger), - networkIdentifier: networkIdentifier.toString('hex'), + networkIdentifier, }, output: { - transaction: encodedTx.toString('hex'), + transaction: encodedTx, }, }; }; @@ -358,10 +358,10 @@ const generateValidProofOfMisbehaviorTransactionForScenario2 = () => { input: { reportingAccount: getHexAccount(accounts.reporter), targetAccount: getHexAccount(accounts.forger), - networkIdentifier: networkIdentifier.toString('hex'), + networkIdentifier, }, output: { - transaction: encodedTx.toString('hex'), + transaction: encodedTx, }, }; }; @@ -448,10 +448,10 @@ const generateValidProofOfMisbehaviorTransactionForScenario3 = () => { input: { reportingAccount: getHexAccount(accounts.reporter), targetAccount: getHexAccount(accounts.forger), - networkIdentifier: networkIdentifier.toString('hex'), + networkIdentifier, }, output: { - transaction: encodedTx.toString('hex'), + transaction: encodedTx, }, }; }; diff --git a/protocol-specs/generators/transaction_merkle_root_for_blocks/index.js b/protocol-specs/generators/transaction_merkle_root_for_blocks/index.js index be7cd06953a..070e7201e81 100644 --- a/protocol-specs/generators/transaction_merkle_root_for_blocks/index.js +++ b/protocol-specs/generators/transaction_merkle_root_for_blocks/index.js @@ -57,7 +57,7 @@ const generateTransactionMerkleRoot = () => transactionIds: ids, }, output: { - transactionMerkleRoot: transactionMerkleRoot.toString('hex'), + transactionMerkleRoot, }, }; }); diff --git a/protocol-specs/generators/transaction_network_id_and_change_order/index.js b/protocol-specs/generators/transaction_network_id_and_change_order/index.js index 381f5bd38d5..0774653dd7a 100644 --- a/protocol-specs/generators/transaction_network_id_and_change_order/index.js +++ b/protocol-specs/generators/transaction_network_id_and_change_order/index.js @@ -137,15 +137,15 @@ const generateValidTransferTransaction = () => { input: { account: { ...accounts[0], - nonce: accounts[0].nonce.toString(), - publicKey: accounts[0].publicKey.toString('hex'), - privateKey: accounts[0].privateKey.toString('hex'), - address: accounts[0].address.toString('hex'), + nonce: accounts[0].nonce, + publicKey: accounts[0].publicKey, + privateKey: accounts[0].privateKey, + address: accounts[0].address, }, - networkIdentifier: networkIdentifier.toString('hex'), + networkIdentifier, }, output: { - transaction: encodedTx.toString('hex'), + transaction: encodedTx, }, }; }; @@ -189,15 +189,15 @@ const generateValidDelegateTransaction = () => { input: { account: { ...accounts[0], - nonce: accounts[0].nonce.toString(), - publicKey: accounts[0].publicKey.toString('hex'), - privateKey: accounts[0].privateKey.toString('hex'), - address: accounts[0].address.toString('hex'), + nonce: accounts[0].nonce, + publicKey: accounts[0].publicKey, + privateKey: accounts[0].privateKey, + address: accounts[0].address, }, - networkIdentifier: networkIdentifier.toString('hex'), + networkIdentifier, }, output: { - transaction: encodedTx.toString('hex'), + transaction: encodedTx, }, }; }; diff --git a/protocol-specs/generators/unlocking_transaction/index.js b/protocol-specs/generators/unlocking_transaction/index.js index 4d362957555..2bc9ab53b4d 100644 --- a/protocol-specs/generators/unlocking_transaction/index.js +++ b/protocol-specs/generators/unlocking_transaction/index.js @@ -346,18 +346,18 @@ const generateValidUpvoteTransaction = () => { input: { account: { ...senderAccount, - address: senderAccount.address.toString('hex'), - publicKey: senderAccount.publicKey.toString('hex'), + address: senderAccount.address, + publicKey: senderAccount.publicKey, }, - networkIdentifier: networkIdentifier.toString('hex'), + networkIdentifier, delegates: delegateAccounts.map(d => ({ ...d, - address: d.address.toString('hex'), - publicKey: d.publicKey.toString('hex'), + address: d.address, + publicKey: d.publicKey, })), }, output: { - transaction: encodedTx.toString('hex'), + transaction: encodedTx, }, }; }; diff --git a/protocol-specs/generators/vote_transaction/index.js b/protocol-specs/generators/vote_transaction/index.js index acbca97ecc1..e84b3e783c8 100644 --- a/protocol-specs/generators/vote_transaction/index.js +++ b/protocol-specs/generators/vote_transaction/index.js @@ -304,18 +304,18 @@ const generateValidUpvoteTransaction = () => { input: { account: { ...senderAccount, - address: senderAccount.address.toString('hex'), - publicKey: senderAccount.publicKey.toString('hex'), + address: senderAccount.address, + publicKey: senderAccount.publicKey, }, - networkIdentifier: networkIdentifier.toString('hex'), + networkIdentifier, delegates: delegateAccounts.map(d => ({ ...d, - address: d.address.toString('hex'), - publicKey: d.publicKey.toString('hex'), + address: d.address, + publicKey: d.publicKey, })), }, output: { - transaction: encodedTx.toString('hex'), + transaction: encodedTx, }, }; }; @@ -387,18 +387,18 @@ const generateValidDownvoteTransaction = () => { input: { account: { ...senderAccount, - address: senderAccount.address.toString('hex'), - publicKey: senderAccount.publicKey.toString('hex'), + address: senderAccount.address, + publicKey: senderAccount.publicKey, }, - networkIdentifier: networkIdentifier.toString('hex'), + networkIdentifier, delegates: delegateAccounts.map(d => ({ ...d, - address: d.address.toString('hex'), - publicKey: d.publicKey.toString('hex'), + address: d.address, + publicKey: d.publicKey, })), }, output: { - transaction: encodedTx.toString('hex'), + transaction: encodedTx, }, }; }; @@ -510,18 +510,18 @@ const generateValidUpvoteAndDownvoteVoteTransaction = () => { input: { account: { ...senderAccount, - address: senderAccount.address.toString('hex'), - publicKey: senderAccount.publicKey.toString('hex'), + address: senderAccount.address, + publicKey: senderAccount.publicKey, }, - networkIdentifier: networkIdentifier.toString('hex'), + networkIdentifier, delegates: delegateAccounts.map(d => ({ ...d, - address: d.address.toString('hex'), - publicKey: d.publicKey.toString('hex'), + address: d.address, + publicKey: d.publicKey, })), }, output: { - transaction: encodedTx.toString('hex'), + transaction: encodedTx, }, }; }; diff --git a/protocol-specs/utils/index.js b/protocol-specs/utils/index.js index c5bf2d2ce66..d2f3bf0e8d1 100644 --- a/protocol-specs/utils/index.js +++ b/protocol-specs/utils/index.js @@ -36,6 +36,28 @@ const getFilesFromDir = (dir, fileTypes) => { return filesToReturn; }; +const replacer = (_, value) => { + if ( + value && + typeof value === 'object' && + value.type && + value.type === 'Buffer' && + value.data && + Array.isArray(value.data) + ) { + return Buffer.from(value.data).toString('hex'); + } + + if (value && typeof value === 'bigint') { + return value.toString(); + } + + return value; +}; + +const jsonStringify = (obj, space) => JSON.stringify(obj, replacer, space); + module.exports = { getFilesFromDir, + jsonStringify, };