Skip to content

Commit

Permalink
Cleaned the code
Browse files Browse the repository at this point in the history
  • Loading branch information
ayushj05 committed Apr 30, 2024
1 parent b7b16b1 commit 37423f6
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 196 deletions.
4 changes: 4 additions & 0 deletions src/block.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ using namespace std;

extern string target;

// Function to get the merkle root of the transactions in txns_included
string getMerkleRoot(vector<string> &txns_included) {
queue<string> hashQ;
// Push all txids into the queue in reverse order because coinbase txn is at the end
Expand Down Expand Up @@ -46,6 +47,7 @@ string getMerkleRoot(vector<string> &txns_included) {
return hashQ.front();
}

// Generate coinbase transaction from reward and wTXID commitment
string genCoinbaseTxn (int64_t reward, string wTXID_commitment) {
string coinbase_txn = "";

Expand Down Expand Up @@ -75,6 +77,7 @@ string genCoinbaseTxn (int64_t reward, string wTXID_commitment) {
return coinbase_txn;
}

// Generate block header from txns_included
string genBlockHeader (vector<string> &txns_included) {
string block_header = "";
block_header += int2bin(32); // Version
Expand All @@ -91,6 +94,7 @@ string genBlockHeader (vector<string> &txns_included) {
return block_header;
}

// Calculate the nonce for the block
string calcNonce (string blockHeader) {
string nonce = "";
for (int i = 0; i < 4; i++)
Expand Down
166 changes: 18 additions & 148 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,15 @@ set<string> inputs_used;
string target = string(2, 0) + string(2, 255) + string(28, 0);

void mine() {
vector<pair<uint64_t, Json::Value>> transactions; // Vector to store all transactions
// Vector to store all transactions in the mempool
vector<pair<uint64_t, Json::Value>> transactions;

for (auto &entry : filesystem::directory_iterator("mempool")) {
ifstream txn_file(entry.path(), ifstream::binary);
Json::Value txn;
txn_file >> txn;

// Check whether sum of inputs >= sum of outputs
uint64_t sum_input = 0, sum_output = 0;
for (auto &inp : txn["vin"])
sum_input += inp["prevout"]["value"].asUInt64();
Expand All @@ -29,11 +31,15 @@ void mine() {
continue;
}

// Serialize the transaction
string ser_txn = serialize_txn(txn);

// Push the transaction to the vector
// The first element of the pair is the fee to size ratio
transactions.push_back({(sum_input - sum_output)/ser_txn.size(), txn});
}

// Sort the transactions in decreasing order of fee to size ratio
sort(transactions.begin(), transactions.end(), greater<pair<uint64_t, Json::Value>>());

vector<string> blockTxns;
Expand All @@ -43,6 +49,7 @@ void mine() {
uint32_t block_size = 80;

for (auto &txn : transactions) {
// Look for ony those transaction whose inputs are of type p2pkh, p2sh, v0_p2wpkh
bool flag = false;
for (auto &inp : txn.second["vin"]) {
if (inp["prevout"]["scriptpubkey_type"] != "v0_p2wpkh" && inp["prevout"]["scriptpubkey_type"] != "p2pkh" && inp["prevout"]["scriptpubkey_type"] != "p2sh"){
Expand All @@ -54,6 +61,7 @@ void mine() {

string ser_txn = serialize_txn(txn.second);

// Check block size limit and whether the transaction is valid
if (block_size + ser_txn.size() < 1024*1024 && verify_txn(txn.second) >= 0) {
blockTxns.push_back(ser_txn);
wTXIDs.push_back(hash256(ser_wit(txn.second)));
Expand All @@ -66,42 +74,49 @@ void mine() {
for (string txn : blockTxns)
blockTxnIds.push_back(hash256(txn));


// Calculate wTXID commitment
wTXIDs.push_back(string(32, 0));
string wTXID_commitment = hash256(getMerkleRoot(wTXIDs) + string(32, 0));
string coinbaseTxn = genCoinbaseTxn(reward, wTXID_commitment);
block_size += coinbaseTxn.size();

// Calculate TXID of the coinbase transaction
string coinbaseTxnId = "";
char sha[SHA256_DIGEST_LENGTH];
SHA256((unsigned char*) coinbaseTxn.c_str(), coinbaseTxn.length(), (unsigned char*) sha);
SHA256((unsigned char*) sha, SHA256_DIGEST_LENGTH, (unsigned char*) sha);
for (int i = 0; i < SHA256_DIGEST_LENGTH; i++)
coinbaseTxnId.push_back(sha[i]);
// Add the coinbase txn to the list of txns to be included in the block
blockTxnIds.push_back(coinbaseTxnId);

// Generate the block
string block = "";
// Generate the block header
string blockHeader = genBlockHeader(blockTxnIds);
block += blockHeader;
string nonce = calcNonce(blockHeader);
block += nonce;
block = bstr2hexstr(block, block.length());
block.push_back('\n');

// Add the coinbase txn to the block
block += bstr2hexstr(coinbaseTxn, coinbaseTxn.length());
block.push_back('\n');

// Add all the transactions to the block in reverse order because coinbase txn is at the end
// Add all the TXIDs to the block in reverse order because coinbase txn is at the end
for (int i = blockTxnIds.size() - 1; i >= 0; i--) {
reverse(blockTxnIds[i].begin(), blockTxnIds[i].end());
block += bstr2hexstr(blockTxnIds[i], blockTxnIds[i].length());
block.push_back('\n');
}

// Write the block to output.txt
ofstream block_file("output.txt");
block_file << block;
block_file.close();

// Just some messages
cout << "Block mined successfully!" << endl;
cout << "Nonce = " << bstr2hexstr(nonce, nonce.length()) << endl;
cout << "Reward = " << reward << endl;
Expand All @@ -112,150 +127,5 @@ void mine() {
int main() {
mine();

// vector<Json::Value> transactions; // Vector to store all transactions

// // Iterate through all files in the mempool directory and read the transactions
// for (auto &entry : filesystem::directory_iterator("mempool")) {
// ifstream txn_file(entry.path(), ifstream::binary);

// std::ostringstream tmp;
// tmp << txn_file.rdbuf();
// std::string txn_str = tmp.str();

// Json::Value txn;
// Json::Reader reader;
// reader.parse(txn_str.c_str(), txn);

// bool check = true;
// for (auto &inp : txn["vin"]) {
// if (inp["prevout"]["scriptpubkey_type"] != "p2pkh"){
// check = false;
// break;
// }
// }
// if (check) cout << verify_txn(txn, txn_str) << endl;
// // transactions.push_back(txn);
// }


// vector<Json::Value> transactions; // Vector to store all transactions
// for (auto &entry : filesystem::directory_iterator("mempool")) {
// ifstream txn_file(entry.path(), ifstream::binary);
// Json::Value txn;
// txn_file >> txn;
// transactions.push_back(txn);
// }
// set<string> s;
// for (auto &txn : transactions) {
// for (auto &inp : txn["vin"]) {
// if (inp["prevout"]["scriptpubkey_type"] == "p2sh"){
// vector<string> ops = getOps(inp["inner_redeemscript_asm"].asString());
// if (identifyScriptType(ops) == "Invalid")
// cout << inp["inner_redeemscript_asm"].asString() << '\n' << endl;
// s.insert(identifyScriptType(ops));
// }
// }
// }

// for (auto &i : s) {
// cout << i << endl;
// }


// // Test for int2hex function defined in serialize.h
// cout << int2hex(uint32_t(UINT32_MAX - 255 + 97)) << endl; // Should print a in the beginning



// ifstream txn_file("mempool/ff907975dc0cfa299e908e5fba6df56c764866d9a9c22828824c28b8e4511320.json");
// // ifstream txn_file("mempool/fff53b0fda0ab690ddaa23c84536e0d364a736bb93137a76ebf5d78f57cdd32f.json");

// std::ostringstream tmp;
// tmp << txn_file.rdbuf();
// std::string txn_str = tmp.str();

// Json::Value txn;
// Json::Reader reader;
// reader.parse(txn_str.c_str(), txn);

// string ser_txn = sertialize_txn(txn);
// string ser_txn_hex = bstr2hexstr(ser_txn, ser_txn.length());

// char sha[SHA256_DIGEST_LENGTH];
// SHA256((unsigned char*) ser_txn.c_str(), ser_txn.length(), (unsigned char*) sha);
// SHA256((unsigned char*) sha, SHA256_DIGEST_LENGTH, (unsigned char*) sha);
// SHA256((unsigned char*) sha, SHA256_DIGEST_LENGTH, (unsigned char*) sha);
// // cout << "SHA256: " << bstr2hexstr(sha, SHA256_DIGEST_LENGTH) << endl;

// cout << verify_txn(txn) << endl;


// u_int64_t total_fees = 0;

// for (auto &entry : filesystem::directory_iterator("mempool")) {
// ifstream txn_file(entry.path(), ifstream::binary);
// Json::Value txn;
// txn_file >> txn;

// bool flag = false;
// for (auto &inp : txn["vin"]) {
// if (inp["prevout"]["scriptpubkey_type"] != "p2pkh" && inp["prevout"]["scriptpubkey_type"] != "v0_p2wpkh"){
// flag = true;
// break;
// }
// }
// if (flag) continue;

// total++;

// int64_t status = verify_txn(txn);
// if (status >= 0) {
// valid_txns++;
// total_fees += status;
// }
// else
// invalid_txns++;
// }
// cout << total << ' ' << valid_txns << ' ' << invalid_txns << endl;

// // for (auto &i : s) {
// // cout << i << endl;
// // }
// cout << total_fees << endl;



// // ifstream txn_file("mempool/ff907975dc0cfa299e908e5fba6df56c764866d9a9c22828824c28b8e4511320.json"); // p2pkh
// // ifstream txn_file("mempool/fff53b0fda0ab690ddaa23c84536e0d364a736bb93137a76ebf5d78f57cdd32f.json"); // p2wpkh
// ifstream txn_file("mempool/ff7f94f1344696386e4e75e33114fd158055104793eb151e73f0b032a073b35e.json"); // p2wsh
// // ifstream txn_file("mempool/598ed237ba816ab2c80ba7b57cd48c3ad87a15bf33736061eb0304ee23412d85.json"); // p2sh-p2wpkh
// // ifstream txn_file("mempool/598ed237ba816ab2c80ba7b57cd48c3ad87a15bf33736061eb0304ee23412d85.json");


// std::ostringstream tmp;
// tmp << txn_file.rdbuf();
// std::string txn_str = tmp.str();

// Json::Value txn;
// Json::Reader reader;
// reader.parse(txn_str.c_str(), txn);

// // string ser_txn = serialize_txn(txn, true);
// // string ser_txn_hex = bstr2hexstr(ser_txn, ser_txn.length());
// // cout << ser_txn_hex << '\n' << endl;

// // char sha[SHA256_DIGEST_LENGTH];
// // SHA256((unsigned char*) ser_txn.c_str(), ser_txn.length(), (unsigned char*) sha);
// // SHA256((unsigned char*) sha, SHA256_DIGEST_LENGTH, (unsigned char*) sha);
// // for (int i = 0; i < SHA256_DIGEST_LENGTH/2; i++)
// // swap(sha[i], sha[SHA256_DIGEST_LENGTH - i - 1]);

// // SHA256((unsigned char*) sha, SHA256_DIGEST_LENGTH, (unsigned char*) sha);
// // cout << "Triple SHA256: " << bstr2hexstr(sha, SHA256_DIGEST_LENGTH) << endl;

// cout << txn << endl;
// cout << verify_txn(txn) << endl;


return 0;
}
Loading

0 comments on commit 37423f6

Please sign in to comment.