Generic-BlockChain-container-cpp

namespace ra :: BlockChain

Aim:

To create an iterable blockchain, which could work with an extendable transaction class. Here the link GitHub repo!

Blockchain in Brief:

Scope of this Project:

Plan of Attack:

Setup

Lets Setup the extra files!

RSA: Library will be used for signing and verification of transactions.

Create a includes folder and add files from Github


not_implemented_exception: The exception that is thrown when a requested method or operation is not implemented.
class not_implemented_exception : public logic_error
{
public:
    not_implemented_exception(
        string function = __builtin_FUNCTION()
    ) : logic_error("`" + function + "` not implemented!"){};
};

base_transaction<EncryptionAlgo>

Here are the required things:

Every Function logic will be same, which is to throw not_implemented_exception, for example:

operator string() const {throw not_implemented_exception();}

custom_transaction : public base_transaction<EncryptionAlgo>

Private Members:

Public Members:

custom_transaction(from_adrr, to_addr, amount){
    from_address = from_addr;
    to_address = to_addr;
    transfer_amount = amount;
    timestamp = chrono::steady_clock::now();
    signature = 0;
}
# Concatenation of all stringified private variables (except Signature)
# to_string(...) in C++
return (str(from_addr) + str(to_addr) + str(amount) + str(timestamp))
operator string() const
{
  string from_address_str = "\nfrom_address: " + to_string(from_address);
  string to_address_str = "\nto_address: " + to_string(to_address);
  string transfer_amount_str = "\ntransfer_amount: " + to_string(transfer_amount);
  string signature_str = "\nsignature: " + to_string(signature);
  return from_address_str + to_address_str + transfer_amount_str + signature_str;
}
friend ostream &operator<<(ostream &out, custom_transaction const &temp)
{
  // Using above string operator
  return out << string(temp);
}
bool operator<(const custom_transaction &other) const
{
  return generate_hash_input() < other.generate_hash_input();
}
float get_balance(addr) const
{
  if (addr == to_address)
    return transfer_amount;
  if (addr == from_address)
    return -transfer_amount;
  return 0;
}

Here are some Pointers regarding signing

bool is_transaction_valid(bool (*verfication_function)(string, size_t, address)) const
{
  // reward, since reward is given by admin, there is no key pair
  if (from_address == 0)
    return true;
  return verfication_function(generate_hash_input(), signature, from_address);
}

block <Transaction, HashFunctionClass>

NOTES:

# Here `nonce` is found via brute force
# until HASH_PATTERNis matched.
while(1):
    hash_input = str(...) + str(nonce)
    if re.match(hash_function(hash_input), HASH_PATTERN):
        return nonce
    nonce += 1

block_chain <Transaction, HashFunctionClass>

Well as the name states it is a chain of blocks, So Naturally, It will contain the same Template Arguments as Block

Private Members:

Public Members:

iterator operator--(int)
{
    iterator _temp = *this;
    m_block_ptr--;
    return _temp;
}
const iterator begin() { return chain.begin(); }
const iterator end() { return chain.end(); }
block_type &back() { return chain.back(); }
block_type &front() { return chain.front(); }
int size() { return chain.size(); }