Skip to content

Commit

Permalink
fix: introduce constant in tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Mikelle committed Nov 21, 2024
1 parent 32fbd28 commit e7e2440
Show file tree
Hide file tree
Showing 11 changed files with 137 additions and 40 deletions.
13 changes: 13 additions & 0 deletions contracts-abi/abi/BidderRegistry.abi
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,19 @@
],
"stateMutability": "view"
},
{
"type": "function",
"name": "PRECISION",
"inputs": [],
"outputs": [
{
"name": "",
"type": "uint256",
"internalType": "uint256"
}
],
"stateMutability": "view"
},
{
"type": "function",
"name": "UPGRADE_INTERFACE_VERSION",
Expand Down
13 changes: 13 additions & 0 deletions contracts-abi/abi/ProviderRegistry.abi
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,19 @@
],
"stateMutability": "view"
},
{
"type": "function",
"name": "PRECISION",
"inputs": [],
"outputs": [
{
"name": "",
"type": "uint256",
"internalType": "uint256"
}
],
"stateMutability": "view"
},
{
"type": "function",
"name": "UPGRADE_INTERFACE_VERSION",
Expand Down
33 changes: 32 additions & 1 deletion contracts-abi/clients/BidderRegistry/BidderRegistry.go

Large diffs are not rendered by default.

33 changes: 32 additions & 1 deletion contracts-abi/clients/ProviderRegistry/ProviderRegistry.go

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion contracts/contracts/core/BidderRegistryStorage.sol
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ abstract contract BidderRegistryStorage {
using FeePayout for FeePayout.Tracker;

/// @dev For improved precision
uint256 constant public PERCENT = 1e18;
uint256 constant public PRECISION = 1e16;
uint256 constant public PERCENT = 100 * PRECISION;

/// @dev Address of the preconfManager contract
address public preconfManager;
Expand Down
3 changes: 2 additions & 1 deletion contracts/contracts/core/ProviderRegistryStorage.sol
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ abstract contract ProviderRegistryStorage {
using FeePayout for FeePayout.Tracker;

/// @dev For improved precision
uint256 public constant PERCENT = 1e18;
uint256 public constant PRECISION = 1e16;
uint256 public constant PERCENT = 100 * PRECISION;

/// @dev Minimum stake required for registration
uint256 public minStake;
Expand Down
7 changes: 4 additions & 3 deletions contracts/scripts/core/DeployCore.s.sol
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ contract DeployTestnet is Script {

// Amount of ETH to initially fund the oracle account on L1 chain.
uint256 public constant ORACLE_INITIAL_FUNDING = 1 ether;

uint256 public constant PERCENT_MULTIPLIER = 1e16;

error FailedToSendETHToOracle(address addr);

function run() external {
Expand All @@ -29,8 +30,8 @@ contract DeployTestnet is Script {
address protocolFeeRecipient = address(
0xfA0B0f5d298d28EFE4d35641724141ef19C05684 // Placeholder for now, L1 preconf.eth address
);
uint256 feePercent = 2 * 1e16; // 2%
uint256 providerPenaltyPercent = 5 * 1e16; // 5%
uint256 feePercent = 2 * PERCENT_MULTIPLIER; // 2%
uint256 providerPenaltyPercent = 5 * PERCENT_MULTIPLIER; // 5%
uint64 commitmentDispatchWindow = 2000;
uint256 withdrawalDelay = 24 hours * 1000; // 24 hours in milliseconds
uint256 protocolFeePayoutPeriodBlocks = 5 hours ; // 1 hour with 200ms blocks
Expand Down
14 changes: 7 additions & 7 deletions contracts/test/core/BidderRegistryTest.sol
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ contract BidderRegistryTest is Test {

bidderRegistry.openBid(commitmentDigest, 1 ether, bidder, blockNumber);

bidderRegistry.retrieveFunds(nextWindow, commitmentDigest, payable(provider), 1e18);
bidderRegistry.retrieveFunds(nextWindow, commitmentDigest, payable(provider), bidderRegistry.PERCENT());
uint256 providerAmount = bidderRegistry.providerAmount(provider);
uint256 feeRecipientAmount = bidderRegistry.getAccumulatedProtocolFee();

Expand All @@ -220,7 +220,7 @@ contract BidderRegistryTest is Test {

uint256 bidderBalance = bidder.balance;

bidderRegistry.retrieveFunds(nextWindow, commitmentDigest, payable(provider), 5e17);
bidderRegistry.retrieveFunds(nextWindow, commitmentDigest, payable(provider), 50 * bidderRegistry.PRECISION());
uint256 providerAmount = bidderRegistry.providerAmount(provider);
uint256 feeRecipientAmount = bidderRegistry.getAccumulatedProtocolFee();

Expand Down Expand Up @@ -269,7 +269,7 @@ contract BidderRegistryTest is Test {
vm.expectRevert(bytes(""));
bytes32 commitmentDigest = keccak256("1234");
bidderRegistry.openBid(commitmentDigest, 1 ether, bidder, blockNumber);
bidderRegistry.retrieveFunds(nextWindow, commitmentDigest, payable(provider),1e18);
bidderRegistry.retrieveFunds(nextWindow, commitmentDigest, payable(provider), bidderRegistry.PERCENT());
}

function testFail_shouldRetrieveFundsGreaterThanStake() public {
Expand All @@ -287,7 +287,7 @@ contract BidderRegistryTest is Test {
vm.prank(address(this));
bytes32 commitmentDigest = keccak256("1234");
bidderRegistry.openBid(commitmentDigest, 3 ether, bidder, blockNumber);
bidderRegistry.retrieveFunds(nextWindow, commitmentDigest, payable(provider),1e18);
bidderRegistry.retrieveFunds(nextWindow, commitmentDigest, payable(provider), bidderRegistry.PERCENT());
}

function test_withdrawProviderAmount() public {
Expand All @@ -305,7 +305,7 @@ contract BidderRegistryTest is Test {

bidderRegistry.openBid(commitmentDigest, 2 ether, bidder, blockNumber);

bidderRegistry.retrieveFunds(nextWindow, commitmentDigest, payable(provider), 1e18);
bidderRegistry.retrieveFunds(nextWindow, commitmentDigest, payable(provider), bidderRegistry.PERCENT());
bidderRegistry.withdrawProviderAmount(payable(provider));
uint256 balanceAfter = address(provider).balance;
assertEq(balanceAfter - balanceBefore, 1800000000000000000);
Expand Down Expand Up @@ -451,7 +451,7 @@ contract BidderRegistryTest is Test {
bidderRegistry.openBid(commitmentDigest, 1 ether, bidder, blockNumber);
vm.expectEmit(true, true, true, true);
emit FeeTransfer(100000000000000000, feeRecipient);
bidderRegistry.retrieveFunds(nextWindow, commitmentDigest, payable(provider),1e18);
bidderRegistry.retrieveFunds(nextWindow, commitmentDigest, payable(provider), bidderRegistry.PERCENT());
uint256 balanceAfter = feeRecipient.balance;
assertEq(balanceAfter - balanceBefore, 100000000000000000);
assertEq(bidderRegistry.getAccumulatedProtocolFee(), 0);
Expand All @@ -470,7 +470,7 @@ contract BidderRegistryTest is Test {
blockTracker.addBuilderAddress("test", provider);
blockTracker.recordL1Block(blockNumber, "test");
bidderRegistry.openBid(commitmentDigest, 1 ether, bidder, blockNumber);
bidderRegistry.retrieveFunds(nextWindow, commitmentDigest, payable(provider),1e18);
bidderRegistry.retrieveFunds(nextWindow, commitmentDigest, payable(provider), bidderRegistry.PERCENT());
uint256 balanceAfter = feeRecipient.balance;
assertEq(balanceAfter - balanceBefore, 0);
assertEq(bidderRegistry.getAccumulatedProtocolFee(), 100000000000000000);
Expand Down
22 changes: 11 additions & 11 deletions contracts/test/core/OracleTest.sol
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ contract OracleTest is Test {
1000
);

feePercent = 10 * 1e16;
feePercent = 10 * 1e16; // 10%
minStake = 1e18 wei;
feeRecipient = vm.addr(9);

Expand Down Expand Up @@ -199,7 +199,7 @@ contract OracleTest is Test {
blockNumber,
provider,
false,
5 * 1e17
50 * bidderRegistry.PRECISION()
);
vm.stopPrank();
assertEq(
Expand Down Expand Up @@ -249,7 +249,7 @@ contract OracleTest is Test {
blockNumber,
provider,
true,
5 * 1e17
50 * bidderRegistry.PRECISION()
);
vm.stopPrank();
assertEq(
Expand Down Expand Up @@ -313,7 +313,7 @@ contract OracleTest is Test {
blockNumber,
provider,
true,
1e18
providerRegistry.PERCENT()
);

vm.expectEmit(true, false, false, true);
Expand All @@ -323,13 +323,13 @@ contract OracleTest is Test {
blockNumber,
provider,
false,
5 * 1e17
50 * providerRegistry.PRECISION()
);
vm.stopPrank();
assertEq(providerRegistry.getProviderStake(provider), 250 ether - ((bid*110)/100));
assertEq(
bidderRegistry.getProviderAmount(provider),
(((bid * (1e18 - feePercent)) / 1e18) * residualAfterDecay) / 100
(((bid * (providerRegistry.PERCENT() - feePercent)) / providerRegistry.PERCENT()) * residualAfterDecay) / 100
);
}

Expand Down Expand Up @@ -410,7 +410,7 @@ contract OracleTest is Test {
blockNumber,
provider,
true,
1e18
bidderRegistry.PERCENT()
);
vm.expectEmit(true, false, false, true);
emit CommitmentProcessed(index2, true);
Expand All @@ -419,7 +419,7 @@ contract OracleTest is Test {
blockNumber,
provider,
true,
1e18
bidderRegistry.PERCENT()
);
vm.expectEmit(true, false, false, true);
emit CommitmentProcessed(index3, true);
Expand All @@ -428,7 +428,7 @@ contract OracleTest is Test {
blockNumber,
provider,
true,
1e18
bidderRegistry.PERCENT()
);
vm.expectEmit(true, false, false, true);
emit CommitmentProcessed(index4, true);
Expand All @@ -437,7 +437,7 @@ contract OracleTest is Test {
blockNumber,
provider,
true,
1e18
bidderRegistry.PERCENT()
);
vm.stopPrank();
assertEq(providerRegistry.getProviderStake(provider), 250 ether - bid * 4);
Expand Down Expand Up @@ -526,7 +526,7 @@ contract OracleTest is Test {
blockNumber,
provider,
false,
1e18
bidderRegistry.PERCENT()
);
}
vm.stopPrank();
Expand Down
26 changes: 14 additions & 12 deletions contracts/test/core/ProviderRegistryTest.sol
Original file line number Diff line number Diff line change
Expand Up @@ -205,14 +205,14 @@ contract ProviderRegistryTest is Test {

function test_SetNewFeePercent() public {
vm.prank(address(this));
providerRegistry.setNewFeePercent(uint16(25));
providerRegistry.setNewFeePercent(25);

assertEq(providerRegistry.feePercent(), uint16(25));
assertEq(providerRegistry.feePercent(), 25);
}

function testFail_SetNewFeePercent() public {
vm.expectRevert(bytes(""));
providerRegistry.setNewFeePercent(uint16(25));
providerRegistry.setNewFeePercent(25);
}

function test_SetPreConfContract() public {
Expand Down Expand Up @@ -258,7 +258,7 @@ contract ProviderRegistryTest is Test {
address bidder = vm.addr(4);

vm.expectCall(bidder, 1000000000000000000 wei, new bytes(0));
providerRegistry.slash(1 ether, provider, payable(bidder), 1e18);
providerRegistry.slash(1 ether, provider, payable(bidder), providerRegistry.PERCENT());

assertEq(bidder.balance, 1000000000000000000 wei);
assertEq(providerRegistry.providerStakes(provider), 0.9 ether);
Expand All @@ -270,7 +270,7 @@ contract ProviderRegistryTest is Test {
providerRegistry.registerAndStake{value: 2 ether}(validBLSPubkeys);
address bidder = vm.addr(4);
vm.expectRevert(bytes(""));
providerRegistry.slash(1 ether, provider, payable(bidder), 1e18);
providerRegistry.slash(1 ether, provider, payable(bidder), providerRegistry.PERCENT());
}
function test_ShouldRetrieveFundsWhenSlashIsGreaterThanStake() public {
vm.prank(address(this));
Expand All @@ -284,7 +284,7 @@ contract ProviderRegistryTest is Test {

vm.expectEmit(true, true, true, true);
emit InsufficientFundsToSlash(provider, 2 ether, 3 ether, 0.3 ether);
providerRegistry.slash(3 ether, provider, payable(bidder), 1e18);
providerRegistry.slash(3 ether, provider, payable(bidder), providerRegistry.PERCENT());

assertEq(providerRegistry.getAccumulatedPenaltyFee(), 0);
assertEq(providerRegistry.providerStakes(provider), 0 ether);
Expand All @@ -302,7 +302,7 @@ contract ProviderRegistryTest is Test {

vm.expectEmit(true, true, true, true);
emit InsufficientFundsToSlash(provider, 3 ether, 3 ether, 0.3 ether);
providerRegistry.slash(3 ether, provider, payable(bidder), 1e18);
providerRegistry.slash(3 ether, provider, payable(bidder), providerRegistry.PERCENT());

assertEq(providerRegistry.getAccumulatedPenaltyFee(), 0);
assertEq(providerRegistry.providerStakes(provider), 0 ether);
Expand All @@ -320,7 +320,7 @@ contract ProviderRegistryTest is Test {

vm.expectEmit(true, true, true, true);
emit InsufficientFundsToSlash(provider, 3.1 ether, 3 ether, 0.3 ether);
providerRegistry.slash(3 ether, provider, payable(bidder), 1e18);
providerRegistry.slash(3 ether, provider, payable(bidder), providerRegistry.PERCENT());

assertEq(providerRegistry.getAccumulatedPenaltyFee(), 0.1 ether);
assertEq(providerRegistry.providerStakes(provider), 0 ether);
Expand All @@ -334,7 +334,7 @@ contract ProviderRegistryTest is Test {

providerRegistry.registerAndStake{value: 2 ether}(validBLSPubkeys);
providerRegistry.setPreconfManager(address(this));
providerRegistry.slash(1e18 wei, provider, payable(bidder), 5e17);
providerRegistry.slash(1e18 wei, provider, payable(bidder), 50 * providerRegistry.PRECISION());
assertEq(
providerRegistry.getAccumulatedPenaltyFee(),
5e16 wei,
Expand All @@ -350,7 +350,7 @@ contract ProviderRegistryTest is Test {

vm.expectEmit(true, true, true, true);
emit FeeTransfer(1e17 wei, vm.addr(6));
providerRegistry.slash(1e18 wei, newProvider, payable(bidder), 5e17);
providerRegistry.slash(1e18 wei, newProvider, payable(bidder), 50 * providerRegistry.PRECISION());

assertEq(
providerRegistry.getAccumulatedPenaltyFee(),
Expand All @@ -368,14 +368,15 @@ contract ProviderRegistryTest is Test {
providerRegistry.setNewPenaltyFeeRecipient(address(0));
address newProvider = vm.addr(8);
address bidder = vm.addr(9);
uint256 percent = providerRegistry.PERCENT();
vm.deal(newProvider, 3 ether);
vm.prank(newProvider);
providerRegistry.registerAndStake{value: 2e18 wei}(validBLSPubkeys);
providerRegistry.setPreconfManager(
address(preconfManager)
);
vm.prank(address(preconfManager));
providerRegistry.slash(1e18 wei, newProvider, payable(bidder),1e18);
providerRegistry.slash(1e18 wei, newProvider, payable(bidder), percent);
vm.prank(newProvider);
providerRegistry.unstake();
vm.warp(block.timestamp + 24 hours); // Move forward in time
Expand Down Expand Up @@ -449,14 +450,15 @@ contract ProviderRegistryTest is Test {
function test_WithdrawStakedAmount() public {
address newProvider = vm.addr(8);
address bidder = vm.addr(9);
uint256 percent = providerRegistry.PERCENT();
vm.deal(newProvider, 3 ether);
vm.prank(newProvider);
providerRegistry.registerAndStake{value: 2e18 wei}(validBLSPubkeys);
providerRegistry.setPreconfManager(
address(preconfManager)
);
vm.prank(address(preconfManager));
providerRegistry.slash(1e18 wei, newProvider, payable(bidder),1e18);
providerRegistry.slash(1e18 wei, newProvider, payable(bidder), percent);
vm.prank(newProvider);
providerRegistry.unstake();
vm.warp(block.timestamp + 24 hours); // Move forward in time
Expand Down
10 changes: 7 additions & 3 deletions oracle/pkg/updater/updater.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ const (
SettlementTypeSlash SettlementType = "slash"
)

const (
PERCENT = 1e18
)

type Winner struct {
Winner []byte
Window int64
Expand Down Expand Up @@ -590,9 +594,9 @@ func (u *Updater) computeDecayPercentage(startTimestamp, endTimestamp, commitTim
// Calculate the decay percentage
decayPercentage := float64(timePassed) / float64(totalTime)

decayPercentageRound := int64(math.Round(decayPercentage * 1e18))
if decayPercentageRound > 1e18 {
decayPercentageRound = 1e18
decayPercentageRound := int64(math.Round(decayPercentage * PERCENT))
if decayPercentageRound > PERCENT {
decayPercentageRound = PERCENT
}
u.logger.Debug("decay information",
"startTimestamp", startTimestamp,
Expand Down

0 comments on commit e7e2440

Please sign in to comment.