referrerpolicy=no-referrer-when-downgrade
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
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: 2023 Snowfork <hello@snowfork.com>
use crate::{
	config::{EXTRA_DATA_SIZE, FEE_RECIPIENT_SIZE, LOGS_BLOOM_SIZE, PUBKEY_SIZE, SIGNATURE_SIZE},
	types::{
		BeaconHeader, ExecutionPayloadHeader, ForkData, SigningData, SyncAggregate, SyncCommittee,
	},
};
use byte_slice_cast::AsByteSlice;
use sp_core::H256;
use sp_std::{vec, vec::Vec};
use ssz_rs::{
	prelude::{List, Vector},
	Bitvector, Deserialize, DeserializeError, SimpleSerialize, SimpleSerializeError, Sized, U256,
};
use ssz_rs_derive::SimpleSerialize as SimpleSerializeDerive;

#[derive(Default, SimpleSerializeDerive, Clone, Debug)]
pub struct SSZBeaconBlockHeader {
	pub slot: u64,
	pub proposer_index: u64,
	pub parent_root: [u8; 32],
	pub state_root: [u8; 32],
	pub body_root: [u8; 32],
}

impl From<BeaconHeader> for SSZBeaconBlockHeader {
	fn from(beacon_header: BeaconHeader) -> Self {
		SSZBeaconBlockHeader {
			slot: beacon_header.slot,
			proposer_index: beacon_header.proposer_index,
			parent_root: beacon_header.parent_root.to_fixed_bytes(),
			state_root: beacon_header.state_root.to_fixed_bytes(),
			body_root: beacon_header.body_root.to_fixed_bytes(),
		}
	}
}

#[derive(Default, SimpleSerializeDerive, Clone)]
pub struct SSZSyncCommittee<const COMMITTEE_SIZE: usize> {
	pub pubkeys: Vector<Vector<u8, PUBKEY_SIZE>, COMMITTEE_SIZE>,
	pub aggregate_pubkey: Vector<u8, PUBKEY_SIZE>,
}

impl<const COMMITTEE_SIZE: usize> From<SyncCommittee<COMMITTEE_SIZE>>
	for SSZSyncCommittee<COMMITTEE_SIZE>
{
	fn from(sync_committee: SyncCommittee<COMMITTEE_SIZE>) -> Self {
		let mut pubkeys_vec = Vec::new();

		for pubkey in sync_committee.pubkeys.iter() {
			// The only thing that can go wrong in the conversion from vec to Vector (ssz type) is
			// that the Vector size is 0, or that the given data to create the Vector from does not
			// match the expected size N. Because these sizes are statically checked (i.e.
			// PublicKey's size is 48, and const PUBKEY_SIZE is 48, it is impossible for "try_from"
			// to return an error condition.
			let conv_pubkey = Vector::<u8, PUBKEY_SIZE>::try_from(pubkey.0.to_vec())
				.expect("checked statically; qed");

			pubkeys_vec.push(conv_pubkey);
		}

		let pubkeys = Vector::<Vector<u8, PUBKEY_SIZE>, { COMMITTEE_SIZE }>::try_from(pubkeys_vec)
			.expect("checked statically; qed");

		let aggregate_pubkey =
			Vector::<u8, PUBKEY_SIZE>::try_from(sync_committee.aggregate_pubkey.0.to_vec())
				.expect("checked statically; qed");

		SSZSyncCommittee { pubkeys, aggregate_pubkey }
	}
}

#[derive(Default, Debug, SimpleSerializeDerive, Clone)]
pub struct SSZSyncAggregate<const COMMITTEE_SIZE: usize> {
	pub sync_committee_bits: Bitvector<COMMITTEE_SIZE>,
	pub sync_committee_signature: Vector<u8, SIGNATURE_SIZE>,
}

impl<const COMMITTEE_SIZE: usize, const COMMITTEE_BITS_SIZE: usize>
	From<SyncAggregate<COMMITTEE_SIZE, COMMITTEE_BITS_SIZE>> for SSZSyncAggregate<COMMITTEE_SIZE>
{
	fn from(sync_aggregate: SyncAggregate<COMMITTEE_SIZE, COMMITTEE_BITS_SIZE>) -> Self {
		SSZSyncAggregate {
			sync_committee_bits: Bitvector::<COMMITTEE_SIZE>::deserialize(
				&sync_aggregate.sync_committee_bits,
			)
			.expect("checked statically; qed"),
			sync_committee_signature: Vector::<u8, SIGNATURE_SIZE>::try_from(
				sync_aggregate.sync_committee_signature.0.to_vec(),
			)
			.expect("checked statically; qed"),
		}
	}
}

#[derive(Default, SimpleSerializeDerive, Clone)]
pub struct SSZForkData {
	pub current_version: [u8; 4],
	pub genesis_validators_root: [u8; 32],
}

impl From<ForkData> for SSZForkData {
	fn from(fork_data: ForkData) -> Self {
		SSZForkData {
			current_version: fork_data.current_version,
			genesis_validators_root: fork_data.genesis_validators_root,
		}
	}
}

#[derive(Default, SimpleSerializeDerive, Clone)]
pub struct SSZSigningData {
	pub object_root: [u8; 32],
	pub domain: [u8; 32],
}

impl From<SigningData> for SSZSigningData {
	fn from(signing_data: SigningData) -> Self {
		SSZSigningData {
			object_root: signing_data.object_root.into(),
			domain: signing_data.domain.into(),
		}
	}
}

#[derive(Default, SimpleSerializeDerive, Clone, Debug)]
pub struct SSZExecutionPayloadHeader {
	pub parent_hash: [u8; 32],
	pub fee_recipient: Vector<u8, FEE_RECIPIENT_SIZE>,
	pub state_root: [u8; 32],
	pub receipts_root: [u8; 32],
	pub logs_bloom: Vector<u8, LOGS_BLOOM_SIZE>,
	pub prev_randao: [u8; 32],
	pub block_number: u64,
	pub gas_limit: u64,
	pub gas_used: u64,
	pub timestamp: u64,
	pub extra_data: List<u8, EXTRA_DATA_SIZE>,
	pub base_fee_per_gas: U256,
	pub block_hash: [u8; 32],
	pub transactions_root: [u8; 32],
	pub withdrawals_root: [u8; 32],
}

impl TryFrom<ExecutionPayloadHeader> for SSZExecutionPayloadHeader {
	type Error = SimpleSerializeError;

	fn try_from(payload: ExecutionPayloadHeader) -> Result<Self, Self::Error> {
		Ok(SSZExecutionPayloadHeader {
			parent_hash: payload.parent_hash.to_fixed_bytes(),
			fee_recipient: Vector::<u8, FEE_RECIPIENT_SIZE>::try_from(
				payload.fee_recipient.to_fixed_bytes().to_vec(),
			)
			.expect("checked statically; qed"),
			state_root: payload.state_root.to_fixed_bytes(),
			receipts_root: payload.receipts_root.to_fixed_bytes(),
			// Logs bloom bytes size is not constrained, so here we do need to check the try_from
			// error
			logs_bloom: Vector::<u8, LOGS_BLOOM_SIZE>::try_from(payload.logs_bloom)
				.map_err(|(_, err)| err)?,
			prev_randao: payload.prev_randao.to_fixed_bytes(),
			block_number: payload.block_number,
			gas_limit: payload.gas_limit,
			gas_used: payload.gas_used,
			timestamp: payload.timestamp,
			// Extra data bytes size is not constrained, so here we do need to check the try_from
			// error
			extra_data: List::<u8, EXTRA_DATA_SIZE>::try_from(payload.extra_data)
				.map_err(|(_, err)| err)?,
			base_fee_per_gas: U256::from_bytes_le(
				payload
					.base_fee_per_gas
					.as_byte_slice()
					.try_into()
					.expect("checked in prep; qed"),
			),
			block_hash: payload.block_hash.to_fixed_bytes(),
			transactions_root: payload.transactions_root.to_fixed_bytes(),
			withdrawals_root: payload.withdrawals_root.to_fixed_bytes(),
		})
	}
}

pub fn hash_tree_root<T: SimpleSerialize>(mut object: T) -> Result<H256, SimpleSerializeError> {
	match object.hash_tree_root() {
		Ok(node) => {
			let fixed_bytes: [u8; 32] =
				node.as_ref().try_into().expect("Node is a newtype over [u8; 32]; qed");
			Ok(fixed_bytes.into())
		},
		Err(err) => Err(err.into()),
	}
}

pub mod deneb {
	use crate::{
		config::{EXTRA_DATA_SIZE, FEE_RECIPIENT_SIZE, LOGS_BLOOM_SIZE},
		ssz::hash_tree_root,
		types::deneb::ExecutionPayloadHeader,
	};
	use byte_slice_cast::AsByteSlice;
	use sp_core::H256;
	use sp_std::{vec, vec::Vec};
	use ssz_rs::{
		prelude::{List, Vector},
		Deserialize, DeserializeError, SimpleSerializeError, Sized, U256,
	};
	use ssz_rs_derive::SimpleSerialize as SimpleSerializeDerive;

	#[derive(Default, SimpleSerializeDerive, Clone, Debug)]
	pub struct SSZExecutionPayloadHeader {
		pub parent_hash: [u8; 32],
		pub fee_recipient: Vector<u8, FEE_RECIPIENT_SIZE>,
		pub state_root: [u8; 32],
		pub receipts_root: [u8; 32],
		pub logs_bloom: Vector<u8, LOGS_BLOOM_SIZE>,
		pub prev_randao: [u8; 32],
		pub block_number: u64,
		pub gas_limit: u64,
		pub gas_used: u64,
		pub timestamp: u64,
		pub extra_data: List<u8, EXTRA_DATA_SIZE>,
		pub base_fee_per_gas: U256,
		pub block_hash: [u8; 32],
		pub transactions_root: [u8; 32],
		pub withdrawals_root: [u8; 32],
		pub blob_gas_used: u64,
		pub excess_blob_gas: u64,
	}

	impl TryFrom<ExecutionPayloadHeader> for SSZExecutionPayloadHeader {
		type Error = SimpleSerializeError;

		fn try_from(payload: ExecutionPayloadHeader) -> Result<Self, Self::Error> {
			Ok(SSZExecutionPayloadHeader {
				parent_hash: payload.parent_hash.to_fixed_bytes(),
				fee_recipient: Vector::<u8, FEE_RECIPIENT_SIZE>::try_from(
					payload.fee_recipient.to_fixed_bytes().to_vec(),
				)
				.expect("checked statically; qed"),
				state_root: payload.state_root.to_fixed_bytes(),
				receipts_root: payload.receipts_root.to_fixed_bytes(),
				// Logs bloom bytes size is not constrained, so here we do need to check the
				// try_from error
				logs_bloom: Vector::<u8, LOGS_BLOOM_SIZE>::try_from(payload.logs_bloom)
					.map_err(|(_, err)| err)?,
				prev_randao: payload.prev_randao.to_fixed_bytes(),
				block_number: payload.block_number,
				gas_limit: payload.gas_limit,
				gas_used: payload.gas_used,
				timestamp: payload.timestamp,
				// Extra data bytes size is not constrained, so here we do need to check the
				// try_from error
				extra_data: List::<u8, EXTRA_DATA_SIZE>::try_from(payload.extra_data)
					.map_err(|(_, err)| err)?,
				base_fee_per_gas: U256::from_bytes_le(
					payload
						.base_fee_per_gas
						.as_byte_slice()
						.try_into()
						.expect("checked in prep; qed"),
				),
				block_hash: payload.block_hash.to_fixed_bytes(),
				transactions_root: payload.transactions_root.to_fixed_bytes(),
				withdrawals_root: payload.withdrawals_root.to_fixed_bytes(),
				blob_gas_used: payload.blob_gas_used,
				excess_blob_gas: payload.excess_blob_gas,
			})
		}
	}

	impl ExecutionPayloadHeader {
		pub fn hash_tree_root(&self) -> Result<H256, SimpleSerializeError> {
			hash_tree_root::<SSZExecutionPayloadHeader>(self.clone().try_into()?)
		}
	}
}