chia_sdk_bindings/
simulator.rs

1use std::sync::{Arc, Mutex};
2
3use bindy::Result;
4use chia_bls::SecretKey;
5use chia_protocol::{Bytes32, Coin, CoinSpend, CoinState, SpendBundle};
6use chia_sdk_test::SimulatorConfig;
7
8use crate::BlsPairWithCoin;
9
10#[derive(Default, Clone)]
11pub struct Simulator(Arc<Mutex<chia_sdk_test::Simulator>>);
12
13impl Simulator {
14    pub fn new() -> Result<Self> {
15        Ok(Self::default())
16    }
17
18    pub fn with_seed(seed: u64) -> Result<Self> {
19        Ok(Self(Arc::new(Mutex::new(
20            chia_sdk_test::Simulator::with_config(SimulatorConfig {
21                seed,
22                ..Default::default()
23            }),
24        ))))
25    }
26
27    pub fn height(&self) -> Result<u32> {
28        Ok(self.0.lock().unwrap().height())
29    }
30
31    pub fn next_timestamp(&self) -> Result<u64> {
32        Ok(self.0.lock().unwrap().next_timestamp())
33    }
34
35    pub fn header_hash(&self) -> Result<Bytes32> {
36        Ok(self.0.lock().unwrap().header_hash())
37    }
38
39    pub fn header_hash_of(&self, height: u32) -> Result<Option<Bytes32>> {
40        Ok(self.0.lock().unwrap().header_hash_of(height))
41    }
42
43    pub fn insert_coin(&self, coin: Coin) -> Result<()> {
44        self.0.lock().unwrap().insert_coin(coin);
45        Ok(())
46    }
47
48    pub fn new_coin(&self, puzzle_hash: Bytes32, amount: u64) -> Result<Coin> {
49        Ok(self.0.lock().unwrap().new_coin(puzzle_hash, amount))
50    }
51
52    pub fn bls(&self, amount: u64) -> Result<BlsPairWithCoin> {
53        Ok(self.0.lock().unwrap().bls(amount).into())
54    }
55
56    pub fn set_next_timestamp(&self, time: u64) -> Result<()> {
57        self.0.lock().unwrap().set_next_timestamp(time)?;
58        Ok(())
59    }
60
61    pub fn pass_time(&self, time: u64) -> Result<()> {
62        self.0.lock().unwrap().pass_time(time);
63        Ok(())
64    }
65
66    pub fn hint_coin(&self, coin_id: Bytes32, hint: Bytes32) -> Result<()> {
67        self.0.lock().unwrap().hint_coin(coin_id, hint);
68        Ok(())
69    }
70
71    pub fn coin_state(&self, coin_id: Bytes32) -> Result<Option<CoinState>> {
72        Ok(self.0.lock().unwrap().coin_state(coin_id))
73    }
74
75    pub fn children(&self, coin_id: Bytes32) -> Result<Vec<CoinState>> {
76        Ok(self.0.lock().unwrap().children(coin_id))
77    }
78
79    pub fn hinted_coins(&self, hint: Bytes32) -> Result<Vec<Bytes32>> {
80        Ok(self.0.lock().unwrap().hinted_coins(hint))
81    }
82
83    pub fn coin_spend(&self, coin_id: Bytes32) -> Result<Option<CoinSpend>> {
84        Ok(self.0.lock().unwrap().coin_spend(coin_id))
85    }
86
87    pub fn spend_coins(
88        &self,
89        coin_spends: Vec<CoinSpend>,
90        secret_keys: Vec<SecretKey>,
91    ) -> Result<()> {
92        self.0
93            .lock()
94            .unwrap()
95            .spend_coins(coin_spends, &secret_keys)?;
96        Ok(())
97    }
98
99    pub fn new_transaction(&self, spend_bundle: SpendBundle) -> Result<()> {
100        self.0.lock().unwrap().new_transaction(spend_bundle)?;
101        Ok(())
102    }
103
104    pub fn lookup_coin_ids(&self, coin_ids: Vec<Bytes32>) -> Result<Vec<CoinState>> {
105        Ok(self
106            .0
107            .lock()
108            .unwrap()
109            .lookup_coin_ids(&coin_ids.into_iter().collect()))
110    }
111
112    pub fn lookup_puzzle_hashes(
113        &self,
114        puzzle_hashes: Vec<Bytes32>,
115        include_hints: bool,
116    ) -> Result<Vec<CoinState>> {
117        Ok(self
118            .0
119            .lock()
120            .unwrap()
121            .lookup_puzzle_hashes(puzzle_hashes.into_iter().collect(), include_hints))
122    }
123
124    pub fn unspent_coins(&self, puzzle_hash: Bytes32, include_hints: bool) -> Result<Vec<Coin>> {
125        Ok(self
126            .0
127            .lock()
128            .unwrap()
129            .unspent_coins(puzzle_hash, include_hints))
130    }
131
132    pub fn create_block(&self) -> Result<()> {
133        self.0.lock().unwrap().create_block();
134        Ok(())
135    }
136}