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
//! Regtest automation utilities for testing
//!
//! Provides helper functions for automated testing with Bitcoin Core in regtest mode.
use crate::client::BitcoinClient;
use crate::error::BitcoinError;
use bitcoin::{Address, Amount};
use std::str::FromStr;
/// Configuration for regtest automation
#[derive(Debug, Clone)]
pub struct RegtestConfig {
/// Number of blocks to mine for initial maturity
pub initial_blocks: u32,
/// Number of blocks to mine for transaction confirmations
pub confirmation_blocks: u32,
/// Auto-mine on transaction submission
pub auto_mine: bool,
/// Default amount to fund test wallets (in satoshis)
pub default_funding_amount: u64,
}
impl Default for RegtestConfig {
fn default() -> Self {
Self {
initial_blocks: 101, // Coinbase maturity is 100 blocks
confirmation_blocks: 6,
auto_mine: true,
default_funding_amount: 100_000_000, // 1 BTC
}
}
}
/// Regtest automation helper
pub struct RegtestHelper {
client: BitcoinClient,
config: RegtestConfig,
mining_address: Option<Address>,
}
impl RegtestHelper {
/// Create a new regtest helper
pub fn new(client: BitcoinClient, config: RegtestConfig) -> Self {
Self {
client,
config,
mining_address: None,
}
}
/// Initialize regtest environment
///
/// # Example
///
/// ```rust,no_run
/// # use kaccy_bitcoin::{BitcoinClient, BitcoinNetwork, regtest_utils::{RegtestHelper, RegtestConfig}};
/// # fn example() -> Result<(), Box<dyn std::error::Error>> {
/// # let client = BitcoinClient::new("http://localhost:18443", "user", "pass", BitcoinNetwork::Regtest)?;
/// let config = RegtestConfig::default();
/// let mut helper = RegtestHelper::new(client, config);
/// helper.initialize()?;
/// # Ok(())
/// # }
/// ```
pub fn initialize(&mut self) -> Result<(), BitcoinError> {
// Generate a mining address if not set
if self.mining_address.is_none() {
let address = self.client.get_new_address(None)?.assume_checked();
self.mining_address = Some(address);
}
// Mine initial blocks for coinbase maturity
self.mine_blocks(self.config.initial_blocks)?;
Ok(())
}
/// Set custom mining address
pub fn set_mining_address(&mut self, address: Address) {
self.mining_address = Some(address);
}
/// Mine a specified number of blocks
pub fn mine_blocks(&self, count: u32) -> Result<Vec<String>, BitcoinError> {
let address = self
.mining_address
.clone()
.ok_or_else(|| BitcoinError::InvalidAddress("Mining address not set".to_string()))?;
let block_hashes = self
.client
.generate_to_address(count as u64, &address)
.map_err(|e| BitcoinError::RpcError(e.to_string()))?;
Ok(block_hashes.iter().map(|h| h.to_string()).collect())
}
/// Mine a single block
pub fn mine_block(&self) -> Result<String, BitcoinError> {
let blocks = self.mine_blocks(1)?;
Ok(blocks.into_iter().next().unwrap())
}
/// Fund a wallet address with test coins
///
/// # Example
///
/// ```rust,no_run
/// # use kaccy_bitcoin::{BitcoinClient, BitcoinNetwork, regtest_utils::{RegtestHelper, RegtestConfig}};
/// # use bitcoin::Address;
/// # use std::str::FromStr;
/// # fn example() -> Result<(), Box<dyn std::error::Error>> {
/// # let client = BitcoinClient::new("http://localhost:18443", "user", "pass", BitcoinNetwork::Regtest)?;
/// # let helper = RegtestHelper::new(client, RegtestConfig::default());
/// let address = Address::from_str("bcrt1qxy2kgdygjrsqtzq2n0yrf2493p83kkfjhx0wlh")?
/// .require_network(bitcoin::Network::Regtest)?;
/// let txid = helper.fund_address(&address, None)?;
/// # Ok(())
/// # }
/// ```
pub fn fund_address(
&self,
address: &Address,
amount: Option<u64>,
) -> Result<String, BitcoinError> {
let amount_satoshis = amount.unwrap_or(self.config.default_funding_amount);
let amount_btc = Amount::from_sat(amount_satoshis);
let txid = self
.client
.send_to_address(address, amount_btc)
.map_err(|e| BitcoinError::RpcError(e.to_string()))?;
// Auto-mine if enabled
if self.config.auto_mine {
self.mine_blocks(self.config.confirmation_blocks)?;
}
Ok(txid.to_string())
}
/// Wait for a transaction to be confirmed
pub fn wait_for_confirmation(&self, txid: &str) -> Result<u32, BitcoinError> {
let txid_parsed = bitcoin::Txid::from_str(txid)
.map_err(|e| BitcoinError::InvalidTransaction(e.to_string()))?;
// Mine blocks to confirm the transaction
self.mine_blocks(self.config.confirmation_blocks)?;
// Get confirmation count
let tx_info = self
.client
.get_transaction(&txid_parsed)
.map_err(|e| BitcoinError::RpcError(e.to_string()))?;
Ok(tx_info.info.confirmations as u32)
}
/// Advance blockchain time by mining empty blocks
pub fn advance_time(&self, blocks: u32) -> Result<(), BitcoinError> {
self.mine_blocks(blocks)?;
Ok(())
}
/// Get current block height
pub fn get_height(&self) -> Result<u64, BitcoinError> {
self.client.get_block_height()
}
/// Invalidate a block (simulate reorg)
pub fn invalidate_block(&self, block_hash: &str) -> Result<(), BitcoinError> {
let hash = bitcoin::BlockHash::from_str(block_hash)
.map_err(|e| BitcoinError::InvalidTransaction(e.to_string()))?;
self.client
.invalidate_block(&hash)
.map_err(|e| BitcoinError::RpcError(e.to_string()))?;
Ok(())
}
/// Reconsider a block (undo invalidation)
pub fn reconsider_block(&self, block_hash: &str) -> Result<(), BitcoinError> {
let hash = bitcoin::BlockHash::from_str(block_hash)
.map_err(|e| BitcoinError::InvalidTransaction(e.to_string()))?;
self.client
.reconsider_block(&hash)
.map_err(|e| BitcoinError::RpcError(e.to_string()))?;
Ok(())
}
/// Simulate a blockchain reorganization
///
/// Creates a fork by invalidating recent blocks and mining a longer chain
pub fn simulate_reorg(&self, depth: u32) -> Result<ReorgInfo, BitcoinError> {
let start_height = self.get_height()?;
// Get the hash of the block at the fork point
let fork_height = start_height.saturating_sub(depth as u64);
let fork_hash = self.client.get_block_hash(fork_height)?.to_string();
// Invalidate blocks from the fork point
for i in 0..depth {
let height = start_height - i as u64;
let hash = self.client.get_block_hash(height)?.to_string();
self.invalidate_block(&hash)?;
}
let current_height = self.get_height()?;
// Mine a longer chain (depth + 1 blocks)
self.mine_blocks(depth + 1)?;
let new_height = self.get_height()?;
Ok(ReorgInfo {
fork_height,
fork_hash,
old_height: start_height,
new_height,
depth,
blocks_invalidated: depth,
blocks_added: (new_height - current_height) as u32,
})
}
/// Generate a test wallet with funded addresses
pub fn generate_test_wallet(
&self,
num_addresses: usize,
) -> Result<Vec<(Address, String)>, BitcoinError> {
let mut wallet = Vec::new();
for _ in 0..num_addresses {
let address = self.client.get_new_address(None)?.assume_checked();
let txid = self.fund_address(&address, None)?;
wallet.push((address, txid));
}
Ok(wallet)
}
/// Reset regtest chain to genesis
pub fn reset_chain(&mut self) -> Result<(), BitcoinError> {
// Get genesis block hash
let genesis_hash = self.client.get_block_hash(0)?.to_string();
// Invalidate all blocks after genesis
let current_height = self.get_height()?;
for height in (1..=current_height).rev() {
let hash = self.client.get_block_hash(height)?.to_string();
self.invalidate_block(&hash)?;
}
// Reconsider genesis
self.reconsider_block(&genesis_hash)?;
// Re-initialize
self.initialize()?;
Ok(())
}
}
/// Information about a blockchain reorganization
#[derive(Debug, Clone)]
pub struct ReorgInfo {
/// Height of the fork point
pub fork_height: u64,
/// Hash of the block at the fork point
pub fork_hash: String,
/// Old chain height before reorg
pub old_height: u64,
/// New chain height after reorg
pub new_height: u64,
/// Depth of the reorg
pub depth: u32,
/// Number of blocks invalidated
pub blocks_invalidated: u32,
/// Number of new blocks added
pub blocks_added: u32,
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_regtest_config_default() {
let config = RegtestConfig::default();
assert_eq!(config.initial_blocks, 101);
assert_eq!(config.confirmation_blocks, 6);
assert!(config.auto_mine);
assert_eq!(config.default_funding_amount, 100_000_000);
}
#[test]
fn test_reorg_info_fields() {
let info = ReorgInfo {
fork_height: 100,
fork_hash: "abc123".to_string(),
old_height: 110,
new_height: 112,
depth: 10,
blocks_invalidated: 10,
blocks_added: 12,
};
assert_eq!(info.fork_height, 100);
assert_eq!(info.depth, 10);
}
}