Skip to content

Commit

Permalink
Merge pull request #945 from input-output-hk/feat/lw-8113-add-asset-p…
Browse files Browse the repository at this point in the history
…rojection

LW-8113 add Asset projection
  • Loading branch information
mkazlauskas committed Oct 11, 2023
2 parents 02bec94 + f0cf66b commit 5d0dbab
Show file tree
Hide file tree
Showing 7 changed files with 136 additions and 4 deletions.
41 changes: 41 additions & 0 deletions compose/common.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ x-with-postgres: &with-postgres
- postgres_password
- postgres_user
- postgres_db
- postgres_db_asset
- postgres_db_db_sync
- postgres_db_handle
- postgres_db_stake_pool
Expand Down Expand Up @@ -69,6 +70,7 @@ x-provider-server-environment: &provider-server-environment
x-sdk-environment: &sdk-environment
LOGGER_MIN_SEVERITY: ${LOGGER_MIN_SEVERITY:-info}
OGMIOS_URL: ws://cardano-node-ogmios:1337
POSTGRES_DB_FILE_ASSET: /run/secrets/postgres_db_handle
POSTGRES_DB_FILE_DB_SYNC: /run/secrets/postgres_db_db_sync
POSTGRES_DB_FILE_HANDLE: /run/secrets/postgres_db_handle
POSTGRES_DB_FILE_STAKE_POOL: /run/secrets/postgres_db_stake_pool
Expand All @@ -78,12 +80,14 @@ x-sdk-environment: &sdk-environment
POSTGRES_POOL_MAX_DB_SYNC: ${POSTGRES_POOL_MAX:-10}
POSTGRES_POOL_MAX_HANDLE: ${POSTGRES_POOL_MAX:-10}
POSTGRES_POOL_MAX_STAKE_POOL: ${POSTGRES_POOL_MAX:-10}
POSTGRES_PASSWORD_FILE_ASSET: /run/secrets/postgres_password
POSTGRES_PASSWORD_FILE_DB_SYNC: /run/secrets/postgres_password
POSTGRES_PASSWORD_FILE_HANDLE: /run/secrets/postgres_password
POSTGRES_PASSWORD_FILE_STAKE_POOL: /run/secrets/postgres_password
POSTGRES_PORT_DB_SYNC: 5432
POSTGRES_PORT_HANDLE: 5432
POSTGRES_PORT_STAKE_POOL: 5432
POSTGRES_USER_FILE_ASSET: /run/secrets/postgres_user
POSTGRES_USER_FILE_DB_SYNC: /run/secrets/postgres_user
POSTGRES_USER_FILE_HANDLE: /run/secrets/postgres_user
POSTGRES_USER_FILE_STAKE_POOL: /run/secrets/postgres_user
Expand Down Expand Up @@ -192,6 +196,27 @@ services:
- postgres_user
shm_size: '2gb'

asset-projector:
<<:
- *from-sdk
- *logging
- *projector
- *with-postgres
environment:
<<:
- *projector-environment
- *sdk-environment
POSTGRES_DB_FILE: /run/secrets/postgres_db_asset
PROJECTION_NAMES: asset
healthcheck:
test:
[
'CMD-SHELL',
'test `curl -fs http://localhost:3000/v1.0.0/health | jq -r ".services[0].projectedTip.blockNo"` -gt 1'
]
ports:
- ${ASSET_PROJECTOR_PORT:-4006}:3000

handle-projector:
<<:
- *from-sdk
Expand Down Expand Up @@ -262,6 +287,20 @@ services:
ports:
- ${STAKE_POOL_API_PORT:-4010}:3000

asset-provider-server:
<<:
- *from-sdk
- *logging
- *provider-server
- *with-postgres
environment:
<<:
- *sdk-environment
- *provider-server-environment
SERVICE_NAMES: asset
ports:
- ${HANDLE_API_PORT:-4014}:3000

handle-provider-server:
<<:
- *from-sdk
Expand Down Expand Up @@ -321,6 +360,8 @@ secrets:
# See https://github.com/input-output-hk/cardano-db-sync/blob/aed18d1be192bd58d054cdba23758b579dae9f4e/nix/docker.nix#L116
postgres_db:
file: ../../compose/placeholder-secrets/postgres_db_db_sync
postgres_db_asset:
file: ../../compose/placeholder-secrets/postgres_db_asset
postgres_db_db_sync:
file: ../../compose/placeholder-secrets/postgres_db_db_sync
postgres_db_handle:
Expand Down
8 changes: 8 additions & 0 deletions compose/dev.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
version: '3.9'

services:
asset-projector:
volumes:
- ../..:/app

asset-provider-server:
volumes:
- ../..:/app

pg-boss-worker:
volumes:
- ../..:/app
Expand Down
1 change: 1 addition & 0 deletions compose/placeholder-secrets/postgres_db_asset
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
asset
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ import { passthrough } from '@cardano-sdk/util-rxjs';
*
*/
export enum ProjectionName {
Asset = 'asset',
Handle = 'handle',
StakePool = 'stake-pool',
StakePoolMetadataJob = 'stake-pool-metadata-job',
Expand All @@ -65,6 +66,10 @@ const createMapperOperators = (
const filterUtxo = applyUtxoAndMintFilters
? Mapper.filterProducedUtxoByAssetPolicyId({ policyIds: handlePolicyIds })
: passthrough();
const filterProducedUtxoByAssetsPresence =
projectionNames.includes(ProjectionName.Asset) && !projectionNames.includes(ProjectionName.UTXO)
? Mapper.filterProducedUtxoByAssetsPresence()
: passthrough();
const filterMint = applyUtxoAndMintFilters
? Mapper.filterMintByPolicyIds({ policyIds: handlePolicyIds })
: passthrough();
Expand All @@ -74,6 +79,7 @@ const createMapperOperators = (
: passthrough();
return {
filterMint,
filterProducedUtxoByAssetsPresence,
filterUtxo,
withAddresses: Mapper.withAddresses(),
withCIP67: Mapper.withCIP67(),
Expand Down Expand Up @@ -218,6 +224,7 @@ const storeInterDependencies: Partial<Record<StoreName, StoreName[]>> = {

const projectionStoreDependencies: Record<ProjectionName, StoreName[]> = {
address: ['storeAddresses'],
asset: ['storeAssets'],
// TODO: remove storeNftMetadata when TypeormAssetProvider tests
// are updated to use 'asset' database instead of a handle database
handle: ['storeHandles', 'storeHandleMetadata', 'storeNftMetadata'],
Expand Down
7 changes: 7 additions & 0 deletions packages/e2e/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,13 @@ services:
- ./local-network/config/network:/config
- sdk-ipc:/sdk-ipc

asset-provider-server:
environment:
HANDLE_POLICY_IDS_FILE: /sdk-ipc/handle_policy_ids
volumes:
- ./local-network/config/network:/config
- sdk-ipc:/sdk-ipc

volumes:
sdk-ipc:
driver: local
Expand Down
14 changes: 14 additions & 0 deletions packages/projection/src/operators/Mappers/withUtxo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,20 @@ export const filterProducedUtxoByAddresses =
}))
);

export const filterProducedUtxoByAssetsPresence =
<PropsIn extends WithUtxo>(): ProjectionOperator<PropsIn> =>
// eslint-disable-next-line unicorn/consistent-function-scoping
(evt$) =>
evt$.pipe(
map((evt) => ({
...evt,
utxo: {
...evt.utxo,
produced: evt.utxo.produced.filter(([_, { value }]) => value.assets && value.assets.size > 0)
}
}))
);

export const filterProducedUtxoByAssetPolicyId =
<PropsIn extends WithUtxo>({ policyIds }: FilterByPolicyIds): ProjectionOperator<PropsIn> =>
(evt$) =>
Expand Down
62 changes: 58 additions & 4 deletions packages/projection/test/operators/Mappers/withUtxo.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { ProjectionEvent } from '../../../src';
import {
filterProducedUtxoByAddresses,
filterProducedUtxoByAssetPolicyId,
filterProducedUtxoByAssetsPresence,
withUtxo
} from '../../../src/operators/Mappers';
import { firstValueFrom, of } from 'rxjs';
Expand Down Expand Up @@ -71,6 +72,43 @@ export const validTxSource$ = of({
]
},
inputSource: Cardano.InputSource.inputs
},
{
body: {
inputs: [
{
index: 0,
txId: '73e26ff267b5ee32d8e413635f4f4c9547db1c2af1694faf51be20b9f508b8f6'
}
],
outputs: [
{
address:
'addr_test1qzrf8t56qhzcp2chrtn7deqhep0dttr3eemhnut6lth3gulj7cuplfarmnq5fyumgl0lklddvau9dhamaexykljzvpyswqt56p',
value: {
assets: new Map([
[
Cardano.AssetId(
'8f78a4388b1a3e1a1435257e9356fa0c2cc0d3a5999d63b5886c96435365636f6e6454657374746f6b656e'
),
1n
],
[Cardano.AssetId('8f78a4388b1a3e1a1435257e9356fa0c2cc0d3a5999d63b5886c964354657374746f6b656e'), 1n],
[Cardano.AssetId('7f78a4388b1a3e1a1435257e9356fa0c2cc0d3a5999d63b5886c964354657374746f6b656e'), 1n]
]),
coins: 25_485_292n
}
},
{
address: 'addr_test1vptwv4jvaqt635jvthpa29lww3vkzypm8l6vk4lv4tqfhhgajdgwf',
value: {
assets: new Map(),
coins: 74_341_815n
}
}
]
},
inputSource: Cardano.InputSource.inputs
}
]
}
Expand Down Expand Up @@ -172,8 +210,8 @@ describe('withUtxo', () => {
const {
utxo: { consumed, produced }
} = await firstValueFrom(validTxSource$.pipe(withUtxo()));
expect(consumed).toHaveLength(3);
expect(produced).toHaveLength(3);
expect(consumed).toHaveLength(4);
expect(produced).toHaveLength(5);
});

it('when inputSource is collateral: maps consumed/produced utxo from collateral/collateralReturn', async () => {
Expand Down Expand Up @@ -203,7 +241,23 @@ describe('withUtxo', () => {
)
);

expect(produced).toHaveLength(1);
expect(produced).toHaveLength(2);
});
});

describe('filterProducedUtxoByAssetsPresence', () => {
it('keeps only utxo produced that contain any assets', async () => {
const {
utxo: { produced }
} = await firstValueFrom(validTxSource$.pipe(withUtxo(), filterProducedUtxoByAssetsPresence()));

const utxosWithAssets = produced.filter(([_key, { value }]) => value.assets && value.assets.size > 0);

expect(utxosWithAssets.length).toBe(produced.length);

for (const [_key, { value }] of utxosWithAssets) {
expect(value.assets?.size).toBeGreaterThan(0);
}
});
});

Expand All @@ -220,7 +274,7 @@ describe('withUtxo', () => {
)
);

expect(produced).toHaveLength(1);
expect(produced).toHaveLength(2);
expect(produced[0][1].value.assets?.size).toBe(2);
});
});
Expand Down

0 comments on commit 5d0dbab

Please sign in to comment.