Skip to content

Commit

Permalink
Merge pull request #797 from anoma/cwgoes/fix-shielded-to-shielded
Browse files Browse the repository at this point in the history
Avoid any reads when balance change is 0
  • Loading branch information
cwgoes authored Nov 17, 2022
2 parents 48a6172 + 7c03468 commit 9a7bed2
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 53 deletions.
100 changes: 52 additions & 48 deletions tx_prelude/src/token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,64 +18,68 @@ pub fn transfer(
key: &Option<String>,
shielded: &Option<Transaction>,
) -> TxResult {
let src_key = match &sub_prefix {
Some(sub_prefix) => {
let prefix = token::multitoken_balance_prefix(token, sub_prefix);
token::multitoken_balance_key(&prefix, src)
}
None => token::balance_key(token, src),
};
let dest_key = match &sub_prefix {
Some(sub_prefix) => {
let prefix = token::multitoken_balance_prefix(token, sub_prefix);
token::multitoken_balance_key(&prefix, dest)
}
None => token::balance_key(token, dest),
};
let src_bal: Option<Amount> = match src {
Address::Internal(InternalAddress::IbcMint) => Some(Amount::max()),
Address::Internal(InternalAddress::IbcBurn) => {
log_string("invalid transfer from the burn address");
unreachable!()
}
_ => ctx.read(&src_key)?,
};
let mut src_bal = src_bal.unwrap_or_else(|| {
log_string(format!("src {} has no balance", src_key));
unreachable!()
});
src_bal.spend(&amount);
let mut dest_bal: Amount = match dest {
Address::Internal(InternalAddress::IbcMint) => {
log_string("invalid transfer to the mint address");
unreachable!()
}
_ => ctx.read(&dest_key)?.unwrap_or_default(),
};
dest_bal.receive(&amount);
if src != dest {
match src {
Address::Internal(InternalAddress::IbcMint) => {
ctx.write_temp(&src_key, src_bal)?;
if amount != Amount::default() {
let src_key = match &sub_prefix {
Some(sub_prefix) => {
let prefix =
token::multitoken_balance_prefix(token, sub_prefix);
token::multitoken_balance_key(&prefix, src)
}
None => token::balance_key(token, src),
};
let dest_key = match &sub_prefix {
Some(sub_prefix) => {
let prefix =
token::multitoken_balance_prefix(token, sub_prefix);
token::multitoken_balance_key(&prefix, dest)
}
None => token::balance_key(token, dest),
};
let src_bal: Option<Amount> = match src {
Address::Internal(InternalAddress::IbcMint) => Some(Amount::max()),
Address::Internal(InternalAddress::IbcBurn) => {
log_string("invalid transfer from the burn address");
unreachable!()
}
_ => {
ctx.write(&src_key, src_bal)?;
}
}
match dest {
_ => ctx.read(&src_key)?,
};
let mut src_bal = src_bal.unwrap_or_else(|| {
log_string(format!("src {} has no balance", src_key));
unreachable!()
});
src_bal.spend(&amount);
let mut dest_bal: Amount = match dest {
Address::Internal(InternalAddress::IbcMint) => {
log_string("invalid transfer to the mint address");
unreachable!()
}
Address::Internal(InternalAddress::IbcBurn) => {
ctx.write_temp(&dest_key, dest_bal)?;
_ => ctx.read(&dest_key)?.unwrap_or_default(),
};
dest_bal.receive(&amount);
if src != dest {
match src {
Address::Internal(InternalAddress::IbcMint) => {
ctx.write_temp(&src_key, src_bal)?;
}
Address::Internal(InternalAddress::IbcBurn) => {
log_string("invalid transfer from the burn address");
unreachable!()
}
_ => {
ctx.write(&src_key, src_bal)?;
}
}
_ => {
ctx.write(&dest_key, dest_bal)?;
match dest {
Address::Internal(InternalAddress::IbcMint) => {
log_string("invalid transfer to the mint address");
unreachable!()
}
Address::Internal(InternalAddress::IbcBurn) => {
ctx.write_temp(&dest_key, dest_bal)?;
}
_ => {
ctx.write(&dest_key, dest_bal)?;
}
}
}
}
Expand Down
10 changes: 5 additions & 5 deletions wasm/checksums.json
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
{
"tx_bond.wasm": "tx_bond.2adfcfc7d9c204cd3baba31ba7cd9077378068ef7ab4d9e0adf4884230631a39.wasm",
"tx_ibc.wasm": "tx_ibc.f55f05afa9620b1e67b4cac25145376905f94ea066bc404402967d91205480d7.wasm",
"tx_bond.wasm": "tx_bond.27d0de1ed672ea7b460fb0b0084615b77d821388a4e7c89a7518aebd5ee2349e.wasm",
"tx_ibc.wasm": "tx_ibc.92eb16ca8ea7371c7bd43bb0337db1e9800604919ade679e7a33b705dbd788d8.wasm",
"tx_init_account.wasm": "tx_init_account.7e73c0a0368ec6dda8751456ec55ce2c8770c9b9f5391895d5e140a33edfa850.wasm",
"tx_init_proposal.wasm": "tx_init_proposal.f78e71d4f423ee163ed41a136d535f29beed73d7937e2e8b2e468ec90e755a58.wasm",
"tx_init_proposal.wasm": "tx_init_proposal.d5b88266ce1d1b4d0b800587cf82e93c0b4c7ee58009b980c64771e20670332a.wasm",
"tx_init_validator.wasm": "tx_init_validator.e4f906d19483774f989bdf7ac1a8259745a8f130db3f2c13159c092eaa540a1c.wasm",
"tx_transfer.wasm": "tx_transfer.0058ca2742f1beef62e9067f7245f896730408a6dda072f2c376efc0b99cc1f8.wasm",
"tx_transfer.wasm": "tx_transfer.fd8cb4a45f4712ad2507540be3f67d8f6b54dfcff9828ae55f745c7435e1c539.wasm",
"tx_unbond.wasm": "tx_unbond.1f03df3ff9ce1e5397ddd9862f85652f1d3b34ece7c44755252985fdf759816f.wasm",
"tx_update_vp.wasm": "tx_update_vp.f45e7812c2285cfc4631b68e3dea876faa031f495ec761f3f49b8d1089a5f912.wasm",
"tx_vote_proposal.wasm": "tx_vote_proposal.db2478624772da21445f9e5d5bfce4009b66041a49d3470ffdae4899632ef279.wasm",
"tx_withdraw.wasm": "tx_withdraw.e5d9a390abf858f2c32dd65d401a4341f1205114b4cc541efcb2708cacd6d7db.wasm",
"tx_withdraw.wasm": "tx_withdraw.9a6970db8994157a24a7e9b119e9c829518b17a6ff86b20d5fccad2662f72528.wasm",
"vp_masp.wasm": "vp_masp.626dacdbc61f73d6d860488fba2dfb49cc428629383173f97fbed69417bbd370.wasm",
"vp_testnet_faucet.wasm": "vp_testnet_faucet.12def040a549619eeefbd24321f6275dd343e2b57a6643e0948e098c818cb4f7.wasm",
"vp_token.wasm": "vp_token.91e218a96a9d6f26fc2f3cb8d1e4ff514761bb7ccb07ab7944e616834cdaec77.wasm",
Expand Down

0 comments on commit 9a7bed2

Please sign in to comment.