forked from bcoin-org/bcoin
-
Notifications
You must be signed in to change notification settings - Fork 1
/
tx.js
200 lines (147 loc) · 3.29 KB
/
tx.js
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
'use strict';
const random = require('bcrypto/lib/random');
const Address = require('../lib/primitives/address');
const TX = require('../lib/primitives/tx');
const Script = require('../lib/script/script');
const MTX = require('../lib/primitives/mtx');
const consensus = require('../lib/protocol/consensus');
const common = require('../test/util/common');
const bench = require('./bench');
const tx3 = common.readTX('tx3');
const tx5 = common.readTX('tx5');
const tx10 = common.readTX('tx10');
{
const raw = tx5.getRaw();
const end = bench('parse');
for (let i = 0; i < 10000; i++)
TX.fromRaw(raw);
end(10000);
}
{
const [tx, view] = tx5.getTX();
const end = bench('sigops');
for (let i = 0; i < 100000; i++)
tx.getSigopsCost(view);
end(100000);
}
{
const [tx] = tx5.getTX();
const end = bench('serialize');
for (let i = 0; i < 10000; i++) {
tx._raw = null;
tx.toRaw();
}
end(10000);
}
{
const [tx] = tx3.getTX();
const end = bench('hash');
for (let i = 0; i < 30000; i++) {
tx.hash();
tx._hash = null;
}
end(30000);
}
{
const [tx] = tx5.getTX();
const end = bench('witness hash');
for (let i = 0; i < 30000; i++) {
tx.witnessHash();
tx._whash = null;
}
end(30000);
}
{
const [tx] = tx5.getTX();
const end = bench('sanity');
for (let i = 0; i < 10000; i++)
tx.isSane();
end(10000);
}
{
const [tx] = tx5.getTX();
const end = bench('input hashes');
for (let i = 0; i < 10000; i++)
tx.getInputHashes();
end(10000);
}
{
const [tx] = tx5.getTX();
const end = bench('output hashes');
for (let i = 0; i < 10000; i++)
tx.getOutputHashes();
end(10000);
}
{
const [tx] = tx5.getTX();
const end = bench('all hashes');
for (let i = 0; i < 10000; i++)
tx.getHashes();
end(10000);
}
{
const [tx, view] = tx3.getTX();
const end = bench('verify');
for (let i = 0; i < 30000; i++)
tx.verify(view, Script.flags.VERIFY_P2SH);
end(30000 * tx.inputs.length);
}
{
const [tx, view] = tx3.getTX();
const {script} = view.getOutputFor(tx.inputs[0]);
const end = bench('sighash');
for (let i = 0; i < 1000000; i++)
tx.signatureHashV0(0, script, Script.hashType.ALL);
end(1000000);
}
{
const [tx, view] = tx3.getTX();
const end = bench('fee');
for (let i = 0; i < 10000; i++)
tx.getFee(view);
end(10000);
}
{
const [tx, view] = tx10.getTX();
const flags = Script.flags.VERIFY_P2SH | Script.flags.VERIFY_DERSIG;
const end = bench('verify multisig');
for (let i = 0; i < 30000; i++)
tx.verify(view, flags);
end(30000 * tx.inputs.length);
}
const mtx = new MTX();
for (let i = 0; i < 100; i++) {
mtx.addInput({
prevout: {
hash: consensus.ZERO_HASH,
index: 0
},
script: new Script()
.pushData(Buffer.allocUnsafe(9))
.pushData(random.randomBytes(33))
.compile()
});
mtx.addOutput({
address: Address.fromHash(random.randomBytes(20)),
value: 0
});
}
const tx2 = mtx.toTX();
{
const end = bench('input hashes');
for (let i = 0; i < 10000; i++)
tx2.getInputHashes();
end(10000);
}
{
const end = bench('output hashes');
for (let i = 0; i < 10000; i++)
tx2.getOutputHashes();
end(10000);
}
{
const end = bench('all hashes');
for (let i = 0; i < 10000; i++)
tx2.getHashes();
end(10000);
}