<s> class POBColorDefinition ( GenesisColorDefinition ) : <EOL> CLASS_CODE = '<STR_LIT>' <EOL> PADDING = <NUM_LIT> <EOL> def run_kernel ( self , tx , in_colorvalues ) : <EOL> """<STR_LIT>""" <EOL> is_genesis = ( tx . hash == self . genesis [ '<STR_LIT>' ] ) <EOL> input_running_sums = [ ] <EOL> ZERO_COLORVALUE = SimpleColorValue ( colordef = self , value = <NUM_LIT:0> ) <EOL> running_sum = ZERO_COLORVALUE . clone ( ) <EOL> tx . ensure_input_values ( ) <EOL> for element in tx . inputs : <EOL> colorvalue = self . satoshi_to_color ( element . value ) <EOL> if colorvalue <= ZERO_COLORVALUE : <EOL> break <EOL> running_sum += colorvalue <EOL> input_running_sums . append ( running_sum . clone ( ) ) <EOL> output_running_sums = [ ] <EOL> running_sum = ZERO_COLORVALUE . clone ( ) <EOL> for element in tx . outputs : <EOL> colorvalue = self . satoshi_to_color ( element . value ) <EOL> if colorvalue <= ZERO_COLORVALUE : <EOL> break <EOL> running_sum += colorvalue <EOL> output_running_sums . append ( running_sum . clone ( ) ) <EOL> out_colorvalues = [ None for i in output_running_sums ] <EOL> if is_genesis : <EOL> i = self . genesis [ '<STR_LIT>' ] <EOL> out_colorvalues [ i ] = self . satoshi_to_color ( tx . outputs [ i ] . value ) <EOL> return out_colorvalues <EOL> nonnull_sequences = <NUM_LIT:0> <EOL> if in_colorvalues [ <NUM_LIT:0> ] is None : <EOL> current_sequence_is_null = True <EOL> input_color_start = None <EOL> else : <EOL> current_sequence_is_null = False <EOL> input_color_start = <NUM_LIT:0> <EOL> input_color_end = None <EOL> for i , colorvalue in enumerate ( in_colorvalues ) : <EOL> if colorvalue is not None and current_sequence_is_null : <EOL> current_sequence_is_null = False <EOL> input_color_start = i <EOL> elif colorvalue is None and not current_sequence_is_null : <EOL> current_sequence_is_null = True <EOL> input_color_end = i - <NUM_LIT:1> <EOL> nonnull_sequences += <NUM_LIT:1> <EOL> if not current_sequence_is_null : <EOL> nonnull_sequences += <NUM_LIT:1> <EOL> input_color_end = len ( in_colorvalues ) - <NUM_LIT:1> <EOL> if nonnull_sequences > <NUM_LIT:1> : <EOL> return out_colorvalues <EOL> if input_color_start == <NUM_LIT:0> : <EOL> sum_before_sequence = ZERO_COLORVALUE . clone ( ) <EOL> else : <EOL> sum_before_sequence = input_running_sums [ input_color_start - <NUM_LIT:1> ] <EOL> sum_after_sequence = input_running_sums [ input_color_end ] <EOL> if ( sum_before_sequence != ZERO_COLORVALUE <EOL> and sum_before_sequence not in output_running_sums ) or sum_after_sequence not in output_running_sums : <EOL> return out_colorvalues <EOL> if sum_before_sequence == ZERO_COLORVALUE : <EOL> output_color_start = <NUM_LIT:0> <EOL> else : <EOL> output_color_start = output_running_sums . index ( <EOL> sum_before_sequence ) + <NUM_LIT:1> <EOL> output_color_end = output_running_sums . index ( sum_after_sequence ) <EOL> for i in range ( output_color_start , output_color_end + <NUM_LIT:1> ) : <EOL> previous_sum = ZERO_COLORVALUE if i == <NUM_LIT:0> else output_running_sums [ i - <NUM_LIT:1> ] <EOL> out_colorvalues [ i ] = output_running_sums [ i ] - previous_sum <EOL> return out_colorvalues <EOL> def satoshi_to_color ( self , satoshivalue ) : <EOL> return SimpleColorValue ( colordef = self , <EOL> value = satoshivalue - self . PADDING ) <EOL> @ classmethod <EOL> def color_to_satoshi ( cls , colorvalue ) : <EOL> return colorvalue . get_value ( ) + cls . PADDING <EOL> @ classmethod <EOL> def compose_genesis_tx_spec ( cls , op_tx_spec ) : <EOL> targets = op_tx_spec . get_targets ( ) [ : ] <EOL> if len ( targets ) != <NUM_LIT:1> : <EOL> raise InvalidTargetError ( <EOL> '<STR_LIT>' ) <EOL> target = targets [ <NUM_LIT:0> ] <EOL> if target . get_colordef ( ) != GENESIS_OUTPUT_MARKER : <EOL> raise InvalidColorError ( <EOL> '<STR_LIT>' ) <EOL> fee = op_tx_spec . get_required_fee ( <NUM_LIT> ) <EOL> amount = target . colorvalue . get_satoshi ( ) <EOL> uncolored_value = SimpleColorValue ( colordef = UNCOLORED_MARKER , <EOL> value = amount ) <EOL> colorvalue = fee + uncolored_value <EOL> inputs , total = op_tx_spec . select_coins ( colorvalue ) <EOL> change = total - fee - uncolored_value <EOL> if change > <NUM_LIT:0> : <EOL> targets . append ( ColorTarget ( <EOL> op_tx_spec . get_change_addr ( UNCOLORED_MARKER ) , change ) ) <EOL> txouts = [ txspec . ComposedTxSpec . TxOut ( target . get_satoshi ( ) , <EOL> target . get_address ( ) ) <EOL> for target in targets ] <EOL> return txspec . ComposedTxSpec ( inputs , txouts ) <EOL> def compose_tx_spec ( self , op_tx_spec ) : <EOL> targets_by_color = defaultdict ( list ) <EOL> for target in op_tx_spec . get_targets ( ) : <EOL> color_def = target . get_colordef ( ) <EOL> if color_def == UNCOLORED_MARKER or isinstance ( color_def , POBColorDefinition ) : <EOL> targets_by_color [ color_def . color_id ] . append ( target ) <EOL> else : <EOL> raise InvalidColorError ( '<STR_LIT>' ) <EOL> uncolored_targets = targets_by_color . pop ( UNCOLORED_MARKER . color_id , [ ] ) <EOL> colored_inputs = [ ] <EOL> colored_targets = [ ] <EOL> for color_id , targets in targets_by_color . items ( ) : <EOL> color_def = targets [ <NUM_LIT:0> ] . get_colordef ( ) <EOL> needed_sum = ColorTarget . sum ( targets ) <EOL> inputs , total = op_tx_spec . select_coins ( needed_sum ) <EOL> change = total - needed_sum <EOL> if change > <NUM_LIT:0> : <EOL> targets . append ( ColorTarget ( <EOL> op_tx_spec . get_change_addr ( color_def ) , change ) ) <EOL> colored_inputs += inputs <EOL> colored_targets += targets <EOL> padding_needed = ( len ( colored_targets ) - len ( colored_inputs ) ) * self . PADDING <EOL> uncolored_needed = ColorTarget . sum ( uncolored_targets ) + SimpleColorValue ( colordef = UNCOLORED_MARKER , value = padding_needed ) <EOL> fee = op_tx_spec . get_required_fee ( <NUM_LIT> * ( len ( colored_inputs ) + <NUM_LIT:1> ) ) <EOL> amount_needed = uncolored_needed + fee <EOL> zero = SimpleColorValue ( colordef = UNCOLORED_MARKER , value = <NUM_LIT:0> ) <EOL> if amount_needed == zero : <EOL> uncolored_change = zero <EOL> uncolored_inputs = [ ] <EOL> else : <EOL> uncolored_inputs , uncolored_total = op_tx_spec . select_coins ( amount_needed ) <EOL> uncolored_change = uncolored_total - amount_needed <EOL> if uncolored_change > zero : <EOL> uncolored_targets . append ( <EOL> ColorTarget ( op_tx_spec . get_change_addr ( UNCOLORED_MARKER ) , <EOL> uncolored_change ) ) <EOL> txins = colored_inputs + uncolored_inputs <EOL> txouts = [ <EOL> txspec . ComposedTxSpec . TxOut ( target . get_satoshi ( ) , <EOL> target . get_address ( ) ) <EOL> for target in colored_targets ] <EOL> txouts += [ txspec . ComposedTxSpec . TxOut ( target . get_satoshi ( ) , <EOL> target . get_address ( ) ) <EOL> for target in uncolored_targets ] <EOL> return txspec . ComposedTxSpec ( txins , txouts ) <EOL> def ones ( n ) : <EOL> """<STR_LIT>""" <EOL> while n : <EOL> b = n & ( ~ n + <NUM_LIT:1> ) <EOL> i = int ( math . log ( b , <NUM_LIT:2> ) ) <EOL> yield i <EOL> n ^= b <EOL> class BFTColorDefinition ( GenesisColorDefinition ) : <EOL> CLASS_CODE = '<STR_LIT>' <EOL> def run_kernel ( self , tx , in_colorvalues ) : <EOL> """<STR_LIT>""" <EOL> tx . ensure_input_values ( ) <EOL> if tx . hash == self . genesis [ '<STR_LIT>' ] : <EOL> return [ SimpleColorValue ( colordef = self , value = out . value ) <EOL> if self . genesis [ '<STR_LIT>' ] == i else None for i , out in enumerate ( tx . outputs ) ] <EOL> nones = [ None for _ in tx . outputs ] <EOL> out_colorvalues = [ None for _ in tx . outputs ] <EOL> output_groups = { } <EOL> for inp_index in xrange ( len ( tx . inputs ) ) : <EOL> color_value = in_colorvalues [ inp_index ] <EOL> if color_value : <EOL> nSequence = tx . raw . vin [ inp_index ] . nSequence <EOL> output_group = list ( ones ( nSequence ) ) <EOL> if nSequence == <NUM_LIT:0> : <EOL> return nones <EOL> for out_idx in output_group : <EOL> if out_idx >= len ( tx . inputs ) : <EOL> return nones <EOL> if not nSequence in output_groups : <EOL> for og in output_groups : <EOL> if len ( set ( ones ( og ) ) . intersection ( output_group ) ) != <NUM_LIT:0> : <EOL> return nones <EOL> output_groups [ nSequence ] = SimpleColorValue ( colordef = self , <EOL> value = <NUM_LIT:0> ) <EOL> output_groups [ nSequence ] += color_value <EOL> for nSequence in output_groups : <EOL> output_group = list ( ones ( nSequence ) ) <EOL> in_colorvalue = output_groups [ nSequence ] <EOL> ssvalue = sum ( tx . outputs [ out_idx ] . value for out_idx in output_group ) <EOL> for n in xrange ( - <NUM_LIT> , <NUM_LIT:32> ) : <EOL> if ssvalue * <NUM_LIT:2> ** n == in_colorvalue . get_value ( ) : <EOL> for out_idx in output_group : <EOL> svalue = tx . outputs [ out_idx ] . value <EOL> out_colorvalues [ out_idx ] = SimpleColorValue ( colordef = self , value = svalue * <NUM_LIT:2> ** n , label = in_colorvalue . get_label ( ) ) <EOL> break <EOL> else : <EOL> return nones <EOL> return out_colorvalues </s>
