Skip to content

Commit d78ba32

Browse files
hrishibhatjaybuidl
authored andcommitted
Implementation of challenge-path functions
1 parent 1afc05b commit d78ba32

File tree

2 files changed

+50
-7
lines changed

2 files changed

+50
-7
lines changed

contracts/src/bridge/FastBridgeReceiverOnEthereum.sol

Lines changed: 48 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,23 @@ contract FastBridgeReceiverOnEthereum is SafeBridgeReceiverOnEthereum, IFastBrid
2929
bool relayed;
3030
}
3131

32+
struct Challenge {
33+
address challenger;
34+
uint256 challengedAt;
35+
uint256 challengeDeposit;
36+
bool relayed;
37+
}
38+
3239
// ************************************* //
3340
// * Storage * //
3441
// ************************************* //
3542

3643
uint256 public override claimDeposit;
3744
uint256 public override challengeDeposit;
3845
uint256 public override challengeDuration;
46+
uint256 public override safeBridgeTimeout;
3947
mapping(bytes32 => Claim) public claims; // The claims by message hash.
48+
mapping(bytes32 => Challenge) public challenges; // The challenges by message hash.
4049

4150
// ************************************* //
4251
// * Events * //
@@ -51,11 +60,13 @@ contract FastBridgeReceiverOnEthereum is SafeBridgeReceiverOnEthereum, IFastBrid
5160
address _inbox,
5261
uint256 _claimDeposit,
5362
uint256 _challengeDeposit,
54-
uint256 _challengeDuration
63+
uint256 _challengeDuration,
64+
uint256 _safeBridgeTimeout
5565
) SafeBridgeReceiverOnEthereum(_governor, _safeBridgeSender, _inbox) {
5666
claimDeposit = _claimDeposit;
5767
challengeDeposit = _challengeDeposit;
5868
challengeDuration = _challengeDuration;
69+
safeBridgeTimeout - _safeBridgeTimeout;
5970
}
6071

6172
// ************************************* //
@@ -77,8 +88,20 @@ contract FastBridgeReceiverOnEthereum is SafeBridgeReceiverOnEthereum, IFastBrid
7788
}
7889

7990
function challenge(bytes32 _messageHash) external payable override {
80-
// TODO
81-
revert("Not Implemented");
91+
Claim memory claim = claims[_messageHash];
92+
require(claim.bridger != address(0), "Claim does not exist");
93+
require(block.timestamp - claim.claimedAt < challengeDuration, "Challenge period over");
94+
require(msg.value >= challengeDeposit, "Not enough challenge deposit");
95+
require(challenges[_messageHash].challenger == address(0), "Claim already challenged");
96+
97+
challenges[_messageHash] = Challenge({
98+
challenger: msg.sender,
99+
challengedAt: block.timestamp,
100+
challengeDeposit: msg.value,
101+
relayed: false
102+
});
103+
104+
emit ClaimChallenged(_messageHash, block.timestamp);
82105
}
83106

84107
function verifyAndRelay(bytes32 _messageHash, bytes memory _encodedData) external override {
@@ -100,8 +123,17 @@ contract FastBridgeReceiverOnEthereum is SafeBridgeReceiverOnEthereum, IFastBrid
100123
function verifyAndRelaySafe(bytes32 _messageHash, bytes memory _encodedData) external override {
101124
require(isSentBySafeBridge(), "Access not allowed: SafeBridgeSender only.");
102125

103-
// TODO
104-
revert("Not Implemented");
126+
Challenge storage challenge = challenges[_messageHash];
127+
Claim storage claim = claims[_messageHash];
128+
require(claim.relayed != true, "Claim already relayed");
129+
require(challenge.relayed != true, "Challenge already relayed");
130+
131+
// Decode the receiver address from the data encoded by the SafeBridgeSenderToEthereum
132+
(address receiver, bytes memory data) = abi.decode(_encodedData, (address, bytes));
133+
(bool success, ) = address(receiver).call(data);
134+
require(success, "Failed to call contract");
135+
136+
challenge.relayed == true;
105137
}
106138

107139
function withdrawClaimDeposit(bytes32 _messageHash) external override {
@@ -115,8 +147,13 @@ contract FastBridgeReceiverOnEthereum is SafeBridgeReceiverOnEthereum, IFastBrid
115147
}
116148

117149
function withdrawChallengeDeposit(bytes32 _messageHash) external override {
118-
// TODO
119-
revert("Not Implemented");
150+
Challenge storage challenge = challenges[_messageHash];
151+
require(challenge.challenger != address(0), "Challenge does not exist");
152+
require(challenge.relayed == true || block.timestamp > challenge.challengedAt + safeBridgeTimeout, "Challenge not relayed or timed out");
153+
154+
uint256 amount = challenge.challengeDeposit + claims[_messageHash].claimDeposit;
155+
challenge.challengeDeposit = 0;
156+
payable(challenge.challenger).send(amount);
120157
}
121158

122159
// ************************************* //
@@ -147,4 +184,8 @@ contract FastBridgeReceiverOnEthereum is SafeBridgeReceiverOnEthereum, IFastBrid
147184
function setChallengePeriodDuration(uint256 _challengeDuration) external onlyByGovernor {
148185
challengeDuration = _challengeDuration;
149186
}
187+
188+
function setSafeBridgeTimeout(uint256 _safeBridgeTimeout) external onlyByGovernor {
189+
safeBridgeTimeout = _safeBridgeTimeout;
190+
}
150191
}

contracts/src/bridge/interfaces/IFastBridgeReceiver.sol

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,6 @@ interface IFastBridgeReceiver {
2020
function challengeDeposit() external view returns (uint256 amount);
2121

2222
function challengeDuration() external view returns (uint256 amount);
23+
24+
function safeBridgeTimeout() external view returns (uint256 amount);
2325
}

0 commit comments

Comments
 (0)