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

Commit

Permalink
Refactor base interoperability store and endpoints (#7700)
Browse files Browse the repository at this point in the history
### What was the problem?

This PR resolves #7647 

### How was it solved?

♻️ Move store functions out of BaseInteroperabilityStore

🌱 Add getAllAccounts to ChainAccountStore

🌱 Create BaseInternalMethod and rename BaseInteroperabilityStore->BaseInteroperabilityInternalMethod

🔥 Remove events from endpoints

### How was it tested?

`npm run test`
  • Loading branch information
ishantiw authored Nov 2, 2022
1 parent c03b27a commit 0ca0709
Show file tree
Hide file tree
Showing 67 changed files with 1,480 additions and 1,985 deletions.
25 changes: 25 additions & 0 deletions framework/src/modules/BaseInternalMethod.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
* Copyright © 2022 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 { NamedRegistry } from './named_registry';

export abstract class BaseInternalMethod {
public readonly events: NamedRegistry;
protected readonly stores: NamedRegistry;

public constructor(stores: NamedRegistry, events: NamedRegistry) {
this.stores = stores;
this.events = events;
}
}
9 changes: 2 additions & 7 deletions framework/src/modules/auth/endpoint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,8 @@ import { AuthAccountStore } from './stores/auth_account';
import { NamedRegistry } from '../named_registry';

export class AuthEndpoint extends BaseEndpoint {
public constructor(
_moduleName: string,
stores: NamedRegistry,
offchainStores: NamedRegistry,
events: NamedRegistry,
) {
super(stores, offchainStores, events);
public constructor(_moduleName: string, stores: NamedRegistry, offchainStores: NamedRegistry) {
super(stores, offchainStores);
}

public async getAuthAccount(context: ModuleEndpointContext): Promise<AuthAccountJSON> {
Expand Down
2 changes: 1 addition & 1 deletion framework/src/modules/auth/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ import { RegisterMultisignatureCommand } from './commands/register_multisignatur

export class AuthModule extends BaseModule {
public method = new AuthMethod(this.stores, this.events);
public endpoint = new AuthEndpoint(this.name, this.stores, this.offchainStores, this.events);
public endpoint = new AuthEndpoint(this.name, this.stores, this.offchainStores);
public configSchema = configSchema;
public commands = [new RegisterMultisignatureCommand(this.stores, this.events)];

Expand Down
6 changes: 1 addition & 5 deletions framework/src/modules/base_endpoint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,5 @@ export abstract class BaseEndpoint {
[key: string]: unknown;

// eslint-disable-next-line no-useless-constructor
public constructor(
protected stores: NamedRegistry,
protected offchainStores: NamedRegistry,
protected events: NamedRegistry,
) {}
public constructor(protected stores: NamedRegistry, protected offchainStores: NamedRegistry) {}
}
2 changes: 1 addition & 1 deletion framework/src/modules/dpos_v2/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ import { VoterStore } from './stores/voter';
export class DPoSModule extends BaseModule {
public method = new DPoSMethod(this.stores, this.events);
public configSchema = configSchema;
public endpoint = new DPoSEndpoint(this.stores, this.offchainStores, this.events);
public endpoint = new DPoSEndpoint(this.stores, this.offchainStores);

private readonly _delegateRegistrationCommand = new DelegateRegistrationCommand(
this.stores,
Expand Down
2 changes: 1 addition & 1 deletion framework/src/modules/fee/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import { configSchema } from './schemas';
export class FeeModule extends BaseModule {
public method = new FeeMethod(this.stores, this.events);
public configSchema = configSchema;
public endpoint = new FeeEndpoint(this.stores, this.offchainStores, this.events);
public endpoint = new FeeEndpoint(this.stores, this.offchainStores);
private _tokenMethod!: TokenMethod;
private _minFeePerByte!: number;
private _tokenID!: Buffer;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
import { StoreGetter } from '../base_store';
import { NamedRegistry } from '../named_registry';
import { BaseCCCommand } from './base_cc_command';
import { BaseInteroperabilityStore } from './base_interoperability_store';
import { BaseInteroperabilityInternalMethod } from './base_interoperability_internal_methods';
import { BaseInteroperableMethod } from './base_interoperable_method';

export abstract class BaseInteroperabilityCCCommand extends BaseCCCommand {
Expand All @@ -30,5 +30,7 @@ export abstract class BaseInteroperabilityCCCommand extends BaseCCCommand {
this.interoperableCCMethods = interoperableCCMethods;
}

protected abstract getInteroperabilityStore(context: StoreGetter): BaseInteroperabilityStore;
protected abstract getInteroperabilityInternalMethod(
context: StoreGetter,
): BaseInteroperabilityInternalMethod;
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import { BaseCommand } from '../base_command';
import { ImmutableStoreGetter, StoreGetter } from '../base_store';
import { NamedRegistry } from '../named_registry';
import { BaseCCCommand } from './base_cc_command';
import { BaseInteroperabilityStore } from './base_interoperability_store';
import { BaseInteroperabilityInternalMethod } from './base_interoperability_internal_methods';
import { BaseInteroperableMethod } from './base_interoperable_method';

export abstract class BaseInteroperabilityCommand extends BaseCommand {
Expand All @@ -34,7 +34,7 @@ export abstract class BaseInteroperabilityCommand extends BaseCommand {
this.ccCommands = ccCommands;
}

protected abstract getInteroperabilityStore(
protected abstract getInteroperabilityInternalMethod(
context: StoreGetter | ImmutableStoreGetter,
): BaseInteroperabilityStore;
): BaseInteroperabilityInternalMethod;
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import { BaseEndpoint } from '../base_endpoint';
import { BaseInteroperableMethod } from './base_interoperable_method';
import {
ChainAccountJSON,
ChainValidators,
ChainValidatorsJSON,
ChannelDataJSON,
Inbox,
InboxJSON,
Expand All @@ -26,46 +26,35 @@ import {
} from './types';
import { ModuleEndpointContext } from '../../types';
import { NamedRegistry } from '../named_registry';
import { TerminatedStateAccountJSON } from './stores/terminated_state';
import { TerminatedOutboxAccountJSON } from './stores/terminated_outbox';
import { BaseInteroperabilityStore } from './base_interoperability_store';
import { TerminatedStateAccountJSON, TerminatedStateStore } from './stores/terminated_state';
import { TerminatedOutboxAccountJSON, TerminatedOutboxStore } from './stores/terminated_outbox';
import { chainAccountToJSON } from './utils';
import { ImmutableStoreGetter, StoreGetter } from '../base_store';
import { ChainValidatorsStore } from './stores/chain_validators';
import { ChainAccountStore } from './stores/chain_account';
import { ChannelDataStore } from './stores/channel_data';
import { OwnChainAccountStore } from './stores/own_chain_account';
import { EMPTY_BYTES } from './constants';

export abstract class BaseInteroperabilityEndpoint<
T extends BaseInteroperabilityStore
> extends BaseEndpoint {
export abstract class BaseInteroperabilityEndpoint extends BaseEndpoint {
protected readonly interoperableCCMethods = new Map<string, BaseInteroperableMethod>();
protected abstract getInteroperabilityStore: (context: StoreGetter | ImmutableStoreGetter) => T;

public constructor(
protected stores: NamedRegistry,
protected offchainStores: NamedRegistry,
interoperableCCMethods: Map<string, BaseInteroperableMethod>,
protected events: NamedRegistry,
) {
super(stores, offchainStores, events);
this.interoperableCCMethods = interoperableCCMethods;

public constructor(protected stores: NamedRegistry, protected offchainStores: NamedRegistry) {
super(stores, offchainStores);
}

public async getChainAccount(
context: ModuleEndpointContext,
chainID: Buffer,
): Promise<ChainAccountJSON> {
const interoperabilityStore = this.getInteroperabilityStore(context);
return chainAccountToJSON(await interoperabilityStore.getChainAccount(chainID));
return chainAccountToJSON(await this.stores.get(ChainAccountStore).get(context, chainID));
}

public async getAllChainAccounts(
context: ModuleEndpointContext,
startChainID: Buffer,
): Promise<{ chains: ChainAccountJSON[] }> {
const interoperabilityStore = this.getInteroperabilityStore(context);

const chainAccounts = (
await interoperabilityStore.getAllChainAccounts(startChainID)
await this.stores.get(ChainAccountStore).getAllAccounts(context, startChainID)
).map(chainAccount => chainAccountToJSON(chainAccount));

return { chains: chainAccounts };
Expand All @@ -75,14 +64,9 @@ export abstract class BaseInteroperabilityEndpoint<
context: ModuleEndpointContext,
chainID: Buffer,
): Promise<ChannelDataJSON> {
const interoperabilityStore = this.getInteroperabilityStore(context);

const {
inbox,
messageFeeTokenID,
outbox,
partnerChainOutboxRoot,
} = await interoperabilityStore.getChannel(chainID);
const { inbox, messageFeeTokenID, outbox, partnerChainOutboxRoot } = await this.stores
.get(ChannelDataStore)
.get(context, chainID);

const inboxJSON = this._toBoxJSON(inbox) as InboxJSON;
const outboxJSON = this._toBoxJSON(outbox) as OutboxJSON;
Expand All @@ -96,9 +80,9 @@ export abstract class BaseInteroperabilityEndpoint<
}

public async getOwnChainAccount(context: ModuleEndpointContext): Promise<OwnChainAccountJSON> {
const interoperabilityStore = this.getInteroperabilityStore(context);

const { chainID, name, nonce } = await interoperabilityStore.getOwnChainAccount();
const { chainID, name, nonce } = await this.stores
.get(OwnChainAccountStore)
.get(context, EMPTY_BYTES);

return {
chainID: chainID.toString('hex'),
Expand All @@ -111,13 +95,9 @@ export abstract class BaseInteroperabilityEndpoint<
context: ModuleEndpointContext,
chainID: Buffer,
): Promise<TerminatedStateAccountJSON> {
const interoperabilityStore = this.getInteroperabilityStore(context);

const {
stateRoot,
initialized,
mainchainStateRoot,
} = await interoperabilityStore.getTerminatedStateAccount(chainID);
const { stateRoot, initialized, mainchainStateRoot } = await this.stores
.get(TerminatedStateStore)
.get(context, chainID);

return {
stateRoot: stateRoot.toString('hex'),
Expand All @@ -130,13 +110,9 @@ export abstract class BaseInteroperabilityEndpoint<
context: ModuleEndpointContext,
chainID: Buffer,
): Promise<TerminatedOutboxAccountJSON> {
const interoperabilityStore = this.getInteroperabilityStore(context);

const {
outboxRoot,
outboxSize,
partnerChainInboxSize,
} = await interoperabilityStore.getTerminatedOutboxAccount(chainID);
const { outboxRoot, outboxSize, partnerChainInboxSize } = await this.stores
.get(TerminatedOutboxStore)
.get(context, chainID);

return {
outboxRoot: outboxRoot.toString('hex'),
Expand All @@ -148,7 +124,7 @@ export abstract class BaseInteroperabilityEndpoint<
public async getChainValidators(
context: ModuleEndpointContext,
chainID: Buffer,
): Promise<ChainValidators> {
): Promise<ChainValidatorsJSON> {
const chainAccountStore = this.stores.get(ChainAccountStore);
const chainAccountExists = await chainAccountStore.has(context, chainID);
if (!chainAccountExists) {
Expand All @@ -159,7 +135,13 @@ export abstract class BaseInteroperabilityEndpoint<

const validators = await chainValidatorsStore.get(context, chainID);

return validators;
return {
activeValidators: validators.activeValidators.map(validator => ({
blsKey: validator.blsKey.toString('hex'),
bftWeight: validator.bftWeight.toString(),
})),
certificateThreshold: validators.certificateThreshold.toString(),
};
}

public async isChainIDAvailable(
Expand Down
Loading

0 comments on commit 0ca0709

Please sign in to comment.