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 all 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
5 changes: 4 additions & 1 deletion modules/renter/contractor/allowance.go
Expand Up @@ -65,7 +65,10 @@ 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
if a.RenewWindow >= c.blockHeight {
return errors.New("unable to set allowance, Renew Window can't be greater than the Block Height")
}
c.currentPeriod = c.blockHeight - a.RenewWindow
}
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
48 changes: 37 additions & 11 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 @@ -929,7 +931,7 @@ func TestRenewFailing(t *testing.T) {
renterParams.Allowance = siatest.DefaultAllowance
renterParams.Allowance.Hosts = uint64(len(tg.Hosts()) - 1)
renterParams.Allowance.Period = 100
renterParams.Allowance.RenewWindow = 50
renterParams.Allowance.RenewWindow = 40
nodes, err := tg.AddNodes(renterParams)
if err != nil {
t.Fatal(err)
Expand Down Expand Up @@ -1220,6 +1222,7 @@ func TestRenterContractEndHeight(t *testing.T) {
if testing.Short() {
t.SkipNow()
}
// fdsfds
t.Parallel()

// Create a group for the subtests
Expand Down Expand Up @@ -1251,6 +1254,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 +1300,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 +1351,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 @@ -1654,9 +1671,9 @@ func TestRenterPersistData(t *testing.T) {
}()

// Set renter allowance to finish renter set up
// Currently /renter POST endpoint errors if the allowance
// is not previously set or passed in as an argument
err = r.RenterPostAllowance(siatest.DefaultAllowance)
a := siatest.DefaultAllowance
a.RenewWindow = 10
err = r.RenterPostAllowance(a)
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -2485,10 +2502,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
2 changes: 1 addition & 1 deletion siatest/testgroup.go
Expand Up @@ -86,7 +86,7 @@ func NewGroup(nodeParams ...node.NodeParams) (*TestGroup, error) {
return nil, errors.New("cannot fund group without miners")
}
miner := tg.Miners()[0]
for i := types.BlockHeight(0); i <= types.MaturityDelay+types.TaxHardforkHeight; i++ {
for i := types.BlockHeight(0); i <= types.MaturityDelay+types.TaxHardforkHeight+DefaultAllowance.RenewWindow; i++ {
if err := miner.MineBlock(); err != nil {
return nil, errors.AddContext(err, "failed to mine block for funding")
}
Expand Down