Skip to content

Commit 31d3ba6

Browse files
committed
track gas usage in the recovery flow
1 parent e7d5569 commit 31d3ba6

File tree

2 files changed

+16
-20
lines changed

2 files changed

+16
-20
lines changed

target_chains/ethereum/contracts/contracts/entropy/Entropy.sol

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -669,15 +669,18 @@ abstract contract Entropy is IEntropy, EntropyState {
669669
}
670670
} else {
671671
// This case uses the checks-effects-interactions pattern to avoid reentry attacks
672+
address callAddress = req.requester;
673+
EntropyStructs.Request memory reqV1 = EntropyStructConverter
674+
.toV1Request(req);
672675
clearRequest(provider, sequenceNumber);
676+
// WARNING: DO NOT USE req BELOW HERE AS ITS CONTENTS HAS BEEN CLEARED
673677

674678
// Check if the requester is a contract account.
675679
uint len;
676-
address callAddress = req.requester;
677680
assembly {
678681
len := extcodesize(callAddress)
679682
}
680-
uint256 startingGas = gasleft();
683+
uint256 startingGas = gasleft();
681684
if (len != 0) {
682685
IEntropyConsumer(callAddress)._entropyCallback(
683686
sequenceNumber,
@@ -688,15 +691,15 @@ abstract contract Entropy is IEntropy, EntropyState {
688691
uint32 gasUsed = SafeCast.toUint32(startingGas - gasleft());
689692

690693
emit RevealedWithCallback(
691-
EntropyStructConverter.toV1Request(req),
694+
reqV1,
692695
userRandomNumber,
693696
providerRevelation,
694697
randomNumber
695698
);
696699
emit EntropyEventsV2.Revealed(
697700
provider,
698-
req.requester,
699-
req.sequenceNumber,
701+
callAddress,
702+
sequenceNumber,
700703
randomNumber,
701704
false,
702705
bytes(""),

target_chains/ethereum/contracts/forge-test/Entropy.t.sol

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1778,7 +1778,6 @@ contract EntropyTest is Test, EntropyTestUtils, EntropyEvents, EntropyEventsV2 {
17781778
// can never cause a callback to fail because it runs out of gas.
17791779
vm.prank(provider1);
17801780
random.setDefaultGasLimit(0);
1781-
17821781
assertCallbackResult(0, 190000, true);
17831782
assertCallbackResult(0, 210000, true);
17841783
assertCallbackResult(300000, 290000, true);
@@ -1864,12 +1863,13 @@ contract EntropyTest is Test, EntropyTestUtils, EntropyEvents, EntropyEventsV2 {
18641863
assertEq(callbackFailed, true);
18651864
assertEq(callbackErrorCode, bytes(""));
18661865

1867-
// callback gas usage is approximate and only triggered when the provider has set a gas limit.
1868-
// Note: this condition is somewhat janky, but we hit the stack limit so can't put in any more local variables :(
1869-
assertTrue(
1866+
// callback gas usage is approximate
1867+
assertTrue(
1868+
random.getProviderInfoV2(provider1).defaultGasLimit == 0 ||
18701869
((callbackGasUsage * 90) / 100 < callbackGasUsed)
18711870
);
1872-
assertTrue(
1871+
assertTrue(
1872+
random.getProviderInfoV2(provider1).defaultGasLimit == 0 ||
18731873
(callbackGasUsed < (callbackGasUsage * 110) / 100)
18741874
);
18751875
assertEq(extraArgs, bytes(""));
@@ -1939,16 +1939,9 @@ contract EntropyTest is Test, EntropyTestUtils, EntropyEvents, EntropyEventsV2 {
19391939
);
19401940
assertEq(callbackFailed, false);
19411941
assertEq(callbackErrorCode, bytes(""));
1942-
// callback gas usage is approximate and only triggered when the provider has set a gas limit
1943-
// Note: this condition is somewhat janky, but we hit the stack limit so can't put in any more local variables :(
1944-
assertTrue(
1945-
random.getProviderInfoV2(provider1).defaultGasLimit == 0 ||
1946-
((callbackGasUsage * 90) / 100 < callbackGasUsed)
1947-
);
1948-
assertTrue(
1949-
random.getProviderInfoV2(provider1).defaultGasLimit == 0 ||
1950-
(callbackGasUsed < (callbackGasUsage * 110) / 100)
1951-
);
1942+
// callback gas usage is approximate
1943+
assertTrue((callbackGasUsage * 90) / 100 < callbackGasUsed);
1944+
assertTrue(callbackGasUsed < (callbackGasUsage * 110) / 100);
19521945
assertEq(extraArgs, bytes(""));
19531946

19541947
// Verify request is cleared after successful callback

0 commit comments

Comments
 (0)