-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathasync_store.rs
More file actions
307 lines (275 loc) · 10.8 KB
/
async_store.rs
File metadata and controls
307 lines (275 loc) · 10.8 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
//! [`Store`] provides async read and write methods of persisting BDK change sets by way of [`sqlx`].
use std::str::FromStr;
use std::sync::Arc;
use bdk_chain::{
BlockId, ConfirmationBlockTime, DescriptorId, bitcoin, keychain_txout, local_chain, tx_graph,
};
use bitcoin::{Amount, BlockHash, OutPoint, ScriptBuf, Transaction, TxOut, Txid, consensus};
use sqlx::{
Row,
sqlite::{SqliteConnectOptions, SqlitePool as Pool},
};
use crate::Error;
/// Store.
#[derive(Debug, Clone)]
pub struct Store {
/// Pool.
pub(crate) pool: Pool,
}
impl Store {
/// New in memory.
pub async fn new_memory() -> Result<Self, Error> {
let mut options = sqlx::sqlite::SqlitePoolOptions::new();
// Don't test the health of the connection before returning it.
// See docs for `Pool::acquire`.
options = options.test_before_acquire(false);
let pool = options.connect("sqlite::memory:").await?;
Ok(Self { pool })
}
/// Create a new [`Store`] instance.
///
/// This will create a new database at the given path if it doesn't exist.
///
/// Note that `path` can be a filename, e.g. `foo.db` or a standard URL,
/// e.g. `sqlite://foo.db`.
pub async fn new(path: &str) -> Result<Self, Error> {
let options = SqliteConnectOptions::from_str(path)?.create_if_missing(true);
let pool = Pool::connect_with(options).await?;
Ok(Self { pool })
}
/// Create a new [`Store`] from an existing [`Pool`].
pub async fn new_pool(pool: Pool) -> Result<Self, Error> {
let store = Self { pool };
Ok(store)
}
/// Runs pending migrations against the database.
pub async fn migrate(&self) -> Result<(), Error> {
Ok(sqlx::migrate!().run(&self.pool).await?)
}
}
impl Store {
/// Write tx_graph.
pub async fn write_tx_graph(
&self,
tx_graph: &tx_graph::ChangeSet<ConfirmationBlockTime>,
) -> Result<(), Error> {
let txs = &tx_graph.txs;
let txouts = &tx_graph.txouts;
let anchors = &tx_graph.anchors;
let first_seen = &tx_graph.first_seen;
let last_seen = &tx_graph.last_seen;
let last_evicted = &tx_graph.last_evicted;
for tx in txs {
let txid = tx.compute_txid();
sqlx::query(
"INSERT INTO tx(txid, tx) VALUES($1, $2) ON CONFLICT DO UPDATE SET tx = $2",
)
.bind(txid.to_string())
.bind(consensus::encode::serialize(tx))
.execute(&self.pool)
.await?;
}
for (txid, t) in first_seen {
sqlx::query("INSERT INTO tx(txid, first_seen) VALUES($1, $2) ON CONFLICT DO UPDATE SET first_seen = $2")
.bind(txid.to_string())
.bind(i64::try_from(*t)?)
.execute(&self.pool)
.await?;
}
for (txid, t) in last_seen {
sqlx::query("INSERT INTO tx(txid, last_seen) VALUES($1, $2) ON CONFLICT DO UPDATE SET last_seen = $2")
.bind(txid.to_string())
.bind(i64::try_from(*t)?)
.execute(&self.pool)
.await?;
}
for (txid, t) in last_evicted {
sqlx::query("INSERT INTO tx(txid, last_evicted) VALUES($1, $2) ON CONFLICT DO UPDATE SET last_evicted = $2")
.bind(txid.to_string())
.bind(i64::try_from(*t)?)
.execute(&self.pool)
.await?;
}
for (op, txout) in txouts {
let OutPoint { txid, vout } = op;
let TxOut {
value,
script_pubkey,
} = txout;
sqlx::query("INSERT INTO txout(txid, vout, value, script) VALUES($1, $2, $3, $4) ON CONFLICT DO UPDATE SET value = $3, script = $4")
.bind(txid.to_string())
.bind(vout)
.bind(i64::try_from(value.to_sat())?)
.bind(script_pubkey.to_bytes())
.execute(&self.pool)
.await?;
}
for (anchor, txid) in anchors {
let BlockId { height, hash } = anchor.block_id;
let confirmation_time = anchor.confirmation_time;
sqlx::query("INSERT OR IGNORE INTO anchor(block_height, block_hash, txid, confirmation_time) VALUES($1, $2, $3, $4)")
.bind(height)
.bind(hash.to_string())
.bind(txid.to_string())
.bind(i64::try_from(confirmation_time)?)
.execute(&self.pool)
.await?;
}
Ok(())
}
/// Write local_chain.
pub async fn write_local_chain(
&self,
local_chain: &local_chain::ChangeSet,
) -> Result<(), Error> {
for (&height, hash) in &local_chain.blocks {
match hash {
Some(hash) => {
sqlx::query("INSERT OR IGNORE INTO block(height, hash) VALUES($1, $2)")
.bind(height)
.bind(hash.to_string())
.execute(&self.pool)
.await?;
}
None => {
sqlx::query("DELETE FROM block WHERE height = $1")
.bind(height)
.execute(&self.pool)
.await?;
}
}
}
Ok(())
}
/// Write keychain_txout.
pub async fn write_keychain_txout(
&self,
keychain_txout: &keychain_txout::ChangeSet,
) -> Result<(), Error> {
for (descriptor_id, last_revealed) in &keychain_txout.last_revealed {
sqlx::query(
"INSERT INTO keychain_last_revealed(descriptor_id, last_revealed) VALUES($1, $2) ON CONFLICT DO UPDATE SET last_revealed = $2",
)
.bind(descriptor_id.to_string())
.bind(last_revealed)
.execute(&self.pool)
.await?;
}
for (descriptor_id, spk_cache) in &keychain_txout.spk_cache {
for (derivation_index, script) in spk_cache {
sqlx::query(
"INSERT OR IGNORE INTO keychain_script_pubkey(descriptor_id, derivation_index, script) VALUES($1, $2, $3)",
)
.bind(descriptor_id.to_string())
.bind(*derivation_index)
.bind(script.to_bytes())
.execute(&self.pool)
.await?;
}
}
Ok(())
}
/// Read tx_graph.
pub async fn read_tx_graph(&self) -> Result<tx_graph::ChangeSet<ConfirmationBlockTime>, Error> {
let mut changeset = tx_graph::ChangeSet::default();
let rows = sqlx::query("SELECT txid, tx, first_seen, last_seen, last_evicted FROM tx")
.fetch_all(&self.pool)
.await?;
for row in rows {
let txid: String = row.get("txid");
let txid: Txid = txid.parse()?;
let data: Vec<u8> = row.get("tx");
let tx: Transaction = consensus::encode::deserialize(&data)?;
let first_seen: i64 = row.get("first_seen");
let last_seen: i64 = row.get("last_seen");
let last_evicted: i64 = row.get("last_evicted");
changeset.txs.insert(Arc::new(tx));
changeset.first_seen.insert(txid, first_seen.try_into()?);
changeset.last_seen.insert(txid, last_seen.try_into()?);
changeset
.last_evicted
.insert(txid, last_evicted.try_into()?);
}
let rows = sqlx::query("SELECT txid, vout, value, script FROM txout")
.fetch_all(&self.pool)
.await?;
for row in rows {
let txid: String = row.get("txid");
let txid: Txid = txid.parse()?;
let vout: u32 = row.get("vout");
let value: i64 = row.get("value");
let value = Amount::from_sat(value.try_into()?);
let script: Vec<u8> = row.get("script");
let script_pubkey = ScriptBuf::from_bytes(script);
let outpoint = OutPoint { txid, vout };
let txout = TxOut {
value,
script_pubkey,
};
changeset.txouts.insert(outpoint, txout);
}
let rows =
sqlx::query("SELECT block_height, block_hash, txid, confirmation_time FROM anchor")
.fetch_all(&self.pool)
.await?;
for row in rows {
let height: u32 = row.get("block_height");
let hash: String = row.get("block_hash");
let hash: BlockHash = hash.parse()?;
let txid: String = row.get("txid");
let txid: Txid = txid.parse()?;
let confirmation_time: i64 = row.get("confirmation_time");
let anchor = ConfirmationBlockTime {
block_id: BlockId { height, hash },
confirmation_time: confirmation_time.try_into()?,
};
changeset.anchors.insert((anchor, txid));
}
Ok(changeset)
}
/// Read local_chain.
pub async fn read_local_chain(&self) -> Result<local_chain::ChangeSet, Error> {
let mut changeset = local_chain::ChangeSet::default();
let rows = sqlx::query("SELECT height, hash FROM block")
.fetch_all(&self.pool)
.await?;
for row in rows {
let height: u32 = row.get("height");
let hash: String = row.get("hash");
let hash: BlockHash = hash.parse()?;
changeset.blocks.insert(height, Some(hash));
}
Ok(changeset)
}
/// Read keychain_txout.
pub async fn read_keychain_txout(&self) -> Result<keychain_txout::ChangeSet, Error> {
let mut changeset = keychain_txout::ChangeSet::default();
let rows = sqlx::query("SELECT descriptor_id, last_revealed FROM keychain_last_revealed")
.fetch_all(&self.pool)
.await?;
for row in rows {
let descriptor_id: String = row.get("descriptor_id");
let descriptor_id: DescriptorId = descriptor_id.parse()?;
let last_revealed: u32 = row.get("last_revealed");
changeset.last_revealed.insert(descriptor_id, last_revealed);
}
let rows = sqlx::query(
"SELECT descriptor_id, derivation_index, script FROM keychain_script_pubkey",
)
.fetch_all(&self.pool)
.await?;
for row in rows {
let descriptor_id: String = row.get("descriptor_id");
let descriptor_id: DescriptorId = descriptor_id.parse()?;
let derivation_index: u32 = row.get("derivation_index");
let script: Vec<u8> = row.get("script");
let script = ScriptBuf::from_bytes(script);
changeset
.spk_cache
.entry(descriptor_id)
.or_default()
.insert(derivation_index, script);
}
Ok(changeset)
}
}