Skip to content

fix(cardano-services): correct mapping of chain history redeemer purpose #1176

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

Merged
merged 3 commits into from
Apr 30, 2024
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
2 changes: 1 addition & 1 deletion .github/workflows/std.yml
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ jobs:

for target in \
"dev-preview@us-east-1" \
"dev-preprod@us-east-1@v1" \
"dev-preprod@us-east-1@v2" \
"dev-mainnet@us-east-1" \
; do
nix run -L ".#cardano-services.${target}.plan" | tee k8s-plan.diff
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import {
WithCertType,
WithdrawalModel
} from './types';
import { Cardano } from '@cardano-sdk/core';
import { Cardano, NotImplementedError } from '@cardano-sdk/core';
import { Hash32ByteBase16 } from '@cardano-sdk/crypto';
import {
isDelegationCertModel,
Expand Down Expand Up @@ -105,14 +105,29 @@ export const mapWithdrawal = (withdrawalModel: WithdrawalModel): Cardano.Withdra
// Remove this and select the actual redeemer data from `redeemer_data` table.
const stubRedeemerData = Buffer.from('not implemented');

const redeemerPurposeMap: Record<RedeemerModel['purpose'], Cardano.RedeemerPurpose> = {
cert: Cardano.RedeemerPurpose.certificate,
mint: Cardano.RedeemerPurpose.mint,
proposing: Cardano.RedeemerPurpose.propose,
reward: Cardano.RedeemerPurpose.withdrawal,
spend: Cardano.RedeemerPurpose.spend,
voting: Cardano.RedeemerPurpose.vote
};

const mapRedeemerPurpose = (purpose: RedeemerModel['purpose']): Cardano.RedeemerPurpose =>
redeemerPurposeMap[purpose] ||
(() => {
throw new NotImplementedError(`Failed to map redeemer "purpose": ${purpose}`);
})();

export const mapRedeemer = (redeemerModel: RedeemerModel): Cardano.Redeemer => ({
data: stubRedeemerData,
executionUnits: {
memory: Number(redeemerModel.unit_mem),
steps: Number(redeemerModel.unit_steps)
},
index: redeemerModel.index,
purpose: redeemerModel.purpose as Cardano.RedeemerPurpose
purpose: mapRedeemerPurpose(redeemerModel.purpose)
});

export const mapCertificate = (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ export interface WithdrawalModel {

export interface RedeemerModel {
index: number;
purpose: string;
purpose: 'cert' | 'mint' | 'spend' | 'reward' | 'voting' | 'proposing';
Copy link
Contributor

Choose a reason for hiding this comment

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

👌

Probably it is possible to automatically get them from RedeemerModel but this is way better than string

Maybe keysof RedeemerModel["purpose"]

Copy link
Member Author

Choose a reason for hiding this comment

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

Sorry @gytis-ivaskevicius but I don't understand what you're suggesting as the source, this is the RedeemerModel 🤔

Copy link
Member

Choose a reason for hiding this comment

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

@gytis-ivaskevicius next action on you here

Copy link
Contributor

Choose a reason for hiding this comment

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

Realized that what I wrote was invalid, idea was to take purpose type from elsewhere to avoid hardcoding. Not sure if its possible but anyways. No action on this

Copy link
Member Author

Choose a reason for hiding this comment

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

I see, that would be difficult to achieve, it's options from db-sync schema

script_hash: Buffer;
unit_mem: string;
unit_steps: string;
Expand Down
4 changes: 3 additions & 1 deletion packages/cardano-services/src/ChainHistory/openApi.json
Original file line number Diff line number Diff line change
Expand Up @@ -361,7 +361,9 @@
"spend",
"mint",
"certificate",
"withdrawal"
"withdrawal",
"vote",
"propose"
]
},
"data": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -294,24 +294,30 @@ describe('chain history mappers', () => {
});
});
describe('mapRedeemer', () => {
const redeemerModel: RedeemerModel = {
const redeemerModel: Omit<RedeemerModel, 'purpose'> = {
index: 1,
purpose: 'mint',
script_hash: Buffer.from(hash28ByteBase16, 'hex'),
tx_id: Buffer.from(transactionHash, 'hex'),
unit_mem: '2000',
unit_steps: '5000'
};
test('map RedeemerModel to Cardano.Redeemer', () => {
const result = mappers.mapRedeemer(redeemerModel);
test.each([
['spend' as const, Cardano.RedeemerPurpose.spend],
['mint' as const, Cardano.RedeemerPurpose.mint],
['cert' as const, Cardano.RedeemerPurpose.certificate],
['reward' as const, Cardano.RedeemerPurpose.withdrawal],
['voting' as const, Cardano.RedeemerPurpose.vote],
['proposing' as const, Cardano.RedeemerPurpose.propose]
])("maps '%p' redeemer", (dbSyncRedeemerPurpose, sdkRedeemerPurpose) => {
const result = mappers.mapRedeemer({ ...redeemerModel, purpose: dbSyncRedeemerPurpose });
expect(result).toEqual<Cardano.Redeemer>({
data: Buffer.from('not implemented'),
executionUnits: {
memory: 2000,
steps: 5000
},
index: 1,
purpose: Cardano.RedeemerPurpose.mint
purpose: sdkRedeemerPurpose
});
});
});
Expand Down
1 change: 0 additions & 1 deletion packages/core/src/Cardano/types/Transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,6 @@ export enum RedeemerPurpose {
mint = 'mint',
certificate = 'certificate',
withdrawal = 'withdrawal',
delegateRepresentative = 'representative',
propose = 'propose',
vote = 'vote'
}
Expand Down
Loading