Author

Topic: Need help simply connecting a Python script to a local bitcoin node (Read 77 times)

member
Activity: 65
Merit: 24

Im just beginning with bitcoin development but am really struggling with even the most basic thing: connect to a local bitcoin node in regtest mode and list transactions.

I have a simple script:

Code:
import bitcoin
from bitcoin.rpc import Proxy

# create a connection to the local bitcoin node
conn = Proxy(
    service_url='http://user:pass@localhost:8332/'
)

print("conn is ")
print(conn)

# list the transactions in the wallet
print(conn.listtransactions())
Regtest uses different ports from mainnet (and testnet and signet, they're all using different ports). The RPC port for regtest is 18443, not mainnet's 8332.

localaddresses is empty, which I believe means theres something wrong with the node (
Code:
problem number 2
)
No, that's pretty normal. The local address is often not known unless explicitly configured or using an anonymity network like Tor or I2P where that network's daemon will inform Bitcoin Core.

I also run into problem number 3 when I run
Code:
bitcoin-cli getdatadir -regtest
, it returns:
Two problems here, the options to bitcoin-cli come before the RPC you want to run, and getdatadir is not a RPC.

Is Bitcoin development really this slow and painful? Its 2022, are there not simple, straightforward tutorials on how to run a node locally, and have a python query it? If anyone can please help me acheive this very simple tasl, it would be much apprciated
No, but it helps to read the documentation, the log files, and any errors that you get.

Ok got it working by creating a bitcoin.conf file and setting the rpcuser, rpcpassword and rpcauth in it. Then running `bitcoind` with:

Code:
./src/bitcoind -wallet=mywallet -printtoconsole -regtest

And I could get the balance with the script:

Code:
import bitcoin
# this comes from lib python-bitcoinlib
# In general, python-bitcoinlib is a more comprehensive library for working with Bitcoin, while python-bitcoinrpc is a more specialized library that is specifically designed for interacting with a Bitcoin server using JSON-RPC.
from bitcoin.rpc import Proxy

# create a connection to the local bitcoin node
conn = Proxy(
    service_url='http://mark:[email protected]:18443/'
)

print("conn is ")
# print all of conn's attributes

for attr in dir(conn):
    print("obj.%s = %r" % (attr, getattr(conn, attr)))
print(conn)

# list the transactions in the wallet
print(conn.getbalance())

Thanks for the help.
member
Activity: 65
Merit: 24

Im just beginning with bitcoin development but am really struggling with even the most basic thing: connect to a local bitcoin node in regtest mode and list transactions.

I have a simple script:

Code:
import bitcoin
from bitcoin.rpc import Proxy

# create a connection to the local bitcoin node
conn = Proxy(
    service_url='http://user:pass@localhost:8332/'
)

print("conn is ")
print(conn)

# list the transactions in the wallet
print(conn.listtransactions())
Regtest uses different ports from mainnet (and testnet and signet, they're all using different ports). The RPC port for regtest is 18443, not mainnet's 8332.

localaddresses is empty, which I believe means theres something wrong with the node (
Code:
problem number 2
)
No, that's pretty normal. The local address is often not known unless explicitly configured or using an anonymity network like Tor or I2P where that network's daemon will inform Bitcoin Core.

I also run into problem number 3 when I run
Code:
bitcoin-cli getdatadir -regtest
, it returns:
Two problems here, the options to bitcoin-cli come before the RPC you want to run, and getdatadir is not a RPC.

Is Bitcoin development really this slow and painful? Its 2022, are there not simple, straightforward tutorials on how to run a node locally, and have a python query it? If anyone can please help me acheive this very simple tasl, it would be much apprciated
No, but it helps to read the documentation, the log files, and any errors that you get.

Ok thanks for this. Ya I did skip a lot of documentation and went straight to trying to connect a script to a node. Its late here but will go through what youve said tomorrow and see can I get it working.
staff
Activity: 3458
Merit: 6793
Just writing some code

Im just beginning with bitcoin development but am really struggling with even the most basic thing: connect to a local bitcoin node in regtest mode and list transactions.

I have a simple script:

Code:
import bitcoin
from bitcoin.rpc import Proxy

# create a connection to the local bitcoin node
conn = Proxy(
    service_url='http://user:pass@localhost:8332/'
)

print("conn is ")
print(conn)

# list the transactions in the wallet
print(conn.listtransactions())
Regtest uses different ports from mainnet (and testnet and signet, they're all using different ports). The RPC port for regtest is 18443, not mainnet's 8332.

localaddresses is empty, which I believe means theres something wrong with the node (
Code:
problem number 2
)
No, that's pretty normal. The local address is often not known unless explicitly configured or using an anonymity network like Tor or I2P where that network's daemon will inform Bitcoin Core.

I also run into problem number 3 when I run
Code:
bitcoin-cli getdatadir -regtest
, it returns:
Two problems here, the options to bitcoin-cli come before the RPC you want to run, and getdatadir is not a RPC.

Is Bitcoin development really this slow and painful? Its 2022, are there not simple, straightforward tutorials on how to run a node locally, and have a python query it? If anyone can please help me acheive this very simple tasl, it would be much apprciated
No, but it helps to read the documentation, the log files, and any errors that you get.
member
Activity: 65
Merit: 24

Im just beginning with bitcoin development but am really struggling with even the most basic thing: connect to a local bitcoin node in regtest mode and list transactions.

I have a simple script:

Code:
import bitcoin
from bitcoin.rpc import Proxy

# create a connection to the local bitcoin node
conn = Proxy(
    service_url='http://user:pass@localhost:8332/'
)

print("conn is ")
print(conn)

# list the transactions in the wallet
print(conn.listtransactions())

When I create a virtualenv and activate, and install the dependancy
Code:
pip3 install bitcoin
and then run the script
Code:
python3 scripts_bitcoin.py
I meet problem number 1:

Code:
  File "scripts_bitcoin.py", line 2, in
    from bitcoin.rpc import Proxy

After many many hours trying different things, I abandoned virtualenv. When I dont bother with it and I just run `python3 scripts_bitcoin.py` (so python3 globally), I get the error:


Code:
conn is

Traceback (most recent call last):
  File "/Users/me/Bitcoin/exchange/scripts_bitcoin.py", line 13, in
    print(conn.listtransactions())
AttributeError: 'Proxy' object has no attribute 'listtransactions'

So this is some progress.

I started bitcoindd with:

Code:
./src/bitcoind -printtoconsole -regtest

I get the output:
Code:
2022-12-24T18:53:49Z Bitcoin Core version v24.99.0-9c47eb450346 (release build)
2022-12-24T18:53:49Z Using the 'sse4(1way),sse41(4way),avx2(8way)' SHA256 implementation
2022-12-24T18:53:49Z Using RdSeed as an additional entropy source
2022-12-24T18:53:49Z Using RdRand as an additional entropy source
2022-12-24T18:53:49Z Default data directory /Users/me/Library/Application Support/Bitcoin
2022-12-24T18:53:49Z Using data directory /Users/me/Library/Application Support/Bitcoin/regtest
2022-12-24T18:53:49Z Config file: /Users/me/Library/Application Support/Bitcoin/bitcoin.conf (not found, skipping)
2022-12-24T18:53:49Z Command-line arg: printtoconsole=""
2022-12-24T18:53:49Z Command-line arg: regtest=""
2022-12-24T18:53:49Z Using at most 125 automatic connections (285 file descriptors available)
2022-12-24T18:53:49Z Using 16 MiB out of 16 MiB requested for signature cache, able to store 524288 elements
2022-12-24T18:53:49Z Using 16 MiB out of 16 MiB requested for script execution cache, able to store 524288 elements
2022-12-24T18:53:49Z Script verification uses 11 additional threads
2022-12-24T18:53:49Z scheduler thread start
2022-12-24T18:53:49Z Binding RPC on address ::1 port 18443
2022-12-24T18:53:49Z Binding RPC on address 127.0.0.1 port 18443
2022-12-24T18:53:49Z [http] creating work queue of depth 16
2022-12-24T18:53:49Z Using random cookie authentication.
2022-12-24T18:53:49Z Generated RPC authentication cookie /Users/me/Library/Application Support/Bitcoin/regtest/.cookie
2022-12-24T18:53:49Z [http] starting 4 worker threads
2022-12-24T18:53:49Z Using wallet directory /Users/me/Library/Application Support/Bitcoin/regtest/wallets
2022-12-24T18:53:49Z init message: Verifying wallet(s)…
2022-12-24T18:53:49Z Using /16 prefix for IP bucketing
2022-12-24T18:53:49Z init message: Loading P2P addresses…
2022-12-24T18:53:49Z Loaded 0 addresses from peers.dat  0ms
2022-12-24T18:53:49Z init message: Loading banlist…
2022-12-24T18:53:49Z SetNetworkActive: true
2022-12-24T18:53:49Z Cache configuration:
2022-12-24T18:53:49Z * Using 2.0 MiB for block index database
2022-12-24T18:53:49Z * Using 8.0 MiB for chain state database
2022-12-24T18:53:49Z * Using 440.0 MiB for in-memory UTXO set (plus up to 286.1 MiB of unused mempool space)
2022-12-24T18:53:49Z init message: Loading block index…
2022-12-24T18:53:49Z Validating signatures for all blocks.
2022-12-24T18:53:49Z Setting nMinimumChainWork=0000000000000000000000000000000000000000000000000000000000000000
2022-12-24T18:53:49Z Opening LevelDB in /Users/me/Library/Application Support/Bitcoin/regtest/blocks/index
2022-12-24T18:53:49Z Opened LevelDB successfully
2022-12-24T18:53:49Z Using obfuscation key for /Users/me/Library/Application Support/Bitcoin/regtest/blocks/index: 0000000000000000
2022-12-24T18:53:49Z LoadBlockIndexDB: last block file = 0
2022-12-24T18:53:49Z LoadBlockIndexDB: last block file info: CBlockFileInfo(blocks=103, size=26491, heights=0...102, time=2011-02-02...2022-12-24)
2022-12-24T18:53:49Z Checking all blk files are present...
2022-12-24T18:53:49Z Initializing chainstate Chainstate [ibd] @ height -1 (null)
2022-12-24T18:53:49Z Opening LevelDB in /Users/me/Library/Application Support/Bitcoin/regtest/chainstate
2022-12-24T18:53:49Z Opened LevelDB successfully
2022-12-24T18:53:49Z Using obfuscation key for /Users/me/Library/Application Support/Bitcoin/regtest/chainstate: ab8accc48fb6836f
2022-12-24T18:53:49Z Loaded best chain: hashBestChain=24ac1848b98bb39fcde1d2553698086f80aadbcea93018a42ba943b467a4c83a height=102 date=2022-12-24T14:18:54Z progress=1.000000
2022-12-24T18:53:49Z [snapshot] allocating all cache to the IBD chainstate
2022-12-24T18:53:49Z Opening LevelDB in /Users/me/Library/Application Support/Bitcoin/regtest/chainstate
2022-12-24T18:53:49Z Opened LevelDB successfully
2022-12-24T18:53:49Z Using obfuscation key for /Users/me/Library/Application Support/Bitcoin/regtest/chainstate: ab8accc48fb6836f
2022-12-24T18:53:49Z [Chainstate [ibd] @ height 102 (24ac1848b98bb39fcde1d2553698086f80aadbcea93018a42ba943b467a4c83a)] resized coinsdb cache to 8.0 MiB
2022-12-24T18:53:49Z [Chainstate [ibd] @ height 102 (24ac1848b98bb39fcde1d2553698086f80aadbcea93018a42ba943b467a4c83a)] resized coinstip cache to 440.0 MiB
2022-12-24T18:53:49Z init message: Verifying blocks…
2022-12-24T18:53:49Z Verifying last 6 blocks at level 3
2022-12-24T18:53:49Z [0%]...[16%]...[33%]...[50%]...[66%]...[83%]...[99%]...[DONE].
2022-12-24T18:53:49Z No coin database inconsistencies in last 6 blocks (6 transactions)
2022-12-24T18:53:49Z  block index             289ms
2022-12-24T18:53:49Z Setting NODE_NETWORK on non-prune mode
2022-12-24T18:53:49Z block tree size = 103
2022-12-24T18:53:49Z nBestHeight = 102
2022-12-24T18:53:49Z loadblk thread start
2022-12-24T18:53:49Z torcontrol thread start
2022-12-24T18:53:49Z Imported mempool transactions from disk: 0 succeeded, 0 failed, 0 expired, 0 already there, 0 waiting for initial broadcast
2022-12-24T18:53:49Z loadblk thread exit
2022-12-24T18:53:49Z Bound to 127.0.0.1:18445
2022-12-24T18:53:49Z Bound to [::]:18444
2022-12-24T18:53:49Z Bound to 0.0.0.0:18444
2022-12-24T18:53:49Z Loaded 0 addresses from "anchors.dat"
2022-12-24T18:53:49Z 0 block-relay-only anchors will be tried for connections.
2022-12-24T18:53:49Z init message: Starting network threads…
2022-12-24T18:53:49Z dnsseed thread start
2022-12-24T18:53:49Z init message: Done loading
2022-12-24T18:53:49Z msghand thread start
2022-12-24T18:53:49Z net thread start
2022-12-24T18:53:49Z addcon thread start
2022-12-24T18:53:49Z opencon thread start
2022-12-24T18:53:49Z Loading addresses from DNS seed dummySeed.invalid.
2022-12-24T18:53:49Z 0 addresses found from DNS seeds
2022-12-24T18:53:49Z dnsseed thread exit


Maybe the line 2022-12-24T18:53:49Z Config file: /Users/me/Library/Application Support/Bitcoin/bitcoin.conf (not found, skipping) is important here, Im not sure.

What should be in bitcoin.conf?

I want to get the local address and port its running on. So I run `bitcoin-cli -regtest getnetworkinfo`:
Code:
{
  "version": 249900,
  "subversion": "/Satoshi:24.99.0/",
  "protocolversion": 70016,
  "localservices": "0000000000000409",
  "localservicesnames": [
    "NETWORK",
    "WITNESS",
    "NETWORK_LIMITED"
  ],
  "localrelay": true,
  "timeoffset": 0,
  "networkactive": true,
  "connections": 0,
  "connections_in": 0,
  "connections_out": 0,
  "networks": [
    {
      "name": "ipv4",
      "limited": false,
      "reachable": true,
      "proxy": "",
      "proxy_randomize_credentials": false
    },
    {
      "name": "ipv6",
      "limited": false,
      "reachable": true,
      "proxy": "",
      "proxy_randomize_credentials": false
    },
    {
      "name": "onion",
      "limited": true,
      "reachable": false,
      "proxy": "",
      "proxy_randomize_credentials": false
    },
    {
      "name": "i2p",
      "limited": true,
      "reachable": false,
      "proxy": "",
      "proxy_randomize_credentials": false
    },
    {
      "name": "cjdns",
      "limited": true,
      "reachable": false,
      "proxy": "",
      "proxy_randomize_credentials": false
    }
  ],
  "relayfee": 0.00001000,
  "incrementalfee": 0.00001000,
  "localaddresses": [
  ],
  "warnings": "This is a pre-release test build - use at your own risk - do not use for mining or merchant applications"
}


localaddresses is empty, which I believe means theres something wrong with the node (
Code:
problem number 2
)

I also run into problem number 3 when I run
Code:
bitcoin-cli getdatadir -regtest
, it returns:

Code:
error: timeout on transient error: Could not connect to the server 127.0.0.1:8332

Make sure the bitcoind server is running and that you are connecting to the correct RPC port.

Even though the node appears to be running (see logs above).



Is Bitcoin development really this slow and painful? Its 2022, are there not simple, straightforward tutorials on how to run a node locally, and have a python query it? If anyone can please help me acheive this very simple tasl, it would be much apprciated
Jump to: