Skip to content

Commit

Permalink
Merge pull request #1460 from oasisprotocol/kostko/fix/query-store-ro…
Browse files Browse the repository at this point in the history
…llback

runtime-sdk: Always rollback any changes to storage from queries
  • Loading branch information
kostko authored Aug 22, 2023
2 parents c450763 + 8a8d511 commit 1f8edd8
Showing 1 changed file with 17 additions and 10 deletions.
27 changes: 17 additions & 10 deletions runtime-sdk/src/dispatcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -519,18 +519,25 @@ impl<R: Runtime> Dispatcher<R> {
let args = cbor::from_slice(&args)
.map_err(|err| modules::core::Error::InvalidArgument(err.into()))?;

// Catch any panics that occur during query dispatch.
std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
// Perform state migrations if required.
R::migrate(ctx);
CurrentStore::with_transaction(|| {
// Catch any panics that occur during query dispatch.
let result = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
// Perform state migrations if required.
R::migrate(ctx);

if !R::is_allowed_query(method) || !ctx.is_allowed_query::<R>(method) {
return Err(modules::core::Error::Forbidden.into());
}

if !R::is_allowed_query(method) || !ctx.is_allowed_query::<R>(method) {
return Err(modules::core::Error::Forbidden.into());
}
R::Modules::dispatch_query(ctx, method, args)
.ok_or_else(|| modules::core::Error::InvalidMethod(method.into()))?
}));

R::Modules::dispatch_query(ctx, method, args)
.ok_or_else(|| modules::core::Error::InvalidMethod(method.into()))?
}))
// Always rollback any changes to storage. Note that this is usually a no-op because
// Oasis Core would rollback any storage changes related to queries, but this makes it
// explicit to ensure this remains the case regardless of upstream changes.
TransactionResult::Rollback(result)
})
.map_err(|err| -> RuntimeError { Error::QueryAborted(format!("{err:?}")).into() })?
.map(cbor::to_vec)
}
Expand Down

0 comments on commit 1f8edd8

Please sign in to comment.