Skip to content

Commit ccc82f4

Browse files
feat: Add reward caller (using transcoder-to-reward caller mapping direction) (#648)
1 parent 3337250 commit ccc82f4

5 files changed

Lines changed: 212 additions & 14 deletions

File tree

‎.github/workflows/test.yaml‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,9 @@ jobs:
4747
name: ${{ github.event.repository.name }}
4848
token: ${{ secrets.CI_CODECOV_TOKEN }}
4949

50+
- name: Enforce test coverage threshold
51+
run: yarn test:coverage:check
52+
5053
editorconfig:
5154
name: Run editorconfig checker
5255
runs-on: ubuntu-latest

‎contracts/bonding/BondingManager.sol‎

Lines changed: 67 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,10 @@ contract BondingManager is ManagerProxyTarget, IBondingManager {
102102
// If the balance of the treasury in LPT is above this value, automatic treasury contributions will halt.
103103
uint256 public treasuryBalanceCeiling;
104104

105+
// Allow reward() calls from one pre-defined address per transcoder.
106+
// @dev Since the setter is callable by any address, a mapping key does not guarantee to be a registered or active transcoder.
107+
mapping(address => address) public transcoderToRewardCaller;
108+
105109
// Check if sender is TicketBroker
106110
modifier onlyTicketBroker() {
107111
_onlyTicketBroker();
@@ -188,6 +192,16 @@ contract BondingManager is ManagerProxyTarget, IBondingManager {
188192
emit ParameterUpdate("numActiveTranscoders");
189193
}
190194

195+
/**
196+
* @notice Set a reward caller for a transcoder
197+
* @param _rewardCaller Address of the new reward caller
198+
* @dev By providing address(0) the reward caller can be unset
199+
*/
200+
function setRewardCaller(address _rewardCaller) external whenSystemNotPaused {
201+
transcoderToRewardCaller[msg.sender] = _rewardCaller;
202+
emit RewardCallerSet(msg.sender, _rewardCaller);
203+
}
204+
191205
/**
192206
* @notice Sets commission rates as a transcoder and if the caller is not in the transcoder pool tries to add it
193207
* @dev Percentages are represented as numerators of fractions over MathUtils.PERC_DIVISOR
@@ -294,6 +308,15 @@ contract BondingManager is ManagerProxyTarget, IBondingManager {
294308
rewardWithHint(address(0), address(0));
295309
}
296310

311+
/**
312+
* @notice Mint token rewards for an active transcoder and its delegators
313+
* @param _transcoder Address of the transcoder on behalf of which the reward is called
314+
* @dev Only callable by trusted rewardCaller
315+
*/
316+
function rewardForTranscoder(address _transcoder) external {
317+
rewardForTranscoderWithHint(_transcoder, address(0), address(0));
318+
}
319+
297320
/**
298321
* @notice Update transcoder's fee pool. Only callable by the TicketBroker
299322
* @param _transcoder Transcoder address
@@ -863,21 +886,53 @@ contract BondingManager is ManagerProxyTarget, IBondingManager {
863886
* @param _newPosPrev Address of previous transcoder in pool if the caller is in the pool
864887
* @param _newPosNext Address of next transcoder in pool if the caller is in the pool
865888
*/
866-
function rewardWithHint(address _newPosPrev, address _newPosNext)
867-
public
868-
whenSystemNotPaused
869-
currentRoundInitialized
870-
autoCheckpoint(msg.sender)
871-
{
889+
function rewardWithHint(address _newPosPrev, address _newPosNext) public {
890+
_rewardWithHint(msg.sender, _newPosPrev, _newPosNext);
891+
}
892+
893+
/**
894+
* @notice Mint token rewards for an active transcoder and its delegators and update the transcoder pool using an optional list hint if needed
895+
* @dev If the `_transcoder` is in the transcoder pool, the caller can provide an optional hint for its insertion position in the
896+
* pool via the `_newPosPrev` and `_newPosNext` params. A linear search will be executed starting at the hint to find the correct position.
897+
* In the best case, the hint is the correct position so no search is executed. See SortedDoublyLL.sol for details on list hints
898+
* @dev Only callable by trusted rewardCaller
899+
* @param _transcoder Address of the transcoder on behalf of which the reward is called
900+
* @param _newPosPrev Address of previous transcoder in pool if the `_transcoder` is in the pool
901+
* @param _newPosNext Address of next transcoder in pool if the `_transcoder` is in the pool
902+
*/
903+
function rewardForTranscoderWithHint(
904+
address _transcoder,
905+
address _newPosPrev,
906+
address _newPosNext
907+
) public {
908+
address rewardCaller = transcoderToRewardCaller[_transcoder];
909+
require(rewardCaller == msg.sender, "caller must be a reward caller set by the transcoder");
910+
_rewardWithHint(_transcoder, _newPosPrev, _newPosNext);
911+
}
912+
913+
/**
914+
* @notice Mint token rewards for an active transcoder and its delegators and update the transcoder pool using an optional list hint if needed
915+
* @dev If the `_transcoder` is in the transcoder pool, the caller can provide an optional hint for its insertion position in the
916+
* pool via the `_newPosPrev` and `_newPosNext` params. A linear search will be executed starting at the hint to find the correct position.
917+
* In the best case, the hint is the correct position so no search is executed. See SortedDoublyLL.sol for details on list hints
918+
* @param _transcoder Address of the transcoder on behalf of which the reward is called
919+
* @param _newPosPrev Address of previous transcoder in pool if `_transcoder` is in the pool
920+
* @param _newPosNext Address of next transcoder in pool if `_transcoder` is in the pool
921+
*/
922+
function _rewardWithHint(
923+
address _transcoder,
924+
address _newPosPrev,
925+
address _newPosNext
926+
) private whenSystemNotPaused currentRoundInitialized autoCheckpoint(_transcoder) {
872927
uint256 currentRound = roundsManager().currentRound();
873928

874-
require(isActiveTranscoder(msg.sender), "caller must be an active transcoder");
929+
require(isActiveTranscoder(_transcoder), "transcoder must be active");
875930
require(
876-
transcoders[msg.sender].lastRewardRound != currentRound,
931+
transcoders[_transcoder].lastRewardRound != currentRound,
877932
"caller has already called reward for the current round"
878933
);
879934

880-
Transcoder storage t = transcoders[msg.sender];
935+
Transcoder storage t = transcoders[_transcoder];
881936
EarningsPool.Data storage earningsPool = t.earningsPoolPerRound[currentRound];
882937

883938
// Set last round that transcoder called reward
@@ -910,17 +965,17 @@ contract BondingManager is ManagerProxyTarget, IBondingManager {
910965

911966
mtr.trustedTransferTokens(trsry, treasuryRewards);
912967

913-
emit TreasuryReward(msg.sender, trsry, treasuryRewards);
968+
emit TreasuryReward(_transcoder, trsry, treasuryRewards);
914969
}
915970

916971
uint256 transcoderRewards = totalRewardTokens.sub(treasuryRewards);
917972

918-
updateTranscoderWithRewards(msg.sender, transcoderRewards, currentRound, _newPosPrev, _newPosNext);
973+
updateTranscoderWithRewards(_transcoder, transcoderRewards, currentRound, _newPosPrev, _newPosNext);
919974

920975
// Set last round that transcoder called reward
921976
t.lastRewardRound = currentRound;
922977

923-
emit Reward(msg.sender, transcoderRewards);
978+
emit Reward(_transcoder, transcoderRewards);
924979
}
925980

926981
/**

‎contracts/bonding/IBondingManager.sol‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ interface IBondingManager {
4444
uint256 startRound,
4545
uint256 endRound
4646
);
47+
event RewardCallerSet(address indexed transcoder, address indexed rewardCaller);
4748

4849
// Deprecated events
4950
// These event signatures can be used to construct the appropriate topic hashes to filter for past logs corresponding
@@ -71,6 +72,20 @@ interface IBondingManager {
7172

7273
function setCurrentRoundTotalActiveStake() external;
7374

75+
function setRewardCaller(address _rewardCaller) external;
76+
77+
function reward() external;
78+
79+
function rewardWithHint(address _newPosPrev, address _newPosNext) external;
80+
81+
function rewardForTranscoder(address _transcoder) external;
82+
83+
function rewardForTranscoderWithHint(
84+
address _transcoder,
85+
address _newPosPrev,
86+
address _newPosNext
87+
) external;
88+
7489
// Public functions
7590
function getTranscoderPoolSize() external view returns (uint256);
7691

‎package.json‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
"clean": "rm -rf cache artifacts typechain",
2222
"compile": "npx hardhat compile",
2323
"test:coverage": "npx hardhat coverage",
24+
"test:coverage:check": "npx istanbul check-coverage ./coverage.json --statements 100 --branches 100 --functions 100 --lines 100",
2425
"test": "npx hardhat test",
2526
"test:unit": "npx hardhat test test/unit/*.*",
2627
"test:integration": "npx hardhat test test/integration/**",

‎test/unit/BondingManager.js‎

Lines changed: 126 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5532,7 +5532,7 @@ describe("BondingManager", () => {
55325532
it("should fail if caller is not a transcoder", async () => {
55335533
await expect(
55345534
bondingManager.connect(nonTranscoder).reward()
5535-
).to.be.revertedWith("caller must be an active transcoder")
5535+
).to.be.revertedWith("transcoder must be active")
55365536
})
55375537

55385538
it("should fail if caller is registered but not an active transcoder yet in the current round", async () => {
@@ -5542,7 +5542,7 @@ describe("BondingManager", () => {
55425542
)
55435543
await expect(
55445544
bondingManager.connect(transcoder).reward()
5545-
).to.be.revertedWith("caller must be an active transcoder")
5545+
).to.be.revertedWith("transcoder must be active")
55465546
})
55475547

55485548
it("should fail if caller already called reward during the current round", async () => {
@@ -6158,6 +6158,130 @@ describe("BondingManager", () => {
61586158
atCeilingTest("when above limit", 1500)
61596159
})
61606160
})
6161+
6162+
describe("reward delegation", () => {
6163+
const transcoderRewards = 1000
6164+
6165+
it("should allow a transcoder to call reward even if RewardCaller is set", async () => {
6166+
const setRewardCallerTx = bondingManager
6167+
.connect(transcoder)
6168+
.setRewardCaller(nonTranscoder.address)
6169+
await expect(setRewardCallerTx)
6170+
.to.emit(bondingManager, "RewardCallerSet")
6171+
.withArgs(transcoder.address, nonTranscoder.address)
6172+
6173+
const rewardTx = bondingManager.connect(transcoder).reward()
6174+
await expect(rewardTx)
6175+
.to.emit(bondingManager, "Reward")
6176+
.withArgs(transcoder.address, transcoderRewards)
6177+
6178+
await fixture.roundsManager.setMockUint256(
6179+
functionSig("currentRound()"),
6180+
currentRound + 3
6181+
)
6182+
6183+
const unsetRewardCallerTx = bondingManager
6184+
.connect(transcoder)
6185+
.setRewardCaller(ZERO_ADDRESS)
6186+
await expect(unsetRewardCallerTx)
6187+
.to.emit(bondingManager, "RewardCallerSet")
6188+
.withArgs(transcoder.address, ZERO_ADDRESS)
6189+
6190+
const rewardTx2 = bondingManager.connect(transcoder).reward()
6191+
await expect(rewardTx2)
6192+
.to.emit(bondingManager, "Reward")
6193+
.withArgs(transcoder.address, transcoderRewards)
6194+
})
6195+
6196+
it("should allow a RewardCaller to call reward", async () => {
6197+
const setRewardCallerTx = bondingManager
6198+
.connect(transcoder)
6199+
.setRewardCaller(nonTranscoder.address)
6200+
await expect(setRewardCallerTx)
6201+
.to.emit(bondingManager, "RewardCallerSet")
6202+
.withArgs(transcoder.address, nonTranscoder.address)
6203+
6204+
const rewardTx = bondingManager
6205+
.connect(nonTranscoder)
6206+
.rewardForTranscoder(transcoder.address)
6207+
await expect(rewardTx)
6208+
.to.emit(bondingManager, "Reward")
6209+
.withArgs(transcoder.address, transcoderRewards)
6210+
6211+
await fixture.roundsManager.setMockUint256(
6212+
functionSig("currentRound()"),
6213+
currentRound + 3
6214+
)
6215+
6216+
const unsetRewardCallerTx = bondingManager
6217+
.connect(transcoder)
6218+
.setRewardCaller(ZERO_ADDRESS)
6219+
await expect(unsetRewardCallerTx)
6220+
.to.emit(bondingManager, "RewardCallerSet")
6221+
.withArgs(transcoder.address, ZERO_ADDRESS)
6222+
6223+
const rewardTx2 = bondingManager
6224+
.connect(nonTranscoder)
6225+
.rewardForTranscoder(transcoder.address)
6226+
await expect(rewardTx2).to.be.revertedWith(
6227+
"caller must be a reward caller set by the transcoder"
6228+
)
6229+
})
6230+
6231+
it("should fail if system is paused", async () => {
6232+
await bondingManager
6233+
.connect(transcoder)
6234+
.setRewardCaller(nonTranscoder.address)
6235+
6236+
await fixture.controller.pause()
6237+
const setRewardCallerTx = bondingManager
6238+
.connect(transcoder)
6239+
.setRewardCaller(nonTranscoder.address)
6240+
await expect(setRewardCallerTx).to.be.revertedWith(
6241+
"system is paused"
6242+
)
6243+
6244+
const rewardTx = bondingManager
6245+
.connect(nonTranscoder)
6246+
.rewardForTranscoder(transcoder.address)
6247+
await expect(rewardTx).to.be.revertedWith("system is paused")
6248+
})
6249+
6250+
it("should fail if current round is not initialized", async () => {
6251+
await bondingManager
6252+
.connect(transcoder)
6253+
.setRewardCaller(nonTranscoder.address)
6254+
await fixture.roundsManager.setMockBool(
6255+
functionSig("currentRoundInitialized()"),
6256+
false
6257+
)
6258+
const rewardTx = bondingManager
6259+
.connect(nonTranscoder)
6260+
.rewardForTranscoder(transcoder.address)
6261+
await expect(rewardTx).to.be.revertedWith(
6262+
"current round is not initialized"
6263+
)
6264+
})
6265+
6266+
it("should always checkpoint the reward recipient, not the RewardCaller", async () => {
6267+
await bondingManager
6268+
.connect(transcoder)
6269+
.setRewardCaller(nonTranscoder.address)
6270+
const rewardCallerTx = await bondingManager
6271+
.connect(nonTranscoder)
6272+
.rewardForTranscoder(transcoder.address)
6273+
6274+
await expectCheckpoints(fixture, rewardCallerTx, {
6275+
account: transcoder.address,
6276+
startRound: currentRound + 2,
6277+
bondedAmount: 1000,
6278+
delegateAddress: transcoder.address,
6279+
delegatedAmount: 2000,
6280+
lastClaimRound: currentRound,
6281+
lastRewardRound: currentRound + 1
6282+
})
6283+
})
6284+
})
61616285
})
61626286

61636287
describe("updateTranscoderWithFees", () => {

0 commit comments

Comments
 (0)