fuel_core/schema/
upgrades.rs

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
use crate::{
    graphql_api::{
        api_service::ConsensusProvider,
        query_costs,
        IntoApiResult,
    },
    schema::{
        chain::ConsensusParameters,
        scalars::HexString,
        ReadViewProvider,
    },
};
use async_graphql::{
    Context,
    Object,
    SimpleObject,
};
use fuel_core_types::{
    blockchain::header::{
        ConsensusParametersVersion,
        StateTransitionBytecodeVersion,
    },
    fuel_types,
    fuel_vm::UploadedBytecode as StorageUploadedBytecode,
};

#[derive(Default)]
pub struct UpgradeQuery;

#[Object]
impl UpgradeQuery {
    #[graphql(complexity = "query_costs().storage_read + child_complexity")]
    async fn consensus_parameters(
        &self,
        ctx: &Context<'_>,
        version: ConsensusParametersVersion,
    ) -> async_graphql::Result<ConsensusParameters> {
        let params = ctx
            .data_unchecked::<ConsensusProvider>()
            .consensus_params_at_version(&version)?;

        Ok(ConsensusParameters(params))
    }

    #[graphql(complexity = "query_costs().storage_read + child_complexity")]
    async fn state_transition_bytecode_by_version(
        &self,
        ctx: &Context<'_>,
        version: StateTransitionBytecodeVersion,
    ) -> async_graphql::Result<Option<StateTransitionBytecode>> {
        let query = ctx.read_view()?;
        query
            .state_transition_bytecode_root(version)
            .into_api_result()
    }

    #[graphql(complexity = "query_costs().storage_read + child_complexity")]
    async fn state_transition_bytecode_by_root(
        &self,
        root: HexString,
    ) -> async_graphql::Result<StateTransitionBytecode> {
        StateTransitionBytecode::try_from(root)
    }
}

pub struct StateTransitionBytecode {
    root: fuel_types::Bytes32,
}

#[Object]
impl StateTransitionBytecode {
    async fn root(&self) -> HexString {
        HexString(self.root.to_vec())
    }

    #[graphql(complexity = "query_costs().state_transition_bytecode_read")]
    async fn bytecode(
        &self,
        ctx: &Context<'_>,
    ) -> async_graphql::Result<UploadedBytecode> {
        let query = ctx.read_view()?;
        query
            .state_transition_bytecode(self.root)
            .map(UploadedBytecode::from)
            .map_err(async_graphql::Error::from)
    }
}

impl From<fuel_types::Bytes32> for StateTransitionBytecode {
    fn from(root: fuel_types::Bytes32) -> Self {
        Self { root }
    }
}

impl TryFrom<HexString> for StateTransitionBytecode {
    type Error = async_graphql::Error;

    fn try_from(root: HexString) -> Result<Self, Self::Error> {
        let root = root.0.as_slice().try_into()?;
        Ok(Self { root })
    }
}

#[derive(SimpleObject)]
pub struct UploadedBytecode {
    /// Combined bytecode of all uploaded subsections.
    bytecode: HexString,
    /// Number of uploaded subsections (if incomplete).
    uploaded_subsections_number: Option<u16>,
    /// Indicates if the bytecode upload is complete.
    completed: bool,
}

impl From<StorageUploadedBytecode> for UploadedBytecode {
    fn from(value: fuel_core_types::fuel_vm::UploadedBytecode) -> Self {
        match value {
            StorageUploadedBytecode::Uncompleted {
                bytecode,
                uploaded_subsections_number,
            } => Self {
                bytecode: HexString(bytecode),
                uploaded_subsections_number: Some(uploaded_subsections_number),
                completed: false,
            },
            StorageUploadedBytecode::Completed(bytecode) => Self {
                bytecode: HexString(bytecode),
                uploaded_subsections_number: None,
                completed: true,
            },
        }
    }
}