1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 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 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681
use std::{
any::Any,
cell::RefCell,
collections::btree_map::{BTreeMap, Entry},
fmt,
marker::PhantomData,
mem,
};
use oasis_core_runtime::{common::crypto::hash::Hash, consensus::roothash, storage::mkvs};
use crate::{
context::Context,
crypto::{random::RootRng, signature::PublicKey},
event::{Event, EventTag, EventTags},
modules::core::Error,
storage::{MKVSStore, NestedStore, OverlayStore, Store},
types::{address::Address, message::MessageEventHookInvocation, transaction},
};
/// Execution mode.
#[derive(Clone, Copy, Default, Debug, PartialEq, Eq)]
#[repr(u8)]
pub enum Mode {
/// Actually execute transactions during block production.
#[default]
Execute,
/// Check that transactions are valid for local acceptance into the transaction pool.
Check,
/// Simulate transaction outcomes (e.g. for gas estimation).
Simulate,
/// Check that transactions are still valid before scheduling.
PreSchedule,
}
const MODE_CHECK: &str = "check";
const MODE_EXECUTE: &str = "execute";
const MODE_SIMULATE: &str = "simulate";
const MODE_PRE_SCHEDULE: &str = "pre_schedule";
impl fmt::Display for Mode {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(self.into())
}
}
impl From<&Mode> for &'static str {
fn from(m: &Mode) -> Self {
match m {
Mode::Check => MODE_CHECK,
Mode::Execute => MODE_EXECUTE,
Mode::Simulate => MODE_SIMULATE,
Mode::PreSchedule => MODE_PRE_SCHEDULE,
}
}
}
/// Information about the execution environment.
#[derive(Clone, Default, Debug)]
pub struct Environment {
mode: Mode,
tx: Option<TransactionWithMeta>,
internal: bool,
}
impl Environment {
/// Execution mode.
pub fn mode(&self) -> Mode {
self.mode
}
/// Whether the execution mode is such that only checks should be performed.
pub fn is_check_only(&self) -> bool {
matches!(self.mode, Mode::Check | Mode::PreSchedule)
}
/// Whether the execution mode is `Mode::PreSchedule`.
pub fn is_pre_schedule(&self) -> bool {
matches!(self.mode, Mode::PreSchedule)
}
/// Whether the execution mode is `Mode::Simulate`.
pub fn is_simulation(&self) -> bool {
matches!(self.mode, Mode::Simulate)
}
/// Whether the execution mode is `Mode::Execute`.
pub fn is_execute(&self) -> bool {
matches!(self.mode, Mode::Execute)
}
/// Whether there is an active transaction in the current environment.
pub fn is_transaction(&self) -> bool {
self.tx.is_some()
}
/// An active transaction's index (order) within the block.
///
/// # Panics
///
/// This method will panic if called outside a transaction environment.
pub fn tx_index(&self) -> usize {
self.tx
.as_ref()
.map(|tx| tx.index)
.expect("only in transaction environment")
}
/// An active transaction's size in bytes.
///
/// # Panics
///
/// This method will panic if called outside a transaction environment.
pub fn tx_size(&self) -> u32 {
self.tx
.as_ref()
.map(|tx| tx.size)
.expect("only in transaction environment")
}
/// An active transaction's authentication information.
///
/// # Panics
///
/// This method will panic if called outside a transaction environment.
pub fn tx_auth_info(&self) -> &transaction::AuthInfo {
self.tx
.as_ref()
.map(|tx| &tx.data.auth_info)
.expect("only in transaction environment")
}
/// An active transaction's call format.
///
/// # Panics
///
/// This method will panic if called outside a transaction environment.
pub fn tx_call_format(&self) -> transaction::CallFormat {
self.tx
.as_ref()
.map(|tx| tx.data.call.format)
.expect("only in transaction environment")
}
/// An active transaction's read only flag.
///
/// # Panics
///
/// This method will panic if called outside a transaction environment.
pub fn is_read_only(&self) -> bool {
self.tx
.as_ref()
.map(|tx| tx.data.call.read_only)
.expect("only in transaction environment")
}
/// Whether the current execution environment is part of an internal subcall.
pub fn is_internal(&self) -> bool {
self.internal
}
/// Authenticated address of the caller.
///
/// In case there are multiple signers of a transaction, this will return the address
/// corresponding to the first signer. If there are no signers, it returns the default address.
///
/// # Panics
///
/// This method will panic if called outside a transaction environment.
pub fn tx_caller_address(&self) -> Address {
self.tx_auth_info()
.signer_info
.first()
.map(|si| si.address_spec.address())
.unwrap_or_default()
}
/// Authenticated caller public key if available.
///
/// In case there are multiple signers of a transaction, this will return the public key
/// corresponding to the first signer. If there are no signers or if the address specification
/// does not represent a single public key, it returns `None`.
///
/// # Panics
///
/// This method will panic if called outside a transaction environment.
pub fn tx_caller_public_key(&self) -> Option<PublicKey> {
self.tx_auth_info()
.signer_info
.first()
.and_then(|si| si.address_spec.public_key())
}
}
/// Decoded transaction with additional metadata.
#[derive(Clone, Debug)]
pub struct TransactionWithMeta {
/// Decoded transaction.
pub data: transaction::Transaction,
/// Transaction size.
pub size: u32,
/// Transaction index within the batch.
pub index: usize,
/// Transaction hash.
pub hash: Hash,
}
impl TransactionWithMeta {
/// Create transaction with metadata for an internally generated transaction.
///
/// Internally generated transactions have zero size, index and hash.
pub fn internal(tx: transaction::Transaction) -> Self {
Self {
data: tx,
size: 0,
index: 0,
hash: Default::default(),
}
}
}
#[cfg(any(test, feature = "test"))]
impl From<transaction::Transaction> for TransactionWithMeta {
fn from(tx: transaction::Transaction) -> Self {
Self::internal(tx) // For use in tests.
}
}
/// Environment modification options.
#[derive(Clone, Default, Debug)]
pub struct Options {
pub mode: Option<Mode>,
pub tx: Option<TransactionWithMeta>,
pub internal: Option<bool>,
pub rng_local_entropy: bool,
}
impl Options {
/// Create options with default values.
pub fn new() -> Self {
Self::default()
}
/// Change the execution mode of the environment.
pub fn with_mode(self, mode: Mode) -> Self {
Self {
mode: Some(mode),
..self
}
}
/// Change the active transaction of the environment.
pub fn with_tx(self, tx: TransactionWithMeta) -> Self {
Self {
tx: Some(tx),
..self
}
}
/// Change the internal flag of the environment.
pub fn with_internal(self, internal: bool) -> Self {
Self {
internal: Some(internal),
..self
}
}
/// Request for local entropy to be mixed into the current RNG.
///
/// # Determinisim
///
/// Using this method will result in non-deterministic behavior as the node's local entropy is
/// mixed into the RNG. As such, this method should only be used in cases where non-determinism
/// is not problematic (e.g. local queries).
pub fn with_rng_local_entropy(self) -> Self {
Self {
rng_local_entropy: true,
..self
}
}
}
/// Mutable block state of a runtime.
///
/// The state includes storage, emitted events, messages to consensus layer, etc. States can be
/// nested via `open`, `commit` and `rollback` methods which behave like transactions.
pub struct State {
parent: Option<Box<State>>,
store: Option<OverlayStore<Box<dyn Store>>>,
events: EventTags,
unconditional_events: EventTags,
messages: Vec<(roothash::Message, MessageEventHookInvocation)>,
block_values: BTreeMap<&'static str, Box<dyn Any>>,
hidden_block_values: Option<BTreeMap<&'static str, Box<dyn Any>>>,
local_values: BTreeMap<&'static str, Box<dyn Any>>,
rng: Option<RootRng>,
hidden_rng: Option<RootRng>,
env: Environment,
always_rollback: bool,
}
impl State {
/// Initialize the state with the given options.
fn init(&mut self, opts: Options) {
if let Some(mode) = opts.mode {
// Change mode.
self.env.mode = mode;
// If we have enabled pre-schedule or simulation mode, always rollback state and hide
// block values to prevent leaking them.
if matches!(mode, Mode::PreSchedule | Mode::Simulate) {
self.always_rollback = true;
self.hide_block_values();
}
}
if let Some(tx) = opts.tx {
// Change RNG state.
self.rng.as_mut().unwrap().append_tx(tx.hash);
// Change tx metadata.
self.env.tx = Some(tx);
}
if let Some(internal) = opts.internal {
self.env.internal = internal;
if internal {
self.hide_block_values();
}
}
if opts.rng_local_entropy {
// Append local entropy to RNG state.
self.rng.as_mut().unwrap().append_local_entropy();
}
if !matches!(self.env.mode, Mode::PreSchedule) {
// Record opening a child state in the RNG.
self.rng.as_mut().unwrap().append_subcontext();
} else {
// Use an invalid RNG as its use is not allowed in pre-schedule context.
self.disable_rng();
}
}
/// Open a child state after which self will point to the child state.
pub fn open(&mut self) {
let mut parent = Self {
parent: None,
store: None,
events: EventTags::new(),
unconditional_events: EventTags::new(),
messages: Vec::new(),
block_values: BTreeMap::new(),
hidden_block_values: None,
local_values: BTreeMap::new(),
rng: None,
hidden_rng: None,
env: self.env.clone(),
always_rollback: false,
};
mem::swap(&mut parent, self);
// Wrap parent store to create an overlay child store.
self.store = parent
.store
.take()
.map(|pstore| OverlayStore::new(Box::new(pstore) as Box<dyn Store>));
// Take block values map. We will put it back after commit/rollback.
mem::swap(&mut parent.block_values, &mut self.block_values);
// Take RNG. We will put it back after commit/rollback.
mem::swap(&mut parent.rng, &mut self.rng);
self.parent = Some(Box::new(parent));
}
fn convert_store(store: Box<dyn Store>) -> OverlayStore<Box<dyn Store>> {
let raw = Box::into_raw(store);
unsafe {
// SAFETY: This is safe because we always wrap child stores into OverlayStore.
*Box::from_raw(raw as *mut OverlayStore<Box<dyn Store>>)
}
}
/// Commit the current state and return to its parent state.
///
/// # Panics
///
/// This method will panic when attempting to commit the root state.
pub fn commit(&mut self) {
if self.always_rollback {
self.rollback();
} else {
self._commit();
}
}
fn _commit(&mut self) {
let mut child = *self.parent.take().expect("cannot commit on root state");
mem::swap(&mut child, self);
// Commit storage.
self.store = child
.store
.take()
.map(|cstore| Self::convert_store(cstore.commit()));
// Propagate messages.
self.messages.extend(child.messages);
// Propagate events.
for (key, event) in child.events {
let events = self.events.entry(key).or_default();
events.extend(event);
}
for (key, event) in child.unconditional_events {
let events = self.unconditional_events.entry(key).or_default();
events.extend(event);
}
// Put back per-block values.
if let Some(mut block_values) = child.hidden_block_values {
mem::swap(&mut block_values, &mut self.block_values); // Block values were hidden.
} else {
mem::swap(&mut child.block_values, &mut self.block_values);
}
// Always drop local values.
// Put back RNG.
if child.hidden_rng.is_some() {
mem::swap(&mut child.hidden_rng, &mut self.rng); // RNG was hidden.
} else {
mem::swap(&mut child.rng, &mut self.rng);
}
}
/// Rollback the current state and return to its parent state.
///
/// # Panics
///
/// This method will panic when attempting to rollback the root state.
pub fn rollback(&mut self) {
let mut child = *self.parent.take().expect("cannot rollback on root state");
mem::swap(&mut child, self);
// Rollback storage.
self.store = child
.store
.take()
.map(|cstore| Self::convert_store(cstore.rollback()));
// Always put back per-block values.
if let Some(mut block_values) = child.hidden_block_values {
mem::swap(&mut block_values, &mut self.block_values); // Block values were hidden.
} else {
mem::swap(&mut child.block_values, &mut self.block_values);
}
// Always drop local values.
// Always put back RNG.
if child.hidden_rng.is_some() {
mem::swap(&mut child.hidden_rng, &mut self.rng); // RNG was hidden.
} else {
mem::swap(&mut child.rng, &mut self.rng);
}
}
/// Fetches a block state value entry.
///
/// Block values live as long as the root `State` and are propagated to child states. They are
/// not affected by state rollbacks. If you need state-scoped values, use local values.
pub fn block_value<V: Any>(&mut self, key: &'static str) -> StateValue<'_, V> {
StateValue::new(self.block_values.entry(key))
}
/// Fetches a local state value entry.
///
/// Local values only live as long as the current `State`, are dropped upon exiting to parent
/// state and child states start with an empty set. If you need longer-lived values, use block
/// values.
pub fn local_value<V: Any>(&mut self, key: &'static str) -> StateValue<'_, V> {
StateValue::new(self.local_values.entry(key))
}
/// Hides block values from the current state which will have an empty set of values after this
/// method returns. Hidden values will be restored upon exit to parent state.
pub fn hide_block_values(&mut self) {
if self.parent.is_none() {
// Allowing hiding on root state would prevent those values from ever being recovered.
panic!("cannot hide block values on root state");
}
if self.hidden_block_values.is_some() {
return; // Parent block values already hidden.
}
self.hidden_block_values = Some(mem::take(&mut self.block_values));
}
/// Emitted messages count returns the number of messages emitted so far across this and all
/// parent states.
pub fn emitted_messages_count(&self) -> usize {
self.messages.len()
+ self
.parent
.as_ref()
.map(|p| p.emitted_messages_count())
.unwrap_or_default()
}
/// Emitted messages count returns the number of messages emitted so far in this state, not
/// counting any parent states.
pub fn emitted_messages_local_count(&self) -> usize {
self.messages.len()
}
/// Maximum number of messages that can be emitted.
pub fn emitted_messages_max<C: Context>(&self, ctx: &C) -> u32 {
if self.env.is_transaction() {
let limit = self.env.tx_auth_info().fee.consensus_messages;
if limit > 0 {
limit
} else {
ctx.max_messages() // Zero means an implicit limit by gas use.
}
} else {
ctx.max_messages()
}
}
/// Queue a message to be emitted by the runtime for consensus layer to process.
pub fn emit_message<C: Context>(
&mut self,
ctx: &C,
msg: roothash::Message,
hook: MessageEventHookInvocation,
) -> Result<(), Error> {
// Check against maximum number of messages that can be emitted per round.
if self.emitted_messages_count() >= self.emitted_messages_max(ctx) as usize {
return Err(Error::OutOfMessageSlots);
}
self.messages.push((msg, hook));
Ok(())
}
/// Take all messages accumulated in the current state.
pub fn take_messages(&mut self) -> Vec<(roothash::Message, MessageEventHookInvocation)> {
mem::take(&mut self.messages)
}
/// Emit an event.
pub fn emit_event<E: Event>(&mut self, event: E) {
self.emit_event_raw(event.into_event_tag());
}
/// Emit a raw event.
pub fn emit_event_raw(&mut self, etag: EventTag) {
let events = self.events.entry(etag.key).or_default();
events.push(etag.value);
}
/// Emit an unconditional event.
///
/// The only difference to regular events is that these are handled as a separate set.
pub fn emit_unconditional_event<E: Event>(&mut self, event: E) {
let etag = event.into_event_tag();
let events = self.unconditional_events.entry(etag.key).or_default();
events.push(etag.value);
}
/// Take all regular events accumulated in the current state.
pub fn take_events(&mut self) -> EventTags {
mem::take(&mut self.events)
}
/// Take all unconditional events accumulated in the current state.
pub fn take_unconditional_events(&mut self) -> EventTags {
mem::take(&mut self.unconditional_events)
}
/// Take all events accumulated in the current state and return the merged set.
pub fn take_all_events(&mut self) -> EventTags {
let mut events = self.take_events();
let unconditional_events = self.take_unconditional_events();
for (key, val) in unconditional_events {
let tag = events.entry(key).or_default();
tag.extend(val)
}
events
}
/// Store associated with the state.
///
/// # Panics
///
/// This method will panic if no store exists.
pub fn store(&mut self) -> &mut dyn Store {
self.store.as_mut().unwrap()
}
/// Whether the store associated with the state has any pending updates.
pub fn has_pending_store_updates(&self) -> bool {
self.store
.as_ref()
.map(|store| store.has_pending_updates())
.unwrap_or_default()
}
/// Size (in bytes) of any pending updates in the associated store.
pub fn pending_store_update_byte_size(&self) -> usize {
self.store
.as_ref()
.map(|store| store.pending_update_byte_size())
.unwrap_or_default()
}
/// Random number generator.
pub fn rng(&mut self) -> &mut RootRng {
self.rng.as_mut().unwrap()
}
/// Disables the RNG by replacing the instance with an invalid RNG.
fn disable_rng(&mut self) {
if self.parent.is_none() {
// Allowing hiding on root state would prevent the RNG from ever being recovered.
panic!("cannot hide the RNG on root state");
}
if self.hidden_rng.is_some() {
return; // Parent RNG already hidden.
}
self.hidden_rng = mem::replace(&mut self.rng, Some(RootRng::invalid()));
}
/// Environment information.
pub fn env(&self) -> &Environment {
&self.env
}
/// Origin environment information.
///
/// The origin environment is the first non-internal environment in the hierarchy.
pub fn env_origin(&self) -> &Environment {
match self.parent {
Some(ref parent) if self.env.internal => parent.env_origin(),
_ => &self.env,
}
}
/// Returns the nesting level of the current state.
pub fn level(&self) -> usize {
if let Some(ref parent) = self.parent {
parent.level() + 1
} else {
0
}
}
}
thread_local! {
static CURRENT: RefCell<Vec<State>> = const { RefCell::new(Vec::new()) };
}
struct CurrentStateGuard;
impl Drop for CurrentStateGuard {
fn drop(&mut self) {
CURRENT.with(|c| {
let root = c.borrow_mut().pop().expect("must have current state");
// Commit root state as it has been wrapped in an overlay.
let store = root
.store
.expect("must not have open child states after exiting root state");
store.commit();
});
}
}
struct TransactionGuard(usize);
impl Drop for TransactionGuard {
fn drop(&mut self) {
let level = CurrentState::with(|state| state.level());
// If transaction hasn't been either committed or reverted, rollback.
if level == self.0 {
CurrentState::rollback_transaction();
}
}
}
/// Result of a transaction helper closure.
pub enum TransactionResult<T> {
Commit(T),
Rollback(T),
}
impl From<()> for TransactionResult<()> {
fn from(_: ()) -> TransactionResult<()> {
TransactionResult::Commit(())
}
}
impl<R, E> From<Result<R, E>> for TransactionResult<Result<R, E>> {
fn from(v: Result<R, E>) -> TransactionResult<Result<R, E>> {
match v {
Ok(_) => TransactionResult::Commit(v),
Err(_) => TransactionResult::Rollback(v),
}
}
}
/// State attached to the current thread.
pub struct CurrentState;
impl CurrentState {
/// Attach a new state to the current thread and enter the state's context.
///
/// The passed store is used as the root store.
///
/// # Panics
///
/// This method will panic if called from within a `CurrentState::with` block.
pub fn enter<S, F, R>(root: S, f: F) -> R
where
S: Store,
F: FnOnce() -> R,
{
Self::enter_opts(
Options {
mode: Some(Default::default()), // Make sure there is a default mode.
..Options::default()
},
root,
f,
)
}
/// Attach a new state to the current thread and enter the state's context.
///
/// The passed store is used as the root store.
///
/// # Panics
///
/// This method will panic if called from within a `CurrentState::with` block or if the mode
/// has not been explicitly set in `opts`.
pub fn enter_opts<S, F, R>(opts: Options, mut root: S, f: F) -> R
where
S: Store,
F: FnOnce() -> R,
{
let root = unsafe {
// SAFETY: Keeping the root store is safe as it can only be accessed from the current
// thread while we are running inside `CurrentState::enter` where we are holding a
// mutable reference on it.
std::mem::transmute::<&mut dyn Store, &mut (dyn Store + 'static)>(
&mut root as &mut dyn Store,
)
};
// Initialize the root state.
let mode = opts
.mode
.expect("mode must be explicitly set on root state");
let mut root = State {
parent: None,
store: Some(OverlayStore::new(Box::new(root) as Box<dyn Store>)),
events: EventTags::new(),
unconditional_events: EventTags::new(),
messages: Vec::new(),
block_values: BTreeMap::new(),
hidden_block_values: None,
local_values: BTreeMap::new(),
rng: Some(RootRng::new(mode)),
hidden_rng: None,
env: Default::default(),
always_rollback: false,
};
// Apply options to allow customization of the root state.
root.init(opts);
CURRENT.with(|c| {
c.try_borrow_mut()
.expect("must not re-enter from with block")
.push(root)
});
let _guard = CurrentStateGuard; // Ensure current state is popped once we return.
f()
}
/// Create an empty baseline state for the current thread.
///
/// This should only be used in tests to have state always available.
///
/// # Panics
///
/// This method will panic if any states have been attached to the local thread or if called
/// within a `CurrentState::with` block.
#[doc(hidden)]
pub(crate) fn init_local_fallback() {
thread_local! {
static BASE_STATE_INIT: RefCell<bool> = const { RefCell::new(false) };
}
BASE_STATE_INIT.with(|initialized| {
// Initialize once per thread.
if *initialized.borrow() {
return;
}
*initialized.borrow_mut() = true;
let root = mkvs::OverlayTree::new(
mkvs::Tree::builder()
.with_root_type(mkvs::RootType::State)
.build(Box::new(mkvs::sync::NoopReadSyncer)),
);
let root = MKVSStore::new(root);
// Initialize the root state.
let root = State {
parent: None,
store: Some(OverlayStore::new(Box::new(root) as Box<dyn Store>)),
events: EventTags::new(),
unconditional_events: EventTags::new(),
messages: Vec::new(),
block_values: BTreeMap::new(),
hidden_block_values: None,
local_values: BTreeMap::new(),
rng: Some(RootRng::new(Default::default())),
hidden_rng: None,
env: Default::default(),
always_rollback: false,
};
CURRENT.with(|c| {
let mut current = c
.try_borrow_mut()
.expect("must not re-enter from with block");
assert!(
current.is_empty(),
"must have no prior states attached to local thread"
);
current.push(root);
});
});
}
/// Run a closure with the currently active state.
///
/// # Panics
///
/// This method will panic if called outside `CurrentState::enter` or if any transaction methods
/// are called from the closure.
pub fn with<F, R>(f: F) -> R
where
F: FnOnce(&mut State) -> R,
{
CURRENT.with(|c| {
let mut current_ref = c.try_borrow_mut().expect("must not re-enter with");
let current = current_ref.last_mut().expect("must enter context");
f(current)
})
}
/// Run a closure with the store of the currently active state.
///
/// # Panics
///
/// This method will panic if called outside `CurrentState::enter` or if any transaction methods
/// are called from the closure.
pub fn with_store<F, R>(f: F) -> R
where
F: FnOnce(&mut dyn Store) -> R,
{
Self::with(|state| f(state.store()))
}
/// Run a closure with the environment of the currently active state.
///
/// # Panics
///
/// This method will panic if called outside `CurrentState::enter` or if any transaction methods
/// are called from the closure.
pub fn with_env<F, R>(f: F) -> R
where
F: FnOnce(&Environment) -> R,
{
Self::with(|state| f(state.env()))
}
/// Run a closure with the origin environment of the currently active state.
///
/// # Panics
///
/// This method will panic if called outside `CurrentState::enter` or if any transaction methods
/// are called from the closure.
pub fn with_env_origin<F, R>(f: F) -> R
where
F: FnOnce(&Environment) -> R,
{
Self::with(|state| f(state.env_origin()))
}
/// Start a new transaction by opening a new child state.
///
/// # Panics
///
/// This method will panic if called outside `CurrentState::enter` or if called within a
/// `CurrentState::with` block.
pub fn start_transaction() {
Self::with(|state| state.open());
}
/// Commit a previously started transaction.
///
/// # Panics
///
/// This method will panic if called outside `CurrentState::enter`, if there is no currently
/// open transaction (started via `CurrentState::start_transaction`) or if called within a
/// `CurrentState::with` block.
pub fn commit_transaction() {
Self::with(|state| state.commit());
}
/// Rollback a previously started transaction.
///
/// # Panics
///
/// This method will panic if called outside `CurrentState::enter`, if there is no currently
/// open transaction (started via `CurrentState::start_transaction`) or if called within a
/// `CurrentState::with` block.
pub fn rollback_transaction() {
Self::with(|state| state.rollback());
}
/// Run a closure within a state transaction.
///
/// If the closure returns `TransactionResult::Commit(R)` then the child state is committed,
/// otherwise the child state is rolled back.
pub fn with_transaction<F, R, Rs>(f: F) -> R
where
F: FnOnce() -> Rs,
Rs: Into<TransactionResult<R>>,
{
Self::with_transaction_opts(Options::default(), f)
}
/// Run a closure within a state transaction, allowing the caller to customize state.
///
/// If the closure returns `TransactionResult::Commit(R)` then the child state is committed,
/// otherwise the child state is rolled back.
pub fn with_transaction_opts<F, R, Rs>(opts: Options, f: F) -> R
where
F: FnOnce() -> Rs,
Rs: Into<TransactionResult<R>>,
{
let level = Self::with(|state| {
state.open();
state.init(opts);
state.level()
});
let _guard = TransactionGuard(level); // Ensure transaction is always closed.
match f().into() {
TransactionResult::Commit(result) => {
Self::commit_transaction();
result
}
TransactionResult::Rollback(result) => {
Self::rollback_transaction();
result
}
}
}
}
/// A per-state arbitrary value.
pub struct StateValue<'a, V> {
inner: Entry<'a, &'static str, Box<dyn Any>>,
_value: PhantomData<V>,
}
impl<'a, V: Any> StateValue<'a, V> {
fn new(inner: Entry<'a, &'static str, Box<dyn Any>>) -> Self {
Self {
inner,
_value: PhantomData,
}
}
/// Gets a reference to the specified per-state value.
///
/// # Panics
///
/// Panics if the retrieved type is not the type that was stored.
pub fn get(self) -> Option<&'a V> {
match self.inner {
Entry::Occupied(oe) => Some(
oe.into_mut()
.downcast_ref()
.expect("type should stay the same"),
),
_ => None,
}
}
/// Gets a mutable reference to the specified per-state value.
///
/// # Panics
///
/// Panics if the retrieved type is not the type that was stored.
pub fn get_mut(&mut self) -> Option<&mut V> {
match &mut self.inner {
Entry::Occupied(oe) => Some(
oe.get_mut()
.downcast_mut()
.expect("type should stay the same"),
),
_ => None,
}
}
/// Sets the context value, returning a mutable reference to the set value.
///
/// # Panics
///
/// Panics if the retrieved type is not the type that was stored.
pub fn set(self, value: V) -> &'a mut V {
let value = Box::new(value);
match self.inner {
Entry::Occupied(mut oe) => {
oe.insert(value);
oe.into_mut()
}
Entry::Vacant(ve) => ve.insert(value),
}
.downcast_mut()
.expect("type should stay the same")
}
/// Takes the context value, if it exists.
///
/// # Panics
///
/// Panics if the retrieved type is not the type that was stored.
pub fn take(self) -> Option<V> {
match self.inner {
Entry::Occupied(oe) => {
Some(*oe.remove().downcast().expect("type should stay the same"))
}
Entry::Vacant(_) => None,
}
}
}
impl<'a, V: Any + Default> StateValue<'a, V> {
/// Retrieves the existing value or inserts and returns the default.
///
/// # Panics
///
/// Panics if the retrieved type is not the type that was stored.
pub fn or_default(self) -> &'a mut V {
match self.inner {
Entry::Occupied(oe) => oe.into_mut(),
Entry::Vacant(ve) => ve.insert(Box::<V>::default()),
}
.downcast_mut()
.expect("type should stay the same")
}
}
#[cfg(test)]
mod test {
use oasis_core_runtime::{
common::versioned::Versioned,
consensus::{roothash, staking},
storage::mkvs,
};
use super::{CurrentState, Mode, Options, TransactionResult, TransactionWithMeta};
use crate::{
modules::core::Event,
storage::{MKVSStore, Store},
testing::mock::{self, Mock},
types::message::MessageEventHookInvocation,
};
#[test]
fn test_value() {
CurrentState::init_local_fallback();
CurrentState::with(|state| {
let x = state.block_value::<u64>("module.TestKey").get();
assert_eq!(x, None);
state.block_value::<u64>("module.TestKey").set(42);
let y = state.block_value::<u64>("module.TestKey").get();
assert_eq!(y, Some(&42u64));
let z = state.block_value::<u64>("module.TestKey").take();
assert_eq!(z, Some(42u64));
let y = state.block_value::<u64>("module.TestKey").get();
assert_eq!(y, None);
});
}
#[test]
#[should_panic]
fn test_value_type_change_block_value() {
CurrentState::init_local_fallback();
CurrentState::with(|state| {
state.block_value::<u64>("module.TestKey").or_default();
state.block_value::<u32>("module.TestKey").get();
});
}
#[test]
#[should_panic]
fn test_value_type_change_local_value() {
CurrentState::init_local_fallback();
CurrentState::with(|state| {
state.local_value::<u64>("module.TestKey").or_default();
state.local_value::<u32>("module.TestKey").get();
});
}
#[test]
fn test_value_hidden_block_values() {
CurrentState::init_local_fallback();
CurrentState::with(|state| {
state.block_value("module.TestKey").set(42u64);
state.open();
state.hide_block_values();
let v = state.block_value::<u64>("module.TestKey").get();
assert!(v.is_none(), "block values should not propagate when hidden");
state.block_value("module.TestKey").set(48u64);
state.commit();
let v = state.block_value::<u64>("module.TestKey").get();
assert_eq!(
v,
Some(&42u64),
"block values should not propagate when hidden"
);
});
}
#[test]
fn test_value_local() {
CurrentState::init_local_fallback();
CurrentState::with(|state| {
state.block_value("module.TestKey").set(42u64);
state.open();
let mut y = state.block_value::<u64>("module.TestKey");
let y = y.get_mut().unwrap();
assert_eq!(*y, 42);
*y = 48;
let a = state.local_value::<u64>("module.TestTxKey").get();
assert_eq!(a, None);
state.local_value::<u64>("module.TestTxKey").set(65);
let b = state.local_value::<u64>("module.TestTxKey").get();
assert_eq!(b, Some(&65));
let c = state
.local_value::<u64>("module.TestTakeTxKey")
.or_default();
*c = 67;
let d = state.local_value::<u64>("module.TestTakeTxKey").take();
assert_eq!(d, Some(67));
let e = state.local_value::<u64>("module.TestTakeTxKey").get();
assert_eq!(e, None);
state.rollback(); // Block values are always propagated.
let x = state.block_value::<u64>("module.TestKey").get();
assert_eq!(x, Some(&48));
state.open();
let z = state.block_value::<u64>("module.TestKey").take();
assert_eq!(z, Some(48));
let a = state.local_value::<u64>("module.TestTxKey").get();
assert_eq!(a, None, "local values should not be propagated");
state.rollback(); // Block values are always propagated.
let y = state.block_value::<u64>("module.TestKey").get();
assert_eq!(y, None);
});
}
#[test]
fn test_emit_messages() {
let mut mock = Mock::default(); // Also creates local fallback state.
let max_messages = mock.max_messages as usize;
let ctx = mock.create_ctx();
CurrentState::with(|state| {
state.open();
assert_eq!(state.emitted_messages_count(), 0);
assert_eq!(state.emitted_messages_local_count(), 0);
state
.emit_message(
&ctx,
roothash::Message::Staking(Versioned::new(
0,
roothash::StakingMessage::Transfer(staking::Transfer::default()),
)),
MessageEventHookInvocation::new("test".to_string(), ""),
)
.expect("message emission should succeed");
assert_eq!(state.emitted_messages_count(), 1);
assert_eq!(state.emitted_messages_local_count(), 1);
assert_eq!(state.emitted_messages_max(&ctx), max_messages as u32);
state.open(); // Start child state.
assert_eq!(state.emitted_messages_local_count(), 0);
state
.emit_message(
&ctx,
roothash::Message::Staking(Versioned::new(
0,
roothash::StakingMessage::Transfer(staking::Transfer::default()),
)),
MessageEventHookInvocation::new("test".to_string(), ""),
)
.expect("message emission should succeed");
assert_eq!(state.emitted_messages_count(), 2);
assert_eq!(state.emitted_messages_local_count(), 1);
assert_eq!(state.emitted_messages_max(&ctx), max_messages as u32);
state.rollback(); // Rollback.
assert_eq!(
state.emitted_messages_count(),
1,
"emitted message should have been rolled back"
);
assert_eq!(state.emitted_messages_local_count(), 1);
state.open(); // Start child state.
assert_eq!(state.emitted_messages_local_count(), 0);
state
.emit_message(
&ctx,
roothash::Message::Staking(Versioned::new(
0,
roothash::StakingMessage::Transfer(staking::Transfer::default()),
)),
MessageEventHookInvocation::new("test".to_string(), ""),
)
.expect("message emission should succeed");
assert_eq!(state.emitted_messages_count(), 2);
assert_eq!(state.emitted_messages_local_count(), 1);
state.commit(); // Commit.
assert_eq!(
state.emitted_messages_count(),
2,
"emitted message should have been committed"
);
// Emit some more messages.
for _ in 0..max_messages - 2 {
state
.emit_message(
&ctx,
roothash::Message::Staking(Versioned::new(
0,
roothash::StakingMessage::Transfer(staking::Transfer::default()),
)),
MessageEventHookInvocation::new("test".to_string(), ""),
)
.expect("message emission should succeed");
}
assert_eq!(state.emitted_messages_count(), max_messages);
// Emitting one more message should be rejected.
state
.emit_message(
&ctx,
roothash::Message::Staking(Versioned::new(
0,
roothash::StakingMessage::Transfer(staking::Transfer::default()),
)),
MessageEventHookInvocation::new("test".to_string(), ""),
)
.expect_err("message emission should fail due to out of slots");
assert_eq!(state.emitted_messages_count(), max_messages);
state.rollback(); // Rollback.
assert_eq!(state.emitted_messages_count(), 0);
});
// Change the maximum amount of messages.
let mut tx = mock::transaction();
tx.auth_info.fee.consensus_messages = 1; // Limit amount of messages.
CurrentState::with_transaction_opts(Options::new().with_tx(tx.into()), || {
CurrentState::with(|state| {
assert_eq!(state.emitted_messages_max(&ctx), 1);
});
});
let mut tx = mock::transaction();
tx.auth_info.fee.consensus_messages = 0; // Zero means an implicit limit by gas use.
CurrentState::with_transaction_opts(Options::new().with_tx(tx.into()), || {
CurrentState::with(|state| {
assert_eq!(state.emitted_messages_max(&ctx), max_messages as u32);
});
});
}
#[test]
fn test_emit_events() {
CurrentState::init_local_fallback();
CurrentState::with(|state| {
state.open();
state.open();
state.emit_event(Event::GasUsed { amount: 41 });
state.emit_event(Event::GasUsed { amount: 42 });
state.emit_event(Event::GasUsed { amount: 43 });
state.emit_unconditional_event(Event::GasUsed { amount: 10 });
state.commit();
let events = state.take_events();
assert_eq!(events.len(), 1, "events should have been propagated");
let event_key = b"core\x00\x00\x00\x01".to_vec();
assert_eq!(events[&event_key].len(), 3);
let events = state.take_unconditional_events();
assert_eq!(
events.len(),
1,
"unconditional events should have been propagated"
);
let event_key = b"core\x00\x00\x00\x01".to_vec();
assert_eq!(events[&event_key].len(), 1);
state.emit_event(Event::GasUsed { amount: 41 });
state.emit_event(Event::GasUsed { amount: 42 });
state.emit_event(Event::GasUsed { amount: 43 });
state.emit_unconditional_event(Event::GasUsed { amount: 20 });
state.rollback();
let events = state.take_events();
assert_eq!(events.len(), 0, "events should not have been propagated");
let events = state.take_unconditional_events();
assert_eq!(
events.len(),
0,
"unconditional events should not have been propagated"
);
});
}
fn test_store_basic() {
CurrentState::start_transaction();
assert!(
!CurrentState::with(|state| state.has_pending_store_updates()),
"should not have pending updates"
);
CurrentState::with_store(|store| {
store.insert(b"test", b"value");
});
assert!(
CurrentState::with(|state| state.has_pending_store_updates()),
"should have pending updates after insert"
);
// Transaction helper.
CurrentState::with_transaction(|| {
assert!(
!CurrentState::with(|state| state.has_pending_store_updates()),
"should not have pending updates"
);
CurrentState::with_store(|store| {
store.insert(b"test", b"b0rken");
});
assert!(
CurrentState::with(|state| state.has_pending_store_updates()),
"should have pending updates after insert"
);
TransactionResult::Rollback(())
});
// Transaction helper with options.
CurrentState::with_transaction_opts(
Options::new()
.with_mode(Mode::Check)
.with_internal(true)
.with_tx(TransactionWithMeta {
data: mock::transaction(),
size: 888,
index: 42,
hash: Default::default(),
}),
|| {
CurrentState::with_env(|env| {
assert!(env.is_check_only(), "environment should be updated");
assert!(env.is_internal(), "environment should be updated");
assert!(env.is_transaction(), "environment should be updated");
assert_eq!(env.tx_index(), 42, "environment should be updated");
assert_eq!(env.tx_size(), 888, "environment should be updated");
});
CurrentState::with_env_origin(|env_origin| {
assert!(
!env_origin.is_check_only(),
"origin environment should be correct"
);
assert!(
!env_origin.is_transaction(),
"origin environment should be correct"
);
});
CurrentState::with_transaction(|| {
// Check environment propagation.
CurrentState::with_env(|env| {
assert!(env.is_check_only(), "environment should propagate");
assert!(env.is_internal(), "environment should propagate");
assert!(env.is_transaction(), "environment should propagate");
assert_eq!(env.tx_index(), 42, "environment should propagate");
assert_eq!(env.tx_size(), 888, "environment should propagate");
});
TransactionResult::Rollback(())
});
TransactionResult::Rollback(())
},
);
CurrentState::with_env(|env| {
assert!(!env.is_transaction(), "environment should not leak");
});
// Nested entering, but with a different store.
let unrelated = mkvs::OverlayTree::new(
mkvs::Tree::builder()
.with_root_type(mkvs::RootType::State)
.build(Box::new(mkvs::sync::NoopReadSyncer)),
);
let mut unrelated = MKVSStore::new(unrelated);
CurrentState::enter(&mut unrelated, || {
CurrentState::start_transaction();
CurrentState::with_store(|store| {
store.insert(b"test", b"should not touch the original root");
});
CurrentState::commit_transaction();
});
CurrentState::with_store(|store| {
store.insert(b"another", b"value 2");
});
CurrentState::commit_transaction();
}
#[test]
fn test_basic() {
let root = mkvs::OverlayTree::new(
mkvs::Tree::builder()
.with_root_type(mkvs::RootType::State)
.build(Box::new(mkvs::sync::NoopReadSyncer)),
);
let mut root = MKVSStore::new(root);
CurrentState::enter(&mut root, || {
test_store_basic();
});
let value = root.get(b"test").unwrap();
assert_eq!(value, b"value");
}
#[test]
fn test_local_fallback() {
// Initialize the local fallback store.
CurrentState::init_local_fallback();
CurrentState::init_local_fallback(); // Should be no-op.
// Test the basic store -- note, no need to enter as fallback current store is available.
test_store_basic();
CurrentState::with_store(|store| {
let value = store.get(b"test").unwrap();
assert_eq!(value, b"value");
});
// It should be possible to override the fallback by entering explicitly.
let root = mkvs::OverlayTree::new(
mkvs::Tree::builder()
.with_root_type(mkvs::RootType::State)
.build(Box::new(mkvs::sync::NoopReadSyncer)),
);
let mut root = MKVSStore::new(root);
CurrentState::enter(&mut root, || {
CurrentState::with_store(|store| {
assert!(store.get(b"test").is_none(), "store should be empty");
store.insert(b"unrelated", b"unrelated");
});
test_store_basic();
});
let value = root.get(b"test").unwrap();
assert_eq!(value, b"value");
let value = root.get(b"unrelated").unwrap();
assert_eq!(value, b"unrelated");
// Changes should not leak to fallback store.
CurrentState::with_store(|store| {
assert!(store.get(b"unrelated").is_none(), "changes should not leak");
});
}
#[test]
#[should_panic(expected = "must enter context")]
fn test_fail_not_entered() {
test_store_basic(); // Should panic due to no current store being available.
}
#[test]
#[should_panic(expected = "must not re-enter with")]
fn test_fail_reenter_with() {
CurrentState::init_local_fallback();
CurrentState::with(|_| {
CurrentState::with(|_| {
// Should panic.
});
});
}
#[test]
#[should_panic(expected = "must not re-enter with")]
fn test_fail_reenter_with_start_transaction() {
CurrentState::init_local_fallback();
CurrentState::with(|_| {
CurrentState::start_transaction(); // Should panic.
});
}
#[test]
#[should_panic(expected = "must not re-enter with")]
fn test_fail_reenter_with_commit_transaction() {
CurrentState::init_local_fallback();
CurrentState::with(|_| {
CurrentState::commit_transaction(); // Should panic.
});
}
#[test]
#[should_panic(expected = "must not re-enter with")]
fn test_fail_reenter_with_rollback_transaction() {
CurrentState::init_local_fallback();
CurrentState::with(|_| {
CurrentState::rollback_transaction(); // Should panic.
});
}
#[test]
#[should_panic(expected = "must not re-enter from with block")]
fn test_fail_reenter_with_enter() {
CurrentState::init_local_fallback();
CurrentState::with(|_| {
let unrelated = mkvs::OverlayTree::new(
mkvs::Tree::builder()
.with_root_type(mkvs::RootType::State)
.build(Box::new(mkvs::sync::NoopReadSyncer)),
);
let mut unrelated = MKVSStore::new(unrelated);
CurrentState::enter(&mut unrelated, || {
// Should panic.
});
});
}
#[test]
#[should_panic(expected = "must not re-enter from with block")]
fn test_fail_local_fallback_within_with() {
let root = mkvs::OverlayTree::new(
mkvs::Tree::builder()
.with_root_type(mkvs::RootType::State)
.build(Box::new(mkvs::sync::NoopReadSyncer)),
);
let mut root = MKVSStore::new(root);
CurrentState::enter(&mut root, || {
CurrentState::with(|_| {
CurrentState::init_local_fallback(); // Should panic.
})
});
}
#[test]
#[should_panic(expected = "must have no prior states attached to local thread")]
fn test_fail_local_fallback_within_enter() {
let root = mkvs::OverlayTree::new(
mkvs::Tree::builder()
.with_root_type(mkvs::RootType::State)
.build(Box::new(mkvs::sync::NoopReadSyncer)),
);
let mut root = MKVSStore::new(root);
CurrentState::enter(&mut root, || {
CurrentState::init_local_fallback(); // Should panic.
});
}
#[test]
#[should_panic(expected = "cannot commit on root state")]
fn test_fail_commit_transaction_must_exist() {
CurrentState::init_local_fallback();
CurrentState::commit_transaction(); // Should panic.
}
#[test]
#[should_panic(expected = "cannot rollback on root state")]
fn test_fail_rollback_transaction_must_exist() {
CurrentState::init_local_fallback();
CurrentState::rollback_transaction(); // Should panic.
}
}