Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: verify transaction metadata (#4078)(PRO-819) #4078

Merged
merged 20 commits into from
Oct 25, 2023

Conversation

Janislav
Copy link
Contributor

@Janislav Janislav commented Oct 6, 2023

Pull Request

Closes: PRO-819

Checklist

Please conduct a thorough self-review before opening the PR.

  • I am confident that the code works.
  • I have updated documentation where appropriate.

Summary

Please include a succinct description of the purpose and content of the PR. What problem does it solve, and how? Link issues, discussions, other PRs, and anything else that will help the reviewer.

@linear
Copy link

linear bot commented Oct 6, 2023

PRO-819 Broadcast confirmation for EVM transactions

When we request a transaction broadcast for EVM, we are trusting the validators to sign and send a transaction without tampering.

We also set a very high cap on the gas limit for most transactions (15M gas).

There is a possible attack vector whereby a validator could create a 'forwarding' contract that consumes any excess gas up to the limit, and forwards the calldata to the chainflip contract. The net effect would be that the KeyManager emits the correct event and we witness the transaction as succeeded, and register the consumed gas to be refunded to the validator.

A validator could use this to drain funds from the vault (assuming we do refund the validator, which is something we intend to do!).

As a solution, we should re-instate the signed transaction checks for these cases: in order to become whitelisted for a fee refund, a validator must prove that they signed the specific transaction without altering the transaction details.

(Specific transaction details for the evm case are

See https://github.com/chainflip-io/chainflip-backend/blob/chore/mainnet-ccm-gaslimit-test/state-chain/chains/src/evm.rs#L422)

Initial outline here:

#3968

@codecov
Copy link

codecov bot commented Oct 6, 2023

Codecov Report

Merging #4078 (ee0f7f8) into main (194cd53) will decrease coverage by 0%.
The diff coverage is 70%.

@@          Coverage Diff           @@
##            main   #4078    +/-   ##
======================================
- Coverage     72%     72%    -0%     
======================================
  Files        378     378            
  Lines      61153   61335   +182     
  Branches   61153   61335   +182     
======================================
+ Hits       43876   43998   +122     
- Misses     15007   15062    +55     
- Partials    2270    2275     +5     
Files Coverage Δ
state-chain/chains/src/any.rs 0% <ø> (ø)
state-chain/chains/src/btc.rs 85% <ø> (ø)
state-chain/chains/src/dot.rs 41% <ø> (ø)
state-chain/chains/src/eth.rs 72% <ø> (ø)
state-chain/chains/src/none.rs 0% <ø> (ø)
state-chain/pallets/cf-broadcast/src/mock.rs 93% <100%> (+<1%) ⬆️
state-chain/pallets/cf-broadcast/src/tests.rs 97% <100%> (+<1%) ⬆️
engine/src/witness/btc.rs 27% <0%> (-<1%) ⬇️
engine/src/witness/dot.rs 29% <0%> (-<1%) ⬇️
state-chain/chains/src/evm.rs 63% <98%> (+5%) ⬆️
... and 8 more

... and 2 files with indirect coverage changes

📣 We’re building smart automated test selection to slash your CI/CD build times. Learn more

@Janislav Janislav marked this pull request as ready for review October 12, 2023 11:38
@Janislav Janislav requested a review from dandanlen October 12, 2023 11:48
engine/src/witness/eth/key_manager.rs Outdated Show resolved Hide resolved
engine/src/witness/eth/key_manager.rs Show resolved Hide resolved
state-chain/chains/src/eth.rs Outdated Show resolved Hide resolved
state-chain/chains/src/eth.rs Outdated Show resolved Hide resolved
state-chain/chains/src/eth.rs Outdated Show resolved Hide resolved
// There is no need for replay protection on Bitcoin since it is a UTXO chain.
type ReplayProtectionParams = ();
type ReplayProtection = ();
}

pub struct BitcoinTransactionMetaDataHandler;

impl TransactionMetaDataHandler<Bitcoin> for BitcoinTransactionMetaDataHandler {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This could just be a default blanket impl on ().

impl<C: Chain> TransactionMetaDataHandler<C> for () {
	// ...
}

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did this, it removes a lot of code.

state-chain/pallets/cf-broadcast/src/lib.rs Outdated Show resolved Hide resolved
state-chain/chains/src/lib.rs Outdated Show resolved Hide resolved
Comment on lines 579 to 582
log::warn!(
"Transaction metadata verification failed for broadcast {}. Validator can not get refunded.",
broadcast_id
);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could have an event for this too - ValidatorRefundRefused?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I renamed these events to TransactionFeeDeficitRecorded/TransactionFeeDeficitRefused.

I think it makes it clearer that (a) it's not always a validator (btc and dot fees are paid by the vault) and (b) it's not refunded, just recorded for later possible refunds.

state-chain/pallets/cf-broadcast/src/tests.rs Outdated Show resolved Hide resolved
@Janislav
Copy link
Contributor Author

I've implemented the required changes or explained why I think we should not change it.

- Consistent naming MetaData -> Metadata
- Remove redundant trait defs
- Factor out some repetition
- Define trait in terms of Self
- Define generic EvmTransactionMetadata
- Remove storage at cleanup
- Rename events to reflect that we don't refund
  and that it's not always a validator.
@dandanlen
Copy link
Collaborator

I made some refactors and added a test. LGTM, thanks!

@dandanlen
Copy link
Collaborator

Actually before merging, @kylezs can you sanity check the engine changes please?

@dandanlen dandanlen requested a review from kylezs October 20, 2023 14:16
Copy link
Contributor

@kylezs kylezs left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah looks fine CFE side

engine/src/witness/btc.rs Outdated Show resolved Hide resolved
@dandanlen dandanlen enabled auto-merge (squash) October 23, 2023 14:03
@dandanlen dandanlen disabled auto-merge October 23, 2023 14:03
@dandanlen dandanlen changed the title feature/PRO-819/confirm-evm-transaction feat: verify transaction metadata (#4078)(PRO-819) Oct 23, 2023
@dandanlen dandanlen enabled auto-merge (squash) October 23, 2023 14:04
@dandanlen dandanlen merged commit 0359e25 into main Oct 25, 2023
42 checks passed
@dandanlen dandanlen deleted the feature/PRO-819/confirm-evm-transaction branch October 25, 2023 09:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants