Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

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.

The computationally challenging task used by Batcoin is searching for a value, called a nonce. The nonce must have the property that when a certain hash function is applied to the nonce, the result begins with several zeroes. The minimum number of zeroes is specified by the difficulty parameter of the currency, which changes over time for market reasons. The hash function used by Batcoin is difficult to predict, so there are (probably) no shortcuts to finding a nonce with the desired property. Thus finding such a nonce (i.e. mining a coin) consists of brute force trying of many different nonces until one is found.

Please do not change/add to files or add libraries to main, just use what is given to create mine.cpp.

Instructions are given in comments in each file.

///////////////////////////////////////////////////////////////////////////////////////////////////////

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 << " prev: "; for (int i = 0; i < 8; ++i) { unsigned int v = prev[i]; if (v < 100) oss << " "; if (v < 10) oss << " "; oss << v; if (i < 7) oss << " "; } oss << endl; oss << "nonce: "; for (int i = 0; i < 8; ++i) { unsigned int v = nonce[i]; if (v < 100) oss << " "; if (v < 10) oss << " "; oss << v; if (i < 7) oss << " "; } oss << endl; 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; }
//////////////////////////////////////////////////////
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 

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

Professional Visual Basic 6 Databases

Authors: Charles Williams

1st Edition

1861002025, 978-1861002020

More Books

Students also viewed these Databases questions