Skip to content

Commit

Permalink
feat!: rename era-specific types in core
Browse files Browse the repository at this point in the history
- rename `TxAlonzo` -> `HydratedTx`, `TxBodyAlonzo` -> `HydratedTxBody`, `TxIn` -> `HydratedTxIn`
- rename `NewTxAlonzo` -> `Tx`, `NewTxBodyAlonzo` -> `TxBody`, `NewTxIn` -> `TxIn`
  • Loading branch information
Ivaylo Andonov committed Nov 29, 2022
1 parent ce687b3 commit 33af062
Show file tree
Hide file tree
Showing 63 changed files with 261 additions and 259 deletions.
Expand Up @@ -53,7 +53,7 @@ export class DbSyncChainHistoryProvider extends DbSyncProvider() implements Chai
addresses,
pagination,
blockRange
}: TransactionsByAddressesArgs): Promise<Paginated<Cardano.TxAlonzo>> {
}: TransactionsByAddressesArgs): Promise<Paginated<Cardano.HydratedTx>> {
if (addresses.length > this.#paginationPageSizeLimit) {
throw new ProviderError(
ProviderFailure.BadRequest,
Expand Down Expand Up @@ -104,7 +104,7 @@ export class DbSyncChainHistoryProvider extends DbSyncProvider() implements Chai
};
}

public async transactionsByHashes({ ids }: TransactionsByIdsArgs): Promise<Cardano.TxAlonzo[]> {
public async transactionsByHashes({ ids }: TransactionsByIdsArgs): Promise<Cardano.HydratedTx[]> {
if (ids.length > this.#paginationPageSizeLimit) {
throw new ProviderError(
ProviderFailure.BadRequest,
Expand Down
Expand Up @@ -59,7 +59,7 @@ export const mapTxOutTokenMap = (multiAssetModels: TxOutMultiAssetModel[]): TxOu
return txTokenMap;
};

export const mapTxIn = (txIn: TxInput): Cardano.TxIn => ({
export const mapTxIn = (txIn: TxInput): Cardano.HydratedTxIn => ({
address: txIn.address,
index: txIn.index,
txId: txIn.txSourceId
Expand Down Expand Up @@ -153,20 +153,20 @@ export const mapCertificate = (
};

interface TxAlonzoData {
inputs: Cardano.TxIn[];
inputs: Cardano.HydratedTxIn[];
outputs: Cardano.TxOut[];
mint?: Cardano.TokenMap;
withdrawals?: Cardano.Withdrawal[];
redeemers?: Cardano.Redeemer[];
metadata?: Cardano.TxMetadata;
collaterals?: Cardano.TxIn[];
collaterals?: Cardano.HydratedTxIn[];
certificates?: Cardano.Certificate[];
}

export const mapTxAlonzo = (
txModel: TxModel,
{ inputs, outputs, mint, withdrawals, redeemers, metadata, collaterals, certificates }: TxAlonzoData
): Cardano.TxAlonzo => ({
): Cardano.HydratedTx => ({
auxiliaryData:
metadata && metadata.size > 0
? {
Expand Down
Expand Up @@ -225,7 +225,7 @@ describe('ChainHistoryHttpService', () => {
it('has outputs with multi-assets', async () => {
const ids = await fixtureBuilder.getTxHashes(1, { with: [TxWith.MultiAsset] });
const response = await provider.transactionsByHashes({ ids });
const tx: Cardano.TxAlonzo = response[0];
const tx: Cardano.HydratedTx = response[0];

// A transaction involving multi assets could also have outputs without multi assets, so we must first
// find the index of the output inside the transaction with the native tokens.
Expand All @@ -240,7 +240,7 @@ describe('ChainHistoryHttpService', () => {
const response = await provider.transactionsByHashes({
ids: await fixtureBuilder.getTxHashes(1, { with: [TxWith.Mint] })
});
const tx: Cardano.TxAlonzo = response[0];
const tx: Cardano.HydratedTx = response[0];
expect(response.length).toEqual(1);
expect(tx.body.mint).toMatchShapeOf(DataMocks.Tx.mint);
expect(tx.body.mint?.size).toBeGreaterThan(0);
Expand All @@ -250,7 +250,7 @@ describe('ChainHistoryHttpService', () => {
const response = await provider.transactionsByHashes({
ids: await fixtureBuilder.getTxHashes(1, { with: [TxWith.Withdrawal] })
});
const tx: Cardano.TxAlonzo = response[0];
const tx: Cardano.HydratedTx = response[0];
expect(response.length).toEqual(1);
expect(tx.body.withdrawals!).toMatchShapeOf(DataMocks.Tx.withdrawals);
expect(tx.body.withdrawals?.length).toBeGreaterThan(0);
Expand All @@ -261,7 +261,7 @@ describe('ChainHistoryHttpService', () => {
ids: await fixtureBuilder.getTxHashes(1, { with: [TxWith.Redeemer] })
});

const tx: Cardano.NewTxAlonzo = response[0];
const tx: Cardano.Tx = response[0];
expect(response.length).toEqual(1);
expect(tx.witness).toMatchShapeOf(DataMocks.Tx.witnessRedeemers);
expect(tx.witness.redeemers?.length).toBeGreaterThan(0);
Expand All @@ -271,7 +271,7 @@ describe('ChainHistoryHttpService', () => {
const response = await provider.transactionsByHashes({
ids: await fixtureBuilder.getTxHashes(1, { with: [TxWith.AuxiliaryData] })
});
const tx: Cardano.TxAlonzo = response[0];
const tx: Cardano.HydratedTx = response[0];
expect(response.length).toEqual(1);
expect(tx.auxiliaryData).toBeDefined();
});
Expand All @@ -280,7 +280,7 @@ describe('ChainHistoryHttpService', () => {
const response = await provider.transactionsByHashes({
ids: await fixtureBuilder.getTxHashes(1, { with: [TxWith.CollateralInput] })
});
const tx: Cardano.TxAlonzo = response[0];
const tx: Cardano.HydratedTx = response[0];
expect(response.length).toEqual(1);

expect(tx.body.collaterals).toMatchShapeOf(DataMocks.Tx.collateralInputs);
Expand All @@ -292,8 +292,8 @@ describe('ChainHistoryHttpService', () => {
ids: await fixtureBuilder.getTxHashes(2, { with: [TxWith.DelegationCertificate] })
});

const tx1: Cardano.TxAlonzo = response[0];
const tx2: Cardano.TxAlonzo = response[1];
const tx1: Cardano.HydratedTx = response[0];
const tx2: Cardano.HydratedTx = response[1];

expect(response.length).toEqual(2);
expect(tx1.body.certificates?.length).toBeGreaterThan(0);
Expand Down
Expand Up @@ -297,7 +297,7 @@ describe('chain history mappers', () => {
});
});
describe('mapTxAlonzo', () => {
const inputs: Cardano.TxIn[] = [
const inputs: Cardano.HydratedTxIn[] = [
{ address: Cardano.Address(address), index: 1, txId: Cardano.TransactionId(transactionHash) }
];
const outputs: Cardano.TxOut[] = [{ address: Cardano.Address(address), value: { assets, coins: 20_000_000n } }];
Expand All @@ -318,26 +318,26 @@ describe('chain history mappers', () => {
}
];

const expected: Cardano.TxAlonzo = {
const expected: Cardano.HydratedTx = {
blockHeader: { blockNo: 200, hash: Cardano.BlockId(blockHash), slot: 250 },
body: { fee: 170_000n, inputs, outputs, validityInterval: { invalidBefore: 300, invalidHereafter: 500 } },
id: Cardano.TransactionId(transactionHash),
index: 1,
txSize: 20,
witness: { signatures: new Map() }
};
test('map TxModel to Cardano.TxAlonzo with minimal data', () => {
test('map TxModel to Cardano.HydratedTx with minimal data', () => {
const result = mappers.mapTxAlonzo(txModel, { inputs, outputs });
expect(result).toEqual<Cardano.TxAlonzo>(expected);
expect(result).toEqual<Cardano.HydratedTx>(expected);
});
test('map TxModel with null fields to Cardano.TxAlonzo', () => {
test('map TxModel with null fields to Cardano.HydratedTx', () => {
const result = mappers.mapTxAlonzo(
{ ...txModel, invalid_before: null, invalid_hereafter: null },
{ inputs, outputs }
);
expect(result).toEqual<Cardano.TxAlonzo>({ ...expected, body: { ...expected.body, validityInterval: {} } });
expect(result).toEqual<Cardano.HydratedTx>({ ...expected, body: { ...expected.body, validityInterval: {} } });
});
test('map TxModel to Cardano.TxAlonzo with extra data', () => {
test('map TxModel to Cardano.HydratedTx with extra data', () => {
const result = mappers.mapTxAlonzo(txModel, {
certificates,
collaterals: inputs,
Expand All @@ -348,7 +348,7 @@ describe('chain history mappers', () => {
redeemers,
withdrawals
});
expect(result).toEqual<Cardano.TxAlonzo>({
expect(result).toEqual<Cardano.HydratedTx>({
...expected,
auxiliaryData: { body: { blob: metadata } },
body: { ...expected.body, certificates, collaterals: inputs, mint: assets, withdrawals },
Expand All @@ -369,9 +369,9 @@ describe('chain history mappers', () => {
});
});
describe('mapTxIn', () => {
test('map TxInput to Cardano.TxIn', () => {
test('map TxInput to Cardano.HydratedTxIn', () => {
const result = mappers.mapTxIn(txInput);
expect(result).toEqual<Cardano.TxIn>({
expect(result).toEqual<Cardano.HydratedTxIn>({
address: Cardano.Address(address),
index: 1,
txId: Cardano.TransactionId(sourceTransactionHash)
Expand Down
20 changes: 10 additions & 10 deletions packages/cardano-services/test/data-mocks/tx.ts
Expand Up @@ -25,7 +25,7 @@ export const txOutWithCoinOnly: Cardano.TxOut = { ...txOutBase, value: valueWith

export const txOutWithAssets: Cardano.TxOut = { ...txOutBase, value: valueWithAssets };

export const txIn: Cardano.TxIn = {
export const txIn: Cardano.HydratedTxIn = {
address: Cardano.Address('addr_test1wrsexavz37208qda7mwwu4k7hcpg26cz0ce86f5e9kul3hqzlh22t'),
index: 0,
txId: Cardano.TransactionId('cefd2fcf657e5e5d6c35975f4e052f427819391b153ebb16ad8aa107ba5a3819')
Expand All @@ -41,7 +41,7 @@ export const base = {
blockNo: 3_157_934,
hash: Cardano.BlockId('f03084089ec7e74a79e69a5929b2d3c0836d6f12279bd103d0875847c740ae27'),
slot: 45_286_016
} as Cardano.TxAlonzo['blockHeader'],
} as Cardano.HydratedTx['blockHeader'],
body: {
fee: 191_109n,
inputs: [txIn],
Expand All @@ -50,7 +50,7 @@ export const base = {
invalidBefore: undefined,
invalidHereafter: undefined
}
} as Omit<Cardano.TxAlonzo['body'], 'outputs'>,
} as Omit<Cardano.HydratedTx['body'], 'outputs'>,
id: Cardano.TransactionId('cefd2fcf657e5e5d6c35975f4e052f427819391b153ebb16ad8aa107ba5a3819'),
index: 30,
txSize: 711,
Expand All @@ -59,11 +59,11 @@ export const base = {
}
};

export const withCoinOnly: Cardano.TxAlonzo = merge(base, {
export const withCoinOnly: Cardano.HydratedTx = merge(base, {
body: { outputs: [txOutWithCoinOnly] }
});

export const withAssets: Cardano.TxAlonzo = merge(base, {
export const withAssets: Cardano.HydratedTx = merge(base, {
body: {
...base.body,
outputs: [txOutWithAssets]
Expand All @@ -73,7 +73,7 @@ export const withAssets: Cardano.TxAlonzo = merge(base, {
}
});

export const withMint: Cardano.TxAlonzo = merge(withAssets, {
export const withMint: Cardano.HydratedTx = merge(withAssets, {
body: {
mint: new Map([
[Cardano.AssetId('57fca08abbaddee36da742a839f7d83a7e1d2419f1507fcbf3916522534245525259'), 10_000_000n]
Expand All @@ -85,7 +85,7 @@ export const mint: Cardano.TokenMap = new Map([
[Cardano.AssetId('57fca08abbaddee36da742a839f7d83a7e1d2419f1507fcbf3916522534245525259'), 10_000_000n]
]);

export const withAuxiliaryData: Cardano.TxAlonzo = merge(withAssets, {
export const withAuxiliaryData: Cardano.HydratedTx = merge(withAssets, {
auxiliaryData: {
body: {
blob: new Map([[1, 'abc']])
Expand All @@ -107,7 +107,7 @@ export const collateralInputs = [
}
];

export const withValidityInterval: Cardano.TxAlonzo = merge(withAssets, {
export const withValidityInterval: Cardano.HydratedTx = merge(withAssets, {
body: {
validityInterval: {
invalidBefore: 1,
Expand All @@ -123,7 +123,7 @@ export const withdrawals = [
}
];

export const withWithdrawals: Cardano.TxAlonzo = merge(withAssets, {
export const withWithdrawals: Cardano.HydratedTx = merge(withAssets, {
body: {
withdrawals
}
Expand Down Expand Up @@ -163,7 +163,7 @@ export const output = {

export const outputs = [output];

export const blockHeader: Cardano.TxAlonzo['blockHeader'] = {
export const blockHeader: Cardano.HydratedTx['blockHeader'] = {
blockNo: 3_157_934,
hash: Cardano.BlockId('f03084089ec7e74a79e69a5929b2d3c0836d6f12279bd103d0875847c740ae27'),
slot: 45_286_016
Expand Down
10 changes: 5 additions & 5 deletions packages/core/src/CML/cmlToCore/cmlToCore.ts
Expand Up @@ -57,7 +57,7 @@ export const value = (cslValue: CML.Value): Cardano.Value =>
return result;
});

export const txIn = (input: CML.TransactionInput): Cardano.NewTxIn =>
export const txIn = (input: CML.TransactionInput): Cardano.TxIn =>
usingAutoFree((scope) => ({
index: Number(scope.manage(input.index()).to_str()),
txId: Cardano.TransactionId.fromHexBlob(util.bytesToHex(scope.manage(input.transaction_id()).to_bytes()))
Expand All @@ -82,9 +82,9 @@ export const txOutputs = (outputs: CML.TransactionOutputs): Cardano.TxOut[] =>
return result;
});

export const txInputs = (inputs: CML.TransactionInputs): Cardano.NewTxIn[] =>
export const txInputs = (inputs: CML.TransactionInputs): Cardano.TxIn[] =>
usingAutoFree((scope) => {
const result: Cardano.NewTxIn[] = [];
const result: Cardano.TxIn[] = [];
for (let i = 0; i < inputs.len(); i++) {
result.push(txIn(scope.manage(inputs.get(i))));
}
Expand Down Expand Up @@ -126,7 +126,7 @@ export const txMint = (assets?: CML.Mint): Cardano.TokenMap | undefined =>
return assetMap;
});

export const txBody = (body: CML.TransactionBody): Cardano.NewTxBodyAlonzo =>
export const txBody = (body: CML.TransactionBody): Cardano.TxBody =>
usingAutoFree((scope) => {
const cslScriptDataHash = scope.manage(body.script_data_hash());
const cslCollaterals = scope.manage(body.collateral());
Expand Down Expand Up @@ -292,7 +292,7 @@ export const utxo = (cslUtxos: CML.TransactionUnspentOutput[]) =>
cslUtxos.map((cslUtxo) => [txIn(scope.manage(cslUtxo.input())), txOut(scope.manage(cslUtxo.output()))])
);

export const newTx = (cslTx: CML.Transaction): Cardano.NewTxAlonzo =>
export const newTx = (cslTx: CML.Transaction): Cardano.Tx =>
usingAutoFree((scope) => {
const transactionHash = Cardano.TransactionId.fromHexBlob(
util.bytesToHex(scope.manage(CML.hash_transaction(scope.manage(cslTx.body()))).to_bytes())
Expand Down
8 changes: 4 additions & 4 deletions packages/core/src/CML/coreToCml/coreToCml.ts
Expand Up @@ -99,7 +99,7 @@ export const value = (scope: ManagedFreeableScope, { coins, assets }: Cardano.Va
return result;
};

export const txIn = (scope: ManagedFreeableScope, core: Cardano.NewTxIn): TransactionInput =>
export const txIn = (scope: ManagedFreeableScope, core: Cardano.TxIn): TransactionInput =>
scope.manage(
TransactionInput.new(
scope.manage(TransactionHash.from_bytes(Buffer.from(core.txId, 'hex'))),
Expand Down Expand Up @@ -321,7 +321,7 @@ export const txAuxiliaryData = (
return result;
};

const txInputs = (scope: ManagedFreeableScope, coreInputs: Cardano.NewTxIn[]) => {
const txInputs = (scope: ManagedFreeableScope, coreInputs: Cardano.TxIn[]) => {
const cslInputs = scope.manage(TransactionInputs.new());
for (const input of coreInputs) {
cslInputs.add(txIn(scope, input));
Expand Down Expand Up @@ -363,7 +363,7 @@ export const txBody = (
collaterals,
requiredExtraSignatures,
scriptIntegrityHash
}: Cardano.NewTxBodyAlonzo,
}: Cardano.TxBody,
auxiliaryData?: Cardano.AuxiliaryData
): TransactionBody => {
const cslOutputs = scope.manage(TransactionOutputs.new());
Expand Down Expand Up @@ -464,7 +464,7 @@ export const witnessSet = (scope: ManagedFreeableScope, witness: Cardano.Witness
return cslWitnessSet;
};

export const tx = (scope: ManagedFreeableScope, { body, witness, auxiliaryData }: Cardano.NewTxAlonzo): Transaction => {
export const tx = (scope: ManagedFreeableScope, { body, witness, auxiliaryData }: Cardano.Tx): Transaction => {
const txWitnessSet = witnessSet(scope, witness);
// Possible optimization: only convert auxiliary data once
return scope.manage(
Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/CML/util.ts
Expand Up @@ -2,7 +2,7 @@
import { BigNum } from '@dcspark/cardano-multiplatform-lib-nodejs';
import { CML } from './CML';
import { HexBlob } from '../Cardano/util/primitives';
import { NewTxAlonzo, NewTxBodyAlonzo } from '../Cardano/types';
import { Tx, TxBody } from '../Cardano/types';
import { newTx } from './cmlToCore';
import { usingAutoFree } from '@cardano-sdk/util';

Expand Down Expand Up @@ -39,4 +39,4 @@ export const deserializeTx = ((txBody: Buffer | Uint8Array | string) => usingAut
const txDecoded = scope.manage(CML.Transaction.from_bytes(buffer));

return newTx(txDecoded);
})) as (txBody: HexBlob | Buffer | Uint8Array | string) => NewTxAlonzo<NewTxBodyAlonzo>;
})) as (txBody: HexBlob | Buffer | Uint8Array | string) => Tx<TxBody>;
4 changes: 2 additions & 2 deletions packages/core/src/Cardano/types/Block.ts
Expand Up @@ -3,8 +3,8 @@ import { Ed25519PublicKey } from './Key';
import { Hash28ByteBase16, Hash32ByteBase16, OpaqueString, typedBech32 } from '../util/primitives';
import { InvalidStringError } from '../../errors';
import { Lovelace } from './Value';
import { NewTxAlonzo } from './Transaction';
import { PoolId } from './StakePool/primitives';
import { Tx } from './Transaction';

/**
* The block size in bytes
Expand Down Expand Up @@ -108,7 +108,7 @@ export interface BlockInfo {
}

export interface Block extends BlockInfo {
body: NewTxAlonzo[];
body: Tx[];
}

export interface ExtendedBlockInfo
Expand Down

0 comments on commit 33af062

Please sign in to comment.