Skip to content
This repository has been archived by the owner on Nov 2, 2018. It is now read-only.

Update initial current period to be set in the past when allowance is created #3157

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion modules/renter/contractor/allowance.go
Expand Up @@ -65,7 +65,7 @@ func (c *Contractor) SetAllowance(a modules.Allowance) error {
// set the current period to the blockheight if the existing allowance is
// empty
if reflect.DeepEqual(c.allowance, modules.Allowance{}) {
c.currentPeriod = c.blockHeight
c.currentPeriod = c.blockHeight - a.RenewWindow

This comment was marked as resolved.

}
c.allowance = a
err := c.saveSync()
Expand Down
12 changes: 6 additions & 6 deletions modules/renter/contractor/contracts.go
Expand Up @@ -25,7 +25,7 @@ var (
// contractEndHeight returns the height at which the Contractor's contracts
// end. If there are no contracts, it returns zero.
func (c *Contractor) contractEndHeight() types.BlockHeight {
return c.currentPeriod + c.allowance.Period
return c.currentPeriod + c.allowance.Period + c.allowance.RenewWindow
}

// managedContractUtility returns the ContractUtility for a contract with a given id.
Expand Down Expand Up @@ -351,14 +351,12 @@ func (c *Contractor) threadedContractMaintenance() {
var renewSet []renewal

c.mu.RLock()
currentPeriod := c.currentPeriod
allowance := c.allowance
blockHeight := c.blockHeight
c.mu.RUnlock()

// Grab the end height that should be used for the contracts created
// in the current period.
endHeight = currentPeriod + allowance.Period
endHeight = c.contractEndHeight()
c.mu.RUnlock()

// Determine how many funds have been used already in this billing cycle,
// and how many funds are remaining. We have to calculate these numbers
Expand Down Expand Up @@ -565,7 +563,9 @@ func (c *Contractor) threadedContractMaintenance() {
}

// Calculate endHeight for renewed contracts
endHeight = currentPeriod + allowance.Period
c.mu.RLock()
endHeight = c.contractEndHeight()
c.mu.RUnlock()

// Perform the actual renew. If the renew fails, return the
// contract. If the renew fails we check how often it has failed
Expand Down
8 changes: 2 additions & 6 deletions modules/renter/contractor/update.go
Expand Up @@ -56,12 +56,8 @@ func (c *Contractor) ProcessConsensusChange(cc modules.ConsensusChange) {
}

// If we have entered the next period, update currentPeriod
// NOTE: "period" refers to the duration of contracts, whereas "cycle"
// refers to how frequently the period metrics are reset.
// TODO: How to make this more explicit.
cycleLen := c.allowance.Period - c.allowance.RenewWindow
if c.blockHeight >= c.currentPeriod+cycleLen {
c.currentPeriod += cycleLen
if c.blockHeight >= c.currentPeriod+c.allowance.Period {
c.currentPeriod += c.allowance.Period
// COMPATv1.0.4-lts
// if we were storing a special metrics contract, it will be invalid
// after we enter the next period.
Expand Down
4 changes: 2 additions & 2 deletions modules/renter/contractor/update_test.go
Expand Up @@ -79,7 +79,7 @@ func TestIntegrationAutoRenew(t *testing.T) {

// check renewed contract
contract = c.Contracts()[0]
endHeight := c.CurrentPeriod() + c.allowance.Period
endHeight := c.contractEndHeight()
if contract.EndHeight != endHeight {
t.Fatalf("Wrong end height, expected %v got %v\n", endHeight, contract.EndHeight)
}
Expand Down Expand Up @@ -147,7 +147,7 @@ func TestIntegrationRenewInvalidate(t *testing.T) {

// check renewed contract
contract = c.Contracts()[0]
endHeight := c.CurrentPeriod() + c.allowance.Period
endHeight := c.contractEndHeight()
c.mu.Lock()
if contract.EndHeight != endHeight {
t.Fatalf("Wrong end height, expected %v got %v\n", endHeight, contract.EndHeight)
Expand Down
39 changes: 32 additions & 7 deletions siatest/renter/renter_test.go
Expand Up @@ -452,7 +452,9 @@ func testSingleFileGet(t *testing.T, tg *siatest.TestGroup) {
if err != nil {
t.Fatal("Failed to request single file", err)
}
if file != f {
if !reflect.DeepEqual(f, file) {
t.Log(f)
t.Log(file)
t.Fatal("Single file queries does not match file previously requested.")
}
}
Expand Down Expand Up @@ -1251,6 +1253,19 @@ func TestRenterContractEndHeight(t *testing.T) {
renewWindow := rg.Settings.Allowance.RenewWindow
numRenewals := 0

// Check if the current period was set in the past
cg, err := r.ConsensusGet()
if err != nil {
t.Fatal(err)
}
if currentPeriodStart > cg.Height-renewWindow {
t.Fatalf(`Current period not set in the past as expected.
CP: %v
BH: %v
RW: %v
`, currentPeriodStart, cg.Height, renewWindow)
}

// Confirm Contracts were created as expected. There should be 2 active
// contracts and no inactive or expired contracts
err = build.Retry(200, 100*time.Millisecond, func() error {
Expand Down Expand Up @@ -1284,11 +1299,12 @@ func TestRenterContractEndHeight(t *testing.T) {

// Confirm contract end heights were set properly
for _, c := range rc.ActiveContracts {
if c.EndHeight != currentPeriodStart+period {
if c.EndHeight != currentPeriodStart+period+renewWindow {
t.Log("Endheight:", c.EndHeight)
t.Log("Allowance Period:", period)
t.Log("Renew Window:", renewWindow)
t.Log("Current Period:", currentPeriodStart)
t.Fatal("Contract endheight not set to Current period + Allowance Period")
t.Fatal("Contract endheight not set to Current period + Allowance Period + Renew Window")
}
}

Expand Down Expand Up @@ -1334,12 +1350,12 @@ func TestRenterContractEndHeight(t *testing.T) {
t.Fatal(err)
}
for _, c := range rc.ActiveContracts {
if c.EndHeight != currentPeriodStart+(2*period)-renewWindow && c.GoodForRenew {
if c.EndHeight != currentPeriodStart+(2*period)+renewWindow && c.GoodForRenew {
t.Log("Endheight:", c.EndHeight)
t.Log("Allowance Period:", period)
t.Log("Renew Window:", renewWindow)
t.Log("Current Period:", currentPeriodStart)
t.Fatal("Contract endheight not set to Current period + 2 * Allowance Period - Renew Window")
t.Fatal("Contract endheight not set to Current period + 2 * Allowance Period + Renew Window")
}
}

Expand Down Expand Up @@ -2485,10 +2501,19 @@ func checkRenewedContracts(renewedContracts []api.RenterContract) error {
func renewContractsByRenewWindow(renter *siatest.TestNode, tg *siatest.TestGroup) error {
rg, err := renter.RenterGet()
if err != nil {
return errors.AddContext(err, "failed to get RenterGet")
return err
}
cg, err := renter.ConsensusGet()
if err != nil {
return err
}
rc, err := renter.RenterContractsGet()
if err != nil {
return err
}
blocksToMine := rc.ActiveContracts[0].EndHeight - rg.Settings.Allowance.RenewWindow - cg.Height
m := tg.Miners()[0]
for i := 0; i < int(rg.Settings.Allowance.Period-rg.Settings.Allowance.RenewWindow); i++ {
for i := 0; i < int(blocksToMine); i++ {
if err = m.MineBlock(); err != nil {
return err
}
Expand Down