-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathblockSizes.cpp
100 lines (92 loc) · 3.01 KB
/
blockSizes.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#include <iostream>
#include <vector>
#include <string>
#include <fstream>
#include <sstream>
using namespace std;
string blocksFolder = "/mnt/f/Bitcoin/Data/blocks/";
vector<string> getFileContent(string path), split(string s, string delimiter = " ");
void print(string s);
int convertStrToInt(string str);
template<typename T>
string convertNbToStr(const T& number)
{
ostringstream convert;
convert << number;
return convert.str();
}
int main()
{
vector<string> lines = getFileContent("../piX.txt");
unsigned int linesSize = lines.size();
unsigned long long totalSize = 0;
for(unsigned int linesIndex = 0; linesIndex < linesSize; linesIndex++)
{
//if(linesIndex % 10 == 0)
// print(convertNbToStr(linesIndex) + " / " + convertNbToStr(linesSize));
string line = lines[linesIndex];
vector<string> lineParts = split(line);
unsigned short linePartsSize = lineParts.size();
if(linePartsSize != 2)
print("format problem");
unsigned short blockNumber = convertStrToInt(lineParts[0]);
unsigned int blockPosition = convertStrToInt(lineParts[1]);
string blockNumberStr = lineParts[0];
unsigned short blockNumberStrLength = blockNumberStr.length();
//print("blockNumberStr (def - " + convertNbToStr(blockNumberStrLength) + "): " + blockNumberStr);
for(unsigned short i = 0; i < 5 - blockNumberStrLength; i++) // it seems that I was using blockNumberStr.length() and it was recomputing each round
{
blockNumberStr = "0" + blockNumberStr;
//print("blockNumberStr (add): " + blockNumberStr);
}
string filePath = blocksFolder + "blk" + blockNumberStr + ".dat";
FILE* f = fopen(filePath.c_str(), "rb");
if(f == NULL)
{
print("Failed to open file (" + filePath + ") !");
//continue;
return 1;
}
fseek(f, blockPosition + 4, SEEK_SET);
char blockSizeChars[4];
for(unsigned short i = 0; i < 4; i++)
blockSizeChars[i] = fgetc(f);
fclose(f);
unsigned int blockSize = *(reinterpret_cast<unsigned int*>(blockSizeChars));
totalSize += blockSize + 8;
print(convertNbToStr(blockSize + 8));
}
//print("totalSize (bytes): " + convertNbToStr(totalSize)); // 1 779 414 479 - now 963 261 638
return 0;
}
int convertStrToInt(string str)
{
int number;
sscanf(str.c_str(), "%d", &number);
return number;
}
vector<string> getFileContent(string path)
{
vector<string> vec;
ifstream infile(path.c_str());
string line;
while(getline(infile, line))
vec.push_back(line);
return vec;
}
vector<string> split(string s, string delimiter)
{
vector<string> toReturn;
size_t pos = 0;
while((pos = s.find(delimiter)) != string::npos)
{
toReturn.push_back(s.substr(0, pos));
s.erase(0, pos + delimiter.length());
}
toReturn.push_back(s);
return toReturn;
}
void print(string s)
{
cout << s << endl;
}