feat: return any value type from execute_contract_allow_private

This commit is contained in:
Hugo Caillard
2024-03-11 22:33:48 +01:00
parent 067633d7b1
commit deaeeb518b
2 changed files with 13 additions and 6 deletions

View File

@@ -340,8 +340,8 @@ impl DefinedFunction {
pub fn apply(&self, args: &[Value], env: &mut Environment) -> Result<Value> {
match self.define_type {
DefineType::Private => self.execute_apply(args, env),
DefineType::Public => env.execute_function_as_transaction(self, args, None),
DefineType::ReadOnly => env.execute_function_as_transaction(self, args, None),
DefineType::Public => env.execute_function_as_transaction(self, args, None, false),
DefineType::ReadOnly => env.execute_function_as_transaction(self, args, None, false),
}
}

View File

@@ -1139,8 +1139,7 @@ impl<'a, 'b, 'hooks> Environment<'a, 'b, 'hooks> {
return Err(CheckErrors::CircularReference(vec![func_identifier.to_string()]).into())
}
self.call_stack.insert(&func_identifier, true);
let res = self.execute_function_as_transaction(&func, &args, Some(&contract.contract_context));
let res = self.execute_function_as_transaction(&func, &args, Some(&contract.contract_context), allow_private);
self.call_stack.remove(&func_identifier, true)?;
match res {
@@ -1168,6 +1167,7 @@ impl<'a, 'b, 'hooks> Environment<'a, 'b, 'hooks> {
function: &DefinedFunction,
args: &[Value],
next_contract_context: Option<&ContractContext>,
allow_private: bool,
) -> Result<Value> {
let make_read_only = function.is_read_only();
@@ -1196,7 +1196,7 @@ impl<'a, 'b, 'hooks> Environment<'a, 'b, 'hooks> {
self.global_context.roll_back()?;
result
} else {
self.global_context.handle_tx_result(result)
self.global_context.handle_tx_result(result, allow_private)
}
}
@@ -1726,7 +1726,11 @@ impl<'a, 'hooks> GlobalContext<'a, 'hooks> {
self.database.roll_back()
}
pub fn handle_tx_result(&mut self, result: Result<Value>) -> Result<Value> {
pub fn handle_tx_result(
&mut self,
result: Result<Value>,
allow_private: bool,
) -> Result<Value> {
if let Ok(result) = result {
if let Value::Response(data) = result {
if data.committed {
@@ -1735,6 +1739,9 @@ impl<'a, 'hooks> GlobalContext<'a, 'hooks> {
self.roll_back()?;
}
Ok(Value::Response(data))
} else if allow_private {
self.commit()?;
Ok(result)
} else {
Err(
CheckErrors::PublicFunctionMustReturnResponse(TypeSignature::type_of(&result)?)