From 3a4056dc6dcb5985925e03ad0adca246e4fd83b6 Mon Sep 17 00:00:00 2001 From: SidestreamSweatyPumpkin Date: Mon, 30 Jun 2025 12:50:41 +0200 Subject: [PATCH 01/14] add rewardCallerToTranscoder mapping, reward logic, setter --- contracts/bonding/BondingManager.sol | 28 +++++++++++++++++++++------ contracts/bonding/IBondingManager.sol | 1 + 2 files changed, 23 insertions(+), 6 deletions(-) diff --git a/contracts/bonding/BondingManager.sol b/contracts/bonding/BondingManager.sol index c401324b..0bdeeb07 100644 --- a/contracts/bonding/BondingManager.sol +++ b/contracts/bonding/BondingManager.sol @@ -102,6 +102,9 @@ contract BondingManager is ManagerProxyTarget, IBondingManager { // If the balance of the treasury in LPT is above this value, automatic treasury contributions will halt. uint256 public treasuryBalanceCeiling; + // Allow reward() calls by pre-defined set of addresses + mapping(address => address) private rewardCallerToTranscoder; + // Check if sender is TicketBroker modifier onlyTicketBroker() { _onlyTicketBroker(); @@ -188,6 +191,15 @@ contract BondingManager is ManagerProxyTarget, IBondingManager { emit ParameterUpdate("numActiveTranscoders"); } + /** + * @notice Set (or unset using 0 address) a reward caller for a transcoder + * @param _rewardCaller Address of a trusted reward caller + */ + function setRewardCaller(address _rewardCaller) external whenSystemNotPaused { + rewardCallerToTranscoder[_rewardCaller] = msg.sender; + emit RewardCallerUpdated(_rewardCaller, msg.sender); + } + /** * @notice Sets commission rates as a transcoder and if the caller is not in the transcoder pool tries to add it * @dev Percentages are represented as numerators of fractions over MathUtils.PERC_DIVISOR @@ -869,13 +881,17 @@ contract BondingManager is ManagerProxyTarget, IBondingManager { { uint256 currentRound = roundsManager().currentRound(); - require(isActiveTranscoder(msg.sender), "caller must be an active transcoder"); + address transcoderAddress = msg.sender; + if (!isActiveTranscoder(transcoderAddress)) { + transcoderAddress = rewardCallerToTranscoder[msg.sender]; + require(isActiveTranscoder(transcoderAddress), "caller must be an active transcoder"); + } require( - transcoders[msg.sender].lastRewardRound != currentRound, + transcoders[transcoderAddress].lastRewardRound != currentRound, "caller has already called reward for the current round" ); - Transcoder storage t = transcoders[msg.sender]; + Transcoder storage t = transcoders[transcoderAddress]; EarningsPool.Data storage earningsPool = t.earningsPoolPerRound[currentRound]; // Set last round that transcoder called reward @@ -908,17 +924,17 @@ contract BondingManager is ManagerProxyTarget, IBondingManager { mtr.trustedTransferTokens(trsry, treasuryRewards); - emit TreasuryReward(msg.sender, trsry, treasuryRewards); + emit TreasuryReward(transcoderAddress, trsry, treasuryRewards); } uint256 transcoderRewards = totalRewardTokens.sub(treasuryRewards); - updateTranscoderWithRewards(msg.sender, transcoderRewards, currentRound, _newPosPrev, _newPosNext); + updateTranscoderWithRewards(transcoderAddress, transcoderRewards, currentRound, _newPosPrev, _newPosNext); // Set last round that transcoder called reward t.lastRewardRound = currentRound; - emit Reward(msg.sender, transcoderRewards); + emit Reward(transcoderAddress, transcoderRewards); } /** diff --git a/contracts/bonding/IBondingManager.sol b/contracts/bonding/IBondingManager.sol index b7482085..21b7b344 100644 --- a/contracts/bonding/IBondingManager.sol +++ b/contracts/bonding/IBondingManager.sol @@ -44,6 +44,7 @@ interface IBondingManager { uint256 startRound, uint256 endRound ); + event RewardCallerUpdated(address indexed rewardCaller, address indexed transcoder); // Deprecated events // These event signatures can be used to construct the appropriate topic hashes to filter for past logs corresponding From 32d094a442f7364e6b43afbee2a0ab57bb97215e Mon Sep 17 00:00:00 2001 From: SidestreamSweatyPumpkin Date: Mon, 30 Jun 2025 13:14:31 +0200 Subject: [PATCH 02/14] add basic tests --- test/unit/BondingManager.js | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/test/unit/BondingManager.js b/test/unit/BondingManager.js index 161a3f85..37b4cc67 100644 --- a/test/unit/BondingManager.js +++ b/test/unit/BondingManager.js @@ -6100,6 +6100,42 @@ describe("BondingManager", () => { atCeilingTest("when above limit", 1500) }) }) + + describe("reward delegation", () => { + const transcoderRewards = 1000 + + it("should allow a RewardCaller to call reward", async () => { + // Transcoder should be able to set a non-transcoder as a reward caller + const setRewardCallerTx = bondingManager + .connect(transcoder) + .setRewardCaller(nonTranscoder.address) + await expect(setRewardCallerTx) + .to.emit(bondingManager, "RewardCallerUpdated") + .withArgs(nonTranscoder.address, transcoder.address) + + // Non-transcoder should now be able to call reward on behalf of the transcoder + const rewardTx = bondingManager.connect(nonTranscoder).reward() + await expect(rewardTx) + .to.emit(bondingManager, "Reward") + .withArgs(transcoder.address, transcoderRewards) + }) + + it("should allow a transcoder to call reward even if RewardCaller is set", async () => { + // Transcoder should be able to set a non-transcoder as a reward caller + const setRewardCallerTx = bondingManager + .connect(transcoder) + .setRewardCaller(nonTranscoder.address) + await expect(setRewardCallerTx) + .to.emit(bondingManager, "RewardCallerUpdated") + .withArgs(nonTranscoder.address, transcoder.address) + + // Non-transcoder should now be able to call reward on behalf of the transcoder + const rewardTx = bondingManager.connect(transcoder).reward() + await expect(rewardTx) + .to.emit(bondingManager, "Reward") + .withArgs(transcoder.address, transcoderRewards) + }) + }) }) describe("updateTranscoderWithFees", () => { From e69bdcb817aec06e3621b91bd3dc7511b7272780 Mon Sep 17 00:00:00 2001 From: SidestreamSweatyPumpkin Date: Mon, 30 Jun 2025 13:18:40 +0200 Subject: [PATCH 03/14] ci: enforce test coverage threshold --- .github/workflows/test.yaml | 3 +++ package.json | 1 + 2 files changed, 4 insertions(+) diff --git a/.github/workflows/test.yaml b/.github/workflows/test.yaml index a8db0943..5dba1360 100644 --- a/.github/workflows/test.yaml +++ b/.github/workflows/test.yaml @@ -47,6 +47,9 @@ jobs: name: ${{ github.event.repository.name }} token: ${{ secrets.CI_CODECOV_TOKEN }} + - name: Enforce test coverage threshold + run: yarn test:coverage:check + editorconfig: name: Run editorconfig checker runs-on: ubuntu-latest diff --git a/package.json b/package.json index e0f26e8f..8ae362bc 100644 --- a/package.json +++ b/package.json @@ -21,6 +21,7 @@ "clean": "rm -rf cache artifacts typechain", "compile": "npx hardhat compile", "test:coverage": "npx hardhat coverage", + "test:coverage:check": "npx istanbul check-coverage ./coverage.json --statements 100 --branches 100 --functions 100 --lines 100", "test": "npx hardhat test", "test:unit": "npx hardhat test test/unit/*.*", "test:integration": "npx hardhat test test/integration/**", From 6fe4dfa69f7e0bdc6026aebfcc31d4656ef6bed6 Mon Sep 17 00:00:00 2001 From: SidestreamSweatyPumpkin Date: Mon, 30 Jun 2025 16:02:36 +0200 Subject: [PATCH 04/14] add unsetRewardCaller --- contracts/bonding/BondingManager.sol | 21 +++++++-- contracts/bonding/IBondingManager.sol | 3 +- test/unit/BondingManager.js | 62 +++++++++++++++++++++------ 3 files changed, 67 insertions(+), 19 deletions(-) diff --git a/contracts/bonding/BondingManager.sol b/contracts/bonding/BondingManager.sol index 0bdeeb07..93ec77bf 100644 --- a/contracts/bonding/BondingManager.sol +++ b/contracts/bonding/BondingManager.sol @@ -192,12 +192,25 @@ contract BondingManager is ManagerProxyTarget, IBondingManager { } /** - * @notice Set (or unset using 0 address) a reward caller for a transcoder - * @param _rewardCaller Address of a trusted reward caller + * @notice Set a reward caller for a transcoder + * @param _rewardCaller Address of the new reward caller */ function setRewardCaller(address _rewardCaller) external whenSystemNotPaused { + address transcoder = rewardCallerToTranscoder[_rewardCaller]; + require(transcoder == address(0), "reward caller is already set"); rewardCallerToTranscoder[_rewardCaller] = msg.sender; - emit RewardCallerUpdated(_rewardCaller, msg.sender); + emit RewardCallerSet(msg.sender, _rewardCaller); + } + + /** + * @notice Unset a reward caller for a transcoder + * @param _rewardCaller Address of the existing reward caller + */ + function unsetRewardCaller(address _rewardCaller) external whenSystemNotPaused { + address transcoder = rewardCallerToTranscoder[_rewardCaller]; + require(transcoder == msg.sender, "only relevant transcoder can unset"); + rewardCallerToTranscoder[_rewardCaller] = address(0); + emit RewardCallerUnset(msg.sender, _rewardCaller); } /** @@ -884,7 +897,7 @@ contract BondingManager is ManagerProxyTarget, IBondingManager { address transcoderAddress = msg.sender; if (!isActiveTranscoder(transcoderAddress)) { transcoderAddress = rewardCallerToTranscoder[msg.sender]; - require(isActiveTranscoder(transcoderAddress), "caller must be an active transcoder"); + require(isActiveTranscoder(transcoderAddress), "caller must be an active transcoder or rewardCaller"); } require( transcoders[transcoderAddress].lastRewardRound != currentRound, diff --git a/contracts/bonding/IBondingManager.sol b/contracts/bonding/IBondingManager.sol index 21b7b344..ecf9842a 100644 --- a/contracts/bonding/IBondingManager.sol +++ b/contracts/bonding/IBondingManager.sol @@ -44,7 +44,8 @@ interface IBondingManager { uint256 startRound, uint256 endRound ); - event RewardCallerUpdated(address indexed rewardCaller, address indexed transcoder); + event RewardCallerSet(address indexed transcoder, address indexed rewardCaller); + event RewardCallerUnset(address indexed transcoder, address indexed rewardCaller); // Deprecated events // These event signatures can be used to construct the appropriate topic hashes to filter for past logs corresponding diff --git a/test/unit/BondingManager.js b/test/unit/BondingManager.js index 37b4cc67..b813c837 100644 --- a/test/unit/BondingManager.js +++ b/test/unit/BondingManager.js @@ -5474,7 +5474,9 @@ describe("BondingManager", () => { it("should fail if caller is not a transcoder", async () => { await expect( bondingManager.connect(nonTranscoder).reward() - ).to.be.revertedWith("caller must be an active transcoder") + ).to.be.revertedWith( + "caller must be an active transcoder or rewardCaller" + ) }) it("should fail if caller is registered but not an active transcoder yet in the current round", async () => { @@ -5484,7 +5486,9 @@ describe("BondingManager", () => { ) await expect( bondingManager.connect(transcoder).reward() - ).to.be.revertedWith("caller must be an active transcoder") + ).to.be.revertedWith( + "caller must be an active transcoder or rewardCaller" + ) }) it("should fail if caller already called reward during the current round", async () => { @@ -6104,36 +6108,66 @@ describe("BondingManager", () => { describe("reward delegation", () => { const transcoderRewards = 1000 - it("should allow a RewardCaller to call reward", async () => { - // Transcoder should be able to set a non-transcoder as a reward caller + it("should allow a transcoder to call reward even if RewardCaller is set", async () => { const setRewardCallerTx = bondingManager .connect(transcoder) .setRewardCaller(nonTranscoder.address) await expect(setRewardCallerTx) - .to.emit(bondingManager, "RewardCallerUpdated") - .withArgs(nonTranscoder.address, transcoder.address) + .to.emit(bondingManager, "RewardCallerSet") + .withArgs(transcoder.address, nonTranscoder.address) - // Non-transcoder should now be able to call reward on behalf of the transcoder - const rewardTx = bondingManager.connect(nonTranscoder).reward() + const rewardTx = bondingManager.connect(transcoder).reward() await expect(rewardTx) .to.emit(bondingManager, "Reward") .withArgs(transcoder.address, transcoderRewards) + + await fixture.roundsManager.setMockUint256( + functionSig("currentRound()"), + currentRound + 3 + ) + + const unsetRewardCallerTx = bondingManager + .connect(transcoder) + .unsetRewardCaller(nonTranscoder.address) + await expect(unsetRewardCallerTx) + .to.emit(bondingManager, "RewardCallerUnset") + .withArgs(transcoder.address, nonTranscoder.address) + + const rewardTx2 = bondingManager.connect(transcoder).reward() + await expect(rewardTx2) + .to.emit(bondingManager, "Reward") + .withArgs(transcoder.address, transcoderRewards) }) - it("should allow a transcoder to call reward even if RewardCaller is set", async () => { - // Transcoder should be able to set a non-transcoder as a reward caller + it("should allow a RewardCaller to call reward", async () => { const setRewardCallerTx = bondingManager .connect(transcoder) .setRewardCaller(nonTranscoder.address) await expect(setRewardCallerTx) - .to.emit(bondingManager, "RewardCallerUpdated") - .withArgs(nonTranscoder.address, transcoder.address) + .to.emit(bondingManager, "RewardCallerSet") + .withArgs(transcoder.address, nonTranscoder.address) - // Non-transcoder should now be able to call reward on behalf of the transcoder - const rewardTx = bondingManager.connect(transcoder).reward() + const rewardTx = bondingManager.connect(nonTranscoder).reward() await expect(rewardTx) .to.emit(bondingManager, "Reward") .withArgs(transcoder.address, transcoderRewards) + + await fixture.roundsManager.setMockUint256( + functionSig("currentRound()"), + currentRound + 3 + ) + + const unsetRewardCallerTx = bondingManager + .connect(transcoder) + .unsetRewardCaller(nonTranscoder.address) + await expect(unsetRewardCallerTx) + .to.emit(bondingManager, "RewardCallerUnset") + .withArgs(transcoder.address, nonTranscoder.address) + + const rewardTx2 = bondingManager.connect(nonTranscoder).reward() + await expect(rewardTx2).to.be.revertedWith( + "caller must be an active transcoder or rewardCaller" + ) }) }) }) From a3bd4306c93aa0595d13296cbf091d0e62c114dd Mon Sep 17 00:00:00 2001 From: SidestreamSweatyPumpkin Date: Mon, 30 Jun 2025 16:28:47 +0200 Subject: [PATCH 05/14] full test coverage --- test/unit/BondingManager.js | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/test/unit/BondingManager.js b/test/unit/BondingManager.js index b813c837..af66b8b2 100644 --- a/test/unit/BondingManager.js +++ b/test/unit/BondingManager.js @@ -6169,6 +6169,38 @@ describe("BondingManager", () => { "caller must be an active transcoder or rewardCaller" ) }) + + it("impossible to set the same RewardCaller twice", async () => { + const setRewardCallerTx = bondingManager + .connect(transcoder) + .setRewardCaller(nonTranscoder.address) + await expect(setRewardCallerTx) + .to.emit(bondingManager, "RewardCallerSet") + .withArgs(transcoder.address, nonTranscoder.address) + + const setRewardCallerTx2 = bondingManager + .connect(transcoder) + .setRewardCaller(nonTranscoder.address) + await expect(setRewardCallerTx2).to.be.revertedWith( + "reward caller is already set" + ) + }) + + it("impossible to unset the RewardCaller for another transcoder", async () => { + const setRewardCallerTx = bondingManager + .connect(transcoder) + .setRewardCaller(nonTranscoder.address) + await expect(setRewardCallerTx) + .to.emit(bondingManager, "RewardCallerSet") + .withArgs(transcoder.address, nonTranscoder.address) + + const unsetRewardCallerTx = bondingManager + .connect(nonTranscoder) + .unsetRewardCaller(nonTranscoder.address) + await expect(unsetRewardCallerTx).to.be.revertedWith( + "only relevant transcoder can unset" + ) + }) }) }) From 5bce7ca22eb89df2c3e1375fdfb8cdc08041206f Mon Sep 17 00:00:00 2001 From: SidestreamSweatyPumpkin Date: Mon, 30 Jun 2025 16:58:34 +0200 Subject: [PATCH 06/14] remove unnecessary variable --- contracts/bonding/BondingManager.sol | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/contracts/bonding/BondingManager.sol b/contracts/bonding/BondingManager.sol index 93ec77bf..3c3ef708 100644 --- a/contracts/bonding/BondingManager.sol +++ b/contracts/bonding/BondingManager.sol @@ -196,8 +196,7 @@ contract BondingManager is ManagerProxyTarget, IBondingManager { * @param _rewardCaller Address of the new reward caller */ function setRewardCaller(address _rewardCaller) external whenSystemNotPaused { - address transcoder = rewardCallerToTranscoder[_rewardCaller]; - require(transcoder == address(0), "reward caller is already set"); + require(rewardCallerToTranscoder[_rewardCaller] == address(0), "reward caller is already set"); rewardCallerToTranscoder[_rewardCaller] = msg.sender; emit RewardCallerSet(msg.sender, _rewardCaller); } @@ -207,8 +206,7 @@ contract BondingManager is ManagerProxyTarget, IBondingManager { * @param _rewardCaller Address of the existing reward caller */ function unsetRewardCaller(address _rewardCaller) external whenSystemNotPaused { - address transcoder = rewardCallerToTranscoder[_rewardCaller]; - require(transcoder == msg.sender, "only relevant transcoder can unset"); + require(rewardCallerToTranscoder[_rewardCaller] == msg.sender, "only relevant transcoder can unset"); rewardCallerToTranscoder[_rewardCaller] = address(0); emit RewardCallerUnset(msg.sender, _rewardCaller); } From 884a9a297edb343343b772cf1eac8eaed7ebf89c Mon Sep 17 00:00:00 2001 From: SidestreamSweatyPumpkin Date: Wed, 3 Sep 2025 12:49:18 +0200 Subject: [PATCH 07/14] use transcoder -> rewardCaller mapping direction to prevent frontruns --- contracts/bonding/BondingManager.sol | 87 ++++++++++++++++++--------- contracts/bonding/IBondingManager.sol | 1 - test/unit/BondingManager.js | 60 +++++------------- 3 files changed, 71 insertions(+), 77 deletions(-) diff --git a/contracts/bonding/BondingManager.sol b/contracts/bonding/BondingManager.sol index 3c3ef708..6f502dc5 100644 --- a/contracts/bonding/BondingManager.sol +++ b/contracts/bonding/BondingManager.sol @@ -102,8 +102,8 @@ contract BondingManager is ManagerProxyTarget, IBondingManager { // If the balance of the treasury in LPT is above this value, automatic treasury contributions will halt. uint256 public treasuryBalanceCeiling; - // Allow reward() calls by pre-defined set of addresses - mapping(address => address) private rewardCallerToTranscoder; + // Allow reward() calls from one pre-defined address per transcoder + mapping(address => address) private transcoderToRewardCaller; // Check if sender is TicketBroker modifier onlyTicketBroker() { @@ -194,23 +194,13 @@ contract BondingManager is ManagerProxyTarget, IBondingManager { /** * @notice Set a reward caller for a transcoder * @param _rewardCaller Address of the new reward caller + * @dev By providing address(0) the reward caller can be unset */ function setRewardCaller(address _rewardCaller) external whenSystemNotPaused { - require(rewardCallerToTranscoder[_rewardCaller] == address(0), "reward caller is already set"); - rewardCallerToTranscoder[_rewardCaller] = msg.sender; + transcoderToRewardCaller[msg.sender] = _rewardCaller; emit RewardCallerSet(msg.sender, _rewardCaller); } - /** - * @notice Unset a reward caller for a transcoder - * @param _rewardCaller Address of the existing reward caller - */ - function unsetRewardCaller(address _rewardCaller) external whenSystemNotPaused { - require(rewardCallerToTranscoder[_rewardCaller] == msg.sender, "only relevant transcoder can unset"); - rewardCallerToTranscoder[_rewardCaller] = address(0); - emit RewardCallerUnset(msg.sender, _rewardCaller); - } - /** * @notice Sets commission rates as a transcoder and if the caller is not in the transcoder pool tries to add it * @dev Percentages are represented as numerators of fractions over MathUtils.PERC_DIVISOR @@ -317,6 +307,15 @@ contract BondingManager is ManagerProxyTarget, IBondingManager { rewardWithHint(address(0), address(0)); } + /** + * @notice Mint token rewards for an active transcoder and its delegators + * @param _transcoder Address of the transcoder on behalf of which the reward is called + * @dev Only callable by trusted rewardCaller + */ + function rewardForTranscoder(address _transcoder) external { + rewardForTranscoderWithHint(_transcoder, address(0), address(0)); + } + /** * @notice Update transcoder's fee pool. Only callable by the TicketBroker * @param _transcoder Transcoder address @@ -884,25 +883,53 @@ contract BondingManager is ManagerProxyTarget, IBondingManager { * @param _newPosPrev Address of previous transcoder in pool if the caller is in the pool * @param _newPosNext Address of next transcoder in pool if the caller is in the pool */ - function rewardWithHint(address _newPosPrev, address _newPosNext) - public - whenSystemNotPaused - currentRoundInitialized - autoCheckpoint(msg.sender) - { + function rewardWithHint(address _newPosPrev, address _newPosNext) public { + _rewardWithHint(msg.sender, _newPosPrev, _newPosNext); + } + + /** + * @notice Mint token rewards for an active transcoder and its delegators and update the transcoder pool using an optional list hint if needed + * @dev If the caller is in the transcoder pool, the caller can provide an optional hint for its insertion position in the + * pool via the `_newPosPrev` and `_newPosNext` params. A linear search will be executed starting at the hint to find the correct position. + * In the best case, the hint is the correct position so no search is executed. See SortedDoublyLL.sol for details on list hints + * @dev Only callable by trusted rewardCaller + * @param _transcoder Address of the transcoder on behalf of which the reward is called + * @param _newPosPrev Address of previous transcoder in pool if the caller is in the pool + * @param _newPosNext Address of next transcoder in pool if the caller is in the pool + */ + function rewardForTranscoderWithHint( + address _transcoder, + address _newPosPrev, + address _newPosNext + ) public { + address rewardCaller = transcoderToRewardCaller[_transcoder]; + require(rewardCaller == msg.sender, "caller must be a reward caller set by the transcoder"); + _rewardWithHint(_transcoder, _newPosPrev, _newPosNext); + } + + /** + * @notice Mint token rewards for an active transcoder and its delegators and update the transcoder pool using an optional list hint if needed + * @dev If the caller is in the transcoder pool, the caller can provide an optional hint for its insertion position in the + * pool via the `_newPosPrev` and `_newPosNext` params. A linear search will be executed starting at the hint to find the correct position. + * In the best case, the hint is the correct position so no search is executed. See SortedDoublyLL.sol for details on list hints + * @param _transcoder Address of the transcoder on behalf of which the reward is called + * @param _newPosPrev Address of previous transcoder in pool if the caller is in the pool + * @param _newPosNext Address of next transcoder in pool if the caller is in the pool + */ + function _rewardWithHint( + address _transcoder, + address _newPosPrev, + address _newPosNext + ) private whenSystemNotPaused currentRoundInitialized autoCheckpoint(_transcoder) { uint256 currentRound = roundsManager().currentRound(); - address transcoderAddress = msg.sender; - if (!isActiveTranscoder(transcoderAddress)) { - transcoderAddress = rewardCallerToTranscoder[msg.sender]; - require(isActiveTranscoder(transcoderAddress), "caller must be an active transcoder or rewardCaller"); - } + require(isActiveTranscoder(_transcoder), "transcoder must be an active"); require( - transcoders[transcoderAddress].lastRewardRound != currentRound, + transcoders[_transcoder].lastRewardRound != currentRound, "caller has already called reward for the current round" ); - Transcoder storage t = transcoders[transcoderAddress]; + Transcoder storage t = transcoders[_transcoder]; EarningsPool.Data storage earningsPool = t.earningsPoolPerRound[currentRound]; // Set last round that transcoder called reward @@ -935,17 +962,17 @@ contract BondingManager is ManagerProxyTarget, IBondingManager { mtr.trustedTransferTokens(trsry, treasuryRewards); - emit TreasuryReward(transcoderAddress, trsry, treasuryRewards); + emit TreasuryReward(_transcoder, trsry, treasuryRewards); } uint256 transcoderRewards = totalRewardTokens.sub(treasuryRewards); - updateTranscoderWithRewards(transcoderAddress, transcoderRewards, currentRound, _newPosPrev, _newPosNext); + updateTranscoderWithRewards(_transcoder, transcoderRewards, currentRound, _newPosPrev, _newPosNext); // Set last round that transcoder called reward t.lastRewardRound = currentRound; - emit Reward(transcoderAddress, transcoderRewards); + emit Reward(_transcoder, transcoderRewards); } /** diff --git a/contracts/bonding/IBondingManager.sol b/contracts/bonding/IBondingManager.sol index ecf9842a..653e5a97 100644 --- a/contracts/bonding/IBondingManager.sol +++ b/contracts/bonding/IBondingManager.sol @@ -45,7 +45,6 @@ interface IBondingManager { uint256 endRound ); event RewardCallerSet(address indexed transcoder, address indexed rewardCaller); - event RewardCallerUnset(address indexed transcoder, address indexed rewardCaller); // Deprecated events // These event signatures can be used to construct the appropriate topic hashes to filter for past logs corresponding diff --git a/test/unit/BondingManager.js b/test/unit/BondingManager.js index af66b8b2..9ad38e91 100644 --- a/test/unit/BondingManager.js +++ b/test/unit/BondingManager.js @@ -5474,9 +5474,7 @@ describe("BondingManager", () => { it("should fail if caller is not a transcoder", async () => { await expect( bondingManager.connect(nonTranscoder).reward() - ).to.be.revertedWith( - "caller must be an active transcoder or rewardCaller" - ) + ).to.be.revertedWith("transcoder must be an active") }) it("should fail if caller is registered but not an active transcoder yet in the current round", async () => { @@ -5486,9 +5484,7 @@ describe("BondingManager", () => { ) await expect( bondingManager.connect(transcoder).reward() - ).to.be.revertedWith( - "caller must be an active transcoder or rewardCaller" - ) + ).to.be.revertedWith("transcoder must be an active") }) it("should fail if caller already called reward during the current round", async () => { @@ -6128,10 +6124,10 @@ describe("BondingManager", () => { const unsetRewardCallerTx = bondingManager .connect(transcoder) - .unsetRewardCaller(nonTranscoder.address) + .setRewardCaller(ZERO_ADDRESS) await expect(unsetRewardCallerTx) - .to.emit(bondingManager, "RewardCallerUnset") - .withArgs(transcoder.address, nonTranscoder.address) + .to.emit(bondingManager, "RewardCallerSet") + .withArgs(transcoder.address, ZERO_ADDRESS) const rewardTx2 = bondingManager.connect(transcoder).reward() await expect(rewardTx2) @@ -6147,7 +6143,9 @@ describe("BondingManager", () => { .to.emit(bondingManager, "RewardCallerSet") .withArgs(transcoder.address, nonTranscoder.address) - const rewardTx = bondingManager.connect(nonTranscoder).reward() + const rewardTx = bondingManager + .connect(nonTranscoder) + .rewardForTranscoder(transcoder.address) await expect(rewardTx) .to.emit(bondingManager, "Reward") .withArgs(transcoder.address, transcoderRewards) @@ -6159,46 +6157,16 @@ describe("BondingManager", () => { const unsetRewardCallerTx = bondingManager .connect(transcoder) - .unsetRewardCaller(nonTranscoder.address) + .setRewardCaller(ZERO_ADDRESS) await expect(unsetRewardCallerTx) - .to.emit(bondingManager, "RewardCallerUnset") - .withArgs(transcoder.address, nonTranscoder.address) - - const rewardTx2 = bondingManager.connect(nonTranscoder).reward() - await expect(rewardTx2).to.be.revertedWith( - "caller must be an active transcoder or rewardCaller" - ) - }) - - it("impossible to set the same RewardCaller twice", async () => { - const setRewardCallerTx = bondingManager - .connect(transcoder) - .setRewardCaller(nonTranscoder.address) - await expect(setRewardCallerTx) - .to.emit(bondingManager, "RewardCallerSet") - .withArgs(transcoder.address, nonTranscoder.address) - - const setRewardCallerTx2 = bondingManager - .connect(transcoder) - .setRewardCaller(nonTranscoder.address) - await expect(setRewardCallerTx2).to.be.revertedWith( - "reward caller is already set" - ) - }) - - it("impossible to unset the RewardCaller for another transcoder", async () => { - const setRewardCallerTx = bondingManager - .connect(transcoder) - .setRewardCaller(nonTranscoder.address) - await expect(setRewardCallerTx) .to.emit(bondingManager, "RewardCallerSet") - .withArgs(transcoder.address, nonTranscoder.address) + .withArgs(transcoder.address, ZERO_ADDRESS) - const unsetRewardCallerTx = bondingManager + const rewardTx2 = bondingManager .connect(nonTranscoder) - .unsetRewardCaller(nonTranscoder.address) - await expect(unsetRewardCallerTx).to.be.revertedWith( - "only relevant transcoder can unset" + .rewardForTranscoder(transcoder.address) + await expect(rewardTx2).to.be.revertedWith( + "caller must be a reward caller set by the transcoder" ) }) }) From c3d1256108f8bf8dbe6b22891b8b231008c8949b Mon Sep 17 00:00:00 2001 From: SidestreamSweatyPumpkin Date: Mon, 8 Sep 2025 11:41:41 +0200 Subject: [PATCH 08/14] make mapping public --- contracts/bonding/BondingManager.sol | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/contracts/bonding/BondingManager.sol b/contracts/bonding/BondingManager.sol index 6f502dc5..c58630a7 100644 --- a/contracts/bonding/BondingManager.sol +++ b/contracts/bonding/BondingManager.sol @@ -103,7 +103,7 @@ contract BondingManager is ManagerProxyTarget, IBondingManager { uint256 public treasuryBalanceCeiling; // Allow reward() calls from one pre-defined address per transcoder - mapping(address => address) private transcoderToRewardCaller; + mapping(address => address) public transcoderToRewardCaller; // Check if sender is TicketBroker modifier onlyTicketBroker() { From 9a01599dae013a6265a167038fa6dce7d9b6decc Mon Sep 17 00:00:00 2001 From: SidestreamSweatyPumpkin Date: Mon, 8 Sep 2025 11:42:31 +0200 Subject: [PATCH 09/14] add checkpoint test for the reward caller --- test/unit/BondingManager.js | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/test/unit/BondingManager.js b/test/unit/BondingManager.js index 9ad38e91..34ad501e 100644 --- a/test/unit/BondingManager.js +++ b/test/unit/BondingManager.js @@ -6169,6 +6169,25 @@ describe("BondingManager", () => { "caller must be a reward caller set by the transcoder" ) }) + + it("should always checkpoint the reward recipient, not the RewardCaller", async () => { + await bondingManager + .connect(transcoder) + .setRewardCaller(nonTranscoder.address) + const rewardCallerTx = await bondingManager + .connect(nonTranscoder) + .rewardForTranscoder(transcoder.address) + + await expectCheckpoints(fixture, rewardCallerTx, { + account: transcoder.address, + startRound: currentRound + 2, + bondedAmount: 1000, + delegateAddress: transcoder.address, + delegatedAmount: 2000, + lastClaimRound: currentRound, + lastRewardRound: currentRound + 1 + }) + }) }) }) From fa8522e365ce7be07f21101a7ffa6873af168ee0 Mon Sep 17 00:00:00 2001 From: SidestreamSweatyPumpkin Date: Tue, 9 Sep 2025 18:06:37 +0200 Subject: [PATCH 10/14] fix typo: an active -> active --- contracts/bonding/BondingManager.sol | 2 +- test/unit/BondingManager.js | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/contracts/bonding/BondingManager.sol b/contracts/bonding/BondingManager.sol index c58630a7..da618cec 100644 --- a/contracts/bonding/BondingManager.sol +++ b/contracts/bonding/BondingManager.sol @@ -923,7 +923,7 @@ contract BondingManager is ManagerProxyTarget, IBondingManager { ) private whenSystemNotPaused currentRoundInitialized autoCheckpoint(_transcoder) { uint256 currentRound = roundsManager().currentRound(); - require(isActiveTranscoder(_transcoder), "transcoder must be an active"); + require(isActiveTranscoder(_transcoder), "transcoder must be active"); require( transcoders[_transcoder].lastRewardRound != currentRound, "caller has already called reward for the current round" diff --git a/test/unit/BondingManager.js b/test/unit/BondingManager.js index 34ad501e..b4f3b58b 100644 --- a/test/unit/BondingManager.js +++ b/test/unit/BondingManager.js @@ -5474,7 +5474,7 @@ describe("BondingManager", () => { it("should fail if caller is not a transcoder", async () => { await expect( bondingManager.connect(nonTranscoder).reward() - ).to.be.revertedWith("transcoder must be an active") + ).to.be.revertedWith("transcoder must be active") }) it("should fail if caller is registered but not an active transcoder yet in the current round", async () => { @@ -5484,7 +5484,7 @@ describe("BondingManager", () => { ) await expect( bondingManager.connect(transcoder).reward() - ).to.be.revertedWith("transcoder must be an active") + ).to.be.revertedWith("transcoder must be active") }) it("should fail if caller already called reward during the current round", async () => { From d27972bd684db998a2f065fe3e883f82203fd5f9 Mon Sep 17 00:00:00 2001 From: SidestreamSweatyPumpkin Date: Tue, 9 Sep 2025 18:08:55 +0200 Subject: [PATCH 11/14] fix: rewardForTranscoderWithHint, _rewardWithHint comments to mention _transcoder --- contracts/bonding/BondingManager.sol | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/contracts/bonding/BondingManager.sol b/contracts/bonding/BondingManager.sol index da618cec..c9b3d3ee 100644 --- a/contracts/bonding/BondingManager.sol +++ b/contracts/bonding/BondingManager.sol @@ -889,13 +889,13 @@ contract BondingManager is ManagerProxyTarget, IBondingManager { /** * @notice Mint token rewards for an active transcoder and its delegators and update the transcoder pool using an optional list hint if needed - * @dev If the caller is in the transcoder pool, the caller can provide an optional hint for its insertion position in the + * @dev If the `_transcoder` is in the transcoder pool, the caller can provide an optional hint for its insertion position in the * pool via the `_newPosPrev` and `_newPosNext` params. A linear search will be executed starting at the hint to find the correct position. * In the best case, the hint is the correct position so no search is executed. See SortedDoublyLL.sol for details on list hints * @dev Only callable by trusted rewardCaller * @param _transcoder Address of the transcoder on behalf of which the reward is called - * @param _newPosPrev Address of previous transcoder in pool if the caller is in the pool - * @param _newPosNext Address of next transcoder in pool if the caller is in the pool + * @param _newPosPrev Address of previous transcoder in pool if the `_transcoder` is in the pool + * @param _newPosNext Address of previous transcoder in pool if the `_transcoder` is in the pool */ function rewardForTranscoderWithHint( address _transcoder, @@ -909,7 +909,7 @@ contract BondingManager is ManagerProxyTarget, IBondingManager { /** * @notice Mint token rewards for an active transcoder and its delegators and update the transcoder pool using an optional list hint if needed - * @dev If the caller is in the transcoder pool, the caller can provide an optional hint for its insertion position in the + * @dev If the `_transcoder` is in the transcoder pool, the caller can provide an optional hint for its insertion position in the * pool via the `_newPosPrev` and `_newPosNext` params. A linear search will be executed starting at the hint to find the correct position. * In the best case, the hint is the correct position so no search is executed. See SortedDoublyLL.sol for details on list hints * @param _transcoder Address of the transcoder on behalf of which the reward is called From b1df47dd268b7410d7058e5a4d1b3838759f3630 Mon Sep 17 00:00:00 2001 From: SidestreamSweatyPumpkin Date: Wed, 10 Sep 2025 11:12:23 +0200 Subject: [PATCH 12/14] add missing interfaces --- contracts/bonding/IBondingManager.sol | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/contracts/bonding/IBondingManager.sol b/contracts/bonding/IBondingManager.sol index 653e5a97..b044e256 100644 --- a/contracts/bonding/IBondingManager.sol +++ b/contracts/bonding/IBondingManager.sol @@ -72,6 +72,20 @@ interface IBondingManager { function setCurrentRoundTotalActiveStake() external; + function setRewardCaller(address _rewardCaller) external; + + function reward() external; + + function rewardWithHint(address _newPosPrev, address _newPosNext) external; + + function rewardForTranscoder(address _transcoder) external; + + function rewardForTranscoderWithHint( + address _transcoder, + address _newPosPrev, + address _newPosNext + ) external; + // Public functions function getTranscoderPoolSize() external view returns (uint256); From 3e28524ef36b17972195f83b50e0f8685eb435be Mon Sep 17 00:00:00 2001 From: SidestreamSweatyPumpkin Date: Wed, 10 Sep 2025 11:57:48 +0200 Subject: [PATCH 13/14] add additional sanity checks --- test/unit/BondingManager.js | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/test/unit/BondingManager.js b/test/unit/BondingManager.js index b4f3b58b..42de876b 100644 --- a/test/unit/BondingManager.js +++ b/test/unit/BondingManager.js @@ -6170,6 +6170,41 @@ describe("BondingManager", () => { ) }) + it("should fail if system is paused", async () => { + await bondingManager + .connect(transcoder) + .setRewardCaller(nonTranscoder.address) + + await fixture.controller.pause() + const setRewardCallerTx = bondingManager + .connect(transcoder) + .setRewardCaller(nonTranscoder.address) + await expect(setRewardCallerTx).to.be.revertedWith( + "system is paused" + ) + + const rewardTx = bondingManager + .connect(nonTranscoder) + .rewardForTranscoder(transcoder.address) + await expect(rewardTx).to.be.revertedWith("system is paused") + }) + + it("should fail if current round is not initialized", async () => { + await bondingManager + .connect(transcoder) + .setRewardCaller(nonTranscoder.address) + await fixture.roundsManager.setMockBool( + functionSig("currentRoundInitialized()"), + false + ) + const rewardTx = bondingManager + .connect(nonTranscoder) + .rewardForTranscoder(transcoder.address) + await expect(rewardTx).to.be.revertedWith( + "current round is not initialized" + ) + }) + it("should always checkpoint the reward recipient, not the RewardCaller", async () => { await bondingManager .connect(transcoder) From ae18d5a2bed2027b72e67f5e076f85412ea8fc2d Mon Sep 17 00:00:00 2001 From: SidestreamSweatyPumpkin Date: Wed, 10 Sep 2025 18:25:15 +0200 Subject: [PATCH 14/14] update _rewardWithHint natspec comment --- contracts/bonding/BondingManager.sol | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/contracts/bonding/BondingManager.sol b/contracts/bonding/BondingManager.sol index c9b3d3ee..a7eda6bd 100644 --- a/contracts/bonding/BondingManager.sol +++ b/contracts/bonding/BondingManager.sol @@ -913,8 +913,8 @@ contract BondingManager is ManagerProxyTarget, IBondingManager { * pool via the `_newPosPrev` and `_newPosNext` params. A linear search will be executed starting at the hint to find the correct position. * In the best case, the hint is the correct position so no search is executed. See SortedDoublyLL.sol for details on list hints * @param _transcoder Address of the transcoder on behalf of which the reward is called - * @param _newPosPrev Address of previous transcoder in pool if the caller is in the pool - * @param _newPosNext Address of next transcoder in pool if the caller is in the pool + * @param _newPosPrev Address of previous transcoder in pool if `_transcoder` is in the pool + * @param _newPosNext Address of next transcoder in pool if `_transcoder` is in the pool */ function _rewardWithHint( address _transcoder,