def init_blockchain_state(self, config): thin = config.get('thin', True) if thin and not config.get('use_bitcoind', False): chromanode_url = config.get('chromanode_url', None) if not chromanode_url: if self.testnet: chromanode_url = "http://chromanode-tn.bitcontracts.org" else: chromanode_url = "http://chromanode.bitcontracts.org" self.blockchain_state = ChromaBlockchainState( chromanode_url, self.testnet) else: self.blockchain_state = BlockchainState.from_url( None, self.testnet) if not thin and not self.testnet: try: # try fetching transaction from the second block of # the bitcoin blockchain to see whether txindex works self.blockchain_state.bitcoind.getrawtransaction( "9b0fc92260312ce44e74ef369f5c66bbb85848f2eddd5" "a7a1cde251e54ccfdd5") except Exception as e: # use Electrum to request transactions self.blockchain_state = EnhancedBlockchainState( "electrum.cafebitcoin.com", 50001)
def init_blockchain_state(self, config): thin = config.get('thin', True) if thin and not config.get('use_bitcoind', False): chromanode_url = config.get('chromanode_url', None) if not chromanode_url: if self.testnet: chromanode_url = "http://54.77.120.222" else: chromanode_url = "http://54.77.120.222" self.blockchain_state = ChromaBlockchainState( chromanode_url, self.testnet) else: self.blockchain_state = BlockchainState.from_url( None, self.testnet) if not thin and not self.testnet: try: # try fetching transaction from the second block of # the bitcoin blockchain to see whether txindex works self.blockchain_state.bitcoind.getrawtransaction( "ff778bf7c173a6c20baab100ac0436f69c9d7a797cab3" "51b26df19c35985db0e") except Exception as e: # use Electrum to request transactions self.blockchain_state = EnhancedBlockchainState( "myr.electr.us", 50009)
def __init__(self, config): """Creates a Colored Coin Context given a config <config> """ params = config.get('ccc', {}) self.testnet = config.get('testnet', False) self.blockchain_state = BlockchainState.from_url(None, self.testnet) if not self.testnet: try: # try fetching transaction from the second block of # the bitcoin blockchain to see whether txindex works self.blockchain_state.bitcoind.getrawtransaction( "9b0fc92260312ce44e74ef369f5c66bbb85848f2eddd5" "a7a1cde251e54ccfdd5") except Exception as e: # use Electrum to request transactions self.blockchain_state = EnhancedBlockchainState( "electrum.cafebitcoin.com", 50001) self.store_conn = DataStoreConnection( params.get("colordb_path", "color.db")) self.cdstore = ColorDataStore(self.store_conn.conn) self.metastore = ColorMetaStore(self.store_conn.conn) self.colormap = ColorMap(self.metastore) cdbuilder = ColorDataBuilderManager( self.colormap, self.blockchain_state, self.cdstore, self.metastore, FullScanColorDataBuilder) self.colordata = ThickColorData( cdbuilder, self.blockchain_state, self.cdstore)
def POST(self): # data is sent in as json data = json.loads(web.data()) self.require(data, 'txhash', "TX requires txhash") txhash = data.get('txhash') print txhash blockchainstate = BlockchainState.from_url(None, testnet) return blockchainstate.get_raw(txhash)
def POST(self): # data is sent in as json data = json.loads(web.data()) self.require(data, 'txhash', "TX requires txhash") txhash = data.get('txhash') print txhash blockchainstate = BlockchainState.from_url(None, testnet) blockhash, in_mempool = blockchainstate.get_tx_blockhash(txhash) return json.dumps([blockhash, in_mempool])
def POST(self): txdata = web.data() reply = None try: blockchainstate = BlockchainState.from_url(None, testnet) reply = blockchainstate.bitcoind.sendrawtransaction(txdata) except Exception as e: reply = ("Error: " + str(e)) return reply
def __init__(self, config): """Creates a Colored Coin Context given a config <config> """ params = config.get('ccc', {}) thin = params.get('thin', True) self.testnet = config.get('testnet', False) if thin: color_data_class = ThinColorData color_data_builder = AidedColorDataBuilder else: color_data_class = ThickColorData color_data_builder = FullScanColorDataBuilder if thin and not params.get('use_bitcoind', False): chromanode_url = params.get('chromanode_url', None) if not chromanode_url: if self.testnet: chromanode_url = "http://chromanode-tn.bitcontracts.org" else: chromanode_url = "http://chromanode.bitcontracts.org" self.blockchain_state = ChromaBlockchainState( chromanode_url, self.testnet) else: self.blockchain_state = BlockchainState.from_url( None, self.testnet) if not thin and not self.testnet: try: # try fetching transaction from the second block of # the bitcoin blockchain to see whether txindex works self.blockchain_state.bitcoind.getrawtransaction( "9b0fc92260312ce44e74ef369f5c66bbb85848f2eddd5" "a7a1cde251e54ccfdd5") except Exception as e: # use Electrum to request transactions self.blockchain_state = EnhancedBlockchainState( "electrum.cafebitcoin.com", 50001) self.store_conn = DataStoreConnection( params.get("colordb_path", "color.db")) self.cdstore = ColorDataStore(self.store_conn.conn) self.metastore = ColorMetaStore(self.store_conn.conn) self.colormap = ColorMap(self.metastore) cdbuilder = ColorDataBuilderManager( self.colormap, self.blockchain_state, self.cdstore, self.metastore, color_data_builder) self.colordata = color_data_class( cdbuilder, self.blockchain_state, self.cdstore, self.colormap)
def POST(self): blockchainstate = BlockchainState.from_url(None, testnet) data = json.loads(web.data()) block_hash = data.get('block_hash') if not block_hash: self.require(data, 'height', "block_hash or height required") height = data.get('height') block_hash = blockchainstate.get_block_hash(height) block = blockchainstate.get_block(block_hash) return json.dumps({ 'block_height': block['height'], 'version': block['version'], 'prev_block_hash': block['previousblockhash'], 'merkle_root': block['merkleroot'], 'timestamp': block['time'], 'bits': int(block['bits'], 16), 'nonce': block['nonce'], }, cls=DecimalEncoder)
def run(self): self.headers = open(HEADERS_FILE, 'ab+').read() with self.lock: self.running = True run_time = time.time() while self.is_running(): if run_time > time.time(): time.sleep(0.05) continue run_time = time.time() + 1 try: blockchainstate = BlockchainState.from_url(None, testnet) height = blockchainstate.get_block_count() if height == self.height: continue if height < self.height: self.headers = self.headers[:height*80] while height > self.height: if not self.is_running(): break block_height = self.height + 1 blockhash = blockchainstate.get_block_hash(block_height) block = blockchainstate.get_block(blockhash) if block_height == 0: self.headers = self._header_to_string(block) else: prev_hash = self._hash_header(self.headers[-80:]) if prev_hash == block['previousblockhash']: self.headers += self._header_to_string(block) else: self.headers = self.headers[:-80] open(HEADERS_FILE, 'wb').write(self.headers) except httplib.BadStatusLine: pass # bad connection, try again later except SocketError: pass # bad connection, try again later except JSONRPCException as e: if e.error["code"] != -28: raise # Not error we are looking for pass # Loading block index... , try again later
def POST(self): blockchainstate = BlockchainState.from_url(None, testnet) # data is sent in as json data = json.loads(web.data()) self.require(data, 'txhash', "Prefetch requires txhash") self.require(data, 'output_set', "Prefetch requires output_set") self.require(data, 'color_desc', "Prefetch requires color_desc") txhash = data.get('txhash') output_set = data.get('output_set') color_desc = data.get('color_desc') limit = data.get('limit') # note the id doesn't actually matter we need to add it so # we have a valid color definition color_def = ColorDefinition.from_color_desc(9999, color_desc) # gather all the transactions and return them tx_lookup = {} def process(current_txhash, current_outindex): """For any tx out, process the colorvalues of the affecting inputs first and then scan that tx. """ if limit and len(tx_lookup) > limit: return if tx_lookup.get(current_txhash): return current_tx = blockchainstate.get_tx(current_txhash) if not current_tx: return tx_lookup[current_txhash] = blockchainstate.get_raw(current_txhash) # note a genesis tx will simply have 0 affecting inputs inputs = set() inputs = inputs.union( color_def.get_affecting_inputs(current_tx, [current_outindex])) for i in inputs: process(i.prevout.hash, i.prevout.n) for oi in output_set: process(txhash, oi) return tx_lookup
def POST(self): data = json.loads(web.data()) self.require(data, 'txhash', "Merkle requires txhash") self.require(data, 'blockhash', "Merkle requires blockhash") txhash = data.get('txhash') blockhash = data.get('blockhash') hash_decode = lambda x: x.decode('hex')[::-1] hash_encode = lambda x: x[::-1].encode('hex') Hash = lambda x: hashlib.sha256(hashlib.sha256(x).digest()).digest() blockchainstate = BlockchainState.from_url(None, testnet) b = blockchainstate.get_block(blockhash) tx_list = b.get('tx') tx_pos = tx_list.index(txhash) merkle = map(hash_decode, tx_list) target_hash = hash_decode(txhash) s = [] while len(merkle) != 1: if len(merkle) % 2: merkle.append(merkle[-1]) n = [] while merkle: new_hash = Hash(merkle[0] + merkle[1]) if merkle[0] == target_hash: s.append(hash_encode(merkle[1])) target_hash = new_hash elif merkle[1] == target_hash: s.append(hash_encode(merkle[0])) target_hash = new_hash n.append(new_hash) merkle = merkle[2:] merkle = n return json.dumps({"block_height": b.get('height'), "merkle": s, "pos": tx_pos})
#!/usr/bin/env python import json import sys import web from coloredcoinlib import BlockchainState, ColorDefinition blockchainstate = BlockchainState.from_url(None, True) urls = ( '/tx', 'Tx', '/prefetch', 'Prefetch', ) class ErrorThrowingRequestProcessor: def require(self, data, key, message): value = data.get(key) if not value: raise web.HTTPError("400 Bad request!", {"content-type": "text/plain"}, message) class Tx(ErrorThrowingRequestProcessor): def POST(self): # data is sent in as json data = json.loads(web.data()) self.require(data, 'txhash', "TX requires txhash")
def __init__(self, *args, **kwargs): threading.Thread.__init__(self, *args, **kwargs) self.running = False self.lock = threading.Lock() self.blockchainstate = BlockchainState.from_url(None, testnet) self.headers = ''
'/prefetch', 'Prefetch', '/blockcount', 'BlockCount', '/header', 'Header', '/chunk', 'Chunk', '/merkle', 'Merkle' ) testnet = False if (len(sys.argv) > 2) and (sys.argv[2] == 'testnet'): testnet = True HEADERS_FILE = 'headers.testnet' if testnet else 'headers.mainnet' if (len(sys.argv) > 3): HEADERS_FILE = sys.argv[3] blockchainstate = BlockchainState.from_url(None, testnet) class ErrorThrowingRequestProcessor: def require(self, data, key, message): value = data.get(key, None) if value is None: raise web.HTTPError("400 Bad request", {"content-type": "text/plain"}, message) class Tx(ErrorThrowingRequestProcessor): def POST(self): # data is sent in as json data = json.loads(web.data())
def GET(self): blockchainstate = BlockchainState.from_url(None, testnet) return str(blockchainstate.get_block_count())
import web from coloredcoinlib import BlockchainState, ColorDefinition urls = ( '/tx', 'Tx', '/publish_tx', 'PublishTx', '/tx_blockhash', 'TxBlockhash', '/prefetch', 'Prefetch', '/blockcount', 'BlockCount' ) testnet = False if (len(sys.argv) > 2) and (sys.argv[2] == 'testnet'): testnet = True blockchainstate = BlockchainState.from_url(None, testnet) class ErrorThrowingRequestProcessor: def require(self, data, key, message): value = data.get(key) if not value: raise web.HTTPError("400 Bad request", {"content-type": "text/plain"}, message) class PublishTx(ErrorThrowingRequestProcessor): def POST(self): txdata = web.data() reply = None