@@ -80,6 +80,10 @@ import "@pythnetwork/entropy-sdk-solidity/EntropyStatusConstants.sol";
8080abstract  contract  Entropy  is  IEntropy , EntropyState  {
8181    using ExcessivelySafeCall   for  address ;
8282
83+     uint32  public constant  TEN_THOUSAND =  10000 ;
84+     uint32  public constant  MAX_GAS_LIMIT = 
85+         uint32 (type (uint16 ).max) *  TEN_THOUSAND;
86+ 
8387    function _initialize  (
8488        address  admin ,
8589        uint128  pythFeeInWei ,
@@ -267,7 +271,7 @@ abstract contract Entropy is IEntropy, EntropyState {
267271            //    they still pay for the full gas limit. So we may as well give them the full limit here. 
268272            // 2. If a provider has a defaultGasLimit != 0, we need to ensure that all requests have a >0 gas limit 
269273            //    so that we opt-in to the new callback failure state flow. 
270-             req.gasLimit10k =  roundGas (
274+             req.gasLimit10k =  roundTo10kGas (
271275                callbackGasLimit <  providerInfo.defaultGasLimit
272276                    ?  providerInfo.defaultGasLimit
273277                    :  callbackGasLimit
@@ -546,7 +550,7 @@ abstract contract Entropy is IEntropy, EntropyState {
546550                // than the indicated gas limit. (See CALL opcode docs here https://www.evm.codes/?fork=cancun#f1) 
547551                // Consequently, out-of-gas reverts need to be handled carefully to ensure that the callback 
548552                // was truly provided with a sufficient amount of gas. 
549-                 uint256 (req.gasLimit10k) *  10000 ,
553+                 uint256 (req.gasLimit10k) *  TEN_THOUSAND ,
550554                256 , // copy at most 256 bytes of the return value into ret. 
551555                abi.encodeWithSelector (
552556                    IEntropyConsumer._entropyCallback.selector ,
@@ -568,7 +572,8 @@ abstract contract Entropy is IEntropy, EntropyState {
568572                clearRequest (provider, sequenceNumber);
569573            } else  if  (
570574                ret.length  >  0  || 
571-                 (startingGas *  31 ) /  32  >  uint256 (req.gasLimit10k) *  10000 
575+                 (startingGas *  31 ) /  32  > 
576+                 uint256 (req.gasLimit10k) *  TEN_THOUSAND
572577            ) {
573578                // The callback reverted for some reason. 
574579                // If ret.length > 0, then we know the callback manually triggered a revert, so it's safe to mark it as failed. 
@@ -668,7 +673,7 @@ abstract contract Entropy is IEntropy, EntropyState {
668673        // This approach may be somewhat simplistic, but it allows us to continue using the 
669674        // existing feeInWei parameter for the callback failure flow instead of defining new 
670675        // configuration values. 
671-         uint32  roundedGasLimit =  uint32 (roundGas (gasLimit)) *  10000 ;
676+         uint32  roundedGasLimit =  uint32 (roundTo10kGas (gasLimit)) *  TEN_THOUSAND ;
672677        if  (
673678            provider.defaultGasLimit >  0  && 
674679            roundedGasLimit >  provider.defaultGasLimit
@@ -787,6 +792,10 @@ abstract contract Entropy is IEntropy, EntropyState {
787792            revert  EntropyErrors.NoSuchProvider ();
788793        }
789794
795+         // Check that we can round the gas limit into the 10k gas. This reverts 
796+         // if the provided value exceeds the max. 
797+         roundTo10kGas (gasLimit);
798+ 
790799        uint32  oldGasLimit =  provider.defaultGasLimit;
791800        provider.defaultGasLimit =  gasLimit;
792801        emit  ProviderDefaultGasLimitUpdated (msg .sender , oldGasLimit, gasLimit);
@@ -810,11 +819,16 @@ abstract contract Entropy is IEntropy, EntropyState {
810819
811820    // Rounds the provided quantity of gas into units of 10k gas. 
812821    // If gas is not evenly divisible by 10k, rounds up. 
813-     function roundGas  (uint32  gas ) internal  pure  returns  (uint16 ) {
814-         uint32  gas10k =  gas /  10000 ;
815-         if  (gas10k *  10000  <  gas) {
822+     function roundTo10kGas  (uint32  gas ) internal  pure  returns  (uint16 ) {
823+         if  (gas >  MAX_GAS_LIMIT) {
824+             revert  EntropyErrors.MaxGasLimitExceeded ();
825+         }
826+ 
827+         uint32  gas10k =  gas /  TEN_THOUSAND;
828+         if  (gas10k *  TEN_THOUSAND <  gas) {
816829            gas10k +=  1 ;
817830        }
831+         // Note: safe cast here should never revert due to the if statement above. 
818832        return  SafeCast.toUint16 (gas10k);
819833    }
820834
0 commit comments