Skip to content

Commit

Permalink
fix: serde internally tag non-cryptos enums with #type
Browse files Browse the repository at this point in the history
  • Loading branch information
zcabter committed Nov 14, 2024
1 parent 048d33a commit 43e0328
Show file tree
Hide file tree
Showing 5 changed files with 221 additions and 63 deletions.
215 changes: 160 additions & 55 deletions crates/jstz_node/openapi.json
Original file line number Diff line number Diff line change
Expand Up @@ -422,26 +422,46 @@
"Content": {
"oneOf": [
{
"type": "object",
"required": [
"DeployFunction"
],
"properties": {
"DeployFunction": {
"allOf": [
{
"$ref": "#/components/schemas/DeployFunction"
},
{
"type": "object",
"required": [
"_type"
],
"properties": {
"_type": {
"type": "string",
"enum": [
"DeployFunction"
]
}
}
}
}
]
},
{
"type": "object",
"required": [
"RunFunction"
],
"properties": {
"RunFunction": {
"allOf": [
{
"$ref": "#/components/schemas/RunFunction"
},
{
"type": "object",
"required": [
"_type"
],
"properties": {
"_type": {
"type": "string",
"enum": [
"RunFunction"
]
}
}
}
}
]
}
]
},
Expand Down Expand Up @@ -473,6 +493,23 @@
}
}
},
"DepositReceipt": {
"type": "object",
"required": [
"account",
"updated_balance"
],
"properties": {
"account": {
"$ref": "#/components/schemas/PublicKeyHash"
},
"updated_balance": {
"type": "integer",
"format": "int64",
"minimum": 0
}
}
},
"FaDepositReceipt": {
"type": "object",
"required": [
Expand Down Expand Up @@ -637,75 +674,143 @@
"ReceiptResult_ReceiptContent": {
"oneOf": [
{
"type": "object",
"required": [
"Ok"
],
"properties": {
"Ok": {
"allOf": [
{
"oneOf": [
{
"type": "object",
"required": [
"DeployFunction"
],
"properties": {
"DeployFunction": {
"allOf": [
{
"$ref": "#/components/schemas/DeployFunctionReceipt"
},
{
"type": "object",
"required": [
"_type"
],
"properties": {
"_type": {
"type": "string",
"enum": [
"DeployFunction"
]
}
}
}
}
]
},
{
"type": "object",
"required": [
"RunFunction"
],
"properties": {
"RunFunction": {
"allOf": [
{
"$ref": "#/components/schemas/RunFunctionReceipt"
},
{
"type": "object",
"required": [
"_type"
],
"properties": {
"_type": {
"type": "string",
"enum": [
"RunFunction"
]
}
}
}
}
]
},
{
"type": "string",
"enum": [
"Deposit"
"allOf": [
{
"$ref": "#/components/schemas/DepositReceipt"
},
{
"type": "object",
"required": [
"_type"
],
"properties": {
"_type": {
"type": "string",
"enum": [
"Deposit"
]
}
}
}
]
},
{
"type": "object",
"required": [
"FaDeposit"
],
"properties": {
"FaDeposit": {
"allOf": [
{
"$ref": "#/components/schemas/FaDepositReceipt"
},
{
"type": "object",
"required": [
"_type"
],
"properties": {
"_type": {
"type": "string",
"enum": [
"FaDeposit"
]
}
}
}
}
]
},
{
"type": "object",
"required": [
"FaWithdraw"
],
"properties": {
"FaWithdraw": {
"allOf": [
{
"$ref": "#/components/schemas/FaWithdrawReceipt"
},
{
"type": "object",
"required": [
"_type"
],
"properties": {
"_type": {
"type": "string",
"enum": [
"FaWithdraw"
]
}
}
}
}
]
}
]
},
{
"type": "object",
"required": [
"_type"
],
"properties": {
"_type": {
"type": "string",
"enum": [
"Ok"
]
}
}
}
}
]
},
{
"type": "object",
"required": [
"Err"
"_type"
],
"properties": {
"Err": {
"type": "string"
"_type": {
"type": "string",
"enum": [
"Err"
]
}
}
}
Expand Down
8 changes: 4 additions & 4 deletions crates/jstz_proto/src/context/account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,29 +159,29 @@ impl Account {
tx: &mut Transaction,
addr: &Address,
amount: Amount,
) -> Result<()> {
) -> Result<u64> {
let account = Self::get_mut(hrt, tx, addr)?;
let checked_balance = account
.amount
.checked_add(amount)
.ok_or(crate::error::Error::BalanceOverflow)?;

account.amount = checked_balance;
Ok(())
Ok(account.amount)
}

pub fn sub_balance(
hrt: &impl HostRuntime,
tx: &mut Transaction,
addr: &Address,
amount: Amount,
) -> Result<()> {
) -> Result<u64> {
let account = Self::get_mut(hrt, tx, addr)?;
if account.amount < amount {
return Err(Error::InsufficientFunds)?;
}
account.amount -= amount;
Ok(())
Ok(account.amount)
}

pub fn set_balance(
Expand Down
47 changes: 45 additions & 2 deletions crates/jstz_proto/src/executor/deposit.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
use jstz_core::{host::HostRuntime, kv::Transaction};
use jstz_crypto::hash::Blake2b;

use crate::{context::account::Account, operation::external::Deposit, receipt::Receipt};
use crate::{
context::account::Account,
operation::external::Deposit,
receipt::{DepositReceipt, Receipt},
};

pub fn execute(
hrt: &mut impl HostRuntime,
Expand All @@ -16,6 +20,45 @@ pub fn execute(
let hash = Blake2b::from(deposit.inbox_id.to_be_bytes().as_slice());
Receipt::new(
hash,
result.map(|_| crate::receipt::ReceiptContent::Deposit),
result.map(|updated_balance| {
crate::receipt::ReceiptContent::Deposit(DepositReceipt {
account: receiver,
updated_balance,
})
}),
)
}

#[cfg(test)]
mod test {
use jstz_core::kv::Transaction;
use tezos_smart_rollup_mock::MockHost;

use crate::{
operation::external::Deposit,
receipt::{DepositReceipt, ReceiptContent},
};

use super::execute;

#[test]
fn test_execute_receipt() {
let mut host = MockHost::default();
let mut tx = Transaction::default();
let receiver = jstz_mock::account1();
let deposit = Deposit {
inbox_id: 1,
amount: 20,
receiver: receiver.clone(),
};
tx.begin();
let receipt = execute(&mut host, &mut tx, deposit);
assert!(matches!(
receipt.inner,
Ok(ReceiptContent::Deposit(DepositReceipt {
account,
updated_balance,
})) if account == receiver && updated_balance == 20
))
}
}
1 change: 1 addition & 0 deletions crates/jstz_proto/src/operation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ pub struct RunFunction {
}

#[derive(Debug, Serialize, Deserialize, PartialEq, Eq, Clone, ToSchema)]
#[serde(tag = "_type")]
pub enum Content {
DeployFunction(DeployFunction),
RunFunction(RunFunction),
Expand Down
Loading

0 comments on commit 43e0328

Please sign in to comment.