Skip to content

Commit

Permalink
Remaining test + fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Fletch153 committed Nov 19, 2024
1 parent e27c382 commit 54d2dd1
Show file tree
Hide file tree
Showing 6 changed files with 349 additions and 15 deletions.
35 changes: 21 additions & 14 deletions contracts/src/v0.8/llo-feeds/v0.5.0/Verifier.sol
Original file line number Diff line number Diff line change
Expand Up @@ -389,22 +389,29 @@ contract Verifier is IVerifier, ConfirmedOwner, TypeAndVersionInterface {
uint64 offchainConfigVersion,
bytes memory offchainConfig
) internal pure returns (bytes32) {

bytes[] memory signersAsBytes = new bytes[](signers.length);
for (uint i; i < signers.length; ++i){
signersAsBytes[i] = abi.encode(signers[i]);
}

uint256 h = uint256(
keccak256(
abi.encode(
configId,
sourceChainId,
sourceAddress,
configCount,
signers,
offchainTransmitters,
f,
onchainConfig,
offchainConfigVersion,
offchainConfig
keccak256(
abi.encode(
configId,
sourceChainId,
sourceAddress,
configCount,
signersAsBytes,
offchainTransmitters,
f,
onchainConfig,
offchainConfigVersion,
offchainConfig
)
)
)
);
);

uint256 prefixMask = type(uint256).max << (256 - 16); // 0xFFFF00..00
// 0x0009 corresponds to ConfigDigestPrefixMercuryV02 in libocr
uint256 prefix = 0x0009 << (256 - 16); // 0x000900..00
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,18 @@ contract BaseFeeManagerTest is Test {
changePrank(originalAddr);
}

function setSubscriberGlobalDiscount(address subscriber, address token, uint256 discount, address sender) public {
//record the current address and switch to the recipient
address originalAddr = msg.sender;
changePrank(sender);

//set the discount
feeManager.updateSubscriberGlobalDiscount(subscriber, token, uint64(discount));

//change back to the original address
changePrank(originalAddr);
}

function setNativeSurcharge(uint256 surcharge, address sender) public {
//record the current address and switch to the recipient
address originalAddr = msg.sender;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -603,4 +603,118 @@ contract FeeManagerProcessFeeTest is BaseFeeManagerTest {
//fee should be half the default
assertEq(discount, FEE_SCALAR / 2);
}

function test_GlobalDiscountWithNative() public {
//set the global discount to 50%
setSubscriberGlobalDiscount(USER, address(native), FEE_SCALAR / 2, ADMIN);

//get the discount applied
uint256 discount = getAppliedDiscount(getV3Report(DEFAULT_FEED_1_V3), getNativeQuote(), USER);

//fee should be half the default
assertEq(discount, FEE_SCALAR / 2);
}

function test_GlobalDiscountWithLink() public {
//set the global discount to 50%
setSubscriberGlobalDiscount(USER, address(link), FEE_SCALAR / 2, ADMIN);

//get the discount applied
uint256 discount = getAppliedDiscount(getV3Report(DEFAULT_FEED_1_V3), getLinkQuote(), USER);

//fee should be half the default
assertEq(discount, FEE_SCALAR / 2);
}

function test_GlobalDiscountWithNativeAndLink() public {
//set the global discount to 50%
setSubscriberGlobalDiscount(USER, address(native), FEE_SCALAR / 2, ADMIN);
setSubscriberGlobalDiscount(USER, address(link), FEE_SCALAR / 2, ADMIN);

//get the discount applied
uint256 discount = getAppliedDiscount(getV3Report(DEFAULT_FEED_1_V3), getLinkQuote(), USER);

//fee should be half the default
assertEq(discount, FEE_SCALAR / 2);
}

function test_GlobalDiscountIsOverridenByIndividualDiscountNative() public {
//set the global discount to 50%
setSubscriberGlobalDiscount(USER, address(native), FEE_SCALAR / 2, ADMIN);
setSubscriberDiscount(USER, DEFAULT_FEED_1_V3, address(native), FEE_SCALAR / 4, ADMIN);

//get the discount applied
uint256 discount = getAppliedDiscount(getV3Report(DEFAULT_FEED_1_V3), getNativeQuote(), USER);

//fee should be half the default
assertEq(discount, FEE_SCALAR / 4);
}

function test_GlobalDiscountIsOverridenByIndividualDiscountLink() public {
//set the global discount to 50%
setSubscriberGlobalDiscount(USER, address(link), FEE_SCALAR / 2, ADMIN);
setSubscriberDiscount(USER, DEFAULT_FEED_1_V3, address(link), FEE_SCALAR / 4, ADMIN);

//get the discount applied
uint256 discount = getAppliedDiscount(getV3Report(DEFAULT_FEED_1_V3), getLinkQuote(), USER);

//fee should be half the default
assertEq(discount, FEE_SCALAR / 4);
}

function test_GlobalDiscountIsUpdatedAfterBeingSetToZeroLink() public {
//set the global discount to 50%
setSubscriberGlobalDiscount(USER, address(link), FEE_SCALAR / 2, ADMIN);

//get the discount applied
uint256 discount = getAppliedDiscount(getV3Report(DEFAULT_FEED_1_V3), getLinkQuote(), USER);

//fee should be half the default
assertEq(discount, FEE_SCALAR / 2);

//set the global discount to zero
setSubscriberGlobalDiscount(USER, address(link), 0, ADMIN);

//get the discount applied
discount = getAppliedDiscount(getV3Report(DEFAULT_FEED_1_V3), getLinkQuote(), USER);

//fee should be zero
assertEq(discount, 0);
}

function test_GlobalDiscountIsUpdatedAfterBeingSetToZeroNative() public {
//set the global discount to 50%
setSubscriberGlobalDiscount(USER, address(native), FEE_SCALAR / 2, ADMIN);

//get the discount applied
uint256 discount = getAppliedDiscount(getV3Report(DEFAULT_FEED_1_V3), getNativeQuote(), USER);

//fee should be half the default
assertEq(discount, FEE_SCALAR / 2);

//set the global discount to zero
setSubscriberGlobalDiscount(USER, address(native), 0, ADMIN);

//get the discount applied
discount = getAppliedDiscount(getV3Report(DEFAULT_FEED_1_V3), getNativeQuote(), USER);

//fee should be zero
assertEq(discount, 0);
}

function test_GlobalDiscountCantBeSetToMoreThanMaximum() public {
//should revert with invalid discount
vm.expectRevert(INVALID_DISCOUNT_ERROR);

//set the global discount to 101%
setSubscriberGlobalDiscount(USER, address(native), FEE_SCALAR + 1, ADMIN);
}

function test_onlyOwnerCanSetGlobalDiscount() public {
//should revert with unauthorized
vm.expectRevert(ONLY_CALLABLE_BY_OWNER_ERROR);

//set the global discount to 50%
setSubscriberGlobalDiscount(USER, address(native), FEE_SCALAR / 2, USER);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
// SPDX-License-Identifier: MIT
pragma solidity 0.8.19;

// Derived from v0.5/Configurator.sol
contract MockConfigurator {
struct ConfigurationState {
uint64 configCount;
uint32 latestConfigBlockNumber;
bytes32 configDigest;
}

mapping(bytes32 => ConfigurationState) public s_configurationStates;

function setStagingConfig(
bytes32 configId,
bytes[] memory signers,
bytes32[] memory offchainTransmitters,
uint8 f,
bytes memory onchainConfig,
uint64 offchainConfigVersion,
bytes memory offchainConfig
) external {
ConfigurationState storage configurationState = s_configurationStates[configId];

uint64 newConfigCount = ++configurationState.configCount;

bytes32 configDigest = _configDigestFromConfigData(
configId,
block.chainid,
address(this),
newConfigCount,
signers,
offchainTransmitters,
f,
onchainConfig,
offchainConfigVersion,
offchainConfig
);

s_configurationStates[configId].configDigest = configDigest;
configurationState.latestConfigBlockNumber = uint32(block.number);
}

function _configDigestFromConfigData(
bytes32 configId,
uint256 sourceChainId,
address sourceAddress,
uint64 configCount,
bytes[] memory signers,
bytes32[] memory offchainTransmitters,
uint8 f,
bytes memory onchainConfig,
uint64 offchainConfigVersion,
bytes memory offchainConfig
) internal pure returns (bytes32) {
uint256 h = uint256(
keccak256(
abi.encode(
configId,
sourceChainId,
sourceAddress,
configCount,
signers,
offchainTransmitters,
f,
onchainConfig,
offchainConfigVersion,
offchainConfig
)
)
);
uint256 prefixMask = type(uint256).max << (256 - 16);
uint256 prefix = 0x0009 << (256 - 16);
return bytes32((prefix & prefixMask) | (h & ~prefixMask));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -191,14 +191,21 @@ contract BaseTest is Test {
uint64 offchainConfigVersion,
bytes memory offchainConfig
) internal pure returns (bytes32) {

// Convert addresses to bytes array to match configurator
bytes[] memory signersAsBytes = new bytes[](signers.length);
for (uint i; i < signers.length; ++i){
signersAsBytes[i] = abi.encode(signers[i]);
}

uint256 h = uint256(
keccak256(
abi.encode(
feedId,
chainId,
verifierAddr,
configCount,
signers,
signersAsBytes,
offchainTransmitters,
f,
onchainConfig,
Expand Down
Loading

0 comments on commit 54d2dd1

Please sign in to comment.