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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
use json_patch::diff;
use serde_json::Value;
use wasmtime::Engine;

use crate::{
    commons::{
        models::{evaluation::EvaluationRequest, HashId},
        schema_handler::Schema,
    },
    database::DB,
    evaluator::errors::ExecutorErrorResponses,
    governance::GovernanceInterface,
    identifier::DigestIdentifier,
    request::FactRequest,
    DatabaseCollection, Derivable, EvaluationResponse, EventRequest, ValueWrapper,
};

use super::executor::{Contract, ContractExecutor, ContractResult};
use crate::database::Error as DbError;
pub struct TapleRunner<C: DatabaseCollection, G: GovernanceInterface> {
    database: DB<C>,
    executor: ContractExecutor,
    gov_api: G,
}

impl<C: DatabaseCollection, G: GovernanceInterface> TapleRunner<C, G> {
    pub fn new(database: DB<C>, engine: Engine, gov_api: G) -> Self {
        Self {
            database,
            executor: ContractExecutor::new(engine),
            gov_api,
        }
    }

    pub fn generate_context_hash(
        execute_contract: &EvaluationRequest,
    ) -> Result<DigestIdentifier, ExecutorErrorResponses> {
        DigestIdentifier::from_serializable_borsh(execute_contract)
            .map_err(|_| ExecutorErrorResponses::ContextHashGenerationFailed)
    }

    pub async fn execute_contract(
        &self,
        execute_contract: &EvaluationRequest,
        state_data: &FactRequest,
    ) -> Result<EvaluationResponse, ExecutorErrorResponses> {
        // Check governance version
        let governance_id = if &execute_contract.context.schema_id == "governance" {
            if let EventRequest::Fact(data) = &execute_contract.event_request.content {
                data.subject_id.clone()
            } else {
                return Err(ExecutorErrorResponses::CreateRequestNotAllowed);
            }
        } else {
            execute_contract.context.governance_id.clone()
        };
        let context_hash = Self::generate_context_hash(execute_contract)?;
        let governance = match self.database.get_subject(&governance_id) {
            Ok(governance) => governance,
            Err(DbError::EntryNotFound) => {
                // Revisar esto
                return Err(ExecutorErrorResponses::GovernanceNotFound);
            }
            Err(error) => return Err(ExecutorErrorResponses::DatabaseError(error.to_string())),
        };
        if governance.sn > execute_contract.gov_version {
            // Nuestra gov es mayor: mandamos mensaje para que actualice el emisor
            return Err(ExecutorErrorResponses::OurGovIsHigher);
        } else if governance.sn < execute_contract.gov_version {
            // Nuestra gov es menor: no podemos hacer nada. Pedimos LCE al que nos lo envió
            return Err(ExecutorErrorResponses::OurGovIsLower);
        }

        // Governances can be updated without WASM because we know the contract beforehand
        let (contract, contract_gov_version): (Contract, u64) = if execute_contract
            .context
            .schema_id
            == "governance"
            && execute_contract.context.governance_id.digest.is_empty()
        {
            (Contract::GovContract, governance.sn)
        } else {
            match self.database.get_contract(
                &execute_contract.context.governance_id,
                &execute_contract.context.schema_id,
            ) {
                Ok((contract, _, contract_gov_version)) => {
                    (Contract::CompiledContract(contract), contract_gov_version)
                }
                Err(DbError::EntryNotFound) => {
                    return Err(ExecutorErrorResponses::ContractNotFound(
                        execute_contract.context.schema_id.clone(),
                        execute_contract.context.governance_id.to_str(),
                    ));
                }
                Err(error) => return Err(ExecutorErrorResponses::DatabaseError(error.to_string())),
            }
        };
        if contract_gov_version != execute_contract.gov_version {
            return Err(ExecutorErrorResponses::ContractNotUpdated);
        }
        let previous_state = &execute_contract.context.state.clone();
        let mut contract_result = match self
            .executor
            .execute_contract(
                &execute_contract.context.state,
                &state_data.payload,
                contract,
                execute_contract.context.is_owner,
            )
            .await
        {
            Ok(contract_result) => contract_result,
            Err(error) => {
                match error {
                    ExecutorErrorResponses::ContractExecutionFailed
                    | ExecutorErrorResponses::ContractNotInstantiated
                    | ExecutorErrorResponses::ContractNotFound(_, _)
                    | ExecutorErrorResponses::ContractEntryPointNotFound
                    | ExecutorErrorResponses::FunctionLinkingFailed(_)
                    | ExecutorErrorResponses::SubjectError(_)
                    | ExecutorErrorResponses::CantGenerateContractResult
                    | ExecutorErrorResponses::StateHashGenerationFailed
                    | ExecutorErrorResponses::ContextHashGenerationFailed
                    | ExecutorErrorResponses::RolesObtentionFailed
                    | ExecutorErrorResponses::OurGovIsLower
                    | ExecutorErrorResponses::OurGovIsHigher
                    | ExecutorErrorResponses::CreateRequestNotAllowed
                    | ExecutorErrorResponses::GovernanceError(_)
                    | ExecutorErrorResponses::SchemaCompilationFailed
                    | ExecutorErrorResponses::InvalidPointerPovided => {
                        return Ok(EvaluationResponse {
                            patch: ValueWrapper(serde_json::from_str("[]").map_err(|_| {
                                ExecutorErrorResponses::JSONPATCHDeserializationFailed
                            })?),
                            state_hash: DigestIdentifier::from_serializable_borsh(
                                &execute_contract.context.state,
                            )
                            .map_err(|_| ExecutorErrorResponses::StateHashGenerationFailed)?,
                            eval_req_hash: context_hash,
                            eval_success: false,
                            appr_required: false,
                        })
                    }
                    _ => return Err(error),
                    // ExecutorErrorResponses::ValueToStringConversionFailed => todo!(),
                    //  ExecutorErrorResponses::StateJSONDeserializationFailed => todo!(),
                    //  ExecutorErrorResponses::JSONPATCHDeserializationFailed => todo!(),
                    // ExecutorErrorResponses::BorshSerializationError => todo!(),
                    // ExecutorErrorResponses::BorshDeserializationError => todo!(),
                    // ExecutorErrorResponses::DatabaseError(_) => return Err(error),
                }
            }
        };
        let (patch, hash) = match contract_result.success {
            true => {
                match self
                    .validation_state(
                        &contract_result,
                        &governance_id,
                        execute_contract.context.schema_id.clone(),
                        execute_contract.gov_version,
                    )
                    .await
                {
                    Ok(false) | Err(_) => {
                        contract_result.success = false;
                        contract_result.approval_required = false;
                        (
                            serde_json::from_str("[]").map_err(|_| {
                                ExecutorErrorResponses::JSONPATCHDeserializationFailed
                            })?,
                            execute_contract.context.state.hash_id()?,
                        )
                    }
                    _ => (
                        generate_json_patch(&previous_state.0, &contract_result.final_state.0)?,
                        DigestIdentifier::from_serializable_borsh(&contract_result.final_state)
                            .map_err(|_| ExecutorErrorResponses::StateHashGenerationFailed)?,
                    ),
                }
            }
            false => (
                serde_json::from_str("[]")
                    .map_err(|_| ExecutorErrorResponses::JSONPATCHDeserializationFailed)?,
                execute_contract.context.state.hash_id()?,
            ),
        };
        Ok(EvaluationResponse {
            patch: ValueWrapper(patch),
            state_hash: hash,
            eval_req_hash: context_hash,
            eval_success: contract_result.success,
            appr_required: contract_result.approval_required,
        })
    }

    async fn validation_state(
        &self,
        contract_result: &ContractResult,
        governance_id: &DigestIdentifier,
        schema_id: String,
        governance_version: u64,
    ) -> Result<bool, ExecutorErrorResponses> {
        if contract_result.success {
            let new_state = &contract_result.final_state;
            // Comprobar el estado contra el esquema definido en la gobernanza
            let schema = self
                .gov_api
                .get_schema(governance_id.clone(), schema_id, governance_version)
                .await?;
            let schema = Schema::compile(&schema.0)
                .map_err(|_| ExecutorErrorResponses::SchemaCompilationFailed)?;
            Ok(schema.validate(
                &serde_json::to_value(new_state)
                    .map_err(|_| ExecutorErrorResponses::StateJSONDeserializationFailed)?,
            ))
        } else {
            Ok(true)
        }
    }
}

fn generate_json_patch(
    prev_state: &Value,
    new_state: &Value,
) -> Result<Value, ExecutorErrorResponses> {
    let patch = diff(&prev_state, &new_state);
    Ok(serde_json::to_value(&patch)
        .map_err(|_| ExecutorErrorResponses::JSONPATCHDeserializationFailed)?)
}