Skip to content

Commit

Permalink
Added validation of moved funds sweep proposal
Browse files Browse the repository at this point in the history
  • Loading branch information
tomaszslabon committed Dec 21, 2023
1 parent 3f7d6b3 commit a578602
Showing 1 changed file with 70 additions and 0 deletions.
70 changes: 70 additions & 0 deletions solidity/contracts/bridge/WalletProposalValidator.sol
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import "./BitcoinTx.sol";
import "./Bridge.sol";
import "./Deposit.sol";
import "./Redemption.sol";
import "./MovingFunds.sol";
import "./Wallets.sol";

/// @title Wallet proposal validator.
Expand Down Expand Up @@ -104,6 +105,20 @@ contract WalletProposalValidator {
uint256 movingFundsTxFee;
}

/// @notice Helper structure representing a moved funds sweep proposal.
struct MovedFundsSweepProposal {
// 20-byte public key hash of the wallet.
bytes20 walletPubKeyHash;
// 32-byte hash of the moving funds transaction that caused the sweep
// request to be created.
bytes32 movingFundsTxHash;
// Index of the moving funds transaction output that is subject of the
// sweep request.
uint32 movingFundsTxOutputIndex;
// Proposed BTC fee for the entire transaction.
uint256 movedFundsSweepTxFee;
}

/// @notice Helper structure representing a heartbeat proposal.
struct HeartbeatProposal {
// 20-byte public key hash of the target wallet.
Expand Down Expand Up @@ -662,6 +677,61 @@ contract WalletProposalValidator {
return true;
}

function validateMovedFundsSweepProposal(
MovedFundsSweepProposal calldata proposal
) external view returns (bool) {
Wallets.Wallet memory wallet = bridge.wallets(
proposal.walletPubKeyHash
);

// Make sure the wallet is in Live or MovingFunds state.
require(
wallet.state == Wallets.WalletState.Live ||
wallet.state == Wallets.WalletState.MovingFunds,
"Source wallet is not in Live or MovingFunds state"
);

// Make sure the moved funds sweep request is valid.
uint256 sweepRequestKeyUint = uint256(
keccak256(
abi.encodePacked(
proposal.movingFundsTxHash,
proposal.movingFundsTxOutputIndex
)
)
);

MovingFunds.MovedFundsSweepRequest memory sweepRequest = bridge
.movedFundsSweepRequests(sweepRequestKeyUint);

require(
sweepRequest.state ==
MovingFunds.MovedFundsSweepRequestState.Pending,
"Sweep request is not in Pending state"
);

require(
sweepRequest.walletPubKeyHash == proposal.walletPubKeyHash,
"Sweep request does not belong to the wallet"
);

// Make sure the proposed fee is valid.
(, , , , , , , uint64 movedFundsSweepTxMaxTotalFee, , , ) = bridge
.movingFundsParameters();

require(
proposal.movedFundsSweepTxFee > 0,
"Proposed transaction fee cannot be zero"
);

require(
proposal.movedFundsSweepTxFee <= movedFundsSweepTxMaxTotalFee,
"Proposed transaction fee is too high"
);

return true;
}

/// @notice View function encapsulating the main rules of a valid heartbeat
/// proposal. This function is meant to facilitate the off-chain
/// validation of the incoming proposals. Thanks to it, most
Expand Down

0 comments on commit a578602

Please sign in to comment.