-
Notifications
You must be signed in to change notification settings - Fork 715
/
ledger_db.rs
683 lines (619 loc) · 23.3 KB
/
ledger_db.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
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
//! Copyright (c) 2022 MASSA LABS <info@massa.net>
//! Module to interact with the disk ledger
use massa_db_exports::{
DBBatch, MassaDirection, MassaIteratorMode, ShareableMassaDBController, CRUD_ERROR,
KEY_SER_ERROR, LEDGER_PREFIX, STATE_CF,
};
use massa_ledger_exports::*;
use massa_models::amount::AmountDeserializer;
use massa_models::bytecode::BytecodeDeserializer;
use massa_models::datastore::get_prefix_bounds;
use massa_models::{
address::Address, amount::AmountSerializer, bytecode::BytecodeSerializer, slot::Slot,
};
use massa_serialization::{
DeserializeError, Deserializer, Serializer, U64VarIntDeserializer, U64VarIntSerializer,
};
use std::collections::{BTreeSet, HashMap};
use std::fmt::Debug;
use massa_models::amount::Amount;
use std::ops::Bound;
/// Ledger sub entry enum
pub enum LedgerSubEntry {
/// Version
Version,
/// Balance
Balance,
/// Bytecode
Bytecode,
/// Datastore entry
Datastore(Vec<u8>),
}
impl LedgerSubEntry {
fn derive_key(&self, addr: &Address) -> Key {
match self {
LedgerSubEntry::Version => Key::new(addr, KeyType::VERSION),
LedgerSubEntry::Balance => Key::new(addr, KeyType::BALANCE),
LedgerSubEntry::Bytecode => Key::new(addr, KeyType::BYTECODE),
LedgerSubEntry::Datastore(hash) => Key::new(addr, KeyType::DATASTORE(hash.to_vec())),
}
}
}
/// Disk ledger DB module
///
/// Contains a `RocksDB` DB instance
pub struct LedgerDB {
db: ShareableMassaDBController,
thread_count: u8,
key_serializer_db: KeySerializer,
key_deserializer_db: KeyDeserializer,
amount_serializer: AmountSerializer,
version_serializer: U64VarIntSerializer,
version_deserializer: U64VarIntDeserializer,
bytecode_serializer: BytecodeSerializer,
amount_deserializer: AmountDeserializer,
bytecode_deserializer: BytecodeDeserializer,
max_datastore_value_length: u64,
max_datastore_key_length: u8,
}
impl Debug for LedgerDB {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let db = self.db.read();
write!(f, "{:#?}", db)
}
}
impl LedgerDB {
/// Create and initialize a new `LedgerDB`.
///
/// # Arguments
/// * path: path to the desired disk ledger db directory
pub fn new(
db: ShareableMassaDBController,
thread_count: u8,
max_datastore_key_length: u8,
max_datastore_value_length: u64,
) -> Self {
LedgerDB {
db,
thread_count,
key_serializer_db: KeySerializer::new(false),
key_deserializer_db: KeyDeserializer::new(max_datastore_key_length, false),
amount_serializer: AmountSerializer::new(),
bytecode_serializer: BytecodeSerializer::new(),
amount_deserializer: AmountDeserializer::new(
Bound::Included(Amount::MIN),
Bound::Included(Amount::MAX),
),
bytecode_deserializer: BytecodeDeserializer::new(max_datastore_value_length),
version_serializer: U64VarIntSerializer::new(),
version_deserializer: U64VarIntDeserializer::new(
Bound::Included(0),
Bound::Included(u64::MAX),
),
max_datastore_value_length,
max_datastore_key_length,
}
}
/// Loads the initial disk ledger
///
/// # Arguments
pub fn load_initial_ledger(&mut self, initial_ledger: HashMap<Address, LedgerEntry>) {
let mut batch = DBBatch::new();
for (address, entry) in initial_ledger {
self.put_entry(&address, entry, &mut batch);
}
self.db.write().write_batch(
batch,
Default::default(),
Some(Slot::new(0, self.thread_count.saturating_sub(1))),
);
}
/// Allows applying `LedgerChanges` to the disk ledger
///
/// # Arguments
/// * changes: ledger changes to be applied
/// * batch: the batch to apply the changes to
pub fn apply_changes_to_batch(&self, changes: LedgerChanges, batch: &mut DBBatch) {
// for all incoming changes
for (addr, change) in changes.0 {
match change {
// the incoming change sets a ledger entry to a new one
SetUpdateOrDelete::Set(new_entry) => {
// inserts/overwrites the entry with the incoming one
self.put_entry(&addr, new_entry, batch);
}
// the incoming change updates an existing ledger entry
SetUpdateOrDelete::Update(entry_update) => {
// applies the updates to the entry
// if the entry does not exist, inserts a default one and applies the updates to it
self.update_entry(&addr, entry_update, batch);
}
// the incoming change deletes a ledger entry
SetUpdateOrDelete::Delete => {
// delete the entry, if it exists
self.delete_entry(&addr, batch);
}
}
}
}
/// Get the given sub-entry of a given address.
///
/// # Arguments
/// * `addr`: associated address
/// * `ty`: type of the queried sub-entry
///
/// # Returns
/// An Option of the sub-entry value as bytes
pub fn get_sub_entry(&self, addr: &Address, ty: LedgerSubEntry) -> Option<Vec<u8>> {
let db = self.db.read();
let key = ty.derive_key(addr);
let mut serialized_key = Vec::new();
self.key_serializer_db
.serialize(&key, &mut serialized_key)
.expect(KEY_SER_ERROR);
db.get_cf(STATE_CF, serialized_key).expect(CRUD_ERROR)
}
/// Get every key of the datastore for a given address.
///
/// # Returns
/// A `BTreeSet` of the datastore keys
pub fn get_datastore_keys(&self, addr: &Address, prefix: &[u8]) -> Option<BTreeSet<Vec<u8>>> {
let db = self.db.read();
// check if address exists, return None if it does not
{
let key = LedgerSubEntry::Balance.derive_key(addr);
let mut serialized_key = Vec::new();
self.key_serializer_db
.serialize(&key, &mut serialized_key)
.expect(KEY_SER_ERROR);
db.get_cf(STATE_CF, serialized_key).expect(CRUD_ERROR)?;
}
// collect keys starting with prefix
let start_prefix = datastore_prefix_from_address(addr, prefix);
let end_prefix = end_prefix(&start_prefix);
Some(
db.iterator_cf(
STATE_CF,
MassaIteratorMode::From(&start_prefix, MassaDirection::Forward),
)
.take_while(|(key, _)| match &end_prefix {
Some(end) => key < end,
None => true,
})
.filter_map(|(key, _)| {
let (_rest, key) = self
.key_deserializer_db
.deserialize::<DeserializeError>(&key)
.expect("could not deserialize datastore key from state db");
match key.key_type {
KeyType::DATASTORE(datastore_vec) => Some(datastore_vec),
_ => None,
}
})
.collect(),
)
}
pub fn reset(&self) {
self.db.write().delete_prefix(LEDGER_PREFIX, STATE_CF, None);
}
/// Deserializes the key and value, useful after bootstrap
pub fn is_key_value_valid(&self, serialized_key: &[u8], serialized_value: &[u8]) -> bool {
if !serialized_key.starts_with(LEDGER_PREFIX.as_bytes()) {
return false;
}
let Ok((rest, key)) = self
.key_deserializer_db
.deserialize::<DeserializeError>(serialized_key)
else {
return false;
};
if !rest.is_empty() {
return false;
}
match key.key_type {
KeyType::VERSION => {
let Ok((rest, _version)) = self
.version_deserializer
.deserialize::<DeserializeError>(serialized_value)
else {
return false;
};
if !rest.is_empty() {
return false;
}
}
KeyType::BALANCE => {
let Ok((rest, _amount)) = self
.amount_deserializer
.deserialize::<DeserializeError>(serialized_value)
else {
return false;
};
if !rest.is_empty() {
return false;
}
}
KeyType::BYTECODE => {
let Ok((rest, _bytecode)) = self
.bytecode_deserializer
.deserialize::<DeserializeError>(serialized_value)
else {
return false;
};
if !rest.is_empty() {
return false;
}
}
KeyType::DATASTORE(_) => {
if serialized_value.len() >= self.max_datastore_value_length as usize {
return false;
}
}
}
true
}
}
// Private helpers
impl LedgerDB {
/// Add every sub-entry individually for a given entry.
///
/// # Arguments
/// * `addr`: associated address
/// * `ledger_entry`: complete entry to be added
/// * `batch`: the given operation batch to update
fn put_entry(&self, addr: &Address, ledger_entry: LedgerEntry, batch: &mut DBBatch) {
let db = self.db.read();
// Version
//TODO: Get version number from parameters
let mut bytes_version = Vec::new();
self.version_serializer
.serialize(&0, &mut bytes_version)
.unwrap();
let mut serialized_key = Vec::new();
self.key_serializer_db
.serialize(&Key::new(addr, KeyType::VERSION), &mut serialized_key)
.expect(KEY_SER_ERROR);
db.put_or_update_entry_value(batch, serialized_key, &bytes_version);
// Amount serialization never fails
let mut bytes_balance = Vec::new();
self.amount_serializer
.serialize(&ledger_entry.balance, &mut bytes_balance)
.unwrap();
let mut bytes_bytecode = Vec::new();
self.bytecode_serializer
.serialize(&ledger_entry.bytecode, &mut bytes_bytecode)
.unwrap();
// balance
let mut serialized_key = Vec::new();
self.key_serializer_db
.serialize(&Key::new(addr, KeyType::BALANCE), &mut serialized_key)
.expect(KEY_SER_ERROR);
db.put_or_update_entry_value(batch, serialized_key, &bytes_balance);
// bytecode
let mut serialized_key = Vec::new();
self.key_serializer_db
.serialize(&Key::new(addr, KeyType::BYTECODE), &mut serialized_key)
.expect(KEY_SER_ERROR);
db.put_or_update_entry_value(batch, serialized_key, &bytes_bytecode);
// datastore
for (key, entry) in ledger_entry.datastore {
if entry.len() > self.max_datastore_value_length as usize {
panic!(
"Datastore entry for address {} and key {:?} is too big ({} > {})",
addr,
key,
entry.len(),
self.max_datastore_value_length
);
}
if key.len() > self.max_datastore_key_length as usize {
panic!(
"Datastore key for address {} and key {:?} is too big ({} > {})",
addr,
key,
key.len(),
self.max_datastore_key_length
);
}
let mut serialized_key = Vec::new();
self.key_serializer_db
.serialize(
&Key::new(addr, KeyType::DATASTORE(key)),
&mut serialized_key,
)
.expect(KEY_SER_ERROR);
db.put_or_update_entry_value(batch, serialized_key, &entry);
}
}
/// Update the ledger entry of a given address.
///
/// # Arguments
/// * `entry_update`: a descriptor of the entry updates to be applied
/// * `batch`: the given operation batch to update
fn update_entry(&self, addr: &Address, entry_update: LedgerEntryUpdate, batch: &mut DBBatch) {
let db = self.db.read();
// balance
if let SetOrKeep::Set(balance) = entry_update.balance {
let mut bytes = Vec::new();
// Amount serialization never fails
self.amount_serializer
.serialize(&balance, &mut bytes)
.unwrap();
let mut serialized_key = Vec::new();
self.key_serializer_db
.serialize(&Key::new(addr, KeyType::BALANCE), &mut serialized_key)
.expect(KEY_SER_ERROR);
db.put_or_update_entry_value(batch, serialized_key, &bytes);
}
// bytecode
if let SetOrKeep::Set(bytecode) = entry_update.bytecode {
let mut bytes = Vec::new();
self.bytecode_serializer
.serialize(&bytecode, &mut bytes)
.unwrap();
let mut serialized_key = Vec::new();
self.key_serializer_db
.serialize(&Key::new(addr, KeyType::BYTECODE), &mut serialized_key)
.expect(KEY_SER_ERROR);
db.put_or_update_entry_value(batch, serialized_key, &bytes);
}
// datastore
for (key, update) in entry_update.datastore {
if key.len() > self.max_datastore_key_length as usize {
panic!(
"Datastore key for address {} and key {:?} is too big ({} > {})",
addr,
key,
key.len(),
self.max_datastore_key_length
);
}
let mut serialized_key = Vec::new();
self.key_serializer_db
.serialize(
&Key::new(addr, KeyType::DATASTORE(key)),
&mut serialized_key,
)
.expect(KEY_SER_ERROR);
match update {
SetOrDelete::Set(entry) => {
if entry.len() > self.max_datastore_value_length as usize {
panic!(
"Datastore entry for address {} is too big ({} > {})",
addr,
entry.len(),
self.max_datastore_value_length
);
} else {
db.put_or_update_entry_value(batch, serialized_key, &entry);
}
}
SetOrDelete::Delete => db.delete_key(batch, serialized_key),
}
}
}
/// Delete every sub-entry associated to the given address.
///
/// # Arguments
/// * batch: the given operation batch to update
fn delete_entry(&self, addr: &Address, batch: &mut DBBatch) {
let db = self.db.read();
// version
let mut serialized_key = Vec::new();
self.key_serializer_db
.serialize(&Key::new(addr, KeyType::VERSION), &mut serialized_key)
.expect(KEY_SER_ERROR);
db.delete_key(batch, serialized_key);
// balance
let mut serialized_key = Vec::new();
self.key_serializer_db
.serialize(&Key::new(addr, KeyType::BALANCE), &mut serialized_key)
.expect(KEY_SER_ERROR);
db.delete_key(batch, serialized_key);
// bytecode
let mut serialized_key = Vec::new();
self.key_serializer_db
.serialize(&Key::new(addr, KeyType::BYTECODE), &mut serialized_key)
.expect(KEY_SER_ERROR);
db.delete_key(batch, serialized_key);
// datastore
let key_prefix = datastore_prefix_from_address(addr, &[]);
for (serialized_key, _) in db
.iterator_cf(
STATE_CF,
MassaIteratorMode::From(&key_prefix, MassaDirection::Forward),
)
.take_while(|(key, _)| key <= &end_prefix(&key_prefix).unwrap())
{
db.delete_key(batch, serialized_key.to_vec());
}
}
}
// test helpers
impl LedgerDB {
/// Get every address and their corresponding balance.
///
/// IMPORTANT: This should only be used for debug purposes.
///
/// # Returns
/// A `BTreeMap` with the address as key and the balance as value
#[cfg(feature = "test-exports")]
pub fn get_every_address(
&self,
) -> std::collections::BTreeMap<Address, massa_models::amount::Amount> {
use massa_models::address::AddressDeserializer;
let db = self.db.write();
let ledger = db
.prefix_iterator_cf(STATE_CF, LEDGER_PREFIX.as_bytes())
.take_while(|(key, _)| key.starts_with(LEDGER_PREFIX.as_bytes()))
.collect::<Vec<_>>();
let mut addresses = std::collections::BTreeMap::new();
let address_deserializer = AddressDeserializer::new();
for (key, entry) in ledger.iter() {
let (rest, address) = address_deserializer
.deserialize::<DeserializeError>(&key[LEDGER_PREFIX.len()..])
.unwrap();
if rest.first() == Some(&BALANCE_IDENT) {
let (_, amount) = self
.amount_deserializer
.deserialize::<DeserializeError>(entry)
.unwrap();
addresses.insert(address, amount);
}
}
addresses
}
/// Get the entire datastore for a given address.
///
/// IMPORTANT: This should only be used for debug purposes.
///
/// # Returns
/// A `BTreeMap` with the entry hash as key and the data bytes as value
#[cfg(any(test, feature = "test-exports"))]
pub fn get_entire_datastore(
&self,
addr: &Address,
) -> std::collections::BTreeMap<Vec<u8>, Vec<u8>> {
let db = self.db.read();
let key_prefix = datastore_prefix_from_address(addr, &[]);
db.iterator_cf(
STATE_CF,
MassaIteratorMode::From(&key_prefix, MassaDirection::Forward),
)
.take_while(|(k, _)| k <= &end_prefix(&key_prefix).unwrap())
.map(|(key, data)| {
let (_rest, key) = self
.key_deserializer_db
.deserialize::<DeserializeError>(&key)
.unwrap();
match key.key_type {
KeyType::DATASTORE(datastore_vec) => (datastore_vec, data.to_vec()),
_ => (vec![], vec![]),
}
})
.collect()
}
}
/// For a given start prefix (inclusive), returns the correct end prefix (non-inclusive).
/// This assumes the key bytes are ordered in lexicographical order.
/// Since key length is not limited, for some case we return `None` because there is
/// no bounded limit (every keys in the series `[]`, `[255]`, `[255, 255]` ...).
fn end_prefix(prefix: &[u8]) -> Option<Vec<u8>> {
match get_prefix_bounds(prefix).1 {
std::ops::Bound::Excluded(end) => Some(end),
std::ops::Bound::Unbounded => None,
_ => panic!("unexpected key bound"),
}
}
#[cfg(test)]
mod tests {
use super::*;
use massa_db_exports::{MassaDBConfig, MassaDBController, STATE_HASH_INITIAL_BYTES};
use massa_db_worker::MassaDB;
use massa_hash::HashXof;
use massa_ledger_exports::{LedgerEntry, LedgerEntryUpdate, SetOrKeep};
use massa_models::{
address::Address,
amount::{Amount, AmountDeserializer},
};
use massa_serialization::{DeserializeError, Deserializer};
use massa_signature::KeyPair;
use parking_lot::RwLock;
use std::collections::BTreeMap;
use std::ops::Bound::Included;
use std::str::FromStr;
use std::sync::Arc;
use tempfile::TempDir;
fn init_test_ledger(addr: Address) -> (LedgerDB, BTreeMap<Vec<u8>, Vec<u8>>) {
// init data
let mut data = BTreeMap::new();
data.insert(b"1".to_vec(), b"a".to_vec());
data.insert(b"2".to_vec(), b"b".to_vec());
data.insert(b"3".to_vec(), b"c".to_vec());
let entry = LedgerEntry {
balance: Amount::from_str("42").unwrap(),
datastore: data.clone(),
..Default::default()
};
let entry_update = LedgerEntryUpdate {
balance: SetOrKeep::Set(Amount::from_str("21").unwrap()),
bytecode: SetOrKeep::Keep,
..Default::default()
};
// write data
let temp_dir = TempDir::new().unwrap();
let db_config = MassaDBConfig {
path: temp_dir.path().to_path_buf(),
max_history_length: 10,
max_final_state_elements_size: 100_000,
max_versioning_elements_size: 100_000,
max_ledger_backups: 10,
thread_count: 32,
};
let db = Arc::new(RwLock::new(
Box::new(MassaDB::new(db_config)) as Box<(dyn MassaDBController + 'static)>
));
let ledger_db = LedgerDB::new(db.clone(), 32, 255, 1000);
let mut batch = DBBatch::new();
ledger_db.put_entry(&addr, entry, &mut batch);
ledger_db.update_entry(&addr, entry_update, &mut batch);
ledger_db
.db
.write()
.write_batch(batch, Default::default(), None);
// return db and initial data
(ledger_db, data)
}
/// Functional test of `LedgerDB`
#[test]
fn test_ledger_db() {
let addr = Address::from_public_key(&KeyPair::generate(0).unwrap().get_public_key());
let (ledger_db, data) = init_test_ledger(addr);
let amount_deserializer =
AmountDeserializer::new(Included(Amount::MIN), Included(Amount::MAX));
// check initial state and entry update
assert!(ledger_db
.get_sub_entry(&addr, LedgerSubEntry::Balance)
.is_some());
assert!(ledger_db
.get_sub_entry(&addr, LedgerSubEntry::Version)
.is_some());
assert_eq!(
amount_deserializer
.deserialize::<DeserializeError>(
&ledger_db
.get_sub_entry(&addr, LedgerSubEntry::Balance)
.unwrap()
)
.unwrap()
.1,
Amount::from_str("21").unwrap()
);
assert_eq!(data, ledger_db.get_entire_datastore(&addr));
assert_ne!(
HashXof(*STATE_HASH_INITIAL_BYTES),
ledger_db.db.read().get_xof_db_hash()
);
// delete entry
let mut batch = DBBatch::new();
ledger_db.delete_entry(&addr, &mut batch);
ledger_db
.db
.write()
.write_batch(batch, Default::default(), None);
// check deleted address and ledger hash
assert_eq!(
HashXof(*STATE_HASH_INITIAL_BYTES),
ledger_db.db.read().get_xof_db_hash()
);
assert!(ledger_db
.get_sub_entry(&addr, LedgerSubEntry::Balance)
.is_none());
assert!(ledger_db.get_entire_datastore(&addr).is_empty());
}
#[test]
fn test_end_prefix() {
assert_eq!(end_prefix(&[5, 6, 7]), Some(vec![5, 6, 8]));
assert_eq!(end_prefix(&[5, 6, 255]), Some(vec![5, 7]));
}
}