python-bitcoinlib icon indicating copy to clipboard operation
python-bitcoinlib copied to clipboard

Add support to change the base URL of service.

Open vincenzopalazzo opened this issue 4 years ago • 0 comments

With Bitcoin 0.21.0 we there is no default wallet, and I realized that there is no way to change the base URL inside the library to change the RPC URL.

In order to give the possibility to change the URL at runtime I added an additional propriety base_url that can be used to store get the basic URL given by the __init__ method.

I added the set and getter method to work change the URL path.

This can be an example of proxy that handle all change alone

class CustomBitcoinProxy:
    def __init__(self, btc_conf_file, *args, **kwargs):
        self.__proxy__ = BitcoinProxy(btc_conf_file=btc_conf_file)

    def __getattr__(self, name):
        if name.startswith("__") and name.endswith("__"):
            # Python internal stuff
            raise AttributeError

        def f(*args):
            logging.debug(
                "Calling {name} with arguments {args}".format(name=name, args=args)
            )
            res = self.__proxy__._call(name, *args)
            logging.error(
                "Result for {name} call: {res}".format(
                    name=name,
                    res=res,
                )
            )
            if "createwallet" in name:
                self.__proxy__.service_url = "{}/wallet/{}".format(self.__proxy__.base_url, args[0])
            return res

        # Make debuggers show <function bitcoin.rpc.name> rather than <function
        # bitcoin.rpc.<lambda>>
        f.__name__ = name
        return f

This is a test to reproduce the failure that require to change URL in the RPC request

def test_load_new_wallet(bitcoind):
    addr = bitcoind.rpc.getnewaddress()
    list_hash = bitcoind.rpc.generatetoaddress(10, addr)
    assert list_hash is not None

    bitcoind.rpc.createwallet("api")
    with pytest.raises(JSONRPCError):
        bitcoind.rpc.loadwallet("api")
        ## Raise because we need to change the url

A complete test suite that test this feature is available here: https://gitlab.com/vincenzopalazzo/draft-ideas/-/blob/master/pyln-test-sandbox/test/test_multiple_wallet.py

PS: Maybe the test architecture used in the previous link can be helpful in this repository to start to make test on the rpc interface?

vincenzopalazzo avatar May 17 '21 21:05 vincenzopalazzo