-
Notifications
You must be signed in to change notification settings - Fork 178
Expand file tree
/
Copy pathchains_coordinator.rs
More file actions
778 lines (709 loc) · 27.9 KB
/
chains_coordinator.rs
File metadata and controls
778 lines (709 loc) · 27.9 KB
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
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
use super::DevnetEvent;
use crate::deployment::types::DeploymentSpecification;
use crate::deployment::{apply_on_chain_deployment, DeploymentCommand, DeploymentEvent};
use crate::indexer::{chains, Indexer, IndexerConfig};
use crate::integrate::{MempoolAdmissionData, ServiceStatusData, Status};
use crate::types::{self, AccountConfig, ChainConfig, DevnetConfig, ProjectManifest};
use crate::types::{BitcoinChainEvent, ChainsCoordinatorCommand, StacksChainEvent, StacksNetwork};
use crate::utils;
use crate::utils::stacks::{transactions, PoxInfo, StacksRpc};
use base58::FromBase58;
use clarity_repl::clarity::representations::ClarityName;
use clarity_repl::clarity::types::{BuffData, SequenceData, TupleData, Value as ClarityValue};
use clarity_repl::clarity::util::address::AddressHashMode;
use clarity_repl::clarity::util::hash::{hex_bytes, Hash160};
use rocket::config::{Config, LogLevel};
use rocket::serde::json::{json, Json, Value as JsonValue};
use rocket::serde::Deserialize;
use rocket::State;
use std::convert::TryFrom;
use std::error::Error;
use std::net::{IpAddr, Ipv4Addr};
use std::str;
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::mpsc::{channel, Receiver, Sender};
use std::sync::{Arc, Mutex, RwLock};
use tracing::info;
#[derive(Deserialize)]
pub struct NewTransaction {
pub txid: String,
pub status: String,
pub raw_result: String,
pub raw_tx: String,
}
#[derive(Clone, Debug)]
pub struct StacksEventObserverConfig {
pub devnet_config: DevnetConfig,
pub accounts: Vec<AccountConfig>,
pub deployment: DeploymentSpecification,
pub manifest: ProjectManifest,
pub deployment_fee_rate: u64,
}
#[derive(Clone, Debug)]
pub struct DevnetInitializationStatus {
pub should_deploy_protocol: bool,
}
#[derive(Deserialize, Debug)]
pub struct ContractReadonlyCall {
pub okay: bool,
pub result: String,
}
#[derive(Debug, Clone, Deserialize, Serialize)]
/// JSONRPC Request
pub struct BitcoinRPCRequest {
/// The name of the RPC call
pub method: String,
/// Parameters to the RPC call
pub params: serde_json::Value,
/// Identifier for this Request, which should appear in the response
pub id: serde_json::Value,
/// jsonrpc field, MUST be "2.0"
pub jsonrpc: serde_json::Value,
}
impl StacksEventObserverConfig {
pub fn new(
devnet_config: DevnetConfig,
manifest: ProjectManifest,
deployment: DeploymentSpecification,
) -> Self {
info!("Checking contracts...");
let chain_config = ChainConfig::from_manifest_path(&manifest.path, &StacksNetwork::Devnet);
StacksEventObserverConfig {
devnet_config,
accounts: chain_config.accounts.into_values().collect::<Vec<_>>(),
manifest,
deployment,
deployment_fee_rate: chain_config.network.deployment_fee_rate,
}
}
}
pub async fn start_chains_coordinator(
config: StacksEventObserverConfig,
devnet_event_tx: Sender<DevnetEvent>,
chains_coordinator_commands_rx: Receiver<ChainsCoordinatorCommand>,
chains_coordinator_commands_tx: Sender<ChainsCoordinatorCommand>,
) -> Result<(), Box<dyn Error>> {
let indexer = Indexer::new(IndexerConfig {
stacks_node_rpc_url: format!(
"http://localhost:{}",
config.devnet_config.stacks_node_rpc_port
),
bitcoin_node_rpc_url: format!(
"http://localhost:{}",
config.devnet_config.bitcoin_node_rpc_port
),
bitcoin_node_rpc_username: config.devnet_config.bitcoin_node_username.clone(),
bitcoin_node_rpc_password: config.devnet_config.bitcoin_node_password.clone(),
});
let (deployment_events_tx, deployment_events_rx) = channel();
let (deployment_commands_tx, deployments_command_rx) = channel();
prepare_protocol_deployment(
&config.manifest,
&config.deployment,
deployment_events_tx,
deployments_command_rx,
);
let init_status = DevnetInitializationStatus {
should_deploy_protocol: true,
};
let port = config.devnet_config.orchestrator_port;
let config_mutex = Arc::new(Mutex::new(config.clone()));
let init_status_rw_lock = Arc::new(RwLock::new(init_status));
let indexer_rw_lock = Arc::new(RwLock::new(indexer));
let devnet_event_tx_mutex = Arc::new(Mutex::new(devnet_event_tx.clone()));
let background_job_tx_mutex = Arc::new(Mutex::new(chains_coordinator_commands_tx.clone()));
let moved_init_status_rw_lock = init_status_rw_lock.clone();
let rocket_config = Config {
port: port,
workers: 4,
address: IpAddr::V4(Ipv4Addr::new(0, 0, 0, 0)),
keep_alive: 5,
temp_dir: std::env::temp_dir(),
log_level: LogLevel::Debug,
..Config::default()
};
let _ = std::thread::spawn(move || {
let future = rocket::custom(rocket_config)
.manage(indexer_rw_lock)
.manage(devnet_event_tx_mutex)
.manage(config_mutex)
.manage(moved_init_status_rw_lock)
.manage(background_job_tx_mutex)
.mount(
"/",
routes![
handle_ping,
handle_new_bitcoin_block,
handle_new_stacks_block,
handle_new_microblocks,
handle_new_mempool_tx,
handle_drop_mempool_tx,
handle_bitcoin_rpc_call,
],
)
.launch();
let rt = utils::create_basic_runtime();
rt.block_on(future).expect("Unable to spawn event observer");
});
// This loop is used for handling background jobs, emitted by HTTP calls.
let mut should_deploy_protocol = true;
let mut protocol_deployed = false;
let stop_miner = Arc::new(AtomicBool::new(false));
let mut deployment_events_rx = Some(deployment_events_rx);
loop {
match chains_coordinator_commands_rx.recv() {
Ok(ChainsCoordinatorCommand::Terminate(true)) => {
devnet_event_tx
.send(DevnetEvent::info("Terminating event observer".into()))
.expect("Unable to terminate event observer");
break;
}
Ok(ChainsCoordinatorCommand::Terminate(false)) => {
// Will be removed via https://github.com/hirosystems/clarinet/pull/340
unreachable!();
}
Ok(ChainsCoordinatorCommand::PublishInitialContracts) => {
if should_deploy_protocol {
should_deploy_protocol = false;
if let Ok(mut init_status) = init_status_rw_lock.write() {
init_status.should_deploy_protocol = false;
}
if let Some(deployment_events_rx) = deployment_events_rx.take() {
perform_protocol_deployment(
deployment_events_rx,
&deployment_commands_tx,
&devnet_event_tx,
&chains_coordinator_commands_tx,
)
}
}
}
Ok(ChainsCoordinatorCommand::ProtocolDeployed) => {
should_deploy_protocol = false;
protocol_deployed = true;
if !config.devnet_config.bitcoin_controller_automining_disabled {
let _ = chains_coordinator_commands_tx
.send(ChainsCoordinatorCommand::StartAutoMining);
}
}
Ok(ChainsCoordinatorCommand::BitcoinOpSent) => {
if !protocol_deployed {
std::thread::sleep(std::time::Duration::from_secs(1));
mine_bitcoin_block(
config.devnet_config.bitcoin_node_rpc_port,
&config.devnet_config.bitcoin_node_username,
&config.devnet_config.bitcoin_node_password,
&config.devnet_config.miner_btc_address,
);
}
}
Ok(ChainsCoordinatorCommand::StartAutoMining) => {
stop_miner.store(false, Ordering::SeqCst);
let stop_miner_reader = stop_miner.clone();
let devnet_config = config.devnet_config.clone();
std::thread::spawn(move || loop {
std::thread::sleep(std::time::Duration::from_millis(
devnet_config.bitcoin_controller_block_time.into(),
));
mine_bitcoin_block(
devnet_config.bitcoin_node_rpc_port,
&devnet_config.bitcoin_node_username,
&devnet_config.bitcoin_node_password,
&devnet_config.miner_btc_address,
);
if stop_miner_reader.load(Ordering::SeqCst) {
break;
}
});
}
Ok(ChainsCoordinatorCommand::StopAutoMining) => {
stop_miner.store(true, Ordering::SeqCst);
}
Ok(ChainsCoordinatorCommand::MineBitcoinBlock) => {
mine_bitcoin_block(
config.devnet_config.bitcoin_node_rpc_port,
config.devnet_config.bitcoin_node_username.as_str(),
&config.devnet_config.bitcoin_node_password.as_str(),
&config.devnet_config.miner_btc_address.as_str(),
);
}
Ok(ChainsCoordinatorCommand::InvalidateBitcoinChainTip) => {
invalidate_bitcoin_chain_tip(
config.devnet_config.bitcoin_node_rpc_port,
config.devnet_config.bitcoin_node_username.as_str(),
&config.devnet_config.bitcoin_node_password.as_str(),
);
}
Ok(ChainsCoordinatorCommand::PublishPoxStackingOrders(block_identifier)) => {
let bitcoin_block_height = block_identifier.index;
let res = publish_stacking_orders(
&config.devnet_config,
&config.accounts,
config.deployment_fee_rate,
bitcoin_block_height as u32,
)
.await;
if let Some(tx_count) = res {
let _ = devnet_event_tx.send(DevnetEvent::success(format!(
"Will broadcast {} stacking orders",
tx_count
)));
}
}
Err(_) => {
break;
}
}
}
Ok(())
}
#[post("/new_burn_block", format = "json", data = "<marshalled_block>")]
pub fn handle_new_bitcoin_block(
indexer_rw_lock: &State<Arc<RwLock<Indexer>>>,
devnet_events_tx: &State<Arc<Mutex<Sender<DevnetEvent>>>>,
marshalled_block: Json<JsonValue>,
) -> Json<JsonValue> {
let devnet_events_tx = devnet_events_tx.inner();
// Standardize the structure of the block, and identify the
// kind of update that this new block would imply, taking
// into account the last 7 blocks.
let chain_update = match indexer_rw_lock.inner().write() {
Ok(mut indexer) => indexer.handle_bitcoin_block(marshalled_block.into_inner()),
_ => {
return Json(json!({
"status": 200,
"result": "Ok",
}))
}
};
// Contextual shortcut: Devnet is an environment under control,
// with 1 miner. As such we will ignore Reorgs handling.
let (log, status) = match &chain_update {
BitcoinChainEvent::ChainUpdatedWithBlock(block) => {
let log = format!("Bitcoin block #{} received", block.block_identifier.index);
let status = format!(
"mining blocks (chaintip = #{})",
block.block_identifier.index
);
(log, status)
}
BitcoinChainEvent::ChainUpdatedWithReorg(_old_blocks, new_blocks) => {
let tip = new_blocks.last().unwrap();
let log = format!(
"Bitcoin reorg received (new height: {})",
tip.block_identifier.index
);
let status = format!("mining blocks (chaintip = #{})", tip.block_identifier.index);
(log, status)
}
};
match devnet_events_tx.lock() {
Ok(tx) => {
let _ = tx.send(DevnetEvent::debug(log));
let _ = tx.send(DevnetEvent::ServiceStatus(ServiceStatusData {
order: 0,
status: Status::Green,
name: "bitcoin-node".into(),
comment: status,
}));
let _ = tx.send(DevnetEvent::BitcoinChainEvent(chain_update));
}
_ => {}
};
Json(json!({
"status": 200,
"result": "Ok",
}))
}
#[post("/new_block", format = "application/json", data = "<marshalled_block>")]
pub fn handle_new_stacks_block(
indexer_rw_lock: &State<Arc<RwLock<Indexer>>>,
devnet_events_tx: &State<Arc<Mutex<Sender<DevnetEvent>>>>,
init_status: &State<Arc<RwLock<DevnetInitializationStatus>>>,
background_job_tx_mutex: &State<Arc<Mutex<Sender<ChainsCoordinatorCommand>>>>,
marshalled_block: Json<JsonValue>,
) -> Json<JsonValue> {
// A few things need to be done:
// - Contracts deployment orchestration during the first blocks (max: 25 / block)
// - PoX stacking order orchestration: enable PoX with some "stack-stx" transactions
// defined in the devnet file config.
if let Ok(init_status_writer) = init_status.inner().read() {
if init_status_writer.should_deploy_protocol {
if let Ok(background_job_tx) = background_job_tx_mutex.lock() {
let _ = background_job_tx.send(ChainsCoordinatorCommand::PublishInitialContracts);
}
}
}
// Standardize the structure of the block, and identify the
// kind of update that this new block would imply, taking
// into account the last 7 blocks.
let (pox_info, chain_event) = match indexer_rw_lock.inner().write() {
Ok(mut indexer) => {
let pox_info = indexer.get_pox_info();
let chain_event = indexer.handle_stacks_block(marshalled_block.into_inner());
(pox_info, chain_event)
}
_ => {
return Json(json!({
"status": 200,
"result": "Ok",
}))
}
};
// Contextual: Devnet is an environment under control,
// with 1 miner. As such we will ignore Reorgs handling.
let update = match &chain_event {
StacksChainEvent::ChainUpdatedWithBlock(block) => block.clone(),
StacksChainEvent::ChainUpdatedWithMicroblock(_) => {
unreachable!() // TODO(lgalabru): good enough for now - code path unreachable in the context of Devnet
}
StacksChainEvent::ChainUpdatedWithMicroblockReorg(_) => {
unreachable!() // TODO(lgalabru): good enough for now - code path unreachable in the context of Devnet
}
StacksChainEvent::ChainUpdatedWithReorg(_) => {
unreachable!() // TODO(lgalabru): good enough for now - code path unreachable in the context of Devnet
}
};
// Partially update the UI. With current approach a full update
// would requires either cloning the block, or passing ownership.
let devnet_events_tx = devnet_events_tx.inner();
if let Ok(tx) = devnet_events_tx.lock() {
let _ = tx.send(DevnetEvent::ServiceStatus(ServiceStatusData {
order: 1,
status: Status::Green,
name: "stacks-node".into(),
comment: format!(
"mining blocks (chaintip = #{})",
update.new_block.block_identifier.index
),
}));
let _ = tx.send(DevnetEvent::info(format!(
"Block #{} anchored in Bitcoin block #{} includes {} transactions",
update.new_block.block_identifier.index,
update
.new_block
.metadata
.bitcoin_anchor_block_identifier
.index,
update.new_block.transactions.len(),
)));
}
// Every penultimate block, we check if some stacking orders should be submitted before the next
// cycle starts.
let pox_cycle_length: u32 =
pox_info.prepare_phase_block_length + pox_info.reward_phase_block_length;
let should_submit_pox_orders =
update.new_block.metadata.pox_cycle_position == (pox_cycle_length - 2);
if should_submit_pox_orders {
if let Ok(background_job_tx) = background_job_tx_mutex.lock() {
let _ = background_job_tx.send(ChainsCoordinatorCommand::PublishPoxStackingOrders(
update
.new_block
.metadata
.bitcoin_anchor_block_identifier
.clone(),
));
}
}
if let Ok(tx) = devnet_events_tx.lock() {
let _ = tx.send(DevnetEvent::StacksChainEvent(chain_event));
}
Json(json!({
"status": 200,
"result": "Ok",
}))
}
#[post(
"/new_microblocks",
format = "application/json",
data = "<marshalled_microblock>"
)]
pub fn handle_new_microblocks(
_config: &State<Arc<Mutex<StacksEventObserverConfig>>>,
indexer_rw_lock: &State<Arc<RwLock<Indexer>>>,
devnet_events_tx: &State<Arc<Mutex<Sender<DevnetEvent>>>>,
marshalled_microblock: Json<JsonValue>,
) -> Json<JsonValue> {
let devnet_events_tx = devnet_events_tx.inner();
// Standardize the structure of the microblock, and identify the
// kind of update that this new microblock would imply
let chain_event = match indexer_rw_lock.inner().write() {
Ok(mut indexer) => {
let chain_event = indexer.handle_stacks_microblock(marshalled_microblock.into_inner());
chain_event
}
_ => {
return Json(json!({
"status": 200,
"result": "Ok",
}))
}
};
if let Ok(tx) = devnet_events_tx.lock() {
if let StacksChainEvent::ChainUpdatedWithMicroblock(ref update) = chain_event {
if let Some(microblock) = update.current_trail.microblocks.last() {
let _ = tx.send(DevnetEvent::info(format!(
"Microblock received including {} transactions",
microblock.transactions.len(),
)));
}
}
let _ = tx.send(DevnetEvent::StacksChainEvent(chain_event));
}
Json(json!({
"status": 200,
"result": "Ok",
}))
}
#[post("/new_mempool_tx", format = "application/json", data = "<raw_txs>")]
pub fn handle_new_mempool_tx(
devnet_events_tx: &State<Arc<Mutex<Sender<DevnetEvent>>>>,
raw_txs: Json<Vec<String>>,
) -> Json<JsonValue> {
let decoded_transactions = raw_txs
.iter()
.map(|t| {
let (txid, ..) =
chains::stacks::get_tx_description(t).expect("unable to parse transaction");
txid
})
.collect::<Vec<String>>();
if let Ok(tx_sender) = devnet_events_tx.lock() {
for tx in decoded_transactions.into_iter() {
let _ = tx_sender.send(DevnetEvent::MempoolAdmission(MempoolAdmissionData { tx }));
}
}
Json(json!({
"status": 200,
"result": "Ok",
}))
}
#[post("/drop_mempool_tx", format = "application/json")]
pub fn handle_drop_mempool_tx() -> Json<JsonValue> {
Json(json!({
"status": 200,
"result": "Ok",
}))
}
#[get("/ping", format = "application/json")]
pub fn handle_ping() -> Json<JsonValue> {
Json(json!({
"status": 200,
"result": "Ok",
}))
}
#[post("/", format = "application/json", data = "<bitcoin_rpc_call>")]
pub async fn handle_bitcoin_rpc_call(
config: &State<Arc<Mutex<StacksEventObserverConfig>>>,
_devnet_events_tx: &State<Arc<Mutex<Sender<DevnetEvent>>>>,
background_job_tx_mutex: &State<Arc<Mutex<Sender<ChainsCoordinatorCommand>>>>,
bitcoin_rpc_call: Json<BitcoinRPCRequest>,
) -> Json<JsonValue> {
use base64::encode;
use reqwest::Client;
let bitcoin_rpc_call = bitcoin_rpc_call.into_inner().clone();
let method = bitcoin_rpc_call.method.clone();
let body = rocket::serde::json::serde_json::to_vec(&bitcoin_rpc_call).unwrap();
let builder = match config.inner().lock() {
Ok(config) => {
let token = encode(format!(
"{}:{}",
config.devnet_config.bitcoin_node_username,
config.devnet_config.bitcoin_node_password
));
let client = Client::new();
client
.post(format!(
"http://localhost:{}/",
config.devnet_config.bitcoin_node_rpc_port
))
.header("Content-Type", "application/json")
.header("Authorization", format!("Basic {}", token))
}
_ => unreachable!(),
};
if method == "sendrawtransaction" {
if let Ok(background_job_tx) = background_job_tx_mutex.lock() {
let _ = background_job_tx.send(ChainsCoordinatorCommand::BitcoinOpSent);
}
}
let res = builder.body(body).send().await.unwrap();
Json(res.json().await.unwrap())
}
pub fn prepare_protocol_deployment(
manifest: &ProjectManifest,
deployment: &DeploymentSpecification,
deployment_event_tx: Sender<DeploymentEvent>,
deployment_command_rx: Receiver<DeploymentCommand>,
) {
let manifest = manifest.clone();
let deployment = deployment.clone();
std::thread::spawn(move || {
apply_on_chain_deployment(
&manifest,
deployment,
deployment_event_tx,
deployment_command_rx,
false,
);
});
}
pub fn perform_protocol_deployment(
deployment_events_rx: Receiver<DeploymentEvent>,
deployment_commands_tx: &Sender<DeploymentCommand>,
devnet_event_tx: &Sender<DevnetEvent>,
chains_coordinator_commands_tx: &Sender<ChainsCoordinatorCommand>,
) {
let devnet_event_tx = devnet_event_tx.clone();
let chains_coordinator_commands_tx = chains_coordinator_commands_tx.clone();
let _ = deployment_commands_tx.send(DeploymentCommand::Start);
std::thread::spawn(move || {
loop {
let event = match deployment_events_rx.recv() {
Ok(event) => event,
Err(_e) => break,
};
match event {
DeploymentEvent::ContractUpdate(_) => {}
DeploymentEvent::Interrupted(_) => {
// Terminate
break;
}
DeploymentEvent::ProtocolDeployed => {
let _ = chains_coordinator_commands_tx
.send(ChainsCoordinatorCommand::ProtocolDeployed);
let _ = devnet_event_tx.send(DevnetEvent::ProtocolDeployed);
break;
}
}
}
});
}
pub async fn publish_stacking_orders(
devnet_config: &DevnetConfig,
accounts: &Vec<AccountConfig>,
fee_rate: u64,
bitcoin_block_height: u32,
) -> Option<usize> {
if devnet_config.pox_stacking_orders.len() == 0 {
return None;
}
let stacks_node_rpc_url = format!("http://localhost:{}", devnet_config.stacks_node_rpc_port);
let mut transactions = 0;
let pox_info: PoxInfo = reqwest::get(format!("{}/v2/pox", stacks_node_rpc_url))
.await
.expect("Unable to retrieve pox info")
.json()
.await
.expect("Unable to parse contract");
for pox_stacking_order in devnet_config.pox_stacking_orders.iter() {
if pox_stacking_order.start_at_cycle == (pox_info.reward_cycle_id + 1) {
let mut account = None;
let mut accounts_iter = accounts.iter();
while let Some(e) = accounts_iter.next() {
if e.label == pox_stacking_order.wallet {
account = Some(e.clone());
break;
}
}
let account = match account {
Some(account) => account,
_ => continue,
};
transactions += 1;
let stx_amount = pox_info.next_cycle.min_threshold_ustx * pox_stacking_order.slots;
let addr_bytes = pox_stacking_order
.btc_address
.from_base58()
.expect("Unable to get bytes from btc address");
let duration = pox_stacking_order.duration.into();
let node_url = stacks_node_rpc_url.clone();
let pox_contract_id = pox_info.contract_id.clone();
std::thread::spawn(move || {
let default_fee = fee_rate * 1000;
let stacks_rpc = StacksRpc::new(&node_url);
let nonce = stacks_rpc
.get_nonce(&account.address)
.expect("Unable to retrieve nonce");
let (_, _, account_secret_key) =
types::compute_addresses(&account.mnemonic, &account.derivation, false);
let addr_bytes = Hash160::from_bytes(&addr_bytes[1..21]).unwrap();
let addr_version = AddressHashMode::SerializeP2PKH;
let stack_stx_tx = transactions::build_contrat_call_transaction(
pox_contract_id,
"stack-stx".into(),
vec![
ClarityValue::UInt(stx_amount.into()),
ClarityValue::Tuple(
TupleData::from_data(vec![
(
ClarityName::try_from("version".to_owned()).unwrap(),
ClarityValue::buff_from_byte(addr_version as u8),
),
(
ClarityName::try_from("hashbytes".to_owned()).unwrap(),
ClarityValue::Sequence(SequenceData::Buffer(BuffData {
data: addr_bytes.as_bytes().to_vec(),
})),
),
])
.unwrap(),
),
ClarityValue::UInt((bitcoin_block_height - 1).into()),
ClarityValue::UInt(duration),
],
nonce,
default_fee,
&hex_bytes(&account_secret_key).unwrap(),
);
let _ = stacks_rpc
.post_transaction(stack_stx_tx)
.expect("Unable to broadcast transaction");
});
}
}
if transactions > 0 {
Some(transactions)
} else {
None
}
}
pub fn invalidate_bitcoin_chain_tip(
bitcoin_node_rpc_port: u16,
bitcoin_node_username: &str,
bitcoin_node_password: &str,
) {
use bitcoincore_rpc::{Auth, Client, RpcApi};
let rpc = Client::new(
&format!("http://localhost:{}", bitcoin_node_rpc_port),
Auth::UserPass(
bitcoin_node_username.to_string(),
bitcoin_node_password.to_string(),
),
)
.unwrap();
let chain_tip = rpc.get_best_block_hash().expect("Unable to get chain tip");
let _ = rpc
.invalidate_block(&chain_tip)
.expect("Unable to invalidate chain tip");
}
pub fn mine_bitcoin_block(
bitcoin_node_rpc_port: u16,
bitcoin_node_username: &str,
bitcoin_node_password: &str,
miner_btc_address: &str,
) {
use bitcoincore_rpc::bitcoin::Address;
use bitcoincore_rpc::{Auth, Client, RpcApi};
use std::str::FromStr;
let rpc = Client::new(
&format!("http://localhost:{}", bitcoin_node_rpc_port),
Auth::UserPass(
bitcoin_node_username.to_string(),
bitcoin_node_password.to_string(),
),
)
.unwrap();
let miner_address = Address::from_str(miner_btc_address).unwrap();
let _ = rpc.generate_to_address(1, &miner_address);
}