@@ -5326,12 +5326,22 @@ describe("BondingManager", () => {
53265326 ) . to . be . revertedWith ( "current round is not initialized" )
53275327 } )
53285328
5329- it ( "should fail if caller is not an active transcoder for the current round " , async ( ) => {
5329+ it ( "should fail if caller is not a transcoder" , async ( ) => {
53305330 await expect (
53315331 bondingManager . connect ( nonTranscoder ) . reward ( )
53325332 ) . to . be . revertedWith ( "caller must be an active transcoder" )
53335333 } )
53345334
5335+ it ( "should fail if caller is registered but not an active transcoder yet in the current round" , async ( ) => {
5336+ await fixture . roundsManager . setMockUint256 (
5337+ functionSig ( "currentRound()" ) ,
5338+ currentRound
5339+ )
5340+ await expect (
5341+ bondingManager . connect ( transcoder ) . reward ( )
5342+ ) . to . be . revertedWith ( "caller must be an active transcoder" )
5343+ } )
5344+
53355345 it ( "should fail if caller already called reward during the current round" , async ( ) => {
53365346 await bondingManager . connect ( transcoder ) . reward ( )
53375347 // This should fail because transcoder already called reward during the current round
@@ -5342,57 +5352,250 @@ describe("BondingManager", () => {
53425352 )
53435353 } )
53445354
5345- it ( "should update caller with rewards" , async ( ) => {
5346- const startDelegatedAmount = (
5347- await bondingManager . getDelegator ( transcoder . address )
5348- ) [ 3 ]
5349- const startTotalStake = await bondingManager . transcoderTotalStake (
5350- transcoder . address
5351- )
5352- const startNextTotalStake =
5353- await bondingManager . nextRoundTotalActiveStake ( )
5354- await bondingManager . connect ( transcoder ) . reward ( )
5355- const endDelegatedAmount = (
5356- await bondingManager . getDelegator ( transcoder . address )
5357- ) [ 3 ]
5358- const endTotalStake = await bondingManager . transcoderTotalStake (
5359- transcoder . address
5360- )
5361- const endNextTotalStake =
5362- await bondingManager . nextRoundTotalActiveStake ( )
5355+ describe ( "should update caller with rewards" , ( ) => {
5356+ it ( "when caller is active and registered" , async ( ) => {
5357+ const startDelegatedAmount = (
5358+ await bondingManager . getDelegator ( transcoder . address )
5359+ ) [ 3 ]
5360+ const startTotalStake =
5361+ await bondingManager . transcoderTotalStake (
5362+ transcoder . address
5363+ )
5364+ const startNextTotalStake =
5365+ await bondingManager . nextRoundTotalActiveStake ( )
5366+ await bondingManager . connect ( transcoder ) . reward ( )
5367+ const endDelegatedAmount = (
5368+ await bondingManager . getDelegator ( transcoder . address )
5369+ ) [ 3 ]
5370+ const endTotalStake = await bondingManager . transcoderTotalStake (
5371+ transcoder . address
5372+ )
5373+ const endNextTotalStake =
5374+ await bondingManager . nextRoundTotalActiveStake ( )
53635375
5364- const earningsPool =
5365- await bondingManager . getTranscoderEarningsPoolForRound (
5366- transcoder . address ,
5367- currentRound + 1
5376+ const earningsPool =
5377+ await bondingManager . getTranscoderEarningsPoolForRound (
5378+ transcoder . address ,
5379+ currentRound + 1
5380+ )
5381+ const nextRoundEarningsPool =
5382+ await bondingManager . getTranscoderEarningsPoolForRound (
5383+ transcoder . address ,
5384+ currentRound + 2
5385+ )
5386+ const expRewardFactor = constants . PERC_DIVISOR_PRECISE . add (
5387+ math . precise . percPoints (
5388+ BigNumber . from ( 500 ) ,
5389+ BigNumber . from ( 1000 )
5390+ )
53685391 )
5369- const expRewardFactor = constants . PERC_DIVISOR_PRECISE . add (
5370- math . precise . percPoints (
5371- BigNumber . from ( 500 ) ,
5372- BigNumber . from ( 1000 )
5392+ assert . equal (
5393+ earningsPool . cumulativeRewardFactor . toString ( ) ,
5394+ expRewardFactor . toString ( ) ,
5395+ "should update cumulativeRewardFactor in earningsPool"
5396+ )
5397+ assert . equal (
5398+ nextRoundEarningsPool . totalStake . sub ( endTotalStake ) ,
5399+ 0 ,
5400+ "should update next round earnings pool totalStake"
53735401 )
5374- )
5375- assert . equal (
5376- earningsPool . cumulativeRewardFactor . toString ( ) ,
5377- expRewardFactor . toString ( ) ,
5378- "should update cumulativeRewardFactor in earningsPool"
5379- )
53805402
5381- assert . equal (
5382- endDelegatedAmount . sub ( startDelegatedAmount ) ,
5383- 1000 ,
5384- "should update delegatedAmount with new rewards"
5385- )
5386- assert . equal (
5387- endTotalStake . sub ( startTotalStake ) ,
5388- 1000 ,
5389- "should update transcoder's total stake in the pool with new rewards"
5390- )
5391- assert . equal (
5392- endNextTotalStake . sub ( startNextTotalStake ) ,
5393- 1000 ,
5394- "should update next total stake with new rewards"
5395- )
5403+ assert . equal (
5404+ endDelegatedAmount . sub ( startDelegatedAmount ) ,
5405+ 1000 ,
5406+ "should update delegatedAmount with new rewards"
5407+ )
5408+ assert . equal (
5409+ endTotalStake . sub ( startTotalStake ) ,
5410+ 1000 ,
5411+ "should update transcoder's total stake in the pool with new rewards"
5412+ )
5413+ assert . equal (
5414+ endNextTotalStake . sub ( startNextTotalStake ) ,
5415+ 1000 ,
5416+ "should update next total stake with new rewards"
5417+ )
5418+ } )
5419+
5420+ it ( "when caller is deactivating on the next round" , async ( ) => {
5421+ // add 2 transcoders with much higher stake to kick the existing transcoder out of the active pool
5422+ for ( let i = 0 ; i < 2 ; i ++ ) {
5423+ const newTranscoder = signers [ 10 + i ]
5424+ await bondingManager
5425+ . connect ( newTranscoder )
5426+ . bond ( 10000 , newTranscoder . address )
5427+ await bondingManager
5428+ . connect ( newTranscoder )
5429+ . transcoder ( 50 * PERC_MULTIPLIER , 10 )
5430+ }
5431+ let { deactivationRound} = await bondingManager . getTranscoder (
5432+ transcoder . address
5433+ )
5434+ assert . equal (
5435+ deactivationRound . toNumber ( ) ,
5436+ currentRound + 2 ,
5437+ "transcoder should be deactivating"
5438+ )
5439+
5440+ const startDelegatedAmount = (
5441+ await bondingManager . getDelegator ( transcoder . address )
5442+ ) [ 3 ]
5443+ const startTotalStake =
5444+ await bondingManager . transcoderTotalStake (
5445+ transcoder . address
5446+ )
5447+ const startNextTotalStake =
5448+ await bondingManager . nextRoundTotalActiveStake ( )
5449+
5450+ await bondingManager . connect ( transcoder ) . reward ( )
5451+ const endDelegatedAmount = (
5452+ await bondingManager . getDelegator ( transcoder . address )
5453+ ) [ 3 ]
5454+ const endTotalStake = await bondingManager . transcoderTotalStake (
5455+ transcoder . address
5456+ )
5457+ const endNextTotalStake =
5458+ await bondingManager . nextRoundTotalActiveStake ( )
5459+
5460+ const earningsPool =
5461+ await bondingManager . getTranscoderEarningsPoolForRound (
5462+ transcoder . address ,
5463+ currentRound + 1
5464+ )
5465+ const nextRoundEarningsPool =
5466+ await bondingManager . getTranscoderEarningsPoolForRound (
5467+ transcoder . address ,
5468+ currentRound + 2
5469+ )
5470+ ; ( { deactivationRound} = await bondingManager . getTranscoder (
5471+ transcoder . address
5472+ ) )
5473+
5474+ assert . equal (
5475+ deactivationRound . toNumber ( ) ,
5476+ currentRound + 2 ,
5477+ "transcoder should still be deactivating"
5478+ )
5479+
5480+ const expRewardFactor = constants . PERC_DIVISOR_PRECISE . add (
5481+ math . precise . percPoints (
5482+ BigNumber . from ( 500 ) ,
5483+ BigNumber . from ( 1000 )
5484+ )
5485+ )
5486+ assert . equal (
5487+ earningsPool . cumulativeRewardFactor . toString ( ) ,
5488+ expRewardFactor . toString ( ) ,
5489+ "should update cumulativeRewardFactor in earningsPool"
5490+ )
5491+ assert . equal (
5492+ nextRoundEarningsPool . totalStake . sub ( endTotalStake ) ,
5493+ 0 ,
5494+ "should update next round earnings pool totalStake"
5495+ )
5496+
5497+ assert . equal (
5498+ endDelegatedAmount . sub ( startDelegatedAmount ) ,
5499+ 1000 ,
5500+ "should update delegatedAmount with new rewards"
5501+ )
5502+ assert . equal (
5503+ endTotalStake . sub ( startTotalStake ) ,
5504+ 1000 ,
5505+ "should update transcoder's total stake in the pool with new rewards"
5506+ )
5507+ assert . equal (
5508+ endNextTotalStake . sub ( startNextTotalStake ) ,
5509+ 0 ,
5510+ "should not update next total stake since the transcoder will not be active"
5511+ )
5512+ } )
5513+
5514+ it ( "when caller is deactivating but not registered" , async ( ) => {
5515+ // full unbond to resign transcoder from active pool
5516+ await bondingManager . connect ( transcoder ) . unbond ( 1000 )
5517+ assert . isTrue (
5518+ await bondingManager . isActiveTranscoder ( transcoder . address )
5519+ )
5520+ assert . isFalse (
5521+ await bondingManager . isRegisteredTranscoder (
5522+ transcoder . address
5523+ )
5524+ )
5525+
5526+ const startDelegatedAmount = (
5527+ await bondingManager . getDelegator ( transcoder . address )
5528+ ) [ 3 ]
5529+ const startTotalStake =
5530+ await bondingManager . transcoderTotalStake (
5531+ transcoder . address
5532+ )
5533+ const startNextTotalStake =
5534+ await bondingManager . nextRoundTotalActiveStake ( )
5535+
5536+ await bondingManager . connect ( transcoder ) . reward ( )
5537+
5538+ const endDelegatedAmount = (
5539+ await bondingManager . getDelegator ( transcoder . address )
5540+ ) [ 3 ]
5541+ const endTotalStake = await bondingManager . transcoderTotalStake (
5542+ transcoder . address
5543+ )
5544+ const endNextTotalStake =
5545+ await bondingManager . nextRoundTotalActiveStake ( )
5546+
5547+ const earningsPool =
5548+ await bondingManager . getTranscoderEarningsPoolForRound (
5549+ transcoder . address ,
5550+ currentRound + 1
5551+ )
5552+ const nextRoundEarningsPool =
5553+ await bondingManager . getTranscoderEarningsPoolForRound (
5554+ transcoder . address ,
5555+ currentRound + 2
5556+ )
5557+
5558+ const { deactivationRound} =
5559+ await bondingManager . getTranscoder ( transcoder . address )
5560+ assert . equal (
5561+ deactivationRound . toNumber ( ) ,
5562+ currentRound + 2 ,
5563+ "transcoder should still be deactivating"
5564+ )
5565+
5566+ const expRewardFactor = constants . PERC_DIVISOR_PRECISE . add (
5567+ math . precise . percPoints (
5568+ BigNumber . from ( 500 ) ,
5569+ BigNumber . from ( 1000 )
5570+ )
5571+ )
5572+ assert . equal (
5573+ earningsPool . cumulativeRewardFactor . toString ( ) ,
5574+ expRewardFactor . toString ( ) ,
5575+ "should update cumulativeRewardFactor in earningsPool"
5576+ )
5577+ assert . equal (
5578+ nextRoundEarningsPool . totalStake . sub ( endTotalStake ) ,
5579+ 0 ,
5580+ "should update next round earnings pool totalStake"
5581+ )
5582+
5583+ assert . equal (
5584+ endDelegatedAmount . sub ( startDelegatedAmount ) ,
5585+ 1000 ,
5586+ "should update delegatedAmount with new rewards"
5587+ )
5588+ assert . equal (
5589+ endTotalStake . sub ( startTotalStake ) ,
5590+ 1000 ,
5591+ "should update transcoder's total stake in the pool with new rewards"
5592+ )
5593+ assert . equal (
5594+ endNextTotalStake . sub ( startNextTotalStake ) ,
5595+ 0 ,
5596+ "should not update next total stake since the transcoder will not be active"
5597+ )
5598+ } )
53965599 } )
53975600
53985601 it ( "should checkpoint the caller state" , async ( ) => {
0 commit comments