idx int64 0 251k | question stringlengths 53 3.53k | target stringlengths 5 1.23k | len_question int64 20 893 | len_target int64 3 238 |
|---|---|---|---|---|
224,100 | def consume_gas ( self , amount : int , reason : str ) -> None : return self . _gas_meter . consume_gas ( amount , reason ) | Consume amount of gas from the remaining gas . Raise eth . exceptions . OutOfGas if there is not enough gas remaining . | 34 | 26 |
224,101 | def stack_pop ( self , num_items : int = 1 , type_hint : str = None ) -> Any : # TODO: Needs to be replaced with # `Union[int, bytes, Tuple[Union[int, bytes], ...]]` if done properly return self . _stack . pop ( num_items , type_hint ) | Pop and return a number of items equal to num_items from the stack . type_hint can be either uint256 or bytes . The return value will be an int or bytes type depending on the value provided for the type_hint . | 76 | 50 |
224,102 | def stack_push ( self , value : Union [ int , bytes ] ) -> None : return self . _stack . push ( value ) | Push value onto the stack . | 29 | 6 |
224,103 | def prepare_child_message ( self , gas : int , to : Address , value : int , data : BytesOrView , code : bytes , * * kwargs : Any ) -> Message : kwargs . setdefault ( 'sender' , self . msg . storage_address ) child_message = Message ( gas = gas , to = to , value = value , data = data , code = code , depth = self . msg . depth + 1 , * * kwargs ) return child_message | Helper method for creating a child computation . | 109 | 8 |
224,104 | def apply_child_computation ( self , child_msg : Message ) -> 'BaseComputation' : child_computation = self . generate_child_computation ( child_msg ) self . add_child_computation ( child_computation ) return child_computation | Apply the vm message child_msg as a child computation . | 67 | 12 |
224,105 | def _get_log_entries ( self ) -> List [ Tuple [ int , bytes , List [ int ] , bytes ] ] : if self . is_error : return [ ] else : return sorted ( itertools . chain ( self . _log_entries , * ( child . _get_log_entries ( ) for child in self . children ) ) ) | Return the log entries for this computation and its children . | 81 | 11 |
224,106 | def apply_computation ( cls , state : BaseState , message : Message , transaction_context : BaseTransactionContext ) -> 'BaseComputation' : with cls ( state , message , transaction_context ) as computation : # Early exit on pre-compiles if message . code_address in computation . precompiles : computation . precompiles [ message . code_address ] ( computation ) return computation show_debug2 = computation . logger . show_debug2 for opcode in computation . code : opcode_fn = computation . get_opcode_fn ( opcode ) if show_debug2 : computation . logger . debug2 ( "OPCODE: 0x%x (%s) | pc: %s" , opcode , opcode_fn . mnemonic , max ( 0 , computation . code . pc - 1 ) , ) try : opcode_fn ( computation = computation ) except Halt : break return computation | Perform the computation that would be triggered by the VM message . | 202 | 13 |
224,107 | def compute_homestead_difficulty ( parent_header : BlockHeader , timestamp : int ) -> int : parent_tstamp = parent_header . timestamp validate_gt ( timestamp , parent_tstamp , title = "Header.timestamp" ) offset = parent_header . difficulty // DIFFICULTY_ADJUSTMENT_DENOMINATOR sign = max ( 1 - ( timestamp - parent_tstamp ) // HOMESTEAD_DIFFICULTY_ADJUSTMENT_CUTOFF , - 99 ) difficulty = int ( max ( parent_header . difficulty + offset * sign , min ( parent_header . difficulty , DIFFICULTY_MINIMUM ) ) ) num_bomb_periods = ( ( parent_header . block_number + 1 ) // BOMB_EXPONENTIAL_PERIOD ) - BOMB_EXPONENTIAL_FREE_PERIODS if num_bomb_periods >= 0 : return max ( difficulty + 2 ** num_bomb_periods , DIFFICULTY_MINIMUM ) else : return difficulty | Computes the difficulty for a homestead block based on the parent block . | 239 | 15 |
224,108 | def snapshot ( self ) -> Tuple [ Hash32 , UUID ] : return self . state_root , self . _account_db . record ( ) | Perform a full snapshot of the current state . | 33 | 10 |
224,109 | def revert ( self , snapshot : Tuple [ Hash32 , UUID ] ) -> None : state_root , account_snapshot = snapshot # first revert the database state root. self . _account_db . state_root = state_root # now roll the underlying database back self . _account_db . discard ( account_snapshot ) | Revert the VM to the state at the snapshot | 73 | 11 |
224,110 | def get_computation ( self , message : Message , transaction_context : 'BaseTransactionContext' ) -> 'BaseComputation' : if self . computation_class is None : raise AttributeError ( "No `computation_class` has been set for this State" ) else : computation = self . computation_class ( self , message , transaction_context ) return computation | Return a computation instance for the given message and transaction_context | 82 | 12 |
224,111 | def apply_transaction ( self , transaction : BaseOrSpoofTransaction ) -> 'BaseComputation' : if self . state_root != BLANK_ROOT_HASH and not self . _account_db . has_root ( self . state_root ) : raise StateRootNotFound ( self . state_root ) else : return self . execute_transaction ( transaction ) | Apply transaction to the vm state | 83 | 6 |
224,112 | def get_env_value ( name : str , required : bool = False , default : Any = empty ) -> str : if required and default is not empty : raise ValueError ( "Using `default` with `required=True` is invalid" ) elif required : try : value = os . environ [ name ] except KeyError : raise KeyError ( "Must set environment variable {0}" . format ( name ) ) else : value = os . environ . get ( name , default ) return value | Core function for extracting the environment variable . | 107 | 8 |
224,113 | def make_receipt ( self , base_header : BlockHeader , transaction : BaseTransaction , computation : BaseComputation , state : BaseState ) -> Receipt : raise NotImplementedError ( "VM classes must implement this method" ) | Generate the receipt resulting from applying the transaction . | 53 | 10 |
224,114 | def execute_bytecode ( self , origin : Address , gas_price : int , gas : int , to : Address , sender : Address , value : int , data : bytes , code : bytes , code_address : Address = None , ) -> BaseComputation : if origin is None : origin = sender # Construct a message message = Message ( gas = gas , to = to , sender = sender , value = value , data = data , code = code , code_address = code_address , ) # Construction a tx context transaction_context = self . state . get_transaction_context_class ( ) ( gas_price = gas_price , origin = origin , ) # Execute it in the VM return self . state . get_computation ( message , transaction_context ) . apply_computation ( self . state , message , transaction_context , ) | Execute raw bytecode in the context of the current state of the virtual machine . | 185 | 17 |
224,115 | def import_block ( self , block : BaseBlock ) -> BaseBlock : if self . block . number != block . number : raise ValidationError ( "This VM can only import blocks at number #{}, the attempted block was #{}" . format ( self . block . number , block . number , ) ) self . block = self . block . copy ( header = self . configure_header ( coinbase = block . header . coinbase , gas_limit = block . header . gas_limit , timestamp = block . header . timestamp , extra_data = block . header . extra_data , mix_hash = block . header . mix_hash , nonce = block . header . nonce , uncles_hash = keccak ( rlp . encode ( block . uncles ) ) , ) , uncles = block . uncles , ) # we need to re-initialize the `state` to update the execution context. self . _state = self . build_state ( self . chaindb . db , self . header , self . previous_hashes ) # run all of the transactions. new_header , receipts , _ = self . apply_all_transactions ( block . transactions , self . header ) self . block = self . set_block_transactions ( self . block , new_header , block . transactions , receipts , ) return self . mine_block ( ) | Import the given block to the chain . | 295 | 8 |
224,116 | def mine_block ( self , * args : Any , * * kwargs : Any ) -> BaseBlock : packed_block = self . pack_block ( self . block , * args , * * kwargs ) final_block = self . finalize_block ( packed_block ) # Perform validation self . validate_block ( final_block ) return final_block | Mine the current block . Proxies to self . pack_block method . | 79 | 16 |
224,117 | def finalize_block ( self , block : BaseBlock ) -> BaseBlock : if block . number > 0 : self . _assign_block_rewards ( block ) # We need to call `persist` here since the state db batches # all writes until we tell it to write to the underlying db self . state . persist ( ) return block . copy ( header = block . header . copy ( state_root = self . state . state_root ) ) | Perform any finalization steps like awarding the block mining reward and persisting the final state root . | 98 | 20 |
224,118 | def pack_block ( self , block : BaseBlock , * args : Any , * * kwargs : Any ) -> BaseBlock : if 'uncles' in kwargs : uncles = kwargs . pop ( 'uncles' ) kwargs . setdefault ( 'uncles_hash' , keccak ( rlp . encode ( uncles ) ) ) else : uncles = block . uncles provided_fields = set ( kwargs . keys ( ) ) known_fields = set ( BlockHeader . _meta . field_names ) unknown_fields = provided_fields . difference ( known_fields ) if unknown_fields : raise AttributeError ( "Unable to set the field(s) {0} on the `BlockHeader` class. " "Received the following unexpected fields: {1}." . format ( ", " . join ( known_fields ) , ", " . join ( unknown_fields ) , ) ) header = block . header . copy ( * * kwargs ) packed_block = block . copy ( uncles = uncles , header = header ) return packed_block | Pack block for mining . | 239 | 5 |
224,119 | def generate_block_from_parent_header_and_coinbase ( cls , parent_header : BlockHeader , coinbase : Address ) -> BaseBlock : block_header = generate_header_from_parent_header ( cls . compute_difficulty , parent_header , coinbase , timestamp = parent_header . timestamp + 1 , ) block = cls . get_block_class ( ) ( block_header , transactions = [ ] , uncles = [ ] , ) return block | Generate block from parent header and coinbase . | 106 | 10 |
224,120 | def previous_hashes ( self ) -> Optional [ Iterable [ Hash32 ] ] : return self . get_prev_hashes ( self . header . parent_hash , self . chaindb ) | Convenience API for accessing the previous 255 block hashes . | 42 | 12 |
224,121 | def create_transaction ( self , * args : Any , * * kwargs : Any ) -> BaseTransaction : return self . get_transaction_class ( ) ( * args , * * kwargs ) | Proxy for instantiating a signed transaction for this VM . | 46 | 11 |
224,122 | def create_unsigned_transaction ( cls , * , nonce : int , gas_price : int , gas : int , to : Address , value : int , data : bytes ) -> 'BaseUnsignedTransaction' : return cls . get_transaction_class ( ) . create_unsigned_transaction ( nonce = nonce , gas_price = gas_price , gas = gas , to = to , value = value , data = data ) | Proxy for instantiating an unsigned transaction for this VM . | 98 | 11 |
224,123 | def validate_block ( self , block : BaseBlock ) -> None : if not isinstance ( block , self . get_block_class ( ) ) : raise ValidationError ( "This vm ({0!r}) is not equipped to validate a block of type {1!r}" . format ( self , block , ) ) if block . is_genesis : validate_length_lte ( block . header . extra_data , 32 , title = "BlockHeader.extra_data" ) else : parent_header = get_parent_header ( block . header , self . chaindb ) self . validate_header ( block . header , parent_header ) tx_root_hash , _ = make_trie_root_and_nodes ( block . transactions ) if tx_root_hash != block . header . transaction_root : raise ValidationError ( "Block's transaction_root ({0}) does not match expected value: {1}" . format ( block . header . transaction_root , tx_root_hash ) ) if len ( block . uncles ) > MAX_UNCLES : raise ValidationError ( "Blocks may have a maximum of {0} uncles. Found " "{1}." . format ( MAX_UNCLES , len ( block . uncles ) ) ) if not self . chaindb . exists ( block . header . state_root ) : raise ValidationError ( "`state_root` was not found in the db.\n" "- state_root: {0}" . format ( block . header . state_root , ) ) local_uncle_hash = keccak ( rlp . encode ( block . uncles ) ) if local_uncle_hash != block . header . uncles_hash : raise ValidationError ( "`uncles_hash` and block `uncles` do not match.\n" " - num_uncles : {0}\n" " - block uncle_hash : {1}\n" " - header uncle_hash: {2}" . format ( len ( block . uncles ) , local_uncle_hash , block . header . uncles_hash , ) ) | Validate the the given block . | 461 | 7 |
224,124 | def validate_uncle ( cls , block : BaseBlock , uncle : BaseBlock , uncle_parent : BaseBlock ) -> None : if uncle . block_number >= block . number : raise ValidationError ( "Uncle number ({0}) is higher than block number ({1})" . format ( uncle . block_number , block . number ) ) if uncle . block_number != uncle_parent . block_number + 1 : raise ValidationError ( "Uncle number ({0}) is not one above ancestor's number ({1})" . format ( uncle . block_number , uncle_parent . block_number ) ) if uncle . timestamp < uncle_parent . timestamp : raise ValidationError ( "Uncle timestamp ({0}) is before ancestor's timestamp ({1})" . format ( uncle . timestamp , uncle_parent . timestamp ) ) if uncle . gas_used > uncle . gas_limit : raise ValidationError ( "Uncle's gas usage ({0}) is above the limit ({1})" . format ( uncle . gas_used , uncle . gas_limit ) ) | Validate the given uncle in the context of the given block . | 229 | 13 |
224,125 | def is_cleanly_mergable ( * dicts : Dict [ Any , Any ] ) -> bool : if len ( dicts ) <= 1 : return True elif len ( dicts ) == 2 : if not all ( isinstance ( d , Mapping ) for d in dicts ) : return False else : shared_keys = set ( dicts [ 0 ] . keys ( ) ) & set ( dicts [ 1 ] . keys ( ) ) return all ( is_cleanly_mergable ( dicts [ 0 ] [ key ] , dicts [ 1 ] [ key ] ) for key in shared_keys ) else : dict_combinations = itertools . combinations ( dicts , 2 ) return all ( is_cleanly_mergable ( * combination ) for combination in dict_combinations ) | Check that nothing will be overwritten when dictionaries are merged using deep_merge . | 176 | 18 |
224,126 | def deleted_keys ( self ) -> Iterable [ bytes ] : for key , value in self . _changes . items ( ) : if value is DELETED : yield key | List all the keys that have been deleted . | 37 | 9 |
224,127 | def apply_to ( self , db : Union [ BaseDB , ABC_Mutable_Mapping ] , apply_deletes : bool = True ) -> None : for key , value in self . _changes . items ( ) : if value is DELETED : if apply_deletes : try : del db [ key ] except KeyError : pass else : pass else : db [ key ] = value | Apply the changes in this diff to the given database . You may choose to opt out of deleting any underlying keys . | 85 | 23 |
224,128 | def join ( cls , diffs : Iterable [ 'DBDiff' ] ) -> 'DBDiff' : tracker = DBDiffTracker ( ) for diff in diffs : diff . apply_to ( tracker ) return tracker . diff ( ) | Join several DBDiff objects into a single DBDiff object . | 52 | 14 |
224,129 | def hash_log_entries ( log_entries : Iterable [ Tuple [ bytes , List [ int ] , bytes ] ] ) -> Hash32 : logs = [ Log ( * entry ) for entry in log_entries ] encoded_logs = rlp . encode ( logs ) logs_hash = keccak ( encoded_logs ) return logs_hash | Helper function for computing the RLP hash of the logs from transaction execution . | 79 | 15 |
224,130 | def get_vm_class_for_block_number ( cls , block_number : BlockNumber ) -> Type [ 'BaseVM' ] : if cls . vm_configuration is None : raise AttributeError ( "Chain classes must define the VMs in vm_configuration" ) validate_block_number ( block_number ) for start_block , vm_class in reversed ( cls . vm_configuration ) : if block_number >= start_block : return vm_class else : raise VMNotFound ( "No vm available for block #{0}" . format ( block_number ) ) | Returns the VM class for the given block number . | 131 | 10 |
224,131 | def validate_chain ( cls , root : BlockHeader , descendants : Tuple [ BlockHeader , ... ] , seal_check_random_sample_rate : int = 1 ) -> None : all_indices = range ( len ( descendants ) ) if seal_check_random_sample_rate == 1 : indices_to_check_seal = set ( all_indices ) else : sample_size = len ( all_indices ) // seal_check_random_sample_rate indices_to_check_seal = set ( random . sample ( all_indices , sample_size ) ) header_pairs = sliding_window ( 2 , concatv ( [ root ] , descendants ) ) for index , ( parent , child ) in enumerate ( header_pairs ) : if child . parent_hash != parent . hash : raise ValidationError ( "Invalid header chain; {} has parent {}, but expected {}" . format ( child , child . parent_hash , parent . hash ) ) should_check_seal = index in indices_to_check_seal vm_class = cls . get_vm_class_for_block_number ( child . block_number ) try : vm_class . validate_header ( child , parent , check_seal = should_check_seal ) except ValidationError as exc : raise ValidationError ( "%s is not a valid child of %s: %s" % ( child , parent , exc , ) ) from exc | Validate that all of the descendents are valid given that the root header is valid . | 322 | 18 |
224,132 | def from_genesis ( cls , base_db : BaseAtomicDB , genesis_params : Dict [ str , HeaderParams ] , genesis_state : AccountState = None ) -> 'BaseChain' : genesis_vm_class = cls . get_vm_class_for_block_number ( BlockNumber ( 0 ) ) pre_genesis_header = BlockHeader ( difficulty = 0 , block_number = - 1 , gas_limit = 0 ) state = genesis_vm_class . build_state ( base_db , pre_genesis_header ) if genesis_state is None : genesis_state = { } # mutation apply_state_dict ( state , genesis_state ) state . persist ( ) if 'state_root' not in genesis_params : # If the genesis state_root was not specified, use the value # computed from the initialized state database. genesis_params = assoc ( genesis_params , 'state_root' , state . state_root ) elif genesis_params [ 'state_root' ] != state . state_root : # If the genesis state_root was specified, validate that it matches # the computed state from the initialized state database. raise ValidationError ( "The provided genesis state root does not match the computed " "genesis state root. Got {0}. Expected {1}" . format ( state . state_root , genesis_params [ 'state_root' ] , ) ) genesis_header = BlockHeader ( * * genesis_params ) return cls . from_genesis_header ( base_db , genesis_header ) | Initializes the Chain from a genesis state . | 342 | 9 |
224,133 | def get_vm ( self , at_header : BlockHeader = None ) -> 'BaseVM' : header = self . ensure_header ( at_header ) vm_class = self . get_vm_class_for_block_number ( header . block_number ) return vm_class ( header = header , chaindb = self . chaindb ) | Returns the VM instance for the given block number . | 75 | 10 |
224,134 | def create_header_from_parent ( self , parent_header : BlockHeader , * * header_params : HeaderParams ) -> BlockHeader : return self . get_vm_class_for_block_number ( block_number = parent_header . block_number + 1 , ) . create_header_from_parent ( parent_header , * * header_params ) | Passthrough helper to the VM class of the block descending from the given header . | 81 | 18 |
224,135 | def ensure_header ( self , header : BlockHeader = None ) -> BlockHeader : if header is None : head = self . get_canonical_head ( ) return self . create_header_from_parent ( head ) else : return header | Return header if it is not None otherwise return the header of the canonical head . | 52 | 16 |
224,136 | def get_ancestors ( self , limit : int , header : BlockHeader ) -> Tuple [ BaseBlock , ... ] : ancestor_count = min ( header . block_number , limit ) # We construct a temporary block object vm_class = self . get_vm_class_for_block_number ( header . block_number ) block_class = vm_class . get_block_class ( ) block = block_class ( header = header , uncles = [ ] ) ancestor_generator = iterate ( compose ( self . get_block_by_hash , operator . attrgetter ( 'parent_hash' ) , operator . attrgetter ( 'header' ) , ) , block ) # we peel off the first element from the iterator which will be the # temporary block object we constructed. next ( ancestor_generator ) return tuple ( take ( ancestor_count , ancestor_generator ) ) | Return limit number of ancestor blocks from the current canonical head . | 197 | 12 |
224,137 | def get_block_by_hash ( self , block_hash : Hash32 ) -> BaseBlock : validate_word ( block_hash , title = "Block Hash" ) block_header = self . get_block_header_by_hash ( block_hash ) return self . get_block_by_header ( block_header ) | Returns the requested block as specified by block hash . | 72 | 10 |
224,138 | def get_block_by_header ( self , block_header : BlockHeader ) -> BaseBlock : vm = self . get_vm ( block_header ) return vm . block | Returns the requested block as specified by the block header . | 38 | 11 |
224,139 | def get_canonical_block_by_number ( self , block_number : BlockNumber ) -> BaseBlock : validate_uint256 ( block_number , title = "Block Number" ) return self . get_block_by_hash ( self . chaindb . get_canonical_block_hash ( block_number ) ) | Returns the block with the given number in the canonical chain . | 71 | 12 |
224,140 | def get_canonical_transaction ( self , transaction_hash : Hash32 ) -> BaseTransaction : ( block_num , index ) = self . chaindb . get_transaction_index ( transaction_hash ) VM_class = self . get_vm_class_for_block_number ( block_num ) transaction = self . chaindb . get_transaction_by_index ( block_num , index , VM_class . get_transaction_class ( ) , ) if transaction . hash == transaction_hash : return transaction else : raise TransactionNotFound ( "Found transaction {} instead of {} in block {} at {}" . format ( encode_hex ( transaction . hash ) , encode_hex ( transaction_hash ) , block_num , index , ) ) | Returns the requested transaction as specified by the transaction hash from the canonical chain . | 164 | 15 |
224,141 | def estimate_gas ( self , transaction : BaseOrSpoofTransaction , at_header : BlockHeader = None ) -> int : if at_header is None : at_header = self . get_canonical_head ( ) with self . get_vm ( at_header ) . state_in_temp_block ( ) as state : return self . gas_estimator ( state , transaction ) | Returns an estimation of the amount of gas the given transaction will use if executed on top of the block specified by the given header . | 85 | 26 |
224,142 | def import_block ( self , block : BaseBlock , perform_validation : bool = True ) -> Tuple [ BaseBlock , Tuple [ BaseBlock , ... ] , Tuple [ BaseBlock , ... ] ] : try : parent_header = self . get_block_header_by_hash ( block . header . parent_hash ) except HeaderNotFound : raise ValidationError ( "Attempt to import block #{}. Cannot import block {} before importing " "its parent block at {}" . format ( block . number , block . hash , block . header . parent_hash , ) ) base_header_for_import = self . create_header_from_parent ( parent_header ) imported_block = self . get_vm ( base_header_for_import ) . import_block ( block ) # Validate the imported block. if perform_validation : validate_imported_block_unchanged ( imported_block , block ) self . validate_block ( imported_block ) ( new_canonical_hashes , old_canonical_hashes , ) = self . chaindb . persist_block ( imported_block ) self . logger . debug ( 'IMPORTED_BLOCK: number %s | hash %s' , imported_block . number , encode_hex ( imported_block . hash ) , ) new_canonical_blocks = tuple ( self . get_block_by_hash ( header_hash ) for header_hash in new_canonical_hashes ) old_canonical_blocks = tuple ( self . get_block_by_hash ( header_hash ) for header_hash in old_canonical_hashes ) return imported_block , new_canonical_blocks , old_canonical_blocks | Imports a complete block and returns a 3 - tuple | 375 | 11 |
224,143 | def validate_block ( self , block : BaseBlock ) -> None : if block . is_genesis : raise ValidationError ( "Cannot validate genesis block this way" ) VM_class = self . get_vm_class_for_block_number ( BlockNumber ( block . number ) ) parent_block = self . get_block_by_hash ( block . header . parent_hash ) VM_class . validate_header ( block . header , parent_block . header , check_seal = True ) self . validate_uncles ( block ) self . validate_gaslimit ( block . header ) | Performs validation on a block that is either being mined or imported . | 131 | 14 |
224,144 | def validate_gaslimit ( self , header : BlockHeader ) -> None : parent_header = self . get_block_header_by_hash ( header . parent_hash ) low_bound , high_bound = compute_gas_limit_bounds ( parent_header ) if header . gas_limit < low_bound : raise ValidationError ( "The gas limit on block {0} is too low: {1}. It must be at least {2}" . format ( encode_hex ( header . hash ) , header . gas_limit , low_bound ) ) elif header . gas_limit > high_bound : raise ValidationError ( "The gas limit on block {0} is too high: {1}. It must be at most {2}" . format ( encode_hex ( header . hash ) , header . gas_limit , high_bound ) ) | Validate the gas limit on the given header . | 186 | 10 |
224,145 | def validate_uncles ( self , block : BaseBlock ) -> None : has_uncles = len ( block . uncles ) > 0 should_have_uncles = block . header . uncles_hash != EMPTY_UNCLE_HASH if not has_uncles and not should_have_uncles : # optimization to avoid loading ancestors from DB, since the block has no uncles return elif has_uncles and not should_have_uncles : raise ValidationError ( "Block has uncles but header suggests uncles should be empty" ) elif should_have_uncles and not has_uncles : raise ValidationError ( "Header suggests block should have uncles but block has none" ) # Check for duplicates uncle_groups = groupby ( operator . attrgetter ( 'hash' ) , block . uncles ) duplicate_uncles = tuple ( sorted ( hash for hash , twins in uncle_groups . items ( ) if len ( twins ) > 1 ) ) if duplicate_uncles : raise ValidationError ( "Block contains duplicate uncles:\n" " - {0}" . format ( ' - ' . join ( duplicate_uncles ) ) ) recent_ancestors = tuple ( ancestor for ancestor in self . get_ancestors ( MAX_UNCLE_DEPTH + 1 , header = block . header ) ) recent_ancestor_hashes = { ancestor . hash for ancestor in recent_ancestors } recent_uncle_hashes = _extract_uncle_hashes ( recent_ancestors ) for uncle in block . uncles : if uncle . hash == block . hash : raise ValidationError ( "Uncle has same hash as block" ) # ensure the uncle has not already been included. if uncle . hash in recent_uncle_hashes : raise ValidationError ( "Duplicate uncle: {0}" . format ( encode_hex ( uncle . hash ) ) ) # ensure that the uncle is not one of the canonical chain blocks. if uncle . hash in recent_ancestor_hashes : raise ValidationError ( "Uncle {0} cannot be an ancestor of {1}" . format ( encode_hex ( uncle . hash ) , encode_hex ( block . hash ) ) ) # ensure that the uncle was built off of one of the canonical chain # blocks. if uncle . parent_hash not in recent_ancestor_hashes or ( uncle . parent_hash == block . header . parent_hash ) : raise ValidationError ( "Uncle's parent {0} is not an ancestor of {1}" . format ( encode_hex ( uncle . parent_hash ) , encode_hex ( block . hash ) ) ) # Now perform VM level validation of the uncle self . validate_seal ( uncle ) try : uncle_parent = self . get_block_header_by_hash ( uncle . parent_hash ) except HeaderNotFound : raise ValidationError ( "Uncle ancestor not found: {0}" . format ( uncle . parent_hash ) ) uncle_vm_class = self . get_vm_class_for_block_number ( uncle . block_number ) uncle_vm_class . validate_uncle ( block , uncle , uncle_parent ) | Validate the uncles for the given block . | 698 | 10 |
224,146 | def apply_transaction ( self , transaction : BaseTransaction ) -> Tuple [ BaseBlock , Receipt , BaseComputation ] : vm = self . get_vm ( self . header ) base_block = vm . block receipt , computation = vm . apply_transaction ( base_block . header , transaction ) header_with_receipt = vm . add_receipt_to_header ( base_block . header , receipt ) # since we are building the block locally, we have to persist all the incremental state vm . state . persist ( ) new_header = header_with_receipt . copy ( state_root = vm . state . state_root ) transactions = base_block . transactions + ( transaction , ) receipts = base_block . get_receipts ( self . chaindb ) + ( receipt , ) new_block = vm . set_block_transactions ( base_block , new_header , transactions , receipts ) self . header = new_block . header return new_block , receipt , computation | Applies the transaction to the current tip block . | 222 | 10 |
224,147 | def wait_for_host ( port , interval = 1 , timeout = 30 , to_start = True , queue = None , ssl_pymongo_options = None ) : host = 'localhost:%i' % port start_time = time . time ( ) while True : if ( time . time ( ) - start_time ) > timeout : if queue : queue . put_nowait ( ( port , False ) ) return False try : # make connection and ping host con = MongoConnection ( host , * * ( ssl_pymongo_options or { } ) ) con . admin . command ( 'ping' ) if to_start : if queue : queue . put_nowait ( ( port , True ) ) return True else : time . sleep ( interval ) except Exception : if to_start : time . sleep ( interval ) else : if queue : queue . put_nowait ( ( port , True ) ) return True | Ping server and wait for response . | 200 | 7 |
224,148 | def shutdown_host ( port , username = None , password = None , authdb = None ) : host = 'localhost:%i' % port try : mc = MongoConnection ( host ) try : if username and password and authdb : if authdb != "admin" : raise RuntimeError ( "given username/password is not for " "admin database" ) else : try : mc . admin . authenticate ( name = username , password = password ) except OperationFailure : # perhaps auth is not required pass mc . admin . command ( 'shutdown' , force = True ) except AutoReconnect : pass except OperationFailure : print ( "Error: cannot authenticate to shut down %s." % host ) return except ConnectionFailure : pass else : mc . close ( ) | Send the shutdown command to a mongod or mongos on given port . | 162 | 17 |
224,149 | def start ( self ) : self . discover ( ) # startup_info only gets loaded from protocol version 2 on, # check if it's loaded if not self . startup_info : # hack to make environment startable with older protocol # versions < 2: try to start nodes via init if all nodes are down if len ( self . get_tagged ( [ 'down' ] ) ) == len ( self . get_tagged ( [ 'all' ] ) ) : self . args = self . loaded_args print ( "upgrading mlaunch environment meta-data." ) return self . init ( ) else : raise SystemExit ( "These nodes were created with an older " "version of mlaunch (v1.1.1 or below). To " "upgrade this environment and make use of " "the start/stop/list commands, stop all " "nodes manually, then run 'mlaunch start' " "again. You only have to do this once." ) # if new unknown_args are present, compare them with loaded ones # (here we can be certain of protocol v2+) if ( self . args [ 'binarypath' ] is not None or ( self . unknown_args and set ( self . unknown_args ) != set ( self . loaded_unknown_args ) ) ) : # store current args, use self.args from file (self.loaded_args) start_args = self . args self . args = self . loaded_args self . args [ 'binarypath' ] = start_args [ 'binarypath' ] # construct new startup strings with updated unknown args. # They are for this start only and will not be persisted in # the .mlaunch_startup file self . _construct_cmdlines ( ) # reset to original args for this start command self . args = start_args matches = self . _get_ports_from_args ( self . args , 'down' ) if len ( matches ) == 0 : raise SystemExit ( 'no nodes started.' ) # start config servers first config_matches = self . get_tagged ( [ 'config' ] ) . intersection ( matches ) self . _start_on_ports ( config_matches , wait = True ) # start shards next mongod_matches = ( self . get_tagged ( [ 'mongod' ] ) - self . get_tagged ( [ 'config' ] ) ) mongod_matches = mongod_matches . intersection ( matches ) self . _start_on_ports ( mongod_matches , wait = True ) # now start mongos mongos_matches = self . get_tagged ( [ 'mongos' ] ) . intersection ( matches ) self . _start_on_ports ( mongos_matches ) # wait for all matched nodes to be running self . wait_for ( matches ) # refresh discover self . discover ( ) | Sub - command start . | 623 | 5 |
224,150 | def is_running ( self , port ) : try : con = self . client ( 'localhost:%s' % port ) con . admin . command ( 'ping' ) return True except ( AutoReconnect , ConnectionFailure , OperationFailure ) : # Catch OperationFailure to work around SERVER-31916. return False | Return True if a host on a specific port is running . | 68 | 12 |
224,151 | def get_tagged ( self , tags ) : # if tags is a simple string, make it a list (note: tuples like # ('mongos', 2) must be in a surrounding list) if not hasattr ( tags , '__iter__' ) and type ( tags ) == str : tags = [ tags ] nodes = set ( self . cluster_tags [ 'all' ] ) for tag in tags : if re . match ( r"\w+ \d{1,2}" , tag ) : # special case for tuple tags: mongos, config, shard, # secondary. These can contain a number tag , number = tag . split ( ) try : branch = self . cluster_tree [ tag ] [ int ( number ) - 1 ] except ( IndexError , KeyError ) : continue if hasattr ( branch , '__iter__' ) : subset = set ( branch ) else : subset = set ( [ branch ] ) else : # otherwise use tags dict to get the subset subset = set ( self . cluster_tags [ tag ] ) nodes = nodes . intersection ( subset ) return nodes | Tag format . | 236 | 3 |
224,152 | def get_tags_of_port ( self , port ) : return ( sorted ( [ tag for tag in self . cluster_tags if port in self . cluster_tags [ tag ] ] ) ) | Get all tags related to a given port . | 42 | 9 |
224,153 | def wait_for ( self , ports , interval = 1.0 , timeout = 30 , to_start = True ) : threads = [ ] queue = Queue . Queue ( ) for port in ports : threads . append ( threading . Thread ( target = wait_for_host , args = ( port , interval , timeout , to_start , queue , self . ssl_pymongo_options ) ) ) if self . args and 'verbose' in self . args and self . args [ 'verbose' ] : print ( "waiting for nodes %s..." % ( 'to start' if to_start else 'to shutdown' ) ) for thread in threads : thread . start ( ) for thread in threads : thread . join ( ) # get all results back and return tuple return tuple ( queue . get_nowait ( ) for _ in ports ) | Spawn threads to ping host using a list of ports . | 184 | 11 |
224,154 | def _load_parameters ( self ) : datapath = self . dir startup_file = os . path . join ( datapath , '.mlaunch_startup' ) if not os . path . exists ( startup_file ) : return False in_dict = json . load ( open ( startup_file , 'rb' ) ) # handle legacy version without versioned protocol if 'protocol_version' not in in_dict : in_dict [ 'protocol_version' ] = 1 self . loaded_args = in_dict self . startup_info = { } # hostname was added recently self . loaded_args [ 'hostname' ] = socket . gethostname ( ) elif in_dict [ 'protocol_version' ] == 2 : self . startup_info = in_dict [ 'startup_info' ] self . loaded_unknown_args = in_dict [ 'unknown_args' ] self . loaded_args = in_dict [ 'parsed_args' ] # changed 'authentication' to 'auth', if present (from old env) rename if 'authentication' in self . loaded_args : self . loaded_args [ 'auth' ] = self . loaded_args [ 'authentication' ] del self . loaded_args [ 'authentication' ] return True | Load the . mlaunch_startup file that exists in each datadir . | 284 | 17 |
224,155 | def _create_paths ( self , basedir , name = None ) : if name : datapath = os . path . join ( basedir , name ) else : datapath = basedir dbpath = os . path . join ( datapath , 'db' ) if not os . path . exists ( dbpath ) : os . makedirs ( dbpath ) if self . args [ 'verbose' ] : print ( 'creating directory: %s' % dbpath ) return datapath | Create datadir and subdir paths . | 110 | 9 |
224,156 | def _filter_valid_arguments ( self , arguments , binary = "mongod" , config = False ) : # get the help list of the binary if self . args and self . args [ 'binarypath' ] : binary = os . path . join ( self . args [ 'binarypath' ] , binary ) ret = ( subprocess . Popen ( [ '%s' % binary , '--help' ] , stderr = subprocess . STDOUT , stdout = subprocess . PIPE , shell = False ) ) out , err = ret . communicate ( ) accepted_arguments = [ ] # extract all arguments starting with a '-' for line in [ option for option in out . decode ( 'utf-8' ) . split ( '\n' ) ] : line = line . lstrip ( ) if line . startswith ( '-' ) : argument = line . split ( ) [ 0 ] # exception: don't allow unsupported config server arguments if config and argument in [ '--oplogSize' , '--storageEngine' , '--smallfiles' , '--nojournal' ] : continue accepted_arguments . append ( argument ) # add undocumented options accepted_arguments . append ( '--setParameter' ) if binary . endswith ( 'mongod' ) : accepted_arguments . append ( '--wiredTigerEngineConfigString' ) # filter valid arguments result = [ ] for i , arg in enumerate ( arguments ) : if arg . startswith ( '-' ) : # check if the binary accepts this argument # or special case -vvv for any number of v argname = arg . split ( '=' , 1 ) [ 0 ] if argname in accepted_arguments or re . match ( r'-v+' , arg ) : result . append ( arg ) elif ( binary . endswith ( 'mongod' ) and argname in self . UNDOCUMENTED_MONGOD_ARGS ) : result . append ( arg ) elif self . ignored_arguments . get ( binary + argname ) is None : # warn once for each combination of binary and unknown arg self . ignored_arguments [ binary + argname ] = True if not ( binary . endswith ( "mongos" ) and arg in self . UNSUPPORTED_MONGOS_ARGS ) : print ( "warning: ignoring unknown argument %s for %s" % ( arg , binary ) ) elif i > 0 and arguments [ i - 1 ] in result : # if it doesn't start with a '-', it could be the value of # the last argument, e.g. `--slowms 1000` result . append ( arg ) # return valid arguments as joined string return ' ' . join ( result ) | Return a list of accepted arguments . | 595 | 7 |
224,157 | def _initiate_replset ( self , port , name , maxwait = 30 ) : if not self . args [ 'replicaset' ] and name != 'configRepl' : if self . args [ 'verbose' ] : print ( 'Skipping replica set initialization for %s' % name ) return con = self . client ( 'localhost:%i' % port ) try : rs_status = con [ 'admin' ] . command ( { 'replSetGetStatus' : 1 } ) return rs_status except OperationFailure as e : # not initiated yet for i in range ( maxwait ) : try : con [ 'admin' ] . command ( { 'replSetInitiate' : self . config_docs [ name ] } ) break except OperationFailure as e : print ( e . message + " - will retry" ) time . sleep ( 1 ) if self . args [ 'verbose' ] : print ( "initializing replica set '%s' with configuration: %s" % ( name , self . config_docs [ name ] ) ) print ( "replica set '%s' initialized." % name ) | Initiate replica set . | 246 | 6 |
224,158 | def _construct_sharded ( self ) : current_version = self . getMongoDVersion ( ) num_mongos = self . args [ 'mongos' ] if self . args [ 'mongos' ] > 0 else 1 shard_names = self . _get_shard_names ( self . args ) # create shards as stand-alones or replica sets nextport = self . args [ 'port' ] + num_mongos for shard in shard_names : if ( self . args [ 'single' ] and LooseVersion ( current_version ) >= LooseVersion ( "3.6.0" ) ) : errmsg = " \n * In MongoDB 3.6 and above a Shard must be " "made up of a replica set. Please use --replicaset " "option when starting a sharded cluster.*" raise SystemExit ( errmsg ) elif ( self . args [ 'single' ] and LooseVersion ( current_version ) < LooseVersion ( "3.6.0" ) ) : self . shard_connection_str . append ( self . _construct_single ( self . dir , nextport , name = shard , extra = '--shardsvr' ) ) nextport += 1 elif self . args [ 'replicaset' ] : self . shard_connection_str . append ( self . _construct_replset ( self . dir , nextport , shard , num_nodes = list ( range ( self . args [ 'nodes' ] ) ) , arbiter = self . args [ 'arbiter' ] , extra = '--shardsvr' ) ) nextport += self . args [ 'nodes' ] if self . args [ 'arbiter' ] : nextport += 1 # start up config server(s) config_string = [ ] # SCCC config servers (MongoDB <3.3.0) if not self . args [ 'csrs' ] and self . args [ 'config' ] >= 3 : config_names = [ 'config1' , 'config2' , 'config3' ] else : config_names = [ 'config' ] # CSRS config servers (MongoDB >=3.1.0) if self . args [ 'csrs' ] : config_string . append ( self . _construct_config ( self . dir , nextport , "configRepl" , True ) ) else : for name in config_names : self . _construct_config ( self . dir , nextport , name ) config_string . append ( '%s:%i' % ( self . args [ 'hostname' ] , nextport ) ) nextport += 1 # multiple mongos use <datadir>/mongos/ as subdir for log files if num_mongos > 1 : mongosdir = os . path . join ( self . dir , 'mongos' ) if not os . path . exists ( mongosdir ) : if self . args [ 'verbose' ] : print ( "creating directory: %s" % mongosdir ) os . makedirs ( mongosdir ) # start up mongos, but put them to the front of the port range nextport = self . args [ 'port' ] for i in range ( num_mongos ) : if num_mongos > 1 : mongos_logfile = 'mongos/mongos_%i.log' % nextport else : mongos_logfile = 'mongos.log' self . _construct_mongos ( os . path . join ( self . dir , mongos_logfile ) , nextport , ',' . join ( config_string ) ) nextport += 1 | Construct command line strings for a sharded cluster . | 811 | 10 |
224,159 | def _construct_replset ( self , basedir , portstart , name , num_nodes , arbiter , extra = '' ) : self . config_docs [ name ] = { '_id' : name , 'members' : [ ] } # Construct individual replica set nodes for i in num_nodes : datapath = self . _create_paths ( basedir , '%s/rs%i' % ( name , i + 1 ) ) self . _construct_mongod ( os . path . join ( datapath , 'db' ) , os . path . join ( datapath , 'mongod.log' ) , portstart + i , replset = name , extra = extra ) host = '%s:%i' % ( self . args [ 'hostname' ] , portstart + i ) member_config = { '_id' : len ( self . config_docs [ name ] [ 'members' ] ) , 'host' : host , } # First node gets increased priority. if i == 0 and self . args [ 'priority' ] : member_config [ 'priority' ] = 10 if i >= 7 : member_config [ 'votes' ] = 0 member_config [ 'priority' ] = 0 self . config_docs [ name ] [ 'members' ] . append ( member_config ) # launch arbiter if True if arbiter : datapath = self . _create_paths ( basedir , '%s/arb' % ( name ) ) self . _construct_mongod ( os . path . join ( datapath , 'db' ) , os . path . join ( datapath , 'mongod.log' ) , portstart + self . args [ 'nodes' ] , replset = name ) host = '%s:%i' % ( self . args [ 'hostname' ] , portstart + self . args [ 'nodes' ] ) ( self . config_docs [ name ] [ 'members' ] . append ( { '_id' : len ( self . config_docs [ name ] [ 'members' ] ) , 'host' : host , 'arbiterOnly' : True } ) ) return ( name + '/' + ',' . join ( [ c [ 'host' ] for c in self . config_docs [ name ] [ 'members' ] ] ) ) | Construct command line strings for a replicaset . | 515 | 10 |
224,160 | def _construct_config ( self , basedir , port , name = None , isreplset = False ) : if isreplset : return self . _construct_replset ( basedir = basedir , portstart = port , name = name , num_nodes = list ( range ( self . args [ 'config' ] ) ) , arbiter = False , extra = '--configsvr' ) else : datapath = self . _create_paths ( basedir , name ) self . _construct_mongod ( os . path . join ( datapath , 'db' ) , os . path . join ( datapath , 'mongod.log' ) , port , replset = None , extra = '--configsvr' ) | Construct command line strings for a config server . | 163 | 9 |
224,161 | def _construct_single ( self , basedir , port , name = None , extra = '' ) : datapath = self . _create_paths ( basedir , name ) self . _construct_mongod ( os . path . join ( datapath , 'db' ) , os . path . join ( datapath , 'mongod.log' ) , port , replset = None , extra = extra ) host = '%s:%i' % ( self . args [ 'hostname' ] , port ) return host | Construct command line strings for a single node . | 116 | 9 |
224,162 | def _construct_mongod ( self , dbpath , logpath , port , replset = None , extra = '' ) : rs_param = '' if replset : rs_param = '--replSet %s' % replset auth_param = '' if self . args [ 'auth' ] : key_path = os . path . abspath ( os . path . join ( self . dir , 'keyfile' ) ) auth_param = '--keyFile %s' % key_path if self . unknown_args : config = '--configsvr' in extra extra = self . _filter_valid_arguments ( self . unknown_args , "mongod" , config = config ) + ' ' + extra # set WiredTiger cache size to 1 GB by default if ( '--wiredTigerCacheSizeGB' not in extra and self . _filter_valid_arguments ( [ '--wiredTigerCacheSizeGB' ] , 'mongod' ) ) : extra += ' --wiredTigerCacheSizeGB 1 ' current_version = self . getMongoDVersion ( ) # Exit with error if hostname is specified but not bind_ip options if ( self . args [ 'hostname' ] != 'localhost' and LooseVersion ( current_version ) >= LooseVersion ( "3.6.0" ) and ( self . args [ 'sharded' ] or self . args [ 'replicaset' ] ) and '--bind_ip' not in extra ) : os . removedirs ( dbpath ) errmsg = " \n * If hostname is specified, please include " "'--bind_ip_all' or '--bind_ip' options when deploying " "replica sets or sharded cluster with MongoDB version 3.6.0 " "or greater" raise SystemExit ( errmsg ) extra += self . _get_ssl_server_args ( ) path = self . args [ 'binarypath' ] or '' if os . name == 'nt' : newdbpath = dbpath . replace ( '\\' , '\\\\' ) newlogpath = logpath . replace ( '\\' , '\\\\' ) command_str = ( "start /b \"\" \"%s\" %s --dbpath \"%s\" " " --logpath \"%s\" --port %i " "%s %s" % ( os . path . join ( path , 'mongod.exe' ) , rs_param , newdbpath , newlogpath , port , auth_param , extra ) ) else : command_str = ( "\"%s\" %s --dbpath \"%s\" --logpath \"%s\" " "--port %i --fork " "%s %s" % ( os . path . join ( path , 'mongod' ) , rs_param , dbpath , logpath , port , auth_param , extra ) ) # store parameters in startup_info self . startup_info [ str ( port ) ] = command_str | Construct command line strings for mongod process . | 647 | 10 |
224,163 | def _construct_mongos ( self , logpath , port , configdb ) : extra = '' auth_param = '' if self . args [ 'auth' ] : key_path = os . path . abspath ( os . path . join ( self . dir , 'keyfile' ) ) auth_param = '--keyFile %s' % key_path if self . unknown_args : extra = self . _filter_valid_arguments ( self . unknown_args , "mongos" ) + extra extra += ' ' + self . _get_ssl_server_args ( ) path = self . args [ 'binarypath' ] or '' if os . name == 'nt' : newlogpath = logpath . replace ( '\\' , '\\\\' ) command_str = ( "start /b %s --logpath \"%s\" --port %i --configdb %s " "%s %s " % ( os . path . join ( path , 'mongos' ) , newlogpath , port , configdb , auth_param , extra ) ) else : command_str = ( "%s --logpath \"%s\" --port %i --configdb %s %s %s " "--fork" % ( os . path . join ( path , 'mongos' ) , logpath , port , configdb , auth_param , extra ) ) # store parameters in startup_info self . startup_info [ str ( port ) ] = command_str | Construct command line strings for a mongos process . | 318 | 11 |
224,164 | def addMatch ( self , version , filename , lineno , loglevel , trigger ) : self . versions . add ( version ) self . matches [ version ] . append ( ( filename , lineno , loglevel , trigger ) ) | Add a match to the LogCodeLine . | 50 | 9 |
224,165 | def accept_line ( self , logevent ) : if ( "is now in state" in logevent . line_str and logevent . split_tokens [ - 1 ] in self . states ) : return True if ( "replSet" in logevent . line_str and logevent . thread == "rsMgr" and logevent . split_tokens [ - 1 ] in self . states ) : return True return False | Return True on match . | 100 | 5 |
224,166 | def color_map ( cls , group ) : print ( "Group %s" % group ) try : state_idx = cls . states . index ( group ) except ValueError : # on any unexpected state, return black state_idx = 5 return cls . colors [ state_idx ] , cls . markers [ 0 ] | Change default color behavior . | 74 | 5 |
224,167 | def add_line ( self , logevent ) : key = None self . empty = False self . groups . setdefault ( key , list ( ) ) . append ( logevent ) | Append log line to this plot type . | 40 | 9 |
224,168 | def logevents ( self ) : for key in self . groups : for logevent in self . groups [ key ] : yield logevent | Iterator yielding all logevents from groups dictionary . | 32 | 11 |
224,169 | def clicked ( self , event ) : group = event . artist . _mt_group n = event . artist . _mt_n dt = num2date ( event . artist . _mt_bin ) print ( "%4i %s events in %s sec beginning at %s" % ( n , group , self . bucketsize , dt . strftime ( "%b %d %H:%M:%S" ) ) ) | Print group name and number of items in bin . | 94 | 10 |
224,170 | def add ( self , item , group_by = None ) : key = None if not group_by : group_by = self . group_by if group_by : # if group_by is a function, use it with item as argument if hasattr ( group_by , '__call__' ) : key = group_by ( item ) # if the item has attribute of group_by as string, use that as key elif isinstance ( group_by , str ) and hasattr ( item , group_by ) : key = getattr ( item , group_by ) else : key = None # try to match str(item) with regular expression if isinstance ( group_by , str ) : match = re . search ( group_by , str ( item ) ) if match : if len ( match . groups ( ) ) > 0 : key = match . group ( 1 ) else : key = match . group ( ) self . groups . setdefault ( key , list ( ) ) . append ( item ) | General purpose class to group items by certain criteria . | 217 | 10 |
224,171 | def regroup ( self , group_by = None ) : if not group_by : group_by = self . group_by groups = self . groups self . groups = { } for g in groups : for item in groups [ g ] : self . add ( item , group_by ) | Regroup items . | 62 | 4 |
224,172 | def move_items ( self , from_group , to_group ) : if from_group not in self . keys ( ) or len ( self . groups [ from_group ] ) == 0 : return self . groups . setdefault ( to_group , list ( ) ) . extend ( self . groups . get ( from_group , list ( ) ) ) if from_group in self . groups : del self . groups [ from_group ] | Take all elements from the from_group and add it to the to_group . | 94 | 17 |
224,173 | def sort_by_size ( self , group_limit = None , discard_others = False , others_label = 'others' ) : # sort groups by number of elements self . groups = OrderedDict ( sorted ( six . iteritems ( self . groups ) , key = lambda x : len ( x [ 1 ] ) , reverse = True ) ) # if group-limit is provided, combine remaining groups if group_limit is not None : # now group together all groups that did not make the limit if not discard_others : group_keys = self . groups . keys ( ) [ group_limit - 1 : ] self . groups . setdefault ( others_label , list ( ) ) else : group_keys = self . groups . keys ( ) [ group_limit : ] # only go to second last (-1), since the 'others' group is now last for g in group_keys : if not discard_others : self . groups [ others_label ] . extend ( self . groups [ g ] ) del self . groups [ g ] # remove if empty if ( others_label in self . groups and len ( self . groups [ others_label ] ) == 0 ) : del self . groups [ others_label ] # remove others group regardless of limit if requested if discard_others and others_label in self . groups : del self . groups [ others_label ] | Sort the groups by the number of elements they contain descending . | 297 | 12 |
224,174 | def import_l2c_db ( ) : data_path = os . path . join ( os . path . dirname ( mtools . __file__ ) , 'data' ) if os . path . exists ( os . path . join ( data_path , 'log2code.pickle' ) ) : av , lv , lbw , lcl = cPickle . load ( open ( os . path . join ( data_path , 'log2code.pickle' ) , 'rb' ) ) return av , lv , lbw , lcl else : raise ImportError ( 'log2code.pickle not found in %s.' % data_path ) | Static import helper function . | 147 | 5 |
224,175 | def _strip_counters ( self , sub_line ) : try : end = sub_line . rindex ( '}' ) except ValueError : return sub_line else : return sub_line [ : ( end + 1 ) ] | Find the codeline end by taking out the counters and durations . | 51 | 14 |
224,176 | def _strip_datetime ( self , sub_line ) : try : begin = sub_line . index ( ']' ) except ValueError : return sub_line else : # create a "" in place character for the beginnings.. # needed when interleaving the lists sub = sub_line [ begin + 1 : ] return sub | Strip datetime and other parts so that there is no redundancy . | 69 | 14 |
224,177 | def _find_variable ( self , pattern , logline ) : var_subs = [ ] # find the beginning of the pattern first_index = logline . index ( pattern [ 0 ] ) beg_str = logline [ : first_index ] # strip the beginning substring var_subs . append ( self . _strip_datetime ( beg_str ) ) for patt , patt_next in zip ( pattern [ : - 1 ] , pattern [ 1 : ] ) : # regular expression pattern that finds what's in the middle of # two substrings pat = re . escape ( patt ) + '(.*)' + re . escape ( patt_next ) # extract whats in the middle of the two substrings between = re . search ( pat , logline ) try : # add what's in between if the search isn't none var_subs . append ( between . group ( 1 ) ) except Exception : pass rest_of_string = logline . rindex ( pattern [ - 1 ] ) + len ( pattern [ - 1 ] ) # add the rest of the string to end minus the counters and durations end_str = logline [ rest_of_string : ] var_subs . append ( self . _strip_counters ( end_str ) ) # strip whitespace from each string, but keep the strings themselves # var_subs = [v.strip() for v in var_subs] return var_subs | Return the variable parts of the code given a tuple of strings pattern . | 311 | 14 |
224,178 | def _variable_parts ( self , line , codeline ) : var_subs = [ ] # codeline has pattern and then has the outputs in different versions if codeline : var_subs = self . _find_variable ( codeline . pattern , line ) else : # make variable part of the line string without all the other stuff line_str = self . _strip_datetime ( self . _strip_counters ( line ) ) var_subs = [ line_str . strip ( ) ] return var_subs | Return variable parts of the codeline given the static parts . | 114 | 12 |
224,179 | def combine ( self , pattern , variable ) : inter_zip = izip_longest ( variable , pattern , fillvalue = '' ) interleaved = [ elt for pair in inter_zip for elt in pair ] return '' . join ( interleaved ) | Combine a pattern and variable parts to be a line string again . | 58 | 14 |
224,180 | def run ( self , arguments = None , get_unknowns = False ) : # redirect PIPE signal to quiet kill script, if not on Windows if os . name != 'nt' : signal . signal ( signal . SIGPIPE , signal . SIG_DFL ) if get_unknowns : if arguments : self . args , self . unknown_args = ( self . argparser . parse_known_args ( args = arguments . split ( ) ) ) else : ( self . args , self . unknown_args ) = self . argparser . parse_known_args ( ) self . args = vars ( self . args ) else : if arguments : myargs = arguments . split ( ) self . args = vars ( self . argparser . parse_args ( args = myargs ) ) else : self . args = vars ( self . argparser . parse_args ( ) ) self . progress_bar_enabled = ( not ( self . args [ 'no_progressbar' ] or self . is_stdin ) ) | Init point to execute the script . | 222 | 7 |
224,181 | def update_progress ( self , progress , prefix = '' ) : total_length = 40 if progress == 1. : sys . stderr . write ( '\r' + ' ' * ( total_length + len ( prefix ) + 50 ) ) sys . stderr . write ( '\n' ) sys . stderr . flush ( ) else : bar_length = int ( round ( total_length * progress ) ) sys . stderr . write ( '\r%s [%s%s] %.1f %% ' % ( prefix , '=' * bar_length , ' ' * ( total_length - bar_length ) , progress * 100 ) ) sys . stderr . flush ( ) | Print a progress bar for longer - running scripts . | 156 | 10 |
224,182 | def accept_line ( self , logevent ) : if self . regex_mode : return bool ( re . search ( self . field , logevent . line_str ) ) else : return getattr ( logevent , self . field ) is not None | Return True if the log line has the nominated yaxis field . | 56 | 13 |
224,183 | def clicked ( self , event ) : group = event . artist . _mt_group indices = event . ind # double click only supported on 1.2 or later major , minor , _ = mpl_version . split ( '.' ) if ( int ( major ) , int ( minor ) ) < ( 1 , 2 ) or not event . mouseevent . dblclick : for i in indices : print ( self . groups [ group ] [ i ] . line_str ) else : # toggle durline first = indices [ 0 ] logevent = self . groups [ group ] [ first ] try : # remove triangle for this event idx = map ( itemgetter ( 0 ) , self . durlines ) . index ( logevent ) _ , poly = self . durlines [ idx ] poly . remove ( ) plt . gcf ( ) . canvas . draw ( ) del self . durlines [ idx ] except ValueError : # construct triangle and add to list of durlines if self . args [ 'optime_start' ] : pts = [ [ date2num ( logevent . datetime ) , 0 ] , [ date2num ( logevent . datetime ) , logevent . duration ] , [ date2num ( logevent . datetime + timedelta ( milliseconds = logevent . duration ) ) , 0 ] ] else : pts = [ [ date2num ( logevent . datetime ) , 0 ] , [ date2num ( logevent . datetime ) , logevent . duration ] , [ date2num ( logevent . datetime - timedelta ( milliseconds = logevent . duration ) ) , 0 ] ] poly = Polygon ( pts , closed = True , alpha = 0.2 , linewidth = 0 , facecolor = event . artist . get_markerfacecolor ( ) , edgecolor = None , zorder = - 10000 ) ax = plt . gca ( ) ax . add_patch ( poly ) plt . gcf ( ) . canvas . draw ( ) self . durlines . append ( ( logevent , poly ) ) | Call if an element of this plottype is clicked . | 459 | 11 |
224,184 | def run ( self , arguments = None ) : LogFileTool . run ( self , arguments ) for i , self . logfile in enumerate ( self . args [ 'logfile' ] ) : if i > 0 : print ( "\n ------------------------------------------\n" ) if self . logfile . datetime_format == 'ctime-pre2.4' : # no milliseconds when datetime format doesn't support it start_time = ( self . logfile . start . strftime ( "%Y %b %d %H:%M:%S" ) if self . logfile . start else "unknown" ) end_time = ( self . logfile . end . strftime ( "%Y %b %d %H:%M:%S" ) if self . logfile . start else "unknown" ) else : # include milliseconds start_time = ( self . logfile . start . strftime ( "%Y %b %d " "%H:%M:%S.%f" ) [ : - 3 ] if self . logfile . start else "unknown" ) end_time = ( self . logfile . end . strftime ( "%Y %b %d " "%H:%M:%S.%f" ) [ : - 3 ] if self . logfile . start else "unknown" ) print ( " source: %s" % self . logfile . name ) print ( " host: %s" % ( self . logfile . hostname + ':' + str ( self . logfile . port ) if self . logfile . hostname else "unknown" ) ) print ( " start: %s" % ( start_time ) ) print ( " end: %s" % ( end_time ) ) # TODO: add timezone if iso8601 format print ( "date format: %s" % self . logfile . datetime_format ) print ( " length: %s" % len ( self . logfile ) ) print ( " binary: %s" % ( self . logfile . binary or "unknown" ) ) version = ( ' -> ' . join ( self . logfile . versions ) or "unknown" ) # if version is unknown, go by date if version == 'unknown' : if self . logfile . datetime_format == 'ctime-pre2.4' : version = '< 2.4 (no milliseconds)' elif self . logfile . datetime_format == 'ctime' : version = '>= 2.4.x ctime (milliseconds present)' elif ( self . logfile . datetime_format == "iso8601-utc" or self . logfile . datetime_format == "iso8601-local" ) : if self . logfile . has_level : version = '>= 3.0 (iso8601 format, level, component)' else : version = '= 2.6.x (iso8601 format)' print ( " version: %s" % version ) print ( " storage: %s" % ( self . logfile . storage_engine or 'unknown' ) ) # now run all sections for section in self . sections : if section . active : print ( "\n%s" % section . name . upper ( ) ) section . run ( ) | Print useful information about the log file . | 708 | 8 |
224,185 | def filesize ( self ) : if self . from_stdin : return None if not self . _filesize : self . _calculate_bounds ( ) return self . _filesize | Lazy evaluation of start and end of logfile . | 42 | 11 |
224,186 | def num_lines ( self ) : if self . from_stdin : return None if not self . _num_lines : self . _iterate_lines ( ) return self . _num_lines | Lazy evaluation of the number of lines . | 43 | 9 |
224,187 | def versions ( self ) : versions = [ ] for v , _ in self . restarts : if len ( versions ) == 0 or v != versions [ - 1 ] : versions . append ( v ) return versions | Return all version changes . | 44 | 5 |
224,188 | def next ( self ) : # use readline here because next() iterator uses internal readahead # buffer so seek position is wrong line = self . filehandle . readline ( ) line = line . decode ( 'utf-8' , 'replace' ) if line == '' : raise StopIteration line = line . rstrip ( '\n' ) le = LogEvent ( line ) # hint format and nextpos from previous line if self . _datetime_format and self . _datetime_nextpos is not None : ret = le . set_datetime_hint ( self . _datetime_format , self . _datetime_nextpos , self . year_rollover ) if not ret : # logevent indicates timestamp format has changed, # invalidate hint info self . _datetime_format = None self . _datetime_nextpos = None elif le . datetime : # gather new hint info from another logevent self . _datetime_format = le . datetime_format self . _datetime_nextpos = le . _datetime_nextpos return le | Get next line adjust for year rollover and hint datetime format . | 235 | 14 |
224,189 | def _calculate_bounds ( self ) : if self . _bounds_calculated : # Assume no need to recalc bounds for lifetime of a Logfile object return if self . from_stdin : return False # we should be able to find a valid log line within max_start_lines max_start_lines = 10 lines_checked = 0 # get start datetime for line in self . filehandle : logevent = LogEvent ( line ) lines_checked += 1 if logevent . datetime : self . _start = logevent . datetime self . _timezone = logevent . datetime . tzinfo self . _datetime_format = logevent . datetime_format self . _datetime_nextpos = logevent . _datetime_nextpos break if lines_checked > max_start_lines : break # sanity check before attempting to find end date if ( self . _start is None ) : raise SystemExit ( "Error: <%s> does not appear to be a supported " "MongoDB log file format" % self . filehandle . name ) # get end datetime (lines are at most 10k, # go back 30k at most to make sure we catch one) self . filehandle . seek ( 0 , 2 ) self . _filesize = self . filehandle . tell ( ) self . filehandle . seek ( - min ( self . _filesize , 30000 ) , 2 ) for line in reversed ( self . filehandle . readlines ( ) ) : logevent = LogEvent ( line ) if logevent . datetime : self . _end = logevent . datetime break # if there was a roll-over, subtract 1 year from start time if self . _end < self . _start : self . _start = self . _start . replace ( year = self . _start . year - 1 ) self . _year_rollover = self . _end else : self . _year_rollover = False # reset logfile self . filehandle . seek ( 0 ) self . _bounds_calculated = True return True | Calculate beginning and end of logfile . | 458 | 10 |
224,190 | def _find_curr_line ( self , prev = False ) : curr_pos = self . filehandle . tell ( ) # jump back 15k characters (at most) and find last newline char jump_back = min ( self . filehandle . tell ( ) , 15000 ) self . filehandle . seek ( - jump_back , 1 ) buff = self . filehandle . read ( jump_back ) self . filehandle . seek ( curr_pos , 0 ) if prev and self . prev_pos is not None and self . prev_pos == curr_pos : # Number of characters to show before/after the log offset error_context = 300 self . filehandle . seek ( - error_context , 1 ) buff = self . filehandle . read ( curr_pos ) hr = "-" * 60 print ( "Fatal log parsing loop detected trying to find previous " "log line near offset %s in %s:\n\n%s\n%s\n" "<--- (current log parsing offset) \n%s\n%s\n" % ( curr_pos , self . name , hr , buff [ : error_context ] , buff [ error_context : error_context + 1 ] , hr ) , file = sys . stderr ) raise SystemExit ( "Cannot parse %s with requested options" % self . filehandle . name ) else : self . prev_pos = curr_pos buff = buff . decode ( "utf-8" , "replace" ) newline_pos = buff . rfind ( '\n' ) if prev : newline_pos = buff [ : newline_pos ] . rfind ( '\n' ) # move back to last newline char if newline_pos == - 1 : self . filehandle . seek ( 0 ) return self . next ( ) self . filehandle . seek ( newline_pos - jump_back + 1 , 1 ) # roll forward until we found a line with a datetime try : logevent = self . next ( ) while not logevent . datetime : logevent = self . next ( ) return logevent except StopIteration : # reached end of file return None | Internal helper function . | 478 | 4 |
224,191 | def fast_forward ( self , start_dt ) : if self . from_stdin : # skip lines until start_dt is reached return else : # fast bisection path max_mark = self . filesize step_size = max_mark # check if start_dt is already smaller than first datetime self . filehandle . seek ( 0 ) le = self . next ( ) if le . datetime and le . datetime >= start_dt : self . filehandle . seek ( 0 ) return le = None self . filehandle . seek ( 0 ) # search for lower bound while abs ( step_size ) > 100 : step_size = ceil ( step_size / 2. ) self . filehandle . seek ( step_size , 1 ) le = self . _find_curr_line ( ) if not le : break if le . datetime >= start_dt : step_size = - abs ( step_size ) else : step_size = abs ( step_size ) if not le : return # now walk backwards until we found a truly smaller line while self . filehandle . tell ( ) >= 2 and ( le . datetime is None or le . datetime >= start_dt ) : self . filehandle . seek ( - 2 , 1 ) le = self . _find_curr_line ( prev = True ) | Fast - forward file to given start_dt datetime obj using binary search . | 285 | 16 |
224,192 | def setup ( self ) : if self . mlogfilter . is_stdin : # assume this year (we have no other info) now = datetime . now ( ) self . startDateTime = datetime ( now . year , 1 , 1 , tzinfo = tzutc ( ) ) self . endDateTime = datetime ( MAXYEAR , 12 , 31 , tzinfo = tzutc ( ) ) else : logfiles = self . mlogfilter . args [ 'logfile' ] self . startDateTime = min ( [ lf . start + timedelta ( hours = self . mlogfilter . args [ 'timezone' ] [ i ] ) for i , lf in enumerate ( logfiles ) ] ) self . endDateTime = max ( [ lf . end + timedelta ( hours = self . mlogfilter . args [ 'timezone' ] [ i ] ) for i , lf in enumerate ( logfiles ) ] ) # now parse for further changes to from and to datetimes dtbound = DateTimeBoundaries ( self . startDateTime , self . endDateTime ) self . fromDateTime , self . toDateTime = dtbound ( self . mlogfilter . args [ 'from' ] or None , self . mlogfilter . args [ 'to' ] or None ) # define start_limit for mlogfilter's fast_forward method self . start_limit = self . fromDateTime # for single logfile, get file seek position of `to` datetime if ( len ( self . mlogfilter . args [ 'logfile' ] ) == 1 and not self . mlogfilter . is_stdin ) : if self . mlogfilter . args [ 'to' ] != "end" : # fast forward, get seek value, then reset file logfile = self . mlogfilter . args [ 'logfile' ] [ 0 ] logfile . fast_forward ( self . toDateTime ) self . seek_to = logfile . filehandle . tell ( ) logfile . filehandle . seek ( 0 ) else : self . seek_to = - 1 else : self . seek_to = False | Get start end end date of logfile before starting to parse . | 470 | 13 |
224,193 | def num_events ( self ) : if not self . _num_events : self . _num_events = self . coll_handle . count ( ) return self . _num_events | Lazy evaluation of the number of events . | 40 | 9 |
224,194 | def next ( self ) : if not self . cursor : self . cursor = self . coll_handle . find ( ) . sort ( [ ( "ts" , ASCENDING ) ] ) doc = self . cursor . next ( ) doc [ 'thread' ] = self . name le = LogEvent ( doc ) return le | Make iterators . | 68 | 4 |
224,195 | def _calculate_bounds ( self ) : # get start datetime first = self . coll_handle . find_one ( None , sort = [ ( "ts" , ASCENDING ) ] ) last = self . coll_handle . find_one ( None , sort = [ ( "ts" , DESCENDING ) ] ) self . _start = first [ 'ts' ] if self . _start . tzinfo is None : self . _start = self . _start . replace ( tzinfo = tzutc ( ) ) self . _end = last [ 'ts' ] if self . _end . tzinfo is None : self . _end = self . _end . replace ( tzinfo = tzutc ( ) ) return True | Calculate beginning and end of log events . | 168 | 10 |
224,196 | def run ( self ) : if ProfileCollection and isinstance ( self . mloginfo . logfile , ProfileCollection ) : print ( "\n not available for system.profile collections\n" ) return codelines = defaultdict ( lambda : 0 ) non_matches = 0 # get log file information logfile = self . mloginfo . logfile if logfile . start and logfile . end and not self . mloginfo . args [ 'verbose' ] : progress_start = self . mloginfo . _datetime_to_epoch ( logfile . start ) progress_total = ( self . mloginfo . _datetime_to_epoch ( logfile . end ) - progress_start ) else : self . mloginfo . progress_bar_enabled = False for i , logevent in enumerate ( self . mloginfo . logfile ) : cl , _ = self . log2code ( logevent . line_str ) # update progress bar every 1000 lines if self . mloginfo . progress_bar_enabled and ( i % 1000 == 0 ) : if logevent . datetime : progress_curr = self . mloginfo . _datetime_to_epoch ( logevent . datetime ) ( self . mloginfo . update_progress ( float ( progress_curr - progress_start ) / progress_total ) ) if cl : codelines [ cl . pattern ] += 1 else : if logevent . operation : # skip operations (command, insert, update, delete, # query, getmore) continue if not logevent . thread : # skip the lines that don't have a thread name # (usually map/reduce or assertions) continue if len ( logevent . split_tokens ) - logevent . datetime_nextpos <= 1 : # skip empty log messages (after thread name) continue if ( "warning: log line attempted" in logevent . line_str and "over max size" in logevent . line_str ) : # skip lines that are too long continue # everything else is a real non-match non_matches += 1 if self . mloginfo . args [ 'verbose' ] : print ( "couldn't match:" + logevent ) # clear progress bar again if self . mloginfo . progress_bar_enabled : self . mloginfo . update_progress ( 1.0 ) if self . mloginfo . args [ 'verbose' ] : print ( '' ) for cl in sorted ( codelines , key = lambda x : codelines [ x ] , reverse = True ) : print ( "%8i %s" % ( codelines [ cl ] , " ... " . join ( cl ) ) ) print ( '' ) if non_matches > 0 : print ( "distinct couldn't match %i lines" % non_matches ) if not self . mloginfo . args [ 'verbose' ] : print ( "to show non-matched lines, run with --verbose." ) | Run each line through log2code and group by matched pattern . | 653 | 13 |
224,197 | def shell2json ( s ) : replace = { r'BinData\(.+?\)' : '1' , r'(new )?Date\(.+?\)' : '1' , r'Timestamp\(.+?\)' : '1' , r'ObjectId\(.+?\)' : '1' , r'DBRef\(.+?\)' : '1' , r'undefined' : '1' , r'MinKey' : '1' , r'MaxKey' : '1' , r'NumberLong\(.+?\)' : '1' , r'/.+?/\w*' : '1' } for key , value in replace . items ( ) : s = re . sub ( key , value , s ) return s | Convert shell syntax to json . | 181 | 7 |
224,198 | def json2pattern ( s ) : # make valid JSON by wrapping field names in quotes s , _ = re . subn ( r'([{,])\s*([^,{\s\'"]+)\s*:' , ' \\1 "\\2" : ' , s ) # handle shell values that are not valid JSON s = shell2json ( s ) # convert to 1 where possible, to get rid of things like new Date(...) s , n = re . subn ( r'([:,\[])\s*([^{}\[\]"]+?)\s*([,}\]])' , '\\1 1 \\3' , s ) # now convert to dictionary, converting unicode to ascii try : doc = json . loads ( s , object_hook = _decode_pattern_dict ) return json . dumps ( doc , sort_keys = True , separators = ( ', ' , ': ' ) ) except ValueError as ex : return None | Convert JSON format to a query pattern . | 210 | 9 |
224,199 | def print_table ( rows , override_headers = None , uppercase_headers = True ) : if len ( rows ) == 0 : return keys = list ( rows [ 0 ] . keys ( ) ) headers = override_headers or keys if uppercase_headers : rows = [ dict ( zip ( keys , map ( lambda x : x . upper ( ) , headers ) ) ) , None ] + rows else : rows = [ dict ( zip ( keys , headers ) ) , None ] + rows lengths = [ max ( len ( str ( row [ k ] ) ) for row in rows if hasattr ( row , '__iter__' ) ) for k in keys ] tmp = [ '{%s:%i}' % ( h , l ) for h , l in zip ( keys [ : - 1 ] , lengths [ : - 1 ] ) ] tmp . append ( '{%s}' % keys [ - 1 ] ) template = ( ' ' * 4 ) . join ( tmp ) for row in rows : if type ( row ) == str : print ( row ) elif row is None : print ( ) elif isinstance ( row , dict ) : row = { k : v if v is not None else 'None' for k , v in row . items ( ) } print ( template . format ( * * row ) ) else : print ( "Unhandled row type:" , row ) | All rows need to be a list of dictionaries all with the same keys . | 299 | 16 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.