-
Notifications
You must be signed in to change notification settings - Fork 20
/
transaction.rs
355 lines (329 loc) · 9.24 KB
/
transaction.rs
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
use std::hashmap::HashMap;
use std::to_str::ToStr;
use decoder;
use util;
use hash;
pub struct TxIn {
prev_hash: ~[u8],
prev_index: u32,
scriptSig: ~[u8],
nSequence: u32,
nHashType: u8
}
pub struct TxOut {
nValue: u64,
scriptPubKey: ~[u8]
}
pub struct Transaction {
nVersion: u32,
nLockTime: u32,
input: ~[TxIn],
output: ~[TxOut]
}
/**
* Hex string parser state machine
*/
enum ParserState {
ReadVersion,
ReadInputCount,
ReadTxinHash,
ReadTxinIndex,
ReadTxinScriptSigLen,
ReadTxinScriptSig,
ReadTxinSequence,
ReadOutputCount,
ReadTxoutValue,
ReadTxoutScriptLen,
ReadTxoutScript,
ReadLockTime,
Error,
Done
}
/**
* Constructor for empty TxIn/TxOut
*/
fn new_blank_txin() -> TxIn
{
TxIn { prev_hash: ~[], prev_index: 0, scriptSig: ~[], nSequence: 0, nHashType: 0 }
}
fn new_blank_txout() -> TxOut
{
TxOut { nValue: 0, scriptPubKey: ~[] }
}
/**
* Copy constructors
*/
impl Clone for TxOut {
fn clone(&self) -> TxOut
{
TxOut { nValue: self.nValue, scriptPubKey: self.scriptPubKey.clone() }
}
}
impl Clone for TxIn {
fn clone(&self) -> TxIn
{
TxIn {
prev_hash: self.prev_hash.clone(),
prev_index: self.prev_index,
scriptSig: self.scriptSig.clone(),
nSequence: self.nSequence,
nHashType: self.nHashType
}
}
}
impl Clone for Transaction {
fn clone(&self) -> Transaction
{
Transaction {
nVersion: self.nVersion,
nLockTime: self.nLockTime,
input: self.input.clone(),
output: self.output.clone()
}
}
}
/**
* Constructor / createrawtransaction parser
*/
pub fn from_hex (hex_string: &[u8]) -> Option<Transaction>
{
let mut rv: Transaction = Transaction {
nVersion: 0,
nLockTime: 0,
input: ~[],
output: ~[]
};
/* Auxiallary state */
let mut width = 0;
let mut vin_counter: u64 = 0;
let mut vout_counter: u64 = 0;
/* RUN STATE MACHINE */
let mut iter = hex_string.iter();
let mut state = ReadVersion; /* Initial state: read version */
loop {
state = match state {
/* Read big-endian u32 version */
ReadVersion => {
match decoder::decode_token (&mut iter, decoder::Unsigned32) {
decoder::Integer(n) => { rv.nVersion = n as u32; ReadInputCount }
_ => Error
}
}
/* READ INPUTS */
ReadInputCount => {
match decoder::decode_token (&mut iter, decoder::VarInt) {
decoder::Integer(0) => { Error } /* zero inputs is a failure */
decoder::Integer(n) => { vin_counter = n; ReadTxinHash }
_ => Error
}
}
/* Read the hash of a txin */
ReadTxinHash => {
match decoder::decode_token (&mut iter, decoder::Bytestring(32)) {
decoder::String(s) => {
let mut new_txin = new_blank_txin();
new_txin.prev_hash = s;
rv.input.push (new_txin);
ReadTxinIndex
}
_ => Error
}
}
/* Read the index of a txin */
ReadTxinIndex => {
match decoder::decode_token (&mut iter, decoder::Unsigned32) {
decoder::Integer(n) => { rv.input[rv.input.len() - 1].prev_index = n as u32; ReadTxinScriptSigLen }
_ => Error
}
}
/* Read the scriptSig of a txin */
ReadTxinScriptSigLen => {
match decoder::decode_token (&mut iter, decoder::VarInt) {
decoder::Integer(0) => { ReadTxinSequence } /* skip scriptSig if it has width 0 */
decoder::Integer(n) => { width = n; ReadTxinScriptSig }
_ => Error
}
}
ReadTxinScriptSig => {
match decoder::decode_token (&mut iter, decoder::Bytestring(width)) {
decoder::String(s) => {
/* A standard tx scriptSig is PUSH[n+1] followed by an n-byte signature
* then a 1-byte hash type. We hardcode this form since it is not clear
* semantically what anything except this exact form means to us --- so
* there's no point in doing any more intelligent processing. */
if s[0] > 0 && s[0] < 76 && s.len() > s[0] as uint {
rv.input[rv.input.len() - 1].nHashType = s[s[0]];
}
rv.input[rv.input.len() - 1].scriptSig = s;
ReadTxinSequence
}
_ => Error
}
}
/* Read the sequence no. of a txin */
ReadTxinSequence => {
match decoder::decode_token (&mut iter, decoder::Unsigned32) {
decoder::Integer(n) => {
rv.input[rv.input.len() - 1].nSequence = n as u32;
vin_counter -= 1;
if vin_counter > 0 {
ReadTxinHash
} else {
ReadOutputCount
}
}
_ => Error
}
}
/* READ OUTPUTS */
ReadOutputCount => {
match decoder::decode_token (&mut iter, decoder::VarInt) {
decoder::Integer(0) => { Error } /* zero outputs is a failure (maybe it shouldn't be?) */
decoder::Integer(n) => { vout_counter = n; ReadTxoutValue }
_ => Error
}
}
/* Read txout value */
ReadTxoutValue => {
match decoder::decode_token (&mut iter, decoder::Unsigned64) {
decoder::Integer(n) => {
let mut new_output = new_blank_txout();
new_output.nValue = n;
rv.output.push (new_output);
ReadTxoutScriptLen
}
_ => Error
}
}
/* Read txout script */
ReadTxoutScriptLen => {
match decoder::decode_token (&mut iter, decoder::VarInt) {
/* skip scriptPubKey if it has width 0 */
decoder::Integer(0) => {
vout_counter -= 1;
if vout_counter > 0 {
ReadTxoutValue
} else {
ReadLockTime
}
}
decoder::Integer(n) => { width = n; ReadTxoutScript }
_ => Error
}
}
ReadTxoutScript => {
match decoder::decode_token (&mut iter, decoder::Bytestring(width)) {
decoder::String(s) => {
rv.output[rv.output.len() - 1].scriptPubKey = s;
vout_counter -= 1;
if vout_counter > 0 {
ReadTxoutValue
} else {
ReadLockTime
}
}
_ => Error
}
}
/* DONE OUTPUTS, Read nLockTime */
ReadLockTime => {
match decoder::decode_token (&mut iter, decoder::Unsigned32) {
decoder::Integer(n) => { rv.nLockTime = n as u32; Done }
_ => Error
}
}
/* Finished */
Error => { return None; }
Done => { break }
}
}
Some (rv)
}
impl Transaction {
/**
* Private serialize function
*/
fn serialize (&self) -> ~[u8]
{
let mut rv:~[u8] = ~[];
/* push version */
rv = hash::push_u32_le (rv, self.nVersion);
/* push txins */
rv = hash::push_vi_le (rv, self.input.len() as u64);
for txin in self.input.iter() {
rv.push_all (txin.prev_hash);
rv = hash::push_u32_le (rv, txin.prev_index);
rv = hash::push_vi_le (rv, txin.scriptSig.len() as u64);
rv.push_all (txin.scriptSig);
rv = hash::push_u32_le (rv, txin.nSequence);
}
/* push txouts */
rv = hash::push_vi_le (rv, self.output.len() as u64);
for txout in self.output.iter() {
rv = hash::push_u64_le (rv, txout.nValue);
rv = hash::push_vi_le (rv, txout.scriptPubKey.len() as u64);
rv.push_all (txout.scriptPubKey);
}
/* push locktime */
rv = hash::push_u32_le (rv, self.nLockTime);
rv
}
/** Getter for mpo */
pub fn most_popular_output (&self) -> u64 {
fn fold_function ((max_elem, max_count): (u64, uint), (&elem, &count): (&u64, &uint)) -> (u64, uint) {
if count > max_count {
(elem, count)
} else if count < max_count {
(max_elem, max_count)
} else if elem == 0 && max_elem == 0 {
(0, count) /* this shouldn't ever happen */
} else {
let mut max_scan = max_elem;
let mut elem_scan = elem;
/* tiebreak goes to rounder number */
while (max_scan % 10) == 0 &&
(elem_scan % 10) == 0 {
max_scan /= 10;
elem_scan /= 10;
}
if max_scan % 10 == 0 { (max_elem, max_count) } else { (elem, count) }
}
};
let mut values: HashMap<u64,uint> = HashMap::new ();
/* For each output increment its count */
for output in self.output.iter() {
values.mangle (output.nValue, (), |_,_| 1, |_,v,_| { *v += 1; });
}
values.iter().fold ((0, 0), fold_function).first()
}
/** Getter for mpo count */
pub fn most_popular_output_count (&self) -> uint {
let mut mpo_count = 0;
let mpo = self.most_popular_output ();
for output in self.output.iter() {
if output.nValue == mpo {
mpo_count += 1;
}
}
mpo_count
}
}
impl hash::Hashable for Transaction {
/**
* This function generates a txid for the transaction.
*/
fn to_hash(&self) -> ~[u8]
{
/* The TXID is the SHA256^2 of the serialization. We reverse it since bitcoin
* treats it as a little-endian 256-bit number. */
let mut rv = hash::sha256_sum (hash::sha256_sum (self.serialize()));
rv.reverse();
rv
}
}
impl ToStr for Transaction {
fn to_str(&self) -> ~str
{
util::u8_to_hex_string (self.serialize())
}
}