Skip to content

[bug]: BlockFrostChainContext is not correctly initialized #349

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
chrissiwaffler opened this issue May 14, 2024 · 3 comments · Fixed by #353
Closed

[bug]: BlockFrostChainContext is not correctly initialized #349

chrissiwaffler opened this issue May 14, 2024 · 3 comments · Fixed by #353
Labels
bug Something isn't working

Comments

@chrissiwaffler
Copy link
Contributor

Describe the bug
The code of pycardano/backend/blockfrost.py seems to be faulty

class BlockFrostChainContext(ChainContext):
    """A `BlockFrost <https://blockfrost.io/>`_ API wrapper for the client code to interact with.

    Args:
        project_id (str): A BlockFrost project ID obtained from https://blockfrost.io.
        network (Network): Network to use.
        base_url (str): Base URL for the BlockFrost API. Defaults to the preprod url.
    """

    api: BlockFrostApi
    _epoch_info: Namespace
    _epoch: Optional[int] = None
    _genesis_param: Optional[GenesisParameters] = None
    _protocol_param: Optional[ProtocolParameters] = None

    def __init__(
        self,
        project_id: str,
        network: Optional[Network] = None,
        base_url: Optional[str] = None,
    ):
        if network is not None:
            warnings.warn(
                "`network` argument will be deprecated in the future. Directly passing `base_url` is recommended."
            )
            self._network = network
        else:
            self._network = Network.TESTNET

        self._project_id = project_id
        self._base_url = (
            base_url
            if base_url
            else ApiUrls.preprod.value
            if self.network == Network.TESTNET
            else ApiUrls.mainnet.value
        )
        self.api = BlockFrostApi(project_id=self._project_id, base_url=self._base_url)
        self._epoch_info = self.api.epoch_latest()
        self._epoch = None
        self._genesis_param = None
        self._protocol_param = None

In this initialization of the BlockFrostChainContext class, the self._network gets always set to Network.TESTNET regardless of the value of base_url.

To Reproduce
initialize a context:

context = BlockFrostChainContext(
        blockfrost_project_id,
        base_url=(
            blockfrost.ApiUrls.mainnet.value
        ),
    )
    print("context network ", context.network)

output:

context network  Network.TESTNET

Expected behavior
The self._network value should be correcly updated according to the value of base_url.

@cffls
Copy link
Collaborator

cffls commented May 26, 2024

self._network is going to be deprecated, and it is not a commended way of getting network. base_url is a preferred way of getting and setting network for blockfrost chain context.

@nielstron
Copy link
Contributor

The Problem is that only setting the base url is not enough and will lead to errors down the line - one needs to set network or an unusable context is obtained.

@cffls
Copy link
Collaborator

cffls commented May 26, 2024

You are right @nielstron , I forgot that this network value is used when building addresses. It is fixed in this PR: #353

@cffls cffls added the bug Something isn't working label May 26, 2024
@cffls cffls closed this as completed in #353 Jun 1, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
3 participants