def validator_crawler(epoch=0, pageToken = '0'): try: for data in client_beacon_chain.getChainHeadStream(): list_validators = client_beacon_chain.ListValidators(epoch=epoch, pageToken=pageToken) for data in list_validators.validator_list: #todo process validator data here total_size = list_validators.total_size validators = data.validator index = data.index public_key = validators.public_key pkB64 = base64.b64encode(public_key).decode() pkHex = common.decode_public_key(pkB64) effective_balance = float(validators.effective_balance)/ 1000000000 exit_epoch = validators.exit_epoch withdrawable_epoch = validators.withdrawable_epoch #from GetValidator client get_validators = client_beacon_chain.GetValidator(index=index) activation_epoch = get_validators.activation_epoch activation_eligibility_epoch = get_validators.activation_eligibility_epoch slashed = get_validators.slashed #Get validators Status get_validators_status = client_validators.ValidatorStatus(public_key) status_key = get_validators_status.status status = {0:'UNKNOWN_STATUS', 1:'DEPOSITED', 2:'PENDING', 3: 'ACTIVE', 4:'EXITING', 5:'SLASHING', 6:'EXITED'} #GetValidatorsBalance get_validators_balance = client_beacon_chain.ListValidatorBalances([public_key]) balances_data = get_validators_balance.balances[0] balance = int(balances_data.balance)/ 1000000000 #insert in validator table db_con = mongo_helper.mongo_conn() db_data = db_con.validators_list.insert({ 'index' : index, 'effective_balance' : effective_balance, 'balance' : balance, 'public_key' : pkHex, 'total_size' : total_size, 'activation_epoch': activation_epoch, 'activation_eligibility_epoch': activation_eligibility_epoch, 'slashed' : slashed, 'status': status[status_key] }) if not (list_validators.next_page_token == ''): validator_crawler(epoch = epoch, pageToken= list_validators.next_page_token) return except Exception as e: error = common.get_error_traceback(sys, e) print (error)
def get_validators_api(args): try: pageToken = args.get("page", "1") pageSize = args.get("perPage", "10") uri = '/eth/v1alpha1/validators' url = base_url+uri # pageSize = 10 validators = http.request( 'GET', url, fields={ 'slot' : third_party.get_current_slot(), 'pageToken' : pageToken, 'pageSize' : pageSize } ) if validators.status == 200: validators = json.loads(validators.data.decode('UTF-8')) additional_data = { 'count' : len(validators.get('validatorList')) } validators_list = validators.get('validatorList') for data in validators_list: pk = dict(data.get('validator')) pkB64 = pk.get('publicKey') pkHex = common.decode_public_key(pkB64) pk['publicKey'] = pkHex pk['effectiveBalance'] = float(pk.get('effectiveBalance')) / 1000000000 data['validator'] = pk return common.send_sucess_msg(validators, **additional_data) except Exception as e: error = common.get_error_traceback(sys,e) print (error) return common.send_error_msg()
def list_validators_grpc(): try: response = client_beacon_chain.list_validators() if not response: raise ('Error') validator_list = [] for data in response: single_data = data.validator data_dist = dict() pk = single_data.public_key data_dist['public_key'] = common.decode_public_key(pk) data_dist['effective_balance'] = single_data.effective_balance validator_list.append(data_dist) return common.send_sucess_msg({'data':validator_list}) except Exception as e : error = common.get_error_traceback(sys,e) print (error) return common.send_error_msg()
def get_attestations(args): try: pageToken = args.get("page", "1") pageSize = args.get("perPage", "10") url = base_url+"/eth/v1alpha1/beacon/attestations" current_epoch = str(third_party.get_current_epoch()) # pageSize = 10 attestations = http.request( 'GET', url, fields={ 'epoch' : current_epoch, 'pageSize' : pageSize, 'pageToken' : pageToken } ) if attestations.status == 200: response = json.loads(attestations.data.decode('UTF-8')) attestation_list = response.get('attestations') attestation_list.reverse() for data in attestation_list: blockchain_data = data.get('data') blockchain_data['beaconBlockRoot'] = common.decode_public_key(blockchain_data.get('beaconBlockRoot')) data['data'] = blockchain_data additional_data = { 'defination' :'An attestation is a validator’s vote, weighted by the validator’s balance. Attestations are broadcasted by validators in addition to blocks.' } return common.send_sucess_msg(response, **additional_data) else: return common.send_error_msg() except Exception as e : error = common.get_error_traceback(sys,e) print (error) return common.send_error_msg()
def get_attestion_by_slot(args): try: ''' ''' uri = '/eth/v1alpha1/beacon/blocks' url = base_url+uri response = http.request( 'GET', url, fields={ 'slot' : args.get('slot') } ) if response.status == 200: slot_data = response.data.decode('UTF-8') slot_data = common.parse_dictionary(slot_data) block_container = slot_data.get('blockContainers') return_list = [] for single_block_data in block_container: return_data = { } block_data = single_block_data.get('block') block_detail = block_data.get('block') return_data['slot'] = block_detail.get('slot') block_body = block_detail.get('body') attestians_data = block_body.get('attestations') return_data['attestations_count'] = len(attestians_data) attestian_list = [] for single_attestation in attestians_data: single_attestians_return_data = { } single_attestians_return_data['aggregationBits'] = single_attestation.get('aggregationBits') single_attestians_return_data['signature'] = common.decode_public_key(single_attestation.get('signature')) single_attestians_data = single_attestation.get('data') single_attestians_return_data['beaconBlockRoot'] = common.decode_public_key(single_attestians_data.get('beaconBlockRoot')) single_attestians_return_data['committeeIndex'] = single_attestians_data.get('committeeIndex') single_attestians_return_data['source_epoch'] = single_attestians_data.get('source').get('epoch') single_attestians_return_data['source_epoch_root'] = common.decode_public_key(single_attestians_data.get('source').get('root')) single_attestians_return_data['target_epoch'] = single_attestians_data.get('target').get('epoch') single_attestians_return_data['target_epoch_root'] = common.decode_public_key(single_attestians_data.get('target').get('root')) attestian_list.append(single_attestians_return_data) return_data['attestian_detail'] = attestian_list return common.send_sucess_msg({'data': return_data}) else: return common.send_error_msg() except Exception as e: error = common.get_error_traceback(sys,e) print (error) return common.send_error_msg()
def get_slot_data(slot): try: ''' retrives block by slot A slot is a chance for a block to be added to the Beacon Chain and shards. ''' uri = '/eth/v1alpha1/beacon/blocks' url = base_url+uri response = http.request( 'GET', url, fields={ 'slot' : slot } ) if response.status == 200: slot_data = response.data.decode('UTF-8') slot_data = common.parse_dictionary(slot_data) block_container = slot_data.get('blockContainers') slot_time = time_calculator.get_slot_time(slot) if len(block_container) == 0 : epoch = get_epoch_by_slot(slot) return_data = [{ 'status' : 'skipped', 'slot' : slot, 'epoch' : epoch, 'time' : slot_time }] return return_data return_list = [] for single_block_data in block_container: return_data = { 'status' : 'proposed', 'time' : slot_time } return_data['block_root'] = common.decode_public_key(single_block_data.get('blockRoot')) block_data = single_block_data.get('block') return_data['signature'] = common.decode_public_key(block_data.get('signature')) block_detail = block_data.get('block') return_data['parent_root'] = common.decode_public_key(block_detail.get('parentRoot')) return_data['proposer'] = block_detail.get('proposerIndex') return_data['slot'] = block_detail.get('slot') return_data['state_root'] = common.decode_public_key(block_detail.get('stateRoot')) block_body = block_detail.get('body') return_data['graffiti'] = { 'utf_8' : common.decode_bytes_utf8(block_body.get('graffiti')), 'hex' : common.decode_public_key(block_body.get('graffiti')) } return_data['randao_reveal'] = common.decode_public_key(block_body.get('randaoReveal')) return_data['deposits'] = len(block_body.get('deposits')) return_data['voluntaryExits'] = len(block_body.get('voluntaryExits')) return_data['slashing'] = { 'attester' : len(block_body.get('attesterSlashings')), 'proposer' : len(block_body.get('proposerSlashings')) } eth1_encoded_data = block_body.get('eth1Data') return_data['eth1_data'] = { 'block_hash' : common.decode_public_key(eth1_encoded_data.get('blockHash')), 'deposit_count' : eth1_encoded_data.get('depositCount'), 'deposit_root' : common.decode_public_key(eth1_encoded_data.get('depositRoot')) } return_data['attestations_count'] = len(block_body.get('attestations')) epoch = get_epoch_by_slot(slot) return_data['epoch'] = epoch return_list.append(return_data) return return_list else: return False except Exception as e: error = common.get_error_traceback(sys,e) print (error) return False
def get_validators_detail_by_public_key(pubkeyHex): ''' Validator info by Publick Key ''' try: uri = '/eth/v1alpha1/validator/status' url = base_url+uri pubkeyB64 = str(common.encode_pubic_key(pubkeyHex[2::]).decode('utf-8')) validators = http.request( 'GET', url, fields={ 'publicKey' : pubkeyB64 } ) if validators.status == 200: status_data = validators.data.decode('UTF-8') status_data = common.parse_dictionary(status_data) return_data = { 'status' : status_data.get('status'), 'activationEpoch' : status_data.get('activationEpoch') # 'eth1_DepositBlockNumber' : status_data.get('eth1DepositBlockNumber') } uri = '/eth/v1alpha1/validators/balances' url = base_url+uri response = http.request( 'GET', url, fields={ 'publicKeys' : pubkeyB64 } ) if response.status == 200: balance_data = response.data.decode('UTF-8') epoch = common.parse_dictionary(balance_data).get('epoch') balance_data = common.parse_dictionary(balance_data).get('balances') balance_data = balance_data[0] balance = int(balance_data.get('balance'))/1000000000 deposits_Received = int(balance_data.get('balance'))/1000000000 deposits_Received = str(round(deposits_Received, 0)) +" ETH" index = balance_data.get('index') uri = '/eth/v1alpha1/validator' url = base_url+uri validators = http.request( 'GET', url, fields={ 'index' : index } ) if validators.status == 200: validators = json.loads(validators.data.decode('UTF-8')) additional_data = { 'publicKey' : common.decode_public_key(validators.get('publicKey')), 'effectiveBalance' : str(int(validators.get('effectiveBalance'))/1000000000) + " ETH", 'slashed' : validators.get('slashed'), 'eligibilityEpoch' : validators.get('activationEligibilityEpoch'), 'withdrawalCredentials' : validators.get('withdrawalCredentials'), 'withdrawableEpoch' : validators.get('withdrawableEpoch') } return_data['currentBalance'] = balance return_data['depositsReceived'] = deposits_Received return_data['index'] = index return_data['epoh'] = epoch return_data['totalIncome'] = round(balance%32,5) return common.send_sucess_msg(return_data, ** additional_data) else: return common.send_error_msg() except Exception as e: error = common.get_error_traceback(sys,e) print (error) return common.send_error_msg()