Skip to content

Commit

Permalink
Mining only 5 p2wpkh txns
Browse files Browse the repository at this point in the history
  • Loading branch information
ayushj05 committed Apr 14, 2024
1 parent 29c2837 commit e442043
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 33 deletions.
Binary file modified bin/main
Binary file not shown.
6 changes: 3 additions & 3 deletions output.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
20000000000000000000000000000000000000000000000000000000000000000000000048700f542c26d305e4edb6dcfce3a96a9633928db09536fcf24d577037107bbba4111c66ffff001f00016636
010000000001010000000000000000000000000000000000000000000000000000000000000000ffffffff0403951a06ffffffff027cc44025000000001976a9142c30a6aaac6d96687291475d7d52f4b469f665a688ac0000000000000000266a24aa21a9ed07a5e6e56a9a192f151526c40c80687e98f96aa294ed36e47ac45ef7e87118210120000000000000000000000000000000000000000000000000000000000000000000000000
04ba8e35719b1041e0b25a304ca0ac179fb67a70a7267605fe6db6b43d334332
2000000000000000000000000000000000000000000000000000000000000000000000003998aec10bc0c04f72fb204958ebaa03ad02f08becc212a928b94da35b27fcb6991b1c66ffff001f0000aa6d
010000000001010000000000000000000000000000000000000000000000000000000000000000ffffffff0403951a06ffffffff027cc44025000000001976a9142c30a6aaac6d96687291475d7d52f4b469f665a688ac0000000000000000266a24aa21a9ed49a508272fe7d8e99dd04a97a46eb930e78330aadfb84ebb7efb4c8e9ba791a10120000000000000000000000000000000000000000000000000000000000000000000000000
4c25d63ea0d359553599808c9b0ce15bccae06d9091eb6db7948a577a9c86842
d46e76a0c8559b76ac00bf5a2f5df1aba3aa2fa8dcbfa9ab288027813b627f24
4c6e1339f6ec894ac4685f4ef934180d376b1870005fb5c6298261353d46cbac
141655b0e4239480afbdc12874e642ffeb94da72536ee4b8bace750820b26a06
Expand Down
7 changes: 1 addition & 6 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ void mine() {

if (block_size + ser_txn.size() < 1024*1024 && verify_txn(txn.second) >= 0) {
blockTxns.push_back(ser_txn);
wTXIDs.push_back(hash256(get_wTXID(txn.second)));
wTXIDs.push_back(hash256(ser_wit(txn.second)));
block_size += ser_txn.size();
reward += txn.first;

Expand All @@ -73,11 +73,6 @@ void mine() {


wTXIDs.push_back(string(32, 0));
for (auto it = wTXIDs.rbegin(); it != wTXIDs.rend(); it++) {
reverse(it->begin(), it->end());
cout << bstr2hexstr(*it, (*it).length()) << endl;
reverse(it->begin(), it->end());
}
string wTXID_commitment = hash256(getMerkleRoot(wTXIDs) + string(32, 0));
string coinbaseTxn = genCoinbaseTxn(reward, wTXID_commitment);
block_size += coinbaseTxn.size();
Expand Down
64 changes: 40 additions & 24 deletions src/serialize.h
Original file line number Diff line number Diff line change
Expand Up @@ -119,9 +119,9 @@ string int2compact (uint64_t num) {
return comp;
}

string get_wTXID (Json::Value txn) {
string serialize_txn (Json::Value txn, bool isCleared = false) {
string ser_txn = "";

ser_txn += int2bin(txn["version"].asUInt());
ser_txn += int2compact(txn["vin"].size());

Expand All @@ -132,9 +132,14 @@ string get_wTXID (Json::Value txn) {
ser_txn += txid;
ser_txn += int2bin(inp["vout"].asUInt());

ser_txn += int2compact(inp["scriptsig"].asString().length()/2);
ser_txn += hexstr2bstr(inp["scriptsig"].asString());

if (isCleared) {
ser_txn += int2compact(inp["prevout"]["scriptpubkey"].asString().length()/2);
ser_txn += hexstr2bstr(inp["prevout"]["scriptpubkey"].asString());
}
else {
ser_txn += int2compact(inp["scriptsig"].asString().length()/2);
ser_txn += hexstr2bstr(inp["scriptsig"].asString());
}
ser_txn += int2bin(inp["sequence"].asUInt());
}

Expand All @@ -146,24 +151,28 @@ string get_wTXID (Json::Value txn) {
ser_txn += hexstr2bstr(out["scriptpubkey"].asString());
}

for (auto &inp : txn["vin"]) {
if (!inp.isMember("witness"))
continue;

ser_txn += int2compact(inp["witness"].size());
for (auto &wit : inp["witness"])
ser_txn += int2compact(wit.asString().length()/2) + hexstr2bstr(wit.asString());
}

ser_txn += int2bin(txn["locktime"].asUInt());

return ser_txn;
}

string serialize_txn (Json::Value txn, bool isCleared = false) {

string ser_wit (Json::Value txn) {
bool no_witness = true;
for (auto &inp : txn["vin"]) {
if (inp.isMember("witness")) {
no_witness = false;
break;
}
}
if (no_witness)
return serialize_txn(txn);

string ser_txn = "";

ser_txn += int2bin(txn["version"].asUInt());
ser_txn += int2compact(0); // Marker for segwit
ser_txn += int2compact(1); // Flag for segwit
ser_txn += int2compact(txn["vin"].size());

for (auto &inp : txn["vin"]) {
Expand All @@ -173,14 +182,9 @@ string serialize_txn (Json::Value txn, bool isCleared = false) {
ser_txn += txid;
ser_txn += int2bin(inp["vout"].asUInt());

if (isCleared) {
ser_txn += int2compact(inp["prevout"]["scriptpubkey"].asString().length()/2);
ser_txn += hexstr2bstr(inp["prevout"]["scriptpubkey"].asString());
}
else {
ser_txn += int2compact(inp["scriptsig"].asString().length()/2);
ser_txn += hexstr2bstr(inp["scriptsig"].asString());
}
ser_txn += int2compact(inp["scriptsig"].asString().length()/2);
ser_txn += hexstr2bstr(inp["scriptsig"].asString());

ser_txn += int2bin(inp["sequence"].asUInt());
}

Expand All @@ -192,11 +196,23 @@ string serialize_txn (Json::Value txn, bool isCleared = false) {
ser_txn += hexstr2bstr(out["scriptpubkey"].asString());
}

for (auto &inp : txn["vin"]) {
if (!inp.isMember("witness")) {
ser_txn += int2compact(0);
continue;
}

ser_txn += int2compact(inp["witness"].size());
for (auto &wit : inp["witness"])
ser_txn += int2compact(wit.asString().length()/2) + hexstr2bstr(wit.asString());
}

ser_txn += int2bin(txn["locktime"].asUInt());

return ser_txn;
}


string serialize_segwit_1 (Json::Value txn) {
string serialized = "";

Expand Down

0 comments on commit e442043

Please sign in to comment.