-
Notifications
You must be signed in to change notification settings - Fork 27
/
ReentrancyGuards.sol
50 lines (44 loc) · 2 KB
/
ReentrancyGuards.sol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
// (c) 2023, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
// SPDX-License-Identifier: Ecosystem
pragma solidity 0.8.25;
/**
* @dev Abstract contract that helps implement reentrancy guards between functions for sending and receiving.
*
* Consecutive calls for sending functions should work together, same for receive functions, but recursive calls
* should be detected as a reentrancy and revert.
*
* Calls between send and receive functions should also be allowed, but not in the case it ends up being a recursive
* send or receive call. For example the following should fail: send -> receive -> send.
*
* @custom:security-contact https://github.com/ava-labs/icm-contracts/blob/main/SECURITY.md
*/
abstract contract ReentrancyGuards {
// Send and Receive reentrancy guards
uint256 internal constant _NOT_ENTERED = 1;
uint256 internal constant _ENTERED = 2;
uint256 private _sendEntered;
uint256 private _receiveEntered;
// senderNonReentrant modifier makes sure we can not reenter between sender calls.
// This modifier should be used for messenger sender functions that have external calls and do not want to allow
// recursive calls with other sender functions.
modifier senderNonReentrant() {
require(_sendEntered == _NOT_ENTERED, "ReentrancyGuards: sender reentrancy");
_sendEntered = _ENTERED;
_;
_sendEntered = _NOT_ENTERED;
}
// receiverNonReentrant modifier makes sure we can not reenter between receiver calls.
// This modifier should be used for messenger receiver functions that have external calls and do not want to allow
// recursive calls with other receiver functions.
modifier receiverNonReentrant() {
require(_receiveEntered == _NOT_ENTERED, "ReentrancyGuards: receiver reentrancy");
_receiveEntered = _ENTERED;
_;
_receiveEntered = _NOT_ENTERED;
}
constructor() {
_sendEntered = _NOT_ENTERED;
_receiveEntered = _NOT_ENTERED;
}
}