חזור לתיקייה: גליון 1

BlockChain

חיפוש בתיקיה
Ctrl K
לחץ כאן או הקש Ctrl K כדי לחפש בכל התיקיה

    BlockChain

    02340124

    מבוא לתכנות מערכות

    רשימות הרצאה לקריאה בשבת

    BlockChain

    הופק מתוך תקיית התואר של שקד

    uni-folder.vercel.app

    blockchain.cpp

    הערה (קובץ קוד)

    קובץ זה הוא קובץ קוד מסוג CPP.

    #include "BlockChain.h"
    
    #include <iostream>
    
    #include "Transaction.h"
    
    BlockChain BlockChainInit()
    {
        Transaction empty = {
            0,
            "Empty1",
            "Empty2"
        };
        Block emptyBlock = {
            empty,
            "00000000000"
        };
        BlockChain newBlock = {
            emptyBlock,
            nullptr
        };
    
        return newBlock;
    }
    
    void BlockChainDestroy(BlockChain &blockChain)
    {
        BlockChain *head = blockChain.next;
        while (head != nullptr)
        {
            const BlockChain *temp = head;
            head = head->next;
            delete temp;
        }
        blockChain.next = nullptr;
    }
    
    int BlockChainGetSize(const BlockChain &blockChain)
    {
        const BlockChain *ptr = &blockChain;
        int size = 0;
    
        while (ptr->next != nullptr)
        {
            size++;
            ptr = ptr->next;
        }
    
        return size;
    }
    
    int BlockChainPersonalBalance(const BlockChain &blockChain, const string &name)
    {
        int count = 0;
        const BlockChain *ptr = &blockChain;
        while (ptr->next != nullptr)
        {
            if (ptr->current.transaction.sender == name)
                count -= ptr->current.transaction.value;
            if (ptr->current.transaction.receiver == name)
                count += ptr->current.transaction.value;
            ptr = ptr->next;
        }
        return count;
    }
    
    void BlockChainAppendTransaction(
        BlockChain &blockChain,
        unsigned int value,
        const string &sender,
        const string &receiver,
        const string &timestamp
    )
    {
        Transaction newTran = {value, sender, receiver};
        BlockChainAppendTransaction(blockChain, newTran, timestamp);
    }
    
    void BlockChainAppendTransaction(
        BlockChain &blockChain,
        const Transaction &transaction,
        const string &timestamp
    )
    {
        BlockChain *oldChain = new BlockChain{blockChain.current, blockChain.next};
        blockChain.current = {transaction, timestamp};
        blockChain.next = oldChain;
    }
    
    void BlockChainLoad(ifstream &file, BlockChain &chain) {
        // TEMPLATE OF EACH LINE: <sender> <receiver> <value> <timestamp>
        if (file.eof())
            return;
    
        string sender, receiver, time;
        int value;
        if (file >> sender >> receiver >> value >> time) {
            BlockChainLoad(file, chain);
            BlockChainAppendTransaction(chain, value, sender, receiver, time);
        }
        else
            BlockChainLoad(file, chain);
    }
    
    BlockChain BlockChainLoad(ifstream &file)
    {
        BlockChain block_chain = BlockChainInit();
        BlockChainLoad(file, block_chain);
        return block_chain;
    }
    
    void BlockChainDump(const BlockChain &blockChain, ofstream &file)
    {
        file << "BlockChain Info:" << std::endl;
        int lineNum = 1;
        const BlockChain *ptr = &blockChain;
        while (ptr->next != nullptr)
        {
            file << lineNum << "." << std::endl;
            TransactionDumpInfo(ptr->current.transaction, file);
            file << "Transaction timestamp: " << ptr->current.timestamp << std::endl;
            ptr = ptr->next;
            lineNum++;
        }
    }
    
    void BlockChainDumpHashed(const BlockChain &blockChain, ofstream &file)
    {
        const BlockChain *ptr = &blockChain;
        while (ptr->next != nullptr)
        {
            file << TransactionHashedMessage(ptr->current.transaction);
            if (ptr->next->next != nullptr)
                file << std::endl;
            ptr = ptr->next;
        }
    }
    
    bool BlockChainVerifyFile(const BlockChain &blockChain, std::ifstream &file)
    {
        const BlockChain *ptr = &blockChain;
        while (ptr->next != nullptr && !file.eof())
        {
            string line;
            std::getline(file, line);
            if (!TransactionVerifyHashedMessage(ptr->current.transaction, line))
                return false;
            ptr = ptr->next;
        }
        if ((ptr->next != nullptr) || !file.eof())
            return false;
        return true;
    }
    
    void BlockChainCompress(BlockChain &blockChain)
    {
        BlockChain *ptr = &blockChain, *comparePtr;
        while (ptr->next != nullptr && ptr->next->next != nullptr)
        {
            comparePtr = ptr;
            unsigned int count = 0;
            while (comparePtr->next != nullptr && comparePtr->next->next != nullptr &&
                   ptr->current.transaction.receiver == comparePtr->next->current.transaction.receiver &&
                   ptr->current.transaction.sender == comparePtr->next->current.transaction.sender)
            {
                count += comparePtr->next->current.transaction.value;
                comparePtr = comparePtr->next;
            }
            if (comparePtr != ptr)
            {
                ptr->current.transaction.value += count;
    
                BlockChain *until = comparePtr->next;
                while (ptr->next != until)
                {
                    BlockChain *temp = ptr->next->next;
                    delete ptr->next;
                    ptr->next = temp;
                }
            }
        }
    }
    
    void BlockChainTransform(BlockChain &blockChain, updateFunction function)
    {
        BlockChain *ptr = &blockChain;
        while (ptr->next != nullptr)
        {
            ptr->current.transaction.value = function(ptr->current.transaction.value);
            ptr = ptr->next;
        }
    }