fuel_core_client/client/schema/
chain.rs

1use crate::client::schema::{
2    block::Block,
3    schema,
4    Address,
5    AssetId,
6    ConversionError,
7    U16,
8    U32,
9    U64,
10};
11
12#[derive(cynic::QueryFragment, Clone, Debug)]
13#[cynic(schema_path = "./assets/schema.sdl")]
14pub struct ConsensusParameters {
15    pub version: ConsensusParametersVersion,
16    pub tx_params: TxParameters,
17    pub predicate_params: PredicateParameters,
18    pub script_params: ScriptParameters,
19    pub contract_params: ContractParameters,
20    pub fee_params: FeeParameters,
21    pub base_asset_id: AssetId,
22    pub block_gas_limit: U64,
23    pub block_transaction_size_limit: U64,
24    pub chain_id: U64,
25    pub gas_costs: GasCosts,
26    pub privileged_address: Address,
27}
28
29#[derive(cynic::Enum, Clone, Debug)]
30#[cynic(schema_path = "./assets/schema.sdl")]
31pub enum ConsensusParametersVersion {
32    V1,
33    #[cynic(fallback)]
34    Unknown,
35}
36
37#[derive(cynic::QueryFragment, Clone, Debug)]
38#[cynic(schema_path = "./assets/schema.sdl")]
39pub struct TxParameters {
40    pub version: TxParametersVersion,
41    pub max_inputs: U16,
42    pub max_outputs: U16,
43    pub max_witnesses: U32,
44    pub max_gas_per_tx: U64,
45    pub max_size: U64,
46    pub max_bytecode_subsections: U16,
47}
48
49#[derive(cynic::Enum, Clone, Debug)]
50#[cynic(schema_path = "./assets/schema.sdl")]
51pub enum TxParametersVersion {
52    V1,
53    #[cynic(fallback)]
54    Unknown,
55}
56
57impl TryFrom<TxParameters> for fuel_core_types::fuel_tx::TxParameters {
58    type Error = ConversionError;
59
60    fn try_from(params: TxParameters) -> Result<Self, Self::Error> {
61        match params.version {
62            TxParametersVersion::V1 => Ok(
63                fuel_core_types::fuel_tx::consensus_parameters::TxParametersV1 {
64                    max_inputs: params.max_inputs.into(),
65                    max_outputs: params.max_outputs.into(),
66                    max_witnesses: params.max_witnesses.into(),
67                    max_gas_per_tx: params.max_gas_per_tx.into(),
68                    max_size: params.max_size.into(),
69                    max_bytecode_subsections: params.max_bytecode_subsections.into(),
70                }
71                .into(),
72            ),
73            _ => Err(ConversionError::UnknownVariant("TxParametersVersion")),
74        }
75    }
76}
77
78#[derive(cynic::QueryFragment, Clone, Debug)]
79#[cynic(schema_path = "./assets/schema.sdl")]
80pub struct PredicateParameters {
81    pub version: PredicateParametersVersion,
82    pub max_predicate_length: U64,
83    pub max_predicate_data_length: U64,
84    pub max_message_data_length: U64,
85    pub max_gas_per_predicate: U64,
86}
87
88#[derive(cynic::Enum, Clone, Debug)]
89#[cynic(schema_path = "./assets/schema.sdl")]
90pub enum PredicateParametersVersion {
91    V1,
92    #[cynic(fallback)]
93    Unknown,
94}
95
96impl TryFrom<PredicateParameters> for fuel_core_types::fuel_tx::PredicateParameters {
97    type Error = ConversionError;
98
99    fn try_from(params: PredicateParameters) -> Result<Self, Self::Error> {
100        match params.version {
101            PredicateParametersVersion::V1 => Ok(
102                fuel_core_types::fuel_tx::consensus_parameters::PredicateParametersV1 {
103                    max_predicate_length: params.max_predicate_length.into(),
104                    max_predicate_data_length: params.max_predicate_data_length.into(),
105                    max_message_data_length: params.max_message_data_length.into(),
106                    max_gas_per_predicate: params.max_gas_per_predicate.into(),
107                }
108                .into(),
109            ),
110            _ => Err(ConversionError::UnknownVariant(
111                "PredicateParametersVersion",
112            )),
113        }
114    }
115}
116
117#[derive(cynic::QueryFragment, Clone, Debug)]
118#[cynic(schema_path = "./assets/schema.sdl")]
119pub struct ScriptParameters {
120    pub version: ScriptParametersVersion,
121    pub max_script_length: U64,
122    pub max_script_data_length: U64,
123}
124
125#[derive(cynic::Enum, Clone, Debug)]
126#[cynic(schema_path = "./assets/schema.sdl")]
127pub enum ScriptParametersVersion {
128    V1,
129    #[cynic(fallback)]
130    Unknown,
131}
132
133impl TryFrom<ScriptParameters> for fuel_core_types::fuel_tx::ScriptParameters {
134    type Error = ConversionError;
135
136    fn try_from(params: ScriptParameters) -> Result<Self, Self::Error> {
137        match params.version {
138            ScriptParametersVersion::V1 => Ok(
139                fuel_core_types::fuel_tx::consensus_parameters::ScriptParametersV1 {
140                    max_script_length: params.max_script_length.into(),
141                    max_script_data_length: params.max_script_data_length.into(),
142                }
143                .into(),
144            ),
145            _ => Err(ConversionError::UnknownVariant("ScriptParametersVersion")),
146        }
147    }
148}
149
150#[derive(cynic::QueryFragment, Clone, Debug)]
151#[cynic(schema_path = "./assets/schema.sdl")]
152pub struct ContractParameters {
153    pub version: ContractParametersVersion,
154    pub contract_max_size: U64,
155    pub max_storage_slots: U64,
156}
157
158#[derive(cynic::Enum, Clone, Debug)]
159#[cynic(schema_path = "./assets/schema.sdl")]
160pub enum ContractParametersVersion {
161    V1,
162    #[cynic(fallback)]
163    Unknown,
164}
165
166impl TryFrom<ContractParameters> for fuel_core_types::fuel_tx::ContractParameters {
167    type Error = ConversionError;
168
169    fn try_from(params: ContractParameters) -> Result<Self, Self::Error> {
170        match params.version {
171            ContractParametersVersion::V1 => Ok(
172                fuel_core_types::fuel_tx::consensus_parameters::ContractParametersV1 {
173                    contract_max_size: params.contract_max_size.into(),
174                    max_storage_slots: params.max_storage_slots.into(),
175                }
176                .into(),
177            ),
178            _ => Err(ConversionError::UnknownVariant("ContractParametersVersion")),
179        }
180    }
181}
182
183#[derive(cynic::QueryFragment, Clone, Debug)]
184#[cynic(schema_path = "./assets/schema.sdl")]
185pub struct FeeParameters {
186    pub version: FeeParametersVersion,
187    pub gas_price_factor: U64,
188    pub gas_per_byte: U64,
189}
190
191#[derive(cynic::Enum, Clone, Debug)]
192#[cynic(schema_path = "./assets/schema.sdl")]
193pub enum FeeParametersVersion {
194    V1,
195    #[cynic(fallback)]
196    Unknown,
197}
198
199impl TryFrom<FeeParameters> for fuel_core_types::fuel_tx::FeeParameters {
200    type Error = ConversionError;
201
202    fn try_from(params: FeeParameters) -> Result<Self, Self::Error> {
203        match params.version {
204            FeeParametersVersion::V1 => Ok(
205                fuel_core_types::fuel_tx::consensus_parameters::FeeParametersV1 {
206                    gas_price_factor: params.gas_price_factor.into(),
207                    gas_per_byte: params.gas_per_byte.into(),
208                }
209                .into(),
210            ),
211            _ => Err(ConversionError::UnknownVariant("FeeParametersVersion")),
212        }
213    }
214}
215
216#[derive(cynic::QueryFragment, Clone, Debug)]
217#[cynic(schema_path = "./assets/schema.sdl")]
218pub struct GasCosts {
219    pub version: GasCostsVersion,
220    pub add: U64,
221    pub addi: U64,
222    pub and: U64,
223    pub andi: U64,
224    pub bal: U64,
225    pub bhei: U64,
226    pub bhsh: U64,
227    pub burn: U64,
228    pub cb: U64,
229    pub cfsi: U64,
230    pub div: U64,
231    pub divi: U64,
232    pub eck1: U64,
233    pub ecr1: U64,
234    pub ed19: U64,
235    pub eq: U64,
236    pub exp: U64,
237    pub expi: U64,
238    pub flag: U64,
239    pub gm: U64,
240    pub gt: U64,
241    pub gtf: U64,
242    pub ji: U64,
243    pub jmp: U64,
244    pub jne: U64,
245    pub jnei: U64,
246    pub jnzi: U64,
247    pub jmpf: U64,
248    pub jmpb: U64,
249    pub jnzf: U64,
250    pub jnzb: U64,
251    pub jnef: U64,
252    pub jneb: U64,
253    pub lb: U64,
254    pub log: U64,
255    pub lt: U64,
256    pub lw: U64,
257    pub mint: U64,
258    pub mlog: U64,
259    pub mod_op: U64,
260    pub modi: U64,
261    pub move_op: U64,
262    pub movi: U64,
263    pub mroo: U64,
264    pub mul: U64,
265    pub muli: U64,
266    pub mldv: U64,
267    pub noop: U64,
268    pub not: U64,
269    pub or: U64,
270    pub ori: U64,
271    pub poph: U64,
272    pub popl: U64,
273    pub pshh: U64,
274    pub pshl: U64,
275    pub ret: U64,
276    pub rvrt: U64,
277    pub sb: U64,
278    pub sll: U64,
279    pub slli: U64,
280    pub srl: U64,
281    pub srli: U64,
282    pub srw: U64,
283    pub sub: U64,
284    pub subi: U64,
285    pub sw: U64,
286    pub sww: U64,
287    pub time: U64,
288    pub tr: U64,
289    pub tro: U64,
290    pub wdcm: U64,
291    pub wqcm: U64,
292    pub wdop: U64,
293    pub wqop: U64,
294    pub wdml: U64,
295    pub wqml: U64,
296    pub wddv: U64,
297    pub wqdv: U64,
298    pub wdmd: U64,
299    pub wqmd: U64,
300    pub wdam: U64,
301    pub wqam: U64,
302    pub wdmm: U64,
303    pub wqmm: U64,
304    pub xor: U64,
305    pub xori: U64,
306    pub ecop: Option<U64>,
307
308    pub aloc_dependent_cost: DependentCost,
309    pub bsiz: Option<DependentCost>,
310    pub bldd: Option<DependentCost>,
311    pub cfe: DependentCost,
312    pub cfei_dependent_cost: DependentCost,
313    pub call: DependentCost,
314    pub ccp: DependentCost,
315    pub croo: DependentCost,
316    pub csiz: DependentCost,
317    pub ed19_dependent_cost: DependentCost,
318    pub k256: DependentCost,
319    pub ldc: DependentCost,
320    pub logd: DependentCost,
321    pub mcl: DependentCost,
322    pub mcli: DependentCost,
323    pub mcp: DependentCost,
324    pub mcpi: DependentCost,
325    pub meq: DependentCost,
326    pub retd: DependentCost,
327    pub s256: DependentCost,
328    pub scwq: DependentCost,
329    pub smo: DependentCost,
330    pub srwq: DependentCost,
331    pub swwq: DependentCost,
332    pub epar: Option<DependentCost>,
333
334    // Non-opcodes prices
335    pub contract_root: DependentCost,
336    pub state_root: DependentCost,
337    pub vm_initialization: DependentCost,
338    pub new_storage_per_byte: U64,
339}
340
341#[derive(cynic::Enum, Clone, Debug)]
342#[cynic(schema_path = "./assets/schema.sdl")]
343pub enum GasCostsVersion {
344    V1,
345    #[cynic(fallback)]
346    Unknown,
347}
348
349impl TryFrom<GasCosts> for fuel_core_types::fuel_tx::GasCosts {
350    type Error = ConversionError;
351
352    fn try_from(value: GasCosts) -> Result<Self, Self::Error> {
353        match value.version {
354            GasCostsVersion::V1 => Ok(fuel_core_types::fuel_tx::GasCosts::new(
355                fuel_core_types::fuel_tx::consensus_parameters::gas::GasCostsValuesV5 {
356                    add: value.add.into(),
357                    addi: value.addi.into(),
358                    and: value.and.into(),
359                    andi: value.andi.into(),
360                    bal: value.bal.into(),
361                    bhei: value.bhei.into(),
362                    bhsh: value.bhsh.into(),
363                    burn: value.burn.into(),
364                    cb: value.cb.into(),
365                    cfsi: value.cfsi.into(),
366                    div: value.div.into(),
367                    divi: value.divi.into(),
368                    eck1: value.eck1.into(),
369                    ecr1: value.ecr1.into(),
370                    eq: value.eq.into(),
371                    exp: value.exp.into(),
372                    expi: value.expi.into(),
373                    flag: value.flag.into(),
374                    gm: value.gm.into(),
375                    gt: value.gt.into(),
376                    gtf: value.gtf.into(),
377                    ji: value.ji.into(),
378                    jmp: value.jmp.into(),
379                    jne: value.jne.into(),
380                    jnei: value.jnei.into(),
381                    jnzi: value.jnzi.into(),
382                    jmpf: value.jmpf.into(),
383                    jmpb: value.jmpb.into(),
384                    jnzf: value.jnzf.into(),
385                    jnzb: value.jnzb.into(),
386                    jnef: value.jnef.into(),
387                    jneb: value.jneb.into(),
388                    lb: value.lb.into(),
389                    log: value.log.into(),
390                    lt: value.lt.into(),
391                    lw: value.lw.into(),
392                    mint: value.mint.into(),
393                    mlog: value.mlog.into(),
394                    mod_op: value.mod_op.into(),
395                    modi: value.modi.into(),
396                    move_op: value.move_op.into(),
397                    movi: value.movi.into(),
398                    mroo: value.mroo.into(),
399                    mul: value.mul.into(),
400                    muli: value.muli.into(),
401                    mldv: value.mldv.into(),
402                    noop: value.noop.into(),
403                    not: value.not.into(),
404                    or: value.or.into(),
405                    ori: value.ori.into(),
406                    poph: value.poph.into(),
407                    popl: value.popl.into(),
408                    pshh: value.pshh.into(),
409                    pshl: value.pshl.into(),
410                    ret: value.ret.into(),
411                    rvrt: value.rvrt.into(),
412                    sb: value.sb.into(),
413                    sll: value.sll.into(),
414                    slli: value.slli.into(),
415                    srl: value.srl.into(),
416                    srli: value.srli.into(),
417                    srw: value.srw.into(),
418                    sub: value.sub.into(),
419                    subi: value.subi.into(),
420                    sw: value.sw.into(),
421                    sww: value.sww.into(),
422                    time: value.time.into(),
423                    tr: value.tr.into(),
424                    tro: value.tro.into(),
425                    wdcm: value.wdcm.into(),
426                    wqcm: value.wqcm.into(),
427                    wdop: value.wdop.into(),
428                    wqop: value.wqop.into(),
429                    wdml: value.wdml.into(),
430                    wqml: value.wqml.into(),
431                    wddv: value.wddv.into(),
432                    wqdv: value.wqdv.into(),
433                    wdmd: value.wdmd.into(),
434                    wqmd: value.wqmd.into(),
435                    wdam: value.wdam.into(),
436                    wqam: value.wqam.into(),
437                    wdmm: value.wdmm.into(),
438                    wqmm: value.wqmm.into(),
439                    xor: value.xor.into(),
440                    xori: value.xori.into(),
441                    ecop: value.ecop.map(Into::into).unwrap_or(0),
442
443                    aloc: value.aloc_dependent_cost.into(),
444                    bsiz: value.bsiz.map(Into::into).unwrap_or(fuel_core_types::fuel_tx::consensus_parameters::DependentCost::free()),
445                    bldd: value.bldd.map(Into::into).unwrap_or(fuel_core_types::fuel_tx::consensus_parameters::DependentCost::free()),
446                    cfe: value.cfe.into(),
447                    cfei: value.cfei_dependent_cost.into(),
448                    call: value.call.into(),
449                    ccp: value.ccp.into(),
450                    croo: value.croo.into(),
451                    csiz: value.csiz.into(),
452                    ed19: value.ed19_dependent_cost.into(),
453                    k256: value.k256.into(),
454                    ldc: value.ldc.into(),
455                    logd: value.logd.into(),
456                    mcl: value.mcl.into(),
457                    mcli: value.mcli.into(),
458                    mcp: value.mcp.into(),
459                    mcpi: value.mcpi.into(),
460                    meq: value.meq.into(),
461                    retd: value.retd.into(),
462                    s256: value.s256.into(),
463                    scwq: value.scwq.into(),
464                    smo: value.smo.into(),
465                    srwq: value.srwq.into(),
466                    swwq: value.swwq.into(),
467                    epar: value.epar.map(Into::into).unwrap_or(fuel_core_types::fuel_tx::consensus_parameters::DependentCost::free()),
468                    contract_root: value.contract_root.into(),
469                    state_root: value.state_root.into(),
470                    vm_initialization: value.vm_initialization.into(),
471                    new_storage_per_byte: value.new_storage_per_byte.into(),
472                }
473                .into(),
474            )),
475            _ => Err(ConversionError::UnknownVariant("GasCostsVersion")),
476        }
477    }
478}
479
480#[derive(cynic::QueryFragment, Clone, Debug)]
481#[cynic(schema_path = "./assets/schema.sdl")]
482pub struct LightOperation {
483    pub base: U64,
484    pub units_per_gas: U64,
485}
486
487#[derive(cynic::QueryFragment, Clone, Debug)]
488#[cynic(schema_path = "./assets/schema.sdl")]
489pub struct HeavyOperation {
490    pub base: U64,
491    pub gas_per_unit: U64,
492}
493
494#[derive(cynic::InlineFragments, Clone, Debug)]
495#[cynic(schema_path = "./assets/schema.sdl")]
496pub enum DependentCost {
497    LightOperation(LightOperation),
498    HeavyOperation(HeavyOperation),
499    #[cynic(fallback)]
500    Unknown,
501}
502
503impl TryFrom<ConsensusParameters> for fuel_core_types::fuel_tx::ConsensusParameters {
504    type Error = ConversionError;
505
506    fn try_from(params: ConsensusParameters) -> Result<Self, Self::Error> {
507        match params.version {
508            ConsensusParametersVersion::V1 => Ok(
509                fuel_core_types::fuel_tx::consensus_parameters::ConsensusParametersV2 {
510                    tx_params: params.tx_params.try_into()?,
511                    predicate_params: params.predicate_params.try_into()?,
512                    script_params: params.script_params.try_into()?,
513                    contract_params: params.contract_params.try_into()?,
514                    fee_params: params.fee_params.try_into()?,
515                    base_asset_id: params.base_asset_id.into(),
516                    block_gas_limit: params.block_gas_limit.into(),
517                    block_transaction_size_limit: params
518                        .block_transaction_size_limit
519                        .into(),
520                    chain_id: params.chain_id.0.into(),
521                    gas_costs: params.gas_costs.try_into()?,
522                    privileged_address: params.privileged_address.into(),
523                }
524                .into(),
525            ),
526            _ => Err(ConversionError::UnknownVariant(
527                "ConsensusParametersVersion",
528            )),
529        }
530    }
531}
532
533#[derive(cynic::QueryFragment, Clone, Debug)]
534#[cynic(schema_path = "./assets/schema.sdl", graphql_type = "Query")]
535pub struct ChainQuery {
536    pub chain: ChainInfo,
537}
538
539#[derive(cynic::QueryFragment, Clone, Debug)]
540#[cynic(schema_path = "./assets/schema.sdl")]
541pub struct ChainInfo {
542    pub da_height: U64,
543    pub name: String,
544    pub latest_block: Block,
545    pub consensus_parameters: ConsensusParameters,
546}
547
548#[cfg(test)]
549mod tests {
550    use super::*;
551
552    #[test]
553    fn chain_gql_query_output() {
554        use cynic::QueryBuilder;
555        let operation = ChainQuery::build(());
556
557        let snapshot_name = if cfg!(feature = "fault-proving") {
558            "chain_gql_query_output_with_tx_id_commitment"
559        } else {
560            "chain_gql_query_output"
561        };
562
563        insta::assert_snapshot!(snapshot_name, operation.query)
564    }
565}