-
Notifications
You must be signed in to change notification settings - Fork 15
/
store.rs
369 lines (306 loc) · 12 KB
/
store.rs
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
use pallas::{
codec::minicbor::decode::Error as DecodingError,
ledger::{
addresses::Address,
traverse::{Era, MultiEraBlock, MultiEraOutput, MultiEraPolicyAssets, MultiEraTx},
},
};
use redb::{
Database, ReadOnlyTable, ReadTransaction, ReadableMultimapTable, ReadableTable,
WriteTransaction,
};
use std::{ops::Deref, path::Path};
use pallas;
use redb::{MultimapTableDefinition, TableDefinition};
use thiserror::Error;
// use std::error::Error;
// Given a block, table "block" maps its slot to its CBOR representation
pub type BlockKeyType<'a> = u64;
pub type BlockValueType<'a> = &'a [u8];
pub type BlockResultType = Vec<u8>;
pub const BLOCK_TABLE: TableDefinition<BlockKeyType, BlockValueType> =
TableDefinition::new("block");
// Given a block, table "block_by_hash" maps its hash to its slot.
pub type BlockByHashKeyType<'a> = &'a [u8; 32];
pub type BlockByHashValueType<'a> = u64;
pub const BLOCK_BY_HASH_INDEX: TableDefinition<BlockByHashKeyType, BlockByHashValueType> =
TableDefinition::new("block_by_hash");
// Given a transaction, table "tx" maps its hash to an encoding representation
// of it
// NOTE: transactions don't have a precise CBOR representation, so we use
// a library encoded representation instead
pub type TxKeyType<'a> = &'a [u8; 32];
pub type TxValueType<'a> = &'a [u8];
pub type TxResultType = Vec<u8>;
pub const TX_TABLE: TableDefinition<TxKeyType, TxValueType> = TableDefinition::new("tx");
// Given a UTxO, table "utxo" maps its output reference (a pair composed of the
// hash of the transaction that produced the UTxO and the index in the list of
// transaction outputs corresponding to it) to the result of encoding said UTxO
// NOTE: Just like transactions, UTxO's don't have a precise CBOR
// representation.
pub type UTxOKeyType<'a> = (&'a [u8], u8);
pub type UTxOValueType<'a> = &'a [u8];
pub type UTxOResultType = Vec<u8>;
pub const UTXO_TABLE: TableDefinition<UTxOKeyType, UTxOValueType> = TableDefinition::new("utxo");
// Given an address, table "utxo_by_addr" maps it to a list of pairs of a tx
// hash and an (output) index (each one representing a UTxO sitting at that
// address)
pub type UTxOByAddrKeyType<'a> = &'a [u8];
pub type UTxOByAddrValueType<'a> = (&'a [u8], u8);
pub type UTxOByAddrResultType = (Vec<u8>, u8);
pub const UTXO_BY_ADDR_INDEX: MultimapTableDefinition<UTxOByAddrKeyType, UTxOByAddrValueType> =
MultimapTableDefinition::new("utxo_by_addr");
// Given a minting policy, table "utxo_by_beacon" maps it to a list of pairs of
// a tx hash and an (output) index (each one representing a UTxO containing a
// token of that policy)
pub type UTxOByBeaconKeyType<'a> = &'a [u8; 28];
pub type UTxOByBeaconValueType<'a> = (&'a [u8], u8);
pub type UTxOByBeaconResultType = (Vec<u8>, u8);
pub const UTXO_BY_POLICY_INDEX: MultimapTableDefinition<
UTxOByBeaconKeyType,
UTxOByBeaconValueType,
> = MultimapTableDefinition::new("utxo_by_beacon");
// Table "prot_params" stores only the latest protocol parameters.
pub type ProtParamsKeyType = ();
pub type ProtParamsValueType<'a> = &'a [u8];
pub type ProtParamsResultType = Vec<u8>;
pub const PROT_PARAMS_TABLE: TableDefinition<ProtParamsKeyType, ProtParamsValueType> =
TableDefinition::new("prot_params");
#[derive(Error, Debug)]
pub enum Error {
#[error("error decoding address")]
AddressDecoding(#[from] pallas::ledger::addresses::Error),
#[error("error decoding block")]
BlockDecoding(#[from] pallas::ledger::traverse::Error),
#[error("key not found")]
KeyNotFound,
#[error("error decoding output")]
OutputDecoding(#[from] pallas::codec::minicbor::decode::Error),
#[error("utxo table invariant broken")]
UTxOTableInvariantBroken,
#[error("Redb error")]
ReDBError(#[from] redb::Error),
#[error("IO error")]
IOError(#[from] std::io::Error),
}
impl Error {
pub fn redb(inner: impl Into<redb::Error>) -> Self {
Self::ReDBError(inner.into())
}
}
pub struct Store {
inner_store: Database,
}
impl Store {
pub fn open(path: impl AsRef<Path>) -> Result<Self, Error> {
Ok(Store {
inner_store: Database::create(path).map_err(Error::redb)?,
})
}
pub fn destroy(path: impl AsRef<Path>) -> Result<(), Error> {
std::fs::remove_file(path).map_err(Error::IOError)
}
pub fn apply_block(&self, block_cbor: BlockValueType) -> Result<(), Error> {
let write_tx: WriteTransaction = self.inner_store.begin_write().map_err(Error::redb)?;
let block: MultiEraBlock = self.store_block(&write_tx, block_cbor)?;
self.store_txs(&write_tx, &block)?;
write_tx.commit().map_err(Error::redb)?;
Ok(())
}
fn store_block<'a>(
&self,
write_tx: &'a WriteTransaction,
block_cbor: &'a [u8],
) -> Result<MultiEraBlock<'a>, Error> {
let block: MultiEraBlock =
MultiEraBlock::decode(block_cbor).map_err(Error::BlockDecoding)?;
write_tx
.open_table(BLOCK_TABLE)
.map_err(Error::redb)?
.insert(block.slot(), block_cbor)
.map_err(Error::redb)?;
write_tx
.open_table(BLOCK_BY_HASH_INDEX)
.map_err(Error::redb)?
.insert(block.hash().deref(), block.slot())
.map_err(Error::redb)?;
Ok(block)
}
fn store_txs(&self, write_tx: &WriteTransaction, block: &MultiEraBlock) -> Result<(), Error> {
let mut tx_table = write_tx.open_table(TX_TABLE).map_err(Error::redb)?;
for tx in block.txs() {
tx_table
.insert(tx.hash().deref(), tx.encode().as_slice())
.map_err(Error::redb)?;
self.update_tx_outputs(write_tx, &tx)?;
}
Ok(())
}
fn update_tx_outputs(&self, write_tx: &WriteTransaction, tx: &MultiEraTx) -> Result<(), Error> {
let mut utxo_table = write_tx.open_table(UTXO_TABLE).map_err(Error::redb)?;
let mut utxo_by_addr_table = write_tx
.open_multimap_table(UTXO_BY_ADDR_INDEX)
.map_err(Error::redb)?;
let mut utxo_by_policy_table = write_tx
.open_multimap_table(UTXO_BY_POLICY_INDEX)
.map_err(Error::redb)?;
for (index, tx_out) in tx.produces() {
let tx_hash = tx.hash();
utxo_by_addr_table
.insert(
output_address(&tx_out)?.to_vec().as_slice(),
(tx_hash.as_slice(), index as u8),
)
.map_err(Error::redb)?;
for policy in output_policy_ids(&tx_out) {
utxo_by_policy_table
.insert(&policy, (tx_hash.as_slice(), index as u8))
.map_err(Error::redb)?;
}
}
for multi_era_input in tx.consumes() {
let tx_in: UTxOKeyType = (
multi_era_input.hash().as_slice(),
multi_era_input.index() as u8,
);
let encoded_tx_out = utxo_table
.get(tx_in)
.map_err(Error::redb)?
.ok_or(Error::UTxOTableInvariantBroken)?
.value()
.to_vec();
let tx_out: MultiEraOutput =
decode_output(&encoded_tx_out).map_err(Error::OutputDecoding)?;
utxo_by_addr_table
.remove(output_address(&tx_out)?.to_vec().as_slice(), tx_in)
.map_err(Error::redb)?;
for policy in output_policy_ids(&tx_out) {
utxo_by_policy_table
.remove(&policy, tx_in)
.map_err(Error::redb)?;
}
utxo_table.remove(tx_in).map_err(Error::redb)?;
}
Ok(())
}
pub fn update_protocol_parameters(
&self,
prot_params: ProtParamsValueType,
) -> Result<(), Error> {
let write_tx: WriteTransaction = self.inner_store.begin_write().map_err(Error::redb)?;
let mut prot_params_table = write_tx
.open_table(PROT_PARAMS_TABLE)
.map_err(Error::redb)?;
let _ = prot_params_table.insert((), prot_params);
drop(prot_params_table);
write_tx.commit().map_err(Error::redb)?;
Ok(())
}
pub fn get_blockchain_tip(&self) -> Result<BlockResultType, Error> {
self.inner_store
.begin_read()
.map_err(Error::redb)?
.open_table(BLOCK_TABLE)
.map_err(Error::redb)?
.last()
.map_err(Error::redb)?
.ok_or(Error::KeyNotFound)
.map(|entry| Vec::from(entry.1.value()))
}
pub fn get_protocol_parameters(&self) -> Result<ProtParamsResultType, Error> {
self.inner_store
.begin_read()
.map_err(Error::redb)?
.open_table(PROT_PARAMS_TABLE)
.map_err(Error::redb)?
.get(())
.map_err(Error::redb)?
.ok_or(Error::KeyNotFound)
.map(|entry| Vec::from(entry.value()))
}
pub fn get_utxos_for_address(
&self,
addr: &UTxOByAddrKeyType,
) -> Result<Vec<UTxOByAddrResultType>, Error> {
let read_tx = self.inner_store.begin_read().map_err(Error::redb)?;
let values = read_tx
.open_multimap_table(UTXO_BY_ADDR_INDEX)
.map_err(Error::redb)?
.get(addr)
.map_err(Error::redb)?
.flatten()
.map(|x| {
let (a, b) = x.value();
(Vec::from(a), b)
})
.collect();
Ok(values)
}
pub fn get_utxo_from_reference(&self, utxo_ref: &UTxOKeyType) -> Option<UTxOResultType> {
let read_tx: ReadTransaction = self.inner_store.begin_read().ok()?;
let utxo_table: ReadOnlyTable<UTxOKeyType, UTxOValueType> =
read_tx.open_table(UTXO_TABLE).ok()?;
let res = utxo_table
.get(utxo_ref)
.ok()?
.map(|val| Vec::from(val.value()));
res
}
pub fn get_tx_from_hash(&self, tx_hash: &TxKeyType) -> Option<TxResultType> {
let read_tx: ReadTransaction = self.inner_store.begin_read().ok()?;
let tx_table: ReadOnlyTable<TxKeyType, TxValueType> = read_tx.open_table(TX_TABLE).ok()?;
let res = tx_table
.get(tx_hash)
.ok()?
.map(|val| Vec::from(val.value()));
res
}
pub fn get_block_from_hash(&self, block_hash: &BlockKeyType) -> Option<BlockResultType> {
self.inner_store
.begin_read()
.ok()?
.open_table(BLOCK_TABLE)
.ok()?
.get(block_hash)
.ok()?
.map(|val| Vec::from(val.value()))
}
pub fn get_utxos_from_beacon(
&self,
beacon_policy_id: &UTxOByBeaconKeyType,
) -> Result<Box<impl Iterator<Item = UTxOByBeaconResultType>>, Error> {
let read_tx = self.inner_store.begin_read().map_err(Error::redb)?;
let utxo_by_beacon_table = read_tx
.open_multimap_table(UTXO_BY_POLICY_INDEX)
.map_err(Error::redb)?;
let res = match utxo_by_beacon_table.get(beacon_policy_id) {
Ok(database_results) => {
let mut res = vec![];
for val in database_results.flatten() {
let (tx_hash, tx_index) = val.value();
res.push((Vec::from(tx_hash), tx_index))
}
Ok(Box::new(res.into_iter()))
}
Err(err) => Err(Error::redb(err)),
};
res
}
}
fn output_policy_ids(output: &MultiEraOutput) -> Vec<[u8; 28]> {
output
.non_ada_assets()
.iter()
.map(MultiEraPolicyAssets::policy)
.map(Deref::deref)
.map(Clone::clone)
.collect()
}
fn output_address(output: &MultiEraOutput) -> Result<Address, Error> {
output.address().map_err(Error::AddressDecoding)
}
fn decode_output(encoded_output: &[u8]) -> Result<MultiEraOutput, DecodingError> {
MultiEraOutput::decode(Era::Byron, encoded_output)
.or_else(|_| MultiEraOutput::decode(Era::Alonzo, encoded_output))
.or_else(|_| MultiEraOutput::decode(Era::Babbage, encoded_output))
}