SafeVariables and commit/revert logic
How we simulate Solidity commit/revert logic in precompiled contracts.
MyClass::updateValueAndThrow(const uint64_t key, const uint64_t value) {
// Suppose the original value of myMap[key] is 5
this->myMap[key] = value; // e.g. 10
throw std::runtime_error("Error");
// myMap[key] remains 10, but it shouldn't because it threw
}MyClass::updateValueAndThrow(const uint64_t key, const uint64_t value) {
const uint64_t oldValue = this->myMap[key]; // Store the old value of myMap[key] first
try {
this->myMap[key] = value; // myMap[key] is now 10
throw std::runtime_error("Error"); // Something went wrong
} catch (std::exception& e) {
this->myMap[key] = oldValue; // Assign the original value back, myMap[key] is now 5 again
}
}Types of SafeVariables
Caveats
Last updated