Categories
Mastering Development

Object with list of structs loses items when added to another list. How does this work in memory?

I’m trying to recreate a super simple blockchain example in c++, however my transactional data disappears. I’m quite new to c++ and would like to know what is happening on the memory side here.

First a blockchain is made
Blockchain blockchain = Blockchain();

then, I create a instance of a struct transaction called transaction1

    transaction transaction1;
    transaction1.id = "13454251";
    transaction1.amount = 39.233;
    transaction1.fromHash = "kajnasdaskln";
    transaction1.toHash = "ajsdjkabsdj";
    transaction1.timestamp = "29/05/2020";

then I add it to a list of transactions transactionBlock1:

    list<transaction> transactionBlock1;
    transactionBlock1.push_back(transaction1);

This is used to create a new block
in main.cpp:

Block block1 = Block(1, "29/05/2020", transactionBlock1);

in block.cpp:

Block::Block(int index, string timestamp, list<transaction> transactions, string previousHash)
    : index{ index },
    timestamp{ timestamp },
    transactions{ transactions },
    previousHash{ previousHash } {
        hash = calculateHash();
    }

After it is added to the blockchain:
in main.cpp:

blockchain.addBlock(block1);

in blockchain.cpp:

    void Blockchain::addBlock(Block newBlock) {
    newBlock.setPreviousHash(getLatestBlock().getHash());
    newBlock.setHash(newBlock.calculateHash());
    blockChain.push_back(newBlock);
}

Now the the resulting blockchain after addition has the extra block but no transaction in the transactions list. It is of length 0.

Why is this and what is happening in memory?

The rest of my code :

main.cpp:

//
//

#include <iostream>
#include "blockchain.hpp"

using namespace std;

int main(int argc, const char * argv[]) {
    // insert code here...
    std::cout << "Hello, World!\n";

    Blockchain blockchain = Blockchain();

    transaction transaction1;
    transaction1.id = "13454251";
    transaction1.amount = 39.233;
    transaction1.fromHash = "kajnasdaskln";
    transaction1.toHash = "ajsdjkabsdj";
    transaction1.timestamp = "29/05/2020";

    list<transaction> transactionBlock1;
    transactionBlock1.push_back(transaction1);
    Block block1 = Block(1, "29/05/2020", transactionBlock1);

    blockchain.addBlock(block1);

    transaction transaction2;
    transaction2.id = "42o41043";
    transaction2.amount = 11.413;
    transaction2.fromHash = "jklnkadflnfa";
    transaction2.toHash = "jaskdndsladmsa;";
    transaction2.timestamp = "30/06/2020";

    list<transaction> transactionBlock2;
    transactionBlock1.push_back(transaction2);

    blockchain.addBlock(Block(2, "30/06/2020", transactionBlock2));

    return 0;
}

blockchain.hpp

#ifndef blockchain_hpp
#define blockchain_hpp

#include <stdio.h>
#include "block.hpp"
#include <list>

class Blockchain {
    private:

    list<Block> blockChain;

    protected:
    public:

    Blockchain();
    ~Blockchain();

    Block createGenesisBlock();
    Block getLatestBlock();
    void addBlock(Block newBlock);
};

#endif /* blockchain_hpp */

block.hpp


#ifndef block_hpp
#define block_hpp

#include <stdio.h>
#include <vector>
#include <string>
#include <list>

using namespace std;

struct transaction {
    string id;
    string fromHash;
    string toHash;
    double amount;
    string timestamp;
};

class Block {


    private:

    int index;
    string timestamp;
    list<transaction> transactions;
    string hash;
    string previousHash;

    protected:

    public:

    // Constructors
    // Block();
    Block(int index, string timestamp, list<transaction> transactions, string previousHash = "");
    // Deconstructor
    ~Block();

    string calculateHash();
    void mineBlock();
    string hashifyTransactions();
    string hashFoo(string input);

    void setHash(string hashCode);
    string getHash();
    void setPreviousHash(string hashCode);
    string getPreviousHash();

};

#endif /* block_hpp */

blockchain.cpp

#include "blockchain.hpp"

Blockchain::Blockchain() {
    blockChain.push_back(Blockchain::createGenesisBlock());
}

Blockchain::~Blockchain() { }

Block Blockchain::createGenesisBlock() {
    return Block(0, "28/04/2020", list<transaction>(), "0");
}

Block Blockchain::getLatestBlock() {
    return blockChain.back();
}

void Blockchain::addBlock(Block newBlock) {
    newBlock.setPreviousHash(getLatestBlock().getHash());
    newBlock.setHash(newBlock.calculateHash());
    blockChain.push_back(newBlock);
}


block.cpp


#include "block.hpp"

// Block::Block() { }

Block::Block(int index, string timestamp, list<transaction> transactions, string previousHash)
    : index{ index },
    timestamp{ timestamp },
    transactions{ transactions },
    previousHash{ previousHash } {
        hash = calculateHash();
    }

Block::~Block() { }


string Block::calculateHash() {
    return hashFoo(to_string(index) + timestamp + hashifyTransactions() + previousHash);
}

void Block::mineBlock() {

}

string Block::hashifyTransactions() {
    list<transaction> :: iterator it;
    string ret;
    for (it = transactions.begin(); it != transactions.end(); it++) {
        ret.append((*it).id);
    }
    return ret;
}

string Block::hashFoo(string input) {
    return input;
}

void Block::setHash(string hashCode) {
    hash = hashCode;
}

string Block::getHash() {
    return hash;
}

void Block::setPreviousHash(string hashCode) {
    previousHash = hashCode;
}

string Block::getPreviousHash() {
    return previousHash;
}

Leave a Reply

Your email address will not be published. Required fields are marked *