Skip to main content

midnight_onchain_runtime/
contract_state_ext.rs

1// This file is part of midnight-ledger.
2// Copyright (C) Midnight Foundation
3// SPDX-License-Identifier: Apache-2.0
4// Licensed under the Apache License, Version 2.0 (the "License");
5// You may not use this file except in compliance with the License.
6// You may obtain a copy of the License at
7// http://www.apache.org/licenses/LICENSE-2.0
8// Unless required by applicable law or agreed to in writing, software
9// distributed under the License is distributed on an "AS IS" BASIS,
10// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11// See the License for the specific language governing permissions and
12// limitations under the License.
13
14use storage::db::DB;
15
16use crate::context::QueryContext;
17use crate::cost_model::CostModel;
18use crate::error::TranscriptRejected;
19use crate::ops::Op;
20use crate::result_mode::{GatherEvent, ResultModeGather};
21use crate::state::ContractState;
22
23pub trait ContractStateExt<D: DB> {
24    #[allow(clippy::type_complexity)]
25    fn query(
26        &self,
27        query: &[Op<ResultModeGather, D>],
28        cost_model: &CostModel,
29    ) -> Result<(ContractState<D>, Vec<GatherEvent<D>>), TranscriptRejected<D>>;
30}
31
32impl<D: DB> ContractStateExt<D> for ContractState<D> {
33    fn query(
34        &self,
35        query: &[Op<ResultModeGather, D>],
36        cost_model: &CostModel,
37    ) -> Result<(ContractState<D>, Vec<GatherEvent<D>>), TranscriptRejected<D>> {
38        let qc = QueryContext {
39            state: self.data.clone(),
40            address: Default::default(),
41            effects: Default::default(),
42            call_context: Default::default(),
43        };
44        let res = qc.query(query, None, cost_model)?;
45        Ok((
46            ContractState {
47                data: res.context.state,
48                ..self.clone()
49            },
50            res.events,
51        ))
52    }
53}