forked from fiat-token/snippets
/
regtest_stressTest.pl
150 lines (127 loc) · 4.6 KB
/
regtest_stressTest.pl
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
use 5.022;
use warnings;
use Getopt::Long;
use experimental 'signatures';
use Time::HiRes qw(gettimeofday);
Getopt::Long::Configure qw(gnu_getopt);
#default params
my $bcreg = "bitcoin-cli -conf=/home/usrBTC/regtest/bitcoin.conf";
my $addresses = 1000;
my $fee = 0.0001;
my $split = 1000;
#get params
GetOptions
(
'bcreg|b=s' => \$bcreg,
'addresses|ad=s' => \$addresses,
'fee|f=s' => \$fee,
'split|s=s' => \$split
) or die "err options!\n";
#output params
#say "Executing script $0\n";
#say "parameters:";
#say "bcreg: $bcreg";
#say "addresses: $addresses";
#say "fee: $fee";
#say "split: $split";
#variables
my @allListAddress;
my @arr;
my $res = $addresses / $split;
my $file = "stressTest.log";
for (1 .. $res)
{
push @arr, $split;
}
stamp("Initialiting loop at: " . gettimeofday());
for my $loopAddress(0 .. $#arr)
{
stamp "Cycle $loopAddress...";
#calculating max utxo
say "\t0: calculating max amount to select input utxo...";
my $amount = get("$bcreg listunspent | jq 'max_by(.amount) | .amount'");
say "\tamount: $amount";
#creating addresses
say "\t1: creating $arr[$loopAddress] addresses..";
my @listAddress = createAddresses($arr[$loopAddress]);
push @allListAddress, @listAddress;
my $outamount = $amount / ($arr[$loopAddress]) - $fee;
say "\tarray: $#arr";
say "\tloopValue: $arr[$loopAddress]";
say "\toutput amount: $outamount";
#creating string containing all the outputs..
say "\t2: creating string containing all the outputs..";
my $stringOutput = "\"" . (join "\": $outamount, \"", @listAddress) . "\": $outamount";
#get utxo_txid
say "\t3: get the input utxo transaction id..";
my $utxo_txid = get("$bcreg listunspent | jq 'max_by(.amount) | .txid'");
say "\tutxo_txid: $utxo_txid";
#get utxo_vout
say "\t4: get the input utxo vout..";
my $utxo_vout = get("$bcreg listunspent | jq -r 'max_by(.amount) | .vout'");
say "\tutxo_vout: $utxo_vout";
#bcreg createrawtransaction
say "\t5: creating transaction with $arr[$loopAddress] outputs..";
my $rawtxhex = get("$bcreg -named createrawtransaction inputs='''[ { \"txid\": $utxo_txid, \"vout\": $utxo_vout } ]''' outputs='''{$stringOutput}'''");
#say "\trawtxhex: $rawtxhex";
#bcreg createrawtransaction
say "\t6: signing transaction with $arr[$loopAddress] outputs..";
my $signedtx = get("$bcreg -named signrawtransaction hexstring=$rawtxhex | jq -r '.hex'");
#bcreg sendrawtransaction
say "\t7: sending transaction with $arr[$loopAddress] outputs..";
my $hashTx = get("$bcreg -named sendrawtransaction hexstring=$signedtx");
#mining
say "\t8: generating new block..";
my $cmdMining = get("$bcreg generate 1");
#say "\tcmdMining: $cmdMining";
}
stamp("End loop");
#inizio creazione e firma delle tx
stamp("9: starting creation and signing of $addresses transactions..");
my @allTx;
for my $index(0 .. @allListAddress)
{
stamp("\t$index/" . scalar @allListAddress . "..") if($index % 100 == 0 || $index == scalar @allListAddress);
my $utxo_txid0 = get("$bcreg listunspent | jq -r '.[$index] | .txid'");
my $utxo_vout0 = get("$bcreg listunspent | jq -r '.[$index] | .vout'");
my $amount0 = get("$bcreg listunspent | jq -r '.[$index] | .amount'");
my $money = $amount0 - $fee;
my $newrecipient0 = get("$bcreg getrawchangeaddress");
my $rawtxhex0 = get("$bcreg -named createrawtransaction inputs='''[ { \"txid\": \"$utxo_txid0\", \"vout\": $utxo_vout0 } ]''' outputs='''{ \"$newrecipient0\": $money }'''");
my $signedtx0 = get("$bcreg -named signrawtransaction hexstring=$rawtxhex0 | jq -r '.hex'");
push @allTx, "$bcreg -named sendrawtransaction hexstring=$signedtx0";
}
#esecuzione finale
my $startTime = gettimeofday();
stamp("10: starting execution at " . localtime(time()));
for my $tx (@allTx)
{
#say "\t" . get("$tx");
get("$tx");
}
my $endTime = gettimeofday();
say "start " . $startTime . " | end: " . $endTime . " | time elapsed: " . ($endTime - $startTime);
stamp("start " . $startTime . " | end: " . $endTime . " | time elapsed: " . ($endTime - $startTime));
#END
#FUNCTIONS
sub get($cmd)
{
chomp(my $out = `$cmd &> stressTest_main.log`);
return $out;
}
sub stamp($data)
{
open my $fh, '>>', $file or die "errore aprendo il file $!\n";
flock $fh, 2;
say $fh "$$-" . $data;
}
sub createAddresses($localAddresses)
{
my @listAddress;
for my $index(1 .. $localAddresses)
{
stamp("\t$index/" . $localAddresses . "..") if($index % 100 == 0 || $index == $localAddresses);
push @listAddress, get("$bcreg getnewaddress");
}
return @listAddress;
}