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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
use std::any::Any;

use anyhow::{anyhow, Result};

use crate::{
    common::{
        crypto::{
            hash::Hash,
            signature::{
                signature_context_with_chain_separation, signature_context_with_runtime_separation,
                PublicKey, Signature, Signer,
            },
        },
        namespace::Namespace,
    },
    consensus::roothash::{Header, Message},
};

use super::OpenCommitment;

/// The signature context used to sign compute results headers with RAK.
pub const COMPUTE_RESULTS_HEADER_SIGNATURE_CONTEXT: &[u8] =
    b"oasis-core/roothash: compute results header";

/// The signature context used to sign executor worker commitments.
pub const EXECUTOR_COMMITMENT_SIGNATURE_CONTEXT: &[u8] =
    b"oasis-core/roothash: executor commitment";

fn executor_commitment_signature_context(
    runtime_id: &Namespace,
    chain_context: &String,
) -> Vec<u8> {
    let context = EXECUTOR_COMMITMENT_SIGNATURE_CONTEXT.to_vec();
    let context = signature_context_with_runtime_separation(context, runtime_id);
    signature_context_with_chain_separation(context, chain_context)
}

/// The header of a computed batch output by a runtime. This header is a
/// compressed representation (e.g., hashes instead of full content) of
/// the actual results.
///
/// # Note
///
/// This should be kept in sync with go/roothash/api/commitment/executor.go.
#[derive(Clone, Debug, Default, PartialEq, Eq, Hash, cbor::Encode, cbor::Decode)]
pub struct ComputeResultsHeader {
    /// Round number.
    pub round: u64,
    /// Hash of the previous block header this batch was computed against.
    pub previous_hash: Hash,

    /// The I/O merkle root.
    #[cbor(optional)]
    pub io_root: Option<Hash>,
    /// The root hash of the state after computing this batch.
    #[cbor(optional)]
    pub state_root: Option<Hash>,
    /// Hash of messages sent from this batch.
    #[cbor(optional)]
    pub messages_hash: Option<Hash>,

    /// The hash of processed incoming messages.
    #[cbor(optional)]
    pub in_msgs_hash: Option<Hash>,
    /// The number of processed incoming messages.
    #[cbor(optional)]
    pub in_msgs_count: u32,
}

impl ComputeResultsHeader {
    /// Returns a hash of an encoded header.
    pub fn encoded_hash(&self) -> Hash {
        Hash::digest_bytes(&cbor::to_vec(self.clone()))
    }

    /// Returns true iff the header is the parent of a child header.
    pub fn is_parent_of(&self, child: &Header) -> bool {
        if self.round != child.round + 1 {
            return false;
        }
        self.previous_hash == child.encoded_hash()
    }
}

/// The executor commitment failure reason.
#[derive(Clone, Debug, Default, PartialEq, Eq, cbor::Encode, cbor::Decode)]
#[repr(u8)]
pub enum ExecutorCommitmentFailure {
    /// Indicates that no failure has occurred.
    #[default]
    FailureNone = 0,

    /// Indicates a generic failure.
    FailureUnknown = 1,

    /// Indicates that batch processing failed due to the state being
    /// unavailable.
    FailureStateUnavailable = 2,
}

/// The header of an executor commitment.
#[derive(Clone, Debug, Default, PartialEq, Eq, cbor::Encode, cbor::Decode)]
pub struct ExecutorCommitmentHeader {
    /// The compute results header.
    pub header: ComputeResultsHeader,

    /// The executor commitment failure reason.
    #[cbor(optional)]
    pub failure: ExecutorCommitmentFailure,

    // Optional fields (may be absent for failure indication).
    #[cbor(optional, rename = "rak_sig")]
    pub rak_signature: Option<Signature>,
}

impl ExecutorCommitmentHeader {
    /// Signs the executor commitment header.
    pub fn sign(
        &self,
        signer: &impl Signer,
        runtime_id: &Namespace,
        chain_context: &String,
    ) -> Result<Signature> {
        let context = executor_commitment_signature_context(runtime_id, chain_context);
        let message = cbor::to_vec(self.clone());

        signer.sign(&context, &message)
    }

    /// Verifies the RAK signature.
    pub fn verify_rak(&self, rak: PublicKey) -> Result<()> {
        let sig = self.rak_signature.ok_or(anyhow!("missing RAK signature"))?;
        let message = cbor::to_vec(self.header.clone());

        sig.verify(&rak, COMPUTE_RESULTS_HEADER_SIGNATURE_CONTEXT, &message)
            .map_err(|_| anyhow!("RAK signature verification failed"))
    }
}

/// A commitment to results of processing a proposed runtime block.
#[derive(Clone, Debug, Default, PartialEq, Eq, cbor::Encode, cbor::Decode)]
pub struct ExecutorCommitment {
    // The public key of the node that generated this commitment.
    pub node_id: PublicKey,

    // The commitment header.
    pub header: ExecutorCommitmentHeader,

    // The commitment header signature.
    #[cbor(rename = "sig")]
    pub signature: Signature,

    // The messages emitted by the runtime.
    //
    // This field is only present in case this commitment belongs to the proposer. In case of
    // the commitment being submitted as equivocation evidence, this field should be omitted.
    #[cbor(optional)]
    pub messages: Vec<Message>,
}

impl ExecutorCommitment {
    /// Signs the executor commitment header and sets the signature on the commitment.
    pub fn sign(
        &mut self,
        signer: &impl Signer,
        runtime_id: &Namespace,
        chain_context: &String,
    ) -> Result<()> {
        let pk = signer.public();
        if self.node_id != pk {
            return Err(anyhow!(
                "node ID does not match signer (ID: {} signer: {})",
                self.node_id,
                pk,
            ));
        }

        self.signature = self.header.sign(signer, runtime_id, chain_context)?;

        Ok(())
    }

    /// Verifies that the header signature is valid.
    pub fn verify(&self, runtime_id: &Namespace, chain_context: &String) -> Result<()> {
        let context = executor_commitment_signature_context(runtime_id, chain_context);
        let message = cbor::to_vec(self.header.clone());

        self.signature
            .verify(&self.node_id, &context, &message)
            .map_err(|_| anyhow!("roothash/commitment: signature verification failed"))
    }

    pub fn validate_basic(&self) -> Result<()> {
        match self.header.failure {
            ExecutorCommitmentFailure::FailureNone => {
                // Ensure header fields are present.
                if self.header.header.io_root.is_none() {
                    return Err(anyhow!("missing IORoot"));
                }
                if self.header.header.state_root.is_none() {
                    return Err(anyhow!("missing StateRoot"));
                }
                if self.header.header.messages_hash.is_none() {
                    return Err(anyhow!("missing messages hash"));
                }
                if self.header.header.in_msgs_hash.is_none() {
                    return Err(anyhow!("missing incoming messages hash"));
                }

                // Validate any included runtime messages.
                for msg in self.messages.iter() {
                    msg.validate_basic()
                        .map_err(|err| anyhow!("bad runtime message: {:?}", err))?;
                }
            }
            ExecutorCommitmentFailure::FailureUnknown
            | ExecutorCommitmentFailure::FailureStateUnavailable => {
                // Ensure header fields are empty.
                if self.header.header.io_root.is_some() {
                    return Err(anyhow!("failure indicating body includes IORoot"));
                }
                if self.header.header.state_root.is_some() {
                    return Err(anyhow!("failure indicating commitment includes StateRoot"));
                }
                if self.header.header.messages_hash.is_some() {
                    return Err(anyhow!(
                        "failure indicating commitment includes MessagesHash"
                    ));
                }
                if self.header.header.in_msgs_hash.is_some()
                    || self.header.header.in_msgs_count != 0
                {
                    return Err(anyhow!(
                        "failure indicating commitment includes InMessagesHash/Count"
                    ));
                }
                // In case of failure indicating commitment make sure RAK signature is empty.
                if self.header.rak_signature.is_some() {
                    return Err(anyhow!("failure indicating body includes RAK signature"));
                }
                // In case of failure indicating commitment make sure messages are empty.
                if !self.messages.is_empty() {
                    return Err(anyhow!("failure indicating body includes messages"));
                }
            }
        }

        Ok(())
    }
}

impl OpenCommitment for ExecutorCommitment {
    fn mostly_equal(&self, other: &Self) -> bool {
        self.to_vote() == other.to_vote()
    }

    fn is_indicating_failure(&self) -> bool {
        self.header.failure != ExecutorCommitmentFailure::FailureNone
    }

    fn to_vote(&self) -> Hash {
        self.header.header.encoded_hash()
    }

    fn to_dd_result(&self) -> &dyn Any {
        self
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_consistent_hash() {
        // NOTE: These hashes MUST be synced with go/roothash/api/commitment/executor_test.go.
        let empty = ComputeResultsHeader::default();
        assert_eq!(
            empty.encoded_hash(),
            Hash::from("57d73e02609a00fcf4ca43cbf8c9f12867c46942d246fb2b0bce42cbdb8db844")
        );

        let populated = ComputeResultsHeader {
            round: 42,
            previous_hash: empty.encoded_hash(),
            io_root: Some(Hash::empty_hash()),
            state_root: Some(Hash::empty_hash()),
            messages_hash: Some(Hash::empty_hash()),
            in_msgs_hash: Some(Hash::empty_hash()),
            in_msgs_count: 0,
        };
        assert_eq!(
            populated.encoded_hash(),
            Hash::from("8459a9e6e3341cd2df5ada5737469a505baf92397aaa88b7100915324506d843")
        );
    }

    #[test]
    fn test_validate_basic() {
        // NOTE: These hashes MUST be synced with go/roothash/api/commitment/executor_test.go.
        let empty = ComputeResultsHeader::default();
        assert_eq!(
            empty.encoded_hash(),
            Hash::from("57d73e02609a00fcf4ca43cbf8c9f12867c46942d246fb2b0bce42cbdb8db844")
        );

        let body = ExecutorCommitment {
            header: ExecutorCommitmentHeader {
                header: ComputeResultsHeader {
                    round: 42,
                    previous_hash: empty.encoded_hash(),
                    io_root: Some(Hash::empty_hash()),
                    state_root: Some(Hash::empty_hash()),
                    messages_hash: Some(Hash::empty_hash()),
                    in_msgs_hash: Some(Hash::empty_hash()),
                    in_msgs_count: 0,
                },
                failure: ExecutorCommitmentFailure::FailureNone,
                rak_signature: None,
            },
            messages: vec![],
            node_id: PublicKey::default(),
            signature: Signature::default(),
        };

        let tcs: Vec<(&str, fn(&mut ExecutorCommitment), bool)> = vec![
            (
                "Ok",
                |ec: &mut ExecutorCommitment| {
                    ec.header.header.round -= 1;
                },
                false,
            ),
            (
                "Bad io_root",
                |ec: &mut ExecutorCommitment| ec.header.header.io_root = None,
                true,
            ),
            (
                "Bad state_root",
                |ec: &mut ExecutorCommitment| ec.header.header.state_root = None,
                true,
            ),
            (
                "Bad messages_hash",
                |ec: &mut ExecutorCommitment| ec.header.header.messages_hash = None,
                true,
            ),
            (
                "Bad Failure (existing io_root)",
                |ec: &mut ExecutorCommitment| {
                    ec.header.failure = ExecutorCommitmentFailure::FailureUnknown;
                    // ec.header.compute_results_header.io_root is set.
                    ec.header.header.state_root = None;
                    ec.header.header.messages_hash = None;
                    ec.header.header.in_msgs_hash = None;
                },
                true,
            ),
            (
                "Bad Failure (existing state_root)",
                |ec: &mut ExecutorCommitment| {
                    ec.header.failure = ExecutorCommitmentFailure::FailureUnknown;
                    ec.header.header.io_root = None;
                    // ec.header.compute_results_header.state_root is set.
                    ec.header.header.messages_hash = None;
                    ec.header.header.in_msgs_hash = None;
                },
                true,
            ),
            (
                "Bad Failure (existing messages_hash)",
                |ec: &mut ExecutorCommitment| {
                    ec.header.failure = ExecutorCommitmentFailure::FailureUnknown;
                    ec.header.header.io_root = None;
                    ec.header.header.state_root = None;
                    // ec.header.compute_results_header.messages_hash is set.
                    ec.header.header.in_msgs_hash = None;
                },
                true,
            ),
            (
                "Bad Failure (existing in_msgs_hash)",
                |ec: &mut ExecutorCommitment| {
                    ec.header.failure = ExecutorCommitmentFailure::FailureUnknown;
                    ec.header.header.io_root = None;
                    ec.header.header.state_root = None;
                    ec.header.header.messages_hash = None;
                    // ec.header.compute_results_header.in_msgs_hash is set.
                },
                true,
            ),
            (
                "Ok Failure",
                |ec: &mut ExecutorCommitment| {
                    ec.header.failure = ExecutorCommitmentFailure::FailureUnknown;
                },
                true,
            ),
        ];

        for (name, f, should_err) in tcs {
            let mut b = body.clone();
            f(&mut b);
            let res = b.validate_basic();
            assert_eq!(res.is_err(), should_err, "validate_basic({})", name)
        }
    }
}