Skip to content

Commit

Permalink
Merge pull request #5516 from LiskHQ/5497-add_getschema_action_on_fra…
Browse files Browse the repository at this point in the history
…mework

Add getSchema action on framework - Closes #5497
  • Loading branch information
shuse2 committed Jul 3, 2020
2 parents a9041fa + ebcc789 commit 074bc8f
Show file tree
Hide file tree
Showing 5 changed files with 122 additions and 1 deletion.
4 changes: 4 additions & 0 deletions elements/lisk-chain/src/chain.ts
Expand Up @@ -107,6 +107,7 @@ export class Chain {
readonly calculateReward: (height: number) => bigint;
readonly calculateSupply: (height: number) => bigint;
};
public readonly accountSchema: object;

private _lastBlock: Block;
private readonly blocksVerify: BlocksVerify;
Expand Down Expand Up @@ -161,6 +162,9 @@ export class Chain {
},
},
};

this.accountSchema = accountSchema;

codec.addSchema(accountSchema);
codec.addSchema(stateDiffSchema);
this._defaultAccountAsset = accountAsset.default;
Expand Down
3 changes: 3 additions & 0 deletions framework/src/application/application.ts
Expand Up @@ -516,6 +516,9 @@ export class Application {
action.params as { ids: readonly string[] },
),
},
getSchema: {
handler: () => this._node.actions.getSchema(),
},
},
{ skipInternalEvents: true },
);
Expand Down
27 changes: 27 additions & 0 deletions framework/src/application/node/node.ts
Expand Up @@ -16,6 +16,7 @@ import {
Chain,
events as chainEvents,
Block,
blockSchema,
Account,
} from '@liskhq/lisk-chain';
import { Dpos, constants as dposConstants } from '@liskhq/lisk-dpos';
Expand All @@ -29,6 +30,7 @@ import {
} from '@liskhq/lisk-transaction-pool';
import { BaseTransaction } from '@liskhq/lisk-transactions';
import { KVStore, NotFoundError } from '@liskhq/lisk-db';
import { Schema } from '@liskhq/lisk-codec';
import { Sequence } from './utils/sequence';
import { Forger, RegisteredDelegate } from './forger';
import {
Expand Down Expand Up @@ -115,6 +117,10 @@ interface NodeStatus {
readonly chainMaxHeightFinalized: number;
}

interface RegisteredSchemas {
[key: string]: Schema;
}

export class Node {
private readonly _channel: InMemoryChannel;
private readonly _options: Options;
Expand Down Expand Up @@ -474,6 +480,16 @@ export class Node {
params.data,
params.peerId,
),
getSchema: () => ({
account: this._chain.accountSchema,
blockHeader: blockSchema,
blockHeadersAssets: {
0: BlockProcessorV0.schema,
2: BlockProcessorV2.schema,
},
baseTransaction: BaseTransaction.BASE_SCHEMA,
transactionsAssets: this._getRegisteredTransactionSchemas(),
}),
};
}

Expand Down Expand Up @@ -768,4 +784,15 @@ export class Node {
private _unsubscribeToEvents(): void {
this._bft.removeAllListeners(EVENT_BFT_BLOCK_FINALIZED);
}
private _getRegisteredTransactionSchemas(): RegisteredSchemas {
const registredTransactions: RegisteredSchemas = {};

for (const aTransactionSchema of Object.entries(
this._options.registeredTransactions,
)) {
registredTransactions[aTransactionSchema[0]] = aTransactionSchema[1]
.ASSET_SCHEMA as Schema;
}
return registredTransactions;
}
}
6 changes: 5 additions & 1 deletion framework/src/plugins/base_plugin.ts
Expand Up @@ -34,6 +34,7 @@ export interface InstantiablePlugin<T, U = object> {

export abstract class BasePlugin {
public readonly options: object;
public schemas!: object;

protected constructor(options: object) {
this.options = options;
Expand All @@ -54,6 +55,9 @@ export abstract class BasePlugin {
}
public abstract get events(): EventsArray;
public abstract get actions(): ActionsDefinition;
public abstract async load(channel: BaseChannel): Promise<void>;

public async load(channel: BaseChannel): Promise<void> {
this.schemas = await channel.invoke('app:getSchema');
}
public abstract async unload(): Promise<void>;
}
83 changes: 83 additions & 0 deletions framework/test/functional/specs/actions/application.spec.ts
@@ -0,0 +1,83 @@
/*
* 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 { blockSchema, baseAccountSchema } from '@liskhq/lisk-chain';

import {
TransferTransaction,
DelegateTransaction,
VoteTransaction,
UnlockTransaction,
MultisignatureTransaction,
ProofOfMisbehaviorTransaction,
BaseTransaction,
} from '@liskhq/lisk-transactions';
import { accountAssetSchema } from '../../../../src/application/node/account';

import { BlockProcessorV0 } from '../../../../src/application/node/block_processor_v0';
import { BlockProcessorV2 } from '../../../../src/application/node/block_processor_v2';

import { createApplication, closeApplication } from '../../utils/application';

import { Application } from '../../../../src';

describe('Application related actions', () => {
let app: Application;

beforeAll(async () => {
app = await createApplication('actions-transactions');
});

afterAll(async () => {
await closeApplication(app);
});

describe('getSchema', () => {
it('should return schemas used to encode objects in framework', async () => {
const frameworkSchemas = await app['_channel'].invoke('app:getSchema');

const accountSchema = {
...baseAccountSchema,
properties: {
...baseAccountSchema.properties,
asset: {
...baseAccountSchema.properties.asset,
properties: accountAssetSchema,
},
},
};

const expectedFrameworkSchemas = {
account: accountSchema,
blockHeader: blockSchema,
blockHeadersAssets: {
0: BlockProcessorV0.schema,
2: BlockProcessorV2.schema,
},
baseTransaction: BaseTransaction.BASE_SCHEMA,
transactionsAssets: {
[TransferTransaction.TYPE]: TransferTransaction.ASSET_SCHEMA,
[DelegateTransaction.TYPE]: DelegateTransaction.ASSET_SCHEMA,
[VoteTransaction.TYPE]: VoteTransaction.ASSET_SCHEMA,
[UnlockTransaction.TYPE]: UnlockTransaction.ASSET_SCHEMA,
[MultisignatureTransaction.TYPE]:
MultisignatureTransaction.ASSET_SCHEMA,
[ProofOfMisbehaviorTransaction.TYPE]:
ProofOfMisbehaviorTransaction.ASSET_SCHEMA,
},
};

expect(frameworkSchemas).toEqual(expectedFrameworkSchemas);
});
});
});

0 comments on commit 074bc8f

Please sign in to comment.