Skip to content

Commit

Permalink
making response always flat
Browse files Browse the repository at this point in the history
  • Loading branch information
benesjan committed Jan 3, 2025
1 parent 8734dec commit f3070e8
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 8 deletions.
15 changes: 9 additions & 6 deletions noir-projects/aztec-nr/aztec/src/oracle/pxe_db.nr
Original file line number Diff line number Diff line change
Expand Up @@ -20,23 +20,26 @@ where
/// Load data from local PXE database. We pass in `t_size` as a parameter to have the information of how many fields
/// we need to pad if the key does not exist (note that the actual response size is `t_size + 1` as the Option prefixes
/// the response with a boolean indicating if the data exists).
///
/// Note that we need to return an Option<[Field; N]> as we cannot return an Option<T> directly. This is because then
/// the shape of T would affect the expected oracle response (e.g. if we were returning a struct of 3 u32 values
/// then the expected response shape would be 3 single items. If instead we had a struct containing
/// `u32, [Field;10], u32`, then the expected shape would be single, array, single.).
#[oracle(load)]
unconstrained fn load_oracle<T, let N: u32>(
unconstrained fn load_oracle<let N: u32>(
contract_address: AztecAddress,
key: Field,
t_size: u32,
) -> Option<T>
where
T: Deserialize<N>,
{}
) -> Option<[Field; N]> {}

/// Load a value of type T that implements Deserialize from local PXE database. The data is scoped to the current
/// contract. If the key does not exist, Option::none() is returned.
pub unconstrained fn load<T, let N: u32>(contract_address: AztecAddress, key: Field) -> Option<T>
where
T: Deserialize<N>,
{
load_oracle(contract_address, key, N)
let serialized_option = load_oracle::<N>(contract_address, key, N);
serialized_option.map(|arr| Deserialize::deserialize(arr))
}

mod test {
Expand Down
4 changes: 2 additions & 2 deletions yarn-project/txe/src/txe_service/txe_service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -620,10 +620,10 @@ export class TXEService {
if (values === null) {
// No data was found so we set the data-found flag to 0 and we pad with zeros get the correct return size.
const processedTSize = fromSingle(tSize).toNumber();
return toForeignCallResult(toArray([new Fr(0), ...Array(processedTSize).fill(new Fr(0))]));
return toForeignCallResult([toSingle(new Fr(0)), toArray(Array(processedTSize).fill(new Fr(0)))]);
} else {
// Data was found so we set the data-found flag to 1 and return it along with the data.
return toForeignCallResult(toArray([new Fr(1), ...values]));
return toForeignCallResult([toSingle(new Fr(1)), toArray(values)]);
}
}

Expand Down

0 comments on commit f3070e8

Please sign in to comment.