Skip to content

Commit

Permalink
Made block storage safer.
Browse files Browse the repository at this point in the history
  • Loading branch information
HSCorp99906 committed Dec 10, 2021
1 parent 76662f7 commit e81984f
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 0 deletions.
6 changes: 6 additions & 0 deletions core/include/Blockchain.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include <fstream>
#include <sstream>


#define REWARD_AMOUNT 150


Expand All @@ -22,6 +23,9 @@ class Blockchain {
uint32_t height;
std::string lastHash;

bool tempStorageActive;
Block* tempStorage;

void add_block(Block* blk);

public:
Expand All @@ -33,6 +37,8 @@ class Blockchain {
void increment_height();
void add_pending_transactions(std::vector<Transaction> t);

void delete_temp_storage();

Block get_prev_block();
};

Expand Down
4 changes: 4 additions & 0 deletions core/info/transactions/transaction-0.log
Original file line number Diff line number Diff line change
Expand Up @@ -70,3 +70,7 @@ Amount: 10.2
To Addr: ca978112ca1bbdcafac231b39a23dc4da786eff8147c4e72b9807785afee48bb
From Addr: 3e23e8160039594a33894f6564e1b1348bbd7a0088d42c4acb73eeaed59c009d
Hash: 76fc85b757be4465198ac87c61c7d3227e11687852b67f66d42f368b4bf2687c
Amount: 10.2
To Addr: ca978112ca1bbdcafac231b39a23dc4da786eff8147c4e72b9807785afee48bb
From Addr: 3e23e8160039594a33894f6564e1b1348bbd7a0088d42c4acb73eeaed59c009d
Hash: 90c698cfb8ac55d139c1728461856a2d9ca776f35736eb41fbc48ff5b9b0c56f
4 changes: 4 additions & 0 deletions core/info/transactions/transaction-1.log
Original file line number Diff line number Diff line change
Expand Up @@ -66,3 +66,7 @@ Amount: 10.2
To Addr: ca978112ca1bbdcafac231b39a23dc4da786eff8147c4e72b9807785afee48bb
From Addr: 3e23e8160039594a33894f6564e1b1348bbd7a0088d42c4acb73eeaed59c009d
Hash: 5dde468777439f3635596c1c5716c4078318570ae7eef447a44107554b8f6123
Amount: 10.2
To Addr: ca978112ca1bbdcafac231b39a23dc4da786eff8147c4e72b9807785afee48bb
From Addr: 3e23e8160039594a33894f6564e1b1348bbd7a0088d42c4acb73eeaed59c009d
Hash: 0cea6ebfeba3e37f1a1ef95fe3ed5eb6c019b938f4420832e00ba13286792e4f
13 changes: 13 additions & 0 deletions core/src/Blockchain.cpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
#include "../include/Blockchain.hpp"


Blockchain::Blockchain() {
this -> current_node = new BlockNode;
this -> head_node = this -> current_node;
this -> temp_node = this -> current_node;
this -> current_node -> next = NULL;
this -> tempStorageActive = false;

std::ifstream blockno;
blockno.open("../info/blockinfo/blockno");
Expand Down Expand Up @@ -57,6 +59,14 @@ void Blockchain::add_pending_transactions(std::vector<Transaction> t) {
}


void Blockchain::delete_temp_storage() {
if (tempStorageActive) {
std::cout << "\n\nPlease be careful abnormally halting process." << std::endl;
delete this -> tempStorage;
}
}


void Blockchain::increment_height() {
std::ofstream of;
of.open("../info/blockinfo/blockno");
Expand All @@ -66,7 +76,10 @@ void Blockchain::increment_height() {


void Blockchain::mine_pending_transactions() {
this -> tempStorageActive = true;
Block* newBlock = new Block(this -> pending_transactions, this -> lastHash, this -> height);
this -> tempStorage = newBlock;

newBlock -> mine(4); // 2 for now.
this -> add_block(newBlock);
std::cout << "********** BLOCK ADDED TO BLOCKCHAIN **********" << std::endl;
Expand Down
1 change: 1 addition & 0 deletions core/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
Blockchain* kcBlockchain;

void die(int dummy) {
kcBlockchain -> delete_temp_storage();
delete kcBlockchain;
exit(0);
}
Expand Down

0 comments on commit e81984f

Please sign in to comment.