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

BlockChain

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

    BlockChain

    02340124

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

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

    BlockChain

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

    uni-folder.vercel.app

    blockchain.h

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

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

    #pragma once
    
    #include <string>
    #include <fstream>
    
    #include "Transaction.h"
    
    using std::string;
    using std::ifstream;
    using std::ofstream;
    
    
    typedef unsigned int (*updateFunction)(unsigned int);
    
    /**
    *
     * BlockChain - Defining the new BlockChain Type
     *
    */
    struct Block{
     Transaction transaction;
     string timestamp;
    };
    
    struct BlockChain
    {
        Block current;
        BlockChain *next;
    };
    
    /**
     * @return newly initialized empty BlockChain Object
    */
    BlockChain BlockChainInit();
    
    /**
     * BlockChainDestory - Destroys an existing BlockChain and deallocates all of its memory, after calling this method, blockChain can't be used/accessed again.
    */
    void BlockChainDestroy(BlockChain &blockChain);
    
    
    /**
     * BlockChainGetSize - returns the number of Blocks in the BlockChain
     *
     * @param blockChain - BlockChain to measure
     *
     * @return Number of Blocks in the BlockChain
    */
    int BlockChainGetSize(const BlockChain &blockChain);
    
    
    /**
     * BlockChainPersonalBalance - returns the balance of a given person, relative to a given BlockChain
     *
     * @param blockChain - BlockChain to calculate the balance from
     * @param name - Name of the person to calculate the balance for
     *
     * @return Balance of the person
    */
    int BlockChainPersonalBalance(const BlockChain &blockChain, const string &name);
    
    
    /**
     * BlockChainAppendTransaction - creates and appends a new transaction to the BlockChain
     *
     * @param blockChain BlockChain to append the transaction to
     * @param value Value of the transaction
     * @param sender Name of the sender
     * @param receiver Name of the receiver
     * @param timestamp String that holds the time the transaction was made
    */
    void BlockChainAppendTransaction(
        BlockChain &blockChain,
        unsigned int value,
        const string &sender,
        const string &receiver,
        const string &timestamp
    );
    
    
    /**
     * BlockChainAppendTransaction - appends a copy of a given transaction to the BlockChain
     *
     * @param blockChain BlockChain to append the transaction to
     * @param transaction Transaction we want to append
     * @param timestamp String that holds the time the transaction was made
    */
    void BlockChainAppendTransaction(
        BlockChain &blockChain,
        const Transaction &transaction,
        const string &timestamp
    );
    
    
    /**
     * BlockChainLoad - Reads data from a file and creates a new block chain
     *
     * @param file Data file to read from
     *
     * @return BlockChain created from the file
     *
    */
    BlockChain BlockChainLoad(ifstream &file);
    
    
    /**
     * BlockChainDump - Prints the data of all transactions in the BlockChain to a given file
     *
     * Data will be printed in the following format:
     *
     * BlockChain info:
     * <n>.
     * Sender Name: <name>
     * Receiver Name: <name>
     * Transaction Value: <value>
     * Transaction Timestamp: <time>
     *
     * @param blockChain BlockChain to print
     * @param file File to print to
     *
    */
    void BlockChainDump(const BlockChain &blockChain, ofstream &file);
    
    
    /**
     * BlockChainDumpHashed - Prints the *hashed data* of all transactions in the BlockChain to a given file
     *
     * Data will be printed in the following format:
     * <hashed message>
     * <hashed message>
     * ...
     * <hashed message>
     *
     * @param blockChain BlockChain to print
     * @param file File to print to
     *
    */
    void BlockChainDumpHashed(const BlockChain &blockChain, ofstream &file);
    
    
    /**
     * BlockChainVerifyFile - verifies that the file contains correct hashed messages of the given BlockChain
     *
     * Input file is expected to contain data in the following format:
     * <hashed message>
     * <hashed message>
     * ...
     * <hashed message>
     *
     * @param blockChain BlockChain to verify
     * @param file File to read from
     *
     * @return true if the file is valid, false otherwise
    */
    bool BlockChainVerifyFile(const BlockChain &blockChain, std::ifstream &file);
    
    
    /**
     * BlockChainCompress - Compresses the given block chain based on the transaction's data.
     * All consecutive blocks with the same sender and receiver will be compressed to one Block.
     *
     * @param blockChain BlockChain to compress
    */
    void BlockChainCompress(BlockChain &blockChain);
    
    
    /**
     * BlockChainTransform - Update the values of each transaction in the BlockChain
     *
     * @param blockChain BlockChain to update
     * @param function a pointer to a transform function
    */
    void BlockChainTransform(BlockChain &blockChain, updateFunction function);