Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

The following files have been given to you: 1. A C++ header file (batcoin.h) declaring the Batcoin BlockHeader class and bathash function. 2. A C++

The following files have been given to you: 1. A C++ header file (batcoin.h) declaring the Batcoin BlockHeader class and bathash function. 2. A C++ header file (batcoin.cpp) implementing the Batcoin BlockHeader class and bathash function. 3. A C++ header file (mine.h) declaring the mine function. 4. A C++ source file (main.cpp) containing a main function with tests.

Create a new C++ source file named mine.cpp that implements the mine function declared in mine.h, so that mine.cpp and the provided files compile into a program that runs with no failed tests. You cannot add any more libraries to the files. Submit the source file "mine.cpp".

(mine.h):

#ifndef MINE_H #define MINE_H #include "batcoin.h" // Returns a BlockHeader whose prev bytes are equal to the // output of bathash(prev) and that hashes to a set of bytes // where the first difficulty of them are equal to 0. BlockHeader mine(BlockHeader prev, int difficulty); #endif 
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
(main.cpp)
#include  #include "batcoin.h" #include "mine.h" using namespace std; inline void _test(const char* expression, const char* file, int line) { cerr << "test(" << expression << ") failed in file " << file; cerr << ", line " << line << "." << endl; abort(); } #define test(EXPRESSION) ((EXPRESSION) ? (void)0 : _test(#EXPRESSION, __FILE__, __LINE__)) void test_mining(BlockHeader genesis, int difficulty, int coins) { BlockHeader prev, cur; unsigned char result[8]; // Mine some BatCoins! prev = genesis; for (int i = 1; i <= coins; ++i) { // Uses your mining algorithm to mine a valid next coin, // i.e. find a valid next block. cur = mine(prev, difficulty); // Check that newly mined block has correct prev bytes. // Namely, those in the output of bathash(prev). bathash(prev, result); for (int c = 0; c < 8; ++c) test(result[c] == cur.prev[c]); // Check that newly mined block has a hash value // with the first difficulty bytes equal to 0. bathash(cur, result); for (int j = 0; j < difficulty; ++j) test(result[j] == 0); // Uncomment the next line to print mined coins //cout << " " << cur.to_string() << endl; prev = cur; } } int main() { // See https://en.bitcoin.it/wiki/Genesis_block // for more information about first block headers. BlockHeader genesis; genesis.prev[0] = 's'; genesis.prev[1] = 'e'; genesis.prev[2] = 'c'; genesis.prev[3] = 'o'; genesis.prev[4] = 'n'; genesis.prev[5] = 'd'; genesis.prev[6] = 'b'; genesis.prev[7] = 'a'; genesis.nonce[0] = 't'; genesis.nonce[1] = 'b'; genesis.nonce[2] = 'a'; genesis.nonce[3] = 'i'; genesis.nonce[4] = 'l'; genesis.nonce[5] = 'o'; genesis.nonce[6] = 'u'; genesis.nonce[7] = 't'; // Mine 10000 coins at difficulty 1 bathash(genesis, genesis.prev); test_mining(genesis, 1, 10000); // Mine 100 coins at difficulty 2 bathash(genesis, genesis.prev); test_mining(genesis, 2, 100); // Mine 1 coin at difficulty 3 bathash(genesis, genesis.prev); test_mining(genesis, 3, 1); cout << "Assignment complete." << endl; } -------------------------------------------------------------------------------------------------------------------------------- 

(batcoin.h):

 #ifndef BATCOIN_H #define BATCOIN_H #include  using namespace std; class BlockHeader { public: BlockHeader(); string to_string(); // These store 8 bytes of information each, // storing each byte as an unsigned char. unsigned char prev[8]; // Previous BlockHeader hash value unsigned char nonce[8]; // The nonce for this BlockHeader }; // The hash function used in the Batcoin protocol. void bathash(BlockHeader b, unsigned char* result); #endif 

--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

(batcoin.cpp):

 #include  #include "batcoin.h" using namespace std; BlockHeader :: BlockHeader() { for (int i = 0; i < 8; ++i) prev[i] = nonce[i] = 0; } string BlockHeader :: to_string() { ostringstream oss; oss << "0x" << hex; for (int i = 0; i < 8; ++i) { unsigned int v = prev[i]; if (v < 16) oss << "0"; oss << v; } oss << " 0x"; for (int i = 0; i < 8; ++i) { unsigned int v = nonce[i]; if (v < 16) oss << "0"; oss << v; } return oss.str(); } void bathash(BlockHeader b, unsigned char* result) { // Sum of all bytes unsigned char sum = 0; for (int i = 0; i < 8; ++i) sum += b.prev[i] + b.nonce[i]; // Set each byte of the result is the XOR of two bytes // (one from prev, one from nonce) // plus sum bits shifted right by 1 // plus 1 for (int i = 0; i < 8; ++i) result[i] = (b.prev[i] ^ b.nonce[7-i]) + (sum >> 1) + 1; // "Rotate" the bytes, shifting each left by one byte and // setting the last byte equal to a function of the sum for (int i = 0; i < 7; ++i) result[i] = result[i+1]; result[7] = 256 - sum; } 

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions