Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(core-api): hasTransactionFinality() on connector API #812

Merged
merged 1 commit into from
Apr 29, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@ import {
ICactusPluginOptions,
} from "@hyperledger/cactus-core-api";

import { PluginRegistry } from "@hyperledger/cactus-core";
import {
PluginRegistry,
consensusHasTransactionFinality,
} from "@hyperledger/cactus-core";

import {
Checks,
Expand Down Expand Up @@ -128,7 +131,11 @@ export class PluginLedgerConnectorStub
> {
return ConsensusAlgorithmFamily.AUTHORITY;
}
public async hasTransactionFinality(): Promise<boolean> {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just copy paste the implementation in those *-plugin-ledger-connectors. But I am not so sure, since it is just a stub, so maybe you would want just return false or do some other things?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's perfectly fine as it is IMO. The stub is just there to help with testing so the expectation is that if someone wants to use it to simulate different consensus algorithm families then they can either sub-class it or override the default behavior some other way.

The only thing I need now is the commits to be squashed into a single one so that there are no in-between states (every commit on the main branch should be valid, passing, tests, etc.
So if you squash and then force push (make sure to have the commit signed off) then I'll smash that approve button right after that. Thank you for the contribution and working with me on hammering out the details!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the help for my first pull request!

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jscode017 About to approve and merge this, but I wanted to give one more piece of advice regarding the mechanics of how PR reviews are done:
If someone reviews the PR (e.g. when I literally click button submit review) from that point on it won't notify me until you hit the 're-request review' button (that little refresh icon next to my name in the top right corner among the reviewers).

This is not an actual issue, but I wanted to explain this anyway because I just saw by chance that you had fixed everything that I was asking for more than a week ago but I only noticed it now (otherwise I would've approved+merged the same day)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry for that, I would hit the 're-request review' next time.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No worries at all, thank you again for the PR!

const currentConsensusAlgorithmFamily = await this.getConsensusAlgorithmFamily();

return consensusHasTransactionFinality(currentConsensusAlgorithmFamily);
}
public async transact(req: unknown): Promise<unknown> {
const fnTag = `${this.className}#transact()`;
Checks.truthy(req, `${fnTag} req`);
Expand Down
18 changes: 18 additions & 0 deletions packages/cactus-core-api/src/main/json/openapi.json
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,24 @@
"org.hyperledger.cactus.consensusalgorithm.PROOF_OF_WORK"
]
},
"ConsensusAlgorithmFamiliesWithTxFinality":{
"type": "string",
"description": "Enumerates a list of consensus algorithm families that provide immediate finality",
"enum": [
"org.hyperledger.cactus.consensusalgorithm.PROOF_OF_AUTHORITY",
"org.hyperledger.cactus.consensusalgorithm.PROOF_OF_STAKE"
]

},
"ConsensusAlgorithmFamiliesWithOutTxFinality":{
"description":"Enumerates a list of consensus algorithm families that do not provide immediate finality",
"type": "string",
"enum": [
"org.hyperledger.cactus.consensusalgorithm.PROOF_OF_WORK"
],
"x-enum-varnames": ["WORK"]

},
"PrimaryKey": {
"type": "string",
"minLength": 1,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,25 @@ export interface CactusNodeMeta {
*/
publicKeyPem: string;
}
/**
* Enumerates a list of consensus algorithm families that do not provide immediate finality
* @export
* @enum {string}
*/
export enum ConsensusAlgorithmFamiliesWithOutTxFinality {
WORK = 'org.hyperledger.cactus.consensusalgorithm.PROOF_OF_WORK'
}

/**
* Enumerates a list of consensus algorithm families that provide immediate finality
* @export
* @enum {string}
*/
export enum ConsensusAlgorithmFamiliesWithTxFinality {
AUTHORITY = 'org.hyperledger.cactus.consensusalgorithm.PROOF_OF_AUTHORITY',
STAKE = 'org.hyperledger.cactus.consensusalgorithm.PROOF_OF_STAKE'
}

/**
* Enumerates a list of consensus algorithm families in existence. Does not intend to be an exhaustive list, just a practical one, meaning that we only include items here that are relevant to Hyperledger Cactus in fulfilling its own duties. This can be extended later as more sophisticated features of Cactus get implemented. This enum is meant to be first and foremest a useful abstraction for achieving practical tasks, not an encyclopedia and therefore we ask of everyone that this to be extended only in ways that serve a practical purpose for the runtime behavior of Cactus or Cactus plugins in general. The bottom line is that we can accept this enum being not 100% accurate as long as it 100% satisfies what it was designed to do.
* @export
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,5 @@ export interface IPluginLedgerConnector<
* @see {ConsensusAlgorithmFamily}
*/
getConsensusAlgorithmFamily(): Promise<ConsensusAlgorithmFamily>;
hasTransactionFinality(): Promise<boolean>;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import {
ConsensusAlgorithmFamily,
ConsensusAlgorithmFamiliesWithTxFinality,
ConsensusAlgorithmFamiliesWithOutTxFinality,
} from "@hyperledger/cactus-core-api";

export function consensusHasTransactionFinality(
consensusAlgorithmFamily: ConsensusAlgorithmFamily,
): boolean {
const isInConsensusAlgorithmFamiliesWithTxFinality = (Object.values(
ConsensusAlgorithmFamiliesWithTxFinality,
) as string[]).includes(consensusAlgorithmFamily.toString());

const isInConsensusAlgorithmFamiliesWithOutTxFinality = (Object.values(
ConsensusAlgorithmFamiliesWithOutTxFinality,
) as string[]).includes(consensusAlgorithmFamily.toString());

const unrecognizedConsensusAlgorithmFamily =
!isInConsensusAlgorithmFamiliesWithTxFinality &&
!isInConsensusAlgorithmFamiliesWithOutTxFinality;

if (unrecognizedConsensusAlgorithmFamily) {
throw new Error(
`Unrecognized consensus algorithm family: ${consensusAlgorithmFamily}`,
);
}
return isInConsensusAlgorithmFamiliesWithTxFinality;
}
2 changes: 2 additions & 0 deletions packages/cactus-core/src/main/typescript/public-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,5 @@ export {
AuthorizationOptionsProvider,
IEndpointAuthzOptionsProviderOptions,
} from "./web-services/authorization-options-provider";

export { consensusHasTransactionFinality } from "./consensus-has-transaction-finality";
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,10 @@ import {
IPluginKeychain,
} from "@hyperledger/cactus-core-api";

import { PluginRegistry } from "@hyperledger/cactus-core";
import {
PluginRegistry,
consensusHasTransactionFinality,
} from "@hyperledger/cactus-core";

import {
Checks,
Expand Down Expand Up @@ -220,7 +223,11 @@ export class PluginLedgerConnectorBesu
> {
return ConsensusAlgorithmFamily.AUTHORITY;
}
public async hasTransactionFinality(): Promise<boolean> {
const currentConsensusAlgorithmFamily = await this.getConsensusAlgorithmFamily();

return consensusHasTransactionFinality(currentConsensusAlgorithmFamily);
}
public async invokeContract(
req: InvokeContractV1Request,
): Promise<InvokeContractV1Response> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {
ICactusPluginOptions,
ConsensusAlgorithmFamily,
} from "@hyperledger/cactus-core-api";

import { consensusHasTransactionFinality } from "@hyperledger/cactus-core";
import {
Checks,
Logger,
Expand Down Expand Up @@ -65,6 +65,11 @@ export class PluginLedgerConnectorCorda
> {
return ConsensusAlgorithmFamily.AUTHORITY;
}
public async hasTransactionFinality(): Promise<boolean> {
const currentConsensusAlgorithmFamily = await this.getConsensusAlgorithmFamily();

return consensusHasTransactionFinality(currentConsensusAlgorithmFamily);
}

public getAspect(): PluginAspect {
return PluginAspect.LEDGER_CONNECTOR;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,10 @@ import {
ICactusPluginOptions,
} from "@hyperledger/cactus-core-api";

import { PluginRegistry } from "@hyperledger/cactus-core";
import {
consensusHasTransactionFinality,
PluginRegistry,
} from "@hyperledger/cactus-core";

import {
Logger,
Expand Down Expand Up @@ -184,7 +187,11 @@ export class PluginLedgerConnectorFabric
> {
return ConsensusAlgorithmFamily.AUTHORITY;
}
public async hasTransactionFinality(): Promise<boolean> {
const currentConsensusAlgorithmFamily = await this.getConsensusAlgorithmFamily();

return consensusHasTransactionFinality(currentConsensusAlgorithmFamily);
}
private async sshExec(
cmd: string,
label: string,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,10 @@ import {
IPluginKeychain,
} from "@hyperledger/cactus-core-api";

import { PluginRegistry } from "@hyperledger/cactus-core";
import {
PluginRegistry,
consensusHasTransactionFinality,
} from "@hyperledger/cactus-core";

import {
Checks,
Expand Down Expand Up @@ -205,7 +208,11 @@ export class PluginLedgerConnectorQuorum
> {
return ConsensusAlgorithmFamily.AUTHORITY;
}
public async hasTransactionFinality(): Promise<boolean> {
const currentConsensusAlgorithmFamily = await this.getConsensusAlgorithmFamily();

return consensusHasTransactionFinality(currentConsensusAlgorithmFamily);
}
public async invokeContract(
req: InvokeContractV1Request,
): Promise<InvokeContractV1Response> {
Expand Down