jade_testing/
chain.rs

1//! Chain environment
2
3use anyhow::{Result, anyhow};
4use service::{
5    EntropyBuffer, OpaqueHash, ServiceId,
6    service::{RefineContext, ServiceAccount},
7};
8use std::collections::BTreeMap;
9
10/// Head of a block
11#[derive(Clone, Default)]
12pub struct Head {
13    /// Hash of the block
14    pub hash: OpaqueHash,
15
16    /// Slot of the block
17    pub slot: u32,
18}
19
20/// Chain environment
21#[derive(Clone, Default)]
22pub struct Chain {
23    /// Best block
24    pub best: Head,
25
26    /// Entropy buffer
27    pub entropy: EntropyBuffer,
28
29    /// Finalized block
30    pub finalized: Head,
31
32    /// Service accounts
33    pub accounts: BTreeMap<u32, ServiceAccount>,
34}
35
36impl Chain {
37    /// Find a service code
38    pub fn service(&self, service: ServiceId) -> Result<OpaqueHash> {
39        self.accounts
40            .get(&service)
41            .map(|account| account.info.code)
42            .ok_or_else(|| anyhow!("Service not found"))
43    }
44
45    /// Get the refine context
46    ///
47    /// TODO: support prerequisites
48    pub fn refine_context(&self) -> RefineContext {
49        RefineContext {
50            anchor: self.best.hash,
51            state_root: Default::default(),
52            beefy_root: Default::default(),
53            lookup_anchor: self.finalized.hash,
54            lookup_anchor_slot: self.finalized.slot,
55            prerequisites: Default::default(),
56        }
57    }
58}