Skip to content

Commit

Permalink
test getting future epoch target end time
Browse files Browse the repository at this point in the history
  • Loading branch information
jordanschalm authored and joshuahannan committed Nov 14, 2023
1 parent b3df211 commit ec49fc8
Show file tree
Hide file tree
Showing 5 changed files with 102 additions and 9 deletions.
25 changes: 16 additions & 9 deletions lib/go/templates/epoch_templates.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,16 @@ const (
epochRegisterDKGParticipantFilename = "epoch/node/register_dkg_participant.cdc"

// Scripts
getCurrentEpochCounterFilename = "epoch/scripts/get_epoch_counter.cdc"
getProposedEpochCounterFilename = "epoch/scripts/get_proposed_counter.cdc"
getEpochMetadataFilename = "epoch/scripts/get_epoch_metadata.cdc"
getConfigMetadataFilename = "epoch/scripts/get_config_metadata.cdc"
getTimingConfigFilename = "epoch/scripts/get_epoch_timing_config.cdc"
getEpochPhaseFilename = "epoch/scripts/get_epoch_phase.cdc"
getCurrentViewFilename = "epoch/scripts/get_current_view.cdc"
getFlowTotalSupplyFilename = "flowToken/scripts/get_supply.cdc"
getFlowBonusTokensFilename = "epoch/scripts/get_bonus_tokens.cdc"
getCurrentEpochCounterFilename = "epoch/scripts/get_epoch_counter.cdc"
getProposedEpochCounterFilename = "epoch/scripts/get_proposed_counter.cdc"
getEpochMetadataFilename = "epoch/scripts/get_epoch_metadata.cdc"
getConfigMetadataFilename = "epoch/scripts/get_config_metadata.cdc"
getTimingConfigFilename = "epoch/scripts/get_epoch_timing_config.cdc"
getTargetEndTimeForEpochFilename = "epoch/scripts/get_target_end_time_for_epoch.cdc"
getEpochPhaseFilename = "epoch/scripts/get_epoch_phase.cdc"
getCurrentViewFilename = "epoch/scripts/get_current_view.cdc"
getFlowTotalSupplyFilename = "flowToken/scripts/get_supply.cdc"
getFlowBonusTokensFilename = "epoch/scripts/get_bonus_tokens.cdc"

// test scripts
getRandomizeFilename = "epoch/scripts/get_randomize.cdc"
Expand Down Expand Up @@ -189,6 +190,12 @@ func GenerateGetEpochTimingConfigScript(env Environment) []byte {
return []byte(ReplaceAddresses(code, env))
}

func GenerateGetTargetEndTimeForEpochScript(env Environment) []byte {
code := assets.MustAssetString(getTargetEndTimeForEpochFilename)

return []byte(ReplaceAddresses(code, env))
}

func GenerateGetEpochPhaseScript(env Environment) []byte {
code := assets.MustAssetString(getEpochPhaseFilename)

Expand Down
23 changes: 23 additions & 0 deletions lib/go/templates/internal/assets/assets.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions lib/go/test/epoch_test_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,10 @@ func (evt EpochSetupEvent) dkgFinalViews() (cadence.UInt64, cadence.UInt64, cade
return fields[6].(cadence.UInt64), fields[7].(cadence.UInt64), fields[8].(cadence.UInt64)
}

func (evt EpochSetupEvent) targetEndTime() cadence.UInt64 {
return evt.Value.Fields[9].(cadence.UInt64)
}

type EpochCommitEvent flow.Event

func (evt EpochCommitEvent) Counter() cadence.UInt64 {
Expand Down Expand Up @@ -662,6 +666,7 @@ func verifyEpochSetup(
assertEqual(t, cadence.NewUInt64(expectedSetup.dkgPhase1FinalView), phase1View)
assertEqual(t, cadence.NewUInt64(expectedSetup.dkgPhase2FinalView), phase2View)
assertEqual(t, cadence.NewUInt64(expectedSetup.dkgPhase3FinalView), phase3View)
assertEqual(t, cadence.NewUInt64(expectedSetup.targetEndTime), emittedEvent.targetEndTime())
}

// / Verifies that the EpochCommit event values are equal to the provided expected values
Expand Down
49 changes: 49 additions & 0 deletions lib/go/test/flow_epoch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -467,6 +467,55 @@ func TestEpochPhaseMetadataChange(t *testing.T) {
})
}

func TestEpochTiming(t *testing.T) {
b, _, accountKeys, env := newTestSetup(t)

// Create new keys for the epoch account
idTableAccountKey, IDTableSigner := accountKeys.NewWithSigner()

// Deploys the staking contract, qc, dkg, and epoch lifecycle contract
// staking contract is deployed with default values (1.25M rewards, 8% cut)
initializeAllEpochContracts(t, b, idTableAccountKey, IDTableSigner, &env,
startEpochCounter, // start epoch counter
numEpochViews, // num views per epoch
numStakingViews, // num views for staking auction
numDKGViews, // num views for DKG phase
numClusters, // num collector clusters
randomSource, // random source
rewardIncreaseFactor)

epochTimingConfigResult := executeScriptAndCheck(t, b, templates.GenerateGetEpochTimingConfigScript(env), nil)

t.Run("should be able to observe end times for current epoch", func(t *testing.T) {
gotEndTimeCdc := executeScriptAndCheck(t, b, templates.GenerateGetTargetEndTimeForEpochScript(env), [][]byte{jsoncdc.MustEncode(cadence.UInt64(startEpochCounter))})
gotEndTime := gotEndTimeCdc.ToGoValue().(uint64)
expectedEndTime := expectedTargetEndTime(epochTimingConfigResult, startEpochCounter)
assert.Equal(t, expectedEndTime, gotEndTime)

// sanity check: should be within 10 minutes of the current time
gotEndTimeParsed := time.Unix(int64(gotEndTime), 0)
assert.InDelta(t, time.Now().Unix(), gotEndTimeParsed.Unix(), float64(10*time.Minute))
gotEndTimeParsed.Sub(time.Now())
})

t.Run("should be able to observe end times for future epochs", func(t *testing.T) {
var lastEndTime uint64
for _, epoch := range []uint64{1, 2, 3, 10, 100, 1000, 10_000} {
gotEndTimeCdc := executeScriptAndCheck(t, b, templates.GenerateGetTargetEndTimeForEpochScript(env), [][]byte{jsoncdc.MustEncode(cadence.UInt64(epoch))})
gotEndTime := gotEndTimeCdc.ToGoValue().(uint64)
expectedEndTime := expectedTargetEndTime(epochTimingConfigResult, epoch)
assert.Equal(t, expectedEndTime, gotEndTime)

// sanity check: target end time should be strictly increasing
if lastEndTime > 0 {
assert.Greater(t, gotEndTime, lastEndTime)
}

lastEndTime = gotEndTime
}
})
}

func TestEpochAdvance(t *testing.T) {
b, adapter, accountKeys, env := newTestSetup(t)

Expand Down
9 changes: 9 additions & 0 deletions transactions/epoch/scripts/get_target_end_time_for_epoch.cdc
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import FlowEpoch from 0xEPOCHADDRESS

pub fun main(targetEpoch: UInt64): UInt64 {
pre {
targetEpoch >= FlowEpoch.currentEpochCounter
}
let config = FlowEpoch.getEpochTimingConfig()
return config.getTargetEndTimeForEpoch(targetEpoch)
}

0 comments on commit ec49fc8

Please sign in to comment.