<s> """<STR_LIT>""" <EOL> import bitcoin <EOL> import json <EOL> import urllib2 <EOL> from coloredcoinlib import CTransaction , BlockchainStateBase <EOL> class UnimplementedError ( RuntimeError ) : <EOL> pass <EOL> class ChromaBlockchainState ( BlockchainStateBase ) : <EOL> tx_lookup = { } <EOL> def __init__ ( self , url_stem = "<STR_LIT>" , testnet = False ) : <EOL> """<STR_LIT>""" <EOL> self . url_stem = url_stem <EOL> def publish_tx ( self , txdata ) : <EOL> url = "<STR_LIT>" % self . url_stem <EOL> req = urllib2 . Request ( url , txdata , <EOL> { '<STR_LIT:Content-Type>' : '<STR_LIT:application/json>' } ) <EOL> f = urllib2 . urlopen ( req ) <EOL> reply = f . read ( ) <EOL> f . close ( ) <EOL> if reply [ <NUM_LIT:0> ] == '<STR_LIT:E>' or ( len ( reply ) != <NUM_LIT:64> ) : <EOL> raise Exception ( reply ) <EOL> else : <EOL> return reply <EOL> def prefetch ( self , txhash , output_set , color_desc , limit ) : <EOL> url = "<STR_LIT>" % self . url_stem <EOL> data = { '<STR_LIT>' : txhash , '<STR_LIT>' : output_set , <EOL> '<STR_LIT>' : color_desc , '<STR_LIT>' : limit } <EOL> req = urllib2 . Request ( url , json . dumps ( data ) , <EOL> { '<STR_LIT:Content-Type>' : '<STR_LIT:application/json>' } ) <EOL> f = urllib2 . urlopen ( req ) <EOL> txs = json . loads ( f . read ( ) ) <EOL> for txhash , txraw in txs . items ( ) : <EOL> self . tx_lookup [ txhash ] = txraw <EOL> f . close ( ) <EOL> return <EOL> def get_tx_blockhash ( self , txhash ) : <EOL> url = "<STR_LIT>" % self . url_stem <EOL> data = { '<STR_LIT>' : txhash } <EOL> req = urllib2 . Request ( url , json . dumps ( data ) , <EOL> { '<STR_LIT:Content-Type>' : '<STR_LIT:application/json>' } ) <EOL> f = urllib2 . urlopen ( req ) <EOL> payload = f . read ( ) <EOL> f . close ( ) <EOL> data = json . loads ( payload ) <EOL> return data [ <NUM_LIT:0> ] , data [ <NUM_LIT:1> ] <EOL> def get_block_count ( self ) : <EOL> url = "<STR_LIT>" % self . url_stem <EOL> data = urllib2 . urlopen ( url ) . read ( ) <EOL> return int ( data ) <EOL> def connected ( self ) : <EOL> try : <EOL> return bool ( self . get_block_count ( ) ) <EOL> except : <EOL> return False <EOL> def get_height ( self ) : <EOL> return self . get_block_count ( ) <EOL> def get_block_height ( self , block_hash ) : <EOL> url = "<STR_LIT>" % self . url_stem <EOL> data = json . dumps ( { <EOL> '<STR_LIT>' : block_hash , <EOL> } ) <EOL> req = urllib2 . urlopen ( urllib2 . Request ( url , <EOL> data , { '<STR_LIT:Content-Type>' : '<STR_LIT:application/json>' } ) ) <EOL> return json . loads ( req . read ( ) ) [ '<STR_LIT>' ] <EOL> def get_header ( self , height ) : <EOL> url = "<STR_LIT>" % self . url_stem <EOL> data = json . dumps ( { <EOL> '<STR_LIT>' : height , <EOL> } ) <EOL> req = urllib2 . urlopen ( urllib2 . Request ( url , <EOL> data , { '<STR_LIT:Content-Type>' : '<STR_LIT:application/json>' } ) ) <EOL> return json . loads ( req . read ( ) ) <EOL> def get_chunk ( self , index ) : <EOL> url = "<STR_LIT>" % self . url_stem <EOL> data = json . dumps ( { <EOL> '<STR_LIT:index>' : index , <EOL> } ) <EOL> req = urllib2 . urlopen ( urllib2 . Request ( url , <EOL> data , { '<STR_LIT:Content-Type>' : '<STR_LIT:application/json>' } ) ) <EOL> return req . read ( ) . encode ( '<STR_LIT>' ) <EOL> def get_merkle ( self , txhash ) : <EOL> url = "<STR_LIT>" % self . url_stem <EOL> data = json . dumps ( { <EOL> '<STR_LIT>' : txhash , <EOL> '<STR_LIT>' : self . get_tx_blockhash ( txhash ) [ <NUM_LIT:0> ] , <EOL> } ) <EOL> req = urllib2 . urlopen ( urllib2 . Request ( url , <EOL> data , { '<STR_LIT:Content-Type>' : '<STR_LIT:application/json>' } ) ) <EOL> return json . loads ( req . read ( ) ) <EOL> def get_raw ( self , txhash ) : <EOL> if self . tx_lookup . get ( txhash ) : <EOL> return self . tx_lookup [ txhash ] <EOL> url = "<STR_LIT>" % self . url_stem <EOL> data = { '<STR_LIT>' : txhash } <EOL> req = urllib2 . Request ( url , json . dumps ( data ) , <EOL> { '<STR_LIT:Content-Type>' : '<STR_LIT:application/json>' } ) <EOL> f = urllib2 . urlopen ( req ) <EOL> payload = f . read ( ) <EOL> self . tx_lookup [ txhash ] = payload <EOL> f . close ( ) <EOL> return payload <EOL> def get_tx ( self , txhash ) : <EOL> txhex = self . get_raw ( txhash ) <EOL> txbin = bitcoin . core . x ( txhex ) <EOL> tx = bitcoin . core . CTransaction . deserialize ( txbin ) <EOL> return CTransaction . from_bitcoincore ( txhash , tx , self ) <EOL> def get_mempool_txs ( self ) : <EOL> return [ ] </s>
