Skip to content

Instantly share code, notes, and snippets.

@laanwj
Last active September 17, 2021 10:37
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save laanwj/f2e0238bd151d5365c07bdd03467588b to your computer and use it in GitHub Desktop.
Save laanwj/f2e0238bd151d5365c07bdd03467588b to your computer and use it in GitHub Desktop.
RPC batching example
#!/usr/bin/env python3
'''
Example showing the use of RPC batching in Bitcoin Core.
'''
# W.J. van der Laan
# SPDX-License-Identifier: MIT
import sys,os
# just grab the library from the closest bitcoin instance
sys.path.append(os.path.join(os.path.dirname(os.path.abspath(__file__)), 'bitcoin', 'test', 'functional'))
from test_framework.authproxy import AuthServiceProxy, JSONRPCException
# use cookie from data directory (regtest)
datadir = os.getenv("DATADIR", os.path.join(os.getenv('HOME'), '.bitcoin', 'regtest'))
with open(os.path.join(datadir,'.cookie'),'r') as f:
cookie = f.read()
prox = AuthServiceProxy('http://'+cookie+'@127.0.0.1:18443/')
# list of addresses to import
addresses = [
'a',
'b',
'c'
]
batch = []
reqid = 0
for addr in addresses:
batch.append({'version': '1.1',
'method': 'importaddress',
'params': [addr, '', False],
'id': reqid})
reqid += 1
responses = prox.batch(batch)
for r in responses:
if r['error'] is not None:
print('Request %i failed with error %i: %s' % (r['id'], r['error']['code'], r['error']['message']))
else:
print('Request %i succeeded: %s' % (r['id'], str(r['result'])))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment