From 2291138fb64d5be7d66e24e72cbfb11af6a55915 Mon Sep 17 00:00:00 2001 From: pythcoiner Date: Mon, 17 Mar 2025 20:02:22 +0100 Subject: [PATCH] lianad: docs & tests for getlabelsbip329 command --- doc/API.md | 20 ++++++++++++++ tests/test_rpc.py | 70 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 90 insertions(+) diff --git a/doc/API.md b/doc/API.md index ead31e98..a7b0f9ff 100644 --- a/doc/API.md +++ b/doc/API.md @@ -25,6 +25,7 @@ Commands must be sent as valid JSONRPC 2.0 requests, ending with a `\n`. | [`createrecovery`](#createrecovery) | Create a recovery transaction to sweep expired coins | | [`updatelabels`](#updatelabels) | Update the labels | | [`getlabels`](#getlabels) | Get the labels for the given addresses, txids and outpoints | +| [`getlabelsbip329`](#getlabelsbip329) | Get the labels in BIP-0329 format | # Reference @@ -460,3 +461,22 @@ Items without labels are not present in the response map. | Field | Type | Description | | -------- | ------ | -------------------------------------------------------------------------------- | | `labels` | object | A mapping of bitcoin addresses, txids and outpoints as keys, and string as values | + +### `getlabelsbip329` + +Retrieve a list of labels in [BIP-0329](https://github.com/bitcoin/bips/blob/master/bip-0329.mediawiki) +format, with pagination support. + +#### Request + +| Field | Type | Description | +| -------- | ------- | ------------------------------------------ | +| `offset` | integer | Index to start returning labels from | +| `limit` | integer | Maximum number of labels to return | + +#### Response + +| Field | Type | Description | +| -------- | ------ | ------------------------------------------------- | +| `labels` | array | A list of BIP-0329-formatted label objects | + diff --git a/tests/test_rpc.py b/tests/test_rpc.py index 4b7a4a3c..39ea5b82 100644 --- a/tests/test_rpc.py +++ b/tests/test_rpc.py @@ -1211,6 +1211,76 @@ def test_labels(lianad, bitcoind): assert res[random_address] == "this address is random" +def test_labels_bip329(lianad, bitcoind): + # Label 5 addresses + addresses = [] + for i in range(0,5): + addr = lianad.rpc.getnewaddress()["address"] + addresses.append(addr) + lianad.rpc.updatelabels({addr: f"addr{i}"}) + + # Label 5 coin + txids = [] + for i in range(0,5): + addr = lianad.rpc.getnewaddress()["address"] + txid = bitcoind.rpc.sendtoaddress(addr, 1) + txids.append(txid) + wait_for(lambda: len(lianad.rpc.listcoins()["coins"]) == i+1 ) + + coins = lianad.rpc.listcoins()["coins"] + for i in range(0,5): + coin = coins[i] + lianad.rpc.updatelabels({coin["outpoint"]: f"coin{i}"}) + + # Label 5 transactions + for i, txid in enumerate(txids): + lianad.rpc.updatelabels({txid: f"tx{i}"}) + + # Get Bip-0329 labels + bip329_labels = lianad.rpc.getlabelsbip329(0,100)["labels"] + assert len(bip329_labels) == 15 + + def label_found(name, labels): + for label in labels: + if label["label"] == name: + return True + return False + + # All transactions are labelled + for i in range(0, len(txids)): + assert label_found(f"tx{i}", bip329_labels) + + # All adresses are labelled + for i in range(0, len(addresses)): + assert label_found(f"addr{i}", bip329_labels) + + # All coins are labelled + for i in range(0, len(coins)): + assert label_found(f"coin{i}", bip329_labels) + + # There is no conflict between batches + batch1 = lianad.rpc.getlabelsbip329(0,5)["labels"] + assert len(batch1) == 5 + + batch2 = lianad.rpc.getlabelsbip329(5,5)["labels"] + assert len(batch2) == 5 + + batch3 = lianad.rpc.getlabelsbip329(10,5)["labels"] + assert len(batch3) == 5 + + for label in batch1: + print(label) + name = label["label"] + + assert not label_found(name, batch2) + assert not label_found(name, batch3) + + for label in batch2: + name = label["label"] + assert not label_found(name, batch1) + assert not label_found(name, batch3) + + def test_rbfpsbt_bump_fee(lianad, bitcoind): """Test the use of RBF to bump the fee of a transaction."""