Esempio n. 1
0
def send_latest_msg():
    try_num = 0
    while 1:
        if try_num > 30:
            return
        if not gbt_height_msgs:
            sleep(1)
            try_num += 1
            continue
        latest_height = max(gbt_height_msgs.keys())
        stratum_msg = gbt_height_msgs[latest_height]
        job_maker_logger.info("send newbi latest msg ", stratum_msg)
        job_producer.send_json_or_dict(stratum_msg, topic=JOB_TOPIC_NEWBI)
        break
Esempio n. 2
0
 def send_json_or_dict(self, params, topic=None):
     print("kafka product", params)
     if not topic:
         topic = self.topic
     try:
         if isinstance(params, dict):
             params = json.dumps(params)
         self.client.send(topic, params.encode('utf-8'))
         self.client.flush()
     except KafkaTimeoutError and KafkaError as e:
         job_maker_logger.warning("kafka send time out :%s" % e)
     except Exception as e:
         job_maker_logger.warning("kafka send error :%s" % e)
     job_maker_logger.info("kafka send %s: %s" % (topic, params))
Esempio n. 3
0
def handler_gbt_msg(gbt_msg_str):
    gbt_msg = json.loads(gbt_msg_str)

    created_at_ts = gbt_msg['created_at_ts']
    block_template_base64 = gbt_msg['block_template_base64']

    block_template_str = base64.decodebytes(block_template_base64.encode())

    block_template = json.loads(block_template_str)

    gbt_result = block_template['result']

    baseTarget = gbt_result['baseTarget']
    generationSignature = gbt_result['generationSignature']
    targetDeadline = gbt_result['targetDeadline']
    height = gbt_result['height']

    if height in gbt_height_msgs.keys(
    ) and targetDeadline == gbt_height_msgs[height]['targetDeadline']:
        return gbt_height_msgs.get(height)

    if gbt_height_msgs:
        already_latest_height = max(gbt_height_msgs.keys())
    else:
        already_latest_height = height

    if already_latest_height > height:
        return gbt_height_msgs[already_latest_height]

    if len(gbt_height_msgs.keys()) > 20:
        outdated_height = min(gbt_height_msgs.keys())
        del gbt_height_msgs[outdated_height]
    """
        height, version, coinbasevalue, previousblockhash
    """
    stratum_msg = {
        "baseTarget": baseTarget,
        "generationSignature": generationSignature,
        "height": height,
        "job_id": str(time()) + str(height),
        "current_time": time(),
        "targetDeadline": targetDeadline
    }

    job_maker_logger.info("send handle msg ", stratum_msg)
    gbt_height_msgs[height] = stratum_msg
    job_producer.send_json_or_dict(stratum_msg, topic=JOB_TOPIC_BHD)