Skip to content

Commit

Permalink
Added validation of moving funds proposal
Browse files Browse the repository at this point in the history
  • Loading branch information
tomaszslabon committed Dec 13, 2023
1 parent cadead9 commit ab82b17
Show file tree
Hide file tree
Showing 2 changed files with 135 additions and 0 deletions.
50 changes: 50 additions & 0 deletions solidity/contracts/bridge/WalletProposalValidator.sol
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,16 @@ contract WalletProposalValidator {
uint256 redemptionTxFee;
}

/// @notice Helper structure representing a moving funds proposal.
struct MovingFundsProposal {
// 20-byte public key hash of the source wallet.
bytes20 walletPubKeyHash;
// List of 20-byte public key hashes of target wallets.
bytes20[] targetWallets;
// Proposed BTC fee for the entire transaction.
uint256 movingFundsTxFee;
}

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

function validateMovingFundsProposal(MovingFundsProposal calldata proposal)
external
view
returns (bool)
{
require(
bridge.wallets(proposal.walletPubKeyHash).state ==
Wallets.WalletState.MovingFunds,
"Wallet is not in MovingFunds state"
);

(
uint64 movingFundsTxMaxTotalFee,
,
,
,
,
,
,
,
,
,
) = bridge.movingFundsParameters();

//TODO: Validate target wallets.

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

// Make sure the proposed fee does not exceed the total fee limit.
require(
proposal.movingFundsTxFee <= movingFundsTxMaxTotalFee,
"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
85 changes: 85 additions & 0 deletions solidity/test/bridge/WalletProposalValidator.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1894,6 +1894,91 @@ describe("WalletProposalValidator", () => {
})
})

describe("validateMovingFundsProposal", () => {
const walletPubKeyHash = "0x7ac2d9378a1c47e589dfb8095ca95ed2140d2726"
const ecdsaWalletID =
"0x4ad6b3ccbca81645865d8d0d575797a15528e98ced22f29a6f906d3259569863"

before(async () => {
await createSnapshot()

// TODO: Fill with appropriate parameters.
bridge.movingFundsParameters.returns([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0])
})

after(async () => {
bridge.movingFundsParameters.reset()

await restoreSnapshot()
})

context("when wallet's state is not MovingFunds", () => {
const testData = [
{
testName: "when wallet state is Unknown",
walletState: walletState.Unknown,
},
{
testName: "when wallet state is Live",
walletState: walletState.Live,
},
{
testName: "when wallet state is Closing",
walletState: walletState.Closing,
},
{
testName: "when wallet state is Closed",
walletState: walletState.Closed,
},
{
testName: "when wallet state is Terminated",
walletState: walletState.Terminated,
},
]

testData.forEach((test) => {
context(test.testName, () => {
before(async () => {
await createSnapshot()

bridge.wallets.whenCalledWith(walletPubKeyHash).returns({
ecdsaWalletID,
mainUtxoHash: HashZero,
pendingRedemptionsValue: 0,
createdAt: 0,
movingFundsRequestedAt: 0,
closingStartedAt: 0,
pendingMovedFundsSweepRequestsCount: 0,
state: test.walletState,
movingFundsTargetWalletsCommitmentHash: HashZero,
})
})

after(async () => {
bridge.wallets.reset()

await restoreSnapshot()
})

it("should revert", async () => {
await expect(
// Only walletPubKeyHash argument is relevant in this scenario.
walletProposalValidator.validateMovingFundsProposal({
walletPubKeyHash,
movingFundsTxFee: 0,
targetWallets: [],
})
).to.be.revertedWith("Wallet is not in MovingFunds state")
})
})
})
})

context("when wallet's state is MovingFunds", () => {
// TODO: Implement
})
})

describe("validateHeartbeatProposal", () => {
context("when message is not valid", () => {
it("should revert", async () => {
Expand Down

0 comments on commit ab82b17

Please sign in to comment.