Skip to content

Commit

Permalink
chore: added unit tests, renamed back previous types.
Browse files Browse the repository at this point in the history
  • Loading branch information
danielmain committed Apr 25, 2024
1 parent 76c1ebb commit d4e8327
Show file tree
Hide file tree
Showing 7 changed files with 133 additions and 34 deletions.
8 changes: 4 additions & 4 deletions packages/core/src/Cardano/types/Governance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,14 +94,14 @@ export type GovernanceAction =
| NewConstitution
| InfoAction;

export enum CoreVote {
export enum Vote {
no = 0,
yes = 1,
abstain = 2
}

export type VotingProcedure = {
vote: CoreVote;
vote: Vote;
anchor: Anchor | null;
};

Expand Down Expand Up @@ -153,15 +153,15 @@ export type StakePoolKeyHashVoter = {
};
};

export type CoreVoter =
export type Voter =
| ConstitutionalCommitteeKeyHashVoter
| ConstitutionalCommitteeScriptHashVoter
| DrepKeyHashVoter
| DrepScriptHashVoter
| StakePoolKeyHashVoter;

export type VotingProcedures = Array<{
voter: CoreVoter;
voter: Voter;
votes: Array<{
actionId: GovernanceActionId;
votingProcedure: VotingProcedure;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ export class Voter {
*
* @returns The Core Voter object.
*/
toCore(): Cardano.CoreVoter {
toCore(): Cardano.Voter {
switch (this.#kind) {
case VoterKind.ConstitutionalCommitteeKeyHash:
return {
Expand Down Expand Up @@ -160,7 +160,7 @@ export class Voter {
*
* @param coreVoter The core Voter object.
*/
static fromCore(coreVoter: Cardano.CoreVoter): Voter {
static fromCore(coreVoter: Cardano.Voter): Voter {
let voter;

switch (coreVoter.__typename) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import * as Cardano from '../../../Cardano';
import { Anchor } from '../../Common/Anchor';
import { CborReader, CborReaderState, CborWriter } from '../../CBOR';
import { CoreVote } from '../../../Cardano';
import { HexBlob } from '@cardano-sdk/util';
import { Vote } from '../../../Cardano';
import { hexToBytes } from '../../../util/misc';

const EMBEDDED_GROUP_SIZE = 2;
Expand All @@ -14,7 +14,7 @@ const EMBEDDED_GROUP_SIZE = 2;
* - an anchor, it links the vote to arbitrary off-chain JSON payload of metadata.
*/
export class VotingProcedure {
#vote: CoreVote;
#vote: Vote;
#anchor: Anchor | undefined;
#originalBytes: HexBlob | undefined = undefined;

Expand All @@ -24,7 +24,7 @@ export class VotingProcedure {
* @param vote The vote (Yes, No or Abstain).
* @param anchor The vote anchor (or undefined if none).
*/
constructor(vote: CoreVote, anchor?: Anchor) {
constructor(vote: Vote, anchor?: Anchor) {
this.#vote = vote;
this.#anchor = anchor;
}
Expand Down Expand Up @@ -106,7 +106,7 @@ export class VotingProcedure {
*
* @returns The vote.
*/
vote(): CoreVote {
vote(): Vote {
return this.#vote;
}

Expand All @@ -115,7 +115,7 @@ export class VotingProcedure {
*
* @param vote The vote.
*/
setVote(vote: CoreVote) {
setVote(vote: Vote) {
this.#vote = vote;
this.#originalBytes = undefined;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { Hash28ByteBase16 } from '@cardano-sdk/crypto';
import { HexBlob } from '@cardano-sdk/util';
import { Voter } from '../../../../src/Serialization';

const testVoter = (voterType: string, cbor: HexBlob, core: Cardano.CoreVoter) => {
const testVoter = (voterType: string, cbor: HexBlob, core: Cardano.Voter) => {
describe(`Voter ${voterType}`, () => {
it('can encode Voter to CBOR', () => {
const voter = Voter.fromCore(core);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@ const testVotingProcedure = (procedureType: string, cbor: HexBlob, core: Cardano
};

// Test data used in the following tests was generated with the cardano-serialization-lib
testVotingProcedure('vote no with null anchor', HexBlob('8200f6'), { anchor: null, vote: Cardano.CoreVote.no });
testVotingProcedure('vote yes with null anchor', HexBlob('8201f6'), { anchor: null, vote: Cardano.CoreVote.yes });
testVotingProcedure('vote no with null anchor', HexBlob('8200f6'), { anchor: null, vote: Cardano.Vote.no });
testVotingProcedure('vote yes with null anchor', HexBlob('8201f6'), { anchor: null, vote: Cardano.Vote.yes });
testVotingProcedure('vote abstain with null anchor', HexBlob('8202f6'), {
anchor: null,
vote: Cardano.CoreVote.abstain
vote: Cardano.Vote.abstain
});
testVotingProcedure(
'vote no with anchor',
Expand All @@ -37,7 +37,7 @@ testVotingProcedure(
dataHash: Crypto.Hash32ByteBase16('0000000000000000000000000000000000000000000000000000000000000000'),
url: 'https://www.someurl.io'
},
vote: Cardano.CoreVote.no
vote: Cardano.Vote.no
}
);
testVotingProcedure(
Expand All @@ -50,7 +50,7 @@ testVotingProcedure(
dataHash: Crypto.Hash32ByteBase16('0000000000000000000000000000000000000000000000000000000000000000'),
url: 'https://www.someurl.io'
},
vote: Cardano.CoreVote.yes
vote: Cardano.Vote.yes
}
);
testVotingProcedure(
Expand All @@ -63,6 +63,6 @@ testVotingProcedure(
dataHash: Crypto.Hash32ByteBase16('0000000000000000000000000000000000000000000000000000000000000000'),
url: 'https://www.someurl.io'
},
vote: Cardano.CoreVote.abstain
vote: Cardano.Vote.abstain
}
);
32 changes: 16 additions & 16 deletions packages/hardware-ledger/src/transformers/votingProcedures.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { Cardano } from '@cardano-sdk/core';
import {
CommitteeKeyHashVoter,
CommitteeScriptHashVoter,
Expand All @@ -10,43 +11,42 @@ import {
VoterType,
VoterVotes
} from '@cardano-foundation/ledgerjs-hw-app-cardano/dist/types/public';
import { CoreVote, CoreVoter, VotingProcedures } from '@cardano-sdk/core/dist/cjs/Cardano';

/**
* Maps a CoreVoter to a Voter from the LedgerJS public types.
* Maps a Voter to a Voter from the LedgerJS public types.
*
* @param {CoreVoter} coreVoter - The voter object defined in Core types.
* @param {Cardano.Voter} voter - The voter object defined in Core types.
* @returns {Voter} Corresponding Voter object for use with LedgerJS.
*/
export const mapCoreVoterToVoter = (coreVoter: CoreVoter): Voter => {
switch (coreVoter.__typename) {
export const mapVoterToLedgerVoter = (voter: Cardano.Voter): Voter => {
switch (voter.__typename) {
case 'ccHotKeyHash':
return {
keyHashHex: coreVoter.credential.hash,
keyHashHex: voter.credential.hash,
type: VoterType.COMMITTEE_KEY_HASH
} as CommitteeKeyHashVoter;

case 'ccHotScriptHash':
return {
scriptHashHex: coreVoter.credential.hash,
scriptHashHex: voter.credential.hash,
type: VoterType.COMMITTEE_SCRIPT_HASH
} as CommitteeScriptHashVoter;

case 'dRepKeyHash':
return {
keyHashHex: coreVoter.credential.hash,
keyHashHex: voter.credential.hash,
type: VoterType.DREP_KEY_HASH
} as DRepKeyHashVoter;

case 'dRepScriptHash':
return {
scriptHashHex: coreVoter.credential.hash,
scriptHashHex: voter.credential.hash,
type: VoterType.DREP_SCRIPT_HASH
} as DRepScriptHashVoter;

case 'stakePoolKeyHash':
return {
keyHashHex: coreVoter.credential.hash,
keyHashHex: voter.credential.hash,
type: VoterType.STAKE_POOL_KEY_HASH
} as StakePoolKeyHashVoter;

Expand All @@ -56,12 +56,12 @@ export const mapCoreVoterToVoter = (coreVoter: CoreVoter): Voter => {
};

/**
* Maps CoreVote to a LedgerJS VoteOption.
* Maps Vote to a LedgerJS VoteOption.
*
* @param {CoreVote} vote - The vote integer representing a voting decision.
* @param {Cardano.Vote} vote - The vote integer representing a voting decision.
* @returns {VoteOption} The corresponding LedgerJS VoteOption.
*/
export const mapVoteOption = (vote: CoreVote): VoteOption => {
export const mapVoteOption = (vote: Cardano.Vote): VoteOption => {
// Implement this based on how the raw data translates to VoteOption
switch (vote) {
case 0:
Expand All @@ -71,7 +71,7 @@ export const mapVoteOption = (vote: CoreVote): VoteOption => {
case 2:
return VoteOption.ABSTAIN;
default:
return VoteOption.NO;
throw new Error('Unsupported vote type');
}
};

Expand All @@ -84,14 +84,14 @@ export const mapVoteOption = (vote: CoreVote): VoteOption => {
* @param {VotingProcedures | undefined} votingProcedures - Array of voting procedures from Core.
* @returns {VoterVotes[] | null} Array of LedgerJS-compatible voting records or null if input is undefined.
*/
export const mapVotingProcedures = (votingProcedures: VotingProcedures | undefined): VoterVotes[] | null => {
export const mapVotingProcedures = (votingProcedures: Cardano.VotingProcedures | undefined): VoterVotes[] | null => {
if (!votingProcedures) {
return null;
}

return votingProcedures.map(
(procedure): VoterVotes => ({
voter: mapCoreVoterToVoter(procedure.voter),
voter: mapVoterToLedgerVoter(procedure.voter),
votes: procedure.votes.map(
(vote): Vote => ({
govActionId: {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
import * as Crypto from '@cardano-sdk/crypto';
import { Cardano } from '@cardano-sdk/core';
import { VoteOption, VoterType, VoterVotes } from '@cardano-foundation/ledgerjs-hw-app-cardano/dist/types/public';
import { mapVoteOption, mapVoterToLedgerVoter, mapVotingProcedures } from '../../src/transformers/votingProcedures';

describe('mapVoterToLedgerVoter', () => {
it('maps a ConstitutionalCommitteeKeyHashVoter correctly', () => {
const coreVoter = {
__typename: 'ccHotKeyHash',
credential: {
hash: 'somehash',
type: Cardano.CredentialType.KeyHash
}
};
const expected = {
keyHashHex: 'somehash',
type: VoterType.COMMITTEE_KEY_HASH
};
expect(mapVoterToLedgerVoter(coreVoter as Cardano.Voter)).toEqual(expected);
});
});

describe('mapVoteOption', () => {
it('maps the vote option NO correctly', () => {
expect(mapVoteOption(0)).toEqual(VoteOption.NO);
});

it('maps the vote option YES correctly', () => {
expect(mapVoteOption(1)).toEqual(VoteOption.YES);
});

it('maps the vote option ABSTAIN correctly', () => {
expect(mapVoteOption(2)).toEqual(VoteOption.ABSTAIN);
});

it('throws on invalid vote options', () => {
expect(() => mapVoteOption(3)).toThrow('Unsupported vote type');
});
});

const toHash32ByteBase16 = (hash: string): Crypto.Hash32ByteBase16 => hash as unknown as Crypto.Hash32ByteBase16;
const toHash28ByteBase16 = (hash: string): Crypto.Hash28ByteBase16 => hash as unknown as Crypto.Hash28ByteBase16;
const toTransactionId = (id: string): Cardano.TransactionId => id as unknown as Cardano.TransactionId;

describe('mapVotingProcedures', () => {
it('maps voting procedures correctly', () => {
const votingProcedures: Cardano.VotingProcedures = [
{
voter: {
__typename: Cardano.VoterType.ccHotKeyHash,
credential: {
hash: toHash28ByteBase16('keyhash'),
type: Cardano.CredentialType.KeyHash
}
},
votes: [
{
actionId: {
actionIndex: 1,
id: toTransactionId('actionId')
},
votingProcedure: {
anchor: {
dataHash: toHash32ByteBase16('datahash'),
url: 'http://example.com'
},
vote: 1
}
}
]
}
];

const voterVotes: VoterVotes[] = [
{
voter: {
keyHashHex: 'keyhash',
type: VoterType.COMMITTEE_KEY_HASH
},
votes: [
{
govActionId: {
govActionIndex: 1,
txHashHex: 'actionId'
},
votingProcedure: {
anchor: {
hashHex: 'datahash',
url: 'http://example.com'
},
vote: VoteOption.YES
}
}
]
}
];
expect(mapVotingProcedures(votingProcedures)).toEqual(voterVotes);
});
});

0 comments on commit d4e8327

Please sign in to comment.