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
//! Runtime transaction batch dispatcher.
use std::sync::{atomic::AtomicBool, Arc};

use super::{context::Context, tags::Tags, types::TxnBatch};
use crate::{
    common::crypto::hash::Hash,
    consensus::roothash,
    types::{CheckTxResult, Error as RuntimeError},
};

/// Runtime transaction dispatcher trait.
///
/// It defines the interface used by the runtime call dispatcher
/// to process transactions.
pub trait Dispatcher: Send + Sync {
    /// Whether dispatch is supported by this dispatcher.
    fn is_supported(&self) -> bool {
        true
    }

    /// Execute the transactions in the given batch.
    ///
    /// # Consensus Layer State Integrity
    ///
    /// Before this method is invoked, consensus layer state integrity verification is performed.
    fn execute_batch(
        &self,
        ctx: Context,
        batch: &TxnBatch,
        in_msgs: &[roothash::IncomingMessage],
    ) -> Result<ExecuteBatchResult, RuntimeError>;

    /// Schedule and execute transactions in the given batch.
    ///
    /// The passed batch is an initial batch. In case the runtime needs additional items it should
    /// request them from the host.
    ///
    /// # Consensus Layer State Integrity
    ///
    /// Before this method is invoked, consensus layer state integrity verification is performed.
    fn schedule_and_execute_batch(
        &self,
        _ctx: Context,
        _initial_batch: &mut TxnBatch,
        _in_msgs: &[roothash::IncomingMessage],
    ) -> Result<ExecuteBatchResult, RuntimeError> {
        Err(RuntimeError::new(
            "rhp/dispatcher",
            3,
            "scheduling not supported",
        ))
    }

    /// Check the transactions in the given batch for validity.
    ///
    /// # Consensus Layer State Integrity
    ///
    /// No consensus layer state integrity verification is performed for queries by default. The
    /// runtime dispatcher implementation should perform integrity verification if needed on a
    /// query-by-query basis.
    fn check_batch(
        &self,
        ctx: Context,
        batch: &TxnBatch,
    ) -> Result<Vec<CheckTxResult>, RuntimeError>;

    /// Invoke the finalizer (if any).
    fn finalize(&self, _new_storage_root: Hash) {
        // Default implementation does nothing.
    }

    /// Configure abort batch flag.
    fn set_abort_batch_flag(&mut self, _abort_batch: Arc<AtomicBool>) {
        // Default implementation does nothing.
    }

    /// Process a query.
    ///
    /// # Consensus Layer State Integrity
    ///
    /// No consensus layer state integrity verification is performed for queries by default. The
    /// runtime dispatcher implementation should perform integrity verification if needed on a
    /// query-by-query basis.
    fn query(&self, _ctx: Context, _method: &str, _args: Vec<u8>) -> Result<Vec<u8>, RuntimeError> {
        // Default implementation returns an error.
        Err(RuntimeError::new(
            "rhp/dispatcher",
            2,
            "query not supported",
        ))
    }
}

impl<T: Dispatcher + ?Sized> Dispatcher for Box<T> {
    fn execute_batch(
        &self,
        ctx: Context,
        batch: &TxnBatch,
        in_msgs: &[roothash::IncomingMessage],
    ) -> Result<ExecuteBatchResult, RuntimeError> {
        T::execute_batch(&**self, ctx, batch, in_msgs)
    }

    fn schedule_and_execute_batch(
        &self,
        ctx: Context,
        initial_batch: &mut TxnBatch,
        in_msgs: &[roothash::IncomingMessage],
    ) -> Result<ExecuteBatchResult, RuntimeError> {
        T::schedule_and_execute_batch(&**self, ctx, initial_batch, in_msgs)
    }

    fn check_batch(
        &self,
        ctx: Context,
        batch: &TxnBatch,
    ) -> Result<Vec<CheckTxResult>, RuntimeError> {
        T::check_batch(&**self, ctx, batch)
    }

    fn finalize(&self, new_storage_root: Hash) {
        T::finalize(&**self, new_storage_root)
    }

    fn set_abort_batch_flag(&mut self, abort_batch: Arc<AtomicBool>) {
        T::set_abort_batch_flag(&mut **self, abort_batch)
    }

    fn query(&self, ctx: Context, method: &str, args: Vec<u8>) -> Result<Vec<u8>, RuntimeError> {
        T::query(&**self, ctx, method, args)
    }
}

impl<T: Dispatcher + ?Sized> Dispatcher for Arc<T> {
    fn execute_batch(
        &self,
        ctx: Context,
        batch: &TxnBatch,
        in_msgs: &[roothash::IncomingMessage],
    ) -> Result<ExecuteBatchResult, RuntimeError> {
        T::execute_batch(&**self, ctx, batch, in_msgs)
    }

    fn schedule_and_execute_batch(
        &self,
        ctx: Context,
        initial_batch: &mut TxnBatch,
        in_msgs: &[roothash::IncomingMessage],
    ) -> Result<ExecuteBatchResult, RuntimeError> {
        T::schedule_and_execute_batch(&**self, ctx, initial_batch, in_msgs)
    }

    fn check_batch(
        &self,
        ctx: Context,
        batch: &TxnBatch,
    ) -> Result<Vec<CheckTxResult>, RuntimeError> {
        T::check_batch(&**self, ctx, batch)
    }

    fn finalize(&self, new_storage_root: Hash) {
        T::finalize(&**self, new_storage_root)
    }

    fn set_abort_batch_flag(&mut self, _abort_batch: Arc<AtomicBool>) {
        unimplemented!()
    }

    fn query(&self, ctx: Context, method: &str, args: Vec<u8>) -> Result<Vec<u8>, RuntimeError> {
        T::query(&**self, ctx, method, args)
    }
}

/// Result of processing an ExecuteTx.
pub struct ExecuteTxResult {
    /// Transaction output.
    pub output: Vec<u8>,
    /// Emitted tags.
    pub tags: Tags,
}

/// Result of processing a batch of ExecuteTx.
pub struct ExecuteBatchResult {
    /// Per-transaction execution results.
    pub results: Vec<ExecuteTxResult>,
    /// Emitted runtime messages.
    pub messages: Vec<roothash::Message>,
    /// Number of processed incoming messages.
    pub in_msgs_count: usize,
    /// Block emitted tags (not emitted by a specific transaction).
    pub block_tags: Tags,
    /// Hashes of transactions to reject.
    ///
    /// Note that these are only taken into account in schedule execution mode.
    pub tx_reject_hashes: Vec<Hash>,
}

/// No-op dispatcher.
///
/// This is mainly used by the runtime dispatcher as a fallback in case
/// the runtime's initializer doesn't produce its own dispatcher object.
#[derive(Default)]
pub struct NoopDispatcher;

impl Dispatcher for NoopDispatcher {
    fn is_supported(&self) -> bool {
        false
    }

    fn execute_batch(
        &self,
        _ctx: Context,
        _batch: &TxnBatch,
        in_msgs: &[roothash::IncomingMessage],
    ) -> Result<ExecuteBatchResult, RuntimeError> {
        Ok(ExecuteBatchResult {
            results: Vec::new(),
            messages: Vec::new(),
            block_tags: Tags::new(),
            in_msgs_count: in_msgs.len(),
            tx_reject_hashes: Vec::new(),
        })
    }

    fn schedule_and_execute_batch(
        &self,
        _ctx: Context,
        _initial_batch: &mut TxnBatch,
        in_msgs: &[roothash::IncomingMessage],
    ) -> Result<ExecuteBatchResult, RuntimeError> {
        Ok(ExecuteBatchResult {
            results: Vec::new(),
            messages: Vec::new(),
            block_tags: Tags::new(),
            in_msgs_count: in_msgs.len(),
            tx_reject_hashes: Vec::new(),
        })
    }

    fn check_batch(
        &self,
        _ctx: Context,
        _batch: &TxnBatch,
    ) -> Result<Vec<CheckTxResult>, RuntimeError> {
        Ok(Vec::new())
    }
}