Esempio n. 1
0
def ProccesstheTransaction(block):
    """
    It performs the transactions in the block.vali list and
    puts the transactions in order.

    Queuing is required so that all nodes have the same transaction hash.
    """

    from_user_list = []
    temp_validating_list = block.validating_list

    temp_accounts = GetAccounts()

    for trans in block.validating_list:
        touser_inlist = False
        address_of_fromUser = Address(trans.fromUser)

        for Accounts in temp_accounts:

            if Accounts.Address == address_of_fromUser:
                Accounts.balance -= (float(trans.amount) +
                                     trans.transaction_fee)
                Accounts.sequance_number += 1
                from_user_list.append(Accounts)

            elif Accounts.Address == trans.toUser:
                Accounts.balance += float(trans.amount)
                touser_inlist = True

        # If not included in the temp_accounts, add.
        if not touser_inlist:
            temp_accounts.append(Account(trans.toUser, float(trans.amount)))

    # Converts public keys to an Account class to use when ordering
    temp_pubkeys = []
    for tx_item in temp_validating_list[:]:
        for Account_item in from_user_list:

            if Address(tx_item.fromUser) == Account_item.Address:
                temp_pubkeys.append(tx_item.fromUser)
                tx_item.fromUser = Account_item

    # Orders the transactions by Address index of temp_accounts.
    temp_validating_list = sorted(
        temp_validating_list, key=lambda x: temp_accounts.index(x.fromUser))

    # Converts the Account class to Public key.
    for temp_validating_list_item in temp_validating_list[:]:
        for temp_pubkey in temp_pubkeys:
            if temp_validating_list_item.fromUser.Address == Address(
                    temp_pubkey):
                temp_validating_list_item.fromUser = temp_pubkey

    # Syncs new sorted list to block.validating_list
    block.validating_list = temp_validating_list

    save_accounts(temp_accounts)
Esempio n. 2
0
def CalculateHash(block):
    """
    Calculates and returns the hash of the block.
    """

    # Transaction
    tx_list = []
    dprint(len(block.validating_list))
    for element in block.validating_list[:]:
        dprint(element)
        tx_list.append(element.signature)
    if len(tx_list) != 0:
        tx_hash = MerkleTree(tx_list).getRootHash()
    else:
        tx_hash = "0"
        
    # Account
    ac_list = []
    for element in GetAccounts()[:]:
        ac_list.append(element.Address)
    dprint(ac_list)
    ac_hash = MerkleTree(ac_list).getRootHash()

    # Other elements
    main_list = []
    main_list.append(block.previous_hash)
    main_list.append(str(block.sequance_number))
    main_list.append(ac_hash)
    main_list.append(tx_hash)
    main_list.append(str(block.default_transaction_fee))
    main_list.append(str(block.default_increase_of_fee))
    main_list.append(str(block.default_optimum_transaction_number))

    # Calculating and returning
    return MerkleTree(main_list).getRootHash()
Esempio n. 3
0
def GetSequanceNumber(user, block):
    user = Address(user)
    sequance_number = 0
    for Accounts in GetAccounts():

        if Accounts.Address == user:

            sequance_number = Accounts.sequance_number

            for trans in block.pendingTransaction + block.validating_list:
                if user == trans.fromUser:
                    sequance_number += 1

            return sequance_number
    return sequance_number
Esempio n. 4
0
def saveBlockstoBlockchainDB(block):
    """
    Adds the block to the blockchain database
    at BLOCKS_PATH.
    """


        
    os.chdir(get_config()["main_folder"])

    with open(BLOCKS_PATH+str(block.sequance_number)+".block", 'wb') as block_file:
        pickle.dump(block, block_file, protocol=2)
        
    with open(BLOCKS_PATH+str(block.sequance_number)+".accounts", 'wb') as block_file:
        pickle.dump(GetAccounts(), block_file, protocol=2)
Esempio n. 5
0
def GetBalance(user, block):
    balance = -GetBlock().minumum_transfer_amount
    user = "".join([
        l.strip() for l in user.splitlines() if l and not l.startswith("-----")
    ])
    user = Address(user)
    for Accounts in GetAccounts():

        if Accounts.Address == user:
            balance += Accounts.balance
            break

    for trans in block.pendingTransaction + block.validating_list:
        if user == trans.fromUser:
            balance -= trans.amount + trans.transaction_fee
    return balance
def saveBlockstoBlockchainDB(block):
    """
    Adds the block to the blockchain database
    at BLOCKS_PATH.
    """

    our_tx = False
    my_public_key = "".join([
        l.strip() for l in Wallet_Import(-1, 0).splitlines()
        if l and not l.startswith("-----")
    ])
    my_address = Wallet_Import(-1, 3)
    for validated_transaction in block.validating_list:
        if validated_transaction.fromUser == my_public_key or validated_transaction.toUser == my_address:
            our_tx = True

    # If the block is our transaction, then add it to the blockchain database.
    if our_tx:
        #
        with open(BLOCKS_PATH + str(block.sequance_number) + ".block",
                  "wb") as block_file:
            pickle.dump(block, block_file, protocol=2)

        with open(BLOCKS_PATH + str(block.sequance_number) + ".accounts",
                  "wb") as block_file:
            pickle.dump(GetAccounts(), block_file, protocol=2)

        with open(BLOCKS_PATH + str(block.sequance_number) + ".accountspart",
                  "wb") as block_file:
            pickle.dump(GetAccounts_part(), block_file, protocol=2)

        with open(BLOCKS_PATH + str(block.sequance_number) + ".blockshash",
                  "wb") as block_file:
            pickle.dump(GetBlockshash(), block_file, protocol=2)

        with open(BLOCKS_PATH + str(block.sequance_number) + ".blockshashpart",
                  "wb") as block_file:
            pickle.dump(GetBlockshash_part(), block_file, protocol=2)
def CalculateHash(block):
    """
    Calculates and returns the hash of the block.
    """

    # Transaction
    tx_list = []
    dprint(len(block.validating_list))
    for element in block.validating_list[:]:
        dprint(element)
        tx_list.append(element.signature)
    if len(tx_list) != 0:
        tx_hash = MerkleTree(tx_list).getRootHash()
    else:
        tx_hash = "0"

    part_amount = 100000

    # Blocks Hash
    blocks_hash_list = []

    part_of_blocks_hash = GetBlockshash_part()
    the_blocks_hash = GetBlockshash()

    if not len(the_blocks_hash) - (len(part_of_blocks_hash) *
                                   part_amount) == part_amount:
        for will_added_blocks_hash in the_blocks_hash[(
                len(part_of_blocks_hash) * part_amount):]:
            blocks_hash_list.append(will_added_blocks_hash)
    else:
        part_of_blocks_hash.append(
            MerkleTree(the_blocks_hash[(len(part_of_blocks_hash) *
                                        part_amount):]).getRootHash())
        SaveBlockshash_part(part_of_blocks_hash)

    for part_of_blocks_hash_element in part_of_blocks_hash:
        blocks_hash_list.append(part_of_blocks_hash_element)

    blockshash_hash = MerkleTree(blocks_hash_list).getRootHash()

    # Account
    account_list = []

    part_of_account = GetAccounts_part()
    the_accounts = GetAccounts()

    if not len(the_accounts) - (len(part_of_account) *
                                part_amount) >= part_amount:
        for will_added_accounts in the_accounts[(len(part_of_account) *
                                                 part_amount):]:
            account_list.append(str(will_added_accounts.dump_json()))
    else:
        part_of_account.append(
            MerkleTree(the_accounts[(len(part_of_account) *
                                     part_amount):]).getRootHash())
        save_accounts(part_of_account)

    for part_of_account_element in part_of_account:
        account_list.append(part_of_account_element)

    for edited_account in block.edited_accounts:
        account_list.append(str(edited_account.dump_json()))

    block.edited_accounts.clear()

    ac_hash = MerkleTree(account_list).getRootHash()

    # Other elements
    main_list = []
    main_list.append(block.previous_hash)
    main_list.append(str(block.sequance_number))
    main_list.append(blockshash_hash)
    main_list.append(ac_hash)
    main_list.append(tx_hash)
    main_list.append(str(block.default_transaction_fee))
    main_list.append(str(block.default_increase_of_fee))
    main_list.append(str(block.default_optimum_transaction_number))

    # Calculating and returning
    return MerkleTree(main_list).getRootHash()