Skip to content

Commit

Permalink
Do not let faulty mpn transactions get half-executed!
Browse files Browse the repository at this point in the history
  • Loading branch information
keyvank committed Jun 5, 2023
1 parent e79847e commit 360424b
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 27 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "bazuka"
version = "0.19.8"
version = "0.19.9"
authors = ["El Geuse <[email protected]>"]
edition = "2021"

Expand Down
16 changes: 10 additions & 6 deletions src/mpn/deposit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ pub fn deposit<K: KvStore, B: Blockchain<K>>(
};
let acc_token = acc.tokens.get(&zk_token_index).clone();

let mut isolated = mirror.mirror();
let mut isolated_state_size = state_size;
if rejected_pub_keys.contains(&src_pub)
|| (acc.address != Default::default() && tx.zk_address.0.decompress() != acc.address)
|| (acc_token.is_some() && acc_token.unwrap().token_id != tx.payment.amount.token_id)
Expand All @@ -86,7 +88,7 @@ pub fn deposit<K: KvStore, B: Blockchain<K>>(
rejected_pub_keys.insert(src_pub);
continue;
}
mirror.update(&[WriteOp::Put(
isolated.update(&[WriteOp::Put(
crate::db::keys::account_balance(&src_pub, tx.payment.amount.token_id),
(src_amount_bal - tx.payment.amount.amount).into(),
)])?;
Expand All @@ -96,7 +98,7 @@ pub fn deposit<K: KvStore, B: Blockchain<K>>(
rejected_pub_keys.insert(src_pub);
continue;
}
mirror.update(&[WriteOp::Put(
isolated.update(&[WriteOp::Put(
crate::db::keys::account_balance(&src_pub, tx.payment.fee.token_id),
(src_fee_bal - tx.payment.fee.amount).into(),
)])?;
Expand All @@ -114,26 +116,26 @@ pub fn deposit<K: KvStore, B: Blockchain<K>>(
.amount += tx.payment.amount.amount;

let balance_proof = KvStoreStateManager::<ZkHasher>::prove(
&mirror,
&isolated,
mpn_contract_id,
ZkDataLocator(vec![account_index, 4]),
zk_token_index,
)
.unwrap();
let proof = KvStoreStateManager::<ZkHasher>::prove(
&mirror,
&isolated,
mpn_contract_id,
ZkDataLocator(vec![]),
account_index,
)
.unwrap();

KvStoreStateManager::<ZkHasher>::set_mpn_account(
&mut mirror,
&mut isolated,
mpn_contract_id,
account_index,
updated_acc,
&mut state_size,
&mut isolated_state_size,
)
.unwrap();

Expand All @@ -152,6 +154,8 @@ pub fn deposit<K: KvStore, B: Blockchain<K>>(
balance_proof,
});
accepted.push(tx);
mirror.update(&isolated.to_ops())?;
state_size = isolated_state_size;
}
}
let next_state =
Expand Down
28 changes: 16 additions & 12 deletions src/mpn/update.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,8 @@ pub fn update<K: KvStore, B: Blockchain<K>>(
continue;
};
let dst_token = dst_before.tokens.get(&dst_token_index);
let mut isolated = mirror.mirror();
let mut isolated_state_size = state_size;
if tx.nonce != src_before.tx_nonce + 1
|| src_before.address != tx.src_pub_key.decompress()
|| (dst_before.address.is_on_curve()
Expand All @@ -115,7 +117,7 @@ pub fn update<K: KvStore, B: Blockchain<K>>(
continue;
} else {
let src_proof = KvStoreStateManager::<ZkHasher>::prove(
&mirror,
&isolated,
mpn_contract_id,
ZkDataLocator(vec![]),
src_index,
Expand All @@ -130,7 +132,7 @@ pub fn update<K: KvStore, B: Blockchain<K>>(
};

let src_balance_proof = KvStoreStateManager::<ZkHasher>::prove(
&mirror,
&isolated,
mpn_contract_id,
ZkDataLocator(vec![src_index, 4]),
src_token_index,
Expand All @@ -139,11 +141,11 @@ pub fn update<K: KvStore, B: Blockchain<K>>(

src_after.tokens.get_mut(&src_token_index).unwrap().amount -= tx.amount.amount;
KvStoreStateManager::<ZkHasher>::set_mpn_account(
&mut mirror,
&mut isolated,
mpn_contract_id,
src_index,
src_after.clone(),
&mut state_size,
&mut isolated_state_size,
)
.unwrap();

Expand All @@ -161,7 +163,7 @@ pub fn update<K: KvStore, B: Blockchain<K>>(
}

let src_fee_balance_proof = KvStoreStateManager::<ZkHasher>::prove(
&mirror,
&isolated,
mpn_contract_id,
ZkDataLocator(vec![src_index, 4]),
src_fee_token_index,
Expand All @@ -174,31 +176,31 @@ pub fn update<K: KvStore, B: Blockchain<K>>(
.unwrap()
.amount -= tx.fee.amount;
KvStoreStateManager::<ZkHasher>::set_mpn_account(
&mut mirror,
&mut isolated,
mpn_contract_id,
src_index,
src_after,
&mut state_size,
&mut isolated_state_size,
)
.unwrap();

let dst_proof = KvStoreStateManager::<ZkHasher>::prove(
&mirror,
&isolated,
mpn_contract_id,
ZkDataLocator(vec![]),
dst_index,
)
.unwrap();
let dst_balance_proof = KvStoreStateManager::<ZkHasher>::prove(
&mirror,
&isolated,
mpn_contract_id,
ZkDataLocator(vec![dst_index, 4]),
dst_token_index,
)
.unwrap();

let dst_before = KvStoreStateManager::<ZkHasher>::get_mpn_account(
&mirror,
&isolated,
mpn_contract_id,
dst_index,
)
Expand All @@ -217,11 +219,11 @@ pub fn update<K: KvStore, B: Blockchain<K>>(
.or_insert(Money::new(tx.amount.token_id, 0))
.amount += tx.amount.amount;
KvStoreStateManager::<ZkHasher>::set_mpn_account(
&mut mirror,
&mut isolated,
mpn_contract_id,
dst_index,
dst_after,
&mut state_size,
&mut isolated_state_size,
)
.unwrap();

Expand Down Expand Up @@ -250,6 +252,8 @@ pub fn update<K: KvStore, B: Blockchain<K>>(
dst_index,
});
accepted.push(tx);
mirror.update(&isolated.to_ops())?;
state_size = isolated_state_size;
}
}

Expand Down
19 changes: 11 additions & 8 deletions src/mpn/withdraw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,14 +71,15 @@ pub fn withdraw<K: KvStore, B: Blockchain<K>>(
continue;
};

let mut isolated = mirror.mirror();
let mut isolated_state_size = state_size;
if (acc.address != Default::default() && tx.zk_address.0.decompress() != acc.address)
|| !tx.verify_calldata::<ZkHasher>()
|| !tx.verify_signature::<ZkHasher>()
|| tx.zk_nonce != acc.withdraw_nonce + 1
|| tx.payment.amount.token_id != acc_token.token_id
|| tx.payment.amount.amount > acc_token.amount
{
println!("{} {}", tx.zk_nonce, acc.withdraw_nonce);
rejected.push(tx.clone());
continue;
} else {
Expand All @@ -91,7 +92,7 @@ pub fn withdraw<K: KvStore, B: Blockchain<K>>(

let before_token_hash = updated_acc.tokens_hash::<ZkHasher>(log4_token_tree_size);
let token_balance_proof = KvStoreStateManager::<ZkHasher>::prove(
&mirror,
&isolated,
mpn_contract_id,
ZkDataLocator(vec![account_index, 4]),
zk_token_index,
Expand All @@ -100,16 +101,16 @@ pub fn withdraw<K: KvStore, B: Blockchain<K>>(

updated_acc.tokens.get_mut(&zk_token_index).unwrap().amount -= tx.payment.amount.amount;
KvStoreStateManager::<ZkHasher>::set_mpn_account(
&mut mirror,
&mut isolated,
mpn_contract_id,
account_index,
updated_acc.clone(),
&mut state_size,
&mut isolated_state_size,
)
.unwrap();

let fee_balance_proof = KvStoreStateManager::<ZkHasher>::prove(
&mirror,
&isolated,
mpn_contract_id,
ZkDataLocator(vec![account_index, 4]),
zk_fee_token_index,
Expand Down Expand Up @@ -137,19 +138,19 @@ pub fn withdraw<K: KvStore, B: Blockchain<K>>(
.amount -= tx.payment.fee.amount;

let proof = KvStoreStateManager::<ZkHasher>::prove(
&mirror,
&isolated,
mpn_contract_id,
ZkDataLocator(vec![]),
account_index,
)
.unwrap();

KvStoreStateManager::<ZkHasher>::set_mpn_account(
&mut mirror,
&mut isolated,
mpn_contract_id,
account_index,
updated_acc,
&mut state_size,
&mut isolated_state_size,
)
.unwrap();

Expand All @@ -168,6 +169,8 @@ pub fn withdraw<K: KvStore, B: Blockchain<K>>(
before_token_hash,
});
accepted.push(tx);
mirror.update(&isolated.to_ops())?;
state_size = isolated_state_size;
}
}
let next_state =
Expand Down

0 comments on commit 360424b

Please sign in to comment.