Struct sp_state_machine::OverlayedChanges
source · pub struct OverlayedChanges { /* private fields */ }
Expand description
The set of changes that are overlaid onto the backend.
It allows changes to be modified using nestable transactions.
Implementations§
source§impl OverlayedChanges
impl OverlayedChanges
sourcepub fn is_empty(&self) -> bool
pub fn is_empty(&self) -> bool
Whether no changes are contained in the top nor in any of the child changes.
sourcepub fn set_collect_extrinsics(&mut self, collect_extrinsics: bool)
pub fn set_collect_extrinsics(&mut self, collect_extrinsics: bool)
Ask to collect/not to collect extrinsics indices where key(s) has been changed.
sourcepub fn storage(&self, key: &[u8]) -> Option<Option<&[u8]>>
pub fn storage(&self, key: &[u8]) -> Option<Option<&[u8]>>
Returns a double-Option: None if the key is unknown (i.e. and the query should be referred to the backend); Some(None) if the key has been deleted. Some(Some(…)) for a key whose value has been set.
Examples found in repository?
More examples
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 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783
fn storage(&self, key: &[u8]) -> Option<StorageValue> {
let _guard = guard();
let result = self
.overlay
.storage(key)
.map(|x| x.map(|x| x.to_vec()))
.unwrap_or_else(|| self.backend.storage(key).expect(EXT_NOT_ALLOWED_TO_FAIL));
// NOTE: be careful about touching the key names – used outside substrate!
trace!(
target: "state",
method = "Get",
ext_id = %HexDisplay::from(&self.id.to_le_bytes()),
key = %HexDisplay::from(&key),
result = ?result.as_ref().map(HexDisplay::from),
result_encoded = %HexDisplay::from(
&result
.as_ref()
.map(|v| EncodeOpaqueValue(v.clone()))
.encode()
),
);
result
}
fn storage_hash(&self, key: &[u8]) -> Option<Vec<u8>> {
let _guard = guard();
let result = self
.overlay
.storage(key)
.map(|x| x.map(|x| H::hash(x)))
.unwrap_or_else(|| self.backend.storage_hash(key).expect(EXT_NOT_ALLOWED_TO_FAIL));
trace!(
target: "state",
method = "Hash",
ext_id = %HexDisplay::from(&self.id.to_le_bytes()),
key = %HexDisplay::from(&key),
?result,
);
result.map(|r| r.encode())
}
fn child_storage(&self, child_info: &ChildInfo, key: &[u8]) -> Option<StorageValue> {
let _guard = guard();
let result = self
.overlay
.child_storage(child_info, key)
.map(|x| x.map(|x| x.to_vec()))
.unwrap_or_else(|| {
self.backend.child_storage(child_info, key).expect(EXT_NOT_ALLOWED_TO_FAIL)
});
trace!(
target: "state",
method = "ChildGet",
ext_id = %HexDisplay::from(&self.id.to_le_bytes()),
child_info = %HexDisplay::from(&child_info.storage_key()),
key = %HexDisplay::from(&key),
result = ?result.as_ref().map(HexDisplay::from)
);
result
}
fn child_storage_hash(&self, child_info: &ChildInfo, key: &[u8]) -> Option<Vec<u8>> {
let _guard = guard();
let result = self
.overlay
.child_storage(child_info, key)
.map(|x| x.map(|x| H::hash(x)))
.unwrap_or_else(|| {
self.backend.child_storage_hash(child_info, key).expect(EXT_NOT_ALLOWED_TO_FAIL)
});
trace!(
target: "state",
method = "ChildHash",
ext_id = %HexDisplay::from(&self.id.to_le_bytes()),
child_info = %HexDisplay::from(&child_info.storage_key()),
key = %HexDisplay::from(&key),
?result,
);
result.map(|r| r.encode())
}
fn exists_storage(&self, key: &[u8]) -> bool {
let _guard = guard();
let result = match self.overlay.storage(key) {
Some(x) => x.is_some(),
_ => self.backend.exists_storage(key).expect(EXT_NOT_ALLOWED_TO_FAIL),
};
trace!(
target: "state",
method = "Exists",
ext_id = %HexDisplay::from(&self.id.to_le_bytes()),
key = %HexDisplay::from(&key),
%result,
);
result
}
fn exists_child_storage(&self, child_info: &ChildInfo, key: &[u8]) -> bool {
let _guard = guard();
let result = match self.overlay.child_storage(child_info, key) {
Some(x) => x.is_some(),
_ => self
.backend
.exists_child_storage(child_info, key)
.expect(EXT_NOT_ALLOWED_TO_FAIL),
};
trace!(
target: "state",
method = "ChildExists",
ext_id = %HexDisplay::from(&self.id.to_le_bytes()),
child_info = %HexDisplay::from(&child_info.storage_key()),
key = %HexDisplay::from(&key),
%result,
);
result
}
fn next_storage_key(&self, key: &[u8]) -> Option<StorageKey> {
let mut next_backend_key =
self.backend.next_storage_key(key).expect(EXT_NOT_ALLOWED_TO_FAIL);
let mut overlay_changes = self.overlay.iter_after(key).peekable();
match (&next_backend_key, overlay_changes.peek()) {
(_, None) => next_backend_key,
(Some(_), Some(_)) => {
for overlay_key in overlay_changes {
let cmp = next_backend_key.as_deref().map(|v| v.cmp(overlay_key.0));
// If `backend_key` is less than the `overlay_key`, we found out next key.
if cmp == Some(Ordering::Less) {
return next_backend_key
} else if overlay_key.1.value().is_some() {
// If there exists a value for the `overlay_key` in the overlay
// (aka the key is still valid), it means we have found our next key.
return Some(overlay_key.0.to_vec())
} else if cmp == Some(Ordering::Equal) {
// If the `backend_key` and `overlay_key` are equal, it means that we need
// to search for the next backend key, because the overlay has overwritten
// this key.
next_backend_key = self
.backend
.next_storage_key(overlay_key.0)
.expect(EXT_NOT_ALLOWED_TO_FAIL);
}
}
next_backend_key
},
(None, Some(_)) => {
// Find the next overlay key that has a value attached.
overlay_changes.find_map(|k| k.1.value().as_ref().map(|_| k.0.to_vec()))
},
}
}
fn next_child_storage_key(&self, child_info: &ChildInfo, key: &[u8]) -> Option<StorageKey> {
let mut next_backend_key = self
.backend
.next_child_storage_key(child_info, key)
.expect(EXT_NOT_ALLOWED_TO_FAIL);
let mut overlay_changes =
self.overlay.child_iter_after(child_info.storage_key(), key).peekable();
match (&next_backend_key, overlay_changes.peek()) {
(_, None) => next_backend_key,
(Some(_), Some(_)) => {
for overlay_key in overlay_changes {
let cmp = next_backend_key.as_deref().map(|v| v.cmp(overlay_key.0));
// If `backend_key` is less than the `overlay_key`, we found out next key.
if cmp == Some(Ordering::Less) {
return next_backend_key
} else if overlay_key.1.value().is_some() {
// If there exists a value for the `overlay_key` in the overlay
// (aka the key is still valid), it means we have found our next key.
return Some(overlay_key.0.to_vec())
} else if cmp == Some(Ordering::Equal) {
// If the `backend_key` and `overlay_key` are equal, it means that we need
// to search for the next backend key, because the overlay has overwritten
// this key.
next_backend_key = self
.backend
.next_child_storage_key(child_info, overlay_key.0)
.expect(EXT_NOT_ALLOWED_TO_FAIL);
}
}
next_backend_key
},
(None, Some(_)) => {
// Find the next overlay key that has a value attached.
overlay_changes.find_map(|k| k.1.value().as_ref().map(|_| k.0.to_vec()))
},
}
}
fn place_storage(&mut self, key: StorageKey, value: Option<StorageValue>) {
let _guard = guard();
if is_child_storage_key(&key) {
warn!(target: "trie", "Refuse to directly set child storage key");
return
}
// NOTE: be careful about touching the key names – used outside substrate!
trace!(
target: "state",
method = "Put",
ext_id = %HexDisplay::from(&self.id.to_le_bytes()),
key = %HexDisplay::from(&key),
value = ?value.as_ref().map(HexDisplay::from),
value_encoded = %HexDisplay::from(
&value
.as_ref()
.map(|v| EncodeOpaqueValue(v.clone()))
.encode()
),
);
self.mark_dirty();
self.overlay.set_storage(key, value);
}
fn place_child_storage(
&mut self,
child_info: &ChildInfo,
key: StorageKey,
value: Option<StorageValue>,
) {
trace!(
target: "state",
method = "ChildPut",
ext_id = %HexDisplay::from(&self.id.to_le_bytes()),
child_info = %HexDisplay::from(&child_info.storage_key()),
key = %HexDisplay::from(&key),
value = ?value.as_ref().map(HexDisplay::from),
);
let _guard = guard();
self.mark_dirty();
self.overlay.set_child_storage(child_info, key, value);
}
fn kill_child_storage(
&mut self,
child_info: &ChildInfo,
maybe_limit: Option<u32>,
maybe_cursor: Option<&[u8]>,
) -> MultiRemovalResults {
trace!(
target: "state",
method = "ChildKill",
ext_id = %HexDisplay::from(&self.id.to_le_bytes()),
child_info = %HexDisplay::from(&child_info.storage_key()),
);
let _guard = guard();
self.mark_dirty();
let overlay = self.overlay.clear_child_storage(child_info);
let (maybe_cursor, backend, loops) =
self.limit_remove_from_backend(Some(child_info), None, maybe_limit, maybe_cursor);
MultiRemovalResults { maybe_cursor, backend, unique: overlay + backend, loops }
}
fn clear_prefix(
&mut self,
prefix: &[u8],
maybe_limit: Option<u32>,
maybe_cursor: Option<&[u8]>,
) -> MultiRemovalResults {
trace!(
target: "state",
method = "ClearPrefix",
ext_id = %HexDisplay::from(&self.id.to_le_bytes()),
prefix = %HexDisplay::from(&prefix),
);
let _guard = guard();
if sp_core::storage::well_known_keys::starts_with_child_storage_key(prefix) {
warn!(
target: "trie",
"Refuse to directly clear prefix that is part or contains of child storage key",
);
return MultiRemovalResults { maybe_cursor: None, backend: 0, unique: 0, loops: 0 }
}
self.mark_dirty();
let overlay = self.overlay.clear_prefix(prefix);
let (maybe_cursor, backend, loops) =
self.limit_remove_from_backend(None, Some(prefix), maybe_limit, maybe_cursor);
MultiRemovalResults { maybe_cursor, backend, unique: overlay + backend, loops }
}
fn clear_child_prefix(
&mut self,
child_info: &ChildInfo,
prefix: &[u8],
maybe_limit: Option<u32>,
maybe_cursor: Option<&[u8]>,
) -> MultiRemovalResults {
trace!(
target: "state",
method = "ChildClearPrefix",
ext_id = %HexDisplay::from(&self.id.to_le_bytes()),
child_info = %HexDisplay::from(&child_info.storage_key()),
prefix = %HexDisplay::from(&prefix),
);
let _guard = guard();
self.mark_dirty();
let overlay = self.overlay.clear_child_prefix(child_info, prefix);
let (maybe_cursor, backend, loops) = self.limit_remove_from_backend(
Some(child_info),
Some(prefix),
maybe_limit,
maybe_cursor,
);
MultiRemovalResults { maybe_cursor, backend, unique: overlay + backend, loops }
}
fn storage_append(&mut self, key: Vec<u8>, value: Vec<u8>) {
trace!(
target: "state",
method = "Append",
ext_id = %HexDisplay::from(&self.id.to_le_bytes()),
key = %HexDisplay::from(&key),
value = %HexDisplay::from(&value),
);
let _guard = guard();
self.mark_dirty();
let backend = &mut self.backend;
let current_value = self.overlay.value_mut_or_insert_with(&key, || {
backend.storage(&key).expect(EXT_NOT_ALLOWED_TO_FAIL).unwrap_or_default()
});
StorageAppend::new(current_value).append(value);
}
fn storage_root(&mut self, state_version: StateVersion) -> Vec<u8> {
let _guard = guard();
if let Some(ref root) = self.storage_transaction_cache.transaction_storage_root {
trace!(
target: "state",
method = "StorageRoot",
ext_id = %HexDisplay::from(&self.id.to_le_bytes()),
storage_root = %HexDisplay::from(&root.as_ref()),
cached = true,
);
return root.encode()
}
let root =
self.overlay
.storage_root(self.backend, self.storage_transaction_cache, state_version);
trace!(
target: "state",
method = "StorageRoot",
ext_id = %HexDisplay::from(&self.id.to_le_bytes()),
storage_root = %HexDisplay::from(&root.as_ref()),
cached = false,
);
root.encode()
}
fn child_storage_root(
&mut self,
child_info: &ChildInfo,
state_version: StateVersion,
) -> Vec<u8> {
let _guard = guard();
let storage_key = child_info.storage_key();
let prefixed_storage_key = child_info.prefixed_storage_key();
if self.storage_transaction_cache.transaction_storage_root.is_some() {
let root = self
.storage(prefixed_storage_key.as_slice())
.and_then(|k| Decode::decode(&mut &k[..]).ok())
// V1 is equivalent to V0 on empty root.
.unwrap_or_else(empty_child_trie_root::<LayoutV1<H>>);
trace!(
target: "state",
method = "ChildStorageRoot",
ext_id = %HexDisplay::from(&self.id.to_le_bytes()),
child_info = %HexDisplay::from(&storage_key),
storage_root = %HexDisplay::from(&root.as_ref()),
cached = true,
);
root.encode()
} else {
let root = if let Some((changes, info)) = self.overlay.child_changes(storage_key) {
let delta = changes.map(|(k, v)| (k.as_ref(), v.value().map(AsRef::as_ref)));
Some(self.backend.child_storage_root(info, delta, state_version))
} else {
None
};
if let Some((root, is_empty, _)) = root {
let root = root.encode();
// We store update in the overlay in order to be able to use
// 'self.storage_transaction' cache. This is brittle as it rely on Ext only querying
// the trie backend for storage root.
// A better design would be to manage 'child_storage_transaction' in a
// similar way as 'storage_transaction' but for each child trie.
if is_empty {
self.overlay.set_storage(prefixed_storage_key.into_inner(), None);
} else {
self.overlay.set_storage(prefixed_storage_key.into_inner(), Some(root.clone()));
}
trace!(
target: "state",
method = "ChildStorageRoot",
ext_id = %HexDisplay::from(&self.id.to_le_bytes()),
child_info = %HexDisplay::from(&storage_key),
storage_root = %HexDisplay::from(&root.as_ref()),
cached = false,
);
root
} else {
// empty overlay
let root = self
.storage(prefixed_storage_key.as_slice())
.and_then(|k| Decode::decode(&mut &k[..]).ok())
// V1 is equivalent to V0 on empty root.
.unwrap_or_else(empty_child_trie_root::<LayoutV1<H>>);
trace!(
target: "state",
method = "ChildStorageRoot",
ext_id = %HexDisplay::from(&self.id.to_le_bytes()),
child_info = %HexDisplay::from(&storage_key),
storage_root = %HexDisplay::from(&root.as_ref()),
cached = false,
);
root.encode()
}
}
}
fn storage_index_transaction(&mut self, index: u32, hash: &[u8], size: u32) {
trace!(
target: "state",
method = "IndexTransaction",
ext_id = %HexDisplay::from(&self.id.to_le_bytes()),
%index,
tx_hash = %HexDisplay::from(&hash),
%size,
);
self.overlay.add_transaction_index(IndexOperation::Insert {
extrinsic: index,
hash: hash.to_vec(),
size,
});
}
/// Renew existing piece of data storage.
fn storage_renew_transaction_index(&mut self, index: u32, hash: &[u8]) {
trace!(
target: "state",
method = "RenewTransactionIndex",
ext_id = %HexDisplay::from(&self.id.to_le_bytes()),
%index,
tx_hash = %HexDisplay::from(&hash),
);
self.overlay
.add_transaction_index(IndexOperation::Renew { extrinsic: index, hash: hash.to_vec() });
}
fn storage_start_transaction(&mut self) {
self.overlay.start_transaction()
}
fn storage_rollback_transaction(&mut self) -> Result<(), ()> {
self.mark_dirty();
self.overlay.rollback_transaction().map_err(|_| ())
}
fn storage_commit_transaction(&mut self) -> Result<(), ()> {
self.overlay.commit_transaction().map_err(|_| ())
}
fn wipe(&mut self) {
for _ in 0..self.overlay.transaction_depth() {
self.overlay.rollback_transaction().expect(BENCHMARKING_FN);
}
self.overlay
.drain_storage_changes(
self.backend,
self.storage_transaction_cache,
Default::default(), // using any state
)
.expect(EXT_NOT_ALLOWED_TO_FAIL);
self.backend.wipe().expect(EXT_NOT_ALLOWED_TO_FAIL);
self.mark_dirty();
self.overlay
.enter_runtime()
.expect("We have reset the overlay above, so we can not be in the runtime; qed");
}
fn commit(&mut self) {
// Bench always use latest state.
let state_version = StateVersion::default();
for _ in 0..self.overlay.transaction_depth() {
self.overlay.commit_transaction().expect(BENCHMARKING_FN);
}
let changes = self
.overlay
.drain_storage_changes(self.backend, self.storage_transaction_cache, state_version)
.expect(EXT_NOT_ALLOWED_TO_FAIL);
self.backend
.commit(
changes.transaction_storage_root,
changes.transaction,
changes.main_storage_changes,
changes.child_storage_changes,
)
.expect(EXT_NOT_ALLOWED_TO_FAIL);
self.mark_dirty();
self.overlay
.enter_runtime()
.expect("We have reset the overlay above, so we can not be in the runtime; qed");
}
fn read_write_count(&self) -> (u32, u32, u32, u32) {
self.backend.read_write_count()
}
fn reset_read_write_count(&mut self) {
self.backend.reset_read_write_count()
}
fn get_whitelist(&self) -> Vec<TrackedStorageKey> {
self.backend.get_whitelist()
}
fn set_whitelist(&mut self, new: Vec<TrackedStorageKey>) {
self.backend.set_whitelist(new)
}
fn proof_size(&self) -> Option<u32> {
self.backend.proof_size()
}
fn get_read_and_written_keys(&self) -> Vec<(Vec<u8>, u32, u32, bool)> {
self.backend.get_read_and_written_keys()
}
}
impl<'a, H, B> Ext<'a, H, B>
where
H: Hasher,
H::Out: Ord + 'static + codec::Codec,
B: Backend<H>,
{
fn limit_remove_from_backend(
&mut self,
maybe_child: Option<&ChildInfo>,
maybe_prefix: Option<&[u8]>,
maybe_limit: Option<u32>,
maybe_cursor: Option<&[u8]>,
) -> (Option<Vec<u8>>, u32, u32) {
let mut delete_count: u32 = 0;
let mut loop_count: u32 = 0;
let mut maybe_next_key = None;
self.backend
.apply_to_keys_while(maybe_child, maybe_prefix, maybe_cursor, |key| {
if maybe_limit.map_or(false, |limit| loop_count == limit) {
maybe_next_key = Some(key.to_vec());
return false
}
let overlay = match maybe_child {
Some(child_info) => self.overlay.child_storage(child_info, key),
None => self.overlay.storage(key),
};
if !matches!(overlay, Some(None)) {
// not pending deletion from the backend - delete it.
if let Some(child_info) = maybe_child {
self.overlay.set_child_storage(child_info, key.to_vec(), None);
} else {
self.overlay.set_storage(key.to_vec(), None);
}
delete_count = delete_count.saturating_add(1);
}
loop_count = loop_count.saturating_add(1);
true
});
(maybe_next_key, delete_count, loop_count)
}
sourcepub fn value_mut_or_insert_with(
&mut self,
key: &[u8],
init: impl Fn() -> StorageValue
) -> &mut StorageValue
pub fn value_mut_or_insert_with(
&mut self,
key: &[u8],
init: impl Fn() -> StorageValue
) -> &mut StorageValue
Returns mutable reference to current value. If there is no value in the overlay, the given callback is used to initiate the value. Warning this function registers a change, so the mutable reference MUST be modified.
Can be rolled back or committed when called inside a transaction.
Examples found in repository?
More examples
512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529
fn storage_append(&mut self, key: Vec<u8>, value: Vec<u8>) {
trace!(
target: "state",
method = "Append",
ext_id = %HexDisplay::from(&self.id.to_le_bytes()),
key = %HexDisplay::from(&key),
value = %HexDisplay::from(&value),
);
let _guard = guard();
self.mark_dirty();
let backend = &mut self.backend;
let current_value = self.overlay.value_mut_or_insert_with(&key, || {
backend.storage(&key).expect(EXT_NOT_ALLOWED_TO_FAIL).unwrap_or_default()
});
StorageAppend::new(current_value).append(value);
}
sourcepub fn child_storage(
&self,
child_info: &ChildInfo,
key: &[u8]
) -> Option<Option<&[u8]>>
pub fn child_storage(
&self,
child_info: &ChildInfo,
key: &[u8]
) -> Option<Option<&[u8]>>
Returns a double-Option: None if the key is unknown (i.e. and the query should be referred to the backend); Some(None) if the key has been deleted. Some(Some(…)) for a key whose value has been set.
Examples found in repository?
More examples
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 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783
fn child_storage(&self, child_info: &ChildInfo, key: &[u8]) -> Option<StorageValue> {
let _guard = guard();
let result = self
.overlay
.child_storage(child_info, key)
.map(|x| x.map(|x| x.to_vec()))
.unwrap_or_else(|| {
self.backend.child_storage(child_info, key).expect(EXT_NOT_ALLOWED_TO_FAIL)
});
trace!(
target: "state",
method = "ChildGet",
ext_id = %HexDisplay::from(&self.id.to_le_bytes()),
child_info = %HexDisplay::from(&child_info.storage_key()),
key = %HexDisplay::from(&key),
result = ?result.as_ref().map(HexDisplay::from)
);
result
}
fn child_storage_hash(&self, child_info: &ChildInfo, key: &[u8]) -> Option<Vec<u8>> {
let _guard = guard();
let result = self
.overlay
.child_storage(child_info, key)
.map(|x| x.map(|x| H::hash(x)))
.unwrap_or_else(|| {
self.backend.child_storage_hash(child_info, key).expect(EXT_NOT_ALLOWED_TO_FAIL)
});
trace!(
target: "state",
method = "ChildHash",
ext_id = %HexDisplay::from(&self.id.to_le_bytes()),
child_info = %HexDisplay::from(&child_info.storage_key()),
key = %HexDisplay::from(&key),
?result,
);
result.map(|r| r.encode())
}
fn exists_storage(&self, key: &[u8]) -> bool {
let _guard = guard();
let result = match self.overlay.storage(key) {
Some(x) => x.is_some(),
_ => self.backend.exists_storage(key).expect(EXT_NOT_ALLOWED_TO_FAIL),
};
trace!(
target: "state",
method = "Exists",
ext_id = %HexDisplay::from(&self.id.to_le_bytes()),
key = %HexDisplay::from(&key),
%result,
);
result
}
fn exists_child_storage(&self, child_info: &ChildInfo, key: &[u8]) -> bool {
let _guard = guard();
let result = match self.overlay.child_storage(child_info, key) {
Some(x) => x.is_some(),
_ => self
.backend
.exists_child_storage(child_info, key)
.expect(EXT_NOT_ALLOWED_TO_FAIL),
};
trace!(
target: "state",
method = "ChildExists",
ext_id = %HexDisplay::from(&self.id.to_le_bytes()),
child_info = %HexDisplay::from(&child_info.storage_key()),
key = %HexDisplay::from(&key),
%result,
);
result
}
fn next_storage_key(&self, key: &[u8]) -> Option<StorageKey> {
let mut next_backend_key =
self.backend.next_storage_key(key).expect(EXT_NOT_ALLOWED_TO_FAIL);
let mut overlay_changes = self.overlay.iter_after(key).peekable();
match (&next_backend_key, overlay_changes.peek()) {
(_, None) => next_backend_key,
(Some(_), Some(_)) => {
for overlay_key in overlay_changes {
let cmp = next_backend_key.as_deref().map(|v| v.cmp(overlay_key.0));
// If `backend_key` is less than the `overlay_key`, we found out next key.
if cmp == Some(Ordering::Less) {
return next_backend_key
} else if overlay_key.1.value().is_some() {
// If there exists a value for the `overlay_key` in the overlay
// (aka the key is still valid), it means we have found our next key.
return Some(overlay_key.0.to_vec())
} else if cmp == Some(Ordering::Equal) {
// If the `backend_key` and `overlay_key` are equal, it means that we need
// to search for the next backend key, because the overlay has overwritten
// this key.
next_backend_key = self
.backend
.next_storage_key(overlay_key.0)
.expect(EXT_NOT_ALLOWED_TO_FAIL);
}
}
next_backend_key
},
(None, Some(_)) => {
// Find the next overlay key that has a value attached.
overlay_changes.find_map(|k| k.1.value().as_ref().map(|_| k.0.to_vec()))
},
}
}
fn next_child_storage_key(&self, child_info: &ChildInfo, key: &[u8]) -> Option<StorageKey> {
let mut next_backend_key = self
.backend
.next_child_storage_key(child_info, key)
.expect(EXT_NOT_ALLOWED_TO_FAIL);
let mut overlay_changes =
self.overlay.child_iter_after(child_info.storage_key(), key).peekable();
match (&next_backend_key, overlay_changes.peek()) {
(_, None) => next_backend_key,
(Some(_), Some(_)) => {
for overlay_key in overlay_changes {
let cmp = next_backend_key.as_deref().map(|v| v.cmp(overlay_key.0));
// If `backend_key` is less than the `overlay_key`, we found out next key.
if cmp == Some(Ordering::Less) {
return next_backend_key
} else if overlay_key.1.value().is_some() {
// If there exists a value for the `overlay_key` in the overlay
// (aka the key is still valid), it means we have found our next key.
return Some(overlay_key.0.to_vec())
} else if cmp == Some(Ordering::Equal) {
// If the `backend_key` and `overlay_key` are equal, it means that we need
// to search for the next backend key, because the overlay has overwritten
// this key.
next_backend_key = self
.backend
.next_child_storage_key(child_info, overlay_key.0)
.expect(EXT_NOT_ALLOWED_TO_FAIL);
}
}
next_backend_key
},
(None, Some(_)) => {
// Find the next overlay key that has a value attached.
overlay_changes.find_map(|k| k.1.value().as_ref().map(|_| k.0.to_vec()))
},
}
}
fn place_storage(&mut self, key: StorageKey, value: Option<StorageValue>) {
let _guard = guard();
if is_child_storage_key(&key) {
warn!(target: "trie", "Refuse to directly set child storage key");
return
}
// NOTE: be careful about touching the key names – used outside substrate!
trace!(
target: "state",
method = "Put",
ext_id = %HexDisplay::from(&self.id.to_le_bytes()),
key = %HexDisplay::from(&key),
value = ?value.as_ref().map(HexDisplay::from),
value_encoded = %HexDisplay::from(
&value
.as_ref()
.map(|v| EncodeOpaqueValue(v.clone()))
.encode()
),
);
self.mark_dirty();
self.overlay.set_storage(key, value);
}
fn place_child_storage(
&mut self,
child_info: &ChildInfo,
key: StorageKey,
value: Option<StorageValue>,
) {
trace!(
target: "state",
method = "ChildPut",
ext_id = %HexDisplay::from(&self.id.to_le_bytes()),
child_info = %HexDisplay::from(&child_info.storage_key()),
key = %HexDisplay::from(&key),
value = ?value.as_ref().map(HexDisplay::from),
);
let _guard = guard();
self.mark_dirty();
self.overlay.set_child_storage(child_info, key, value);
}
fn kill_child_storage(
&mut self,
child_info: &ChildInfo,
maybe_limit: Option<u32>,
maybe_cursor: Option<&[u8]>,
) -> MultiRemovalResults {
trace!(
target: "state",
method = "ChildKill",
ext_id = %HexDisplay::from(&self.id.to_le_bytes()),
child_info = %HexDisplay::from(&child_info.storage_key()),
);
let _guard = guard();
self.mark_dirty();
let overlay = self.overlay.clear_child_storage(child_info);
let (maybe_cursor, backend, loops) =
self.limit_remove_from_backend(Some(child_info), None, maybe_limit, maybe_cursor);
MultiRemovalResults { maybe_cursor, backend, unique: overlay + backend, loops }
}
fn clear_prefix(
&mut self,
prefix: &[u8],
maybe_limit: Option<u32>,
maybe_cursor: Option<&[u8]>,
) -> MultiRemovalResults {
trace!(
target: "state",
method = "ClearPrefix",
ext_id = %HexDisplay::from(&self.id.to_le_bytes()),
prefix = %HexDisplay::from(&prefix),
);
let _guard = guard();
if sp_core::storage::well_known_keys::starts_with_child_storage_key(prefix) {
warn!(
target: "trie",
"Refuse to directly clear prefix that is part or contains of child storage key",
);
return MultiRemovalResults { maybe_cursor: None, backend: 0, unique: 0, loops: 0 }
}
self.mark_dirty();
let overlay = self.overlay.clear_prefix(prefix);
let (maybe_cursor, backend, loops) =
self.limit_remove_from_backend(None, Some(prefix), maybe_limit, maybe_cursor);
MultiRemovalResults { maybe_cursor, backend, unique: overlay + backend, loops }
}
fn clear_child_prefix(
&mut self,
child_info: &ChildInfo,
prefix: &[u8],
maybe_limit: Option<u32>,
maybe_cursor: Option<&[u8]>,
) -> MultiRemovalResults {
trace!(
target: "state",
method = "ChildClearPrefix",
ext_id = %HexDisplay::from(&self.id.to_le_bytes()),
child_info = %HexDisplay::from(&child_info.storage_key()),
prefix = %HexDisplay::from(&prefix),
);
let _guard = guard();
self.mark_dirty();
let overlay = self.overlay.clear_child_prefix(child_info, prefix);
let (maybe_cursor, backend, loops) = self.limit_remove_from_backend(
Some(child_info),
Some(prefix),
maybe_limit,
maybe_cursor,
);
MultiRemovalResults { maybe_cursor, backend, unique: overlay + backend, loops }
}
fn storage_append(&mut self, key: Vec<u8>, value: Vec<u8>) {
trace!(
target: "state",
method = "Append",
ext_id = %HexDisplay::from(&self.id.to_le_bytes()),
key = %HexDisplay::from(&key),
value = %HexDisplay::from(&value),
);
let _guard = guard();
self.mark_dirty();
let backend = &mut self.backend;
let current_value = self.overlay.value_mut_or_insert_with(&key, || {
backend.storage(&key).expect(EXT_NOT_ALLOWED_TO_FAIL).unwrap_or_default()
});
StorageAppend::new(current_value).append(value);
}
fn storage_root(&mut self, state_version: StateVersion) -> Vec<u8> {
let _guard = guard();
if let Some(ref root) = self.storage_transaction_cache.transaction_storage_root {
trace!(
target: "state",
method = "StorageRoot",
ext_id = %HexDisplay::from(&self.id.to_le_bytes()),
storage_root = %HexDisplay::from(&root.as_ref()),
cached = true,
);
return root.encode()
}
let root =
self.overlay
.storage_root(self.backend, self.storage_transaction_cache, state_version);
trace!(
target: "state",
method = "StorageRoot",
ext_id = %HexDisplay::from(&self.id.to_le_bytes()),
storage_root = %HexDisplay::from(&root.as_ref()),
cached = false,
);
root.encode()
}
fn child_storage_root(
&mut self,
child_info: &ChildInfo,
state_version: StateVersion,
) -> Vec<u8> {
let _guard = guard();
let storage_key = child_info.storage_key();
let prefixed_storage_key = child_info.prefixed_storage_key();
if self.storage_transaction_cache.transaction_storage_root.is_some() {
let root = self
.storage(prefixed_storage_key.as_slice())
.and_then(|k| Decode::decode(&mut &k[..]).ok())
// V1 is equivalent to V0 on empty root.
.unwrap_or_else(empty_child_trie_root::<LayoutV1<H>>);
trace!(
target: "state",
method = "ChildStorageRoot",
ext_id = %HexDisplay::from(&self.id.to_le_bytes()),
child_info = %HexDisplay::from(&storage_key),
storage_root = %HexDisplay::from(&root.as_ref()),
cached = true,
);
root.encode()
} else {
let root = if let Some((changes, info)) = self.overlay.child_changes(storage_key) {
let delta = changes.map(|(k, v)| (k.as_ref(), v.value().map(AsRef::as_ref)));
Some(self.backend.child_storage_root(info, delta, state_version))
} else {
None
};
if let Some((root, is_empty, _)) = root {
let root = root.encode();
// We store update in the overlay in order to be able to use
// 'self.storage_transaction' cache. This is brittle as it rely on Ext only querying
// the trie backend for storage root.
// A better design would be to manage 'child_storage_transaction' in a
// similar way as 'storage_transaction' but for each child trie.
if is_empty {
self.overlay.set_storage(prefixed_storage_key.into_inner(), None);
} else {
self.overlay.set_storage(prefixed_storage_key.into_inner(), Some(root.clone()));
}
trace!(
target: "state",
method = "ChildStorageRoot",
ext_id = %HexDisplay::from(&self.id.to_le_bytes()),
child_info = %HexDisplay::from(&storage_key),
storage_root = %HexDisplay::from(&root.as_ref()),
cached = false,
);
root
} else {
// empty overlay
let root = self
.storage(prefixed_storage_key.as_slice())
.and_then(|k| Decode::decode(&mut &k[..]).ok())
// V1 is equivalent to V0 on empty root.
.unwrap_or_else(empty_child_trie_root::<LayoutV1<H>>);
trace!(
target: "state",
method = "ChildStorageRoot",
ext_id = %HexDisplay::from(&self.id.to_le_bytes()),
child_info = %HexDisplay::from(&storage_key),
storage_root = %HexDisplay::from(&root.as_ref()),
cached = false,
);
root.encode()
}
}
}
fn storage_index_transaction(&mut self, index: u32, hash: &[u8], size: u32) {
trace!(
target: "state",
method = "IndexTransaction",
ext_id = %HexDisplay::from(&self.id.to_le_bytes()),
%index,
tx_hash = %HexDisplay::from(&hash),
%size,
);
self.overlay.add_transaction_index(IndexOperation::Insert {
extrinsic: index,
hash: hash.to_vec(),
size,
});
}
/// Renew existing piece of data storage.
fn storage_renew_transaction_index(&mut self, index: u32, hash: &[u8]) {
trace!(
target: "state",
method = "RenewTransactionIndex",
ext_id = %HexDisplay::from(&self.id.to_le_bytes()),
%index,
tx_hash = %HexDisplay::from(&hash),
);
self.overlay
.add_transaction_index(IndexOperation::Renew { extrinsic: index, hash: hash.to_vec() });
}
fn storage_start_transaction(&mut self) {
self.overlay.start_transaction()
}
fn storage_rollback_transaction(&mut self) -> Result<(), ()> {
self.mark_dirty();
self.overlay.rollback_transaction().map_err(|_| ())
}
fn storage_commit_transaction(&mut self) -> Result<(), ()> {
self.overlay.commit_transaction().map_err(|_| ())
}
fn wipe(&mut self) {
for _ in 0..self.overlay.transaction_depth() {
self.overlay.rollback_transaction().expect(BENCHMARKING_FN);
}
self.overlay
.drain_storage_changes(
self.backend,
self.storage_transaction_cache,
Default::default(), // using any state
)
.expect(EXT_NOT_ALLOWED_TO_FAIL);
self.backend.wipe().expect(EXT_NOT_ALLOWED_TO_FAIL);
self.mark_dirty();
self.overlay
.enter_runtime()
.expect("We have reset the overlay above, so we can not be in the runtime; qed");
}
fn commit(&mut self) {
// Bench always use latest state.
let state_version = StateVersion::default();
for _ in 0..self.overlay.transaction_depth() {
self.overlay.commit_transaction().expect(BENCHMARKING_FN);
}
let changes = self
.overlay
.drain_storage_changes(self.backend, self.storage_transaction_cache, state_version)
.expect(EXT_NOT_ALLOWED_TO_FAIL);
self.backend
.commit(
changes.transaction_storage_root,
changes.transaction,
changes.main_storage_changes,
changes.child_storage_changes,
)
.expect(EXT_NOT_ALLOWED_TO_FAIL);
self.mark_dirty();
self.overlay
.enter_runtime()
.expect("We have reset the overlay above, so we can not be in the runtime; qed");
}
fn read_write_count(&self) -> (u32, u32, u32, u32) {
self.backend.read_write_count()
}
fn reset_read_write_count(&mut self) {
self.backend.reset_read_write_count()
}
fn get_whitelist(&self) -> Vec<TrackedStorageKey> {
self.backend.get_whitelist()
}
fn set_whitelist(&mut self, new: Vec<TrackedStorageKey>) {
self.backend.set_whitelist(new)
}
fn proof_size(&self) -> Option<u32> {
self.backend.proof_size()
}
fn get_read_and_written_keys(&self) -> Vec<(Vec<u8>, u32, u32, bool)> {
self.backend.get_read_and_written_keys()
}
}
impl<'a, H, B> Ext<'a, H, B>
where
H: Hasher,
H::Out: Ord + 'static + codec::Codec,
B: Backend<H>,
{
fn limit_remove_from_backend(
&mut self,
maybe_child: Option<&ChildInfo>,
maybe_prefix: Option<&[u8]>,
maybe_limit: Option<u32>,
maybe_cursor: Option<&[u8]>,
) -> (Option<Vec<u8>>, u32, u32) {
let mut delete_count: u32 = 0;
let mut loop_count: u32 = 0;
let mut maybe_next_key = None;
self.backend
.apply_to_keys_while(maybe_child, maybe_prefix, maybe_cursor, |key| {
if maybe_limit.map_or(false, |limit| loop_count == limit) {
maybe_next_key = Some(key.to_vec());
return false
}
let overlay = match maybe_child {
Some(child_info) => self.overlay.child_storage(child_info, key),
None => self.overlay.storage(key),
};
if !matches!(overlay, Some(None)) {
// not pending deletion from the backend - delete it.
if let Some(child_info) = maybe_child {
self.overlay.set_child_storage(child_info, key.to_vec(), None);
} else {
self.overlay.set_storage(key.to_vec(), None);
}
delete_count = delete_count.saturating_add(1);
}
loop_count = loop_count.saturating_add(1);
true
});
(maybe_next_key, delete_count, loop_count)
}
sourcepub fn set_storage(&mut self, key: StorageKey, val: Option<StorageValue>)
pub fn set_storage(&mut self, key: StorageKey, val: Option<StorageValue>)
Set a new value for the specified key.
Can be rolled back or committed when called inside a transaction.
Examples found in repository?
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
pub fn insert(&mut self, k: StorageKey, v: StorageValue) {
self.overlay.set_storage(k, Some(v));
}
/// Consume self and returns inner storages
pub fn into_storages(self) -> Storage {
Storage {
top: self
.overlay
.changes()
.filter_map(|(k, v)| v.value().map(|v| (k.to_vec(), v.to_vec())))
.collect(),
children_default: self
.overlay
.children()
.map(|(iter, i)| {
(
i.storage_key().to_vec(),
sp_core::storage::StorageChild {
data: iter
.filter_map(|(k, v)| v.value().map(|v| (k.to_vec(), v.to_vec())))
.collect(),
child_info: i.clone(),
},
)
})
.collect(),
}
}
/// Execute the given closure `f` with the externalities set and initialized with `storage`.
///
/// Returns the result of the closure and updates `storage` with all changes.
pub fn execute_with_storage<R>(
storage: &mut sp_core::storage::Storage,
f: impl FnOnce() -> R,
) -> R {
let mut ext = Self::new(std::mem::take(storage));
let r = ext.execute_with(f);
*storage = ext.into_storages();
r
}
/// Execute the given closure while `self` is set as externalities.
///
/// Returns the result of the given closure.
pub fn execute_with<R>(&mut self, f: impl FnOnce() -> R) -> R {
sp_externalities::set_and_run_with_externalities(self, f)
}
/// List of active extensions.
pub fn extensions(&mut self) -> &mut Extensions {
&mut self.extensions
}
/// Register an extension.
pub fn register_extension(&mut self, ext: impl Extension) {
self.extensions.register(ext);
}
}
impl PartialEq for BasicExternalities {
fn eq(&self, other: &BasicExternalities) -> bool {
self.overlay.changes().map(|(k, v)| (k, v.value())).collect::<BTreeMap<_, _>>() ==
other.overlay.changes().map(|(k, v)| (k, v.value())).collect::<BTreeMap<_, _>>() &&
self.overlay
.children()
.map(|(iter, i)| (i, iter.map(|(k, v)| (k, v.value())).collect::<BTreeMap<_, _>>()))
.collect::<BTreeMap<_, _>>() ==
other
.overlay
.children()
.map(|(iter, i)| {
(i, iter.map(|(k, v)| (k, v.value())).collect::<BTreeMap<_, _>>())
})
.collect::<BTreeMap<_, _>>()
}
}
impl FromIterator<(StorageKey, StorageValue)> for BasicExternalities {
fn from_iter<I: IntoIterator<Item = (StorageKey, StorageValue)>>(iter: I) -> Self {
let mut t = Self::default();
iter.into_iter().for_each(|(k, v)| t.insert(k, v));
t
}
}
impl Default for BasicExternalities {
fn default() -> Self {
Self::new(Default::default())
}
}
impl From<BTreeMap<StorageKey, StorageValue>> for BasicExternalities {
fn from(map: BTreeMap<StorageKey, StorageValue>) -> Self {
Self::from_iter(map.into_iter())
}
}
impl Externalities for BasicExternalities {
fn set_offchain_storage(&mut self, _key: &[u8], _value: Option<&[u8]>) {}
fn storage(&self, key: &[u8]) -> Option<StorageValue> {
self.overlay.storage(key).and_then(|v| v.map(|v| v.to_vec()))
}
fn storage_hash(&self, key: &[u8]) -> Option<Vec<u8>> {
self.storage(key).map(|v| Blake2Hasher::hash(&v).encode())
}
fn child_storage(&self, child_info: &ChildInfo, key: &[u8]) -> Option<StorageValue> {
self.overlay.child_storage(child_info, key).and_then(|v| v.map(|v| v.to_vec()))
}
fn child_storage_hash(&self, child_info: &ChildInfo, key: &[u8]) -> Option<Vec<u8>> {
self.child_storage(child_info, key).map(|v| Blake2Hasher::hash(&v).encode())
}
fn next_storage_key(&self, key: &[u8]) -> Option<StorageKey> {
self.overlay.iter_after(key).find_map(|(k, v)| v.value().map(|_| k.to_vec()))
}
fn next_child_storage_key(&self, child_info: &ChildInfo, key: &[u8]) -> Option<StorageKey> {
self.overlay
.child_iter_after(child_info.storage_key(), key)
.find_map(|(k, v)| v.value().map(|_| k.to_vec()))
}
fn place_storage(&mut self, key: StorageKey, maybe_value: Option<StorageValue>) {
if is_child_storage_key(&key) {
warn!(target: "trie", "Refuse to set child storage key via main storage");
return
}
self.overlay.set_storage(key, maybe_value)
}
More examples
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 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783
fn place_storage(&mut self, key: StorageKey, value: Option<StorageValue>) {
let _guard = guard();
if is_child_storage_key(&key) {
warn!(target: "trie", "Refuse to directly set child storage key");
return
}
// NOTE: be careful about touching the key names – used outside substrate!
trace!(
target: "state",
method = "Put",
ext_id = %HexDisplay::from(&self.id.to_le_bytes()),
key = %HexDisplay::from(&key),
value = ?value.as_ref().map(HexDisplay::from),
value_encoded = %HexDisplay::from(
&value
.as_ref()
.map(|v| EncodeOpaqueValue(v.clone()))
.encode()
),
);
self.mark_dirty();
self.overlay.set_storage(key, value);
}
fn place_child_storage(
&mut self,
child_info: &ChildInfo,
key: StorageKey,
value: Option<StorageValue>,
) {
trace!(
target: "state",
method = "ChildPut",
ext_id = %HexDisplay::from(&self.id.to_le_bytes()),
child_info = %HexDisplay::from(&child_info.storage_key()),
key = %HexDisplay::from(&key),
value = ?value.as_ref().map(HexDisplay::from),
);
let _guard = guard();
self.mark_dirty();
self.overlay.set_child_storage(child_info, key, value);
}
fn kill_child_storage(
&mut self,
child_info: &ChildInfo,
maybe_limit: Option<u32>,
maybe_cursor: Option<&[u8]>,
) -> MultiRemovalResults {
trace!(
target: "state",
method = "ChildKill",
ext_id = %HexDisplay::from(&self.id.to_le_bytes()),
child_info = %HexDisplay::from(&child_info.storage_key()),
);
let _guard = guard();
self.mark_dirty();
let overlay = self.overlay.clear_child_storage(child_info);
let (maybe_cursor, backend, loops) =
self.limit_remove_from_backend(Some(child_info), None, maybe_limit, maybe_cursor);
MultiRemovalResults { maybe_cursor, backend, unique: overlay + backend, loops }
}
fn clear_prefix(
&mut self,
prefix: &[u8],
maybe_limit: Option<u32>,
maybe_cursor: Option<&[u8]>,
) -> MultiRemovalResults {
trace!(
target: "state",
method = "ClearPrefix",
ext_id = %HexDisplay::from(&self.id.to_le_bytes()),
prefix = %HexDisplay::from(&prefix),
);
let _guard = guard();
if sp_core::storage::well_known_keys::starts_with_child_storage_key(prefix) {
warn!(
target: "trie",
"Refuse to directly clear prefix that is part or contains of child storage key",
);
return MultiRemovalResults { maybe_cursor: None, backend: 0, unique: 0, loops: 0 }
}
self.mark_dirty();
let overlay = self.overlay.clear_prefix(prefix);
let (maybe_cursor, backend, loops) =
self.limit_remove_from_backend(None, Some(prefix), maybe_limit, maybe_cursor);
MultiRemovalResults { maybe_cursor, backend, unique: overlay + backend, loops }
}
fn clear_child_prefix(
&mut self,
child_info: &ChildInfo,
prefix: &[u8],
maybe_limit: Option<u32>,
maybe_cursor: Option<&[u8]>,
) -> MultiRemovalResults {
trace!(
target: "state",
method = "ChildClearPrefix",
ext_id = %HexDisplay::from(&self.id.to_le_bytes()),
child_info = %HexDisplay::from(&child_info.storage_key()),
prefix = %HexDisplay::from(&prefix),
);
let _guard = guard();
self.mark_dirty();
let overlay = self.overlay.clear_child_prefix(child_info, prefix);
let (maybe_cursor, backend, loops) = self.limit_remove_from_backend(
Some(child_info),
Some(prefix),
maybe_limit,
maybe_cursor,
);
MultiRemovalResults { maybe_cursor, backend, unique: overlay + backend, loops }
}
fn storage_append(&mut self, key: Vec<u8>, value: Vec<u8>) {
trace!(
target: "state",
method = "Append",
ext_id = %HexDisplay::from(&self.id.to_le_bytes()),
key = %HexDisplay::from(&key),
value = %HexDisplay::from(&value),
);
let _guard = guard();
self.mark_dirty();
let backend = &mut self.backend;
let current_value = self.overlay.value_mut_or_insert_with(&key, || {
backend.storage(&key).expect(EXT_NOT_ALLOWED_TO_FAIL).unwrap_or_default()
});
StorageAppend::new(current_value).append(value);
}
fn storage_root(&mut self, state_version: StateVersion) -> Vec<u8> {
let _guard = guard();
if let Some(ref root) = self.storage_transaction_cache.transaction_storage_root {
trace!(
target: "state",
method = "StorageRoot",
ext_id = %HexDisplay::from(&self.id.to_le_bytes()),
storage_root = %HexDisplay::from(&root.as_ref()),
cached = true,
);
return root.encode()
}
let root =
self.overlay
.storage_root(self.backend, self.storage_transaction_cache, state_version);
trace!(
target: "state",
method = "StorageRoot",
ext_id = %HexDisplay::from(&self.id.to_le_bytes()),
storage_root = %HexDisplay::from(&root.as_ref()),
cached = false,
);
root.encode()
}
fn child_storage_root(
&mut self,
child_info: &ChildInfo,
state_version: StateVersion,
) -> Vec<u8> {
let _guard = guard();
let storage_key = child_info.storage_key();
let prefixed_storage_key = child_info.prefixed_storage_key();
if self.storage_transaction_cache.transaction_storage_root.is_some() {
let root = self
.storage(prefixed_storage_key.as_slice())
.and_then(|k| Decode::decode(&mut &k[..]).ok())
// V1 is equivalent to V0 on empty root.
.unwrap_or_else(empty_child_trie_root::<LayoutV1<H>>);
trace!(
target: "state",
method = "ChildStorageRoot",
ext_id = %HexDisplay::from(&self.id.to_le_bytes()),
child_info = %HexDisplay::from(&storage_key),
storage_root = %HexDisplay::from(&root.as_ref()),
cached = true,
);
root.encode()
} else {
let root = if let Some((changes, info)) = self.overlay.child_changes(storage_key) {
let delta = changes.map(|(k, v)| (k.as_ref(), v.value().map(AsRef::as_ref)));
Some(self.backend.child_storage_root(info, delta, state_version))
} else {
None
};
if let Some((root, is_empty, _)) = root {
let root = root.encode();
// We store update in the overlay in order to be able to use
// 'self.storage_transaction' cache. This is brittle as it rely on Ext only querying
// the trie backend for storage root.
// A better design would be to manage 'child_storage_transaction' in a
// similar way as 'storage_transaction' but for each child trie.
if is_empty {
self.overlay.set_storage(prefixed_storage_key.into_inner(), None);
} else {
self.overlay.set_storage(prefixed_storage_key.into_inner(), Some(root.clone()));
}
trace!(
target: "state",
method = "ChildStorageRoot",
ext_id = %HexDisplay::from(&self.id.to_le_bytes()),
child_info = %HexDisplay::from(&storage_key),
storage_root = %HexDisplay::from(&root.as_ref()),
cached = false,
);
root
} else {
// empty overlay
let root = self
.storage(prefixed_storage_key.as_slice())
.and_then(|k| Decode::decode(&mut &k[..]).ok())
// V1 is equivalent to V0 on empty root.
.unwrap_or_else(empty_child_trie_root::<LayoutV1<H>>);
trace!(
target: "state",
method = "ChildStorageRoot",
ext_id = %HexDisplay::from(&self.id.to_le_bytes()),
child_info = %HexDisplay::from(&storage_key),
storage_root = %HexDisplay::from(&root.as_ref()),
cached = false,
);
root.encode()
}
}
}
fn storage_index_transaction(&mut self, index: u32, hash: &[u8], size: u32) {
trace!(
target: "state",
method = "IndexTransaction",
ext_id = %HexDisplay::from(&self.id.to_le_bytes()),
%index,
tx_hash = %HexDisplay::from(&hash),
%size,
);
self.overlay.add_transaction_index(IndexOperation::Insert {
extrinsic: index,
hash: hash.to_vec(),
size,
});
}
/// Renew existing piece of data storage.
fn storage_renew_transaction_index(&mut self, index: u32, hash: &[u8]) {
trace!(
target: "state",
method = "RenewTransactionIndex",
ext_id = %HexDisplay::from(&self.id.to_le_bytes()),
%index,
tx_hash = %HexDisplay::from(&hash),
);
self.overlay
.add_transaction_index(IndexOperation::Renew { extrinsic: index, hash: hash.to_vec() });
}
fn storage_start_transaction(&mut self) {
self.overlay.start_transaction()
}
fn storage_rollback_transaction(&mut self) -> Result<(), ()> {
self.mark_dirty();
self.overlay.rollback_transaction().map_err(|_| ())
}
fn storage_commit_transaction(&mut self) -> Result<(), ()> {
self.overlay.commit_transaction().map_err(|_| ())
}
fn wipe(&mut self) {
for _ in 0..self.overlay.transaction_depth() {
self.overlay.rollback_transaction().expect(BENCHMARKING_FN);
}
self.overlay
.drain_storage_changes(
self.backend,
self.storage_transaction_cache,
Default::default(), // using any state
)
.expect(EXT_NOT_ALLOWED_TO_FAIL);
self.backend.wipe().expect(EXT_NOT_ALLOWED_TO_FAIL);
self.mark_dirty();
self.overlay
.enter_runtime()
.expect("We have reset the overlay above, so we can not be in the runtime; qed");
}
fn commit(&mut self) {
// Bench always use latest state.
let state_version = StateVersion::default();
for _ in 0..self.overlay.transaction_depth() {
self.overlay.commit_transaction().expect(BENCHMARKING_FN);
}
let changes = self
.overlay
.drain_storage_changes(self.backend, self.storage_transaction_cache, state_version)
.expect(EXT_NOT_ALLOWED_TO_FAIL);
self.backend
.commit(
changes.transaction_storage_root,
changes.transaction,
changes.main_storage_changes,
changes.child_storage_changes,
)
.expect(EXT_NOT_ALLOWED_TO_FAIL);
self.mark_dirty();
self.overlay
.enter_runtime()
.expect("We have reset the overlay above, so we can not be in the runtime; qed");
}
fn read_write_count(&self) -> (u32, u32, u32, u32) {
self.backend.read_write_count()
}
fn reset_read_write_count(&mut self) {
self.backend.reset_read_write_count()
}
fn get_whitelist(&self) -> Vec<TrackedStorageKey> {
self.backend.get_whitelist()
}
fn set_whitelist(&mut self, new: Vec<TrackedStorageKey>) {
self.backend.set_whitelist(new)
}
fn proof_size(&self) -> Option<u32> {
self.backend.proof_size()
}
fn get_read_and_written_keys(&self) -> Vec<(Vec<u8>, u32, u32, bool)> {
self.backend.get_read_and_written_keys()
}
}
impl<'a, H, B> Ext<'a, H, B>
where
H: Hasher,
H::Out: Ord + 'static + codec::Codec,
B: Backend<H>,
{
fn limit_remove_from_backend(
&mut self,
maybe_child: Option<&ChildInfo>,
maybe_prefix: Option<&[u8]>,
maybe_limit: Option<u32>,
maybe_cursor: Option<&[u8]>,
) -> (Option<Vec<u8>>, u32, u32) {
let mut delete_count: u32 = 0;
let mut loop_count: u32 = 0;
let mut maybe_next_key = None;
self.backend
.apply_to_keys_while(maybe_child, maybe_prefix, maybe_cursor, |key| {
if maybe_limit.map_or(false, |limit| loop_count == limit) {
maybe_next_key = Some(key.to_vec());
return false
}
let overlay = match maybe_child {
Some(child_info) => self.overlay.child_storage(child_info, key),
None => self.overlay.storage(key),
};
if !matches!(overlay, Some(None)) {
// not pending deletion from the backend - delete it.
if let Some(child_info) = maybe_child {
self.overlay.set_child_storage(child_info, key.to_vec(), None);
} else {
self.overlay.set_storage(key.to_vec(), None);
}
delete_count = delete_count.saturating_add(1);
}
loop_count = loop_count.saturating_add(1);
true
});
(maybe_next_key, delete_count, loop_count)
}
sourcepub fn transaction_depth(&self) -> usize
pub fn transaction_depth(&self) -> usize
Returns the current nesting depth of the transaction stack.
A value of zero means that no transaction is open and changes are committed on write.
Examples found in repository?
677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717
fn wipe(&mut self) {
for _ in 0..self.overlay.transaction_depth() {
self.overlay.rollback_transaction().expect(BENCHMARKING_FN);
}
self.overlay
.drain_storage_changes(
self.backend,
self.storage_transaction_cache,
Default::default(), // using any state
)
.expect(EXT_NOT_ALLOWED_TO_FAIL);
self.backend.wipe().expect(EXT_NOT_ALLOWED_TO_FAIL);
self.mark_dirty();
self.overlay
.enter_runtime()
.expect("We have reset the overlay above, so we can not be in the runtime; qed");
}
fn commit(&mut self) {
// Bench always use latest state.
let state_version = StateVersion::default();
for _ in 0..self.overlay.transaction_depth() {
self.overlay.commit_transaction().expect(BENCHMARKING_FN);
}
let changes = self
.overlay
.drain_storage_changes(self.backend, self.storage_transaction_cache, state_version)
.expect(EXT_NOT_ALLOWED_TO_FAIL);
self.backend
.commit(
changes.transaction_storage_root,
changes.transaction,
changes.main_storage_changes,
changes.child_storage_changes,
)
.expect(EXT_NOT_ALLOWED_TO_FAIL);
self.mark_dirty();
self.overlay
.enter_runtime()
.expect("We have reset the overlay above, so we can not be in the runtime; qed");
}
sourcepub fn start_transaction(&mut self)
pub fn start_transaction(&mut self)
Start a new nested transaction.
This allows to either commit or roll back all changes that where made while this
transaction was open. Any transaction must be closed by either rollback_transaction
or
commit_transaction
before this overlay can be converted into storage changes.
Changes made without any open transaction are committed immediately.
Examples found in repository?
More examples
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
fn execute_call_with_both_strategy<Handler>(
&mut self,
on_consensus_failure: Handler,
) -> CallResult<Exec::Error>
where
Handler:
FnOnce(CallResult<Exec::Error>, CallResult<Exec::Error>) -> CallResult<Exec::Error>,
{
self.overlay.start_transaction();
let (result, was_native) = self.execute_aux(true);
if was_native {
self.overlay.rollback_transaction().expect(PROOF_CLOSE_TRANSACTION);
let (wasm_result, _) = self.execute_aux(false);
if (result.is_ok() &&
wasm_result.is_ok() && result.as_ref().ok() == wasm_result.as_ref().ok()) ||
result.is_err() && wasm_result.is_err()
{
result
} else {
on_consensus_failure(wasm_result, result)
}
} else {
self.overlay.commit_transaction().expect(PROOF_CLOSE_TRANSACTION);
result
}
}
fn execute_call_with_native_else_wasm_strategy(&mut self) -> CallResult<Exec::Error> {
self.overlay.start_transaction();
let (result, was_native) = self.execute_aux(true);
if !was_native || result.is_ok() {
self.overlay.commit_transaction().expect(PROOF_CLOSE_TRANSACTION);
result
} else {
self.overlay.rollback_transaction().expect(PROOF_CLOSE_TRANSACTION);
self.execute_aux(false).0
}
}
sourcepub fn rollback_transaction(&mut self) -> Result<(), NoOpenTransaction>
pub fn rollback_transaction(&mut self) -> Result<(), NoOpenTransaction>
Rollback the last transaction started by start_transaction
.
Any changes made during that transaction are discarded. Returns an error if there is no open transaction that can be rolled back.
Examples found in repository?
More examples
668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693
fn storage_rollback_transaction(&mut self) -> Result<(), ()> {
self.mark_dirty();
self.overlay.rollback_transaction().map_err(|_| ())
}
fn storage_commit_transaction(&mut self) -> Result<(), ()> {
self.overlay.commit_transaction().map_err(|_| ())
}
fn wipe(&mut self) {
for _ in 0..self.overlay.transaction_depth() {
self.overlay.rollback_transaction().expect(BENCHMARKING_FN);
}
self.overlay
.drain_storage_changes(
self.backend,
self.storage_transaction_cache,
Default::default(), // using any state
)
.expect(EXT_NOT_ALLOWED_TO_FAIL);
self.backend.wipe().expect(EXT_NOT_ALLOWED_TO_FAIL);
self.mark_dirty();
self.overlay
.enter_runtime()
.expect("We have reset the overlay above, so we can not be in the runtime; qed");
}
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
fn execute_call_with_both_strategy<Handler>(
&mut self,
on_consensus_failure: Handler,
) -> CallResult<Exec::Error>
where
Handler:
FnOnce(CallResult<Exec::Error>, CallResult<Exec::Error>) -> CallResult<Exec::Error>,
{
self.overlay.start_transaction();
let (result, was_native) = self.execute_aux(true);
if was_native {
self.overlay.rollback_transaction().expect(PROOF_CLOSE_TRANSACTION);
let (wasm_result, _) = self.execute_aux(false);
if (result.is_ok() &&
wasm_result.is_ok() && result.as_ref().ok() == wasm_result.as_ref().ok()) ||
result.is_err() && wasm_result.is_err()
{
result
} else {
on_consensus_failure(wasm_result, result)
}
} else {
self.overlay.commit_transaction().expect(PROOF_CLOSE_TRANSACTION);
result
}
}
fn execute_call_with_native_else_wasm_strategy(&mut self) -> CallResult<Exec::Error> {
self.overlay.start_transaction();
let (result, was_native) = self.execute_aux(true);
if !was_native || result.is_ok() {
self.overlay.commit_transaction().expect(PROOF_CLOSE_TRANSACTION);
result
} else {
self.overlay.rollback_transaction().expect(PROOF_CLOSE_TRANSACTION);
self.execute_aux(false).0
}
}
sourcepub fn commit_transaction(&mut self) -> Result<(), NoOpenTransaction>
pub fn commit_transaction(&mut self) -> Result<(), NoOpenTransaction>
Commit the last transaction started by start_transaction
.
Any changes made during that transaction are committed. Returns an error if there is no open transaction that can be committed.
Examples found in repository?
More examples
673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717
fn storage_commit_transaction(&mut self) -> Result<(), ()> {
self.overlay.commit_transaction().map_err(|_| ())
}
fn wipe(&mut self) {
for _ in 0..self.overlay.transaction_depth() {
self.overlay.rollback_transaction().expect(BENCHMARKING_FN);
}
self.overlay
.drain_storage_changes(
self.backend,
self.storage_transaction_cache,
Default::default(), // using any state
)
.expect(EXT_NOT_ALLOWED_TO_FAIL);
self.backend.wipe().expect(EXT_NOT_ALLOWED_TO_FAIL);
self.mark_dirty();
self.overlay
.enter_runtime()
.expect("We have reset the overlay above, so we can not be in the runtime; qed");
}
fn commit(&mut self) {
// Bench always use latest state.
let state_version = StateVersion::default();
for _ in 0..self.overlay.transaction_depth() {
self.overlay.commit_transaction().expect(BENCHMARKING_FN);
}
let changes = self
.overlay
.drain_storage_changes(self.backend, self.storage_transaction_cache, state_version)
.expect(EXT_NOT_ALLOWED_TO_FAIL);
self.backend
.commit(
changes.transaction_storage_root,
changes.transaction,
changes.main_storage_changes,
changes.child_storage_changes,
)
.expect(EXT_NOT_ALLOWED_TO_FAIL);
self.mark_dirty();
self.overlay
.enter_runtime()
.expect("We have reset the overlay above, so we can not be in the runtime; qed");
}
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
fn execute_call_with_both_strategy<Handler>(
&mut self,
on_consensus_failure: Handler,
) -> CallResult<Exec::Error>
where
Handler:
FnOnce(CallResult<Exec::Error>, CallResult<Exec::Error>) -> CallResult<Exec::Error>,
{
self.overlay.start_transaction();
let (result, was_native) = self.execute_aux(true);
if was_native {
self.overlay.rollback_transaction().expect(PROOF_CLOSE_TRANSACTION);
let (wasm_result, _) = self.execute_aux(false);
if (result.is_ok() &&
wasm_result.is_ok() && result.as_ref().ok() == wasm_result.as_ref().ok()) ||
result.is_err() && wasm_result.is_err()
{
result
} else {
on_consensus_failure(wasm_result, result)
}
} else {
self.overlay.commit_transaction().expect(PROOF_CLOSE_TRANSACTION);
result
}
}
fn execute_call_with_native_else_wasm_strategy(&mut self) -> CallResult<Exec::Error> {
self.overlay.start_transaction();
let (result, was_native) = self.execute_aux(true);
if !was_native || result.is_ok() {
self.overlay.commit_transaction().expect(PROOF_CLOSE_TRANSACTION);
result
} else {
self.overlay.rollback_transaction().expect(PROOF_CLOSE_TRANSACTION);
self.execute_aux(false).0
}
}
sourcepub fn enter_runtime(&mut self) -> Result<(), AlreadyInRuntime>
pub fn enter_runtime(&mut self) -> Result<(), AlreadyInRuntime>
Call this before transfering control to the runtime.
This protects all existing transactions from being removed by the runtime. Calling this while already inside the runtime will return an error.
Examples found in repository?
677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717
fn wipe(&mut self) {
for _ in 0..self.overlay.transaction_depth() {
self.overlay.rollback_transaction().expect(BENCHMARKING_FN);
}
self.overlay
.drain_storage_changes(
self.backend,
self.storage_transaction_cache,
Default::default(), // using any state
)
.expect(EXT_NOT_ALLOWED_TO_FAIL);
self.backend.wipe().expect(EXT_NOT_ALLOWED_TO_FAIL);
self.mark_dirty();
self.overlay
.enter_runtime()
.expect("We have reset the overlay above, so we can not be in the runtime; qed");
}
fn commit(&mut self) {
// Bench always use latest state.
let state_version = StateVersion::default();
for _ in 0..self.overlay.transaction_depth() {
self.overlay.commit_transaction().expect(BENCHMARKING_FN);
}
let changes = self
.overlay
.drain_storage_changes(self.backend, self.storage_transaction_cache, state_version)
.expect(EXT_NOT_ALLOWED_TO_FAIL);
self.backend
.commit(
changes.transaction_storage_root,
changes.transaction,
changes.main_storage_changes,
changes.child_storage_changes,
)
.expect(EXT_NOT_ALLOWED_TO_FAIL);
self.mark_dirty();
self.overlay
.enter_runtime()
.expect("We have reset the overlay above, so we can not be in the runtime; qed");
}
More examples
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
fn execute_aux(&mut self, use_native: bool) -> (CallResult<Exec::Error>, bool) {
let mut cache = StorageTransactionCache::default();
let cache = match self.storage_transaction_cache.as_mut() {
Some(cache) => cache,
None => &mut cache,
};
self.overlay
.enter_runtime()
.expect("StateMachine is never called from the runtime; qed");
let mut ext = Ext::new(self.overlay, cache, self.backend, Some(&mut self.extensions));
let ext_id = ext.id;
trace!(
target: "state",
ext_id = %HexDisplay::from(&ext_id.to_le_bytes()),
method = %self.method,
parent_hash = %self.parent_hash.map(|h| format!("{:?}", h)).unwrap_or_else(|| String::from("None")),
input = ?HexDisplay::from(&self.call_data),
"Call",
);
let (result, was_native) = self.exec.call(
&mut ext,
self.runtime_code,
self.method,
self.call_data,
use_native,
);
self.overlay
.exit_runtime()
.expect("Runtime is not able to call this function in the overlay; qed");
trace!(
target: "state",
ext_id = %HexDisplay::from(&ext_id.to_le_bytes()),
?was_native,
?result,
"Return",
);
(result, was_native)
}
sourcepub fn exit_runtime(&mut self) -> Result<(), NotInRuntime>
pub fn exit_runtime(&mut self) -> Result<(), NotInRuntime>
Call this when control returns from the runtime.
This commits all dangling transaction left open by the runtime. Calling this while outside the runtime will return an error.
Examples found in repository?
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
fn execute_aux(&mut self, use_native: bool) -> (CallResult<Exec::Error>, bool) {
let mut cache = StorageTransactionCache::default();
let cache = match self.storage_transaction_cache.as_mut() {
Some(cache) => cache,
None => &mut cache,
};
self.overlay
.enter_runtime()
.expect("StateMachine is never called from the runtime; qed");
let mut ext = Ext::new(self.overlay, cache, self.backend, Some(&mut self.extensions));
let ext_id = ext.id;
trace!(
target: "state",
ext_id = %HexDisplay::from(&ext_id.to_le_bytes()),
method = %self.method,
parent_hash = %self.parent_hash.map(|h| format!("{:?}", h)).unwrap_or_else(|| String::from("None")),
input = ?HexDisplay::from(&self.call_data),
"Call",
);
let (result, was_native) = self.exec.call(
&mut ext,
self.runtime_code,
self.method,
self.call_data,
use_native,
);
self.overlay
.exit_runtime()
.expect("Runtime is not able to call this function in the overlay; qed");
trace!(
target: "state",
ext_id = %HexDisplay::from(&ext_id.to_le_bytes()),
?was_native,
?result,
"Return",
);
(result, was_native)
}
sourcepub fn offchain_drain_committed(
&mut self
) -> impl Iterator<Item = ((StorageKey, StorageKey), OffchainOverlayedChange)>
pub fn offchain_drain_committed(
&mut self
) -> impl Iterator<Item = ((StorageKey, StorageKey), OffchainOverlayedChange)>
Consume all changes (top + children) and return them.
After calling this function no more changes are contained in this changeset.
Panics:
Panics if transaction_depth() > 0
Examples found in repository?
More examples
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
pub fn drain_storage_changes<B: Backend<H>, H: Hasher>(
&mut self,
backend: &B,
cache: &mut StorageTransactionCache<B::Transaction, H>,
state_version: StateVersion,
) -> Result<StorageChanges<B::Transaction, H>, DefaultError>
where
H::Out: Ord + Encode + 'static,
{
// If the transaction does not exist, we generate it.
if cache.transaction.is_none() {
self.storage_root(backend, cache, state_version);
}
let (transaction, transaction_storage_root) = cache
.transaction
.take()
.and_then(|t| cache.transaction_storage_root.take().map(|tr| (t, tr)))
.expect("Transaction was be generated as part of `storage_root`; qed");
let (main_storage_changes, child_storage_changes) = self.drain_committed();
let offchain_storage_changes = self.offchain_drain_committed().collect();
#[cfg(feature = "std")]
let transaction_index_changes = std::mem::take(&mut self.transaction_index_ops);
Ok(StorageChanges {
main_storage_changes: main_storage_changes.collect(),
child_storage_changes: child_storage_changes
.map(|(sk, it)| (sk, it.0.collect()))
.collect(),
offchain_storage_changes,
transaction,
transaction_storage_root,
#[cfg(feature = "std")]
transaction_index_changes,
})
}
sourcepub fn children(
&self
) -> impl Iterator<Item = (impl Iterator<Item = (&StorageKey, &OverlayedEntry<Option<StorageValue>>)>, &ChildInfo)>
pub fn children(
&self
) -> impl Iterator<Item = (impl Iterator<Item = (&StorageKey, &OverlayedEntry<Option<StorageValue>>)>, &ChildInfo)>
Get an iterator over all child changes as seen by the current transaction.
Examples found in repository?
161 162 163 164 165 166 167 168 169 170 171 172 173 174
pub fn as_backend(&self) -> InMemoryBackend<H> {
let top: Vec<_> =
self.overlay.changes().map(|(k, v)| (k.clone(), v.value().cloned())).collect();
let mut transaction = vec![(None, top)];
for (child_changes, child_info) in self.overlay.children() {
transaction.push((
Some(child_info.clone()),
child_changes.map(|(k, v)| (k.clone(), v.value().cloned())).collect(),
))
}
self.backend.update(transaction, self.state_version)
}
More examples
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
pub fn into_storages(self) -> Storage {
Storage {
top: self
.overlay
.changes()
.filter_map(|(k, v)| v.value().map(|v| (k.to_vec(), v.to_vec())))
.collect(),
children_default: self
.overlay
.children()
.map(|(iter, i)| {
(
i.storage_key().to_vec(),
sp_core::storage::StorageChild {
data: iter
.filter_map(|(k, v)| v.value().map(|v| (k.to_vec(), v.to_vec())))
.collect(),
child_info: i.clone(),
},
)
})
.collect(),
}
}
/// Execute the given closure `f` with the externalities set and initialized with `storage`.
///
/// Returns the result of the closure and updates `storage` with all changes.
pub fn execute_with_storage<R>(
storage: &mut sp_core::storage::Storage,
f: impl FnOnce() -> R,
) -> R {
let mut ext = Self::new(std::mem::take(storage));
let r = ext.execute_with(f);
*storage = ext.into_storages();
r
}
/// Execute the given closure while `self` is set as externalities.
///
/// Returns the result of the given closure.
pub fn execute_with<R>(&mut self, f: impl FnOnce() -> R) -> R {
sp_externalities::set_and_run_with_externalities(self, f)
}
/// List of active extensions.
pub fn extensions(&mut self) -> &mut Extensions {
&mut self.extensions
}
/// Register an extension.
pub fn register_extension(&mut self, ext: impl Extension) {
self.extensions.register(ext);
}
}
impl PartialEq for BasicExternalities {
fn eq(&self, other: &BasicExternalities) -> bool {
self.overlay.changes().map(|(k, v)| (k, v.value())).collect::<BTreeMap<_, _>>() ==
other.overlay.changes().map(|(k, v)| (k, v.value())).collect::<BTreeMap<_, _>>() &&
self.overlay
.children()
.map(|(iter, i)| (i, iter.map(|(k, v)| (k, v.value())).collect::<BTreeMap<_, _>>()))
.collect::<BTreeMap<_, _>>() ==
other
.overlay
.children()
.map(|(iter, i)| {
(i, iter.map(|(k, v)| (k, v.value())).collect::<BTreeMap<_, _>>())
})
.collect::<BTreeMap<_, _>>()
}
}
impl FromIterator<(StorageKey, StorageValue)> for BasicExternalities {
fn from_iter<I: IntoIterator<Item = (StorageKey, StorageValue)>>(iter: I) -> Self {
let mut t = Self::default();
iter.into_iter().for_each(|(k, v)| t.insert(k, v));
t
}
}
impl Default for BasicExternalities {
fn default() -> Self {
Self::new(Default::default())
}
}
impl From<BTreeMap<StorageKey, StorageValue>> for BasicExternalities {
fn from(map: BTreeMap<StorageKey, StorageValue>) -> Self {
Self::from_iter(map.into_iter())
}
}
impl Externalities for BasicExternalities {
fn set_offchain_storage(&mut self, _key: &[u8], _value: Option<&[u8]>) {}
fn storage(&self, key: &[u8]) -> Option<StorageValue> {
self.overlay.storage(key).and_then(|v| v.map(|v| v.to_vec()))
}
fn storage_hash(&self, key: &[u8]) -> Option<Vec<u8>> {
self.storage(key).map(|v| Blake2Hasher::hash(&v).encode())
}
fn child_storage(&self, child_info: &ChildInfo, key: &[u8]) -> Option<StorageValue> {
self.overlay.child_storage(child_info, key).and_then(|v| v.map(|v| v.to_vec()))
}
fn child_storage_hash(&self, child_info: &ChildInfo, key: &[u8]) -> Option<Vec<u8>> {
self.child_storage(child_info, key).map(|v| Blake2Hasher::hash(&v).encode())
}
fn next_storage_key(&self, key: &[u8]) -> Option<StorageKey> {
self.overlay.iter_after(key).find_map(|(k, v)| v.value().map(|_| k.to_vec()))
}
fn next_child_storage_key(&self, child_info: &ChildInfo, key: &[u8]) -> Option<StorageKey> {
self.overlay
.child_iter_after(child_info.storage_key(), key)
.find_map(|(k, v)| v.value().map(|_| k.to_vec()))
}
fn place_storage(&mut self, key: StorageKey, maybe_value: Option<StorageValue>) {
if is_child_storage_key(&key) {
warn!(target: "trie", "Refuse to set child storage key via main storage");
return
}
self.overlay.set_storage(key, maybe_value)
}
fn place_child_storage(
&mut self,
child_info: &ChildInfo,
key: StorageKey,
value: Option<StorageValue>,
) {
self.overlay.set_child_storage(child_info, key, value);
}
fn kill_child_storage(
&mut self,
child_info: &ChildInfo,
_maybe_limit: Option<u32>,
_maybe_cursor: Option<&[u8]>,
) -> MultiRemovalResults {
let count = self.overlay.clear_child_storage(child_info);
MultiRemovalResults { maybe_cursor: None, backend: count, unique: count, loops: count }
}
fn clear_prefix(
&mut self,
prefix: &[u8],
_maybe_limit: Option<u32>,
_maybe_cursor: Option<&[u8]>,
) -> MultiRemovalResults {
if is_child_storage_key(prefix) {
warn!(
target: "trie",
"Refuse to clear prefix that is part of child storage key via main storage"
);
let maybe_cursor = Some(prefix.to_vec());
return MultiRemovalResults { maybe_cursor, backend: 0, unique: 0, loops: 0 }
}
let count = self.overlay.clear_prefix(prefix);
MultiRemovalResults { maybe_cursor: None, backend: count, unique: count, loops: count }
}
fn clear_child_prefix(
&mut self,
child_info: &ChildInfo,
prefix: &[u8],
_maybe_limit: Option<u32>,
_maybe_cursor: Option<&[u8]>,
) -> MultiRemovalResults {
let count = self.overlay.clear_child_prefix(child_info, prefix);
MultiRemovalResults { maybe_cursor: None, backend: count, unique: count, loops: count }
}
fn storage_append(&mut self, key: Vec<u8>, value: Vec<u8>) {
let current_value = self.overlay.value_mut_or_insert_with(&key, || Default::default());
crate::ext::StorageAppend::new(current_value).append(value);
}
fn storage_root(&mut self, state_version: StateVersion) -> Vec<u8> {
let mut top = self
.overlay
.changes()
.filter_map(|(k, v)| v.value().map(|v| (k.clone(), v.clone())))
.collect::<BTreeMap<_, _>>();
// Single child trie implementation currently allows using the same child
// empty root for all child trie. Using null storage key until multiple
// type of child trie support.
let empty_hash = empty_child_trie_root::<LayoutV1<Blake2Hasher>>();
for child_info in self.overlay.children().map(|d| d.1.clone()).collect::<Vec<_>>() {
let child_root = self.child_storage_root(&child_info, state_version);
if empty_hash[..] == child_root[..] {
top.remove(child_info.prefixed_storage_key().as_slice());
} else {
top.insert(child_info.prefixed_storage_key().into_inner(), child_root);
}
}
match state_version {
StateVersion::V0 => LayoutV0::<Blake2Hasher>::trie_root(top).as_ref().into(),
StateVersion::V1 => LayoutV1::<Blake2Hasher>::trie_root(top).as_ref().into(),
}
}
579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599
pub fn storage_root<H: Hasher, B: Backend<H>>(
&self,
backend: &B,
cache: &mut StorageTransactionCache<B::Transaction, H>,
state_version: StateVersion,
) -> H::Out
where
H::Out: Ord + Encode,
{
let delta = self.changes().map(|(k, v)| (&k[..], v.value().map(|v| &v[..])));
let child_delta = self.children().map(|(changes, info)| {
(info, changes.map(|(k, v)| (&k[..], v.value().map(|v| &v[..]))))
});
let (root, transaction) = backend.full_storage_root(delta, child_delta, state_version);
cache.transaction = Some(transaction);
cache.transaction_storage_root = Some(root);
root
}
sourcepub fn changes(
&self
) -> impl Iterator<Item = (&StorageKey, &OverlayedEntry<Option<StorageValue>>)>
pub fn changes(
&self
) -> impl Iterator<Item = (&StorageKey, &OverlayedEntry<Option<StorageValue>>)>
Get an iterator over all top changes as been by the current transaction.
Examples found in repository?
161 162 163 164 165 166 167 168 169 170 171 172 173 174
pub fn as_backend(&self) -> InMemoryBackend<H> {
let top: Vec<_> =
self.overlay.changes().map(|(k, v)| (k.clone(), v.value().cloned())).collect();
let mut transaction = vec![(None, top)];
for (child_changes, child_info) in self.overlay.children() {
transaction.push((
Some(child_info.clone()),
child_changes.map(|(k, v)| (k.clone(), v.value().cloned())).collect(),
))
}
self.backend.update(transaction, self.state_version)
}
More examples
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
pub fn into_storages(self) -> Storage {
Storage {
top: self
.overlay
.changes()
.filter_map(|(k, v)| v.value().map(|v| (k.to_vec(), v.to_vec())))
.collect(),
children_default: self
.overlay
.children()
.map(|(iter, i)| {
(
i.storage_key().to_vec(),
sp_core::storage::StorageChild {
data: iter
.filter_map(|(k, v)| v.value().map(|v| (k.to_vec(), v.to_vec())))
.collect(),
child_info: i.clone(),
},
)
})
.collect(),
}
}
/// Execute the given closure `f` with the externalities set and initialized with `storage`.
///
/// Returns the result of the closure and updates `storage` with all changes.
pub fn execute_with_storage<R>(
storage: &mut sp_core::storage::Storage,
f: impl FnOnce() -> R,
) -> R {
let mut ext = Self::new(std::mem::take(storage));
let r = ext.execute_with(f);
*storage = ext.into_storages();
r
}
/// Execute the given closure while `self` is set as externalities.
///
/// Returns the result of the given closure.
pub fn execute_with<R>(&mut self, f: impl FnOnce() -> R) -> R {
sp_externalities::set_and_run_with_externalities(self, f)
}
/// List of active extensions.
pub fn extensions(&mut self) -> &mut Extensions {
&mut self.extensions
}
/// Register an extension.
pub fn register_extension(&mut self, ext: impl Extension) {
self.extensions.register(ext);
}
}
impl PartialEq for BasicExternalities {
fn eq(&self, other: &BasicExternalities) -> bool {
self.overlay.changes().map(|(k, v)| (k, v.value())).collect::<BTreeMap<_, _>>() ==
other.overlay.changes().map(|(k, v)| (k, v.value())).collect::<BTreeMap<_, _>>() &&
self.overlay
.children()
.map(|(iter, i)| (i, iter.map(|(k, v)| (k, v.value())).collect::<BTreeMap<_, _>>()))
.collect::<BTreeMap<_, _>>() ==
other
.overlay
.children()
.map(|(iter, i)| {
(i, iter.map(|(k, v)| (k, v.value())).collect::<BTreeMap<_, _>>())
})
.collect::<BTreeMap<_, _>>()
}
}
impl FromIterator<(StorageKey, StorageValue)> for BasicExternalities {
fn from_iter<I: IntoIterator<Item = (StorageKey, StorageValue)>>(iter: I) -> Self {
let mut t = Self::default();
iter.into_iter().for_each(|(k, v)| t.insert(k, v));
t
}
}
impl Default for BasicExternalities {
fn default() -> Self {
Self::new(Default::default())
}
}
impl From<BTreeMap<StorageKey, StorageValue>> for BasicExternalities {
fn from(map: BTreeMap<StorageKey, StorageValue>) -> Self {
Self::from_iter(map.into_iter())
}
}
impl Externalities for BasicExternalities {
fn set_offchain_storage(&mut self, _key: &[u8], _value: Option<&[u8]>) {}
fn storage(&self, key: &[u8]) -> Option<StorageValue> {
self.overlay.storage(key).and_then(|v| v.map(|v| v.to_vec()))
}
fn storage_hash(&self, key: &[u8]) -> Option<Vec<u8>> {
self.storage(key).map(|v| Blake2Hasher::hash(&v).encode())
}
fn child_storage(&self, child_info: &ChildInfo, key: &[u8]) -> Option<StorageValue> {
self.overlay.child_storage(child_info, key).and_then(|v| v.map(|v| v.to_vec()))
}
fn child_storage_hash(&self, child_info: &ChildInfo, key: &[u8]) -> Option<Vec<u8>> {
self.child_storage(child_info, key).map(|v| Blake2Hasher::hash(&v).encode())
}
fn next_storage_key(&self, key: &[u8]) -> Option<StorageKey> {
self.overlay.iter_after(key).find_map(|(k, v)| v.value().map(|_| k.to_vec()))
}
fn next_child_storage_key(&self, child_info: &ChildInfo, key: &[u8]) -> Option<StorageKey> {
self.overlay
.child_iter_after(child_info.storage_key(), key)
.find_map(|(k, v)| v.value().map(|_| k.to_vec()))
}
fn place_storage(&mut self, key: StorageKey, maybe_value: Option<StorageValue>) {
if is_child_storage_key(&key) {
warn!(target: "trie", "Refuse to set child storage key via main storage");
return
}
self.overlay.set_storage(key, maybe_value)
}
fn place_child_storage(
&mut self,
child_info: &ChildInfo,
key: StorageKey,
value: Option<StorageValue>,
) {
self.overlay.set_child_storage(child_info, key, value);
}
fn kill_child_storage(
&mut self,
child_info: &ChildInfo,
_maybe_limit: Option<u32>,
_maybe_cursor: Option<&[u8]>,
) -> MultiRemovalResults {
let count = self.overlay.clear_child_storage(child_info);
MultiRemovalResults { maybe_cursor: None, backend: count, unique: count, loops: count }
}
fn clear_prefix(
&mut self,
prefix: &[u8],
_maybe_limit: Option<u32>,
_maybe_cursor: Option<&[u8]>,
) -> MultiRemovalResults {
if is_child_storage_key(prefix) {
warn!(
target: "trie",
"Refuse to clear prefix that is part of child storage key via main storage"
);
let maybe_cursor = Some(prefix.to_vec());
return MultiRemovalResults { maybe_cursor, backend: 0, unique: 0, loops: 0 }
}
let count = self.overlay.clear_prefix(prefix);
MultiRemovalResults { maybe_cursor: None, backend: count, unique: count, loops: count }
}
fn clear_child_prefix(
&mut self,
child_info: &ChildInfo,
prefix: &[u8],
_maybe_limit: Option<u32>,
_maybe_cursor: Option<&[u8]>,
) -> MultiRemovalResults {
let count = self.overlay.clear_child_prefix(child_info, prefix);
MultiRemovalResults { maybe_cursor: None, backend: count, unique: count, loops: count }
}
fn storage_append(&mut self, key: Vec<u8>, value: Vec<u8>) {
let current_value = self.overlay.value_mut_or_insert_with(&key, || Default::default());
crate::ext::StorageAppend::new(current_value).append(value);
}
fn storage_root(&mut self, state_version: StateVersion) -> Vec<u8> {
let mut top = self
.overlay
.changes()
.filter_map(|(k, v)| v.value().map(|v| (k.clone(), v.clone())))
.collect::<BTreeMap<_, _>>();
// Single child trie implementation currently allows using the same child
// empty root for all child trie. Using null storage key until multiple
// type of child trie support.
let empty_hash = empty_child_trie_root::<LayoutV1<Blake2Hasher>>();
for child_info in self.overlay.children().map(|d| d.1.clone()).collect::<Vec<_>>() {
let child_root = self.child_storage_root(&child_info, state_version);
if empty_hash[..] == child_root[..] {
top.remove(child_info.prefixed_storage_key().as_slice());
} else {
top.insert(child_info.prefixed_storage_key().into_inner(), child_root);
}
}
match state_version {
StateVersion::V0 => LayoutV0::<Blake2Hasher>::trie_root(top).as_ref().into(),
StateVersion::V1 => LayoutV1::<Blake2Hasher>::trie_root(top).as_ref().into(),
}
}
579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599
pub fn storage_root<H: Hasher, B: Backend<H>>(
&self,
backend: &B,
cache: &mut StorageTransactionCache<B::Transaction, H>,
state_version: StateVersion,
) -> H::Out
where
H::Out: Ord + Encode,
{
let delta = self.changes().map(|(k, v)| (&k[..], v.value().map(|v| &v[..])));
let child_delta = self.children().map(|(changes, info)| {
(info, changes.map(|(k, v)| (&k[..], v.value().map(|v| &v[..]))))
});
let (root, transaction) = backend.full_storage_root(delta, child_delta, state_version);
cache.transaction = Some(transaction);
cache.transaction_storage_root = Some(root);
root
}
sourcepub fn child_changes(
&self,
key: &[u8]
) -> Option<(impl Iterator<Item = (&StorageKey, &OverlayedEntry<Option<StorageValue>>)>, &ChildInfo)>
pub fn child_changes(
&self,
key: &[u8]
) -> Option<(impl Iterator<Item = (&StorageKey, &OverlayedEntry<Option<StorageValue>>)>, &ChildInfo)>
Get an optional iterator over all child changes stored under the supplied key.
Examples found in repository?
277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292
fn child_storage_root(
&mut self,
child_info: &ChildInfo,
state_version: StateVersion,
) -> Vec<u8> {
if let Some((data, child_info)) = self.overlay.child_changes(child_info.storage_key()) {
let delta =
data.into_iter().map(|(k, v)| (k.as_ref(), v.value().map(|v| v.as_slice())));
crate::in_memory_backend::new_in_mem::<Blake2Hasher, HashKey<_>>()
.child_storage_root(&child_info, delta, state_version)
.0
} else {
empty_child_trie_root::<LayoutV1<Blake2Hasher>>()
}
.encode()
}
More examples
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
fn child_storage_root(
&mut self,
child_info: &ChildInfo,
state_version: StateVersion,
) -> Vec<u8> {
let _guard = guard();
let storage_key = child_info.storage_key();
let prefixed_storage_key = child_info.prefixed_storage_key();
if self.storage_transaction_cache.transaction_storage_root.is_some() {
let root = self
.storage(prefixed_storage_key.as_slice())
.and_then(|k| Decode::decode(&mut &k[..]).ok())
// V1 is equivalent to V0 on empty root.
.unwrap_or_else(empty_child_trie_root::<LayoutV1<H>>);
trace!(
target: "state",
method = "ChildStorageRoot",
ext_id = %HexDisplay::from(&self.id.to_le_bytes()),
child_info = %HexDisplay::from(&storage_key),
storage_root = %HexDisplay::from(&root.as_ref()),
cached = true,
);
root.encode()
} else {
let root = if let Some((changes, info)) = self.overlay.child_changes(storage_key) {
let delta = changes.map(|(k, v)| (k.as_ref(), v.value().map(AsRef::as_ref)));
Some(self.backend.child_storage_root(info, delta, state_version))
} else {
None
};
if let Some((root, is_empty, _)) = root {
let root = root.encode();
// We store update in the overlay in order to be able to use
// 'self.storage_transaction' cache. This is brittle as it rely on Ext only querying
// the trie backend for storage root.
// A better design would be to manage 'child_storage_transaction' in a
// similar way as 'storage_transaction' but for each child trie.
if is_empty {
self.overlay.set_storage(prefixed_storage_key.into_inner(), None);
} else {
self.overlay.set_storage(prefixed_storage_key.into_inner(), Some(root.clone()));
}
trace!(
target: "state",
method = "ChildStorageRoot",
ext_id = %HexDisplay::from(&self.id.to_le_bytes()),
child_info = %HexDisplay::from(&storage_key),
storage_root = %HexDisplay::from(&root.as_ref()),
cached = false,
);
root
} else {
// empty overlay
let root = self
.storage(prefixed_storage_key.as_slice())
.and_then(|k| Decode::decode(&mut &k[..]).ok())
// V1 is equivalent to V0 on empty root.
.unwrap_or_else(empty_child_trie_root::<LayoutV1<H>>);
trace!(
target: "state",
method = "ChildStorageRoot",
ext_id = %HexDisplay::from(&self.id.to_le_bytes()),
child_info = %HexDisplay::from(&storage_key),
storage_root = %HexDisplay::from(&root.as_ref()),
cached = false,
);
root.encode()
}
}
}
sourcepub fn transaction_index_ops(&self) -> &[IndexOperation]
pub fn transaction_index_ops(&self) -> &[IndexOperation]
Get an list of all index operations.
sourcepub fn into_storage_changes<B: Backend<H>, H: Hasher>(
self,
backend: &B,
cache: StorageTransactionCache<B::Transaction, H>,
state_version: StateVersion
) -> Result<StorageChanges<B::Transaction, H>, DefaultError>where
H::Out: Ord + Encode + 'static,
pub fn into_storage_changes<B: Backend<H>, H: Hasher>(
self,
backend: &B,
cache: StorageTransactionCache<B::Transaction, H>,
state_version: StateVersion
) -> Result<StorageChanges<B::Transaction, H>, DefaultError>where
H::Out: Ord + Encode + 'static,
Convert this instance with all changes into a StorageChanges
instance.
sourcepub fn drain_storage_changes<B: Backend<H>, H: Hasher>(
&mut self,
backend: &B,
cache: &mut StorageTransactionCache<B::Transaction, H>,
state_version: StateVersion
) -> Result<StorageChanges<B::Transaction, H>, DefaultError>where
H::Out: Ord + Encode + 'static,
pub fn drain_storage_changes<B: Backend<H>, H: Hasher>(
&mut self,
backend: &B,
cache: &mut StorageTransactionCache<B::Transaction, H>,
state_version: StateVersion
) -> Result<StorageChanges<B::Transaction, H>, DefaultError>where
H::Out: Ord + Encode + 'static,
Drain all changes into a StorageChanges
instance. Leave empty overlay in place.
Examples found in repository?
More examples
677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717
fn wipe(&mut self) {
for _ in 0..self.overlay.transaction_depth() {
self.overlay.rollback_transaction().expect(BENCHMARKING_FN);
}
self.overlay
.drain_storage_changes(
self.backend,
self.storage_transaction_cache,
Default::default(), // using any state
)
.expect(EXT_NOT_ALLOWED_TO_FAIL);
self.backend.wipe().expect(EXT_NOT_ALLOWED_TO_FAIL);
self.mark_dirty();
self.overlay
.enter_runtime()
.expect("We have reset the overlay above, so we can not be in the runtime; qed");
}
fn commit(&mut self) {
// Bench always use latest state.
let state_version = StateVersion::default();
for _ in 0..self.overlay.transaction_depth() {
self.overlay.commit_transaction().expect(BENCHMARKING_FN);
}
let changes = self
.overlay
.drain_storage_changes(self.backend, self.storage_transaction_cache, state_version)
.expect(EXT_NOT_ALLOWED_TO_FAIL);
self.backend
.commit(
changes.transaction_storage_root,
changes.transaction,
changes.main_storage_changes,
changes.child_storage_changes,
)
.expect(EXT_NOT_ALLOWED_TO_FAIL);
self.mark_dirty();
self.overlay
.enter_runtime()
.expect("We have reset the overlay above, so we can not be in the runtime; qed");
}
sourcepub fn storage_root<H: Hasher, B: Backend<H>>(
&self,
backend: &B,
cache: &mut StorageTransactionCache<B::Transaction, H>,
state_version: StateVersion
) -> H::Outwhere
H::Out: Ord + Encode,
pub fn storage_root<H: Hasher, B: Backend<H>>(
&self,
backend: &B,
cache: &mut StorageTransactionCache<B::Transaction, H>,
state_version: StateVersion
) -> H::Outwhere
H::Out: Ord + Encode,
Generate the storage root using backend
and all changes
as seen by the current transaction.
Returns the storage root and caches storage transaction in the given cache
.
Examples found in repository?
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
fn storage_root(&mut self, state_version: StateVersion) -> Vec<u8> {
let _guard = guard();
if let Some(ref root) = self.storage_transaction_cache.transaction_storage_root {
trace!(
target: "state",
method = "StorageRoot",
ext_id = %HexDisplay::from(&self.id.to_le_bytes()),
storage_root = %HexDisplay::from(&root.as_ref()),
cached = true,
);
return root.encode()
}
let root =
self.overlay
.storage_root(self.backend, self.storage_transaction_cache, state_version);
trace!(
target: "state",
method = "StorageRoot",
ext_id = %HexDisplay::from(&self.id.to_le_bytes()),
storage_root = %HexDisplay::from(&root.as_ref()),
cached = false,
);
root.encode()
}
More examples
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
pub fn drain_storage_changes<B: Backend<H>, H: Hasher>(
&mut self,
backend: &B,
cache: &mut StorageTransactionCache<B::Transaction, H>,
state_version: StateVersion,
) -> Result<StorageChanges<B::Transaction, H>, DefaultError>
where
H::Out: Ord + Encode + 'static,
{
// If the transaction does not exist, we generate it.
if cache.transaction.is_none() {
self.storage_root(backend, cache, state_version);
}
let (transaction, transaction_storage_root) = cache
.transaction
.take()
.and_then(|t| cache.transaction_storage_root.take().map(|tr| (t, tr)))
.expect("Transaction was be generated as part of `storage_root`; qed");
let (main_storage_changes, child_storage_changes) = self.drain_committed();
let offchain_storage_changes = self.offchain_drain_committed().collect();
#[cfg(feature = "std")]
let transaction_index_changes = std::mem::take(&mut self.transaction_index_ops);
Ok(StorageChanges {
main_storage_changes: main_storage_changes.collect(),
child_storage_changes: child_storage_changes
.map(|(sk, it)| (sk, it.0.collect()))
.collect(),
offchain_storage_changes,
transaction,
transaction_storage_root,
#[cfg(feature = "std")]
transaction_index_changes,
})
}
sourcepub fn iter_after(
&self,
key: &[u8]
) -> impl Iterator<Item = (&[u8], &OverlayedEntry<Option<StorageValue>>)>
pub fn iter_after(
&self,
key: &[u8]
) -> impl Iterator<Item = (&[u8], &OverlayedEntry<Option<StorageValue>>)>
Returns an iterator over the keys (in lexicographic order) following key
(excluding key
)
alongside its value.
Examples found in repository?
More examples
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
fn next_storage_key(&self, key: &[u8]) -> Option<StorageKey> {
let mut next_backend_key =
self.backend.next_storage_key(key).expect(EXT_NOT_ALLOWED_TO_FAIL);
let mut overlay_changes = self.overlay.iter_after(key).peekable();
match (&next_backend_key, overlay_changes.peek()) {
(_, None) => next_backend_key,
(Some(_), Some(_)) => {
for overlay_key in overlay_changes {
let cmp = next_backend_key.as_deref().map(|v| v.cmp(overlay_key.0));
// If `backend_key` is less than the `overlay_key`, we found out next key.
if cmp == Some(Ordering::Less) {
return next_backend_key
} else if overlay_key.1.value().is_some() {
// If there exists a value for the `overlay_key` in the overlay
// (aka the key is still valid), it means we have found our next key.
return Some(overlay_key.0.to_vec())
} else if cmp == Some(Ordering::Equal) {
// If the `backend_key` and `overlay_key` are equal, it means that we need
// to search for the next backend key, because the overlay has overwritten
// this key.
next_backend_key = self
.backend
.next_storage_key(overlay_key.0)
.expect(EXT_NOT_ALLOWED_TO_FAIL);
}
}
next_backend_key
},
(None, Some(_)) => {
// Find the next overlay key that has a value attached.
overlay_changes.find_map(|k| k.1.value().as_ref().map(|_| k.0.to_vec()))
},
}
}
sourcepub fn child_iter_after(
&self,
storage_key: &[u8],
key: &[u8]
) -> impl Iterator<Item = (&[u8], &OverlayedEntry<Option<StorageValue>>)>
pub fn child_iter_after(
&self,
storage_key: &[u8],
key: &[u8]
) -> impl Iterator<Item = (&[u8], &OverlayedEntry<Option<StorageValue>>)>
Returns an iterator over the keys (in lexicographic order) following key
(excluding key
)
alongside its value for the given storage_key
child.
Examples found in repository?
More examples
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
fn next_child_storage_key(&self, child_info: &ChildInfo, key: &[u8]) -> Option<StorageKey> {
let mut next_backend_key = self
.backend
.next_child_storage_key(child_info, key)
.expect(EXT_NOT_ALLOWED_TO_FAIL);
let mut overlay_changes =
self.overlay.child_iter_after(child_info.storage_key(), key).peekable();
match (&next_backend_key, overlay_changes.peek()) {
(_, None) => next_backend_key,
(Some(_), Some(_)) => {
for overlay_key in overlay_changes {
let cmp = next_backend_key.as_deref().map(|v| v.cmp(overlay_key.0));
// If `backend_key` is less than the `overlay_key`, we found out next key.
if cmp == Some(Ordering::Less) {
return next_backend_key
} else if overlay_key.1.value().is_some() {
// If there exists a value for the `overlay_key` in the overlay
// (aka the key is still valid), it means we have found our next key.
return Some(overlay_key.0.to_vec())
} else if cmp == Some(Ordering::Equal) {
// If the `backend_key` and `overlay_key` are equal, it means that we need
// to search for the next backend key, because the overlay has overwritten
// this key.
next_backend_key = self
.backend
.next_child_storage_key(child_info, overlay_key.0)
.expect(EXT_NOT_ALLOWED_TO_FAIL);
}
}
next_backend_key
},
(None, Some(_)) => {
// Find the next overlay key that has a value attached.
overlay_changes.find_map(|k| k.1.value().as_ref().map(|_| k.0.to_vec()))
},
}
}
sourcepub fn offchain(&self) -> &OffchainOverlayedChanges
pub fn offchain(&self) -> &OffchainOverlayedChanges
Read only access ot offchain overlay.
sourcepub fn set_offchain_storage(&mut self, key: &[u8], value: Option<&[u8]>)
pub fn set_offchain_storage(&mut self, key: &[u8], value: Option<&[u8]>)
Write a key value pair to the offchain storage overlay.
sourcepub fn add_transaction_index(&mut self, op: IndexOperation)
pub fn add_transaction_index(&mut self, op: IndexOperation)
Add transaction index operation.
Examples found in repository?
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
fn storage_index_transaction(&mut self, index: u32, hash: &[u8], size: u32) {
trace!(
target: "state",
method = "IndexTransaction",
ext_id = %HexDisplay::from(&self.id.to_le_bytes()),
%index,
tx_hash = %HexDisplay::from(&hash),
%size,
);
self.overlay.add_transaction_index(IndexOperation::Insert {
extrinsic: index,
hash: hash.to_vec(),
size,
});
}
/// Renew existing piece of data storage.
fn storage_renew_transaction_index(&mut self, index: u32, hash: &[u8]) {
trace!(
target: "state",
method = "RenewTransactionIndex",
ext_id = %HexDisplay::from(&self.id.to_le_bytes()),
%index,
tx_hash = %HexDisplay::from(&hash),
);
self.overlay
.add_transaction_index(IndexOperation::Renew { extrinsic: index, hash: hash.to_vec() });
}
Trait Implementations§
source§impl Clone for OverlayedChanges
impl Clone for OverlayedChanges
source§fn clone(&self) -> OverlayedChanges
fn clone(&self) -> OverlayedChanges
1.0.0 · source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read moresource§impl Debug for OverlayedChanges
impl Debug for OverlayedChanges
source§impl Default for OverlayedChanges
impl Default for OverlayedChanges
source§fn default() -> OverlayedChanges
fn default() -> OverlayedChanges
Auto Trait Implementations§
impl !RefUnwindSafe for OverlayedChanges
impl Send for OverlayedChanges
impl !Sync for OverlayedChanges
impl Unpin for OverlayedChanges
impl UnwindSafe for OverlayedChanges
Blanket Implementations§
§impl<T> Downcast for Twhere
T: Any,
impl<T> Downcast for Twhere
T: Any,
§fn into_any(self: Box<T, Global>) -> Box<dyn Any + 'static, Global>
fn into_any(self: Box<T, Global>) -> Box<dyn Any + 'static, Global>
Box<dyn Trait>
(where Trait: Downcast
) to Box<dyn Any>
. Box<dyn Any>
can
then be further downcast
into Box<ConcreteType>
where ConcreteType
implements Trait
.§fn into_any_rc(self: Rc<T>) -> Rc<dyn Any + 'static>
fn into_any_rc(self: Rc<T>) -> Rc<dyn Any + 'static>
Rc<Trait>
(where Trait: Downcast
) to Rc<Any>
. Rc<Any>
can then be
further downcast
into Rc<ConcreteType>
where ConcreteType
implements Trait
.§fn as_any(&self) -> &(dyn Any + 'static)
fn as_any(&self) -> &(dyn Any + 'static)
&Trait
(where Trait: Downcast
) to &Any
. This is needed since Rust cannot
generate &Any
’s vtable from &Trait
’s.§fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
&mut Trait
(where Trait: Downcast
) to &Any
. This is needed since Rust cannot
generate &mut Any
’s vtable from &mut Trait
’s.source§impl<T> Instrument for T
impl<T> Instrument for T
source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
source§impl<T, Outer> IsWrappedBy<Outer> for Twhere
Outer: AsRef<T> + AsMut<T> + From<T>,
T: From<Outer>,
impl<T, Outer> IsWrappedBy<Outer> for Twhere
Outer: AsRef<T> + AsMut<T> + From<T>,
T: From<Outer>,
source§impl<S, T> UncheckedInto<T> for Swhere
T: UncheckedFrom<S>,
impl<S, T> UncheckedInto<T> for Swhere
T: UncheckedFrom<S>,
source§fn unchecked_into(self) -> T
fn unchecked_into(self) -> T
unchecked_from
.