@odatano/core 2.0.0-rc.50 → 2.0.0-rc.51
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/@cds-models/CardanoBackendService/index.d.ts +14 -3
- package/@cds-models/CardanoBackendService/index.js +1 -0
- package/@cds-models/CardanoIndexerService/index.d.ts +98 -0
- package/@cds-models/CardanoIndexerService/index.js +10 -0
- package/@cds-models/CardanoODataService/index.d.ts +3 -0
- package/@cds-models/CardanoSignService/index.d.ts +13 -0
- package/@cds-models/CardanoSignService/index.js +1 -0
- package/@cds-models/CardanoTransactionService/index.d.ts +12 -0
- package/@cds-models/CardanoTransactionService/index.js +1 -0
- package/@cds-models/CardanoWorkerService/index.d.ts +28 -0
- package/@cds-models/CardanoWorkerService/index.js +2 -0
- package/@cds-models/index.d.ts +13 -3
- package/@cds-models/index.js +4 -3
- package/@cds-models/odatano/cardano/index.d.ts +14 -0
- package/README.md +45 -149
- package/db/schema.cds +63 -10
- package/db/types.cds +10 -0
- package/package.json +7 -44
- package/scripts/deploy-schema.mjs +113 -0
- package/scripts/migrate-schema.mjs +209 -15
- package/srv/blockchain/backends/blockfrost-backend.d.ts +2 -4
- package/srv/blockchain/backends/blockfrost-backend.js +61 -45
- package/srv/blockchain/backends/call-signal.d.ts +7 -0
- package/srv/blockchain/backends/call-signal.js +22 -0
- package/srv/blockchain/backends/cardano-backend.d.ts +6 -3
- package/srv/blockchain/backends/koios-backend.d.ts +9 -6
- package/srv/blockchain/backends/koios-backend.js +145 -207
- package/srv/blockchain/backends/odatano-backend.d.ts +2 -1
- package/srv/blockchain/backends/odatano-backend.js +51 -9
- package/srv/blockchain/backends/ogmios-backend.d.ts +1 -2
- package/srv/blockchain/backends/ogmios-backend.js +82 -48
- package/srv/blockchain/cardano-client.d.ts +15 -5
- package/srv/blockchain/cardano-client.js +143 -71
- package/srv/blockchain/cardano-indexer.d.ts +27 -16
- package/srv/blockchain/cardano-indexer.js +223 -102
- package/srv/blockchain/circuit-breaker.d.ts +1 -0
- package/srv/blockchain/circuit-breaker.js +6 -0
- package/srv/blockchain/crawler/account-snapshots.js +22 -2
- package/srv/blockchain/crawler/asset-decimals-backfill.d.ts +2 -0
- package/srv/blockchain/crawler/asset-decimals-backfill.js +29 -29
- package/srv/blockchain/crawler/asset-registry-backfill.d.ts +3 -0
- package/srv/blockchain/crawler/asset-registry-backfill.js +4 -0
- package/srv/blockchain/crawler/asset-slice-backfill.d.ts +35 -0
- package/srv/blockchain/crawler/asset-slice-backfill.js +149 -0
- package/srv/blockchain/crawler/certificate-backfill.d.ts +4 -0
- package/srv/blockchain/crawler/certificate-backfill.js +15 -3
- package/srv/blockchain/crawler/crawler.d.ts +22 -1
- package/srv/blockchain/crawler/crawler.js +223 -24
- package/srv/blockchain/crawler/credential-backfill.d.ts +2 -0
- package/srv/blockchain/crawler/credential-backfill.js +43 -66
- package/srv/blockchain/crawler/input-asset-copies.d.ts +4 -4
- package/srv/blockchain/crawler/input-asset-copies.js +17 -27
- package/srv/blockchain/crawler/key-pages.d.ts +12 -0
- package/srv/blockchain/crawler/key-pages.js +48 -0
- package/srv/blockchain/crawler/metadata-position-backfill.d.ts +6 -0
- package/srv/blockchain/crawler/metadata-position-backfill.js +23 -19
- package/srv/blockchain/crawler/output-datum-backfill.d.ts +3 -1
- package/srv/blockchain/crawler/output-datum-backfill.js +8 -18
- package/srv/blockchain/crawler/sync-state.d.ts +5 -1
- package/srv/blockchain/crawler/sync-state.js +63 -106
- package/srv/blockchain/crawler/transaction-backfill.d.ts +2 -0
- package/srv/blockchain/crawler/transaction-backfill.js +6 -13
- package/srv/blockchain/crawler/utxo-set-import.js +2 -2
- package/srv/blockchain/crawler/window-plan.d.ts +31 -0
- package/srv/blockchain/crawler/window-plan.js +59 -0
- package/srv/blockchain/ledger-state.d.ts +2 -2
- package/srv/blockchain/ledger-state.js +139 -37
- package/srv/blockchain/signing/external-signer.js +5 -5
- package/srv/blockchain/signing/failed-recheck.d.ts +17 -0
- package/srv/blockchain/signing/failed-recheck.js +135 -0
- package/srv/blockchain/signing/landing-proof.d.ts +60 -0
- package/srv/blockchain/signing/landing-proof.js +224 -0
- package/srv/blockchain/signing/signature-verifier.js +10 -13
- package/srv/blockchain/signing/submission-finalizer.d.ts +17 -3
- package/srv/blockchain/signing/submission-finalizer.js +90 -29
- package/srv/blockchain/stored-chain.d.ts +1 -2
- package/srv/blockchain/stored-chain.js +36 -15
- package/srv/blockchain/transaction-building/buildooor-tx.js +16 -10
- package/srv/blockchain/transaction-presence.d.ts +8 -1
- package/srv/blockchain/transaction-presence.js +12 -12
- package/srv/blockchain/wallet-worker/build-request.d.ts +6 -6
- package/srv/blockchain/wallet-worker/build-request.js +35 -365
- package/srv/blockchain/wallet-worker/confirmation-tracker.d.ts +23 -2
- package/srv/blockchain/wallet-worker/confirmation-tracker.js +140 -23
- package/srv/blockchain/wallet-worker/invalidation.d.ts +35 -0
- package/srv/blockchain/wallet-worker/invalidation.js +128 -0
- package/srv/blockchain/wallet-worker/job-store.d.ts +28 -2
- package/srv/blockchain/wallet-worker/job-store.js +107 -80
- package/srv/blockchain/wallet-worker/wallet-worker.d.ts +21 -2
- package/srv/blockchain/wallet-worker/wallet-worker.js +241 -57
- package/srv/cardano-agent-service.cds +1 -1
- package/srv/cardano-agent-service.js +8 -0
- package/srv/cardano-backend-service.cds +4 -1
- package/srv/cardano-backend-service.js +26 -9
- package/srv/cardano-indexer-service.cds +448 -387
- package/srv/cardano-indexer-service.js +226 -281
- package/srv/cardano-service.cds +7 -5
- package/srv/cardano-service.js +404 -69
- package/srv/cardano-sign-service.cds +11 -1
- package/srv/cardano-sign-service.js +161 -108
- package/srv/cardano-tx-service.cds +15 -4
- package/srv/cardano-tx-service.js +105 -604
- package/srv/cardano-worker-service.cds +25 -2
- package/srv/cardano-worker-service.js +56 -4
- package/srv/config.d.ts +4 -0
- package/srv/config.js +10 -0
- package/srv/server.js +5 -7
- package/srv/utils/agent-access.d.ts +4 -2
- package/srv/utils/agent-access.js +26 -7
- package/srv/utils/agent-grants.d.ts +1 -1
- package/srv/utils/agent-grants.js +7 -8
- package/srv/utils/agent-token-auth.js +45 -5
- package/srv/utils/build-request.d.ts +52 -0
- package/srv/utils/build-request.js +487 -0
- package/srv/utils/collections.d.ts +1 -0
- package/srv/utils/collections.js +11 -0
- package/srv/utils/const.d.ts +10 -2
- package/srv/utils/const.js +13 -6
- package/srv/utils/crypto.js +5 -4
- package/srv/utils/db-indexes.d.ts +5 -1
- package/srv/utils/db-indexes.js +79 -14
- package/srv/utils/db-kind.d.ts +23 -0
- package/srv/utils/db-kind.js +42 -0
- package/srv/utils/detached-run.d.ts +41 -0
- package/srv/utils/detached-run.js +101 -0
- package/srv/utils/epoch-slots.d.ts +1 -0
- package/srv/utils/epoch-slots.js +7 -0
- package/srv/utils/error-codes.d.ts +4 -0
- package/srv/utils/error-codes.js +4 -0
- package/srv/utils/errors.d.ts +10 -1
- package/srv/utils/errors.js +51 -9
- package/srv/utils/lease.d.ts +20 -0
- package/srv/utils/lease.js +62 -0
- package/srv/utils/mappers.d.ts +9 -0
- package/srv/utils/mappers.js +57 -67
- package/srv/utils/navigation.d.ts +17 -0
- package/srv/utils/navigation.js +52 -0
- package/srv/utils/network-context.d.ts +0 -1
- package/srv/utils/network-context.js +8 -3
- package/srv/utils/pg-inline-json.js +3 -3
- package/srv/utils/pg-order-nulls.js +3 -3
- package/srv/utils/resolution.d.ts +13 -0
- package/srv/utils/resolution.js +47 -0
- package/srv/utils/row-values.d.ts +7 -0
- package/srv/utils/row-values.js +27 -0
- package/srv/utils/tx-build-helper.d.ts +15 -0
- package/srv/utils/tx-build-helper.js +28 -13
- package/srv/utils/types.d.ts +35 -43
- package/srv/utils/types.js +1 -17
- package/srv/utils/validators.d.ts +2 -2
- package/srv/utils/validators.js +14 -25
package/README.md
CHANGED
|
@@ -1,204 +1,100 @@
|
|
|
1
1
|
<p align="center">
|
|
2
2
|
<picture>
|
|
3
|
-
<source media="(prefers-color-scheme: dark)" srcset="https://cdn.jsdelivr.net/npm/@odatano/brand@1/logos/odatano-
|
|
4
|
-
<img src="https://cdn.jsdelivr.net/npm/@odatano/brand@1/logos/odatano-
|
|
3
|
+
<source media="(prefers-color-scheme: dark)" srcset="https://cdn.jsdelivr.net/npm/@odatano/brand@1/logos/odatano-logo-on-dark.svg">
|
|
4
|
+
<img src="https://cdn.jsdelivr.net/npm/@odatano/brand@1/logos/odatano-logo.svg" alt="ODATANO" height="84">
|
|
5
5
|
</picture>
|
|
6
6
|
</p>
|
|
7
7
|
|
|
8
8
|
# Enterprise OData Services for the Cardano Blockchain
|
|
9
9
|
|
|
10
|
-
Funded by [Cardano Catalyst Fund 14](https://projectcatalyst.io/funds/14/sponsored-by-leftovers/sap-cardano-odata-v4-api-with-cap-and-sap-cardano-sdk) all four milestones completed and approved (April 2026).
|
|
11
|
-
|
|
12
|
-
**ODATANO** is an SAP CAP–based service that exposes the Cardano blockchain via a standardized **OData V4** interface, enabling enterprise-grade read and write access with native transaction building.
|
|
13
|
-
|
|
14
10
|
[](https://github.com/ODATANO/ODATANO/actions/workflows/test.yaml)
|
|
15
11
|
[](https://codecov.io/gh/ODATANO/ODATANO)
|
|
16
12
|
[](https://www.npmjs.com/package/@odatano/core)
|
|
17
13
|
[](https://www.npmjs.com/package/@odatano/core)
|
|
18
14
|
[](LICENSE)
|
|
19
15
|
|
|
16
|
+
**ODATANO** (`@odatano/core`) is an SAP CAP plugin that serves the Cardano blockchain as **OData V4**:
|
|
17
|
+
read chain data, build, sign and submit transactions. Funded by
|
|
18
|
+
[Cardano Catalyst Fund 14](https://projectcatalyst.io/funds/14/sponsored-by-leftovers/sap-cardano-odata-v4-api-with-cap-and-sap-cardano-sdk).
|
|
20
19
|
|
|
21
20
|
## Quick Start
|
|
22
21
|
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
Add ODATANO to any existing SAP CAP project:
|
|
22
|
+
In an SAP CAP project:
|
|
26
23
|
|
|
27
24
|
```bash
|
|
28
25
|
npm install @odatano/core @cap-js/sqlite
|
|
29
26
|
```
|
|
30
27
|
|
|
28
|
+
Add to `package.json`:
|
|
29
|
+
|
|
31
30
|
```json
|
|
32
31
|
{
|
|
33
32
|
"cds": {
|
|
34
33
|
"requires": {
|
|
35
34
|
"db": { "kind": "sqlite" },
|
|
36
35
|
"auth": { "users": { "alice": { "roles": ["Read", "Transact", "Sign"] } } },
|
|
37
|
-
"odatano-core": {
|
|
38
|
-
"network": "preview",
|
|
39
|
-
"backends": ["koios"]
|
|
40
|
-
}
|
|
36
|
+
"odatano-core": { "network": "preview", "backends": ["koios"] }
|
|
41
37
|
}
|
|
42
38
|
}
|
|
43
39
|
}
|
|
44
40
|
```
|
|
45
41
|
|
|
46
|
-
The services ask for the roles `Read`, `Transact` and `Sign` (`Admin` for operator actions). The `auth`
|
|
47
|
-
entry gives them to CAP's development user `alice`; in production your own authentication assigns them.
|
|
48
|
-
|
|
49
|
-
```bash
|
|
50
|
-
cds deploy --to sqlite
|
|
51
|
-
cds serve
|
|
52
|
-
```
|
|
53
|
-
|
|
54
|
-
Koios works without an account on its public tier. A [Koios token](https://koios.rest) is optional and raises the rate limit. Put it in a `.env` file in the project root (loaded automatically by `cds serve` / `cds watch`), not in `package.json`:
|
|
55
|
-
|
|
56
|
-
```bash
|
|
57
|
-
# .env (optional)
|
|
58
|
-
KOIOS_API_KEY=your_koios_token
|
|
59
|
-
```
|
|
60
|
-
|
|
61
|
-
> Config in `cds.requires.odatano-core` takes precedence over environment
|
|
62
|
-
> variables; a `koiosApiKey` set there would override the `.env` value.
|
|
63
|
-
|
|
64
|
-
Other backends are optional and can be combined, for example `["ogmios", "koios"]` with a local node, or
|
|
65
|
-
`blockfrost` with a Blockfrost key or a self-hosted Blockfrost-compatible node. See the
|
|
66
|
-
[Backend Configuration Guide](docs/guides/BACKEND_CONFIGURATION.md).
|
|
67
|
-
|
|
68
|
-
### Standalone
|
|
69
|
-
|
|
70
|
-
Run ODATANO as a self-contained service (no host CAP project needed):
|
|
71
|
-
|
|
72
42
|
```bash
|
|
73
|
-
git clone https://github.com/ODATANO/ODATANO && cd ODATANO
|
|
74
|
-
npm ci
|
|
75
|
-
cp .env.example .env # set NETWORK, BACKENDS (default koios), optional KOIOS_API_KEY
|
|
76
43
|
cds deploy --to sqlite
|
|
77
44
|
cds serve
|
|
45
|
+
curl -u alice: http://localhost:4004/odata/v4/cardano/NetworkInformation
|
|
78
46
|
```
|
|
79
47
|
|
|
80
|
-
|
|
81
|
-
(
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
docker compose
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
> First sync from genesis takes hours; the node DB persists in a Docker volume so
|
|
90
|
-
> restarts resume. Until Ogmios catches up, queries fall back to Koios
|
|
91
|
-
> (optional `KOIOS_API_KEY`).
|
|
92
|
-
|
|
93
|
-
Service available at `http://localhost:4004`. See the [Quick Start Guide](docs/QUICK_START.md#local-ogmios--cardano-node-docker) for the local Ogmios stack and full details.
|
|
94
|
-
|
|
95
|
-
## Dev Mode: Mocked Authentication
|
|
96
|
-
|
|
97
|
-
> **Heads-up.** `cds serve` uses CAP's mocked auth: any Basic-Auth header logs in as the named mock user and passwords are not checked. This repository gives `alice` and `bob` the roles `Read`, `Transact` and `Sign`, `carol` only `Read`, and `admin` all four (`.cdsrc.json`); any other name is logged in without a role and gets 403. Anonymous requests get 401, but anyone reaching the port can authenticate.
|
|
98
|
-
>
|
|
99
|
-
> Production needs the `[production]` profile (`NODE_ENV=production`), which switches to `auth: xsuaa`. Don't expose a dev-mode instance. See the [Security Guide](docs/guides/SECURITY_GUIDE.md#authentication-xsuaa).
|
|
100
|
-
>
|
|
101
|
-
> The Docker image runs `NODE_ENV=production` with HTTP basic auth via `@odatano/cap-auth` (`ODATANO_HTTP_PASSWORD` required, user `ODATANO_HTTP_USER` default `odatano`, roles `ODATANO_HTTP_ROLES` default `Read,Transact,Sign,Admin`; 20 failed attempts per 15 min per client address and user, then 429). `ODATANO_AUTH=dummy` disables authentication for local testing only. `getLiveness()` and `VerifyDataSignature` stay anonymous.
|
|
102
|
-
>
|
|
103
|
-
> Database of the image: `ODATANO_DB_URL=postgres://user:pw@host:5432/db` selects PostgreSQL (schema deployed on every boot); otherwise SQLite at `ODATANO_DB_PATH` (default `/data/db.sqlite`). `docker compose run --rm --no-deps odatano migrate --from /data/db.sqlite` copies an existing SQLite file into a freshly deployed PostgreSQL.
|
|
48
|
+
- **Backend.** Koios needs no account. Ogmios with a local node, Blockfrost and another ODATANO are the
|
|
49
|
+
alternatives: [Backend Configuration](docs/guides/BACKEND_CONFIGURATION.md).
|
|
50
|
+
- **Login.** `alice` is a development user of CAP and the password is not checked, so never expose a
|
|
51
|
+
development instance. In production your own authentication assigns the roles `Read`, `Transact`, `Sign` and `Admin`:
|
|
52
|
+
[Security Guide](docs/guides/SECURITY_GUIDE.md).
|
|
53
|
+
- **Without a CAP project.** Clone this repository, or start cardano-node, Ogmios and ODATANO with
|
|
54
|
+
`ODATANO_HTTP_PASSWORD=<password> docker compose up -d`: [Quick Start](docs/QUICK_START.md),
|
|
55
|
+
[Docker Deployment](docs/guides/DOCKER_DEPLOYMENT.md).
|
|
104
56
|
|
|
105
57
|
## Services
|
|
106
58
|
|
|
107
59
|
| Service | Path | Purpose |
|
|
108
60
|
|---|---|---|
|
|
109
|
-
| CardanoODataService | `/odata/v4/cardano/` | Read
|
|
110
|
-
| CardanoTransactionService | `/odata/v4/cardano-transaction/` | Build
|
|
111
|
-
| CardanoSignService | `/odata/v4/cardano-sign/` | External signing
|
|
112
|
-
| CardanoIndexerService | `/odata/v4/cardano-indexer/` | Chain crawler
|
|
113
|
-
| CardanoWorkerService | `/odata/v4/cardano-worker/` | Asynchronous wallet jobs
|
|
114
|
-
|
|
|
115
|
-
|
|
|
116
|
-
|
|
117
|
-
The
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
```js
|
|
121
|
-
(await cds.connect.to('CardanoWorkerService')).on('jobConfirmed', ({ data }) => …)
|
|
122
|
-
(await cds.connect.to('CardanoIndexerService')).on('blockIndexed', ({ data }) => …)
|
|
123
|
-
```
|
|
124
|
-
|
|
125
|
-
## Agent grants (off by default)
|
|
126
|
-
|
|
127
|
-
Hand an agent a **scoped, budgeted token** instead of a user.
|
|
128
|
-
|
|
129
|
-
```bash
|
|
130
|
-
AGENT_GRANTS_ENABLED=true # or cds.requires.odatano-core.agentGrants.enabled
|
|
131
|
-
AGENT_GRANTS_DELEGATE=mocked # CAP auth kind that keeps authenticating non-token requests (default: configured kind)
|
|
132
|
-
AGENT_GRANT_ADMIN_RATE_LIMIT=10 # grant administration calls per principal per hour (or agentGrants.adminRateLimit)
|
|
133
|
-
AGENT_TOKEN_CACHE_MS=10000 # a resolved token is reused this long before the row is read again; 0 = off (or agentGrants.tokenCacheMs)
|
|
134
|
-
AGENT_GRANT_USAGE_RETENTION_DAYS=400 # usage counters older than this are deleted once a day; 0 = keep all (or agentGrants.usageRetentionDays)
|
|
135
|
-
```
|
|
136
|
-
|
|
137
|
-
1. An **Admin** issues a grant (`POST /odata/v4/cardano-agent/CreateAgentGrant`): an allow list of
|
|
138
|
-
actions, a daily budget and an expiry. The response carries the token `odat_…` **once**; the
|
|
139
|
-
server stores only its SHA-256.
|
|
140
|
-
2. The agent sends `x-agent-token: odat_…` on the service paths. It runs as the principal
|
|
141
|
-
`agent:<grantId>` with the single role `agent-grant`, never as the operator: every
|
|
142
|
-
`@requires: 'Admin'` surface (pause/resume, HSM signing, grant administration) refuses it by
|
|
143
|
-
construction. Reads and compute-only actions are always allowed; allow-listed actions each cost
|
|
144
|
-
one budget unit per UTC day (a build is the service an agent buys); everything else is 403.
|
|
145
|
-
An agent signs its transactions itself: the wallet worker, which signs with server-held keys,
|
|
146
|
-
answers every token request with 403.
|
|
147
|
-
3. `GET /odata/v4/cardano-agent/GetGrantStatus()` tells the agent what it may do and how much budget
|
|
148
|
-
is left; `RevokeAgentGrant` turns the token into an unknown token immediately.
|
|
149
|
-
4. Lifecycle (Admin; the same semantics as the gateway's peer services, so one client drives
|
|
150
|
-
them all): `UpdateAgentGrant` changes label, allow list, daily budget or expiry
|
|
151
|
-
(absent = untouched, explicit `null` = cleared; 409 `GRANT_REVOKED` on a revoked grant); `RotateAgentGrantToken` returns a fresh token once, the old
|
|
152
|
-
one is unknown from the next request; `GetGrantUsage(grantId, since, until)` lists admitted calls
|
|
153
|
-
per service and action over up to 366 days (an Admin for any grant, a token for its own). All
|
|
154
|
-
administration calls share the per-principal hourly rate limit above.
|
|
155
|
-
|
|
156
|
-
Grantable: `Build*`, `SetCollateral`, `CreateSigningRequest`, `VerifySignature`, `SubmitTransaction`,
|
|
157
|
-
`SubmitSignedTransaction`, `SubmitVerifiedTransaction`, `CheckSubmissionStatus`. Never: `SignWithHsm*`,
|
|
158
|
-
`SubmitWalletJob`, `CancelJob`, `Pause*`/`Resume*`, `pauseCrawler`/`resumeCrawler`, grant administration.
|
|
159
|
-
|
|
160
|
-
Programmatic seams for other packages (`@odatano/x402` sells grants against a 402 payment):
|
|
161
|
-
`issueAgentGrant()`, `updateAgentGrant()`, `rotateAgentGrantToken()`, `revokeAgentGrantById()`,
|
|
162
|
-
`getGrantUsage()`, `registerTransportLane()` from `@odatano/core`.
|
|
163
|
-
|
|
164
|
-
## Liveness and readiness probes
|
|
165
|
-
|
|
166
|
-
`GET /odata/v4/cardano-indexer/getLiveness()` answers `200 { status: "alive", timestamp, uptime,
|
|
167
|
-
version, network }` **without credentials** (`@requires: 'any'`) as long as the process runs — no
|
|
168
|
-
backend or DB probe, no secrets. The Docker `HEALTHCHECK`, compose and upstream probes use it
|
|
169
|
-
instead of the service document, so a wrong operator password no longer reads as "ODATANO down".
|
|
170
|
-
|
|
171
|
-
`GET /odata/v4/cardano-indexer/getReadiness()` answers, also without credentials, whether this
|
|
172
|
-
instance can serve requests: `200 { status: "ready", timestamp, context, database, hsm }`, or `503`
|
|
173
|
-
with the same body and `status: "not_ready"` while the app context, the database or a configured
|
|
174
|
-
HSM is missing (`hsm` is `null` without an HSM). Point a load balancer or an upstream gateway at it.
|
|
175
|
-
|
|
176
|
-
The state of crawler, worker and backends stays authenticated: `getStatus()`, `GetWorkerStatus()`,
|
|
177
|
-
`GetNetworkInformation`.
|
|
61
|
+
| CardanoODataService | `/odata/v4/cardano/` | Read chain data (33 entities, 22 actions) |
|
|
62
|
+
| CardanoTransactionService | `/odata/v4/cardano-transaction/` | Build and submit transactions, Plutus included (14 actions) |
|
|
63
|
+
| CardanoSignService | `/odata/v4/cardano-sign/` | External signing and HSM (9 actions) |
|
|
64
|
+
| CardanoIndexerService | `/odata/v4/cardano-indexer/` | Chain crawler, status and health probes |
|
|
65
|
+
| CardanoWorkerService | `/odata/v4/cardano-worker/` | Asynchronous wallet jobs |
|
|
66
|
+
| CardanoAgentService | `/odata/v4/cardano-agent/` | Scoped, budgeted tokens for agents |
|
|
67
|
+
| CardanoBackendService | `/odata/v4/cardano-backend/` | Chain data for another ODATANO that uses this one as its backend |
|
|
68
|
+
|
|
69
|
+
The crawler, the wallet worker and agent grants are off until configured. The indexer and the worker also
|
|
70
|
+
publish CAP events (`blockIndexed`, `jobConfirmed`), so a CAP application subscribes and does not poll. The
|
|
71
|
+
[User Guide](docs/guides/USER_GUIDE.md) and the [Crawler Guide](docs/guides/CRAWLER_GUIDE.md) describe all of it.
|
|
178
72
|
|
|
179
73
|
## Requirements
|
|
180
74
|
|
|
181
|
-
-
|
|
182
|
-
-
|
|
183
|
-
`CardanoReorgLog`, `CardanoWorkerWallets`, `CardanoWalletJobs`, `CardanoAgentGrants`,
|
|
184
|
-
`CardanoAgentGrantUsage`) plus a `dedupKey` column; without the redeploy the new services answer
|
|
185
|
-
`no such table` while the old ones keep working.
|
|
75
|
+
- Node.js 22.5 or later and `@sap/cds` 10 (peer dependency). A CAP 9 project cannot load `@odatano/core@2`.
|
|
76
|
+
- Coming from 1.x: [Upgrading to 2.0](docs/guides/UPGRADE_2.0.md).
|
|
186
77
|
|
|
187
78
|
## Documentation
|
|
188
79
|
|
|
189
|
-
| Guide |
|
|
190
|
-
|
|
191
|
-
| [Quick Start](docs/QUICK_START.md) |
|
|
192
|
-
| [User Guide](docs/guides/USER_GUIDE.md) |
|
|
193
|
-
| [
|
|
194
|
-
| [Transaction Workflow](docs/guides/TRANSACTION_WORKFLOW.md) | Build
|
|
195
|
-
| [
|
|
196
|
-
| [
|
|
80
|
+
| Guide | Content |
|
|
81
|
+
|---|---|
|
|
82
|
+
| [Quick Start](docs/QUICK_START.md) | Plugin, standalone and local node setup, first requests |
|
|
83
|
+
| [User Guide](docs/guides/USER_GUIDE.md) | Entities, actions, signing, wallet worker, agent grants, events |
|
|
84
|
+
| [Crawler Guide](docs/guides/CRAWLER_GUIDE.md) | Chain crawler, snapshots, backfills, UTxO set |
|
|
85
|
+
| [Transaction Workflow](docs/guides/TRANSACTION_WORKFLOW.md) | Build, sign and submit, Plutus contracts |
|
|
86
|
+
| [Backend Configuration](docs/guides/BACKEND_CONFIGURATION.md) | Koios, Ogmios, Blockfrost, another ODATANO |
|
|
87
|
+
| [Security Guide](docs/guides/SECURITY_GUIDE.md) | Authentication, roles, signing, HSM |
|
|
88
|
+
| [Docker Deployment](docs/guides/DOCKER_DEPLOYMENT.md) | Image, compose stack, database, health probes |
|
|
89
|
+
| [Production Deployment](docs/guides/PRODUCTION_DEPLOYMENT.md) | SAP BTP Cloud Foundry and HANA |
|
|
90
|
+
| [SAP Integration Examples](docs/guides/SAP_INTEGRATION_EXAMPLES.md) | Calling the services from S/4HANA and ABAP |
|
|
197
91
|
| [Upgrading to 2.0](docs/guides/UPGRADE_2.0.md) | What a 1.x deployment and its clients change |
|
|
92
|
+
| [Developer Guide](docs/guides/DEVELOPER_GUIDE.md) | Architecture and development |
|
|
93
|
+
| [Changelog](CHANGELOG.md) | Every change by version |
|
|
198
94
|
|
|
199
95
|
## License
|
|
200
96
|
|
|
201
|
-
Apache License 2.0
|
|
97
|
+
Apache License 2.0, see [LICENSE](LICENSE).
|
|
202
98
|
|
|
203
99
|
## Support
|
|
204
100
|
|
package/db/schema.cds
CHANGED
|
@@ -93,6 +93,14 @@ entity Blocks {
|
|
|
93
93
|
@title : 'Block Fees'
|
|
94
94
|
@description: 'Total fees in the block'
|
|
95
95
|
fees : Lovelace;
|
|
96
|
+
|
|
97
|
+
@title : 'Previous Block Hash'
|
|
98
|
+
@description: 'Hash of the block before this one; null when the source did not report it'
|
|
99
|
+
previousHash : String(64);
|
|
100
|
+
|
|
101
|
+
@title : 'Withdrawals Reported'
|
|
102
|
+
@description: 'True when the crawl stored the withdrawals of every transaction of this block; null for blocks stored without them'
|
|
103
|
+
withdrawalsReported : Boolean;
|
|
96
104
|
}
|
|
97
105
|
|
|
98
106
|
@title : 'Epoch Entity'
|
|
@@ -139,6 +147,10 @@ entity Epochs {
|
|
|
139
147
|
@title : 'Active Stake'
|
|
140
148
|
@description: 'Total active stake in the epoch'
|
|
141
149
|
activeStake : Lovelace;
|
|
150
|
+
|
|
151
|
+
@title : 'Final'
|
|
152
|
+
@description: 'True once the row was read after the epoch ended; a read refreshes the row while it is not'
|
|
153
|
+
final : Boolean;
|
|
142
154
|
}
|
|
143
155
|
|
|
144
156
|
@title : 'Pool Entity'
|
|
@@ -533,6 +545,10 @@ entity Assets : temporal {
|
|
|
533
545
|
@title : 'Registry Logo'
|
|
534
546
|
@description: 'CIP-26 off-chain registry: base64-encoded logo or URL'
|
|
535
547
|
registryLogo : LargeString;
|
|
548
|
+
|
|
549
|
+
@title : 'First Seen Slot'
|
|
550
|
+
@description: 'Slot of the crawled block that wrote this bare row; null for rows written otherwise. A rollback past it removes the row'
|
|
551
|
+
firstSeenSlot : Integer64;
|
|
536
552
|
}
|
|
537
553
|
|
|
538
554
|
@title : 'Asset History Entry'
|
|
@@ -783,6 +799,14 @@ entity Accounts : temporal {
|
|
|
783
799
|
@title : 'Has Addresses'
|
|
784
800
|
@description: 'Indicates if account has associated addresses'
|
|
785
801
|
hasAddresses : Boolean;
|
|
802
|
+
|
|
803
|
+
@title : 'Address Count'
|
|
804
|
+
@description: 'Number of addresses under the stake key; read them through the Address navigation'
|
|
805
|
+
addressCount : Integer;
|
|
806
|
+
|
|
807
|
+
@title : 'Address List'
|
|
808
|
+
@description: 'JSON array of the addresses under the stake key, sorted; lets a page of a large account be chosen without the backend'
|
|
809
|
+
addressList : LargeString;
|
|
786
810
|
}
|
|
787
811
|
|
|
788
812
|
@title : 'Transaction Entity'
|
|
@@ -1768,7 +1792,7 @@ entity TransactionSubmissions {
|
|
|
1768
1792
|
submittedAt : Integer64;
|
|
1769
1793
|
|
|
1770
1794
|
@title : 'Submission Status'
|
|
1771
|
-
@description: 'Current status of the transaction submission (pending, submitted, confirmed, failed,
|
|
1795
|
+
@description: 'Current status of the transaction submission (pending, submitted, confirmed, failed, unresolved)'
|
|
1772
1796
|
status : SubmissionStatus default 'pending' @readonly;
|
|
1773
1797
|
|
|
1774
1798
|
@title : 'Error Code'
|
|
@@ -1779,6 +1803,10 @@ entity TransactionSubmissions {
|
|
|
1779
1803
|
@description: 'Detailed error message if failed'
|
|
1780
1804
|
errorMessage : LargeString;
|
|
1781
1805
|
|
|
1806
|
+
@title : 'Resolution'
|
|
1807
|
+
@description: 'How an operator resolved an unresolved outcome: who, when, the outcome and the note. Null otherwise'
|
|
1808
|
+
resolution : String(500);
|
|
1809
|
+
|
|
1782
1810
|
@title : 'Retry Count'
|
|
1783
1811
|
@description: 'Number of retry attempts'
|
|
1784
1812
|
retryCount : Integer;
|
|
@@ -1969,6 +1997,10 @@ entity SigningRequests {
|
|
|
1969
1997
|
@title : 'Error Message'
|
|
1970
1998
|
@description: 'Error message if signing, verification or the submit failed'
|
|
1971
1999
|
errorMessage : LargeString;
|
|
2000
|
+
|
|
2001
|
+
@title : 'Resolution'
|
|
2002
|
+
@description: 'How an operator resolved an unresolved outcome: who, when, the outcome and the note. Null otherwise'
|
|
2003
|
+
resolution : String(500);
|
|
1972
2004
|
}
|
|
1973
2005
|
|
|
1974
2006
|
@title : 'Signature Verifications'
|
|
@@ -2117,6 +2149,10 @@ entity CardanoSyncState {
|
|
|
2117
2149
|
@title : 'Crawler Lease Expiry'
|
|
2118
2150
|
@description: 'Lease deadline renewed by the active crawler; another instance may take over only after this time'
|
|
2119
2151
|
leaseUntil : Timestamp;
|
|
2152
|
+
|
|
2153
|
+
@title : 'Node Behind Since'
|
|
2154
|
+
@description: 'Since when the node behind chain-sync is behind the cursor and its rollback was refused; null while it follows'
|
|
2155
|
+
nodeBehindSince : Timestamp;
|
|
2120
2156
|
}
|
|
2121
2157
|
|
|
2122
2158
|
@title : 'Cardano Reorg Log'
|
|
@@ -2213,11 +2249,8 @@ entity CardanoWorkerWallets {
|
|
|
2213
2249
|
@description: 'Asynchronous transaction jobs executed by the wallet worker (build → sign → submit → confirm)'
|
|
2214
2250
|
// The database enforces idempotency, since two concurrent retries can both find no job.
|
|
2215
2251
|
// The unique constraint makes one insert lose, and insertJob returns the winner's job.
|
|
2216
|
-
|
|
2217
|
-
|
|
2218
|
-
kind,
|
|
2219
|
-
dedupKey
|
|
2220
|
-
]
|
|
2252
|
+
// dedupKey is scoped to the user, so one key names one job across all wallets and kinds.
|
|
2253
|
+
@assert.unique.dedup: [dedupKey]
|
|
2221
2254
|
entity CardanoWalletJobs {
|
|
2222
2255
|
|
|
2223
2256
|
@title : 'Job Id (Key)'
|
|
@@ -2233,15 +2266,15 @@ entity CardanoWalletJobs {
|
|
|
2233
2266
|
kind : WalletJobKind;
|
|
2234
2267
|
|
|
2235
2268
|
@title : 'Status'
|
|
2236
|
-
@description: 'Job lifecycle state (pending | building | submitting | submitted | confirmed | failed | cancelled)'
|
|
2269
|
+
@description: 'Job lifecycle state (pending | building | submitting | submitted | confirmed | failed | cancelled | unresolved)'
|
|
2237
2270
|
status : WalletJobStatus default 'pending';
|
|
2238
2271
|
|
|
2239
2272
|
@title : 'Idempotency Key'
|
|
2240
|
-
@description: 'Optional caller-supplied dedup key:
|
|
2273
|
+
@description: 'Optional caller-supplied dedup key: per user, one key names one job across all wallets and kinds. A retry with the same request gets that job back, another request under the key is refused with 409'
|
|
2241
2274
|
idempotencyKey : String(100);
|
|
2242
2275
|
|
|
2243
2276
|
@title : 'Dedup Key (internal)'
|
|
2244
|
-
@description: 'Backing value of the unique
|
|
2277
|
+
@description: 'Backing value of the unique dedupKey constraint: the idempotency key hashed with the user while the job holds it, otherwise the job ID (unique by construction). Keyless jobs and terminated jobs therefore never collide, with no reliance on NULL semantics, which differ across databases.'
|
|
2245
2278
|
dedupKey : String(100);
|
|
2246
2279
|
|
|
2247
2280
|
@title : 'Request'
|
|
@@ -2313,8 +2346,28 @@ entity CardanoWalletJobs {
|
|
|
2313
2346
|
createdBy : String(100);
|
|
2314
2347
|
|
|
2315
2348
|
@title : 'Finished At'
|
|
2316
|
-
@description: 'Timestamp the job reached a terminal state (confirmed | failed | cancelled)'
|
|
2349
|
+
@description: 'Timestamp the job reached a terminal state (confirmed | failed | cancelled | unresolved)'
|
|
2317
2350
|
finishedAt : Timestamp;
|
|
2351
|
+
|
|
2352
|
+
@title : 'Resolution'
|
|
2353
|
+
@description: 'How an operator resolved an unresolved outcome: who, when, the outcome and the note. Null otherwise'
|
|
2354
|
+
resolution : String(500);
|
|
2355
|
+
|
|
2356
|
+
@title : 'Invalidation Tx Hash'
|
|
2357
|
+
@description: 'Hash of the self-transfer that spends an input of a transaction without an expiry, so that transaction can never land. Null otherwise'
|
|
2358
|
+
invalidationTxHash : String(64);
|
|
2359
|
+
|
|
2360
|
+
@title : 'Invalidation Signed Tx CBOR'
|
|
2361
|
+
@description: 'Signed bytes of that self-transfer, stored before it is sent'
|
|
2362
|
+
invalidationTxCbor : LargeString;
|
|
2363
|
+
|
|
2364
|
+
@title : 'Invalidation Sent At'
|
|
2365
|
+
@description: 'When that self-transfer was stored and sent'
|
|
2366
|
+
invalidationSentAt : Timestamp;
|
|
2367
|
+
|
|
2368
|
+
@title : 'Spends No Wallet UTxO'
|
|
2369
|
+
@description: 'True when the job transaction has no expiry, has not landed and spends no UTxO the wallet holds. It cannot take the wallet funds, so it does not hold the wallet queue'
|
|
2370
|
+
spendsNoWalletUtxo : Boolean;
|
|
2318
2371
|
}
|
|
2319
2372
|
|
|
2320
2373
|
// Agent grants: the token is stored as SHA-256 only; a request carrying it runs as `agent:<ID>`.
|
package/db/types.cds
CHANGED
|
@@ -136,6 +136,7 @@ type SubmissionStatus : String(20) enum {
|
|
|
136
136
|
submitted = 'submitted';
|
|
137
137
|
confirmed = 'confirmed';
|
|
138
138
|
failed = 'failed';
|
|
139
|
+
unresolved = 'unresolved'; // past the last moment it could land, nothing shows whether it did
|
|
139
140
|
}
|
|
140
141
|
|
|
141
142
|
@title : 'Signing Request Status'
|
|
@@ -148,6 +149,7 @@ type SigningStatus : String(20) enum {
|
|
|
148
149
|
submitted = 'submitted';
|
|
149
150
|
expired = 'expired';
|
|
150
151
|
failed = 'failed';
|
|
152
|
+
unresolved = 'unresolved'; // past the last moment it could land, nothing shows whether it did
|
|
151
153
|
}
|
|
152
154
|
|
|
153
155
|
@title : 'Crawl Sync Status'
|
|
@@ -178,6 +180,14 @@ type WalletJobStatus : String(12) enum {
|
|
|
178
180
|
confirmed = 'confirmed'; // on-chain at the configured confirmation depth
|
|
179
181
|
failed = 'failed'; // terminal failure (see errorCode/errorMessage)
|
|
180
182
|
cancelled = 'cancelled'; // cancelled by the caller while still pending
|
|
183
|
+
unresolved = 'unresolved'; // past the last moment it could land, nothing shows whether it did; an operator resolves it
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
@title : 'Resolved Outcome'
|
|
187
|
+
@description: 'What an operator read on the chain for an unresolved outcome'
|
|
188
|
+
type ResolvedOutcome : String(10) enum {
|
|
189
|
+
confirmed = 'confirmed'; // the transaction is on chain with passing scripts
|
|
190
|
+
failed = 'failed'; // the transaction is not on chain and can no longer land
|
|
181
191
|
}
|
|
182
192
|
|
|
183
193
|
@title : 'Wallet Job Kind'
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@odatano/core",
|
|
3
|
-
"version": "2.0.0-rc.
|
|
3
|
+
"version": "2.0.0-rc.51",
|
|
4
4
|
"description": "SAP CAP plugin for Cardano blockchain OData V4 integration",
|
|
5
5
|
"imports": {
|
|
6
6
|
"#cds-models/*": "./@cds-models/*/index.js"
|
|
@@ -17,6 +17,7 @@
|
|
|
17
17
|
"db/**/*.cds",
|
|
18
18
|
"@cds-models/",
|
|
19
19
|
"scripts/migrate-schema.mjs",
|
|
20
|
+
"scripts/deploy-schema.mjs",
|
|
20
21
|
"scripts/migrate-slot-leader.mjs"
|
|
21
22
|
],
|
|
22
23
|
"engines": {
|
|
@@ -27,7 +28,6 @@
|
|
|
27
28
|
},
|
|
28
29
|
"dependencies": {
|
|
29
30
|
"@blockfrost/blockfrost-js": "~6.2.0",
|
|
30
|
-
"@cap-js/hana": "^3",
|
|
31
31
|
"@cardano-ogmios/client": "^7.0.0",
|
|
32
32
|
"@harmoniclabs/buildooor": "0.3.0",
|
|
33
33
|
"@harmoniclabs/cardano-costmodels-ts": "~1.7.0",
|
|
@@ -37,8 +37,7 @@
|
|
|
37
37
|
"@harmoniclabs/plutus-data": "^2.1.1",
|
|
38
38
|
"@harmoniclabs/uint8array-utils": "^1.0.4",
|
|
39
39
|
"@harmoniclabs/uplc": "^2.0.8",
|
|
40
|
-
"@odatano/cap-auth": "^0.
|
|
41
|
-
"@sap/xssec": "^4",
|
|
40
|
+
"@odatano/cap-auth": "^0.2.0",
|
|
42
41
|
"axios": "^1.18.0",
|
|
43
42
|
"bech32": "^2.0.0",
|
|
44
43
|
"blake2b": "^2.1.4"
|
|
@@ -60,11 +59,13 @@
|
|
|
60
59
|
"@cap-js/cds-test": "^0.4.1",
|
|
61
60
|
"@cap-js/cds-typer": "^0.40.0",
|
|
62
61
|
"@cap-js/cds-types": "^0.18.0",
|
|
62
|
+
"@cap-js/hana": "^3",
|
|
63
63
|
"@cap-js/postgres": "^3.1.1",
|
|
64
64
|
"@cap-js/sqlite": "^3",
|
|
65
65
|
"@sap/cds": "^10",
|
|
66
66
|
"@sap/cds-dk": "^10",
|
|
67
67
|
"@sap/eslint-plugin-cds": "^4.2.4",
|
|
68
|
+
"@sap/xssec": "^4",
|
|
68
69
|
"@types/blake2b": "^2.1.3",
|
|
69
70
|
"@types/node": "^22.19.7",
|
|
70
71
|
"@typescript-eslint/eslint-plugin": "^8.50.1",
|
|
@@ -73,6 +74,7 @@
|
|
|
73
74
|
"dotenv": "^16.3.1",
|
|
74
75
|
"eslint": "^9.39.2",
|
|
75
76
|
"nock": "^13.5.6",
|
|
77
|
+
"semver": "^7.8.6",
|
|
76
78
|
"ts-node": "^10.9.2",
|
|
77
79
|
"tsx": "^4.21.0",
|
|
78
80
|
"typescript": "^5.9.3",
|
|
@@ -90,45 +92,6 @@
|
|
|
90
92
|
"/secret/i",
|
|
91
93
|
"/project_id/i"
|
|
92
94
|
]
|
|
93
|
-
},
|
|
94
|
-
"typer": {
|
|
95
|
-
"ieee754compatible": true
|
|
96
|
-
},
|
|
97
|
-
"build": {
|
|
98
|
-
"tasks": [
|
|
99
|
-
{
|
|
100
|
-
"for": "node-cf",
|
|
101
|
-
"src": "srv",
|
|
102
|
-
"dest": "../gen/srv"
|
|
103
|
-
},
|
|
104
|
-
{
|
|
105
|
-
"for": "hana",
|
|
106
|
-
"src": "db",
|
|
107
|
-
"dest": "../gen/db"
|
|
108
|
-
}
|
|
109
|
-
]
|
|
110
|
-
},
|
|
111
|
-
"requires": {
|
|
112
|
-
"db": {
|
|
113
|
-
"kind": "sqlite",
|
|
114
|
-
"credentials": {
|
|
115
|
-
"url": "db.sqlite"
|
|
116
|
-
},
|
|
117
|
-
"client": {
|
|
118
|
-
"timeout": 5000
|
|
119
|
-
}
|
|
120
|
-
},
|
|
121
|
-
"[production]": {
|
|
122
|
-
"db": {
|
|
123
|
-
"kind": "hana",
|
|
124
|
-
"pool": {
|
|
125
|
-
"acquireTimeoutMillis": 30000,
|
|
126
|
-
"min": 0,
|
|
127
|
-
"max": 10
|
|
128
|
-
}
|
|
129
|
-
},
|
|
130
|
-
"auth": "xsuaa"
|
|
131
|
-
}
|
|
132
95
|
}
|
|
133
96
|
},
|
|
134
97
|
"scripts": {
|
|
@@ -141,7 +104,7 @@
|
|
|
141
104
|
"start": "cds-serve",
|
|
142
105
|
"cds:watch": "cds watch",
|
|
143
106
|
"cds:serve": "cds serve --with-mocks",
|
|
144
|
-
"cds:types": "cds-typer \"*\" --outputDirectory @cds-models",
|
|
107
|
+
"cds:types": "cds-typer \"*\" --outputDirectory @cds-models --ieee754compatible true",
|
|
145
108
|
"lint": "eslint .",
|
|
146
109
|
"typecheck": "tsc --noEmit",
|
|
147
110
|
"db:deploy": "cds deploy",
|
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
// Brings the configured database to the schema of this version: first the changes the additive
|
|
3
|
+
// deploy refuses (migrate-schema.mjs), then the deploy itself, which keeps every row.
|
|
4
|
+
// The upgrade command of a plugin project and of the Docker image. --dry-run only reports.
|
|
5
|
+
import cds from '@sap/cds';
|
|
6
|
+
import { existsSync } from 'node:fs';
|
|
7
|
+
import path from 'node:path';
|
|
8
|
+
import { assertDeployable, migrateSchema } from './migrate-schema.mjs';
|
|
9
|
+
|
|
10
|
+
const print = (line) => process.stdout.write(`schema migration: ${line}\n`);
|
|
11
|
+
|
|
12
|
+
/** Migrates, then deploys with schema evolution. Without it CAP drops and recreates every SQLite table. */
|
|
13
|
+
export async function deploySchema(db, { dryRun = false, log = print } = {}) {
|
|
14
|
+
return withDeployLock(db, log, async () => {
|
|
15
|
+
const ran = await migrateSchema(db, { dryRun, log });
|
|
16
|
+
if (dryRun) return ran;
|
|
17
|
+
await assertDeployable(db);
|
|
18
|
+
await cds.deploy('*', { schema_evolution: 'auto' }).to(db);
|
|
19
|
+
return ran;
|
|
20
|
+
});
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
// Two instances starting together on PostgreSQL would run the steps and the deploy at the same time,
|
|
24
|
+
// and one fails. A session lock on a connection of its own makes the second wait until the first is done.
|
|
25
|
+
async function withDeployLock(db, log, run) {
|
|
26
|
+
const factory = db.kind === 'postgres' ? db.factory : null;
|
|
27
|
+
if (!factory) return run();
|
|
28
|
+
const dbc = await factory.create();
|
|
29
|
+
try {
|
|
30
|
+
const schema = db.options?.credentials?.schema;
|
|
31
|
+
if (schema) await dbc.query(`SET search_path TO "${String(schema).replace(/"/g, '""')}", public`);
|
|
32
|
+
const key = "hashtext('odatano_deploy_schema:' || current_schema())";
|
|
33
|
+
const { rows } = await dbc.query(`SELECT pg_try_advisory_lock(${key}) AS got`);
|
|
34
|
+
if (!rows[0]?.got) {
|
|
35
|
+
log('another instance deploys the schema; waiting for it');
|
|
36
|
+
await dbc.query(`SELECT pg_advisory_lock(${key})`);
|
|
37
|
+
}
|
|
38
|
+
try {
|
|
39
|
+
return await run();
|
|
40
|
+
} finally {
|
|
41
|
+
await dbc.query(`SELECT pg_advisory_unlock(${key})`);
|
|
42
|
+
}
|
|
43
|
+
} finally {
|
|
44
|
+
await factory.destroy(dbc);
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/** The SQLite file the configured database names, or null when it is not a file on disk. */
|
|
49
|
+
function sqliteFile() {
|
|
50
|
+
const db = cds.env.requires.db ?? {};
|
|
51
|
+
const url = db.credentials?.url ?? db.credentials?.database;
|
|
52
|
+
if (!['sqlite', 'better-sqlite'].includes(db.kind) || !url || url === ':memory:') return null;
|
|
53
|
+
return path.resolve(cds.root, url);
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/** True when the error says the table does not exist; any other error is not an answer. */
|
|
57
|
+
export function isMissingTable(err) {
|
|
58
|
+
const e = err ?? {};
|
|
59
|
+
if (e.code === '42P01') return true;
|
|
60
|
+
return /no such table/i.test(String(e.message ?? ''));
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/** True when the database holds no table of this model yet. A failed connect or login is thrown. */
|
|
64
|
+
async function isEmpty(db) {
|
|
65
|
+
try {
|
|
66
|
+
await db.run('SELECT 1 FROM odatano_cardano_Blocks WHERE 1 = 0');
|
|
67
|
+
return false;
|
|
68
|
+
} catch (err) {
|
|
69
|
+
if (isMissingTable(err)) return true;
|
|
70
|
+
throw err;
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
async function main() {
|
|
75
|
+
const unknown = process.argv.slice(2).filter((a) => a !== '--dry-run');
|
|
76
|
+
if (unknown.length) throw new Error(`unknown argument ${unknown.join(' ')}; the only option is --dry-run`);
|
|
77
|
+
await cds.plugins;
|
|
78
|
+
const dryRun = process.argv.includes('--dry-run');
|
|
79
|
+
// connecting creates a missing SQLite file, which a dry run must not do
|
|
80
|
+
const file = sqliteFile();
|
|
81
|
+
if (dryRun && file && !existsSync(file)) {
|
|
82
|
+
console.log(`Dry run: ${file} does not exist. A deploy creates it with the schema of this version.`);
|
|
83
|
+
return;
|
|
84
|
+
}
|
|
85
|
+
cds.db = await cds.connect.to('db');
|
|
86
|
+
try {
|
|
87
|
+
if (dryRun && await isEmpty(cds.db)) {
|
|
88
|
+
console.log('Dry run: the database is empty. A deploy creates the schema of this version; nothing changed.');
|
|
89
|
+
return;
|
|
90
|
+
}
|
|
91
|
+
const ran = await deploySchema(cds.db, { dryRun });
|
|
92
|
+
if (dryRun) {
|
|
93
|
+
console.log(ran.length ? 'Dry run: the steps above are due, then the deploy; nothing changed.'
|
|
94
|
+
: 'Dry run: no migration step is due. A deploy still adds what this version adds; nothing changed.');
|
|
95
|
+
}
|
|
96
|
+
} finally {
|
|
97
|
+
await cds.db?.disconnect?.();
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/** The command line: deploys to the configured database, exits with 1 on a failure. */
|
|
102
|
+
export async function runDeploySchemaCli() {
|
|
103
|
+
try {
|
|
104
|
+
await main();
|
|
105
|
+
process.exit(0);
|
|
106
|
+
} catch (err) {
|
|
107
|
+
process.stderr.write(`FATAL: schema deploy failed: ${err instanceof Error ? err.stack : String(err)}\n`);
|
|
108
|
+
process.exit(1);
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
// Replaces @sap/cds/bin/deploy.js, which prints the credentials.
|
|
113
|
+
if (process.argv[1] && import.meta.url === (await import('node:url')).pathToFileURL(process.argv[1]).href) await runDeploySchemaCli();
|