wapod_types/
bench_app.rs

1//! The types used by the benchmark app.
2
3use scale::{Decode, Encode, MaxEncodedLen};
4use scale_info::TypeInfo;
5use serde::{Deserialize, Serialize};
6
7use crate::{
8    crypto::{
9        verify::{verify_app_data, Verifiable},
10        CryptoProvider, Signature,
11    },
12    metrics::MetricsToken,
13    primitives::{Address, WorkerPubkey},
14};
15
16/// The json response of the benchmark app.
17
18#[derive(
19    Default,
20    Debug,
21    Serialize,
22    Deserialize,
23    Decode,
24    Encode,
25    TypeInfo,
26    MaxEncodedLen,
27    Clone,
28    PartialEq,
29    Eq,
30)]
31pub struct BenchScore {
32    /// The score.
33    pub gas_per_second: u64,
34    /// The amount of gas consumed to calculate the score.
35    pub gas_consumed: u64,
36    /// The timestamp (seconds since UNIX epoch) when the score was recorded.
37    pub timestamp_secs: u64,
38    /// The metrics token for the worker.
39    pub metrics_token: MetricsToken,
40}
41
42/// A message that the benchmark app can emit.
43#[derive(Decode, Encode, TypeInfo, MaxEncodedLen, Debug, Clone, PartialEq, Eq)]
44pub enum SigningMessage {
45    /// A benchmark score. This can be submitted to the chain as the worker's initial score.
46    BenchScore(BenchScore),
47}
48
49/// A signed message.
50#[derive(Decode, Encode, TypeInfo, MaxEncodedLen, Debug, Clone, PartialEq, Eq)]
51pub struct SignedMessage {
52    /// The message.
53    pub message: SigningMessage,
54    /// The signature of the message.
55    pub signature: Signature,
56    /// The public key of the worker that signed the message.
57    pub worker_pubkey: WorkerPubkey,
58    /// The address of the app that the message is intended for.
59    pub app_address: Address,
60}
61
62impl Verifiable for SignedMessage {
63    fn verify<Crypto: CryptoProvider>(&self) -> bool {
64        let encoded_message = self.message.encode();
65        verify_app_data::<Crypto>(
66            &self.app_address,
67            &encoded_message,
68            &self.signature,
69            &self.worker_pubkey,
70        )
71    }
72}