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 #7336 from LiskHQ/7334-interop-endpoint-json
Browse files Browse the repository at this point in the history
Return JSON format results from endpoints - Closes #7334
  • Loading branch information
ishantiw authored Jul 27, 2022
2 parents b93207d + a0978cd commit 11aebb6
Show file tree
Hide file tree
Showing 5 changed files with 641 additions and 90 deletions.
128 changes: 108 additions & 20 deletions framework/src/modules/interoperability/mainchain/endpoint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,19 @@

import { BaseEndpoint } from '../../base_endpoint';
import { MainchainInteroperabilityStore } from './store';
import { ImmutableStoreCallback, StoreCallback } from '../types';
import {
ChainAccountJSON,
ChannelDataJSON,
ImmutableStoreCallback,
InboxJSON,
LastCertificateJSON,
MessageFeeTokenIDJSON,
OutboxJSON,
OwnChainAccountJSON,
StoreCallback,
TerminatedOutboxAccountJSON,
TerminatedStateAccountJSON,
} from '../types';
import { BaseInteroperableAPI } from '../base_interoperable_api';
import { ModuleEndpointContext } from '../../../types';

Expand All @@ -26,43 +38,119 @@ export class MainchainInteroperabilityEndpoint extends BaseEndpoint {
this.interoperableCCAPIs = interoperableCCAPIs;
}

public async getChainAccount(context: ModuleEndpointContext, chainID: Buffer) {
public async getChainAccount(
context: ModuleEndpointContext,
chainID: Buffer,
): Promise<ChainAccountJSON> {
const interoperabilityStore = this.getInteroperabilityStore(context.getStore);
const result = await interoperabilityStore.getChainAccount(chainID);

return result;
const {
lastCertificate,
name,
networkID,
status,
} = await interoperabilityStore.getChainAccount(chainID);

const lastCertificateJSON: LastCertificateJSON = {
height: lastCertificate.height,
timestamp: lastCertificate.timestamp,
stateRoot: lastCertificate.stateRoot.toString('hex'),
validatorsHash: lastCertificate.validatorsHash.toString('hex'),
};

return {
lastCertificate: lastCertificateJSON,
name,
status,
networkID: networkID.toString('hex'),
};
}

public async getChannel(context: ModuleEndpointContext, chainID: Buffer) {
public async getChannel(
context: ModuleEndpointContext,
chainID: Buffer,
): Promise<ChannelDataJSON> {
const interoperabilityStore = this.getInteroperabilityStore(context.getStore);

const result = await interoperabilityStore.getChannel(chainID);

return result;
const {
inbox,
messageFeeTokenID,
outbox,
partnerChainOutboxRoot,
} = await interoperabilityStore.getChannel(chainID);

const inboxJSON: InboxJSON = {
appendPath: inbox.appendPath.map(ap => ap.toString('hex')),
root: inbox.root.toString('hex'),
size: inbox.size,
};

const outboxJSON: OutboxJSON = {
appendPath: outbox.appendPath.map(ap => ap.toString('hex')),
root: outbox.root.toString('hex'),
size: outbox.size,
};

const messageFeeTokenIDJSON: MessageFeeTokenIDJSON = {
chainID: messageFeeTokenID.chainID.toString('hex'),
localID: messageFeeTokenID.localID.toString('hex'),
};

return {
messageFeeTokenID: messageFeeTokenIDJSON,
outbox: outboxJSON,
inbox: inboxJSON,
partnerChainOutboxRoot: partnerChainOutboxRoot.toString('hex'),
};
}

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

const result = await interoperabilityStore.getOwnChainAccount();
const { id, name, nonce } = await interoperabilityStore.getOwnChainAccount();

return result;
return {
id: id.toString('hex'),
name,
nonce: nonce.toString(),
};
}

public async getTerminatedStateAccount(context: ModuleEndpointContext, chainID: Buffer) {
public async getTerminatedStateAccount(
context: ModuleEndpointContext,
chainID: Buffer,
): Promise<TerminatedStateAccountJSON> {
const interoperabilityStore = this.getInteroperabilityStore(context.getStore);

const result = await interoperabilityStore.getTerminatedStateAccount(chainID);

return result;
const {
stateRoot,
initialized,
mainchainStateRoot,
} = await interoperabilityStore.getTerminatedStateAccount(chainID);

return {
stateRoot: stateRoot.toString('hex'),
initialized,
mainchainStateRoot: mainchainStateRoot?.toString('hex'),
};
}

public async getTerminatedOutboxAccount(context: ModuleEndpointContext, chainID: Buffer) {
public async getTerminatedOutboxAccount(
context: ModuleEndpointContext,
chainID: Buffer,
): Promise<TerminatedOutboxAccountJSON> {
const interoperabilityStore = this.getInteroperabilityStore(context.getStore);

const result = await interoperabilityStore.getTerminatedOutboxAccount(chainID);

return result;
const {
outboxRoot,
outboxSize,
partnerChainInboxSize,
} = await interoperabilityStore.getTerminatedOutboxAccount(chainID);

return {
outboxRoot: outboxRoot.toString('hex'),
outboxSize,
partnerChainInboxSize,
};
}

protected getInteroperabilityStore(
Expand Down
128 changes: 108 additions & 20 deletions framework/src/modules/interoperability/sidechain/endpoint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,19 @@

import { BaseEndpoint } from '../../base_endpoint';
import { SidechainInteroperabilityStore } from './store';
import { ImmutableStoreCallback, StoreCallback } from '../types';
import {
ChainAccountJSON,
ChannelDataJSON,
ImmutableStoreCallback,
InboxJSON,
LastCertificateJSON,
MessageFeeTokenIDJSON,
OutboxJSON,
OwnChainAccountJSON,
StoreCallback,
TerminatedOutboxAccountJSON,
TerminatedStateAccountJSON,
} from '../types';
import { BaseInteroperableAPI } from '../base_interoperable_api';
import { ModuleEndpointContext } from '../../../types';

Expand All @@ -26,43 +38,119 @@ export class SidechainInteroperabilityEndpoint extends BaseEndpoint {
this.interoperableCCAPIs = interoperableCCAPIs;
}

public async getChainAccount(context: ModuleEndpointContext, chainID: Buffer) {
public async getChainAccount(
context: ModuleEndpointContext,
chainID: Buffer,
): Promise<ChainAccountJSON> {
const interoperabilityStore = this.getInteroperabilityStore(context.getStore);
const result = await interoperabilityStore.getChainAccount(chainID);

return result;
const {
lastCertificate,
name,
networkID,
status,
} = await interoperabilityStore.getChainAccount(chainID);

const lastCertificateJSON: LastCertificateJSON = {
height: lastCertificate.height,
timestamp: lastCertificate.timestamp,
stateRoot: lastCertificate.stateRoot.toString('hex'),
validatorsHash: lastCertificate.validatorsHash.toString('hex'),
};

return {
lastCertificate: lastCertificateJSON,
name,
status,
networkID: networkID.toString('hex'),
};
}

public async getChannel(context: ModuleEndpointContext, chainID: Buffer) {
public async getChannel(
context: ModuleEndpointContext,
chainID: Buffer,
): Promise<ChannelDataJSON> {
const interoperabilityStore = this.getInteroperabilityStore(context.getStore);

const result = await interoperabilityStore.getChannel(chainID);

return result;
const {
inbox,
messageFeeTokenID,
outbox,
partnerChainOutboxRoot,
} = await interoperabilityStore.getChannel(chainID);

const inboxJSON: InboxJSON = {
appendPath: inbox.appendPath.map(ap => ap.toString('hex')),
root: inbox.root.toString('hex'),
size: inbox.size,
};

const outboxJSON: OutboxJSON = {
appendPath: outbox.appendPath.map(ap => ap.toString('hex')),
root: outbox.root.toString('hex'),
size: outbox.size,
};

const messageFeeTokenIDJSON: MessageFeeTokenIDJSON = {
chainID: messageFeeTokenID.chainID.toString('hex'),
localID: messageFeeTokenID.localID.toString('hex'),
};

return {
messageFeeTokenID: messageFeeTokenIDJSON,
outbox: outboxJSON,
inbox: inboxJSON,
partnerChainOutboxRoot: partnerChainOutboxRoot.toString('hex'),
};
}

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

const result = await interoperabilityStore.getOwnChainAccount();
const { id, name, nonce } = await interoperabilityStore.getOwnChainAccount();

return result;
return {
id: id.toString('hex'),
name,
nonce: nonce.toString(),
};
}

public async getTerminatedStateAccount(context: ModuleEndpointContext, chainID: Buffer) {
public async getTerminatedStateAccount(
context: ModuleEndpointContext,
chainID: Buffer,
): Promise<TerminatedStateAccountJSON> {
const interoperabilityStore = this.getInteroperabilityStore(context.getStore);

const result = await interoperabilityStore.getTerminatedStateAccount(chainID);

return result;
const {
stateRoot,
initialized,
mainchainStateRoot,
} = await interoperabilityStore.getTerminatedStateAccount(chainID);

return {
stateRoot: stateRoot.toString('hex'),
initialized,
mainchainStateRoot: mainchainStateRoot?.toString('hex'),
};
}

public async getTerminatedOutboxAccount(context: ModuleEndpointContext, chainID: Buffer) {
public async getTerminatedOutboxAccount(
context: ModuleEndpointContext,
chainID: Buffer,
): Promise<TerminatedOutboxAccountJSON> {
const interoperabilityStore = this.getInteroperabilityStore(context.getStore);

const result = await interoperabilityStore.getTerminatedOutboxAccount(chainID);

return result;
const {
outboxRoot,
outboxSize,
partnerChainInboxSize,
} = await interoperabilityStore.getTerminatedOutboxAccount(chainID);

return {
outboxRoot: outboxRoot.toString('hex'),
outboxSize,
partnerChainInboxSize,
};
}

protected getInteroperabilityStore(
Expand Down
Loading

0 comments on commit 11aebb6

Please sign in to comment.