Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refs: #749 Here we expose the `revealDepositWithExtraData` function in the `Bridge` contract. This function allows revealing a deposit whose funding transaction embeds arbitrary 32-byte extra data. The new function opens a variety of use cases. An EOA depositor can craft a Bitcoin deposit transaction that allows a third-party smart contract to manage the deposit and provide additional services on top. In this case, 32-byte extra data can be used to securely attribute the deposit to the original EOA depositor. ### The `revealDepositWithExtraData` function The `revealDepositWithExtraData` was added next to the existing `revealDeposit` function to provide backward compatibility of the `Bridge` contract API. The `Deposit` library that does the heavy lifting was refactored to handle both regular deposits without extra data and the new deposits with extra data. The common logic was extracted to the `_revealDeposit` internal function to avoid duplication. Worth noting, the extra data passed in the new flow has to be externalized so tBTC wallets can reconstruct the deposit script and sweep those deposits, just as the regular ones. To do so, we decided to store the extra data in the `Bridge` storage (as part of the `DepositRequest` structure). This approach is not ideal but has some advantages: - Negligible gas overhead for regular deposits without extra data (~2000 of gas) - Easier integration as extra data can be easily fetched from storage by tBTC wallets and third-party smart contracts - Making the `Bridge` aware of the extra data may open some use cases in the future Alternative approaches rely on emitting the extra data using contract events. However: - Extending the existing `DepositReveal` event is not backward compatible. The event signature would change and clients trying to fetch past events that don't contain the extra data field would fail. This is especially problematic for tBTC wallets, SPV maintainers, and block explorers - Using a new event just for emitting the extra data is clunky and makes the integration experience significantly worse ### Changes in the `WalletProposalValidator` contract The `WalletProposalValidator` contract is used by tBTC off-chain wallet clients to validate incoming deposit sweep proposals. This contract needs to reconstruct deposit scripts so must be aware of the new alternative format with 32-byte extra data. As part of this pull request, we are adjusting this contract to properly validate deposit sweep proposals that include deposits with extra data. ### Impact on gas costs and contract size Before the presented change, the gas costs of functions and size of affected contract/libraries was as follows: ``` ·------------------------------|---------------------------|--------------|-----------------------------· | Solc version: 0.8.17 · Optimizer enabled: true · Runs: 1000 · Block limit: 30000000 gas │ ·······························|···························|··············|······························ | Methods │ ·············|·················|·············|·············|··············|···············|·············· | Contract · Method · Min · Max · Avg · # calls · eur (avg) │ ·············|·················|·············|·············|··············|···············|·············· | Bridge · revealDeposit · 82767 · 105463 · 102057 · 170 · - │ ·············|·················|·············|·············|··············|···············|·············· ``` ``` ·-------------------------------|-------------|---------------· | Contract Name · Size (KB) · Change (KB) │ ································|·············|················ | Bridge · 21.264 · │ ································|·············|················ | Deposit · 6.327 · │ ································|·············|················ | WalletProposalValidator · 11.266 · │ ································|·············|················ ``` After introducing `revealDepositWithExtraData`, those are as follows: ``` ·------------------------------·············|---------------------------|--------------|-----------------------------· | Solc version: 0.8.17 · Optimizer enabled: true · Runs: 1000 · Block limit: 30000000 gas │ ············································|···························|··············|······························ | Methods │ ·············|······························|·············|·············|··············|···············|·············· | Contract · Method · Min · Max · Avg · # calls · eur (avg) │ ·············|······························|·············|·············|··············|···············|·············· | Bridge · revealDeposit · 85270 · 107966 · 104562 · 170 · - │ ·············|······························|·············|·············|··············|···············|·············· | Bridge · revealDepositWithExtraData · 124176 · 126773 · 125872 · 12 · - │ ·············|······························|·············|·············|··············|···············|·············· ``` ``` ·-------------------------------|-------------|---------------· | Contract Name · Size (KB) · Change (KB) │ ································|·············|················ | Bridge · 21.563 · │ ································|·············|················ | Deposit · 6.894 · │ ································|·············|················ | WalletProposalValidator · 11.520 · │ ································|·············|················ ``` ### Next steps This change is the most important one towards full support of deposits with extra data. However, comprehensive support requires some additional steps: - Add support in the tBTC off-chain client - Add support in the tBTC SDK
- Loading branch information