forked from znort987/blockparser
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdumpTX.cpp
More file actions
368 lines (322 loc) · 10 KB
/
dumpTX.cpp
File metadata and controls
368 lines (322 loc) · 10 KB
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
// Dump everything known about a TX
#include <util.h>
#include <common.h>
#include <errlog.h>
#include <string.h>
#include <callback.h>
typedef GoogMap<Hash256, int, Hash256Hasher, Hash256Equal >::Map TxMap;
struct DumpTX:public Callback
{
optparse::OptionParser parser;
bool dump;
TxMap txMap;
bool isGenTX;
uint64_t bTime;
uint64_t valueIn;
uint64_t valueOut;
uint64_t nbInputs;
uint64_t nbOutputs;
uint64_t currBlock;
uint64_t nbDumped;
const uint8_t *txStart;
std::vector<uint256_t> rootHashes;
DumpTX()
{
parser
.usage("[list of transaction hashes]")
.version("")
.description(
"dumpp all the details of the list of specified transactions"
)
.epilog("")
;
}
virtual const char *name() const { return "dumpTX"; }
virtual const optparse::OptionParser *optionParser() const { return &parser; }
virtual bool needTXHash() const { return true; }
virtual void aliases(
std::vector<const char*> &v
) const
{
v.push_back("txinfo");
v.push_back("txshow");
v.push_back("showtx");
v.push_back("txdetails");
}
virtual int init(
int argc,
const char *argv[]
)
{
nbDumped = 0;
optparse::Values &values = parser.parse_args(argc, argv);
auto args = parser.args();
for(size_t i=1; i<args.size(); ++i) {
loadHash256List(rootHashes, args[i].c_str());
}
if(0<rootHashes.size()) {
info("dumping %d transactions\n", (int)rootHashes.size());
} else {
const char *defaultTX = "a1075db55d416d3ca199f55b6084e2115b9345e16c5cf302fc80e9d5fbf5d48d"; // Expensive pizza
warning("no TX hashes specified, using the infamous 10K pizza TX");
loadHash256List(rootHashes, defaultTX);
}
static uint8_t empty[kSHA256ByteSize] = { 0x42 };
txMap.setEmptyKey(empty);
for(auto const &txHash : rootHashes) {
txMap[txHash.v] = 1;
}
return 0;
}
virtual void startBlock(
const Block *b,
uint64_t
)
{
currBlock = b->height;
const uint8_t *p = b->data;
SKIP(uint32_t, version, p);
SKIP(uint256_t, prevBlkHash, p);
SKIP(uint256_t, blkMerkleRoot, p);
LOAD(uint32_t, blkTime, p);
bTime = blkTime;
}
virtual void startTX(
const uint8_t *p,
const uint8_t *hash
)
{
txStart = p;
nbInputs = 0;
nbOutputs = 0;
dump = (txMap.end()!=txMap.find(hash));
if(dump) {
struct tm gmTime;
time_t blockTime = bTime;
gmtime_r(&blockTime, &gmTime);
char timeBuf[256];
asctime_r(&gmTime, timeBuf);
size_t sz =strlen(timeBuf);
if(0<sz) timeBuf[sz-1] = 0;
LOAD(uint32_t, version, p);
printf("TX = {\n\n");
printf(" version = %" PRIu32 "\n", version);
printf(" minted in block = %" PRIu64 "\n", currBlock-1);
printf(" mint time = %" PRIu64 " (%s GMT)\n", bTime, timeBuf);
printf(" txHash = ");
showHex(hash);
printf("\n\n");
}
}
virtual void startInputs(
const uint8_t *p
)
{
}
virtual void endInputs(
const uint8_t *p
)
{
}
static void canonicalHexDump(
const uint8_t *p,
size_t size,
const char *indent
)
{
const uint8_t *s = p;
const uint8_t *e = size + p;
while(p<e) {
printf(
"%s%06x: ",
indent,
(int)(p-s)
);
const uint8_t *lp = p;
const uint8_t *np = 16 + p;
const uint8_t *le = std::min(e, 16+p);
while(lp<np) {
if(lp<le) printf("%02x ", (int)*lp);
else printf(" ");
++lp;
}
lp = p;
while(lp<le) {
int c = *(lp++);
printf("%c", isprint(c) ? c : '.');
}
printf("\n");
p = np;
}
}
virtual void startInput(
const uint8_t *p
)
{
if(dump) {
printf(
" input[%" PRIu64 "] = {\n\n",
nbInputs++
);
static uint256_t gNullHash;
LOAD(uint256_t, upTXHash, p);
LOAD(uint32_t, upOutputIndex, p);
LOAD_VARINT(inputScriptSize, p);
isGenTX = (0==memcmp(gNullHash.v, upTXHash.v, sizeof(gNullHash)));
if(isGenTX) {
uint64_t reward = getBaseReward(currBlock);
printf(" generation transaction\n");
printf(" based on block height, reward = %.8f\n", 1e-8*reward);
printf(" hex dump of coinbase follows:\n\n");
canonicalHexDump(p, inputScriptSize, " ");
valueIn += reward;
}
}
}
static void showScriptInfo(
const uint8_t *outputScript,
uint64_t outputScriptSize
)
{
uint8_t type[128];
const char *typeName = "unknown";
uint8_t pubKeyHash[kSHA256ByteSize];
int r = solveOutputScript(pubKeyHash, outputScript, outputScriptSize, type);
switch(r) {
case 0: {
typeName = "pays to hash160(pubKey)";
break;
}
case 1: {
typeName = "pays to explicit uncompressed pubKey";
break;
}
case 2: {
typeName = "pays to explicit compressed pubKey";
break;
}
case 3: {
typeName = "pays to hash160(script)";
break;
}
case 4: {
typeName = "pays to hash160(script)";
break;
}
case -2: {
typeName = "broken script generated by p2pool - coins lost";
break;
}
case -1: {
typeName = "couldn't parse script";
break;
}
}
printf("\n");
printf(" script type = %s\n", typeName);
if(0<=r) {
uint8_t btcAddr[64];
hash160ToAddr(btcAddr, pubKeyHash);
printf(" script pays to address %s\n", btcAddr);
}
}
virtual void edge(
uint64_t value,
const uint8_t *upTXHash,
uint64_t outputIndex,
const uint8_t *outputScript,
uint64_t outputScriptSize,
const uint8_t *downTXHash,
uint64_t inputIndex,
const uint8_t *inputScript,
uint64_t inputScriptSize
)
{
if(dump) {
uint8_t buf[1 + 2*kSHA256ByteSize];
toHex(buf, upTXHash);
printf(" outputIndex = %" PRIu64 "\n", outputIndex);
printf(" value = %.8f\n", value*1e-8);
printf(" upTXHash = %s\n\n", buf);
printf(" # challenge answer script, bytes=%" PRIu64 " (on downstream input) =\n", inputScriptSize);
showScript(inputScript, inputScriptSize, 0, " ");
printf(" ||\n");
printf(" VV\n");
printf(" # challenge script, bytes=%" PRIu64 " (on upstream output)=\n", outputScriptSize);
showScript(outputScript, outputScriptSize, 0, " ");
showScriptInfo(outputScript, outputScriptSize);
valueIn += value;
}
}
virtual void endInput(
const uint8_t *p
)
{
if(dump) {
printf(" }\n\n");
}
}
virtual void startOutputs(
const uint8_t *p
)
{
}
virtual void endOutputs(
const uint8_t *p
)
{
}
virtual void startOutput(
const uint8_t *p
)
{
if(dump) {
printf(
"\n"
" output[%" PRIu64 "] = {\n\n",
nbOutputs++
);
}
}
virtual void endOutput(
const uint8_t *p, // Pointer to TX output raw data
uint64_t value, // Number of satoshis on this output
const uint8_t *txHash, // sha256 of the current transaction
uint64_t outputIndex, // Index of this output in the current transaction
const uint8_t *outputScript, // Raw script (challenge to would-be spender) carried by this output
uint64_t outputScriptSize // Byte size of raw script
)
{
if(dump) {
printf(" value = %.8f\n", value*1e-8);
printf(" challenge script, bytes=%" PRIu64 " :\n", outputScriptSize);
showScript(outputScript, outputScriptSize, 0, " ");
showScriptInfo(outputScript, outputScriptSize);
printf(" }\n\n");
valueOut += value;
}
}
virtual void endTX(
const uint8_t *p
)
{
if(dump) {
LOAD(uint32_t, lockTime, p);
printf(" nbInputs = %" PRIu64 "\n", (uint64_t)nbInputs);
printf(" nbOutputs = %" PRIu64 "\n", (uint64_t)nbOutputs);
printf(" byteSize = %" PRIu64 "\n", (uint64_t)(p - txStart));
printf(" lockTime = %" PRIu32 "\n", (uint32_t)lockTime);
printf(" valueIn = %.2f\n", valueIn*1e-8);
printf(" valueOut = %.2f\n", valueOut*1e-8);
if(!isGenTX) {
printf(" fees = %.2f\n", (valueIn-valueOut)*1e-8);
}
printf("}\n");
++nbDumped;
}
if(nbDumped==txMap.size()) {
exit(0);
}
}
};
static DumpTX dumpTX;