Skip to main content

jam_stress_service/
lib.rs

1#![cfg_attr(any(target_arch = "riscv32", target_arch = "riscv64"), no_std)]
2
3extern crate alloc;
4use accumulate::set_storage;
5use jam_pvm_common::*;
6use jam_types::*;
7use refine::gas;
8
9#[allow(dead_code)]
10struct Stress;
11declare_service!(Stress);
12
13const OUTPUT_SIZE: usize = 48000;
14
15fn hash_raw(data: &[u8]) -> [u8; 32] {
16	let h = blake2b_simd::Params::new().hash_length(32).hash(data);
17	h.as_bytes().try_into().expect("Hash length set to 32")
18}
19
20impl Service for Stress {
21	fn refine(
22		_id: ServiceId,
23		payload: WorkPayload,
24		_package_hash: WorkPackageHash,
25		_context: RefineContext,
26		_authorizer: CodeHash,
27	) -> WorkOutput {
28		let mut hash = hash_raw(&payload);
29		let mut result = Vec::with_capacity(OUTPUT_SIZE);
30		for _ in 0..max_exports() {
31			let mut segment = Segment::default();
32			for i in 0..segment.len() / 32 {
33				segment[i * 32..(i + 1) * 32].copy_from_slice(&hash);
34			}
35			let _ = refine::export(&segment);
36			hash = hash_raw(&hash);
37		}
38		while gas() > 200_000 {
39			hash = hash_raw(&hash);
40			if result.len() < OUTPUT_SIZE {
41				result.extend_from_slice(hash.as_slice());
42			}
43		}
44		result.into()
45	}
46
47	fn accumulate(_slot: Slot, _id: ServiceId, items: Vec<AccumulateItem>) -> Option<Hash> {
48		let mut hash = items[0].payload.0;
49		while gas() > 100_000 {
50			set_storage(&hash, &hash).expect("failed to set storage");
51			hash = hash_raw(&hash);
52		}
53		None
54	}
55
56	fn on_transfer(_slot: Slot, _id: ServiceId, _items: Vec<TransferRecord>) {}
57}