Skip to content

Commit

Permalink
parse transaction data using nlohmann-json library
Browse files Browse the repository at this point in the history
  • Loading branch information
110CodingP committed Apr 27, 2024
1 parent 628693a commit 43123be
Show file tree
Hide file tree
Showing 8 changed files with 187 additions and 58 deletions.
5 changes: 5 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"files.associations": {
"iostream": "cpp"
}
}
28 changes: 28 additions & 0 deletions .vscode/tasks.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: g++ build active file",
"command": "/usr/bin/g++",
"args": [
"-fdiagnostics-color=always",
"-g",
"${file}",
"-o",
"${fileDirname}/${fileBasenameNoExtension}"
],
"options": {
"cwd": "${fileDirname}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "Task generated by Debugger."
}
],
"version": "2.0.0"
}
Binary file modified mine
Binary file not shown.
107 changes: 67 additions & 40 deletions mine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,66 +2,93 @@
#include <fstream>
#include <string>
#include <vector>
using namespace std;
#include <json.hpp>

using json = nlohmann::json;

struct Output {
int Amount;
string ScriptPubKey;
}
int amount;
std::string scriptPubKey;
std::string scriptPubKeyAsm;
std::string scriptPubKeyType;
std::string scriptPubKeyAddress;
};

struct Input {
string txId;
std::string txId;
int vOut;
string scriptSig;
string sequence;
vector<string> witness;
vector<Output> prevout;
string scriptSigAsm;
std::string scriptSig;
int sequence;
std::vector<std::string> witness;
Output prevout;
std::string scriptSigAsm;
bool isCoinbase;
}
};

struct Txn {
int version;
int locktime;
vector<Input> vin;
vector<Output> vout;
std::vector<Input> vin;
std::vector<Output> vout;

};

void from_json(const json& j, Output& p) {
j.at("scriptpubkey").get_to(p.scriptPubKey);
j.at("scriptpubkey_asm").get_to(p.scriptPubKeyAsm);
j.at("scriptpubkey_type").get_to(p.scriptPubKeyType);
j.at("scriptpubkey_address").get_to(p.scriptPubKeyAddress);
j.at("value").get_to(p.amount);
}

void from_json(const json& j, Input& p) {
j.at("txid").get_to(p.txId);
j.at("vout").get_to(p.vOut);
j.at("prevout").get_to(p.prevout);
j.at("scriptsig").get_to(p.scriptSig);
j.at("scriptsig_asm").get_to(p.scriptSigAsm);
j.at("witness").get_to(p.witness);
j.at("is_coinbase").get_to(p.isCoinbase);
j.at("sequence").get_to(p.sequence);
}

string serialise(string content) {
string serialised;
int sz = content.size();
return serialised;
void from_json(const json& j, Txn& p) {
j.at("version").get_to(p.version);
j.at("locktime").get_to(p.locktime);
j.at("vin").get_to(p.vin);
j.at("vout").get_to(p.vout);
}

std::string serialise(Txn t) {
std::string serialised;
return t.vin[0].scriptSig;
}

int main() {

freopen("output.txt","w",stdout);
freopen("output.txt","w",stdout);
std::ifstream myfile("txns.txt");
std::string filename;
std::vector<std::string> txns;

ifstream myfile;
myfile.open("txns.txt");
string filename;
vector<string> txns;
while (myfile.good()) {
getline(myfile,filename);
txns.push_back(filename);
}

while (myfile.good()) {
getline(myfile,filename);
txns.push_back(filename);
}
myfile.close();

for (std::string txn: txns) {//man this outer loop is running approx 8000 times!
std::string path = "mempool/" + txn;
myfile.open(path);
json data = json::parse(myfile);
Txn t;
from_json(data , t);
std::cout<<serialise(t)<<std::endl;

myfile.close();
}


for (string txn: txns) {//man this outer loop is running approx 8000 times!
string path = "mempool/" + txn;
myfile.open(path);
string content;
while (myfile.good()) {
string line;
getline(myfile,line);
content+=line;
}
serialise(content);

myfile.close();
}
return 0;
return 0;
}
29 changes: 28 additions & 1 deletion output.txt
Original file line number Diff line number Diff line change
@@ -1 +1,28 @@
{ "version": 2, "locktime": 0, "vin": [ { "txid": "79593e210f4e7c61be57dfb6c33541ab39513a3bb964737c8e8c988bd395986e", "vout": 1, "prevout": { "scriptpubkey": "5120f6dcb2b44ee298462ffc8dd9d7b969d660ddedd98ff31dd3be803b699aea9416", "scriptpubkey_asm": "OP_PUSHNUM_1 OP_PUSHBYTES_32 f6dcb2b44ee298462ffc8dd9d7b969d660ddedd98ff31dd3be803b699aea9416", "scriptpubkey_type": "v1_p2tr", "scriptpubkey_address": "bc1p7mwt9dzwu2vyvtlu3hva0wtf6esdmmwe3le3m5a7sqaknxh2jstq3vztqn", "value": 593313 }, "scriptsig": "", "scriptsig_asm": "", "witness": [ "7c6982626a43525e568d328824702b3743b112e40e75293b3162b182c41e7754d481e911839b211cb7547f266814afa911eea85676f69722c1f268410a75f864" ], "is_coinbase": false, "sequence": 4294967295 }, { "txid": "dd006a4a306b45dab04fb249be9449caae04df24269b60afd443238729260d56", "vout": 1, "prevout": { "scriptpubkey": "5120f6dcb2b44ee298462ffc8dd9d7b969d660ddedd98ff31dd3be803b699aea9416", "scriptpubkey_asm": "OP_PUSHNUM_1 OP_PUSHBYTES_32 f6dcb2b44ee298462ffc8dd9d7b969d660ddedd98ff31dd3be803b699aea9416", "scriptpubkey_type": "v1_p2tr", "scriptpubkey_address": "bc1p7mwt9dzwu2vyvtlu3hva0wtf6esdmmwe3le3m5a7sqaknxh2jstq3vztqn", "value": 10704 }, "scriptsig": "", "scriptsig_asm": "", "witness": [ "c2ed1bd63aa5e6e7d094210615d01cfcd74d87ffeb2232fb909f34dc661300e1ada70e6deda0cf3a05d390820a89b247d2115a59502eb82f1ed619ec929a2acd" ], "is_coinbase": false, "sequence": 4294967295 } ], "vout": [ { "scriptpubkey": "a914bf98ba19c45f75bb76b50afce91fb5bb7276bd7787", "scriptpubkey_asm": "OP_HASH160 OP_PUSHBYTES_20 bf98ba19c45f75bb76b50afce91fb5bb7276bd77 OP_EQUAL", "scriptpubkey_type": "p2sh", "scriptpubkey_address": "3KA5wqaDxNSkEWKdmsnx6jxJBKtQXuhM7u", "value": 600107 } ]}




























43 changes: 37 additions & 6 deletions test.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,41 @@
[
{
"version": 1,
"locktime": 0,
"vin": [
{
"Name": "Test",
"Val": "TestVal"
"txid": "3b7dc918e5671037effad7848727da3d3bf302b05f5ded9bec89449460473bbb",
"vout": 16,
"prevout": {
"scriptpubkey": "0014f8d9f2203c6f0773983392a487d45c0c818f9573",
"scriptpubkey_asm": "OP_0 OP_PUSHBYTES_20 f8d9f2203c6f0773983392a487d45c0c818f9573",
"scriptpubkey_type": "v0_p2wpkh",
"scriptpubkey_address": "bc1qlrvlygpudurh8xpnj2jg04zupjqcl9tnk5np40",
"value": 37079526
},
"scriptsig": "",
"scriptsig_asm": "",
"witness": [
"30440220780ad409b4d13eb1882aaf2e7a53a206734aa302279d6859e254a7f0a7633556022011fd0cbdf5d4374513ef60f850b7059c6a093ab9e46beb002505b7cba0623cf301",
"022bf8c45da789f695d59f93983c813ec205203056e19ec5d3fbefa809af67e2ec"
],
"is_coinbase": false,
"sequence": 4294967295
}
],
"vout": [
{
"scriptpubkey": "76a9146085312a9c500ff9cc35b571b0a1e5efb7fb9f1688ac",
"scriptpubkey_asm": "OP_DUP OP_HASH160 OP_PUSHBYTES_20 6085312a9c500ff9cc35b571b0a1e5efb7fb9f16 OP_EQUALVERIFY OP_CHECKSIG",
"scriptpubkey_type": "p2pkh",
"scriptpubkey_address": "19oMRmCWMYuhnP5W61ABrjjxHc6RphZh11",
"value": 100000
},
{
"Name": "Test2",
"Val": "TestVal2"
"scriptpubkey": "0014ad4cc1cc859c57477bf90d0f944360d90a3998bf",
"scriptpubkey_asm": "OP_0 OP_PUSHBYTES_20 ad4cc1cc859c57477bf90d0f944360d90a3998bf",
"scriptpubkey_type": "v0_p2wpkh",
"scriptpubkey_address": "bc1q44xvrny9n3t5w7lep58egsmqmy9rnx9lt6u0tc",
"value": 36977942
}
]
]
}
Binary file modified trying_nlohman_json
Binary file not shown.
33 changes: 22 additions & 11 deletions trying_nlohman_json.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,32 @@ struct Test {
j.at("Val").get_to(p.Val);
}

template <typename T, size_t N>
void from_json(const json& j, T (&t)[N]) {
if (j.size()!=N) {
throw std::runtime_error("size!");
}
size_t index=0;
for (auto& item : j) {
from_json(item, t[index++]);
}
}
// template <typename T, size_t N>
// void from_json(const json& j, T (&t)[N]) {
// if (j.size()!=N) {
// throw std::runtime_error("size!");
// }
// size_t index=0;
// for (auto& item : j) {
// from_json(item, t[index++]);
// }
// }
int main() {
std::ifstream f("test.json");
json data = json::parse(f);
Test my_array[2];
from_json(data, my_array);
std::cout<<my_array[0].Name<<std::endl;
return 0;
}
}

// [
// {
// "Name": "Test",
// "Val": "TestVal"
// },
// {
// "Name": "Test2",
// "Val": "TestVal2"
// }
// ]

0 comments on commit 43123be

Please sign in to comment.