def GetChainHead():
    try:    
        with grpc.insecure_channel(client_conf.server_address()) as channel:
            stub = beacon_chain_pb2_grpc.BeaconChainStub(channel)
            response = stub.GetChainHead(get_empty_data())
            return response
    except Exception as e:
        raise e
        return False
def GetChainHead():
    try:
        with grpc.insecure_channel('54.243.16.45:4000') as channel:
            stub = beacon_chain_pb2_grpc.BeaconChainStub(channel)
            response = stub.GetChainHead(get_empty_data())
            return response
    except Exception as e:
        raise e
        return False
def GetValidatorPerformance(public_keys):
    try:
        with grpc.insecure_channel(client_conf.server_address()) as channel:
            stub = beacon_chain_pb2_grpc.BeaconChainStub(channel)
            response = stub.GetValidatorPerformance(
                beacon_chain_pb2.ValidatorPerformanceRequest(
                    public_keys=public_keys
                )
            )
            return response
    
    except Exception as e:
        raise e
def GetValidator(index):
    try:
        with grpc.insecure_channel(client_conf.server_address()) as channel:
            stub = beacon_chain_pb2_grpc.BeaconChainStub(channel)
            response = stub.GetValidator(
                beacon_chain_pb2.GetValidatorRequest(
                    index = index
                )
            )
            return response
    
    except Exception as e:
        raise e
def ListValidators(epoch=0,pageToken="0"):
    try:
        with grpc.insecure_channel(client_conf.server_address()) as channel:
            stub = beacon_chain_pb2_grpc.BeaconChainStub(channel)
            response = stub.ListValidators(
                beacon_chain_pb2.ListValidatorsRequest(
                    epoch = epoch,
                    page_token = pageToken
                )
            )
            return response
    except Exception as e:
        raise e
def getChainHeadStream():
    try:
        with grpc.insecure_channel(client_conf.server_address()) as channel:
            stub = beacon_chain_pb2_grpc.BeaconChainStub(channel)
            for data in stub.StreamChainHead(get_empty_data()):
                return_data = {
                    'headEpoch' : data.head_epoch,
                    'headSlot' : data.head_slot
                }

                yield return_data

            
    except Exception as e:
        print (e)
        raise e
def list_validators():
    try:
        with grpc.insecure_channel('54.243.16.45:4000') as channel:
            stub = beacon_chain_pb2_grpc.BeaconChainStub(channel)
            response = stub.ListValidators(
                beacon_chain_pb2.ListValidatorsRequest(epoch=3003,
                                                       page_size=100))
            # return response.validator_list
            isntance = beacon_chain_pb2.Validators()
            response_ = isntance.SerializeToString(response)

            isntance.ParseFromString(response)
            print(isntance)
            # vl= response.validator_list[0].validator

            # print (type(vl))
            # a = client_validators.decode_public_key(vl)
            # print (a)
            # return a
            # vl= response.validator_list

    except Exception as e:
        return False