11use clarinet_lib:: integrate:: { self , BlockData , DevnetEvent , DevnetOrchestrator } ;
2- use clarinet_lib:: types:: DevnetConfigFile ;
2+ use clarinet_lib:: types:: { AccountConfig , DevnetConfigFile , PoxStackingOrder , DEFAULT_DERIVATION_PATH , compute_addresses} ;
3+ use clarinet_lib:: bip39:: { Language , Mnemonic } ;
34use neon:: prelude:: * ;
45use core:: panic;
6+ use std:: collections:: BTreeMap ;
57use std:: path:: PathBuf ;
68use std:: sync:: mpsc;
79use std:: thread;
@@ -23,7 +25,7 @@ enum DevnetCommand {
2325impl Finalize for StacksDevnet { }
2426
2527impl StacksDevnet {
26- fn new < ' a , C > ( cx : & mut C , manifest_path : String ) -> Self
28+ fn new < ' a , C > ( cx : & mut C , manifest_path : String , logs_enabled : bool , _accounts : BTreeMap < String , AccountConfig > , devnet_overrides : DevnetConfigFile ) -> Self
2729 where
2830 C : Context < ' a > ,
2931 {
@@ -37,7 +39,6 @@ impl StacksDevnet {
3739
3840 thread:: spawn ( move || {
3941 let manifest_path = get_manifest_path_or_exit ( Some ( manifest_path. into ( ) ) ) ;
40- let devnet_overrides = DevnetConfigFile :: default ( ) ;
4142 let devnet = DevnetOrchestrator :: new ( manifest_path, Some ( devnet_overrides) ) ;
4243
4344 if let Ok ( DevnetCommand :: Start ( callback) ) = rx. recv ( ) {
@@ -81,7 +82,9 @@ impl StacksDevnet {
8182 stacks_block_tx. send ( block. clone ( ) ) . expect ( "Unable to transmit stacks block" ) ;
8283 }
8384 DevnetEvent :: Log ( log) => {
84- println ! ( "{:?}" , log) ;
85+ if logs_enabled {
86+ println ! ( "{:?}" , log) ;
87+ }
8588 }
8689 _ => { }
8790 }
@@ -111,8 +114,248 @@ impl StacksDevnet {
111114impl StacksDevnet {
112115 fn js_new ( mut cx : FunctionContext ) -> JsResult < JsBox < StacksDevnet > > {
113116 let manifest_path = cx. argument :: < JsString > ( 0 ) ?. value ( & mut cx) ;
117+
118+ let logs_enabled = cx. argument :: < JsBoolean > ( 1 ) ?. value ( & mut cx) ;
119+
120+ let accounts = cx. argument :: < JsArray > ( 2 ) ?. to_vec ( & mut cx) ?;
121+
122+ let devnet_settings = cx. argument :: < JsObject > ( 3 ) ?;
123+
124+ let mut genesis_accounts = BTreeMap :: new ( ) ;
125+
126+ for account in accounts. iter ( ) {
127+ let account_settings = account. downcast_or_throw :: < JsObject , _ > ( & mut cx) ?;
128+ let id = account_settings
129+ . get ( & mut cx, "id" ) ?
130+ . downcast_or_throw :: < JsString , _ > ( & mut cx) ?
131+ . value ( & mut cx) ;
132+
133+ let words = account_settings
134+ . get ( & mut cx, "mnemonic" ) ?
135+ . downcast_or_throw :: < JsString , _ > ( & mut cx) ?
136+ . value ( & mut cx) ;
137+
138+ let mnemonic = Mnemonic :: parse_in_normalized ( Language :: English , & words)
139+ . unwrap ( )
140+ . to_string ( ) ;
141+
142+ let balance = match account_settings. get ( & mut cx, "balance" ) ?. downcast :: < JsNumber , _ > ( & mut cx) {
143+ Ok ( res) => res. value ( & mut cx) ,
144+ _ => 0.0 ,
145+ } ;
146+
147+ let is_mainnet = match account_settings. get ( & mut cx, "is_mainnet" ) ?. downcast :: < JsBoolean , _ > ( & mut cx) {
148+ Ok ( res) => res. value ( & mut cx) ,
149+ _ => false ,
150+ } ;
151+
152+ let derivation = match account_settings. get ( & mut cx, "derivation" ) ?. downcast :: < JsString , _ > ( & mut cx) {
153+ Ok ( res) => res. value ( & mut cx) ,
154+ _ => DEFAULT_DERIVATION_PATH . to_string ( ) ,
155+ } ;
156+
157+ let ( address, _, _) =
158+ compute_addresses ( & mnemonic, & derivation, is_mainnet) ;
159+
160+ let account = AccountConfig {
161+ mnemonic,
162+ address,
163+ derivation,
164+ is_mainnet,
165+ balance : balance as u64 ,
166+ } ;
167+ genesis_accounts. insert ( id, account) ;
168+ }
169+
170+ let mut overrides = DevnetConfigFile :: default ( ) ;
171+
172+ if let Ok ( res) = devnet_settings. get ( & mut cx, "orchestrator_port" ) ?. downcast :: < JsNumber , _ > ( & mut cx) {
173+ overrides. orchestrator_port = Some ( res. value ( & mut cx) as u16 ) ;
174+ }
175+
176+ if let Ok ( res) = devnet_settings. get ( & mut cx, "bitcoin_node_p2p_port" ) ?. downcast :: < JsNumber , _ > ( & mut cx) {
177+ overrides. bitcoin_node_p2p_port = Some ( res. value ( & mut cx) as u16 ) ;
178+ }
179+
180+ if let Ok ( res) = devnet_settings. get ( & mut cx, "bitcoin_node_rpc_port" ) ?. downcast :: < JsNumber , _ > ( & mut cx) {
181+ overrides. bitcoin_node_rpc_port = Some ( res. value ( & mut cx) as u16 ) ;
182+ }
183+
184+ if let Ok ( res) = devnet_settings. get ( & mut cx, "stacks_node_p2p_port" ) ?. downcast :: < JsNumber , _ > ( & mut cx) {
185+ overrides. stacks_node_p2p_port = Some ( res. value ( & mut cx) as u16 ) ;
186+ }
187+
188+ if let Ok ( res) = devnet_settings. get ( & mut cx, "stacks_node_rpc_port" ) ?. downcast :: < JsNumber , _ > ( & mut cx) {
189+ overrides. stacks_node_rpc_port = Some ( res. value ( & mut cx) as u16 ) ;
190+ }
191+
192+ if let Ok ( res) = devnet_settings. get ( & mut cx, "stacks_api_port" ) ?. downcast :: < JsNumber , _ > ( & mut cx) {
193+ overrides. stacks_api_port = Some ( res. value ( & mut cx) as u16 ) ;
194+ }
195+
196+ if let Ok ( res) = devnet_settings. get ( & mut cx, "stacks_api_events_port" ) ?. downcast :: < JsNumber , _ > ( & mut cx) {
197+ overrides. stacks_api_events_port = Some ( res. value ( & mut cx) as u16 ) ;
198+ }
199+
200+ if let Ok ( res) = devnet_settings. get ( & mut cx, "bitcoin_explorer_port" ) ?. downcast :: < JsNumber , _ > ( & mut cx) {
201+ overrides. bitcoin_explorer_port = Some ( res. value ( & mut cx) as u16 ) ;
202+ }
203+
204+ if let Ok ( res) = devnet_settings. get ( & mut cx, "stacks_explorer_port" ) ?. downcast :: < JsNumber , _ > ( & mut cx) {
205+ overrides. stacks_explorer_port = Some ( res. value ( & mut cx) as u16 ) ;
206+ }
207+
208+ if let Ok ( res) = devnet_settings. get ( & mut cx, "bitcoin_controller_port" ) ?. downcast :: < JsNumber , _ > ( & mut cx) {
209+ overrides. bitcoin_controller_port = Some ( res. value ( & mut cx) as u16 ) ;
210+ }
211+
212+ if let Ok ( res) = devnet_settings. get ( & mut cx, "bitcoin_node_username" ) ?. downcast :: < JsString , _ > ( & mut cx) {
213+ overrides. bitcoin_node_username = Some ( res. value ( & mut cx) ) ;
214+ }
215+
216+ if let Ok ( res) = devnet_settings. get ( & mut cx, "bitcoin_node_password" ) ?. downcast :: < JsString , _ > ( & mut cx) {
217+ overrides. bitcoin_node_password = Some ( res. value ( & mut cx) ) ;
218+ }
219+
220+ if let Ok ( res) = devnet_settings. get ( & mut cx, "miner_mnemonic" ) ?. downcast :: < JsString , _ > ( & mut cx) {
221+ overrides. miner_mnemonic = Some ( res. value ( & mut cx) ) ;
222+ }
223+
224+ if let Ok ( res) = devnet_settings. get ( & mut cx, "miner_derivation_path" ) ?. downcast :: < JsString , _ > ( & mut cx) {
225+ overrides. miner_derivation_path = Some ( res. value ( & mut cx) ) ;
226+ }
227+
228+ if let Ok ( res) = devnet_settings. get ( & mut cx, "bitcoin_controller_block_time" ) ?. downcast :: < JsNumber , _ > ( & mut cx) {
229+ overrides. bitcoin_controller_block_time = Some ( res. value ( & mut cx) as u32 ) ;
230+ }
231+
232+ if let Ok ( res) = devnet_settings. get ( & mut cx, "working_dir" ) ?. downcast :: < JsString , _ > ( & mut cx) {
233+ overrides. working_dir = Some ( res. value ( & mut cx) ) ;
234+ }
235+
236+ if let Ok ( res) = devnet_settings. get ( & mut cx, "postgres_port" ) ?. downcast :: < JsNumber , _ > ( & mut cx) {
237+ overrides. postgres_port = Some ( res. value ( & mut cx) as u16 ) ;
238+ }
239+
240+ if let Ok ( res) = devnet_settings. get ( & mut cx, "postgres_username" ) ?. downcast :: < JsString , _ > ( & mut cx) {
241+ overrides. postgres_username = Some ( res. value ( & mut cx) ) ;
242+ }
243+
244+ if let Ok ( res) = devnet_settings. get ( & mut cx, "postgres_password" ) ?. downcast :: < JsString , _ > ( & mut cx) {
245+ overrides. postgres_password = Some ( res. value ( & mut cx) ) ;
246+ }
247+
248+ if let Ok ( res) = devnet_settings. get ( & mut cx, "postgres_database" ) ?. downcast :: < JsString , _ > ( & mut cx) {
249+ overrides. postgres_database = Some ( res. value ( & mut cx) ) ;
250+ }
251+
252+ if let Ok ( res) = devnet_settings. get ( & mut cx, "bitcoin_node_image_url" ) ?. downcast :: < JsString , _ > ( & mut cx) {
253+ overrides. bitcoin_node_image_url = Some ( res. value ( & mut cx) ) ;
254+ }
255+
256+ if let Ok ( res) = devnet_settings. get ( & mut cx, "bitcoin_explorer_image_url" ) ?. downcast :: < JsString , _ > ( & mut cx) {
257+ overrides. bitcoin_explorer_image_url = Some ( res. value ( & mut cx) ) ;
258+ }
259+
260+ if let Ok ( res) = devnet_settings. get ( & mut cx, "stacks_node_image_url" ) ?. downcast :: < JsString , _ > ( & mut cx) {
261+ overrides. stacks_node_image_url = Some ( res. value ( & mut cx) ) ;
262+ }
263+
264+ if let Ok ( res) = devnet_settings. get ( & mut cx, "stacks_api_image_url" ) ?. downcast :: < JsString , _ > ( & mut cx) {
265+ overrides. stacks_api_image_url = Some ( res. value ( & mut cx) ) ;
266+ }
267+
268+ if let Ok ( res) = devnet_settings. get ( & mut cx, "stacks_explorer_image_url" ) ?. downcast :: < JsString , _ > ( & mut cx) {
269+ overrides. stacks_explorer_image_url = Some ( res. value ( & mut cx) ) ;
270+ }
271+
272+ if let Ok ( res) = devnet_settings. get ( & mut cx, "postgres_image_url" ) ?. downcast :: < JsString , _ > ( & mut cx) {
273+ overrides. postgres_image_url = Some ( res. value ( & mut cx) ) ;
274+ }
275+
276+ // Disable scripts
277+ overrides. execute_script = Some ( vec ! [ ] ) ;
278+
279+ // Disable bitcoin_explorer, stacks_explorer and stacks_api by default:
280+ if let Ok ( res) = devnet_settings. get ( & mut cx, "disable_bitcoin_explorer" ) ?. downcast :: < JsBoolean , _ > ( & mut cx) {
281+ overrides. disable_bitcoin_explorer = Some ( res. value ( & mut cx) ) ;
282+ } else {
283+ overrides. disable_bitcoin_explorer = Some ( true ) ;
284+ }
285+
286+ if let Ok ( res) = devnet_settings. get ( & mut cx, "disable_stacks_explorer" ) ?. downcast :: < JsBoolean , _ > ( & mut cx) {
287+ overrides. disable_stacks_explorer = Some ( res. value ( & mut cx) ) ;
288+ } else {
289+ overrides. disable_stacks_explorer = Some ( true ) ;
290+ }
291+
292+ if let Ok ( res) = devnet_settings. get ( & mut cx, "disable_stacks_api" ) ?. downcast :: < JsBoolean , _ > ( & mut cx) {
293+ overrides. disable_stacks_api = Some ( res. value ( & mut cx) ) ;
294+ } else {
295+ overrides. disable_stacks_api = Some ( true ) ;
296+ }
297+
298+ // Retrieve stacks_node_events_observers
299+ if let Ok ( res) = devnet_settings. get ( & mut cx, "stacks_node_events_observers" ) ?. downcast :: < JsArray , _ > ( & mut cx) {
300+ let raw_events_observers = res. to_vec ( & mut cx) ?;
301+ let mut events_observers = vec ! [ ] ;
302+
303+ for raw_events_observer in raw_events_observers. iter ( ) {
304+ let observer_url = raw_events_observer
305+ . downcast_or_throw :: < JsString , _ > ( & mut cx) ?
306+ . value ( & mut cx) ;
307+ events_observers. push ( observer_url) ;
308+ }
309+ overrides. stacks_node_events_observers = Some ( events_observers) ;
310+ }
311+
312+ // Retrieve stacking_orders
313+ if let Ok ( res) = devnet_settings. get ( & mut cx, "pox_stacking_orders" ) ?. downcast :: < JsArray , _ > ( & mut cx) {
314+ let raw_stacking_orders = res. to_vec ( & mut cx) ?;
315+ let mut stacking_orders = vec ! [ ] ;
316+
317+ for raw_stacking_order in raw_stacking_orders. iter ( ) {
318+ let order_settings = raw_stacking_order. downcast_or_throw :: < JsObject , _ > ( & mut cx) ?;
319+
320+ let start_at_cycle = order_settings
321+ . get ( & mut cx, "start_at_cycle" ) ?
322+ . downcast_or_throw :: < JsNumber , _ > ( & mut cx) ?
323+ . value ( & mut cx) as u32 ;
324+
325+ let duration = order_settings
326+ . get ( & mut cx, "duration" ) ?
327+ . downcast_or_throw :: < JsNumber , _ > ( & mut cx) ?
328+ . value ( & mut cx) as u32 ;
329+
330+ let wallet = order_settings
331+ . get ( & mut cx, "wallet" ) ?
332+ . downcast_or_throw :: < JsString , _ > ( & mut cx) ?
333+ . value ( & mut cx) ;
334+
335+ let slots = order_settings
336+ . get ( & mut cx, "slots" ) ?
337+ . downcast_or_throw :: < JsNumber , _ > ( & mut cx) ?
338+ . value ( & mut cx) as u64 ;
339+
340+ let btc_address = order_settings
341+ . get ( & mut cx, "btc_address" ) ?
342+ . downcast_or_throw :: < JsString , _ > ( & mut cx) ?
343+ . value ( & mut cx) ;
344+
345+ stacking_orders. push ( PoxStackingOrder {
346+ start_at_cycle,
347+ duration,
348+ wallet,
349+ slots,
350+ btc_address,
351+ } ) ;
352+ }
353+ overrides. pox_stacking_orders = Some ( stacking_orders) ;
354+ }
114355
115- let devnet = StacksDevnet :: new ( & mut cx, manifest_path) ;
356+ println ! ( "{:?}" , overrides) ;
357+
358+ let devnet = StacksDevnet :: new ( & mut cx, manifest_path, logs_enabled, genesis_accounts, overrides) ;
116359 Ok ( cx. boxed ( devnet) )
117360 }
118361
0 commit comments