trezor / blockbook

:blue_book: Trezor address/account balance backend
https://trezor.io
GNU Affero General Public License v3.0
671 stars 654 forks source link

[Zcash] [Bug] Fees are incorrect for transactions with shielded components #1160

Open daira opened 4 days ago

daira commented 4 days ago

This explorer is calculating fees incorrectly for Zcash transactions with shielded components.

Example: https://zecblockexplorer.com/tx/63679fc7f9628bc5065636813ee6adbe89f6dfdf3635524ac0792b344a9fd588 shows a fee of 72.56179835 ZEC. In fact this is a shielding transaction which transfers 72.56024835 ZEC to the shielded pool. The remaining 72.56179835 - 72.56024835 = 0.00155 ZEC is the fee.

That transaction is shown correctly by other block explorers at https://mainnet.zcashexplorer.app/transactions/63679fc7f9628bc5065636813ee6adbe89f6dfdf3635524ac0792b344a9fd588 and https://3xpl.com/zcash/transaction/63679fc7f9628bc5065636813ee6adbe89f6dfdf3635524ac0792b344a9fd588 .

In order to calculate the fee correctly in all cases:

That is, a negative valueBalanceSapling or valueBalanceOutput, or a nonzero value in any vpub_old, represents a net transfer into the corresponding shielded pool, and is treated like an output of that magnitude when calculating the fee. A positive valueBalanceSapling or valueBalanceOutput, or a nonzero value in any vpub_new, represents a net transfer out of the corresponding shielded pool, and is treated like an input of that magnitude when calculating the fee.

(The transaction given as an example is a v4 transaction that only involves Sapling. I can give other examples that involve the Sprout and Orchard pools if needed.)