Skip to content

Commit aa0f36e

Browse files
committed
Address comments
1 parent 75d054d commit aa0f36e

11 files changed

Lines changed: 25 additions & 16 deletions

File tree

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ It has two implementations: one is pure Python and the other is C, which is inst
6363

6464
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.
6565

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_EXETENSION=1` in their environment, and the default C extension would be used instead.
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.
6767

6868
```bash
6969
ensure_pure_cbor2.sh

pycardano/__init__.py

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,5 @@
11
# flake8: noqa
22

3-
import os
4-
5-
if os.getenv("CBOR_C_EXETENSION", "0") == "1":
6-
import cbor2
7-
else:
8-
import cbor2pure as cbor2 # type: ignore[no-redef]
9-
103
from .address import *
114
from .backend import *
125
from .certificate import *

pycardano/address.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
from cbor2 import CBORTag
1818
from typing_extensions import override
1919

20-
from pycardano import cbor2
20+
from pycardano.cbor2 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
@@ -8,14 +8,14 @@
88
from blockfrost import ApiError, ApiUrls, BlockFrostApi
99
from blockfrost.utils import Namespace
1010

11-
from pycardano import cbor2
1211
from pycardano.address import Address
1312
from pycardano.backend.base import (
1413
ALONZO_COINS_PER_UTXO_WORD,
1514
ChainContext,
1615
GenesisParameters,
1716
ProtocolParameters,
1817
)
18+
from pycardano.cbor2 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
@@ -16,14 +16,14 @@
1616
from cachetools import Cache, LRUCache, TTLCache, func
1717
from docker.errors import APIError
1818

19-
from pycardano import cbor2
2019
from pycardano.address import Address
2120
from pycardano.backend.base import (
2221
ALONZO_COINS_PER_UTXO_WORD,
2322
ChainContext,
2423
GenesisParameters,
2524
ProtocolParameters,
2625
)
26+
from pycardano.cbor2 import cbor2
2727
from pycardano.exception import (
2828
CardanoCliError,
2929
PyCardanoException,

pycardano/cbor2.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
"""
2+
Conditional cbor2 import module.
3+
4+
This module provides a centralized location for importing cbor2,
5+
with support for both the C extension (cbor2) and pure Python (cbor2pure) versions.
6+
Set the environment variable CBOR_C_EXTENSION=1 to use the C extension.
7+
"""
8+
9+
import os
10+
11+
if os.getenv("CBOR_C_EXTENSION", "0") == "1":
12+
import cbor2
13+
else:
14+
import cbor2pure as cbor2 # type: ignore[no-redef]
15+
16+
__all__ = ["cbor2"]

pycardano/plutus.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
from nacl.hash import blake2b
1616
from typeguard import typechecked
1717

18-
from pycardano import cbor2
18+
from pycardano.cbor2 import cbor2
1919
from pycardano.exception import DeserializeException, InvalidArgumentException
2020
from pycardano.hash import DATUM_HASH_SIZE, SCRIPT_HASH_SIZE, DatumHash, ScriptHash
2121
from pycardano.nativescript import NativeScript

pycardano/serialization.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
get_type_hints,
3232
)
3333

34-
from pycardano import cbor2
34+
from pycardano.cbor2 import cbor2
3535
from pycardano.logging import logger
3636

3737
# Remove the semantic decoder for 258 (CBOR tag for set) as we care about the order of elements

pycardano/transaction.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@
1111
from nacl.hash import blake2b
1212
from pprintpp import pformat
1313

14-
from pycardano import cbor2
1514
from pycardano.address import Address
15+
from pycardano.cbor2 import cbor2
1616
from pycardano.certificate import Certificate
1717
from pycardano.exception import InvalidDataException
1818
from pycardano.governance import ProposalProcedure, VotingProcedures

pycardano/utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99
from nacl.encoding import RawEncoder
1010
from nacl.hash import blake2b
1111

12-
from pycardano import cbor2
1312
from pycardano.backend.base import ChainContext
13+
from pycardano.cbor2 import cbor2
1414
from pycardano.hash import SCRIPT_DATA_HASH_SIZE, SCRIPT_HASH_SIZE, ScriptDataHash
1515
from pycardano.plutus import COST_MODELS, CostModels, Datum, RedeemerMap, Redeemers
1616
from pycardano.serialization import NonEmptyOrderedSet, default_encoder

0 commit comments

Comments
 (0)