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
// Copyright 2019-2022 Parity Technologies (UK) Ltd.
// This file is dual-licensed as Apache-2.0 or GPL-3.0.
// see LICENSE for license details.

use crate::{
    dynamic::DecodedValueThunk,
    metadata::DecodeWithMetadata,
};
use std::borrow::Cow;

/// This represents a constant address. Anything implementing this trait
/// can be used to fetch constants.
pub trait ConstantAddress {
    /// The target type of the value that lives at this address.
    type Target: DecodeWithMetadata;

    /// The name of the pallet that the constant lives under.
    fn pallet_name(&self) -> &str;

    /// The name of the constant in a given pallet.
    fn constant_name(&self) -> &str;

    /// An optional hash which, if present, will be checked against
    /// the node metadata to confirm that the return type matches what
    /// we are expecting.
    fn validation_hash(&self) -> Option<[u8; 32]> {
        None
    }
}

/// This represents a statically generated constant lookup address.
pub struct StaticConstantAddress<ReturnTy> {
    pallet_name: &'static str,
    constant_name: &'static str,
    constant_hash: Option<[u8; 32]>,
    _marker: std::marker::PhantomData<ReturnTy>,
}

impl<ReturnTy> StaticConstantAddress<ReturnTy> {
    /// Create a new [`StaticConstantAddress`] that will be validated
    /// against node metadata using the hash given.
    pub fn new(
        pallet_name: &'static str,
        constant_name: &'static str,
        hash: [u8; 32],
    ) -> Self {
        Self {
            pallet_name,
            constant_name,
            constant_hash: Some(hash),
            _marker: std::marker::PhantomData,
        }
    }

    /// Do not validate this constant prior to accessing it.
    pub fn unvalidated(self) -> Self {
        Self {
            pallet_name: self.pallet_name,
            constant_name: self.constant_name,
            constant_hash: None,
            _marker: self._marker,
        }
    }
}

impl<ReturnTy: DecodeWithMetadata> ConstantAddress for StaticConstantAddress<ReturnTy> {
    type Target = ReturnTy;

    fn pallet_name(&self) -> &str {
        self.pallet_name
    }

    fn constant_name(&self) -> &str {
        self.constant_name
    }

    fn validation_hash(&self) -> Option<[u8; 32]> {
        self.constant_hash
    }
}

/// This represents a dynamically generated constant address.
pub struct DynamicConstantAddress<'a> {
    pallet_name: Cow<'a, str>,
    constant_name: Cow<'a, str>,
}

/// Construct a new dynamic constant lookup.
pub fn dynamic<'a>(
    pallet_name: impl Into<Cow<'a, str>>,
    constant_name: impl Into<Cow<'a, str>>,
) -> DynamicConstantAddress<'a> {
    DynamicConstantAddress {
        pallet_name: pallet_name.into(),
        constant_name: constant_name.into(),
    }
}

impl<'a> ConstantAddress for DynamicConstantAddress<'a> {
    type Target = DecodedValueThunk;

    fn pallet_name(&self) -> &str {
        &self.pallet_name
    }

    fn constant_name(&self) -> &str {
        &self.constant_name
    }
}