Skip to content

Commit

Permalink
Merge pull request #1742 from oasisprotocol/jberci/feature/callkey
Browse files Browse the repository at this point in the history
runtime-sdk/modules/core: Add internal calls for call data key and epoch
  • Loading branch information
jberci authored May 8, 2024
2 parents 718ff5c + cd503ce commit ab2358a
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 4 deletions.
36 changes: 32 additions & 4 deletions runtime-sdk/src/modules/core/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -420,6 +420,11 @@ pub trait Config: 'static {
///
/// Note that execution of such transactions is allowed to access confidential state.
const ALLOW_INTERACTIVE_READ_ONLY_TRANSACTIONS: bool = false;

/// The gas cost of the internal call to retrieve the current calldata public key.
const GAS_COST_CALL_CALLDATA_PUBLIC_KEY: u64 = 20;
/// The gas cost of the internal call to retrieve the current epoch.
const GAS_COST_CALL_CURRENT_EPOCH: u64 = 10;
}

pub struct Module<Cfg: Config> {
Expand Down Expand Up @@ -833,11 +838,8 @@ impl<Cfg: Config> Module<Cfg> {
<C::Runtime as Runtime>::Modules::check_invariants(ctx)
}

/// Retrieve the public key for encrypting call data.
#[handler(query = "core.CallDataPublicKey")]
fn query_calldata_public_key<C: Context>(
fn calldata_public_key_common<C: Context>(
ctx: &C,
_args: (),
) -> Result<types::CallDataPublicKeyQueryResponse, Error> {
let key_manager = ctx
.key_manager()
Expand All @@ -855,6 +857,32 @@ impl<Cfg: Config> Module<Cfg> {
Ok(types::CallDataPublicKeyQueryResponse { public_key, epoch })
}

/// Retrieve the public key for encrypting call data.
#[handler(query = "core.CallDataPublicKey")]
fn query_calldata_public_key<C: Context>(
ctx: &C,
_args: (),
) -> Result<types::CallDataPublicKeyQueryResponse, Error> {
Self::calldata_public_key_common(ctx)
}

/// Retrieve the public key for encrypting call data (internally exposed call).
#[handler(call = "core.CallDataPublicKey", internal)]
fn internal_calldata_public_key<C: Context>(
ctx: &C,
_args: (),
) -> Result<types::CallDataPublicKeyQueryResponse, Error> {
<C::Runtime as Runtime>::Core::use_tx_gas(Cfg::GAS_COST_CALL_CALLDATA_PUBLIC_KEY)?;
Self::calldata_public_key_common(ctx)
}

/// Retrieve the current epoch.
#[handler(call = "core.CurrentEpoch", internal)]
fn internal_current_epoch<C: Context>(ctx: &C, _args: ()) -> Result<u64, Error> {
<C::Runtime as Runtime>::Core::use_tx_gas(Cfg::GAS_COST_CALL_CURRENT_EPOCH)?;
Ok(ctx.epoch())
}

/// Query the minimum gas price.
#[handler(query = "core.MinGasPrice")]
fn query_min_gas_price<C: Context>(
Expand Down
2 changes: 2 additions & 0 deletions runtime-sdk/src/modules/core/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1134,6 +1134,8 @@ fn test_module_info() {
MethodHandlerInfo { kind: MethodHandlerKind::Query, name: "core.EstimateGas".to_string() },
MethodHandlerInfo { kind: MethodHandlerKind::Query, name: "core.CheckInvariants".to_string() },
MethodHandlerInfo { kind: MethodHandlerKind::Query, name: "core.CallDataPublicKey".to_string() },
MethodHandlerInfo { kind: MethodHandlerKind::Call, name: "core.CallDataPublicKey".to_string() },
MethodHandlerInfo { kind: MethodHandlerKind::Call, name: "core.CurrentEpoch".to_string() },
MethodHandlerInfo { kind: MethodHandlerKind::Query, name: "core.MinGasPrice".to_string() },
MethodHandlerInfo { kind: MethodHandlerKind::Query, name: "core.RuntimeInfo".to_string() },
MethodHandlerInfo { kind: MethodHandlerKind::Query, name: "core.ExecuteReadOnlyTx".to_string() },
Expand Down

0 comments on commit ab2358a

Please sign in to comment.