Ejemplo n.º 1
0
def main():
    rpc_user = '******'
    rpc_password = '******'
    rpc = AuthServiceProxy(f'http://{rpc_user}:{rpc_password}@127.0.0.1:18332/')
    #rpc = AuthServiceProxy(f'http://{rpc_user}:{rpc_password}@172.17.0.2:18332/')
    print(rpc.getinfo())
    best_block_hash = rpc.getbestblockhash()
    print(rpc.getblock(best_block_hash))
    blhash = rpc.getblockhash(0) #blhashはブロックのhash文字列
    bl = rpc.getblock(blhash) #blはブロック情報
    print(bl)
    dummy_address = '2MudgRfNaaw96kqAWziZ5JGsPbo2pzQp7Jy'
    change_address = '2NAVrak22jX3DQyDqnoqdm5ZTak1RgXWPzo'

    filename = 'mark_token.btc.json'
    url='https://drive.google.com/file/d/1ZR6Q5sCM_acUpPy7s3d9GJH8I2Plh4FI/view?usp=sharing'

    with open(filename, 'rb') as f:
        data2 = f.read()
    hashdata=hashlib.sha256(data2).hexdigest()
    js={'file_hash':hashdata,'url':url}
    data=json.dumps(js).encode("UTF-8")


    while True:
        if len(data) >= 80:
            buffer = data[:80]
            data = data[80:]
        elif len(data) == 0:
            break
        else:
            buffer = data
            data = b''

        first_unspent = rpc.listunspent()[0]
        txid = first_unspent['txid']
        vout = first_unspent['vout']
        input_amount = first_unspent['amount']
        SATOSHI = Decimal("0.00000001")
        change_amount = input_amount - Decimal("0.005") - SATOSHI

        tx = rpc.createrawtransaction([{"txid": txid, "vout": vout}],[{change_address: change_amount}, {'data': hexlify(buffer).decode('utf-8')}, ])
        tx = rpc.signrawtransactionwithwallet(tx)['hex']
        rpc.sendrawtransaction(tx)

    block_hash = rpc.generatetoaddress(1, change_address)[0]
    block = rpc.getblock(block_hash)
    txs = block['tx'][1:]

    print(f'# of txs: {len(txs)}')
    pprint(txs)

    for tx_hash in txs:
        raw_tx = rpc.gettransaction(tx_hash)['hex']
        decoded_tx = rpc.decoderawtransaction(raw_tx)
        # pprint(decoded_tx)
        print(decoded_tx['vout'][1]['scriptPubKey']['asm'])
Ejemplo n.º 2
0
def rpc_loop(ncurses_q, json_q):
	config = ConfigParser.ConfigParser()
	config.read('bitcoind-ncurses.conf')
	rpcuser = config.get('rpc', 'rpcuser')
	rpcpassword = config.get('rpc', 'rpcpassword')
	rpcip = config.get('rpc', 'rpcip')
	rpcport = config.get('rpc', 'rpcport')

	rpcurl = "http://" + rpcuser + ":" + rpcpassword + "@" + rpcip + ":" + rpcport
	rpchandle = AuthServiceProxy(rpcurl, None, 500)

	last_blockcount = 0		# ensures block info is updated initially
	last_update = time.time() - 2
	while 1:
		try: s = json_q.get(False)
		except: s = {}
		
		if 'blockheight' in s:
			blockhash = rpchandle.getblockhash(s['blockheight'])
			blockinfo = rpchandle.getblock(blockhash)
			ncurses_q.put(blockinfo)
			last_blockcount = cur_blockcount
		elif 'txid' in s:
			raw_tx = rpchandle.getrawtransaction(s['txid'])
			decoded_tx = rpchandle.decoderawtransaction(raw_tx)
			ncurses_q.put(decoded_tx)

		if (time.time() - last_update) > 2: 
			info = rpchandle.getinfo()
			ncurses_q.put(info)
		
			nettotals = rpchandle.getnettotals()
			ncurses_q.put(nettotals)
	
			walletinfo = rpchandle.getwalletinfo()
			ncurses_q.put(walletinfo)

			cur_blockcount = info['blocks']
			if (cur_blockcount != last_blockcount):		# minimise RPC calls
				#if (last_blockcount == 0):
				lastblocktime = {'lastblocktime': time.time()}
				ncurses_q.put(lastblocktime)
				
				blockhash = rpchandle.getblockhash(cur_blockcount)
				blockinfo = rpchandle.getblock(blockhash)
				ncurses_q.put(blockinfo)
				last_blockcount = cur_blockcount
			
			last_update = time.time()

		time.sleep(0.5)		# minimise RPC calls
Ejemplo n.º 3
0
def handle_block(height):
    rpc_connection = AuthServiceProxy(
        "http://%s:%[email protected]:8332" % (rpc_user, rpc_password))
    hash = rpc_connection.getblockhash(height)
    block = rpc_connection.getblock(hash, 2)
    cjs = np.zeros((0, 9))
    txids = []
    for tx in block['tx']:
        out_values = Counter([o['value'] for o in tx['vout']])
        m_c = out_values.most_common()
        candidates = filter(lambda m: m[1] > 1, m_c)
        candidates = list(filter(lambda m: m[0] > 0, candidates))
        if len(candidates) == 0:
            continue
        cj = candidates[0]
        addresses = [o['scriptPubKey']['addresses']
                     for o in tx['vout'] if 'addresses' in o['scriptPubKey'].keys()]
        addresses = [item for sublist in addresses for item in sublist]
        is_wasabi = wasabi_address in addresses
        has_op_return = any([out['scriptPubKey']['type'] ==
                             'nulldata' for out in tx['vout']])
        features = [height, len(tx['vin']), len(
            tx['vout']), cj[0], cj[1], max(out_values), min(out_values), has_op_return, is_wasabi]
        cjs = np.vstack((cjs, features))
        txids.append(tx['txid'])
    # logging.info("processed {}, {} cjs".format(height, len(txids)))
    return height, cjs, txids
def getBlock(i, ctx):
    bc_client = ctx.obj['bitcoin_client']
    rpc_connection = AuthServiceProxy('http://{}:{}@{}:{}'.format(
        bc_client['user'], bc_client['pwd'], bc_client['host'],
        bc_client['port']))
    block_hash = rpc_connection.getblockhash(i)
    return rpc_connection.getblock(block_hash)
Ejemplo n.º 5
0
def do_RPC(env, send_resp):
    _, args, cur = urlparse.parse_qs(
        env['QUERY_STRING']), env['PATH_INFO'].split(
            '/')[2:], sqc.dbpool.get().cursor()
    send_resp('200 OK', [('Content-Type', 'application/json')])
    result = []
    if args[0] == "getblockcount":
        result = json.dumps(sqc.cfg['block'])
    elif args[0] == "getinfo":
        result = json.dumps({
            'blocks':
            sqc.cfg['block'],
            'difficulty':
            bits2diff(gethdr(sqc.cfg['block'], sqc.cfg, 'bits'))
        })
    elif args[0] == "getdifficulty":
        result = json.dumps(
            bits2diff(gethdr(sqc.cfg['block'], sqc.cfg, 'bits')))
    else:
        rpc = AuthServiceProxy(sqc.cfg['rpc'])
        if args[0] == "getblock":
            result = json.dumps(rpc.getblock(args[1]), cls=btcEncoder)
        elif args[0] == "getblockhash":
            result = json.dumps(rpc.getblockhash(int(args[1])))
        elif args[0] == "getrawtransaction":
            result = json.dumps(rpc.getrawtransaction(args[1], 1),
                                cls=btcEncoder)
        elif args[0] == "gettxout":
            result = json.dumps(rpcTxOut(cur, args[1], args[2]))
        elif args[0] == "getmempoolinfo":
            result = json.dumps(rpc.getmempoolinfo(), cls=btcEncoder)
        elif args[0] == "getrawmempool":
            result = json.dumps(rpc.getrawmempool(False), cls=btcEncoder)
    return result
Ejemplo n.º 6
0
def do_RPC(env, send_resp):
    get, args, cur = urlparse.parse_qs(env["QUERY_STRING"]), env["PATH_INFO"].split("/")[2:], sqc.dbpool.get().cursor()
    send_resp("200 OK", [("Content-Type", "application/json")])
    if args[0] == "getblockcount":
        return json.dumps(sqc.cfg["block"])
    if args[0] == "getinfo":
        return json.dumps(
            {"blocks": sqc.cfg["block"], "difficulty": bits2diff(gethdr(sqc.cfg["block"], "bits", sqc.cfg["path"]))}
        )
    if args[0] == "getdifficulty":
        return json.dumps(bits2diff(gethdr(sqc.cfg["block"], "bits", sqc.cfg["path"])))

    rpc = AuthServiceProxy(sqc.cfg["rpc"])
    if args[0] == "getblock":
        return json.dumps(rpc.getblock(args[1]), cls=btcEncoder)
    if args[0] == "getblockhash":
        return json.dumps(rpc.getblockhash(int(args[1])))
    if args[0] == "getrawtransaction":
        return json.dumps(rpc.getrawtransaction(args[1], 1), cls=btcEncoder)
    if args[0] == "gettxout":
        return json.dumps(rpcTxOut(cur, args[1], args[2]))
    if args[0] == "getmempoolinfo":
        return json.dumps(rpc.getmempoolinfo(), cls=btcEncoder)
    if args[0] == "getrawmempool":
        return json.dumps(rpc.getrawmempool(False), cls=btcEncoder)
    return []
Ejemplo n.º 7
0
class BitcoinCLI:

    def __init__(self):
        self.rpc_connection = AuthServiceProxy(config.endpoint)

    def get_best_block_hash(self):
        return self.rpc_connection.getbestblockhash()

    def get_block_count(self):
        return self.rpc_connection.getblockcount()

    def get_best_block(self):
        return self.rpc_connection.getblock(self.rpc_connection.getbestblockhash())

    def get_block_hash(self, height):
        return self.rpc_connection.getblockhash(height)

    def get_block(self, hash):
        return self.rpc_connection.getblock(hash)
    def get_transaction(self, hash):
        return self.rpc_connection.gettransaction(hash)

    def get_txn_list_from_block(self, hash):
        block = self.get_block(hash)

        if 'tx' in block:
            return block['tx']
        else:
            raise KeyError('Block {0} has no attribute tx'.format(hash))

    def get_raw_transaction(self, tx_id):
        out = self.rpc_connection.getrawtransaction(tx_id, 1)
        return out
    def decoderawtransaction(self, tx_id):
        out = self.rpc_connection.decoderawtransaction(raw)
        return out

    def get_tx_outputs(self, tx_id):
        tx = self.rpc_connection.getrawtransaction(tx_id, 1)
        outputs = [float(i['value']) for i in tx['vout']]
        return outputs

    def get_tx_details(self, tx_id):
        tx = self.rpc_connection.getrawtransaction(tx_id, 1)
        outputs = [float(i['value']) for i in tx['vout']]
        return outputs
def btc_block(block):
    rpc_connection = AuthServiceProxy(
        "http://%s:%s@bitcoin-node:%s" % (rpc_user, rpc_password, rpc_port), timeout=120)
    block_dict = rpc_connection.getblock(block)
    print("new block_json")
    producer.send(TOPIC, key=bytes(str(block), "utf-8"),
                  value=bytes(json.dumps(block_dict, cls=DecimalEncoder), "utf-8"))

    return "OK"
Ejemplo n.º 9
0
def parseBlock(blockHash):
    hostURL = "http://{0}:{1}@{2}:{3}".format(
        config["rpcuser"]["rpcid"], config["rpcuser"]["rpcpass"],
        config["rpcsetting"]["host"], config["rpcsetting"]["port"])
    rpcConnection = AuthServiceProxy(hostURL)
    block = rpcConnection.getblock(blockHash)
    tx = block["tx"]
    for txid in tx:
        parseTx(rpcConnection, txid)
def main():
    while True:
        try:
                access = AuthServiceProxy(authserv)
                function = access.getnettotals()
                funcname = str("getnettotals")
                for subkey in ['totalbytesrecv', 'totalbytessent']:
                    value = function[subkey]
                    printValue("counter", funcname, funcname, subkey, value)

                function = access.getnetworkinfo()
                funcname = str("getnetworkinfo")
                subkey = str("connections")
                value = function[subkey]
                printValue("gauge", subkey, funcname, subkey, value)

                function = access.getmempoolinfo()
                funcname = str("getmempoolinfo")
                for subkey in ['size', 'bytes']:
                    value = function[subkey]
                    #without this it will appear "stacked" in CGP Panel
                    funccat = (str(funcname) + "_" + str(subkey))
                    printValue("gauge", funccat, funcname, subkey, value)

                #since 0.12 estimatefee 1 fails. Use estimatefee 2 instead.
                #see https://github.com/bitcoin/bitcoin/issues/7545
                function = access.estimatefee(2)
                funcname = str("estimatefee")
                value = function
                printValue("gauge", funcname, funcname, funcname, value)

                #get size, height, diff of the last block
                function = access.getblockcount()
                blockcount = function
                #get hash of last block
                function = access.getblockhash(blockcount)
                blockhash = function
                #get info from blockhash
                function = access.getblock(blockhash)
                funcname = str("getblock")
                for subkey in ['size', 'height', 'difficulty']:
                    funccat = (str(funcname) + "_" + str(subkey))
                    value = function[subkey]
                    printValue("gauge", funccat, funcname, subkey, value)

                #network hashrate
                function = access.getnetworkhashps()
                funcname = str("getnetworkhashps")
                value = function
                printValue("gauge", funcname, funcname, funcname, value)
        except:
                pass

        sys.stdout.flush()
        time.sleep(interval)
Ejemplo n.º 11
0
def search(search_term):
    try:
        rpc_user = get_bitcoin_rpc_username()
        rpc_pass = get_bitcoin_rpc_password()
        rpc_connection = AuthServiceProxy("http://%s:%[email protected]:8332" %
                                          (rpc_user, rpc_pass),
                                          timeout=10)
        results = {}
        results["type"] = "not_found"
        results["id"] = ""
        search_term = search_term.strip()

        # Try to get a block (by height)
        try:
            if (search_term.isdigit()):
                blockhash = rpc_connection.getblockhash(int(search_term))
                results["type"] = "block"
                results["id"] = blockhash
                return results
        except JSONRPCException as e:
            pass

        # Try to get address
        try:
            if is_bitcoin_address(search_term):
                results["type"] = "addr"
                results["id"] = search_term
            return results
        except:
            pass

        # Try to get a block (by hash)
        try:
            block = rpc_connection.getblock(search_term)
            results["type"] = "block"
            results["id"] = search_term
            return results
        except JSONRPCException as e:
            pass

        # Try to get a transaction
        try:
            rawtx = rpc_connection.getrawtransaction(search_term)
            results["type"] = "tx"
            results["id"] = search_term
            return results
        except JSONRPCException as e:
            pass

    except Exception as e:
        results["type"] = "error"
        results["error_message"] = str(e)
    return results
Ejemplo n.º 12
0
def main():
    # rpc_user and rpc_password are set in the bitcoin.conf file
    rpc_connection = AuthServiceProxy("http://%s:%[email protected]:8332" % (rpc_user, rpc_password))
    best_block_hash = rpc_connection.getbestblockhash()
    print(rpc_connection.getblock(best_block_hash))

    # batch support : print timestamps of blocks 0 to 99 in 2 RPC round-trips:
    commands = [["getblockhash", height] for height in range(100)]
    block_hashes = rpc_connection.batch_(commands)
    blocks = rpc_connection.batch_([["getblock", h] for h in block_hashes])
    block_times = [block["time"] for block in blocks]
    print(block_times)
Ejemplo n.º 13
0
def block_updater():
	logger = logging.getLogger('Block Updater')
	logger.setLevel(logging.INFO)
	while 1:
		rpc_connection = AuthServiceProxy(config.config["rpc_server_uri"])
		block = rpc_connection.getblock(config.config["current_block"])
		logger.info(f"Update Tip: Block={config.config['current_block']},Height={block['height']}")
		if "confirmations" in block and "nextblockhash" in block:
			config.config["current_block"] = block["nextblockhash"]
			config.save_config()
			for i in block["tx"]:
				tx = rpc_connection.decoderawtransaction(rpc_connection.getrawtransaction(i))
				decode_opreturn_msg(tx, block)
		else:
			time.sleep(6)
Ejemplo n.º 14
0
def getblockhash(height):
    """taking in block height and return block hash"""
    with open("config.yml", 'r') as ymlfile:
        cfg = yaml.load(ymlfile)
    url = cfg['bitcoind']['url']
    rpc_connection = AuthServiceProxy(url)
    blockhash = rpc_connection.getblockhash(height)
    print("blockhash of height " + str(height) + " = " + blockhash)
    print(rpc_connection.getblock(blockhash))
    print(height)
    #print(rpc_connection.getrawtransaction(''))
    rawtransaction = rpc_connection.getrawtransaction(
        '75a98ce35b869772adbf643b3f8acadfa5b46b4cd8bfef26f9e079c517018285')
    print(rawtransaction)
    tx = Transaction.from_hex(rawtransaction)
    print(tx.json().dumps(parsed, indent=4, sort_keys=True))
Ejemplo n.º 15
0
def block_page(block_hash):
    check_logged_in()

    try:
        # Get info
        rpc_user = get_bitcoin_rpc_username()
        rpc_pass = get_bitcoin_rpc_password()
        rpc_connection = AuthServiceProxy("http://%s:%[email protected]:8332" %
                                          (rpc_user, rpc_pass),
                                          timeout=10)
        block = rpc_connection.getblock(block_hash)

        txs = []
        for t in block["tx"]:
            txs.append(t)

        t = time.gmtime(int(block["time"]))
        tstring = time.strftime("%Y-%m-%d %H:%M:%S", t)

        templateData = {
            "title": "myNode Bitcoin",
            "raw": pformat(block, indent=2),
            "height": block["height"],
            "hash": block["hash"],
            "confirmations": block["confirmations"],
            "num_tx": block["nTx"],
            "difficulty": "{:.3g}".format(block["difficulty"]),
            "size": int(block["size"] / 1000),
            "date": tstring,
            "txs": txs,
            "ui_settings": read_ui_settings()
        }
        return render_template('bitcoind_block.html', **templateData)
    except Exception as e:
        templateData = {
            "title":
            "myNode Bitcoin Error",
            "message":
            Markup(
                "Error communicating with bitcoind. Node may be busy syncing.<br/><br/>{}"
                .format(str(e))),
            "back_url":
            "/bitcoind",
            "ui_settings":
            read_ui_settings()
        }
        return render_template('bitcoind_error.html', **templateData)
Ejemplo n.º 16
0
class ChainData:

    RPC_USER = RPC_NODE["user"]
    RPC_PASSWORD = RPC_NODE["password"]
    RPC_SERVER = RPC_NODE["server"]
    RPC_PORT = RPC_NODE["port"]
    MIN_BLOCK = CHAIN["start_block"]
    MIN_CONFIRMS = CHAIN["confirms"]
    COIN = CHAIN["name"]

    def __init__(self):
        self.rpc_conn = AuthServiceProxy(
            "http://%s:%s@%s:%s" %
            (self.RPC_USER, self.RPC_PASSWORD, self.RPC_SERVER, self.RPC_PORT))

    def get_blocks(self):
        resp = self.rpc_conn.getblockchaininfo()
        return resp["blocks"]

    def get_headers(self):
        resp = self.rpc_conn.getblockchaininfo()
        return resp["headers"]

    def get_blockhash(self, block):
        resp = self.rpc_conn.getblockhash(block)
        return resp

    def _get_blocktransactions(self, block):
        blockhash = self.get_blockhash(block)
        resp = self.rpc_conn.getblock(blockhash, 2)
        return resp["tx"], blockhash

    def getblock_out_balances(self, block):
        txs, blockhash = self._get_blocktransactions(block)
        balances = []
        for tx in txs:
            for iout in tx['vout']:
                if iout.get("scriptPubKey") and iout.get("scriptPubKey").get(
                        "addresses"):
                    balances.append(
                        (iout["scriptPubKey"]["addresses"][0], iout["value"]))
                    if len(iout["scriptPubKey"]["addresses"]) > 1:
                        logger.error(
                            "More than one address detected! block %s, addresses: %s"
                            % (block, iout["scriptPubKey"]["addresses"]))
        return balances, blockhash
Ejemplo n.º 17
0
def get_block_txs(height):
    """
    Method used to get tx hashes from block

    Args:
            block height (int)

    Returns:
            List of transaction ids (array)
    """
    rpc = AuthServiceProxy(
        ("http://%s:%[email protected]:%s/") %
        (config['RPC_USER'], config['RPC_PASS'], config['RPC_PORT']))

    block_hash = rpc.getblockhash(height)
    block = rpc.getblock(block_hash)

    return block['tx']
Ejemplo n.º 18
0
class ChainData:

    RPC_USER = RPC_NODE["user"]
    RPC_PASSWORD = RPC_NODE["password"]
    RPC_SERVER = RPC_NODE["server"]
    RPC_PORT = RPC_NODE["port"]

    def __init__(self):
        self.db1 = db.DataBase()
        self.rpc_conn = AuthServiceProxy(
            "http://%s:%s@%s:%s" %
            (self.RPC_USER, self.RPC_PASSWORD, self.RPC_SERVER, self.RPC_PORT))

    def get_blocks(self):
        resp = self.rpc_conn.getblockchaininfo()
        return resp["blocks"]

    def get_headers(self):
        resp = self.rpc_conn.getblockchaininfo()
        return resp["headers"]

    def get_blockhash(self, block):
        resp = self.rpc_conn.getblockhash(block)
        return resp

    def _get_blocktransactions(self, block):
        blockhash = self.get_blockhash(block)
        resp = self.rpc_conn.getblock(blockhash, 2)
        return resp["tx"], blockhash

    def getblock_out_addresses(self, block):
        txs, blockhash = self._get_blocktransactions(block)
        count = 0
        for tx in txs:
            for iout in tx['vout']:
                if iout.get("scriptPubKey") and iout.get("scriptPubKey").get(
                        "addresses"):
                    addresses = iout["scriptPubKey"]["addresses"]
                    for ad in addresses:
                        count += 1
                        self.db1.add_unique(ad)
Ejemplo n.º 19
0
def main(height, best):
    rpc_connection = AuthServiceProxy(
        'http://%s:%s@%s:%d' % (rpc_user, rpc_password, rpc_ip, rpc_port))
    es = Elasticsearch("%s:%d" % (es_ip, es_port))

    while height < best:
        print 'height: %d' % height
        try:
            block_hash = rpc_connection.getblockhash(height)
            block = rpc_connection.getblock(block_hash, 2)
        except Exception as e:
            print e
            time.sleep(3)
            rpc_connection = AuthServiceProxy(
                'http://%s:%s@%s:%d' %
                (rpc_user, rpc_password, rpc_ip, rpc_port))
        else:
            txs = block['tx']
            txs_result = txs_to_es(rpc_connection, es, txs, block)
            if txs_result == 1:
                height += 1
Ejemplo n.º 20
0
class Coind:
    def __init__(self):
        # connect to the local coin daemon.
        self.access = AuthServiceProxy("http://%s:%[email protected]:12341" %
                                       ('RPCuser', 'RPCpassword'))

    def getblockhash(self, idx):
        b_hash = self.access.getblockhash(idx)
        return (b_hash)

    def getblock(self, b_hash):
        block = self.access.getblock(b_hash)
        #{ u'merkleroot': u'74a2d1db8db7dc5e65d3d6f2058a6f1b5e893ddaf87c4c98d1a008e406b9beae',
        #    u'nonce': 122400,
        #    u'previousblockhash': u'6850dc01c014a262018fe2e29c299fc33dfe6d47fe5ef2f7cfa5f51f10bc61b3',
        #    u'hash': u'642b7b504b315dd12683eb132e9a536958a89c03638ebc7582ef4d50893f0b89',
        #    u'version': 2,
        #    u'tx': [
        #        u'46eb85610e8260a5eeccdfb14bf393b83ff704ccaca08e2dc639c2ebd9cdff57',
        #        u'dd28e708147c66b2ebaa23bfcce436afddfcdd1a268867465389c8c6d114cf82'
        #        ],
        #    u'height': 176058,
        #    u'difficulty': Decimal('587.97880435'),
        #    u'nextblockhash': u'530761664af30cc6e119cef47222ff179982cdc6e5f1fd70d06bb72bafde649c',
        #    u'confirmations': 7,
        #    u'time': 1419937013,
        #    u'bits': u'1b6f7546',
        #    u'size': 1227
        #    }
        return (block)

    def gettransaction(self, t_hash):
        try:
            trans = self.access.gettransaction(t_hash)
        except:
            return (False)
        return (trans)

    def getblockcount(self):
        return (self.access.getblockcount())
Ejemplo n.º 21
0
class Coind:
   def __init__(self):
      # connect to the local coin daemon.
      self.access = AuthServiceProxy("http://%s:%[email protected]:12341"%('RPCuser', 'RPCpassword'))

   def getblockhash(self, idx):
      b_hash = self.access.getblockhash(idx)
      return(b_hash)

   def getblock(self, b_hash):
      block = self.access.getblock(b_hash)
      #{ u'merkleroot': u'74a2d1db8db7dc5e65d3d6f2058a6f1b5e893ddaf87c4c98d1a008e406b9beae', 
      #    u'nonce': 122400, 
      #    u'previousblockhash': u'6850dc01c014a262018fe2e29c299fc33dfe6d47fe5ef2f7cfa5f51f10bc61b3', 
      #    u'hash': u'642b7b504b315dd12683eb132e9a536958a89c03638ebc7582ef4d50893f0b89', 
      #    u'version': 2, 
      #    u'tx': [
      #        u'46eb85610e8260a5eeccdfb14bf393b83ff704ccaca08e2dc639c2ebd9cdff57', 
      #        u'dd28e708147c66b2ebaa23bfcce436afddfcdd1a268867465389c8c6d114cf82'
      #        ], 
      #    u'height': 176058, 
      #    u'difficulty': Decimal('587.97880435'), 
      #    u'nextblockhash': u'530761664af30cc6e119cef47222ff179982cdc6e5f1fd70d06bb72bafde649c', 
      #    u'confirmations': 7, 
      #    u'time': 1419937013, 
      #    u'bits': u'1b6f7546', 
      #    u'size': 1227
      #    }
      return(block)

   def gettransaction(self, t_hash):
      try:
         trans = self.access.gettransaction(t_hash)
      except:
         return(False)
      return(trans)

   def getblockcount(self):
      return(self.access.getblockcount())
Ejemplo n.º 22
0
class RPCBitcoinCoreCommand:
    def __init__(self, username, password, port):
        self.rpc_connection = AuthServiceProxy("http://%s:%[email protected]:%s" %
                                               (username, password, port),
                                               timeout=1000)

    def callCommand(self, command, hash):
        if (command is None or hash is None):
            raise Exception('The command or the hash is null')
        try:
            if command == 'getrawtransaction':
                tx = self.rpc_connection.getrawtransaction(hash)
                logging.debug(tx)
                return "OK"
            elif command == 'getblock':
                blc = self.rpc_connection.getblock(hash)
                logging.debug(blc)
                return "OK"
        except JSONRPCException as jsone:
            logging.warning('Exception generated: ' + str(jsone))
            return hash
        return "KO"
Ejemplo n.º 23
0
def run_cheetah(prevheight, rpc_user, rpc_password, rpc_port, cpu):

    # rpc_user and rpc_password are set in the bitcoin.conf file
    rpc_connection = AuthServiceProxy("http://%s:%[email protected]:%d" %
                                      (rpc_user, rpc_password, rpc_port))

    best_block_hash = rpc_connection.getbestblockhash()
    bestblock = rpc_connection.getblock(best_block_hash)

    cur_epoch = int(time.time())
    bcstucktime = cur_epoch - bestblock['time']

    if bcstucktime < 0:
        print "Height {} block Time is ahead of current time {} seconds, possible miner cheating on timestamp, current {} GMT".format(
            bestblock['height'], bcstucktime, datetime.utcnow())
    mining_info = rpc_connection.getmininginfo()

    if (bcstucktime > 120):
        print "ASIC/GPU miners got stuck on NENG blockchain for {} seconds".format(
            bcstucktime)
        print "Cheetah running, block {}: {} {} GMT".format(
            bestblock['height'], bestblock['time'], datetime.utcnow())

        if not mining_info['generate']:
            print("Cheetah accelerates, NENG CPU Mining Started")
            rpc_connection.setgenerate(True, cpu)
        elif bestblock['height'] != prevheight:
            print("Cheetah jumps, NENG CPU Mining Restarted")
            rpc_connection.setgenerate(False)
            rpc_connection.setgenerate(True, cpu)
    else:
        if mining_info['generate']:
            print "block {}: {} {} GMT".format(bestblock['height'],
                                               bestblock['time'],
                                               datetime.utcnow())
            print("Cheetah rests, NENG CPU Mining Stopped")
            rpc_connection.setgenerate(False)

    return bestblock['height']
Ejemplo n.º 24
0
from bitcoinrpc.authproxy import AuthServiceProxy, JSONRPCException

CSVFiles = "D:\BTC CSV"

rpc_connection = AuthServiceProxy("http://%s:%[email protected]:8332"%("username", "password"))
blockCount = rpc_connection.getblockcount()
for i in range(100000, 100100):#blockCount):
    currentHash = rpc_connection.getblockhash(i)
    currentBlock = rpc_connection.getblock(currentHash)
    currentTime = currentBlock['time']
    for transaction in currentBlock['tx']:
        txInfo = rpc_connection.getrawtransaction(transaction)
        txDecoded = rpc_connection.decoderawtransaction(txInfo)
        processed = [txDecoded['txid'], currentTime, txDecoded['weight']]
        for output in txDecoded['vout']:
            processed.append(str(output['value']))
        print("Processed: " + str(processed))
    print("Done with block " + str(i))
class LocalBlockchainRPCReader(BlockExplorerReader):

    rpc_connection              = None
    #transaction_output_cache    = None ''' deprecated '''

    def __init__(self, database_connector = None):
        BlockExplorerReader.__init__(self, database_connector) #super

        self.rpc_connection = AuthServiceProxy(
            "http://%s:%s@%s:%s" % (self.config.RPC_USERNAME,
                                    self.config.RPC_PASSWORD,
                                    self.config.RPC_HOST,
                                    self.config.RPC_PORT))

    def get_current_blockchain_block_height(self):
        return self.rpc_connection.getblockcount() #TODO: Error checking? - This should already be an integer.

    #Retreives a list of transactions at specified block height. Each tx
    #   will be formatted as a BCI-like tuple per
    #   get_bci_like_tuple_for_tx_id().
    #param0: block_height: Height at which to get a list of txs for.
    #param1: use_tx_out_addr_cache_only (Optional): When looking up addresses
    #   for previous transactions, ONLY refer to cache in SQLite database,
    #   rather than slower option of using RPC interface. If set to True,
    #   process will sleep until the data is available in the cache. Default:
    #   False.
    def get_tx_list(self, block_height, use_tx_out_addr_cache_only = False):
        ids = self.get_tx_ids_at_height(block_height)

        txs = []
        for tx_id in ids:
            bci_like_tuple = self.get_bci_like_tuple_for_tx_id(
                tx_id, use_tx_out_addr_cache_only)
            txs.append(bci_like_tuple)
        return txs

    #Checks if the specified transaction is the first time the specified address
    #   has received funds. If it is, it will cache this for the specified
    #   block height in the database so subsequent lookups will answer
    #   correctly. IMPORTANT: This function assumes that that blocks are being
    #   processed in a complete, monotonically-increasing fashion from the
    #   genesis block. Otherwise, correct results not guaranteed! It is the
    #   caller's responsibility to ensure that enough blocks have been
    #   processed.
    def is_first_transaction_for_address(self, addr, tx_id, block_height,
                                         benchmarker = None):
        if self.database_connector.has_address_been_seen_cache_if_not(addr,
                                                                      block_height):
            dprint("Address %s at block height %d was already seen." %
                (addr, block_height))
            return False
        else:
            dprint("Address %s at block height %d has no prior tx history." %
                  (addr, block_height))
            return True

    def get_block_hash_at_height(self, block_height):
        return self.rpc_connection.getblockhash(block_height)

    def get_tx_json_for_block_hash(self, block_hash):
        return self.rpc_connection.getblock(block_hash)

    def get_tx_ids_at_height(self, block_height):
        block_hash = self.get_block_hash_at_height(block_height)
        tx_json = self.get_tx_json_for_block_hash(block_hash)
        tx_ids = []
        for tx_id in tx_json['tx']:
            tx_ids.append(tx_id)
        return tx_ids

    #Returns the transaction in raw format. If the requested transaction is
    #   the sole transaction of the genesis block, bitcoind's RPC interface
    #   will throw an error 'No information available about transaction
    #   (code -5)' so we preempt this by raising a custom error that callers
    #   should handle; iterating callers should just move onto the next tx.
    #throws: NoDataAvailableForGenesisBlockError
    def get_raw_tx(self, tx_id):
        if tx_id == ('4a5e1e4baab89f3a32518a88c31bc87f618f76673e2cc77ab2127b7af'
                     'deda33b'):
            raise custom_errors.NoDataAvailableForGenesisBlockError()
        else:
            return self.rpc_connection.getrawtransaction(tx_id)

    #Gets a human-readable string of the transaction in JSON format.
    def get_decoded_tx(self, tx_id):
        try:
            return self.rpc_connection.decoderawtransaction(
                self.get_raw_tx(tx_id))
        except custom_errors.NoDataAvailableForGenesisBlockError:
            #bitcoind won't generate this, but here's what it would look like
            genesis_json = {
                'txid':    ('4a5e1e4baab89f3a32518a88c31bc87f618f76673e2cc77ab2'
                            '127b7afdeda33b'),
                'version':  1,
                'locktime': 0,
                'vin': [{
                    "sequence":4294967295,
                    'coinbase': ('04ffff001d0104455468652054696d65732030332f4a6'
                                 '16e2f32303039204368616e63656c6c6f72206f6e2062'
                                 '72696e6b206f66207365636f6e64206261696c6f75742'
                                 '0666f722062616e6b73')
                }],
                'vout': [
                    {
                        'value': 50.00000000,
                        'n': 0,
                        'scriptPubKey': {
                            'asm': ('04678afdb0fe5548271967f1a67130b7105cd6a828'
                                    'e03909a67962e0ea1f61deb649f6bc3f4cef38c4f3'
                                    '5504e51ec112de5c384df7ba0b8d578a4c702b6bf1'
                                    '1d5f OP_CHECKSIG'),
                            'hex': ('4104678afdb0fe5548271967f1a67130b7105cd6a8'
                                    '28e03909a67962e0ea1f61deb649f6bc3f4cef38c4'
                                    'f35504e51ec112de5c384df7ba0b8d578a4c702b6b'
                                    'f11d5fac'),
                            'reqSigs': 1,
                            'type': 'pubkey',
                            'addresses': ['1A1zP1eP5QGefi2DMPTfTL5SLmv7DivfNa']
                        }
                    }
                ]
            }
            return genesis_json

    #Converts required infromation from local bitcoind RPC into a format similar
    #   to that returned by Blockchain.info's API. This helps to make the code
    #   more agnostic as to the source of blockchain data.
    #Note: When an output address cannot be decoded, BCI excludes the "addr"
    #   field from the JSON returned. Therefore, this function will do the same.
    #   See:
    #   https://blockchain.info/tx/cee16a9b222f636cd27d734da0a131cee5dd7a1d09cb5f14f4d1330b22aaa38e
    #Note: When a previous output address for an input cannot be decoded, BCI
    #   excludes the "addr" field from the JSON returned. Therefore, this
    #   function will do the same. See:
    #   https://blockchain.info/tx/8ebe1df6ebf008f7ec42ccd022478c9afaec3ca0444322243b745aa2e317c272
    #param0: tx_id: Specified transaction hash
    #param1: use_tx_out_addr_cache_only (Optional): When looking up addresses
    #   for previous transactions, ONLY refer to cache in SQLite database,
    #   rather than slower option of using RPC interface. If set to True,
    #   process will sleep until the data is available in the cache. Default:
    #   False.
    def get_bci_like_tuple_for_tx_id(self, tx_id,
                                     use_tx_out_addr_cache_only = False):
        json_tuple = {}
        json_tuple['hash'] = tx_id
        json_tuple['inputs'] = []
        json_tuple['out'] = []

        subscription = None
        if use_tx_out_addr_cache_only:
            subscription = data_subscription.TxOutputAddressCacheSubscriber(
                database = self.database_connector)

        tx_json = self.get_decoded_tx(tx_id)

        #populate input addresses
        for vin in tx_json['vin']:
            #look up address based on its previous transaction
            prev_txid = None
            if 'txid' in vin:
                prev_txid = vin['txid']
            prev_vout = None
            if 'vout' in vin:
                prev_vout_num = vin['vout'] #yes, this RPC field is poorly named
                prev_out = {'n': prev_vout_num}
                try:
                    if use_tx_out_addr_cache_only:
                        #flag specifies that we will wait for cache to catch up
                        #   before continuing this operation. Process/thread
                        #   will sleep until then.
                        subscription.next_tx_id_needed = prev_txid
                        subscription.next_prev_tx_ouput_pos_needed = prev_vout_num
                        dprint(("get_bci_like_tuple_for_tx_id: May sleep until "
                                "tx output address is cached..."))
                        subscription.do_sleep_until_producers_ready()

                    address = self.get_output_address(prev_txid, prev_vout_num)
                    prev_out['addr'] = address
                except custom_errors.PrevOutAddressCannotBeDecodedError:
                    pass
                current_input = {'prev_out': prev_out}
                json_tuple['inputs'].append(current_input)
            else:
                #If there's no index specifying the txo from prev tx, there's
                #   probably nothing to do here. Should only come up for
                #   coinbase transactions.
                continue

        #populate output addresses
        for vout in tx_json['vout']:
            output_index = vout['n']
            current_output = {'n':output_index}
            if 'scriptPubKey' in vout and 'addresses' in vout['scriptPubKey']:
                address = vout['scriptPubKey']['addresses'][0]
                current_output['addr'] = address
            json_tuple['out'].append(current_output)

        return json_tuple

    #Returns an ordered list of output addresses for the specified transaction
    #   JSON as returned by the bitcoind RPC interface. If an address cannot be
    #   decoded for one of the outputs, a value of None will be inserted
    #   at that position in the list.
    #TODO: This does not properly handle multisig outputs that list multiple
    #   addresses per output. See:
    #   http://bitcoin.stackexchange.com/questions/4687/can-a-scriptpubkey-have-multiple-addresses
    #   When support for this is added, make sure to add a test case.
    def get_output_addresses(self, tx_json):
        assert 'vout' in tx_json
        output_addresses = []
        for vout in tx_json['vout']:
            assert 'scriptPubKey' in vout
            if 'addresses' in vout['scriptPubKey']:
                ouput_address = vout['scriptPubKey']['addresses'][0]
                output_addresses.append(ouput_address)
            else:
                output_addresses.append(None)
        return output_addresses

    #Raises: custom_errors.PrevOutAddressCannotBeDecoded
    #TODO: This does not properly handle multisig outputs that list multiple
    #   addresses per output.
    def get_output_address(self, tx_id, output_index, tx_json = None):
        if USE_TX_OUTPUT_ADDR_CACHE_FIRST:
            addr = self.database_connector.get_output_address(tx_id,
                                                              output_index)
            if addr is not None:
                return addr

        #not in cache, fall back to querying RPC interface
        if tx_json is None:
            tx_json = self.get_decoded_tx(tx_id)

        if 'vout' in tx_json and len(tx_json['vout']) > output_index and \
                'scriptPubKey' in tx_json['vout'][output_index]:
            if 'addresses' not in tx_json['vout'][output_index]['scriptPubKey']:
                raise custom_errors.PrevOutAddressCannotBeDecodedError
            else:
                return tx_json['vout'][output_index]['scriptPubKey'][
                    'addresses'][0]
        else:
            msg = ("Missing element for vout in get_output_address() with tx "
                   "id %s and output index %d") % (tx_id, output_index)
            logger.log_and_die(msg)
Ejemplo n.º 26
0
class BitcoindRPCWrapper(BlockchainDataWrapper):
    '''
    A connection manager for bitcoind's RPC interface

    All RPC configuration variables will be set using the following env vars:
        * BOLTZMANN_RPC_USERNAME
        * BOLTZMANN_RPC_PASSWORD
        * BOLTZMANN_RPC_HOST
        * BOLTZMANN_RPC_PORT
    '''

    GENESIS_BLOCK = { #rpc-style format
        'txid':    ('4a5e1e4baab89f3a32518a88c31bc87f618f76673e2cc77ab2'
                    '127b7afdeda33b'),
        'version':  1,
        'locktime': 0,
        'vin': [{
            "sequence":4294967295,
            'coinbase': ('04ffff001d0104455468652054696d65732030332f4a6'
                         '16e2f32303039204368616e63656c6c6f72206f6e2062'
                         '72696e6b206f66207365636f6e64206261696c6f75742'
                         '0666f722062616e6b73')
        }],
        'vout': [
            {
                'value': 50.00000000,
                'n': 0,
                'scriptPubKey': {
                    'asm': ('04678afdb0fe5548271967f1a67130b7105cd6a828'
                            'e03909a67962e0ea1f61deb649f6bc3f4cef38c4f3'
                            '5504e51ec112de5c384df7ba0b8d578a4c702b6bf1'
                            '1d5f OP_CHECKSIG'),
                    'hex': ('4104678afdb0fe5548271967f1a67130b7105cd6a8'
                            '28e03909a67962e0ea1f61deb649f6bc3f4cef38c4'
                            'f35504e51ec112de5c384df7ba0b8d578a4c702b6b'
                            'f11d5fac'),
                    'reqSigs': 1,
                    'type': 'pubkey',
                    'addresses': ['1A1zP1eP5QGefi2DMPTfTL5SLmv7DivfNa']
                }
            }
        ]
    }

    def __init__(self):
        rpc = dict()
        rpc['username'] = _get_env('BOLTZMANN_RPC_USERNAME')
        rpc['password'] = _get_env('BOLTZMANN_RPC_PASSWORD')
        rpc['host'] = _get_env('BOLTZMANN_RPC_HOST')
        rpc['port'] = _get_env('BOLTZMANN_RPC_PORT')

        self._con = AuthServiceProxy(
            "http://{username}:{password}@{host}:{port}".format(**rpc))


    def _get_raw_tx(self, txid):
        """Returns the transaction in raw format."""
        if txid == ('4a5e1e4baab89f3a32518a88c31bc87f618f76673e2cc77ab2127b7af'
                    'deda33b'):
            raise NoDataAvailableForGenesisBlockError()
        else:
            return self._con.getrawtransaction(txid)


    def _get_decoded_tx(self, txid):
        """Gets a human-readable string of the transaction in JSON format."""
        try:
            return self._con.getrawtransaction(txid, 1)
        except NoDataAvailableForGenesisBlockError:
            #bitcoind won't generate this, but here's what it would look like
            return self.GENESIS_BLOCK


    def _get_block_height(self, txid, rpc_tx=None):
        """Return the block height at which the transaction was mined.

        Args:
            txid (str)
            rpc_tx (Optional[dict]): The data for the specified transaction, if
                it has already been queried previously from the RPC interface.

        Raises: NoBlockHeightAvailableError: If no block height found.
        """
        best_block_hash = self._con.getbestblockhash()
        best_block = self._con.getblock(best_block_hash)
        best_block_height = best_block['height']

        if rpc_tx is None:
            rpc_tx = self._get_decoded_tx(txid)

        #ready for a fun hack? We can compute block height, but only if the
        #transaction in question has at least one unspent output.
        for idx, _ in enumerate(rpc_tx['vout']):
            #Find number of confirmations for this transaction
            txout = self._con.gettxout(txid, idx)
            if txout is not None:
                return best_block_height - txout['confirmations'] + 1

        warn("Could not find number of confirmations in any txo for {0}".format(
            txid))
        return -1

    def _get_output_address(self, prev_txid, output_index, prev_tx=None):
        """Get the address associated with the specified txo.

        Args:
            prev_txid (str): The id of the tx the output belongs to
            output_index (int): Index within the outputs corresponding to the
                output
            prev_tx (Optional[dict]): The RPC-style prev tx the output belongs
                to. If not specified, this will be fetched from the RPC
                interface.

        TODO: This does not properly handle multisig outputs that list multiple
        addresses per output.

        Raises: PrevOutAddressCannotBeDecodedError
        """
        if prev_tx is None:
            prev_tx = self._get_decoded_tx(prev_txid)

        if('vout' in prev_tx and len(prev_tx['vout']) > output_index and
           'scriptPubKey' in prev_tx['vout'][output_index]):
            if 'addresses' not in prev_tx['vout'][output_index]['scriptPubKey']:
                raise PrevOutAddressCannotBeDecodedError(
                    "Can't decode address for txo {0}:{1}".format(prev_txid,
                                                                  output_index))
            else:
                return ' '.join(prev_tx['vout'][output_index]['scriptPubKey']['addresses'])
        else:
            raise PrevOutAddressCannotBeDecodedError(
                ("Missing element for vout in get_output_address() with tx "
                 "id {0} and output index {1}").format(prev_txid, output_index))

    def _rpc_to_bci_input(self, rpc_input):
        """Convert RPC-style input to BCI-style input.

        Args:
            rpc_input (dict): The RPC-style representation of an input, found in
                a transaction's 'vin' array.

        Returns: dict: Representation of input, to be appended to a list
        attribute called 'inputs'. The input contains these fields:
            * 'prev_out' (dict)
                * 'tx_index' (None)
                * 'txid' (str): previous tx id
                * 'addr' (str)
                * 'n' (int): position of txo in prev tx
                * 'value' (int)
        """
        bci_input = dict()

        #do not create prev_out field if this input is part of a coinbase
        #  transaction. This is consistent with the BCI JSON format.
        if 'txid' in rpc_input:
            bci_input['prev_out'] = dict()
            bci_input['prev_out']['tx_index'] = None
            prev_txid = rpc_input['txid']
            prev_vout_num = rpc_input['vout'] #RPC field is ambiguously named :/
            bci_input['prev_out']['n'] = prev_vout_num

            prev_tx = self._get_decoded_tx(prev_txid)
            bci_input['prev_out']['addr'] = self._get_output_address(
                prev_txid, prev_vout_num, prev_tx)
            bci_input['prev_out']['script'] = (prev_tx['vout'][prev_vout_num]
                                               ['scriptPubKey']['hex'])
            float_val = prev_tx['vout'][prev_vout_num]['value']
            bci_input['prev_out']['value'] = _float_to_satoshi(float_val)
        return bci_input


    def get_tx(self, txid, testnet):
        """Get the `Transaction` object for specified hash.
        
        Testnet parameter isn't used.
        For now, we assume that a single bitcoind is running on the machine. 
        
        The `Transaction` constructors expects these fields for a dict:
            * 'block_height' (int)
            * 'time' (None)
            * 'hash' (str)
            * 'inputs' (List[dict]):
                * 'prev_out' (dict):
                    * 'n' (int)
                    * 'value' (int)
                    * 'addr' (str)
                    * 'tx_index' (None)
            * 'out' (List[dict]): same as 'prev_out'
        """
        rpc_style_tx = self._get_decoded_tx(txid)
        rpc_style_tx['block_height'] = self._get_block_height(txid, rpc_style_tx)
        rpc_style_tx['time'] = None
        rpc_style_tx['hash'] = rpc_style_tx['txid']

        rpc_style_tx['inputs'] = list()
        for txin in rpc_style_tx['vin']:
            inpt = self._rpc_to_bci_input(txin)
            rpc_style_tx['inputs'].append(inpt)

        rpc_style_tx['out'] = list()
        for txout in rpc_style_tx['vout']:
            outpt = _rpc_to_bci_output(txout)
            rpc_style_tx['out'].append(outpt)

        return Transaction(rpc_style_tx)
Ejemplo n.º 27
0
except ImportError as exc:
    sys.stderr.write("Error: install python-bitcoinrpc (https://github.com/jgarzik/python-bitcoinrpc)\n")
    exit(-1)

twister = AuthServiceProxy(main_config['rpc_url'])

try:
    db = anydbm.open(os.path.expanduser(dbfilename), 'c')
    if not 'lastblockhash' in db.keys():
        db['lastblockhash'] = twister.getblockhash(0)
    nextHash = db['lastblockhash']
except ImportError as exc:
    sys.stderr.write("Did not manage to open databases\n")
    exit(-1)

while True:
    block = twister.getblock(nextHash)
    db['lastblockhash'] = block["hash"]
    #print str(block["height"]) + "\r",
    usernames = block["usernames"]
    for u in usernames:
        if not str(u) in db.keys():
            db['user:'******'taken'
    if block.has_key("nextblockhash"):
        nextHash = block["nextblockhash"]
    else:
        break

db.close()

Ejemplo n.º 28
0
def getBlock(i):
    rpc_connection = AuthServiceProxy("http://%s:%[email protected]:8332" %
                                      getCredentials())
    block_hash = rpc_connection.getblockhash(i)
    return rpc_connection.getblock(block_hash)
Ejemplo n.º 29
0
			elif len(str(int(round(sum(outset),-1)))) == 10:
				print('                                                 /\\')
				print('                                                /  \\')
				print('                                               /    \\')
				print('                                              /      \\')
				print('                                             /        \\')
				print('                                             '+str(int(round(sum(outset,-1)))))
				print('                                             \\        /')
				print('                                              \\      /')
				print('                                               \\    /')
				print('                                                \\  /')
				print('                                                 \\/')
			outset.clear()
	bestblockhash = rpc_doge.getbestblockhash()
	if bestblockhash not in cache:
		block = rpc_doge.getblock(bestblockhash)
		print('    _____________________ ')		
		print('   |                     |')		
		print('   |                     |')		
		print('   |                     |')		
		print('   |                     |')
		print('   |                     |')
		print('   |  wow such block     |')
		print('         '+str(len(block['tx'])))
		print('   |            many txs |')
		print('   |                     |')		
		print('   |                     |')		
		print('   |                     |')		
		print('   |                     |')
		print('   |_____________________|')
		cache.add(bestblockhash)
    def do(self):
        rpc = AuthServiceProxy('http://' + settings.BITCOIN_RPC_USERNAME + ':' + settings.BITCOIN_RPC_PASSWORD + '@' + settings.BITCOIN_RPC_IP + ':' + str(settings.BITCOIN_RPC_PORT))

        # Total number of blocks
        blocks = rpc.getblockcount()
        blocks_processed_queryset = CurrentBlockHeight.objects.order_by('-block_height')
        blocks_processed = blocks_processed_queryset[0].block_height if blocks_processed_queryset.count() else 0

        # Now incoming transactions will be processed and added to database. Transactions
        # from new blocks are selected, but also transactions from several older blocks.
        # These extra transactions are updated in case something (for example fork?) is
        # able to modify transactions in old blocks.
        EXTRA_BLOCKS_TO_PROCESS = 6
        process_since = max(0, blocks_processed - EXTRA_BLOCKS_TO_PROCESS)
        process_since_hash = rpc.getblockhash(process_since)

        # Get all old transactions, that require updating
        old_txs = Transaction.objects.filter(incoming_txid__isnull=False, block_height__gt=process_since)
        old_txs = [old_tx for old_tx in old_txs]

        txs = rpc.listsinceblock(process_since_hash)['transactions']
        for tx in txs:
            # Skip other than receiving transactions
            if tx['category'] != 'receive':
                continue

            # Skip unconfirmed transactions for now
            # TODO: Show these too!
            if 'blockhash' not in tx:
                continue

            # Get required info
            txid = tx['txid']
            address = tx['address']
            amount = tx['amount']
            block_height = rpc.getblock(tx['blockhash'])['height']
            created_at = datetime.datetime.utcfromtimestamp(tx['timereceived']).replace(tzinfo=pytz.utc)

            # Skip transaction if it doesn't belong to any Wallet
            try:
                address = Address.objects.get(address=address)
            except Address.DoesNotExist:
                continue

            # Check if transaction already exists
            already_found = False
            for old_tx in old_txs:
                if old_tx.incoming_txid == txid:
                    # Transaction already exists, so do not care about it any more
                    old_txs.remove(old_tx)
                    already_found = True
                    break

            # If transaction is new one
            if not already_found:
                new_tx = Transaction.objects.create(
                    wallet=address.wallet,
                    amount=amount,
                    description='Received',
                    incoming_txid=txid,
                    block_height=block_height,
                    receiving_address=address,
                )
                new_tx.created_at = created_at
                new_tx.save(update_fields=['created_at'])

        # Clean remaining old transactions
        for old_tx in old_txs:
            old_tx.delete()

        # Mark down what the last processed block was
        blocks = rpc.getblockcount()
        if blocks_processed_queryset.count() > 0:
            blocks_processed_queryset.update(block_height=blocks)
        else:
            CurrentBlockHeight.objects.create(block_height=blocks)
Ejemplo n.º 31
0
class Bitcoind:
	"""
	Connection to a Bitcoin daemon process.
	"""

	def __init__(self, settings):
		"""
		Arguments:
		settings: a settings object; must contain the attribute bitcoinRPCURL.

		Connects to a Bitcoin daemon process, indicated by settings.bitcoinRPCURL.
		If settings.bitcoinRPCURL is empty, this object will not be connected.
		"""

		if settings.bitcoinRPCURL != "":
			log.log("Making connection to Bitcoin daemon...")
			self.access = AuthServiceProxy(settings.bitcoinRPCURL)
			log.log("...done")
		else:
			log.log("Bitcoin-RPC URL is not set: not connecting")
			self.access = None


	def isConnected(self):
		"""
		Return value:
		bool

		Returns whether this object is connected.
		"""

		return self.access != None
		

	def getBalance(self):
		"""
		Return value:
		int, in Satoshi

		Returns the balance.
		"""

		return self.DecimaltoAmount(self.access.getbalance())


	def getBlockCount(self):
		"""
		Return value:
		int

		Returns the block count.
		"""

		return self.access.getblockcount()


	def getPrivateKey(self, address):
		"""
		Arguments:
		address: str, Base58Check-encoded address
		Return value:
		str, Base58Check-encoded private key

		Returns the private key corresponding to the given address.
		"""

		return self.access.dumpprivkey(address)


	def getTransactionHashesByBlockHeight(self, height):
		"""
		Arguments:
		height: int
		Return value:
		list of str, hexadecimal, Bitcoin hash byte order

		Returns the transaction hashes in the block (in the main chain) at the
		given height.
		"""

		bhash = self.access.getblockhash(height)
		block = self.access.getblock(bhash)
		return block["tx"]


	def getTransaction(self, thash):
		"""
		Arguments:
		thash: str, hexadecimal, Bitcoin hash byte order

		Return value:
		dict, containing:
			vin: list of dict, each element containing:
				coinbase [only for coinbase transactions]
				txid [only for non-coinbase transactions]:
					str, hexadecimal, Bitcoin hash byte order
					hash of input transaction

		Returns information about the transaction indicated by the given hash.
		"""

		return self.access.getrawtransaction(thash, 1)


	def listUnspent(self):
		"""
		Return value:
		list of dict, each element containing:
			address: 
				str, Base58Check-encoded address
			amount:
				int, in Satoshi
			scriptPubKey:
				str, binary
			txid:
				str, binary, OpenSSL byte order
			vout:
				int

		Returns information about the available unspent transaction outputs.
		"""

		ret = self.access.listunspent()
		for vout in ret:
			vout["txid"] = binascii.unhexlify(vout["txid"])[::-1] #reversed; TODO: is this the right place?
			vout["scriptPubKey"] = binascii.unhexlify(vout["scriptPubKey"])
			vout["amount"] = self.DecimaltoAmount(vout["amount"])
		return ret


	def sendRawTransaction(self, txData):
		"""
		Arguments:
		txData: str, binary

		Send the given serialized transaction over the Bitcoin network.
		"""

		self.access.sendrawtransaction(txData.encode("hex"))


	def DecimaltoAmount(self, value):
		return int(value*100000000)
Ejemplo n.º 32
0
class TwisterScraper:
    CACHE_MAX_DURATION = datetime.timedelta(7)  # ([days [, seconds [,microseconds]]])

    def __init__(self, dbPath, server='localhost', port=28332, user='******', password='******', protocol='http'):
        self.serverUrl = '{protocol}://{user}:{passwd}@{server}:{port}'.format(protocol=protocol,
                                                                               server=server,
                                                                               port=port,
                                                                               user=user,
                                                                               passwd=password)
        self.twister = AuthServiceProxy(self.serverUrl)
        self.dbFile = dbPath
        self.locService = GeoLocationService()

        try:
            with open(self.dbFile, 'rb') as dbFile:
                self.db = pickle.load(dbFile)
        except FileNotFoundError:
            self.db = TwisterDb()
            self.saveDb()

    def get_user(self, username):
        if username in self.db.users:
            return self.db.users[username]
        else:
            return None

    def scrape_users(self):
        nextHash = 0
        nextHash = self.twister.getblockhash(0)

        usernames = set()
        index = 0
        while True:
            block = self.twister.getblock(nextHash)
            self.db.lastBlockHash = block['hash']
            usernames = usernames.union(set(block['usernames']))
            if len(usernames) > index:
                index = len(usernames)
                print('Found {0} usernames'.format(index))
            if "nextblockhash" in block:
                nextHash = block["nextblockhash"]
            else:
                break

        if len(self.db.users) == 0:
            # first run
            for u in usernames:
                blankUser = User()
                blankUser.username = u
                blankUser.updateTime = datetime.datetime.now() - self.CACHE_MAX_DURATION
            self.saveDb()

        now = datetime.datetime.now()
        old_users = self.db.users.keys()
        need_refresh = [u for u in old_users if (self.db.users[u].updateTime + self.CACHE_MAX_DURATION) < now]
        new_users = usernames.difference(set(old_users))
        to_fetch = new_users.union(set(need_refresh))

        total_to_fetch = len(to_fetch)
        for n, u in enumerate(to_fetch):
            try:
                user = self._fetch_user_details(u)
                if hasattr(u, 'location'):
                    try:
                        u.locate()
                    except MaxGeoRequestsException:
                        print("Could not locate '' because of max request limit reached")
                self.db.users[user.username] = user
                if n % 5 == 0:
                    self.saveDb()
                print("({line} of {total}) Fetched {user} ...".format(user=u, line=n, total=total_to_fetch))
            except HTTPException as e:
                print("Connection error retrieving user {0}: {1}".format(u, str(e)))

    def saveDb(self):
        print("Saving db")
        try:
            with open(self.dbFile, 'wb') as dbFile:
                pickle.dump(self.db, dbFile)
        except (KeyboardInterrupt, Exception):
            print("Closing db before quitting...")
            if dbFile:
                # close the hung descriptor and re-try the dumping
                try:
                    dbFile.close()
                except Exception:
                    pass
                with open(self.dbFile, 'wb') as dbFile:
                    pickle.dump(self.db, dbFile)
                    # once clean, re-raise
                raise

    def get_posts_since(self, username, dateObj, maxNum=1000):
        since_epoch = time.mktime(dateObj.timetuple())
        all_posts = self.twister.getposts(1000, [{'username': username}])
        all_posts = sorted(all_posts, key=lambda x: x['userpost']['time'])
        index = int(len(all_posts) / 2)

        def _post_time(i):
            return all_posts[i]['userpost']['time']

        while 0 > index > len(all_posts):
            if _post_time(index - 1) < since_epoch < _post_time(index + 1):
                if _post_time(index) < since_epoch:
                    index += 1
                break
            elif _post_time(index) > since_epoch:
                index = int(index / 2)
            elif _post_time(index) < since_epoch:
                index = int(index + index / 2)

        return all_posts[index:]

    def _fetch_user_details(self, username):
        user = User()
        user.username = username

        avatarData = self.twister.dhtget(username, "avatar", "s")
        if len(avatarData) == 1:
            if 'p' in avatarData[0]:
                if 'v' in avatarData[0]['p']:
                    user.avatar = avatarData[0]['p']['v']

        profileData = self.twister.dhtget(username, 'profile', 's')
        if len(profileData) == 1:
            if 'p' in profileData[0]:
                if 'v' in profileData[0]['p']:
                    profile = profileData[0]['p']['v']
                    for key in ['location', 'url', 'bio', 'fullname']:
                        if key in profile:
                            setattr(user, key, profile[key])

        user.following = self.twister.getfollowing(username)

        user.updateTime = datetime.datetime.now()
        return user
Ejemplo n.º 33
0
from bitcoinrpc.authproxy import AuthServiceProxy, JSONRPCException
import config
import logging

logging.basicConfig()
logging.getLogger("BitcoinRPC").setLevel(logging.DEBUG)

# rpc_user and rpc_password are set in the bitcoin.conf file
rpc_connection = AuthServiceProxy("http://%s:%[email protected]:8332"%(config.rpcuser, config.rpcpassword))
block_hash = rpc_connection.getblockhash(1)
print(block_hash)
block = rpc_connection.getblock(block_hash)
print(block)

#// get the first block

#//get raw data

#// convert it to hex

#// convert to ascii

#// print output

#// check if it has any english words
Ejemplo n.º 34
0
    if len(sys.argv) > 1:
        blockHash = sys.argv[1]
    elif "crawl" in config and "nextblock" in config["crawl"]:
        blockHash = config["crawl"]["nextblock"]
    num = 100
    if len(sys.argv) > 2:
        num = int(sys.argv[2])
    if len(blockHash) != 0:
        hostURL = "http://{0}:{1}@{2}:{3}".format(
            config["rpcuser"]["rpcid"],
            config["rpcuser"]["rpcpass"],
            config["rpcsetting"]["host"],
            config["rpcsetting"]["port"]
        )
        print(hostURL)
        rpcConnection = AuthServiceProxy(hostURL)
        # for i in range(num):
        while(True):
            print(blockHash)
            block = rpcConnection.getblock(blockHash)
            blockHash = block["nextblockhash"]
            res = subprocess.run(
                ["python", "getdistribution.py", blockHash], stdout=subprocess.PIPE)
            sys.stdout.buffer.write(res.stdout)
            try:
                config.set("crawl", 'nextblock', blockHash)
                config.write(open("config.ini", 'w'))
            except Exception as e:
                print >> sys.stderr, 'Error: Could not write to config file: %s' % e
                sys.exit(1)
Ejemplo n.º 35
0
from bitcoinrpc.authproxy import AuthServiceProxy, JSONRPCException
import logging

RPC_USER = '******'
RPC_PASSWORD = '******'
BITCOIN_SERVER = '127.0.0.1:8332'
AUTHSERV = 'http://' + RPC_USER + ':' + RPC_PASSWORD + '@' + BITCOIN_SERVER
print "AUTHSERV: %s" % (AUTHSERV)

# set logging to debug
logging.basicConfig()
logging.getLogger("BitcoinRPC").setLevel(logging.DEBUG)

# rpc_user and rpc_password are set in the bitcoin.conf file
rpc_connection = AuthServiceProxy(AUTHSERV)
best_block_hash = rpc_connection.getbestblockhash()
print(rpc_connection.getblock(best_block_hash))

# batch support : print timestamps of blocks 0 to 99 in 2 RPC round-trips:
commands = [["getblockhash", height] for height in range(100)]
block_hashes = rpc_connection.batch_(commands)
blocks = rpc_connection.batch_([["getblock", h] for h in block_hashes])
block_times = [block["time"] for block in blocks]
print(block_times)
class RPCConnection:
    """Creates a continuous connection to the bitcoind RPC interface.

    RPC connections are configured by the configuration file referred to by
    `CONFIG_FILENAME`.

    Attributes:
        conn (`AuthServiceProxy`): A connection to the RPC interface.
    """
    def __init__(self):
        config_parser = ConfigParser.ConfigParser()
        config_parser.read(CONFIG_FILENAME)
        username = config_parser.get(section='RPC', option='rpc_username')
        password = config_parser.get(section='RPC', option='rpc_password')
        host = config_parser.get(section='RPC', option='rpc_host')
        port = config_parser.get(section='RPC', option='rpc_port')

        self.conn = AuthServiceProxy("http://%s:%s@%s:%s" %
                                     (username, password, host, port))

    def get_block_hash_at_height(self, block_height):
        """Get the hash of the block at the specified block height."""
        return self.conn.getblockhash(block_height)

    def get_json_for_block_hash(self, block_hash):
        """Get a JSON represntation of the specified block."""
        return self.conn.getblock(block_hash)

    def get_tx_ids_at_height(self, block_height):
        """Get a list of transaction IDs contained in the specified block."""
        block_hash = self.get_block_hash_at_height(block_height)
        tx_json = self.get_json_for_block_hash(block_hash)
        tx_ids = []
        for tx_id in tx_json['tx']:
            tx_ids.append(tx_id)
        return tx_ids

    def get_raw_tx(self, tx_id):
        """Return transaction in raw format.

        If the requested transaction is the sole transaction of the genesis
        block, bitcoind's RPC interface will throw an error 'No information
        available about transaction (code -5)' so we preempt this by raising a
        custom error that callers should handle; iterating callers should just
        move onto the next tx.
        """
        if tx_id == ('4a5e1e4baab89f3a32518a88c31bc87f618f76673e2cc77ab2127b7af'
                     'deda33b'):
            raise IndexError
        else:
            return self.conn.getrawtransaction(tx_id)

    def get_decoded_tx(self, tx_id):
        """Gets the transaction in JSON format from the RPC interface."""
        try:
            return self.conn.decoderawtransaction(self.get_raw_tx(tx_id))
        except IndexError:
            #bitcoind won't generate this, but here's what it would look like
            genesis_json = {
                'txid':    ('4a5e1e4baab89f3a32518a88c31bc87f618f76673e2cc77ab2'
                            '127b7afdeda33b'),
                'version':  1,
                'locktime': 0,
                'vin': [{
                    "sequence":4294967295,
                    'coinbase': ('04ffff001d0104455468652054696d65732030332f4a6'
                                 '16e2f32303039204368616e63656c6c6f72206f6e2062'
                                 '72696e6b206f66207365636f6e64206261696c6f75742'
                                 '0666f722062616e6b73')
                }],
                'vout': [
                    {
                        'value': 50.00000000,
                        'n': 0,
                        'scriptPubKey': {
                            'asm': ('04678afdb0fe5548271967f1a67130b7105cd6a828'
                                    'e03909a67962e0ea1f61deb649f6bc3f4cef38c4f3'
                                    '5504e51ec112de5c384df7ba0b8d578a4c702b6bf1'
                                    '1d5f OP_CHECKSIG'),
                            'hex': ('4104678afdb0fe5548271967f1a67130b7105cd6a8'
                                    '28e03909a67962e0ea1f61deb649f6bc3f4cef38c4'
                                    'f35504e51ec112de5c384df7ba0b8d578a4c702b6b'
                                    'f11d5fac'),
                            'reqSigs': 1,
                            'type': 'pubkey',
                            'addresses': ['1A1zP1eP5QGefi2DMPTfTL5SLmv7DivfNa']
                        }
                    }
                ]
            }
            return genesis_json
Ejemplo n.º 37
0
current_total_size = 0
total_size = []
list = []

# parameters for iteration printing
digits = len(str(blockchain_length - 1))
delete = "\b" * digits

start_time = time.time()

for i in range(blockchain_length + 1):
    print("{0}{1:{2}}".format(delete, i, digits), end="")
    sys.stdout.flush()
    if i == 0:
        current_block_hash = genesis_block_hash
    else:
        current_block_hash = list[0]
    # [0] first elemnent in 'list': current block next hash
    # [1] second element in 'list': current block size
    list = at(rpc_connection.getblock(current_block_hash), 'nextblockhash',
              'size')
    current_total_size += list[1]
    total_size.append(current_total_size)

x = np.arange(0, blockchain_length + 1, 1)
plt.plot(x, total_size)
plt.xlabel('block_number')
plt.ylabel('blockchain_size')
print("\nThe program took", time.time() - start_time, "seconds to run")
plt.show()
Ejemplo n.º 38
0
from bitcoinrpc.authproxy import AuthServiceProxy, JSONRPCException

# rpc_user and rpc_password are set in the bitcoin.conf file
rpc_btc = AuthServiceProxy("http://%s:%[email protected]:8332"%('rpc_user', 'rpc_password'))
blockchain = open('blockchain.txt', 'ab+') #you will need to make this blank file in your python folder before running the program
for i in range(1,416570,1): #the range of blocks to grab data for, in this case blocks 1 to 416570, incrementing 1 block at a time
        get_block_hash = rpc_btc.getblockhash(i)
        block = rpc_btc.getblock(get_block_hash)
        coinbase = rpc_btc.decoderawtransaction(rpc_btc.getrawtransaction(block['tx'][0]))
        value = coinbase['vout'][0]['value'] #this gets total block reward
        print(i)
        blockchain.write(str(block['height'])+', '+str(value)+', '+str(block['hash'])+', '+str(block['size'])+', '+str(len(block['tx']))+', '+str(block['version'])+', '+str(block['merkleroot'])+', '+str(block['time'])+', '+str(block['nonce'])+', '+str(block['bits'])+', '+str(block['difficulty'])+', '+str(block['chainwork'])+'\n')
blockchain.close()
print('done')
Ejemplo n.º 39
0
        curblock = int(progress.read())
        progress.closed
else:
    curblock = 0
rpcpipe = AuthServiceProxy('http://' + rpcuser + ':' + rpcpass + '@' +
                           rpchost + ':44663')
while (1 != 2):
    curblock = curblock + 1
    totalblk = rpcpipe.getblockcount()
    if (curblock > totalblk - txnconf):
        with open('/root/moonaudit/progress.dat', 'w') as progress:
            progress.write(str(curblock - 1))
            progress.closed
            exit()
    rawblockhash = rpcpipe.getblockhash(curblock)
    rawblockdata = rpcpipe.getblock(rawblockhash)
    print 'checking block %08d' % (curblock)
    timestamp = find_between(str(rawblockdata), 'time\': ', ', u\'bits')
    sendnum = 0
    for txhash in rawblockdata['tx']:
        sendnum = sendnum + 1
        txraw = rpcpipe.getrawtransaction(txhash)
        txdata = rpcpipe.decoderawtransaction(txraw)
        curvout = -1
        for outputs in txdata['vout']:
            curvout = curvout + 1
            address = ''
            value = 0
            address = find_between(str(outputs), '[u\'', '\']')
            value = find_between(str(outputs), 'Decimal(\'', '\')')
            if (float(str(value)) > 28999999.99999999):
Ejemplo n.º 40
0
    location = ""
    updateTime = 0

class MyDb:
    lastBlockHash = 0

try:
    db = cPickle.load(open(dbFileName))
    nextHash = db.lastBlockHash
except:
    db = MyDb()
    db.usernames = {}
    nextHash = commentchain.getblockhash(0)

while True:
    block = commentchain.getblock(nextHash)
    db.lastBlockHash = block["hash"]
    print str(block["height"]) + "\r",
    usernames = block["usernames"]
    for u in usernames:
        if not db.usernames.has_key(u):
            db.usernames[u] = User()
    if block.has_key("nextblockhash"):
        nextHash = block["nextblockhash"]
    else:
        break

now = time.time()
for u in db.usernames.keys():
    if db.usernames[u].updateTime + cacheTimeout < now:
Ejemplo n.º 41
0
    data["denom_10"] = data["denom_10"][:-3]
    data["denom_50"] = data["denom_50"][:-3]
    data["denom_100"] = data["denom_100"][:-3]
    data["denom_500"] = data["denom_500"][:-3]
    data["denom_1000"] = data["denom_1000"][:-3]
    data["denom_5000"] = data["denom_5000"][:-3]
    data["total"] = data["total"][:-3]
    data["blocks_axis"] = data["blocks_axis"][:-3]

# Add new data points
blockCount = conn.getblockcount()
while data["lastBlockNum"] + 100 <= blockCount:
    data["lastBlockNum"] += 100
    data["lastBlockHash"] = conn.getblockhash(data["lastBlockNum"])
    print("Getting block %d..." % data["lastBlockNum"])
    block = conn.getblock(data["lastBlockHash"], True)
    data["denom_1"].append(int(block["zPIVsupply"]["1"]))
    data["denom_5"].append(int(block["zPIVsupply"]["5"]))
    data["denom_10"].append(int(block["zPIVsupply"]["10"]))
    data["denom_50"].append(int(block["zPIVsupply"]["50"]))
    data["denom_100"].append(int(block["zPIVsupply"]["100"]))
    data["denom_500"].append(int(block["zPIVsupply"]["500"]))
    data["denom_1000"].append(int(block["zPIVsupply"]["1000"]))
    data["denom_5000"].append(int(block["zPIVsupply"]["5000"]))
    data["total"].append(int(block["zPIVsupply"]["total"]))
    data["blocks_axis"].append(data["lastBlockNum"])

# Save to file
try:
    with open("zdogecsupplydata.json", 'w+') as f:
        json.dump(data, f)
Ejemplo n.º 42
0
						sys.exit()

			txid = bitcoin.sendtoaddress(send_address, Decimal(args.coinAmt))
			print("sendtoaddress - Sent tx with id %s" % txid)

	elif args.command == "claim-on-sidechain":
		raw_bitcoin_tx = bitcoin.getrawtransaction(args.sidechainRcvTx, 1)
		if not "confirmations" in raw_bitcoin_tx or raw_bitcoin_tx["confirmations"] <= 10:
			print("Please wait for at least 10 confirmations on the bitcoin transaction first")
			exit(1)
		raw_bitcoin_tx_hex = bitcoin.getrawtransaction(args.sidechainRcvTx, 0)

		# Might need to calculate the scriptSig here
		secondScriptSig = "1"

		bitcoin_block = bitcoin.getblock(raw_bitcoin_tx["blockhash"])
		coinbase_txid = bitcoin_block["tx"][0]
		raw_coinbase_tx_hex = bitcoin.getrawtransaction(coinbase_txid, 0)

		spv_proof = bitcoin.gettxoutproof([coinbase_txid, args.sidechainRcvTx])

		cht = os.popen("%s %s -g -r %s -d %s -n %s" % (contracthashtool_path, testnet_arg, redeem_script, args.sidechainP2SHaddr, args.nonce))
		cht_read = cht.read()
		assert(cht.close() == None)
		raw_dest = cht_read.split("\n")[1 + is_testnet][66:]
		full_contract = cht_read.split("\n")[1 + is_testnet][26:]
		send_address = cht_read.split("\n")[3 + is_testnet][40:]
		assert(len(raw_dest) == 40)

		nout = -1
		value = -1
Ejemplo n.º 43
0
from bitcoinrpc.authproxy import AuthServiceProxy
import simplejson as json

for x in range(1, 4):
    access = AuthServiceProxy("http://*****:*****@172.17.0."+str(x+1)+":18332")
    blockbesthash = access.getbestblockhash()
    blockbest = access.getblock(blockbesthash)
    #Affiche la hauteur du dernier block présent dans chaque noeuds
    print ("node n:"+str(x)+" "+str(blockbest["height"]))

#Affichage des informations du dernier noeud
print json.dumps(access.getmininginfo(), indent=4, separators=(',', ': '))
print json.dumps(access.getpeerinfo(), indent=4, separators=(',', ': '))
print json.dumps(access.getnetworkinfo(), indent=4, separators=(',', ': '))
print json.dumps(access.getblockchaininfo(), indent=4, separators=(',', ': '))
print json.dumps(access.getrawmempool(), indent=4, separators=(',', ': '))
Ejemplo n.º 44
0
class Parsing_block():
    def __init__(self,
                 from_block=0,
                 to_block=-1,
                 db_name="pars",
                 collection="wallet7"):
        self.from_block = from_block
        self.to_block = to_block
        self.qtum = AuthServiceProxy("http://%s:%[email protected]:8333" %
                                     ("qtumuser", "qtum2018"))
        self.db_wallet = Table_new(db_name, collection)

    def block_hash_num(self, block=None):
        try:
            if not block:
                block = self.from_block
            block_hash = self.qtum.getblockhash(block)
            return block_hash
        except:
            pass

    def get_transaction_in_block(self, block_hash=None):
        try:
            if not block_hash:
                block_hash = self.block_hash_num()
            block = self.qtum.getblock(block_hash)
            list_tx = block["tx"]
            return list_tx
        except:
            pass

    def get_raw_transaction(self, transaction_blocks=None):
        try:
            if not transaction_blocks:
                transaction_blocks = self.get_transaction_in_block()
            transaction_list = []
            for transaction_block in transaction_blocks:
                try:
                    transaction_data = self.qtum.getrawtransaction(
                        transaction_block)
                except JSONRPCException:
                    try:
                        send_data = self.qtum.sendrawtransaction(
                            transaction_block)
                        pprint(send_data)
                    except JSONRPCException:
                        pass
                else:
                    transaction_list += [transaction_data]
            return transaction_list
        except:
            pass

    '''
    def insert_db(self, vout):

        for vout_i in vout:
            try:
                n_dict = {}
                script_pub_key = vout_i["scriptPubKey"]
                addresses = script_pub_key["addresses"]
                value = vout_i["value"]
                n = vout_i["n"]
                n_str = str(n)
                list_adr = []
                for iter_adr in addresses:
                    list_adr += [{iter_adr: value}]
                n_dict[n_str] = list_adr
                print(n_dict)

            except KeyError:
                pass
    '''

    def transaction_in(self, vin):
        try:
            for vin_i in vin:
                try:
                    txid = vin_i["txid"]
                    vout_num = vin_i["vout"]
                    encoded_datas = self.get_raw_transaction([txid])
                    for i in encoded_datas:
                        transaction_data = self.qtum.decoderawtransaction(i)
                        vout_prev = transaction_data["vout"]
                        vout_prev_data = vout_prev[vout_num]
                        value_dec = vout_prev_data["value"]
                        script_pub_key = vout_prev_data["scriptPubKey"]
                        addresses = script_pub_key["addresses"]
                        value_int = int(value_dec * (10**8))
                        for address in addresses:
                            news = self.db_wallet.update_inc(
                                address, "value", -value_int)
                except KeyError:
                    pass
        except:
            pass

    def transaction_out(self, vout):
        try:
            for vout_i in vout:
                try:
                    script_pub_key = vout_i["scriptPubKey"]
                    addresses = script_pub_key["addresses"]
                    value = vout_i["value"]
                    value_int = int(value * (10**8))
                    for adr in addresses:
                        if not self.db_wallet.find({'id': adr}):
                            data = self.db_wallet.insert(adr, **{"value": 0})
                        news = self.db_wallet.update_inc(
                            adr, "value", value_int)
                        #self.db_wallet.delete(adr)
                except KeyError:
                    pass
        except:
            pass

    def decode_raw_transaction(self, encoded_datas=None):
        try:
            if not encoded_datas:
                encoded_datas = self.get_raw_transaction()
            for encoded_data in encoded_datas:
                transaction_data = self.qtum.decoderawtransaction(encoded_data)
                vin = transaction_data["vin"]
                vout = transaction_data["vout"]
                self.transaction_out(vout)
                self.transaction_in(vin)
        except:
            pass

    def show_db(self):
        return self.db_wallet.show_db()
    for i in addrlist:
        #                print( i )
        thefile.write("%s\n" % i)
    thefile.close()


if __name__ == '__main__':
    # your bitcoin daemon on your server running full-node
    rpc_user = "******"
    rpc_password = "******"
    rpc_connection = AuthServiceProxy("http://%s:%[email protected]:8332" %
                                      (rpc_user, rpc_password),
                                      timeout=1200)

    best_block_hash = rpc_connection.getbestblockhash()
    blk = rpc_connection.getblock(best_block_hash)

    # dump current block count height

    print("getblockcount = %s" % rpc_connection.getblockcount())

    txlist = []
    alist = []

    for txid in blk['tx']:
        txlist.append(txid)
        al = addrlistVout(txid)
        alist.append(al)

# make list & remove duplicates in list
    newlist = []
Ejemplo n.º 46
0
class Bitcoind_Real:
    """
	Connection to a Bitcoin daemon process.
	"""

    def __init__(self, settings):
        """
		Arguments:
		settings: a settings object; must contain the attribute bitcoinRPCURL.

		Connects to a Bitcoin daemon process, indicated by settings.bitcoinRPCURL.
		If settings.bitcoinRPCURL is empty, this object will not be connected.
		"""

        if settings.bitcoinRPCURL != "":
            log.log("Making connection to Bitcoin daemon...")
            self.access = AuthServiceProxy(settings.bitcoinRPCURL)
            log.log("...done")
        else:
            log.log("Bitcoin-RPC URL is not set: not connecting")
            self.access = None

    def isConnected(self):
        """
		Return value:
		bool

		Returns whether this object is connected.
		"""

        return self.access != None

    def getBalance(self):
        """
		Return value:
		int, in Satoshi

		Returns the balance.
		"""

        return self.DecimaltoAmount(self.access.getbalance())

    def getBlockCount(self):
        """
		Return value:
		int

		Returns the block count.
		"""

        return self.access.getblockcount()

    def getNewAddress(self):
        """
		Return value:
		str, Base58Check-encoded address

		Generates and returns a new address.
		"""
        return self.access.getnewaddress()

    def getPrivateKey(self, address):
        """
		Arguments:
		address: str, Base58Check-encoded address
		Return value:
		str, Base58Check-encoded private key

		Returns the private key corresponding to the given address.
		"""

        return self.access.dumpprivkey(address)

    def getBlockInfoByBlockHeight(self, height):
        """
		Arguments:
		height: int

		Return value:
		dict; containing:
			hash: str; the block hash (hexadecimal)
			merkleroot: str; the block Merkle root (hexadecimal, Bitcoin hash byte order)
			time: int; the block timestamp (UNIX time)

		Returns information about the block (in the main chain) at the
		given height.
		"""
        bhash = self.access.getblockhash(height)
        binfo = self.access.getblock(bhash)
        return {"hash": binfo["hash"], "merkleroot": binfo["merkleroot"], "time": binfo["time"]}

    def getTransactionHashesByBlockHeight(self, height):
        """
		Arguments:
		height: int
		Return value:
		list of str, hexadecimal, Bitcoin hash byte order

		Returns the transaction hashes in the block (in the main chain) at the
		given height.
		"""

        bhash = self.access.getblockhash(height)
        block = self.access.getblock(bhash)
        return block["tx"]

    def getTransaction(self, thash):
        """
		Arguments:
		thash: str, hexadecimal, Bitcoin hash byte order

		Return value:
		dict, containing:
			vin: list of dict, each element containing:
				coinbase [only for coinbase transactions]
				txid [only for non-coinbase transactions]:
					str, hexadecimal, Bitcoin hash byte order
					hash of input transaction
			hex: str, hexadecimal, serialization of the transaction
			confirmations: int, number of confirmations

		Returns information about the transaction indicated by the given hash.
		"""

        return self.access.getrawtransaction(thash, 1)

    def importprivkey(self, privateKey, description, rescan):
        return self.access.importprivkey(privateKey, description, rescan)

    def listUnspent(self):
        """
		Return value:
		list of dict, each element containing:
			address: 
				str, Base58Check-encoded address
			amount:
				int, in Satoshi
			scriptPubKey:
				str, binary
			txid:
				str, binary, OpenSSL byte order
			vout:
				int

		Returns information about the available unspent transaction outputs.
		"""

        ret = self.access.listunspent()
        for vout in ret:
            vout["txid"] = binascii.unhexlify(vout["txid"])[::-1]  # reversed; TODO: is this the right place?
            vout["scriptPubKey"] = binascii.unhexlify(vout["scriptPubKey"])
            vout["amount"] = self.DecimaltoAmount(vout["amount"])
        return ret

    def sendRawTransaction(self, txData):
        """
		Arguments:
		txData: str, binary

		Send the given serialized transaction over the Bitcoin network.
		"""
        try:
            self.access.sendrawtransaction(txData.encode("hex"))
        except JSONRPCException as e:
            if e.error["code"] == RPC_TRANSACTION_ALREADY_IN_CHAIN:
                # It's perfectly fine (but very unlikely) that the transaction is
                # already in the block chain.
                # After all, we WANT it to end up in the block chain!
                pass
            else:
                raise

    def DecimaltoAmount(self, value):
        return int(value * 100000000)

    def handleMessage(self, msg):
        return [
            messages.BitcoinReturnValue(value=msg.function(self), ID=msg.returnID, channelIndex=msg.returnChannelIndex)
        ]