[NebulousLabs/Sia] Calculate spending from contracts renewed during current period (#3040)

MSevey commented on this pull request.

> + renterParams.SkipSetAllowance = true
+ if err = tg.AddNodes(renterParams); err != nil {
+ t.Fatal(err)
+ }
+
+ // Get renter’s initial siacoin balance
+ r := tg.Renters()[0]
+ wg, err := r.WalletGet()
+ if err != nil {
+ t.Fatal(«Failed to get wallet:», err)
+ }
+ initialBalance := wg.ConfirmedSiacoinBalance
+
+ // Set allowance
+ if err = tg.SetRenterAllowance(r, siatest.DefaultAllowance); err != nil {
+ t.Fatal(«Failed to set renter:», err)

Should say «Failed to set renter allowance»
will update

> + // Getting initial financial metrics
+ // Setting variables to easier reference
+ rg, err := r.RenterGet()
+ if err != nil {
+ t.Fatal(«Failed to get RenterGet:», err)
+ }
+ fm := rg.FinancialMetrics
+
+ // Check balance after allowance is set
+ wg, err = r.WalletGet()
+ if err != nil {
+ t.Fatal(«Failed to get wallet:», err)
+ }
+ balanceAfterSetAllowance := initialBalance.Sub(fm.TotalAllocated)
+ if balanceAfterSetAllowance.Cmp(wg.ConfirmedSiacoinBalance) != 0 {
+ t.Fatalf(«Renter Reported Spending does not equal wallet confirmed balance, n%v != n%v», balanceAfterSetAllowance, wg.ConfirmedSiacoinBalance)

Break into two lines for cleaner readability

> + }
+
+ // Waiting for nodes to sync
+ if err = tg.Sync(); err != nil {
+ t.Fatal(err)
+ }
+
+ // Check spending after contract renewal
+ wg, err = r.WalletGet()
+ if err != nil {
+ t.Fatal(«Failed to get wallet:», err)
+ }
+ rg, err = r.RenterGet()
+ if err != nil {
+ t.Fatal(«Failed to get RenterGet:», err)
+ }

Remove, duplicated

> +
+ fm = rg.FinancialMetrics
+ totalSpent := fm.ContractFees.Add(fm.UploadSpending).
+ Add(fm.DownloadSpending).Add(fm.StorageSpending)
+ total := totalSpent.Add(fm.Unspent)
+ allowance := rg.Settings.Allowance
+
+ wg, err = r.WalletGet()
+ if err != nil {
+ t.Fatal(«Failed to get wallet:», err)
+ }
+
+ // Check that renter financial metrics add up to allowance
+ if total.Cmp(allowance.Funds) != 0 {
+ t.Fatalf(«Combined Total of reported spending and unspent funds not equal to allowance, n%v != n%v», total, allowance.Funds)
+ }

Break into two lines

> + Add(fm.DownloadSpending).Add(fm.StorageSpending)
+ total := totalSpent.Add(fm.Unspent)
+ allowance := rg.Settings.Allowance
+
+ wg, err = r.WalletGet()
+ if err != nil {
+ t.Fatal(«Failed to get wallet:», err)
+ }
+
+ // Check that renter financial metrics add up to allowance
+ if total.Cmp(allowance.Funds) != 0 {
+ t.Fatalf(«Combined Total of reported spending and unspent funds not equal to allowance, n%v != n%v», total, allowance.Funds)
+ }
+
+ // Check renter financial metrics against contracts contract spending
+ var spending modules.ContractorSpending

contracts contract, remove one

> + }
+ for _, contract := range renewedContracts {
+ // Calculate ContractFees
+ spending.ContractFees = spending.ContractFees.Add(contract.Fees)
+ // Calculate TotalAllocated
+ spending.TotalAllocated = spending.TotalAllocated.Add(contract.TotalCost)
+ // Calculate Spending
+ spending.DownloadSpending = spending.DownloadSpending.Add(contract.DownloadSpending)
+ spending.UploadSpending = spending.UploadSpending.Add(contract.UploadSpending)
+ spending.StorageSpending = spending.StorageSpending.Add(contract.StorageSpending)
+ }
+
+ // Compare contract fees
+ if fm.ContractFees.Cmp(spending.ContractFees) != 0 {
+ t.Fatalf(«Financial Metrics Contract Fees not equal to Renter Contract Fees, n%v != n%v», fm.ContractFees, spending.ContractFees)
+ }

break into two lines

> + spending.ContractFees = spending.ContractFees.Add(contract.Fees)
+ // Calculate TotalAllocated
+ spending.TotalAllocated = spending.TotalAllocated.Add(contract.TotalCost)
+ // Calculate Spending
+ spending.DownloadSpending = spending.DownloadSpending.Add(contract.DownloadSpending)
+ spending.UploadSpending = spending.UploadSpending.Add(contract.UploadSpending)
+ spending.StorageSpending = spending.StorageSpending.Add(contract.StorageSpending)
+ }
+
+ // Compare contract fees
+ if fm.ContractFees.Cmp(spending.ContractFees) != 0 {
+ t.Fatalf(«Financial Metrics Contract Fees not equal to Renter Contract Fees, n%v != n%v», fm.ContractFees, spending.ContractFees)
+ }
+ // Compare Total Allocated
+ if fm.TotalAllocated.Cmp(spending.TotalAllocated) != 0 {
+ t.Fatalf(«Financial Metrics Total Allocated not equal to Renter Total Allocated, n%v != n%v», fm.TotalAllocated, spending.TotalAllocated)

break into two lines

> + spending.StorageSpending = spending.StorageSpending.Add(contract.StorageSpending)
+ }
+
+ // Compare contract fees
+ if fm.ContractFees.Cmp(spending.ContractFees) != 0 {
+ t.Fatalf(«Financial Metrics Contract Fees not equal to Renter Contract Fees, n%v != n%v», fm.ContractFees, spending.ContractFees)
+ }
+ // Compare Total Allocated
+ if fm.TotalAllocated.Cmp(spending.TotalAllocated) != 0 {
+ t.Fatalf(«Financial Metrics Total Allocated not equal to Renter Total Allocated, n%v != n%v», fm.TotalAllocated, spending.TotalAllocated)
+ }
+ // Compare Spending
+ allSpending := spending.ContractFees.Add(spending.UploadSpending).
+ Add(spending.DownloadSpending).Add(spending.StorageSpending)
+ if totalSpent.Cmp(allSpending) != 0 {
+ t.Fatalf(«Financial Metrics Spending not equal to Renter Spending, n%v != n%v», totalSpent, allSpending)

break into two lines

Добавить комментарий