Skip to content
This repository has been archived by the owner on Jul 19, 2024. It is now read-only.

Commit

Permalink
Merge pull request #140 from CounterpartyXCP/develop
Browse files Browse the repository at this point in the history
Fixed armory_utxsvr
  • Loading branch information
Robby Dermody committed Mar 19, 2016
2 parents daf9629 + bcb27f8 commit e2150c3
Showing 1 changed file with 54 additions and 25 deletions.
79 changes: 54 additions & 25 deletions counterblock/armory_utxsvr.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
import json
import time
import threading
import requests
import datetime

import flask
from flask import request
Expand All @@ -20,6 +22,18 @@
ARMORY_UTXSVR_PORT_MAINNET = 6590
ARMORY_UTXSVR_PORT_TESTNET = 6591
app = flask.Flask(__name__)
is_testnet = False
bitcoind_url = None

def call_rpc(method, params):
headers = {'content-type': 'application/json'}
if not isinstance(params, list): params = [params,]
payload = json.dumps({"method": method, "params": params, "jsonrpc": "2.0", "id": 0})
response = requests.post(bitcoind_url, headers=headers, data=payload, timeout=10)
response_json = response.json()
if 'error' not in response_json.keys() or response_json['error'] == None:
return response_json['result']
raise Exception("API request got error response: %s" % response_json)

@dispatcher.add_method
def serialize_unsigned_tx(unsigned_tx_hex, public_key_hex):
Expand All @@ -29,7 +43,22 @@ def serialize_unsigned_tx(unsigned_tx_hex, public_key_hex):
try:
unsigned_tx_bin = hex_to_binary(unsigned_tx_hex)
pytx = PyTx().unserialize(unsigned_tx_bin)
utx = UnsignedTransaction(pytx=pytx, pubKeyMap=hex_to_binary(public_key_hex))

#compose a txmap manually via bitcoind's getrawtransaction call because armory's way of
# doing it (TheBDM.bdv().getTxByHash()) seems to not always work in 0.93.3+ ...
tx_map = {}
for txin in pytx.inputs:
outpt = txin.outpoint
txhash = outpt.txHash
txhash_hex = binary_to_hex(txhash, BIGENDIAN)
try:
raw_tx_result = call_rpc("getrawtransaction", [txhash_hex, 1])
except Exception as e:
raise Exception("Could not locate input txhash %s: %s" % (txhash_hex, e))
return
tx_map[txhash] = PyTx().unserialize(hex_to_binary(raw_tx_result['hex']))

utx = UnsignedTransaction(pytx=pytx, pubKeyMap=hex_to_binary(public_key_hex), txMap=tx_map)
unsigned_tx_ascii = utx.serializeAscii()
except Exception, e:
raise Exception("Could not serialize transaction: %s" % e)
Expand Down Expand Up @@ -69,40 +98,40 @@ def handle_post():
response = flask.Response(rpc_response_json, 200, mimetype='application/json')
return response

class ArmoryBlockchainUpdaterThread(threading.Thread):
def __init__(self):
threading.Thread.__init__(self)

def run(self):
#loop to check for new blocks
print("**** Polling for blockchain updates ...")
while(True):
prevTop = TheBDM.getTopBlockHeight()
TheBDM.readBlkFileUpdate()
newTop = TheBDM.getTopBlockHeight()
if newTop > prevTop:
print 'New blocks: %d (top: %d)' % (newTop-prevTop, newTop)
time.sleep(1.0) # check every 1 second
def blockchainLoaded(args):
print("**** Initializing Flask (HTTP) server ...")
app.run(host="127.0.0.1", port=ARMORY_UTXSVR_PORT_MAINNET if not is_testnet else ARMORY_UTXSVR_PORT_TESTNET, threaded=True)
print("**** Ready to serve ...")

def newBlock(args):
print('**** NEW BLOCK: Current height is %s' % TheBDM.getTopBlockHeight())

def main():
global is_testnet, bitcoind_url

print("**** Starting up ...")
parser = argparse.ArgumentParser(description='Armory offline transaction generator daemon')
parser.add_argument('--testnet', action='store_true', help='Run for testnet')
args = parser.parse_args()
btcdir = "/home/xcp/.bitcoin" + ("/testnet3" if args.testnet else '')
parser.add_argument('bitcoind_url', help='bitcoind RPC endpoint URL, e.g. "http://rpc:rpcpass@localhost:8332"')
parser_args = parser.parse_args()

btcdir = "/home/xcp/.bitcoin" + ("/testnet3" if parser_args.testnet else '')
is_testnet = parser_args.testnet
bitcoind_url = parser_args.bitcoind_url

print("**** Initializing armory ...")
#require armory to be installed, adding the configured armory path to PYTHONPATH
TheBDM.btcdir = btcdir
#TheBDM.goOnline()
TheBDM.setBlocking(True)
TheBDM.setOnlineMode(True)
blockchainUpdaterThread = ArmoryBlockchainUpdaterThread()
blockchainUpdaterThread.start()
TheBDM.RegisterEventForSignal(blockchainLoaded, FINISH_LOAD_BLOCKCHAIN_ACTION)
TheBDM.RegisterEventForSignal(newBlock, NEW_BLOCK_ACTION)
TheBDM.goOnline()

print("**** Initializing Flask (HTTP) server ...")
app.run(host="127.0.0.1", port=ARMORY_UTXSVR_PORT_MAINNET if not args.testnet else ARMORY_UTXSVR_PORT_TESTNET, threaded=True)
print("**** Ready to serve ...")
try:
while(True):
time.sleep(1)
except KeyboardInterrupt:
print("******** Exiting *********")
exit(0)

if __name__ == '__main__':
main()

0 comments on commit e2150c3

Please sign in to comment.