Ejemplo n.º 1
0
def _loop(v_address):
    while P.F_NOW_BOOTING:
        time.sleep(2)
    for tx in tx_builder.unconfirmed.values():
        if tx.type == C.TX_START_CONTRACT:
            P.VALIDATOR_OBJ.put_unvalidated(tx)

    # check validated start tx
    for tx in tx_builder.unconfirmed.values():
        if tx.type == C.TX_FINISH_CONTRACT:
            c_result, start_hash, cs_diff = bjson.loads(tx.message)
            start_tx = P.VALIDATOR_OBJ[start_hash]
            if start_tx:
                sign_pair = message2signature(tx.b, v_address)
                if sign_pair in tx.signature:
                    P.VALIDATOR_OBJ.put_validated(start_tx)

    logging.info("Enabled validator mode [{}].".format(v_address))
    while True:
        for start_tx in P.VALIDATOR_OBJ.get_unvalidated():
            finish_tx, estimate_gas = finish_contract_tx(start_tx)
            P.VALIDATOR_OBJ.put_validated(start_tx)
            finish_tx.signature.append(message2signature(finish_tx.b, v_address))
            print(estimate_gas, finish_tx)
            logging.info("Validated! {}".format(finish_tx))
            time.sleep(1)
        time.sleep(1)
Ejemplo n.º 2
0
async def sign_raw_tx(request):
    post = await web_base.content_type_json_check(request)
    try:
        binary = unhexlify(post['hex'].encode())
        other_pairs = dict()
        for sk in post.get('pairs', list()):
            pk = public_key(sk=sk)
            ck = get_address(pk=pk, prefix=V.BLOCK_PREFIX)
            other_pairs[ck] = (pk, sign(msg=binary, sk=sk, pk=pk))
        tx = TX(binary=binary)
        for txhash, txindex in tx.inputs:
            input_tx = tx_builder.get_tx(txhash)
            address, coin_id, amount = input_tx.outputs[txindex]
            try:
                tx.signature.append(
                    message2signature(raw=tx.b, address=address))
            except BlockChainError:
                if address not in other_pairs:
                    raise BlockChainError(
                        'Not found secret key "{}"'.format(address))
                tx.signature.append(other_pairs[address])
        data = tx.getinfo()
        return web_base.json_res({
            'hash': data['hash'],
            'signature': data['signature'],
            'hex': hexlify(tx.b).decode()
        })
    except BaseException:
        return web_base.error_res()
Ejemplo n.º 3
0
Archivo: utils.py Proyecto: kmn/bc4py
def setup_contract_signature(tx, validators):
    count = 0
    for address in validators:
        try:
            sign_pairs = message2signature(raw=tx.b, address=address)
        except BlockChainError:
            continue
        if sign_pairs in tx.signature:
            pass
        elif sign_pairs:
            tx.signature.append(sign_pairs)
            count += 1
    return count
Ejemplo n.º 4
0
Archivo: utils.py Proyecto: kmn/bc4py
def setup_signature(tx, input_address):
    # tx.signature.clear()
    for address in input_address:
        sign_pairs = message2signature(raw=tx.b, address=address)
        tx.signature.append(sign_pairs)
Ejemplo n.º 5
0
 def proof_of_stake(self):
     global staking_limit
     limit_deque = deque(maxlen=10)
     self.event_close.set()
     while self.event_close.is_set():
         # check start mining
         if previous_block is None or unconfirmed_txs is None or unspents_txs is None:
             sleep(0.1)
             continue
         if len(unspents_txs) == 0:
             logging.info("No unspents for staking, wait 180s..")
             sleep(180)
             continue
         start = time()
         # create staking block
         bits, target = get_bits_by_hash(previous_hash=previous_block.hash,
                                         consensus=C.BLOCK_POS)
         reward = GompertzCurve.calc_block_reward(previous_block.height + 1)
         staking_block = Block(
             block={
                 'merkleroot': b'\xff' * 32,
                 'time': 0,
                 'previous_hash': previous_block.hash,
                 'bits': bits,
                 'nonce': b'\xff\xff\xff\xff'
             })
         staking_block.height = previous_block.height + 1
         staking_block.flag = C.BLOCK_POS
         staking_block.bits2target()
         staking_block.txs.append(None)  # Dummy proof tx
         staking_block.txs.extend(unconfirmed_txs)
         calculate_nam = 0
         for proof_tx in unspents_txs.copy():
             address = proof_tx.outputs[0][0]
             proof_tx.outputs[0] = (address, 0,
                                    proof_tx.pos_amount + reward)
             proof_tx.update_time()
             calculate_nam += 1
             # next check block
             if previous_block is None or unconfirmed_txs is None or unspents_txs is None:
                 logging.debug("Reset by \"nothing params found\"")
                 sleep(1)
                 break
             elif previous_block.hash != staking_block.previous_hash:
                 logging.debug("Reset by \"Don't match previous_hash\"")
                 sleep(1)
                 break
             elif not proof_tx.pos_check(
                     previous_hash=previous_block.hash,
                     pos_target_hash=staking_block.target_hash):
                 continue
             else:
                 # Staked yay!!
                 proof_tx.height = staking_block.height
                 proof_tx.signature = [
                     message2signature(proof_tx.b, proof_tx.outputs[0][0])
                 ]
                 staking_block.txs[0] = proof_tx
                 # Fit block size
                 while staking_block.getsize() > C.SIZE_BLOCK_LIMIT:
                     tx = staking_block.txs.pop()
                     if tx.type == C.TX_FINISH_CONTRACT:
                         staking_block.txs.pop()
                 staking_block.update_time(proof_tx.time)
                 staking_block.update_merkleroot()
                 confirmed_generating_block(staking_block)
                 break
         else:
             # check time
             used = time() - start
             remain = 1.0 - used
             max_limit = max(50, int(calculate_nam / max(0.0001, used)))
             limit_deque.append(int(max_limit * self.power_limit))
             staking_limit = sum(limit_deque) // len(limit_deque)
             if int(time()) % 90 == 0:
                 logging.info("Staking... margin={}% limit={}".format(
                     round(remain * 100, 1), staking_limit))
             self.hashrate = (calculate_nam, time())
             sleep(max(0.0, remain))
     logging.info("Close signal")