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:
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
pip3 install bitcoin
and then run the script
python3 scripts_bitcoin.py
I meet
problem number 1: 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:
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:
./src/bitcoind -printtoconsole -regtest
I get the output:
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`:
{
"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 (
problem number 2
)
I also run into
problem number 3 when I run
bitcoin-cli getdatadir -regtest
, it returns:
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