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

Commit

Permalink
Merge pull request #6831 from LiskHQ/6631-update-lisk-transactions
Browse files Browse the repository at this point in the history
Rename properties on lisk-transactions - Closes #6631 and #6632
  • Loading branch information
shuse2 authored Oct 15, 2021
2 parents ca95690 + 81ab4b6 commit 1e6a84b
Show file tree
Hide file tree
Showing 23 changed files with 428 additions and 909 deletions.
52 changes: 0 additions & 52 deletions elements/lisk-api-client/src/account.ts

This file was deleted.

9 changes: 1 addition & 8 deletions elements/lisk-api-client/src/api_client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
*/
import { EventCallback, Channel, RegisteredSchemas, NodeInfo } from './types';
import { Node } from './node';
import { Account } from './account';
import { Block } from './block';
import { Transaction } from './transaction';

Expand All @@ -23,7 +22,6 @@ export class APIClient {
private _schemas!: RegisteredSchemas;
private _nodeInfo!: NodeInfo;
private _node!: Node;
private _account!: Account;
private _block!: Block;
private _transaction!: Transaction;

Expand All @@ -32,9 +30,8 @@ export class APIClient {
}

public async init(): Promise<void> {
this._schemas = await this._channel.invoke<RegisteredSchemas>('app:getSchema');
this._schemas = await this._channel.invoke<RegisteredSchemas>('app_getSchema');
this._node = new Node(this._channel);
this._account = new Account(this._channel, this._schemas);
this._block = new Block(this._channel, this._schemas);
this._nodeInfo = await this._node.getNodeInfo();
this._transaction = new Transaction(this._channel, this._schemas, this._nodeInfo);
Expand Down Expand Up @@ -63,10 +60,6 @@ export class APIClient {
return this._node;
}

public get account(): Account {
return this._account;
}

public get block(): Block {
return this._block;
}
Expand Down
48 changes: 20 additions & 28 deletions elements/lisk-api-client/src/block.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
*/
import { codec } from '@liskhq/lisk-codec';
import { Block as BlockType, Channel, RegisteredSchemas } from './types';
import { decodeBlock, encodeBlock, getTransactionAssetSchema } from './codec';
import { decodeBlock, encodeBlock, getTransactionParamsSchema } from './codec';

export class Block {
private readonly _channel: Channel;
Expand All @@ -27,22 +27,23 @@ export class Block {

public async get(id: Buffer | string): Promise<Record<string, unknown>> {
const idString: string = Buffer.isBuffer(id) ? id.toString('hex') : id;
const blockHex = await this._channel.invoke<string>('app:getBlockByID', {
const blockHex = await this._channel.invoke<string>('app_getBlockByID', {
id: idString,
});
const blockBytes = Buffer.from(blockHex, 'hex');
return decodeBlock(blockBytes, this._schemas);
}

public async getByHeight(height: number): Promise<Record<string, unknown>> {
const blockHex = await this._channel.invoke<string>('app:getBlockByHeight', { height });
const blockHex = await this._channel.invoke<string>('app_getBlockByHeight', { height });
const blockBytes = Buffer.from(blockHex, 'hex');
return decodeBlock(blockBytes, this._schemas);
}

public encode(input: {
header: Record<string, unknown>;
payload: Record<string, unknown>[];
assets: string[];
}): Buffer {
return encodeBlock(input, this._schemas);
}
Expand All @@ -57,8 +58,9 @@ export class Block {
): {
header: Record<string, unknown>;
payload: Record<string, unknown>[];
assets: string[];
} {
const { asset, ...headerRoot } = block.header;
const { ...headerRoot } = block.header;

// We need to do this as our schemas do not include the ID. Keep this.
const tmpBlockId = headerRoot.id;
Expand All @@ -67,49 +69,44 @@ export class Block {
// decode header
const header = {
...codec.toJSON(this._schemas.blockHeader, headerRoot),
asset: {},
id: tmpBlockId?.toString('hex'),
};

// decode header's asset
const headerAssetJson = codec.toJSON(
this._schemas.blockHeadersAssets[block.header.version],
asset,
);
header.asset = headerAssetJson;
const assets = block.assets.map(asset => asset.toString('hex'));

const payload: Record<string, unknown>[] = [];

// decode transactions
for (const tx of block.payload) {
const { asset: txAsset, ...txRoot } = tx;
const { params: txParams, ...txRoot } = tx;
// We need to do this as our schemas do not include the ID. Keep this.
const tmpId = txRoot.id;
delete txRoot.id;

const schemaAsset = getTransactionAssetSchema(tx, this._schemas);
const jsonTxAsset = codec.toJSON(schemaAsset, txAsset as object);
const schemaParams = getTransactionParamsSchema(tx, this._schemas);
const jsonTxParams = codec.toJSON(schemaParams, txParams as object);
const jsonTxRoot = codec.toJSON(this._schemas.transaction, txRoot);

const jsonTx = {
...jsonTxRoot,
id: tmpId?.toString('hex'),
asset: jsonTxAsset,
params: jsonTxParams,
};

payload.push(jsonTx);
}

return { header, payload };
return { header, payload, assets };
}

public fromJSON(
block: BlockType<string>,
): {
header: Record<string, unknown>;
assets: Buffer[];
payload: Record<string, unknown>[];
} {
const { asset, ...headerRoot } = block.header;
const { ...headerRoot } = block.header;

// We need to do this as our schemas do not include the ID. Keep this.
const tmpBlockId = headerRoot.id ? Buffer.from(headerRoot.id, 'hex') : Buffer.alloc(0);
Expand All @@ -118,38 +115,33 @@ export class Block {
// decode header
const header = {
...codec.fromJSON(this._schemas.blockHeader, headerRoot),
asset: {},
id: tmpBlockId,
};

// decode header's asset
const headerAssetJson = codec.fromJSON(
this._schemas.blockHeadersAssets[block.header.version],
asset,
);
header.asset = headerAssetJson;
const assets = block.assets.map(asset => Buffer.from(asset, 'hex'));

const payload: Record<string, unknown>[] = [];
// decode transactions
for (const tx of block.payload) {
const { asset: txAsset, ...txRoot } = tx;
const { params: txParams, ...txRoot } = tx;
// We need to do this as our schemas do not include the ID. Keep this.
const tmpId = txRoot.id ? Buffer.from(txRoot.id, 'hex') : Buffer.alloc(0);
delete txRoot.id;

const schemaAsset = getTransactionAssetSchema(tx, this._schemas);
const txAssetObject = codec.fromJSON(schemaAsset, txAsset as object);
const schemaParams = getTransactionParamsSchema(tx, this._schemas);
const txParamsObject = codec.fromJSON(schemaParams, txParams as object);
const txRootObject = codec.fromJSON(this._schemas.transaction, txRoot);

const txObject = {
...txRootObject,
id: tmpId,
asset: txAssetObject,
params: txParamsObject,
};

payload.push(txObject);
}

return { header, payload };
return { header, payload, assets };
}
}
66 changes: 23 additions & 43 deletions elements/lisk-api-client/src/codec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,44 +17,40 @@ import { codec, Schema } from '@liskhq/lisk-codec';
import { hash } from '@liskhq/lisk-cryptography';
import { RegisteredSchemas } from './types';

export const getTransactionAssetSchema = (
export const getTransactionParamsSchema = (
transaction: Record<string, unknown>,
registeredSchema: RegisteredSchemas,
): Schema => {
const txAssetSchema = registeredSchema.transactionsAssets.find(
assetSchema =>
assetSchema.moduleID === transaction.moduleID && assetSchema.assetID === transaction.assetID,
const txParamsSchema = registeredSchema.commands.find(
paramsSchema =>
paramsSchema.moduleID === transaction.moduleID &&
paramsSchema.commandID === transaction.commandID,
);
if (!txAssetSchema) {
if (!txParamsSchema) {
throw new Error(
// eslint-disable-next-line @typescript-eslint/restrict-template-expressions
`ModuleID: ${transaction.moduleID} AssetID: ${transaction.assetID} is not registered.`,
`ModuleID: ${transaction.moduleID} CommandID: ${transaction.commandID} is not registered.`,
);
}
return txAssetSchema.schema;
return txParamsSchema.schema;
};

export const decodeAccount = (
encodedAccount: Buffer,
registeredSchema: RegisteredSchemas,
): Record<string, unknown> => codec.decode(registeredSchema.account, encodedAccount);

export const decodeTransaction = (
encodedTransaction: Buffer,
registeredSchema: RegisteredSchemas,
): Record<string, unknown> => {
const transaction = codec.decode<{
[key: string]: unknown;
asset: Buffer;
params: Buffer;
moduleID: number;
assetID: number;
commandID: number;
}>(registeredSchema.transaction, encodedTransaction);
const assetSchema = getTransactionAssetSchema(transaction, registeredSchema);
const asset = codec.decode(assetSchema, transaction.asset);
const paramsSchema = getTransactionParamsSchema(transaction, registeredSchema);
const params = codec.decode(paramsSchema, transaction.params);
const id = hash(encodedTransaction);
return {
...transaction,
asset,
params,
id,
};
};
Expand All @@ -63,12 +59,12 @@ export const encodeTransaction = (
transaction: Record<string, unknown>,
registeredSchema: RegisteredSchemas,
): Buffer => {
const assetSchema = getTransactionAssetSchema(transaction, registeredSchema);
const encodedAsset = codec.encode(assetSchema, transaction.asset as Record<string, unknown>);
const paramsSchema = getTransactionParamsSchema(transaction, registeredSchema);
const encodedParams = codec.encode(paramsSchema, transaction.params as Record<string, unknown>);

const decodedTransaction = codec.encode(registeredSchema.transaction, {
...transaction,
asset: encodedAsset,
params: encodedParams,
});

return decodedTransaction;
Expand All @@ -78,21 +74,16 @@ export const decodeBlock = (
encodedBlock: Buffer,
registeredSchema: RegisteredSchemas,
): Record<string, unknown> => {
const block = codec.decode<{ header: Buffer; payload: Buffer[] }>(
const block = codec.decode<{ header: Buffer; assets: Buffer[]; payload: Buffer[] }>(
registeredSchema.block,
encodedBlock,
);

const header = codec.decode<{ [key: string]: unknown; version: number; asset: Buffer }>(
const header = codec.decode<{ [key: string]: unknown; version: number }>(
registeredSchema.blockHeader,
block.header,
);
const id = hash(block.header);
const assetSchema = registeredSchema.blockHeadersAssets[header.version];
if (!assetSchema) {
throw new Error(`Block header asset version ${header.version} is not registered.`);
}
const asset = codec.decode(assetSchema, header.asset);
const payload = [];
for (const tx of block.payload) {
payload.push(decodeTransaction(tx, registeredSchema));
Expand All @@ -101,35 +92,24 @@ export const decodeBlock = (
return {
header: {
...header,
asset,
id,
},
assets: block.assets,
payload,
};
};

export const encodeBlock = (
block: { header: Record<string, unknown>; payload: Record<string, unknown>[] },
block: { header: Record<string, unknown>; payload: Record<string, unknown>[]; assets: string[] },
registeredSchema: RegisteredSchemas,
): Buffer => {
const encodedPayload = block.payload.map(p => encodeTransaction(p, registeredSchema));
const assetSchema = registeredSchema.blockHeadersAssets[block.header.version as number];
if (!assetSchema) {
throw new Error(
`Block header asset version ${block.header.version as number} is not registered.`,
);
}
const encodedBlockAsset = codec.encode(
assetSchema,
block.header.asset as Record<string, unknown>,
);
const encodedBlockHeader = codec.encode(registeredSchema.blockHeader, {
...block.header,
asset: encodedBlockAsset,
});
const encodedAssets = block.assets.map(asset => Buffer.from(asset, 'hex'));
const encodedBlockHeader = codec.encode(registeredSchema.blockHeader, block.header);

return codec.encode(registeredSchema.block, {
header: encodedBlockHeader,
payload: encodedPayload,
assets: encodedAssets,
});
};
Loading

0 comments on commit 1e6a84b

Please sign in to comment.