• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

ergoplatform / sigma-rust / 12515632034

27 Dec 2024 11:41AM UTC coverage: 78.537% (+0.09%) from 78.445%
12515632034

Pull #797

github

web-flow
Merge 64e1cbcd6 into 1d4ef472c
Pull Request #797: Global.serialize, tree-based versioning for methods

107 of 118 new or added lines in 19 files covered. (90.68%)

6 existing lines in 1 file now uncovered.

11047 of 14066 relevant lines covered (78.54%)

3.05 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

94.12
/ergotree-ir/src/chain/context.rs
1
//! Context(blockchain) for the interpreter
2
use core::cell::Cell;
3

4
use crate::chain::ergo_box::ErgoBox;
5
use crate::{chain::context_extension::ContextExtension, ergo_tree::ErgoTreeVersion};
6
use bounded_vec::BoundedVec;
7
use ergo_chain_types::{Header, PreHeader};
8

9
/// BoundedVec type for Tx inputs, output_candidates and outputs
10
pub type TxIoVec<T> = BoundedVec<T, 1, { i16::MAX as usize }>;
11

12
/// Interpreter's context (blockchain state)
13
#[derive(Debug, Clone)]
14
pub struct Context<'ctx> {
15
    /// Current height
16
    pub height: u32,
17
    /// Box that contains the script we're evaluating (from spending transaction inputs)
18
    pub self_box: &'ctx ErgoBox,
19
    /// Spending transaction outputs
20
    pub outputs: &'ctx [ErgoBox],
21
    /// Spending transaction data inputs
22
    pub data_inputs: Option<TxIoVec<&'ctx ErgoBox>>,
23
    /// Spending transaction inputs
24
    pub inputs: TxIoVec<&'ctx ErgoBox>,
25
    /// Pre header of current block
26
    pub pre_header: PreHeader,
27
    /// Fixed number of last block headers in descending order (first header is the newest one)
28
    pub headers: [Header; 10],
29
    /// prover-defined key-value pairs, that may be used inside a script
30
    pub extension: ContextExtension,
31
    /// ergo tree version
32
    pub tree_version: Cell<ErgoTreeVersion>,
33
}
34

35
impl<'ctx> Context<'ctx> {
36
    /// Return a new Context with given context extension
37
    pub fn with_extension(self, ext: ContextExtension) -> Self {
1✔
38
        Context {
39
            extension: ext,
40
            ..self
41
        }
42
    }
43
    /// Activated script version corresponds to block version - 1
NEW
44
    pub fn activated_script_version(&self) -> ErgoTreeVersion {
×
NEW
45
        ErgoTreeVersion::from(self.pre_header.version.saturating_sub(1))
×
46
    }
47
    /// Version of ergotree being evaluated under context
48
    pub fn tree_version(&self) -> ErgoTreeVersion {
1✔
49
        self.tree_version.get()
1✔
50
    }
51
}
52

53
#[cfg(feature = "arbitrary")]
54
#[allow(clippy::unwrap_used)]
55
mod arbitrary {
56

57
    use super::*;
58
    use proptest::{collection::vec, option::of, prelude::*};
59

60
    impl Arbitrary for Context<'static> {
61
        type Parameters = ();
62

63
        fn arbitrary_with(_args: Self::Parameters) -> Self::Strategy {
3✔
64
            (
65
                0..i32::MAX as u32,
66
                any::<ErgoBox>(),
5✔
67
                vec(any::<ErgoBox>(), 1..3),
9✔
68
                vec(any::<ErgoBox>(), 1..3),
8✔
69
                of(vec(any::<ErgoBox>(), 1..3)),
9✔
70
                any::<PreHeader>(),
11✔
71
                any::<ContextExtension>(),
8✔
72
                any::<[Header; 10]>(),
5✔
73
            )
74
                .prop_map(
75
                    |(
6✔
76
                        height,
4✔
77
                        self_box,
5✔
78
                        outputs,
3✔
79
                        inputs,
7✔
80
                        data_inputs,
3✔
81
                        pre_header,
7✔
82
                        extension,
3✔
83
                        headers,
7✔
84
                    )| {
85
                        // Leak variables. Since this is only used for testing this is acceptable and avoids introducing a new type (ContextOwned)
86
                        Self {
7✔
87
                            height,
88
                            self_box: Box::leak(Box::new(self_box)),
3✔
89
                            outputs: Vec::leak(outputs),
7✔
90
                            data_inputs: data_inputs.map(|v| {
8✔
91
                                v.into_iter()
5✔
92
                                    .map(|i| &*Box::leak(Box::new(i)))
10✔
93
                                    .collect::<Vec<_>>()
94
                                    .try_into()
95
                                    .unwrap()
96
                            }),
97
                            inputs: inputs
10✔
98
                                .into_iter()
99
                                .map(|i| &*Box::leak(Box::new(i)))
10✔
100
                                .collect::<Vec<_>>()
101
                                .try_into()
102
                                .unwrap(),
103
                            pre_header,
7✔
104
                            extension,
3✔
105
                            headers,
7✔
106
                            tree_version: Default::default(),
3✔
107
                        }
108
                    },
109
                )
110
                .boxed()
111
        }
112

113
        type Strategy = BoxedStrategy<Self>;
114
    }
115
}
116

117
#[cfg(test)]
118
mod tests {}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc