Skip to content

Commit

Permalink
feat!: update core types with deserialized PlutusData
Browse files Browse the repository at this point in the history
BREAKING CHANGE: incompatible with previous revisions of cardano-services
- rename utxo and transactions PouchDB stores
- update type of Tx.witness.redeemers
- update type of Tx.witness.datums
- update type of TxOut.datum
- remove Cardano.Datum type

fix(cardano-services): correct chain history openApi endpoints path url to match version
  • Loading branch information
mkazlauskas committed Sep 14, 2023
1 parent c31603e commit d8cc93b
Show file tree
Hide file tree
Showing 40 changed files with 640 additions and 131 deletions.
4 changes: 2 additions & 2 deletions packages/cardano-services-client/src/version.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
// auto-generated using ../scripts/createVersionSource.js
export const apiVersion = {
assetInfo: '1.0.0',
chainHistory: '1.0.0',
chainHistory: '2.0.0',
handle: '1.0.0',
networkInfo: '1.0.0',
rewards: '1.0.0',
root: '1.0.0',
stakePool: '1.0.0',
txSubmit: '1.0.0',
utxo: '1.0.0'
utxo: '2.0.0'
};
4 changes: 2 additions & 2 deletions packages/cardano-services-client/version.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
{
"assetInfo": "1.0.0",
"chainHistory": "1.0.0",
"chainHistory": "2.0.0",
"handle": "1.0.0",
"networkInfo": "1.0.0",
"rewards": "1.0.0",
"root": "1.0.0",
"stakePool": "1.0.0",
"txSubmit": "1.0.0",
"utxo": "1.0.0"
"utxo": "2.0.0"
}
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ export class DbSyncChainHistoryProvider extends DbSyncProvider() implements Chai
this.#builder.queryTxMintByIds(ids),
this.#builder.queryWithdrawalsByTxIds(ids),
this.#builder.queryRedeemersByIds(ids),
// Missing witness datums
this.#metadataService.queryTxMetadataByRecordIds(ids),
this.#builder.queryTransactionInputsByIds(ids, true),
this.#builder.queryCertificatesByIds(ids)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { BigIntMath, HexBlob } from '@cardano-sdk/util';
import { BigIntMath } from '@cardano-sdk/util';
import {
BlockModel,
BlockOutputModel,
Expand Down Expand Up @@ -85,6 +85,7 @@ export const mapTxOut = (txOut: TxOutput): Cardano.TxOut => ({

export const mapTxOutModel = (txOutModel: TxOutputModel, assets?: Cardano.TokenMap): TxOutput => ({
address: txOutModel.address as unknown as Cardano.PaymentAddress,
// Inline datums are missing, but for now it's ok on ChainHistoryProvider
datumHash: txOutModel.datum ? (txOutModel.datum.toString('hex') as unknown as Hash32ByteBase16) : undefined,
index: txOutModel.index,
txId: txOutModel.tx_id.toString('hex') as unknown as Cardano.TransactionId,
Expand All @@ -99,8 +100,12 @@ export const mapWithdrawal = (withdrawalModel: WithdrawalModel): Cardano.Withdra
stakeAddress: withdrawalModel.stake_address as unknown as Cardano.RewardAccount
});

// TODO: unfortunately this is not nullable and not implemented.
// Remove this and select the actual redeemer data from `redeemer_data` table.
const stubRedeemerData = Buffer.from('not implemented');

export const mapRedeemer = (redeemerModel: RedeemerModel): Cardano.Redeemer => ({
data: redeemerModel.script_hash.toString('hex') as unknown as HexBlob,
data: stubRedeemerData,
executionUnits: {
memory: Number(redeemerModel.unit_mem),
steps: Number(redeemerModel.unit_steps)
Expand Down
12 changes: 6 additions & 6 deletions packages/cardano-services/src/ChainHistory/openApi.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@
"name": "Apache 2.0",
"url": "http://www.apache.org/licenses/LICENSE-2.0.html"
},
"version": "1.0.0"
"version": "2.0.0"
},
"paths": {
"/v1.0.0/chain-history/health": {
"/v2.0.0/chain-history/health": {
"$ref": "../Http/schema.json#/paths/Health"
},
"/v1.0.0/chain-history/blocks/by-hashes": {
"/v2.0.0/chain-history/blocks/by-hashes": {
"post": {
"summary": "block history by hashes",
"operationId": "blocksByHashes",
Expand Down Expand Up @@ -50,7 +50,7 @@
}
}
},
"/v1.0.0/chain-history/txs/by-hashes": {
"/v2.0.0/chain-history/txs/by-hashes": {
"post": {
"summary": "transaction history by hashes",
"operationId": "transactionsByHashes",
Expand Down Expand Up @@ -88,7 +88,7 @@
}
}
},
"/v1.0.0/chain-history/txs/by-addresses": {
"/v2.0.0/chain-history/txs/by-addresses": {
"post": {
"summary": "transaction history by addresses",
"operationId": "transactionsByAddresses",
Expand Down Expand Up @@ -365,7 +365,7 @@
]
},
"data": {
"type": "string"
"type": "object"
}
}
},
Expand Down
2 changes: 1 addition & 1 deletion packages/cardano-services/src/Http/openApi.json
Original file line number Diff line number Diff line change
Expand Up @@ -125,4 +125,4 @@
}
}
}
}
}
12 changes: 10 additions & 2 deletions packages/cardano-services/src/Utxo/DbSyncUtxoProvider/mappers.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
import { Cardano, SerializationError, SerializationFailure, createUtxoId, jsonToNativeScript } from '@cardano-sdk/core';
import {
Cardano,
Serialization,
SerializationError,
SerializationFailure,
createUtxoId,
jsonToNativeScript
} from '@cardano-sdk/core';
import { Hash32ByteBase16 } from '@cardano-sdk/crypto';
import { HexBlob, isNotNil } from '@cardano-sdk/util';
import { ReferenceScriptType, UtxoModel } from './types';
Expand Down Expand Up @@ -84,7 +91,8 @@ export const utxosToCore = (utxosModels: UtxoModel[]): Cardano.Utxo[] => {
}
};
if (isNotNil(current.data_hash)) txOut.datumHash = current.data_hash as unknown as Hash32ByteBase16;
if (isNotNil(current.inline_datum)) txOut.datum = current.inline_datum as unknown as HexBlob;
if (isNotNil(current.inline_datum))
txOut.datum = Serialization.PlutusData.fromCbor(HexBlob(current.inline_datum)).toCore();
if (isNotNil(current.reference_script_type)) txOut.scriptReference = parseReferenceScript(current);

if (isNotNil(current.asset_name) && current.asset_policy && current.asset_quantity) {
Expand Down
36 changes: 27 additions & 9 deletions packages/cardano-services/src/Utxo/openApi.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@
"name": "Apache 2.0",
"url": "http://www.apache.org/licenses/LICENSE-2.0.html"
},
"version": "1.0.0"
"version": "2.0.0"
},
"paths": {
"/v1.0.0/utxo/health": {
"/v2.0.0/utxo/health": {
"$ref": "../Http/schema.json#/paths/Health"
},
"/v1.0.0/utxo/utxo-by-addresses": {
"/v2.0.0/utxo/utxo-by-addresses": {
"post": {
"summary": "UTxO search",
"operationId": "utxoSearch",
Expand Down Expand Up @@ -54,7 +54,9 @@
"components": {
"schemas": {
"TypifiedValue": {
"required": ["value"],
"required": [
"value"
],
"type": "object",
"properties": {
"value": {
Expand All @@ -73,7 +75,9 @@
"example": "addr_test1qpu5vlrf4xkxv2qpwngf6cjhtw542ayty80v8dyr49rf5ewvxwdrt70qlcpeeagscasafhffqsxy36t90ldv06wqrk2qum8x5w"
},
"Value": {
"required": ["coins"],
"required": [
"coins"
],
"type": "object",
"properties": {
"coins": {
Expand All @@ -85,7 +89,11 @@
}
},
"TxIn": {
"required": ["address", "index", "txId"],
"required": [
"address",
"index",
"txId"
],
"type": "object",
"properties": {
"address": {
Expand All @@ -108,7 +116,10 @@
]
},
"TxOut": {
"required": ["address", "value"],
"required": [
"address",
"value"
],
"type": "object",
"properties": {
"address": {
Expand All @@ -118,7 +129,13 @@
"$ref": "#/components/schemas/Value"
},
"datum": {
"type": "object"
},
"datumHash": {
"type": "string"
},
"scriptReference": {
"type": "object"
}
},
"example": [
Expand Down Expand Up @@ -151,14 +168,15 @@
}
},
"utxo_search_body": {
"required": ["addresses"],
"required": [
"addresses"
],
"type": "object",
"properties": {
"addresses": {
"type": "array",
"items": {
"$ref": "#/components/schemas/Address"

}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import { CreateHttpProviderConfig, chainHistoryHttpProvider } from '@cardano-sdk
import { DB_MAX_SAFE_INTEGER } from '../../src/ChainHistory/DbSyncChainHistory/queries';
import { DataMocks } from '../data-mocks';
import { DbPools, LedgerTipModel, findLedgerTip } from '../../src/util/DbSyncProvider';
import { HexBlob } from '@cardano-sdk/util';
import { OgmiosCardanoNode } from '@cardano-sdk/ogmios';
import { Pool } from 'pg';
import { clearDbPools, servicesWithVersionPath as services } from '../util';
Expand Down Expand Up @@ -303,7 +302,6 @@ describe('ChainHistoryHttpService', () => {
expect(response.length).toEqual(1);
expect(tx.witness).toMatchShapeOf(DataMocks.Tx.witnessRedeemers);
expect(tx.witness.redeemers!.length).toBeGreaterThan(0);
expect(() => HexBlob(tx.witness.redeemers![0].data as unknown as string)).not.toThrow();
});

it('has auxiliary data', async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import {
} from '../../../src/ChainHistory/DbSyncChainHistory/types';
import { Cardano } from '@cardano-sdk/core';
import { Hash32ByteBase16 } from '@cardano-sdk/crypto';
import { HexBlob } from '@cardano-sdk/util';

const blockHash = '7a48b034645f51743550bbaf81f8a14771e58856e031eb63844738ca8ad72298';
const poolId = 'pool1zuevzm3xlrhmwjw87ec38mzs02tlkwec9wxpgafcaykmwg7efhh';
Expand Down Expand Up @@ -291,7 +290,7 @@ describe('chain history mappers', () => {
test('map RedeemerModel to Cardano.Redeemer', () => {
const result = mappers.mapRedeemer(redeemerModel);
expect(result).toEqual<Cardano.Redeemer>({
data: HexBlob(hash28ByteBase16),
data: Buffer.from('not implemented'),
executionUnits: {
memory: 2000,
steps: 5000
Expand All @@ -310,7 +309,7 @@ describe('chain history mappers', () => {
];
const redeemers: Cardano.Redeemer[] = [
{
data: HexBlob(hash28ByteBase16),
data: Buffer.from('not implemented'),
executionUnits: { memory: 1, steps: 2 },
index: 1,
purpose: Cardano.RedeemerPurpose.spend
Expand Down
6 changes: 3 additions & 3 deletions packages/cardano-services/test/data-mocks/tx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ export const withWithdrawals: Cardano.HydratedTx = merge(withAssets, {
export const witnessRedeemers = {
redeemers: [
{
data: '67f33146617a5e61936081db3b2117cbf59bd2123748f58ac9678656',
data: {},
executionUnits: {
memory: 1700,
steps: 476_468
Expand Down Expand Up @@ -170,7 +170,7 @@ export const output = {
export const outputWithInlineDatum = {
address:
'addr_test1qpcncempf4svkpw0salztrsxzrfpr5ll323q5whw7lv94vyw0kz5rxvdaq6u6tslwfrrgz6l4n4lpcpnawn87yl9k6dsu4hhg2',
datum: '182a',
datum: 42n,
value: {
coins: 13_499_999_999_819_540n
}
Expand Down Expand Up @@ -220,7 +220,7 @@ export const certificate = {
};

export const redeemer = {
data: '67f33146617a5e61936081db3b2117cbf59bd2123748f58ac9678656',
data: {},
executionUnits: {
memory: 0,
steps: 0
Expand Down
Loading

0 comments on commit d8cc93b

Please sign in to comment.