Skip to content

Commit b34b72c

Browse files
Thomas Kerinafk11
authored andcommitted
add size debugger example
weight aware witness bytes
1 parent 4ec8a9e commit b34b72c

File tree

1 file changed

+61
-0
lines changed

1 file changed

+61
-0
lines changed

examples/size_debugger.php

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use BitWasp\Bitcoin\Transaction\TransactionFactory;
6+
use BitWasp\Buffertools\Buffertools;
7+
8+
require __DIR__ . "/../vendor/autoload.php";
9+
10+
if ($argc > 1) {
11+
$txHex = $argv[1];
12+
} else {
13+
$txHex = "0100000001e13241278d5dbd249891df669d4be4c96512890a4c76c011144a5dc751fa593701000000fdfe0000483045022100c45e2d2d8912039c7e8156fe31fdfde6dd16621699bad97ed7df22e04f053183022038e886df2272901ceb3a982835a0ebb646c1e5ef739ffe08f80027698a32dd6601483045022100855a889a94a4be12b0d56b38ee2270cae00124496628a79fa9a04b9fc798c68d0220553838e47da12cdcd250d83bf1ba91aa4cf09de7590af39beac8a3e866406693014c695221028f1517e5d95149114f866f84bea127e5d6a7e4e54602309900d0065059b48532210334eea53fc0b0a6261e7e31a71437924779c5460435ebf79428f204b3ef791e372103e8b3230be3109457a6874662eacfb4de96e0876318ff8d990450773976a3ce6b53aeffffffff02804a5d050000000017a9144e951cf05cdf421b1867b7990ad5bf8c3be6c9f087028898000000000017a9144aecb4cb3057831067272420194df741007a25db8700000000";
14+
}
15+
16+
$fields = [];
17+
$tx = TransactionFactory::fromHex($txHex);
18+
$fields[] = [4*4, "version {$tx->getVersion()}"];
19+
$fields[] = [4*Buffertools::numToVarInt(count($tx->getInputs()))->getSize(), 'nIn'];
20+
if ($tx->hasWitness()) {
21+
$fields[] = [2, "segwit markers 0001"];
22+
}
23+
foreach ($tx->getInputs() as $input) {
24+
$script = $input->getScript();
25+
$scriptSize = $script->getBuffer()->getSize();
26+
$scriptVarInt = Buffertools::numToVarInt($scriptSize);
27+
$fields[] = [4*32, "\ttxid\t".$input->getOutPoint()->getTxId()->getHex()];
28+
$fields[] = [4*4, "\tvout\t".$input->getOutPoint()->getVout()];
29+
$fields[] = [4*$scriptVarInt->getSize(), "\tvarint\t" . $scriptVarInt->getHex()];
30+
$fields[] = [4*$scriptSize, "\tscript\t".$input->getScript()->getHex()];
31+
$fields[] = [4*4, "\tseq\t".$input->getSequence()];
32+
}
33+
$fields[] = [Buffertools::numToVarInt(count($tx->getOutputs()))->getSize(), 'nOut'];
34+
foreach ($tx->getOutputs() as $output) {
35+
$script = $output->getScript();
36+
$scriptSize = $script->getBuffer()->getSize();
37+
$scriptVarInt = Buffertools::numToVarInt($scriptSize);
38+
$fields[] = [4*8, "\tvalue\t{$output->getValue()}"];
39+
$fields[] = [4*$scriptVarInt->getSize(), "\tvarint {$scriptVarInt->getHex()}\t"];
40+
$fields[] = [4*$scriptSize, "\tscript\n{$script->getHex()}"];
41+
}
42+
if ($tx->hasWitness()) {
43+
for ($i = 0; $i < count($tx->getInputs()); $i++) {
44+
$wit = $tx->getWitness($i);
45+
$fields[] = [Buffertools::numToVarInt(count($wit))->getSize(), "wit {$i}"];
46+
if ($wit->count() > 0) {
47+
foreach ($wit->all() as $value) {
48+
$fields[] = [Buffertools::numToVarInt($value->getSize())->getSize() + $value->getSize(), "\tvalue\t{$value->getHex()}"];
49+
}
50+
}
51+
}
52+
}
53+
$fields[] = [4*4, "lockTime {$tx->getLockTime()}"];
54+
55+
$totalIn = 0;
56+
foreach ($fields as $field) {
57+
$totalIn += $field[0];
58+
echo "{$field[0]}\t{$field[1]}\n";
59+
}
60+
echo "$totalIn\tTOTAL WEIGHT\n";
61+
echo ($totalIn/4)."\tTOTAL VSIZE\n";

0 commit comments

Comments
 (0)