Skip to content

Commit dd2170b

Browse files
authored
Use cbor2pure by default (#475)
* Use cbor2pure by default * Fix workflow and test * Address comments * Remove cbor module from public * Remove cbor2 module to cbor to avoid name conflict * fix linter
1 parent 40e18d5 commit dd2170b

17 files changed

Lines changed: 48 additions & 77 deletions

File tree

.github/workflows/main.yml

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,6 @@ jobs:
2828
- name: Install dependencies
2929
run: |
3030
poetry install
31-
- name: Ensure pure cbor2 is installed
32-
run: |
33-
make ensure-pure-cbor2
3431
- name: Run unit tests
3532
run: |
3633
poetry run pytest --doctest-modules --ignore=examples --cov=pycardano --cov-config=.coveragerc --cov-report=xml

.github/workflows/publish.yml

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,6 @@ jobs:
2424
- name: Install dependencies
2525
run: |
2626
poetry install
27-
- name: Ensure pure cbor2 is installed
28-
run: |
29-
make ensure-pure-cbor2
3027
- name: Lint with flake8
3128
run: |
3229
poetry run flake8 pycardano

Makefile

Lines changed: 4 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -23,25 +23,10 @@ export PRINT_HELP_PYSCRIPT
2323

2424
BROWSER := poetry run python -c "$$BROWSER_PYSCRIPT"
2525

26-
ensure-pure-cbor2: ## ensures cbor2 is installed with pure Python implementation
27-
@poetry run python -c "from importlib.metadata import version; \
28-
print(version('cbor2'))" > .cbor2_version
29-
@poetry run python -c "import cbor2, inspect; \
30-
print('Checking cbor2 implementation...'); \
31-
decoder_path = inspect.getfile(cbor2.CBORDecoder); \
32-
using_c_ext = decoder_path.endswith('.so'); \
33-
print(f'Implementation path: {decoder_path}'); \
34-
print(f'Using C extension: {using_c_ext}'); \
35-
exit(1 if using_c_ext else 0)" || \
36-
(echo "Reinstalling cbor2 with pure Python implementation..." && \
37-
poetry run pip uninstall -y cbor2 && \
38-
CBOR2_BUILD_C_EXTENSION=0 poetry run pip install --no-binary cbor2 "cbor2==$$(cat .cbor2_version)" --force-reinstall && \
39-
rm .cbor2_version)
40-
4126
help:
4227
@python -c "$$PRINT_HELP_PYSCRIPT" < $(MAKEFILE_LIST)
4328

44-
cov: ensure-pure-cbor2 ## check code coverage
29+
cov: ## check code coverage
4530
poetry run pytest -n 4 --cov pycardano
4631

4732
cov-html: cov ## check code coverage and generate an html report
@@ -69,7 +54,7 @@ clean-test: ## remove test and coverage artifacts
6954
rm -fr cov_html/
7055
rm -fr .pytest_cache
7156

72-
test: ensure-pure-cbor2 ## runs tests
57+
test: ## runs tests
7358
poetry run pytest -vv -n 4
7459

7560
test-integration: ## runs integration tests
@@ -78,7 +63,7 @@ test-integration: ## runs integration tests
7863
test-single: ## runs tests with "single" markers
7964
poetry run pytest -s -vv -m single
8065

81-
qa: ensure-pure-cbor2 ## runs static analyses
66+
qa: ## runs static analyses
8267
poetry run flake8 pycardano
8368
poetry run mypy --install-types --non-interactive pycardano
8469
poetry run black --check .
@@ -92,6 +77,6 @@ docs: ## build the documentation
9277
poetry run sphinx-build docs/source docs/build/html
9378
$(BROWSER) docs/build/html/index.html
9479

95-
release: clean qa test format ensure-pure-cbor2 ## build dist version and release to pypi
80+
release: clean qa test format ## build dist version and release to pypi
9681
poetry build
9782
poetry publish

README.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,11 +57,13 @@ Install the library using [pip](https://pip.pypa.io/en/stable/):
5757

5858
`pip install pycardano`
5959

60-
#### Install cbor2 pure python implementation (Optional)
60+
#### cbor2
6161
[cbor2](https://github.com/agronholm/cbor2/tree/master) is a dependency of pycardano. It is used to encode and decode CBOR data.
6262
It has two implementations: one is pure Python and the other is C, which is installed by default. The C implementation is faster, but it is less flexible than the pure Python implementation.
6363

64-
For some users, the C implementation may not work properly when deserializing cbor data. For example, the order of inputs of a transaction isn't guaranteed to be the same as the order of inputs in the original transaction (details could be found in [this issue](https://github.com/Python-Cardano/pycardano/issues/311)). This would result in a different transaction hash when the transaction is serialized again. For users who encounter this issue, we recommend to use the pure Python implementation of cbor2. You can do so by running [ensure_pure_cbor2.sh](./ensure_pure_cbor2.sh), which inspect the cbor2 installed in the running environment and force install pure python implementation if necessary.
64+
For some users, the C implementation may not work properly when deserializing cbor data. For example, the order of inputs of a transaction isn't guaranteed to be the same as the order of inputs in the original transaction (details could be found in [this issue](https://github.com/Python-Cardano/pycardano/issues/311)). This would result in a different transaction hash when the transaction is serialized again.
65+
66+
To solve this problem, a fork of cbor2 is created at [cbor2pure](https://github.com/cffls/cbor2pure). This fork removes C extension and only uses pure python for cbor decoding. By default, for correctness, pycardano uses cbor2pure in decoding. However, if speed is preferred over accuracy, users can set `CBOR_C_EXTENSION=1` in their environment, and the default C extension would be used instead.
6567

6668
```bash
6769
ensure_pure_cbor2.sh

ensure_pure_cbor2.sh

Lines changed: 0 additions & 38 deletions
This file was deleted.

integration-test/run_tests.sh

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ set -o pipefail
66
ROOT=$(pwd)
77

88
poetry install -C ..
9-
make ensure-pure-cbor2 -f ../Makefile
109
#poetry run pip install ogmios
1110

1211
##########

poetry.lock

Lines changed: 16 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pycardano/address.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@
1414
from typing import Optional, Type, Union
1515

1616
import base58
17-
import cbor2
1817
from cbor2 import CBORTag
1918
from typing_extensions import override
2019

20+
from pycardano.cbor import cbor2
2121
from pycardano.crypto.bech32 import decode, encode
2222
from pycardano.exception import (
2323
DecodingException,

pycardano/backend/blockfrost.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
from fractions import Fraction
66
from typing import Dict, List, Optional, Union
77

8-
import cbor2
98
from blockfrost import ApiError, ApiUrls, BlockFrostApi
109
from blockfrost.utils import Namespace
1110

@@ -16,6 +15,7 @@
1615
GenesisParameters,
1716
ProtocolParameters,
1817
)
18+
from pycardano.cbor import cbor2
1919
from pycardano.exception import TransactionFailedException
2020
from pycardano.hash import SCRIPT_HASH_SIZE, DatumHash, ScriptHash
2121
from pycardano.nativescript import NativeScript

pycardano/backend/cardano_cli.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
from pathlib import Path
1313
from typing import Dict, List, Optional, Union
1414

15-
import cbor2
1615
import docker
1716
from cachetools import Cache, LRUCache, TTLCache, func
1817
from docker.errors import APIError
@@ -24,6 +23,7 @@
2423
GenesisParameters,
2524
ProtocolParameters,
2625
)
26+
from pycardano.cbor import cbor2
2727
from pycardano.exception import (
2828
CardanoCliError,
2929
PyCardanoException,

0 commit comments

Comments
 (0)