<s> import os <EOL> import unittest <EOL> from pycoin . tx . script import opcodes , tools <EOL> from coloredcoinlib import ( OBColorDefinition , ColorSet , <EOL> SimpleColorValue , ColorTarget , InvalidColorIdError , <EOL> UNCOLORED_MARKER , ZeroSelectError ) <EOL> from ngcccbase . asset import AssetDefinition , AdditiveAssetValue , AssetTarget <EOL> from ngcccbase . txcons import ( InvalidTargetError , InvalidTransformationError , <EOL> InsufficientFundsError , BasicTxSpec , <EOL> SimpleOperationalTxSpec , RawTxSpec , <EOL> TransactionSpecTransformer ) <EOL> from ngcccbase . pwallet import PersistentWallet <EOL> class TestTxcons ( unittest . TestCase ) : <EOL> def setUp ( self ) : <EOL> self . path = "<STR_LIT>" <EOL> self . pwallet = PersistentWallet ( self . path ) <EOL> self . config = { '<STR_LIT>' : '<STR_LIT:test>' , '<STR_LIT>' : True , '<STR_LIT>' : { <EOL> '<STR_LIT>' : self . path } , '<STR_LIT>' : False } <EOL> self . pwallet . wallet_config = self . config <EOL> self . pwallet . init_model ( ) <EOL> self . model = self . pwallet . get_model ( ) <EOL> self . colormap = self . model . get_color_map ( ) <EOL> self . colordesc0 = "<STR_LIT>" <EOL> self . colordesc1 = "<STR_LIT>" <EOL> self . colordesc2 = "<STR_LIT>" <EOL> self . colorid0 = self . colormap . resolve_color_desc ( self . colordesc0 ) <EOL> self . colorid1 = self . colormap . resolve_color_desc ( self . colordesc1 ) <EOL> self . colorid2 = self . colormap . resolve_color_desc ( self . colordesc2 ) <EOL> self . colordef0 = OBColorDefinition ( <EOL> self . colorid0 , { '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT>' : <NUM_LIT:0> } ) <EOL> self . colordef1 = OBColorDefinition ( <EOL> self . colorid1 , { '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT>' : <NUM_LIT:0> } ) <EOL> self . colordef2 = OBColorDefinition ( <EOL> self . colorid2 , { '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT>' : <NUM_LIT:0> } ) <EOL> self . asset_config = { <EOL> '<STR_LIT>' : [ '<STR_LIT>' ] , <EOL> '<STR_LIT>' : [ self . colordesc0 ] , <EOL> } <EOL> self . basset_config = { <EOL> '<STR_LIT>' : [ '<STR_LIT>' ] , <EOL> '<STR_LIT>' : [ '<STR_LIT>' ] , <EOL> } <EOL> self . asset = AssetDefinition ( self . colormap , self . asset_config ) <EOL> self . basset = AssetDefinition ( self . colormap , self . basset_config ) <EOL> self . basic = BasicTxSpec ( self . model ) <EOL> self . bbasic = BasicTxSpec ( self . model ) <EOL> wam = self . model . get_address_manager ( ) <EOL> self . address0 = wam . get_new_address ( self . asset . get_color_set ( ) ) <EOL> self . addr0 = self . address0 . get_address ( ) <EOL> self . bcolorset = ColorSet ( self . colormap , [ '<STR_LIT>' ] ) <EOL> self . baddress = wam . get_new_address ( self . bcolorset ) <EOL> self . baddr = self . baddress . get_address ( ) <EOL> self . assetvalue0 = AdditiveAssetValue ( asset = self . asset , value = <NUM_LIT:5> ) <EOL> self . assetvalue1 = AdditiveAssetValue ( asset = self . asset , value = <NUM_LIT:6> ) <EOL> self . assetvalue2 = AdditiveAssetValue ( asset = self . asset , value = <NUM_LIT:7> ) <EOL> self . bassetvalue = AdditiveAssetValue ( asset = self . basset , value = <NUM_LIT:8> ) <EOL> self . assettarget0 = AssetTarget ( self . addr0 , self . assetvalue0 ) <EOL> self . assettarget1 = AssetTarget ( self . addr0 , self . assetvalue1 ) <EOL> self . assettarget2 = AssetTarget ( self . addr0 , self . assetvalue2 ) <EOL> self . bassettarget = AssetTarget ( self . baddr , self . bassetvalue ) <EOL> self . atargets = [ self . assettarget0 , self . assettarget1 , self . assettarget2 ] <EOL> self . colorvalue0 = SimpleColorValue ( colordef = self . colordef0 , value = <NUM_LIT:5> ) <EOL> self . colortarget0 = ColorTarget ( self . addr0 , self . colorvalue0 ) <EOL> self . colorvalue1 = SimpleColorValue ( colordef = self . colordef0 , value = <NUM_LIT:6> ) <EOL> self . colortarget1 = ColorTarget ( self . addr0 , self . colorvalue1 ) <EOL> self . colorvalue2 = SimpleColorValue ( colordef = self . colordef0 , value = <NUM_LIT:7> ) <EOL> self . colortarget2 = ColorTarget ( self . addr0 , self . colorvalue2 ) <EOL> self . bcolorvalue = SimpleColorValue ( colordef = UNCOLORED_MARKER , value = <NUM_LIT:8> ) <EOL> self . bcolortarget = ColorTarget ( self . baddr , self . bcolorvalue ) <EOL> self . targets = [ self . colortarget0 , self . colortarget1 , <EOL> self . colortarget2 ] <EOL> self . transformer = TransactionSpecTransformer ( self . model , self . config ) <EOL> self . blockhash = '<STR_LIT>' <EOL> self . txhash = '<STR_LIT>' <EOL> def test_basic ( self ) : <EOL> self . assertRaises ( InvalidTargetError , self . basic . is_monocolor ) <EOL> self . assertRaises ( InvalidTargetError , <EOL> self . basic . add_target , self . colortarget0 ) <EOL> self . basic . add_target ( self . assettarget0 ) <EOL> self . basic . add_target ( self . assettarget1 ) <EOL> self . basic . add_target ( self . assettarget2 ) <EOL> self . assertEqual ( self . basic . is_monocolor ( ) , True ) <EOL> self . assertEqual ( self . basic . is_monoasset ( ) , True ) <EOL> self . assertEqual ( self . basic . targets , self . atargets ) <EOL> self . basic . add_target ( self . bassettarget ) <EOL> self . assertEqual ( self . basic . is_monoasset ( ) , False ) <EOL> self . assertEqual ( self . basic . is_monocolor ( ) , False ) <EOL> self . assertRaises ( InvalidTransformationError , <EOL> self . basic . make_operational_tx_spec , self . asset ) <EOL> def add_coins ( self ) : <EOL> script = tools . compile ( <EOL> "<STR_LIT>" . format ( <EOL> self . address0 . rawPubkey ( ) . encode ( "<STR_LIT>" ) ) ) . encode ( "<STR_LIT>" ) <EOL> self . model . utxo_man . store . add_utxo ( self . addr0 , self . txhash , <EOL> <NUM_LIT:0> , <NUM_LIT:100> , script ) <EOL> script = tools . compile ( <EOL> "<STR_LIT>" . format ( <EOL> self . baddress . rawPubkey ( ) . encode ( "<STR_LIT>" ) ) ) . encode ( "<STR_LIT>" ) <EOL> self . model . utxo_man . store . add_utxo ( self . baddr , self . txhash , <EOL> <NUM_LIT:1> , <NUM_LIT> , script ) <EOL> self . model . ccc . metastore . set_as_scanned ( self . colorid0 , self . blockhash ) <EOL> self . model . ccc . cdstore . add ( self . colorid0 , self . txhash , <NUM_LIT:0> , <NUM_LIT:100> , '<STR_LIT>' ) <EOL> def test_operational ( self ) : <EOL> self . basic . add_target ( self . assettarget0 ) <EOL> self . basic . add_target ( self . assettarget1 ) <EOL> self . basic . add_target ( self . assettarget2 ) <EOL> op = self . transformer . transform_basic ( self . basic , '<STR_LIT>' ) <EOL> self . assertTrue ( self . transformer . classify_tx_spec ( op ) , '<STR_LIT>' ) <EOL> self . assertRaises ( InvalidTargetError , op . add_target , <NUM_LIT:1> ) <EOL> self . assertEqual ( ColorTarget . sum ( op . get_targets ( ) ) , <EOL> ColorTarget . sum ( self . targets ) ) <EOL> self . assertEqual ( op . get_change_addr ( self . colordef0 ) , self . addr0 ) <EOL> self . assertEqual ( op . get_change_addr ( UNCOLORED_MARKER ) , self . baddr ) <EOL> self . assertEqual ( op . get_required_fee ( <NUM_LIT:1> ) . get_value ( ) , <NUM_LIT> ) <EOL> self . assertRaises ( InvalidColorIdError , op . get_change_addr , <EOL> self . colordef1 ) <EOL> cv = SimpleColorValue ( colordef = self . colordef0 , value = <NUM_LIT:0> ) <EOL> self . assertRaises ( ZeroSelectError , op . select_coins , cv ) <EOL> cv = SimpleColorValue ( colordef = self . colordef0 , value = <NUM_LIT:5> ) <EOL> self . assertRaises ( InsufficientFundsError , op . select_coins , cv ) <EOL> self . add_coins ( ) <EOL> self . assertEqual ( op . select_coins ( cv ) [ <NUM_LIT:1> ] . get_value ( ) , <NUM_LIT:100> ) <EOL> def test_composed ( self ) : <EOL> self . basic . add_target ( self . assettarget0 ) <EOL> self . basic . add_target ( self . assettarget1 ) <EOL> self . basic . add_target ( self . assettarget2 ) <EOL> self . add_coins ( ) <EOL> op = self . transformer . transform ( self . basic , '<STR_LIT>' ) <EOL> self . assertEqual ( op . get_change_addr ( self . colordef0 ) , self . addr0 ) <EOL> self . assertEqual ( op . get_change_addr ( UNCOLORED_MARKER ) , self . baddr ) <EOL> comp = self . transformer . transform ( op , '<STR_LIT>' ) <EOL> self . assertTrue ( self . transformer . classify_tx_spec ( comp ) , '<STR_LIT>' ) <EOL> signed = self . transformer . transform ( comp , '<STR_LIT>' ) <EOL> self . assertTrue ( self . transformer . classify_tx_spec ( signed ) , '<STR_LIT>' ) <EOL> self . assertEqual ( len ( signed . get_hex_txhash ( ) ) , <NUM_LIT:64> ) <EOL> txdata = signed . get_tx_data ( ) <EOL> same = RawTxSpec . from_tx_data ( self . model , txdata ) <EOL> self . assertEqual ( same . get_hex_tx_data ( ) , signed . get_hex_tx_data ( ) ) <EOL> self . assertRaises ( InvalidTransformationError , <EOL> self . transformer . transform , <EOL> signed , '<STR_LIT>' ) <EOL> def test_other ( self ) : <EOL> self . assertEqual ( self . transformer . classify_tx_spec ( <NUM_LIT:1> ) , None ) <EOL> self . assertRaises ( InvalidTransformationError , <EOL> self . transformer . transform_basic , <EOL> self . basic , '<STR_LIT>' ) <EOL> self . assertRaises ( InvalidTransformationError , <EOL> self . transformer . transform_operational , <EOL> self . basic , '<STR_LIT>' ) <EOL> self . assertRaises ( InvalidTransformationError , <EOL> self . transformer . transform_composed , <EOL> self . basic , '<STR_LIT>' ) <EOL> self . assertRaises ( InvalidTransformationError , <EOL> self . transformer . transform_signed , <EOL> self . basic , '<STR_LIT>' ) <EOL> self . assertRaises ( InvalidTransformationError , <EOL> self . transformer . transform , <EOL> '<STR_LIT>' , '<STR_LIT>' ) <EOL> self . add_coins ( ) <EOL> self . bbasic . add_target ( self . bassettarget ) <EOL> signed = self . transformer . transform ( self . bbasic , '<STR_LIT>' ) <EOL> self . assertEqual ( len ( signed . get_hex_txhash ( ) ) , <NUM_LIT:64> ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> unittest . main ( ) </s>
