Esempio n. 1
0
def create_transaction(receiver_public_keys: List[str],
                       amounts: List[int],
                       sender_public_key,
                       message="") -> Transaction:
    vout = {}
    vin = {}
    current_amount = 0
    total_amount = sum(amounts)
    i = 0
    for so, utxo_list in BLOCKCHAIN.active_chain.utxo.utxo.items():
        tx_out = utxo_list[0]
        if current_amount >= total_amount:
            break
        if tx_out.address == sender_public_key:
            current_amount += tx_out.amount
            vin[i] = TxIn(payout=SingleOutput.from_json(so),
                          pub_key=sender_public_key,
                          sig="")
            i += 1

    for i, address in enumerate(receiver_public_keys):
        vout[i] = TxOut(amount=amounts[i], address=address)
    change = (current_amount - total_amount)
    if change > 0:
        vout[i + 1] = TxOut(amount=change, address=sender_public_key)

    tx = Transaction(version=consts.MINER_VERSION,
                     locktime=0,
                     timestamp=int(time.time()),
                     vin=vin,
                     vout=vout,
                     message=message)
    return tx
Esempio n. 2
0
def calculate_transaction_fees(tx: Transaction, w: Wallet, bounty: int,
                               fees: int):
    current_amount = 0
    i = 0
    for so, utxo_list in BLOCKCHAIN.active_chain.utxo.utxo.items():
        tx_out = utxo_list[0]
        if utxo_list[2]:
            # check for coinbase TxIn Maturity
            if not (BLOCKCHAIN.active_chain.length -
                    utxo_list[1].height) >= consts.COINBASE_MATURITY:
                continue
        if current_amount >= bounty + fees:
            break
        if tx_out.address == w.public_key:
            current_amount += tx_out.amount
            tx.vin[i] = TxIn(payout=SingleOutput.from_json(so),
                             pub_key=w.public_key,
                             sig="")
            i += 1
    tx.vout[1].amount = current_amount - bounty - fees
    tx.fees = fees
    tx.sign(w)
Esempio n. 3
0

def fetch_peer_list():
    r = requests.get(consts.SEED_SERVER_URL)
    peer_list = json.loads(r.text)
    return peer_list


def get_peer_url(peer: Dict[str, Any]) -> str:
    return "http://" + str(peer["ip"]) + ":" + str(peer["port"])


if __name__ == "__main__":

    # The singleOutput for first coinbase transaction in genesis block
    so = SingleOutput(txid=dhash(first_block_transaction[0]), vout=0)

    first_transaction = Transaction(
        version=1,
        locktime=0,
        timestamp=3,
        is_coinbase=False,
        fees=4000000000,
        vin={0: TxIn(payout=so, sig="", pub_key=consts.WALLET_PUBLIC)},
        vout={0: TxOut(amount=1000000000, address=consts.WALLET_PUBLIC)}
    )

    sign_copy_of_tx = copy.deepcopy(first_transaction)
    sign_copy_of_tx.vin = {}
    w = Wallet()
    w.public_key = consts.WALLET_PUBLIC
Esempio n. 4
0
        return get_block_from_db(hhash)
    return "Hash hi nahi bheja LOL"


@app.route("/getblockhashes", methods=["POST"])
def send_block_hashes():
    peer_height = int(request.form.get("myheight"))
    hash_list = []
    for i in range(peer_height, ACTIVE_CHAIN.length):
        hash_list.append(dhash(ACTIVE_CHAIN.header_list[i]))
    logger.debug(peer_height)
    return jsonify(hash_list)


# The singleOutput for first coinbase transaction in genesis block
so = SingleOutput(txid=dhash(genesis_block_transaction[0]), vout=0)

first_block_transactions = [
    Transaction(
        version=1,
        locktime=0,
        timestamp=2,
        is_coinbase=True,
        fees=0,
        vin={0: TxIn(payout=None, sig="", pub_key=consts.WALLET_PUBLIC)},
        vout={
            0: TxOut(amount=5000000000, address=consts.WALLET_PUBLIC),
            1: TxOut(amount=4000000000, address=consts.WALLET_PUBLIC),
        },
    ),
    Transaction(