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
//! Integer overflow edge cases for transaction validation
//!
//! Tests for consensus-critical integer overflow handling that must match
//! consensus's MoneyRange() behavior exactly.
//!
//! Consensus-critical: Overflow handling differences can cause different validation results.
use blvm_consensus::constants::MAX_MONEY;
use blvm_consensus::transaction::check_transaction;
use blvm_consensus::transaction::check_tx_inputs;
use blvm_consensus::types::{
OutPoint, Transaction, TransactionInput, TransactionOutput, UtxoSet, UTXO,
};
/// Test that output value sum overflow is detected correctly
///
/// consensus's behavior:
/// 1. Check each output: nValue >= 0 && nValue <= MAX_MONEY
/// 2. Add: nValueOut += txout.nValue (no overflow check)
/// 3. Check result: MoneyRange(nValueOut) = (nValueOut >= 0 && nValueOut <= MAX_MONEY)
///
/// Edge case: What if individual values are valid, but sum exceeds MAX_MONEY?
#[test]
fn test_output_sum_exceeds_max_money() {
// Create transaction with outputs that individually pass checks
// but sum exceeds MAX_MONEY
let tx = Transaction {
version: 1,
inputs: vec![TransactionInput {
prevout: OutPoint {
hash: [1; 32].into(),
index: 0,
},
script_sig: vec![0x51],
sequence: 0xffffffff,
}]
.into(),
outputs: vec![
TransactionOutput {
value: MAX_MONEY, // First output at max
script_pubkey: vec![0x51].into(),
},
TransactionOutput {
value: 1, // Second output pushes sum over MAX_MONEY
script_pubkey: vec![0x51].into(),
},
]
.into(),
lock_time: 0,
};
let result = check_transaction(&tx).unwrap();
// Should be invalid: total exceeds MAX_MONEY
assert!(matches!(
result,
blvm_consensus::types::ValidationResult::Invalid(_)
));
}
/// Test that output value sum near i64::MAX is handled correctly
///
/// Edge case: What if sum would overflow i64 but individual values are valid?
/// - MAX_MONEY = 2,100,000,000,000,000 (2.1 quadrillion)
/// - i64::MAX = 9,223,372,036,854,775,807 (9.2 quintillion)
/// - So we can have ~4.4 MAX_MONEY values before hitting i64::MAX
#[test]
fn test_output_sum_near_i64_max() {
// Create transaction with outputs that sum to near i64::MAX
// but still within MAX_MONEY per output
let large_value = MAX_MONEY;
let num_outputs = 4; // 4 * MAX_MONEY = 8.4 quadrillion < i64::MAX
let mut outputs = Vec::new();
for _ in 0..num_outputs {
outputs.push(TransactionOutput {
value: large_value,
script_pubkey: vec![0x51].into(),
});
}
let tx = Transaction {
version: 1,
inputs: vec![TransactionInput {
prevout: OutPoint {
hash: [1; 32].into(),
index: 0,
},
script_sig: vec![0x51],
sequence: 0xffffffff,
}]
.into(),
outputs: outputs.into(),
lock_time: 0,
};
let result = check_transaction(&tx).unwrap();
// Should be invalid: total (4 * MAX_MONEY) exceeds MAX_MONEY
assert!(matches!(
result,
blvm_consensus::types::ValidationResult::Invalid(_)
));
}
/// Test that input value sum overflow is detected correctly
///
/// Similar to output sum, but for inputs in check_tx_inputs
#[test]
fn test_input_sum_overflow() {
let mut utxo_set = UtxoSet::default();
// Create UTXOs with large values
let large_value = MAX_MONEY;
// Add multiple UTXOs that individually pass checks
for i in 0..5 {
let outpoint = OutPoint {
hash: [i as u8; 32].into(),
index: 0,
};
let utxo = UTXO {
value: large_value,
script_pubkey: vec![0x51].into(),
height: 0,
is_coinbase: false,
};
utxo_set.insert(outpoint, std::sync::Arc::new(utxo));
}
// Create transaction spending all these UTXOs
let mut inputs = Vec::new();
for i in 0..5 {
inputs.push(TransactionInput {
prevout: OutPoint {
hash: [i as u8; 32].into(),
index: 0,
},
script_sig: vec![0x51],
sequence: 0xffffffff,
});
}
let tx = Transaction {
version: 1,
inputs: inputs.into(),
outputs: vec![TransactionOutput {
value: 1000,
script_pubkey: vec![0x51].into(),
}]
.into(),
lock_time: 0,
};
let (result, _fee) = check_tx_inputs(&tx, &utxo_set, 0).unwrap();
// Should handle overflow correctly (either reject or handle gracefully)
// The sum of 5 * MAX_MONEY would exceed i64::MAX, so checked_add should catch it
match result {
blvm_consensus::types::ValidationResult::Valid => {
// If valid, fee calculation should also be valid
// This tests that overflow is caught before fee calculation
}
blvm_consensus::types::ValidationResult::Invalid(_) => {
// Expected: overflow detected
}
}
}
/// Test that individual output values at MAX_MONEY are valid
#[test]
fn test_single_output_at_max_money() {
let tx = Transaction {
version: 1,
inputs: vec![TransactionInput {
prevout: OutPoint {
hash: [1; 32].into(),
index: 0,
},
script_sig: vec![0x51],
sequence: 0xffffffff,
}]
.into(),
outputs: vec![TransactionOutput {
value: MAX_MONEY, // Single output at max - should be valid
script_pubkey: vec![0x51].into(),
}]
.into(),
lock_time: 0,
};
let result = check_transaction(&tx).unwrap();
// Should be valid: single output at MAX_MONEY is allowed
assert!(matches!(
result,
blvm_consensus::types::ValidationResult::Valid
));
}
/// Test that output value just above MAX_MONEY is rejected
#[test]
fn test_output_above_max_money() {
let tx = Transaction {
version: 1,
inputs: vec![TransactionInput {
prevout: OutPoint {
hash: [1; 32].into(),
index: 0,
},
script_sig: vec![0x51],
sequence: 0xffffffff,
}]
.into(),
outputs: vec![TransactionOutput {
value: MAX_MONEY + 1, // Just above max - should be invalid
script_pubkey: vec![0x51].into(),
}]
.into(),
lock_time: 0,
};
let result = check_transaction(&tx).unwrap();
// Should be invalid: individual output exceeds MAX_MONEY
assert!(matches!(
result,
blvm_consensus::types::ValidationResult::Invalid(_)
));
}
/// Test that negative output values are rejected
#[test]
fn test_negative_output_value() {
let tx = Transaction {
version: 1,
inputs: vec![TransactionInput {
prevout: OutPoint {
hash: [1; 32].into(),
index: 0,
},
script_sig: vec![0x51],
sequence: 0xffffffff,
}]
.into(),
outputs: vec![TransactionOutput {
value: -1, // Negative value - should be invalid
script_pubkey: vec![0x51].into(),
}]
.into(),
lock_time: 0,
};
let result = check_transaction(&tx).unwrap();
// Should be invalid: negative output value
assert!(matches!(
result,
blvm_consensus::types::ValidationResult::Invalid(_)
));
}
/// Test that zero output values are valid
#[test]
fn test_zero_output_value() {
let tx = Transaction {
version: 1,
inputs: vec![TransactionInput {
prevout: OutPoint {
hash: [1; 32].into(),
index: 0,
},
script_sig: vec![0x51],
sequence: 0xffffffff,
}]
.into(),
outputs: vec![TransactionOutput {
value: 0, // Zero value - should be valid (dust outputs)
script_pubkey: vec![0x51].into(),
}]
.into(),
lock_time: 0,
};
let result = check_transaction(&tx).unwrap();
// Should be valid: zero output value is allowed (though not economically useful)
assert!(matches!(
result,
blvm_consensus::types::ValidationResult::Valid
));
}
/// Test that many small outputs summing to valid total are accepted
#[test]
fn test_many_small_outputs() {
// Create transaction with many small outputs that sum to a valid total
let num_outputs = 1000;
let value_per_output = MAX_MONEY / (num_outputs as i64 + 1); // Ensure sum < MAX_MONEY
let mut outputs = Vec::new();
for _ in 0..num_outputs {
outputs.push(TransactionOutput {
value: value_per_output,
script_pubkey: vec![0x51].into(),
});
}
let tx = Transaction {
version: 1,
inputs: vec![TransactionInput {
prevout: OutPoint {
hash: [1; 32].into(),
index: 0,
},
script_sig: vec![0x51],
sequence: 0xffffffff,
}]
.into(),
outputs: outputs.into(),
lock_time: 0,
};
let result = check_transaction(&tx).unwrap();
// Should be valid: many small outputs summing to valid total
assert!(matches!(
result,
blvm_consensus::types::ValidationResult::Valid
));
}