time_primitives/
task.rs

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
use crate::{BatchId, GmpEvent, TssSignature};
use core::ops::Range;
use polkadot_sdk::{sp_core::ConstU32, sp_runtime::BoundedVec};
use scale_codec::{Decode, DecodeWithMemTracking, Encode};
use scale_info::{prelude::vec::Vec, TypeInfo};
#[cfg(feature = "std")]
use serde::{Deserialize, Serialize};

pub type TaskId = u64;

#[cfg_attr(feature = "std", derive(Serialize, Deserialize))]
#[derive(Debug, Clone, Decode, DecodeWithMemTracking, Encode, TypeInfo, PartialEq)]
pub enum Task {
	ReadGatewayEvents { blocks: Range<u64> },
	SubmitGatewayMessage { batch_id: BatchId },
}

#[cfg(feature = "std")]
impl std::fmt::Display for Task {
	fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
		match self {
			Self::ReadGatewayEvents { blocks } => {
				let start = blocks.start;
				let end = blocks.end;
				write!(f, "ReadGatewayEvents({start}..{end})")
			},
			Self::SubmitGatewayMessage { batch_id } => {
				write!(f, "SubmitGatewayMessage({batch_id})")
			},
		}
	}
}

impl Task {
	pub fn get_input_length(&self) -> u32 {
		self.encoded_size() as _
	}

	pub fn needs_registration(&self) -> bool {
		!matches!(self, Self::ReadGatewayEvents { .. })
	}

	pub fn needs_signer(&self) -> bool {
		matches!(self, Self::SubmitGatewayMessage { .. })
	}

	pub fn start_block(&self) -> u64 {
		if let Self::ReadGatewayEvents { blocks } = self {
			blocks.end
		} else {
			0
		}
	}
}

pub fn encode_gmp_events(task_id: TaskId, events: &[GmpEvent]) -> Vec<u8> {
	(task_id, events).encode()
}

pub const MAX_GMP_EVENTS: u32 = 10_000;
pub const MAX_ERROR_LEN: u32 = 10_000;

/// Bounded vec alias for GMP events submitted in results
#[cfg_attr(feature = "std", derive(Serialize, Deserialize))]
#[derive(Encode, Decode, DecodeWithMemTracking, TypeInfo, PartialEq, Eq, Clone, Debug)]
pub struct GmpEvents(pub BoundedVec<GmpEvent, ConstU32<MAX_GMP_EVENTS>>);
/// Bounded vec alias for SubmitGatewayMessage error
#[cfg_attr(feature = "std", derive(Serialize, Deserialize))]
#[derive(Encode, Decode, DecodeWithMemTracking, TypeInfo, PartialEq, Eq, Clone, Debug)]
pub struct ErrorMsg(pub BoundedVec<u8, ConstU32<MAX_ERROR_LEN>>);

#[cfg_attr(feature = "std", derive(Serialize, Deserialize))]
#[derive(Debug, Clone, Decode, DecodeWithMemTracking, Encode, TypeInfo, PartialEq)]
pub enum TaskResult {
	ReadGatewayEvents {
		events: GmpEvents,
		#[cfg_attr(feature = "std", serde(with = "crate::shard::serde_tss_signature"))]
		signature: TssSignature,
	},
	SubmitGatewayMessage {
		error: ErrorMsg,
	},
}