Commit 3f614c3
committed
Fix serialization of (Plutus) 'Data'
The Cardano ledger does not enforce a single serialization format for
most of the binary data that can end up on-chain. In CBOR,
data-structures like lists or maps can be encoded in two ways: finite
(using an explicit length) or indefinite (using begin/end markers).
For example, the list [14] can be encoded either as:
```
8114 # finite; 81 = 80 + 01, indicates a list of length 1
# OR
9F14FF # indefinite; 9F marks the beginning of the list and FF the end).
```
This means that, a same Haskell (or simply, unmarshalled) data-type
can have multiple binary representations. The ledger copes with that
by memoizing the original binary representation of decoded data,
therefore avoiding to reserialize any data into a _different_
representation. Ogmios, and basically any program manipulating binary
data from the chain MUST also use the same strategy.
There is an example case with the following transaction on the
testnet:
<https://testnet.cexplorer.io/tx/c8b91a27976836f5ba349275bba3f0b81eb1d51aaf31a4340035ce450fdf83a7>
which carries the following datum (<https://testnet.cexplorer.io/datum/06269f665176dc65b334d685b2f64826c092875ca77e15007b5f690ecc9db1af>)
```
D8798441FFD87982D87982D87982D87981581CC279A3FB3B4E62BBC78E2887
83B58045D4AE82A18867D8352D02775AD87981D87981D87981581C121FD22E
0B57AC206FEFC763F8BFA0771919F5218B40691EEA4514D0D87A80D87A801A
002625A0D87983D879801A000F4240D879811A000FA92E
```
Now, the datum reported by CExplorer (using cardano-db-sync) and
Ogmios (before this commit) is:
```
D8799F41FFD8799FD8799FD8799FD8799F581CC279A3FB3B4E62BBC78E2887
83B58045D4AE82A18867D8352D02775AFFD8799FD8799FD8799F581C121FD2
2E0B57AC206FEFC763F8BFA0771919F5218B40691EEA4514D0FFFFFFFFD87A
80FFD87A80FF1A002625A0D8799FD879801A000F4240D8799F1A000FA92EFF
FFFF
```
Both binary representation deserializes to the same high-level
data-type, but the former uses finite data-structure in binary,
whereas the latter uses indefinite structures.
If one tries to re-calculate the hash digest of the reported datum,
one obtains (obviously) something different than expected by the
ledger. That's because this raw datum has been re-serialized by the
programs consuming it from the chain whereas the hash calculated and
used by the ledger is therefore the one corresponding to the original
(finite data-structures) datum!
This is tricky to reason about and also tricky to discover because,
even property tests couldn't catch that (since the ledger's arbitrary
instances do generate data only in the indefinite form!).
Given the output of CExplorer, it seems that cardano-db-sync is also
affected by this bug, and possibly more client applications down the
line. Moreover, hardware devices are serializing all data using
finite structures, creating this discrepency for applications
interacting / using hardware integration.1 parent 9c8ef1b commit 3f614c3
32 files changed
Lines changed: 94 additions & 40 deletions
File tree
- server
- src/Ogmios/Data/Json
- test
- unit
- Ogmios/Data
- Test
- vectors
- ChainSync/Response/RequestNext
- TxMonitor/Response/NextTx
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
137 | 137 | | |
138 | 138 | | |
139 | 139 | | |
| 140 | + | |
140 | 141 | | |
141 | 142 | | |
142 | 143 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
12 | 12 | | |
13 | 13 | | |
14 | 14 | | |
15 | | - | |
16 | | - | |
17 | 15 | | |
18 | 16 | | |
19 | 17 | | |
| |||
200 | 198 | | |
201 | 199 | | |
202 | 200 | | |
203 | | - | |
| 201 | + | |
| 202 | + | |
204 | 203 | | |
205 | | - | |
206 | | - | |
| 204 | + | |
| 205 | + | |
207 | 206 | | |
208 | 207 | | |
209 | 208 | | |
| |||
381 | 380 | | |
382 | 381 | | |
383 | 382 | | |
384 | | - | |
| 383 | + | |
385 | 384 | | |
386 | 385 | | |
387 | 386 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
140 | 140 | | |
141 | 141 | | |
142 | 142 | | |
| 143 | + | |
| 144 | + | |
143 | 145 | | |
144 | 146 | | |
145 | 147 | | |
| |||
154 | 156 | | |
155 | 157 | | |
156 | 158 | | |
| 159 | + | |
157 | 160 | | |
158 | 161 | | |
159 | 162 | | |
| |||
210 | 213 | | |
211 | 214 | | |
212 | 215 | | |
| 216 | + | |
213 | 217 | | |
214 | 218 | | |
215 | 219 | | |
| |||
226 | 230 | | |
227 | 231 | | |
228 | 232 | | |
| 233 | + | |
229 | 234 | | |
230 | 235 | | |
231 | 236 | | |
232 | 237 | | |
233 | 238 | | |
| 239 | + | |
| 240 | + | |
234 | 241 | | |
235 | 242 | | |
236 | | - | |
| 243 | + | |
237 | 244 | | |
238 | 245 | | |
239 | | - | |
| 246 | + | |
240 | 247 | | |
241 | 248 | | |
242 | 249 | | |
| |||
251 | 258 | | |
252 | 259 | | |
253 | 260 | | |
254 | | - | |
| 261 | + | |
255 | 262 | | |
256 | 263 | | |
257 | 264 | | |
| |||
268 | 275 | | |
269 | 276 | | |
270 | 277 | | |
271 | | - | |
| 278 | + | |
272 | 279 | | |
273 | 280 | | |
274 | 281 | | |
| |||
312 | 319 | | |
313 | 320 | | |
314 | 321 | | |
315 | | - | |
316 | 322 | | |
317 | 323 | | |
318 | 324 | | |
| |||
355 | 361 | | |
356 | 362 | | |
357 | 363 | | |
| 364 | + | |
| 365 | + | |
| 366 | + | |
| 367 | + | |
| 368 | + | |
| 369 | + | |
| 370 | + | |
| 371 | + | |
| 372 | + | |
| 373 | + | |
| 374 | + | |
| 375 | + | |
| 376 | + | |
| 377 | + | |
| 378 | + | |
358 | 379 | | |
359 | 380 | | |
360 | 381 | | |
| |||
862 | 883 | | |
863 | 884 | | |
864 | 885 | | |
| 886 | + | |
| 887 | + | |
| 888 | + | |
| 889 | + | |
| 890 | + | |
| 891 | + | |
| 892 | + | |
| 893 | + | |
| 894 | + | |
| 895 | + | |
| 896 | + | |
| 897 | + | |
| 898 | + | |
| 899 | + | |
| 900 | + | |
| 901 | + | |
| 902 | + | |
| 903 | + | |
| 904 | + | |
| 905 | + | |
| 906 | + | |
| 907 | + | |
| 908 | + | |
| 909 | + | |
| 910 | + | |
865 | 911 | | |
866 | 912 | | |
867 | 913 | | |
| |||
911 | 957 | | |
912 | 958 | | |
913 | 959 | | |
914 | | - | |
| 960 | + | |
915 | 961 | | |
916 | 962 | | |
917 | | - | |
| 963 | + | |
918 | 964 | | |
919 | 965 | | |
920 | 966 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
9 | 9 | | |
10 | 10 | | |
11 | 11 | | |
| 12 | + | |
| 13 | + | |
12 | 14 | | |
13 | 15 | | |
14 | 16 | | |
| |||
724 | 726 | | |
725 | 727 | | |
726 | 728 | | |
| 729 | + | |
| 730 | + | |
| 731 | + | |
| 732 | + | |
| 733 | + | |
727 | 734 | | |
728 | 735 | | |
729 | 736 | | |
| |||
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
0 commit comments