Skip to content

Commit

Permalink
runtime-sdk: Add support for reporting sender metadata
Browse files Browse the repository at this point in the history
  • Loading branch information
kostko committed Oct 4, 2022
1 parent c58dbde commit ab8445c
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 4 deletions.
16 changes: 12 additions & 4 deletions runtime-sdk/src/dispatcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ use crate::{
modules::core::API as _,
runtime::Runtime,
schedule_control::ScheduleControlHost,
sender::SenderMeta,
storage::{self, NestedStore, Prefix},
types,
types::transaction::{AuthProof, Transaction},
Expand Down Expand Up @@ -77,6 +78,8 @@ pub struct DispatchResult {
pub tags: Tags,
/// Transaction priority.
pub priority: u64,
/// Transaction sender metadata.
pub sender_metadata: SenderMeta,
/// Call format metadata.
pub call_format_metadata: callformat::Metadata,
}
Expand All @@ -91,6 +94,7 @@ impl DispatchResult {
result,
tags,
priority: 0,
sender_metadata: Default::default(),
call_format_metadata,
}
}
Expand Down Expand Up @@ -258,8 +262,10 @@ impl<R: Runtime> Dispatcher<R> {
);
}

// Load priority, weights.
// Load priority.
let priority = R::Core::take_priority(&mut ctx);
// Load sender metadata.
let sender_metadata = R::Core::take_sender_meta(&mut ctx);

if ctx.is_check_only() {
// Rollback state during checks.
Expand All @@ -270,6 +276,7 @@ impl<R: Runtime> Dispatcher<R> {
result,
tags: Vec::new(),
priority,
sender_metadata,
call_format_metadata,
},
Vec::new(),
Expand All @@ -282,6 +289,7 @@ impl<R: Runtime> Dispatcher<R> {
result,
tags: etags.into_tags(),
priority,
sender_metadata,
call_format_metadata,
},
messages,
Expand Down Expand Up @@ -338,9 +346,9 @@ impl<R: Runtime> Dispatcher<R> {
error: Default::default(),
meta: Some(CheckTxMetadata {
priority: dispatch.priority,
sender: vec![], // TODO: Support indicating senders.
sender_seq: 0, // TODO: Support indicating senders.
sender_state_seq: 0, // TODO: Support indicating senders.
sender: dispatch.sender_metadata.id(),
sender_seq: dispatch.sender_metadata.tx_nonce,
sender_state_seq: dispatch.sender_metadata.state_nonce,
}),
}),

Expand Down
1 change: 1 addition & 0 deletions runtime-sdk/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ pub mod module;
pub mod modules;
pub mod runtime;
pub mod schedule_control;
pub mod sender;
pub mod storage;
pub mod testing;
pub mod types;
Expand Down
18 changes: 18 additions & 0 deletions runtime-sdk/src/modules/core/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ use crate::{
self, CallResult, InvariantHandler as _, MethodHandler as _, Module as _,
ModuleInfoHandler as _,
},
sender::SenderMeta,
types::{
token,
transaction::{self, AddressSpec, AuthProof, Call, CallFormat, UnverifiedTransaction},
Expand Down Expand Up @@ -266,6 +267,12 @@ pub trait API {
/// Takes and returns the stored transaction priority.
fn take_priority<C: Context>(ctx: &mut C) -> u64;

/// Set transaction sender metadata.
fn set_sender_meta<C: Context>(ctx: &mut C, meta: SenderMeta);

/// Takes and returns the stored transaction sender metadata.
fn take_sender_meta<C: Context>(ctx: &mut C) -> SenderMeta;

/// Returns the configured max iterations in the binary search for the estimate
/// gas.
fn estimate_gas_search_max_iters<C: Context>(ctx: &C) -> u64;
Expand Down Expand Up @@ -342,6 +349,7 @@ pub struct Module<Cfg: Config> {

const CONTEXT_KEY_GAS_USED: &str = "core.GasUsed";
const CONTEXT_KEY_PRIORITY: &str = "core.Priority";
const CONTEXT_KEY_SENDER_META: &str = "core.SenderMeta";

impl<Cfg: Config> Module<Cfg> {
/// Initialize state from genesis.
Expand Down Expand Up @@ -447,6 +455,16 @@ impl<Cfg: Config> API for Module<Cfg> {
.unwrap_or_default()
}

fn set_sender_meta<C: Context>(ctx: &mut C, meta: SenderMeta) {
ctx.value::<SenderMeta>(CONTEXT_KEY_SENDER_META).set(meta);
}

fn take_sender_meta<C: Context>(ctx: &mut C) -> SenderMeta {
ctx.value::<SenderMeta>(CONTEXT_KEY_SENDER_META)
.take()
.unwrap_or_default()
}

fn estimate_gas_search_max_iters<C: Context>(ctx: &C) -> u64 {
ctx.local_config(MODULE_NAME)
.as_ref()
Expand Down
20 changes: 20 additions & 0 deletions runtime-sdk/src/modules/core/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use crate::{
module::{self, Module as _, TransactionHandler as _},
runtime::Runtime,
sdk_derive,
sender::SenderMeta,
testing::{configmap, keys, mock},
types::{token, transaction, transaction::CallerAddress},
};
Expand Down Expand Up @@ -749,6 +750,25 @@ fn test_add_priority_overflow() {
);
}

#[test]
fn test_set_sender_meta() {
let mut mock = mock::Mock::default();
let mut ctx = mock.create_ctx();

let sender_meta = SenderMeta {
address: keys::alice::address(),
tx_nonce: 42,
state_nonce: 43,
};
Core::set_sender_meta(&mut ctx, sender_meta.clone());

let taken_sender_meta = Core::take_sender_meta(&mut ctx);
assert_eq!(
taken_sender_meta, sender_meta,
"setting sender metadata should work"
);
}

#[test]
fn test_min_gas_price() {
let mut mock = mock::Mock::default();
Expand Down
26 changes: 26 additions & 0 deletions runtime-sdk/src/sender.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
//! Transaction sender metadata.
use crate::types::address::Address;

/// Transaction sender metadata.
#[derive(Clone, Debug, Eq, PartialEq, Default)]
pub struct SenderMeta {
/// Sender address.
pub address: Address,
/// Sender nonce contained in the transaction.
pub tx_nonce: u64,
/// Sender nonce contained in runtime state.
pub state_nonce: u64,
}

impl SenderMeta {
/// Unique identifier of the sender, currently derived from the sender address.
pub fn id(&self) -> Vec<u8> {
if self.address == Default::default() {
// Use an empty value for the default address as that signals to the host that the
// sender should be ignored.
vec![]
} else {
self.address.into_bytes().to_vec()
}
}
}

0 comments on commit ab8445c

Please sign in to comment.