<s> """<STR_LIT>""" <EOL> from coloredcoinlib . store import DataStore , DataStoreConnection , unwrap1 <EOL> from coloredcoinlib . txspec import ComposedTxSpec <EOL> from txcons import RawTxSpec <EOL> from coloredcoinlib import UNCOLORED_MARKER , SimpleColorValue <EOL> def flatten1 ( lst ) : <EOL> return [ elt [ <NUM_LIT:0> ] for elt in lst ] <EOL> class CoinStore ( DataStore ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , conn ) : <EOL> """<STR_LIT>""" <EOL> super ( CoinStore , self ) . __init__ ( conn ) <EOL> if not self . table_exists ( '<STR_LIT>' ) : <EOL> self . execute ( """<STR_LIT>""" ) <EOL> self . execute ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> self . execute ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> if not self . table_exists ( '<STR_LIT>' ) : <EOL> self . execute ( """<STR_LIT>""" ) <EOL> self . execute ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> self . execute ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> def purge_coins ( self ) : <EOL> self . execute ( "<STR_LIT>" ) <EOL> self . execute ( "<STR_LIT>" ) <EOL> def delete_coin ( self , coin_id ) : <EOL> self . execute ( "<STR_LIT>" , ( coin_id , ) ) <EOL> self . execute ( "<STR_LIT>" , ( coin_id , ) ) <EOL> def add_coin ( self , address , txhash , outindex , value , script ) : <EOL> """<STR_LIT>""" <EOL> self . execute ( <EOL> """<STR_LIT>""" , <EOL> ( address , txhash , outindex , value , script ) ) <EOL> def add_spend ( self , coin_id , spend_txhash ) : <EOL> self . execute ( "<STR_LIT>" , <EOL> ( coin_id , spend_txhash ) ) <EOL> def find_coin ( self , txhash , outindex ) : <EOL> return unwrap1 ( self . execute ( "<STR_LIT>" , <EOL> ( txhash , outindex ) ) . fetchone ( ) ) <EOL> def get_coin_spends ( self , coin_id ) : <EOL> return flatten1 ( self . execute ( "<STR_LIT>" , <EOL> ( coin_id , ) ) . fetchall ( ) ) <EOL> def get_coins_for_address ( self , address ) : <EOL> """<STR_LIT>""" <EOL> return self . execute ( "<STR_LIT>" , <EOL> ( address , ) ) . fetchall ( ) <EOL> def get_coin ( self , coin_id ) : <EOL> return self . execute ( "<STR_LIT>" , <EOL> ( coin_id , ) ) . fetchone ( ) <EOL> class UTXO ( ComposedTxSpec . TxIn ) : <EOL> def __init__ ( self , utxo_data ) : <EOL> super ( UTXO , self ) . __init__ ( utxo_data [ '<STR_LIT>' ] , utxo_data [ '<STR_LIT>' ] ) <EOL> self . txhash = utxo_data [ '<STR_LIT>' ] <EOL> self . outindex = utxo_data [ '<STR_LIT>' ] <EOL> self . value = utxo_data [ '<STR_LIT:value>' ] <EOL> self . script = utxo_data [ '<STR_LIT>' ] <EOL> self . address_rec = None <EOL> self . colorvalues = None <EOL> class Coin ( UTXO ) : <EOL> def __init__ ( self , coin_manager , coin_data ) : <EOL> super ( Coin , self ) . __init__ ( coin_data ) <EOL> self . coin_id = coin_data [ '<STR_LIT:id>' ] <EOL> self . address = coin_data [ '<STR_LIT:address>' ] <EOL> self . coin_manager = coin_manager <EOL> def get_address ( self ) : <EOL> if self . address_rec : <EOL> return self . address_rec . get_address ( ) <EOL> else : <EOL> return "<STR_LIT>" <EOL> def get_colorvalues ( self ) : <EOL> if self . colorvalues : <EOL> return self . colorvalues <EOL> else : <EOL> self . colorvalues = self . coin_manager . compute_colorvalues ( self ) <EOL> return self . colorvalues <EOL> def get_spending_txs ( self ) : <EOL> return self . coin_manager . get_coin_spending_txs ( self ) <EOL> def is_spent ( self ) : <EOL> return self . coin_manager . is_coin_spent ( self ) <EOL> def is_confirmed ( self ) : <EOL> return self . coin_manager . is_coin_confirmed ( self ) <EOL> def is_valid ( self ) : <EOL> return self . coin_manager . is_coin_valid ( self ) <EOL> class CoinQuery ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , model , color_set , filter_options ) : <EOL> """<STR_LIT>""" <EOL> self . model = model <EOL> self . color_set = color_set <EOL> self . coin_manager = model . get_coin_manager ( ) <EOL> self . filter_options = filter_options <EOL> assert '<STR_LIT>' in filter_options <EOL> def coin_matches_filter ( self , coin ) : <EOL> if not coin . is_valid ( ) : <EOL> return False <EOL> if self . filter_options [ '<STR_LIT>' ] != coin . is_spent ( ) : <EOL> return False <EOL> if self . filter_options . get ( '<STR_LIT>' , False ) : <EOL> return not coin . is_confirmed ( ) <EOL> if self . filter_options . get ( '<STR_LIT>' , False ) : <EOL> return True <EOL> return coin . is_confirmed ( ) <EOL> def get_coins_for_address ( self , address_rec ) : <EOL> """<STR_LIT>""" <EOL> color_set = self . color_set <EOL> addr_color_set = address_rec . get_color_set ( ) <EOL> all_coins = filter ( <EOL> self . coin_matches_filter , <EOL> self . coin_manager . get_coins_for_address ( address_rec . get_address ( ) ) ) <EOL> cdata = self . model . ccc . colordata <EOL> address_is_uncolored = addr_color_set . color_id_set == set ( [ <NUM_LIT:0> ] ) <EOL> if address_is_uncolored : <EOL> for coin in all_coins : <EOL> coin . address_rec = address_rec <EOL> coin . colorvalues = [ SimpleColorValue ( colordef = UNCOLORED_MARKER , <EOL> value = coin . value ) ] <EOL> return all_coins <EOL> for coin in all_coins : <EOL> coin . address_rec = address_rec <EOL> coin . colorvalues = None <EOL> try : <EOL> coin . colorvalues = cdata . get_colorvalues ( <EOL> addr_color_set . color_id_set , coin . txhash , coin . outindex ) <EOL> except Exception as e : <EOL> raise <EOL> def relevant ( coin ) : <EOL> cvl = coin . colorvalues <EOL> if coin . colorvalues is None : <EOL> return False <EOL> if cvl == [ ] : <EOL> return color_set . has_color_id ( <NUM_LIT:0> ) <EOL> for cv in cvl : <EOL> if color_set . has_color_id ( cv . get_color_id ( ) ) : <EOL> return True <EOL> return False <EOL> return filter ( relevant , all_coins ) <EOL> def get_result ( self ) : <EOL> """<STR_LIT>""" <EOL> addr_man = self . model . get_address_manager ( ) <EOL> addresses = addr_man . get_addresses_for_color_set ( self . color_set ) <EOL> utxos = [ ] <EOL> for address in addresses : <EOL> utxos . extend ( self . get_coins_for_address ( address ) ) <EOL> return utxos <EOL> class CoinManager ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , model , config ) : <EOL> """<STR_LIT>""" <EOL> params = config . get ( '<STR_LIT>' , { } ) <EOL> self . model = model <EOL> self . store = CoinStore ( self . model . store_conn . conn ) <EOL> def compute_colorvalues ( self , coin ) : <EOL> wam = self . model . get_address_manager ( ) <EOL> address_rec = wam . find_address_record ( coin . address ) <EOL> if not address_rec : <EOL> raise Exception ( '<STR_LIT>' ) <EOL> color_set = address_rec . get_color_set ( ) <EOL> if color_set . uncolored_only ( ) : <EOL> return [ SimpleColorValue ( colordef = UNCOLORED_MARKER , <EOL> value = coin . value ) ] <EOL> else : <EOL> cdata = self . model . ccc . colordata <EOL> return cdata . get_colorvalues ( color_set . color_id_set , <EOL> coin . txhash , coin . outindex ) <EOL> def purge_coins ( self ) : <EOL> """<STR_LIT>""" <EOL> self . store . purge_coins ( ) <EOL> def find_coin ( self , txhash , outindex ) : <EOL> coin_id = self . store . find_coin ( txhash , outindex ) <EOL> if coin_id : <EOL> return self . get_coin ( coin_id ) <EOL> else : <EOL> return None <EOL> def get_coin ( self , coin_id ) : <EOL> coin_rec = self . store . get_coin ( coin_id ) <EOL> if coin_rec : <EOL> return Coin ( self , coin_rec ) <EOL> else : <EOL> return None <EOL> def is_coin_valid ( self , coin ) : <EOL> return self . model . get_tx_db ( ) . is_tx_valid ( coin . txhash ) <EOL> def get_coin_spending_txs ( self , coin ) : <EOL> return filter ( self . model . get_tx_db ( ) . is_tx_valid , <EOL> self . store . get_coin_spends ( coin . coin_id ) ) <EOL> def is_coin_spent ( self , coin ) : <EOL> return len ( self . get_coin_spending_txs ( coin ) ) > <NUM_LIT:0> <EOL> def is_coin_confirmed ( self , coin ) : <EOL> return self . model . get_tx_db ( ) . is_tx_confirmed ( coin . txhash ) <EOL> def get_coins_for_address ( self , address ) : <EOL> """<STR_LIT>""" <EOL> coins = [ ] <EOL> for coin_rec in self . store . get_coins_for_address ( address ) : <EOL> coin = Coin ( self , coin_rec ) <EOL> coins . append ( coin ) <EOL> return coins <EOL> def add_coin ( self , address , txhash , outindex , value , script ) : <EOL> coin_id = self . store . find_coin ( txhash , outindex ) <EOL> if coin_id is None : <EOL> self . store . add_coin ( address , txhash , outindex , value , script ) <EOL> coin_id = self . store . find_coin ( txhash , outindex ) <EOL> def get_coins_for_transaction ( self , raw_tx ) : <EOL> """<STR_LIT>""" <EOL> spent_coins = [ ] <EOL> for txin in raw_tx . composed_tx_spec . txins : <EOL> prev_txhash , prev_outindex = txin . get_outpoint ( ) <EOL> coin = self . find_coin ( prev_txhash , prev_outindex ) <EOL> if coin : <EOL> spent_coins . append ( coin ) <EOL> received_coins = [ ] <EOL> txhash = raw_tx . get_hex_txhash ( ) <EOL> for out_idx in range ( len ( raw_tx . composed_tx_spec . txouts ) ) : <EOL> coin = self . find_coin ( txhash , out_idx ) <EOL> if coin : <EOL> received_coins . append ( coin ) <EOL> return spent_coins , received_coins <EOL> def apply_tx ( self , txhash , raw_tx ) : <EOL> """<STR_LIT>""" <EOL> all_addresses = [ a . get_address ( ) for a in <EOL> self . model . get_address_manager ( ) . get_all_addresses ( ) ] <EOL> ctxs = raw_tx . composed_tx_spec <EOL> for txin in ctxs . txins : <EOL> prev_txhash , prev_outindex = txin . get_outpoint ( ) <EOL> coin_id = self . store . find_coin ( prev_txhash , prev_outindex ) <EOL> if coin_id : <EOL> self . store . add_spend ( coin_id , txhash ) <EOL> for i , txout in enumerate ( ctxs . txouts ) : <EOL> script = raw_tx . pycoin_tx . txs_out [ i ] . script . encode ( '<STR_LIT>' ) <EOL> if txout . target_addr in all_addresses : <EOL> self . add_coin ( txout . target_addr , txhash , i , <EOL> txout . value , script ) </s>
