Skip to content

Commit fcbd0e5

Browse files
authored
Merge pull request #278 from opacity/feature/enable-renew
Feature/enable renew
2 parents a9c3408 + 8dfd683 commit fcbd0e5

13 files changed

+324
-80
lines changed

go.mod

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,8 @@ require (
1010
github.com/AndreasBriese/bbloom v0.0.0-20190825152654-46b345b51c96 // indirect
1111
github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751
1212
github.com/alecthomas/units v0.0.0-20190924025748-f65c72e2690d // indirect
13-
github.com/allegro/bigcache v1.2.0 // indirect
1413
github.com/aristanetworks/goarista v0.0.0-20190308231643-e9fb69a13f45 // indirect
15-
github.com/aws/aws-sdk-go v1.18.0
14+
github.com/aws/aws-sdk-go v1.25.48
1615
github.com/bamzi/jobrunner v0.0.0-20161019143021-273175f8b6eb
1716
github.com/btcsuite/btcd v0.0.0-20190213025234-306aecffea32 // indirect
1817
github.com/caarlos0/env v3.5.0+incompatible
@@ -29,7 +28,7 @@ require (
2928
github.com/dgraph-io/badger v1.6.0
3029
github.com/edsrzf/mmap-go v1.0.0 // indirect
3130
github.com/erikstmartin/go-testdb v0.0.0-20160219214506-8d10e4a1bae5 // indirect
32-
github.com/ethereum/go-ethereum v1.8.23
31+
github.com/ethereum/go-ethereum v1.9.14
3332
github.com/fjl/memsize v0.0.0-20180929194037-2a09253e352a // indirect
3433
github.com/gin-contrib/cors v0.0.0-20190301062745-f9e10995c85a
3534
github.com/gin-gonic/gin v1.3.0
@@ -48,9 +47,7 @@ require (
4847
github.com/gorilla/websocket v1.4.1 // indirect
4948
github.com/grpc-ecosystem/go-grpc-middleware v1.1.0 // indirect
5049
github.com/grpc-ecosystem/grpc-gateway v1.11.3 // indirect
51-
github.com/hashicorp/golang-lru v0.5.3 // indirect
5250
github.com/huin/goupnp v1.0.0 // indirect
53-
github.com/jackpal/go-nat-pmp v1.0.1 // indirect
5451
github.com/jinzhu/gorm v1.9.2
5552
github.com/jinzhu/inflection v0.0.0-20180308033659-04140366298a // indirect
5653
github.com/jinzhu/now v1.0.0 // indirect
@@ -90,18 +87,14 @@ require (
9087
github.com/stripe/stripe-go v61.7.1+incompatible
9188
github.com/swaggo/gin-swagger v1.1.0
9289
github.com/swaggo/swag v1.5.0
93-
github.com/syndtr/goleveldb v1.0.0 // indirect
9490
github.com/ugorji/go v1.1.7 // indirect
9591
go.etcd.io/bbolt v1.3.3 // indirect
9692
go.opencensus.io v0.22.1 // indirect
9793
go.uber.org/multierr v1.2.0 // indirect
9894
golang.org/x/build v0.0.0-20190111050920-041ab4dc3f9d // indirect
99-
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550 // indirect
10095
golang.org/x/image v0.0.0-20191009234506-e7c1f5e7dbb8 // indirect
10196
golang.org/x/mobile v0.0.0-20191002175909-6d0d39b2ca82 // indirect
102-
golang.org/x/net v0.0.0-20191011234655-491137f69257 // indirect
10397
golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e // indirect
104-
golang.org/x/sys v0.0.0-20191010194322-b09406accb47 // indirect
10598
golang.org/x/time v0.0.0-20190921001708-c4c64cad1fd0 // indirect
10699
golang.org/x/tools v0.0.0-20191012152004-8de300cfc20a // indirect
107100
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898 // indirect

go.sum

Lines changed: 100 additions & 0 deletions
Large diffs are not rendered by default.

models/renewals.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ type Renewal struct {
2121
ApiVersion int `json:"apiVersion" binding:"omitempty,gte=1" gorm:"default:1"`
2222
PaymentMethod PaymentMethodType `json:"paymentMethod" gorm:"default:0"`
2323
OpqCost float64 `json:"opqCost" binding:"omitempty,gte=0" example:"1.56"`
24-
UsdCost float64 `json:"usdcost" binding:"omitempty,gte=0" example:"39.99"`
24+
//UsdCost float64 `json:"usdcost" binding:"omitempty,gte=0" example:"39.99"`
2525
DurationInMonths int `json:"durationInMonths" gorm:"default:12" binding:"required,gte=1" minimum:"1" example:"12"`
2626
}
2727

@@ -59,7 +59,7 @@ func (renewal *Renewal) BeforeDelete(scope *gorm.Scope) error {
5959
}
6060

6161
/*GetOrCreateRenewal will either get or create an renewal. If the renewal already existed it will update the OpqCost
62-
and UsdCost but will not update the EthAddress and EthPrivateKey*/
62+
but will not update the EthAddress and EthPrivateKey*/
6363
func GetOrCreateRenewal(renewal Renewal) (*Renewal, error) {
6464
renewalsFromDB, err := GetRenewalsFromAccountID(renewal.AccountID)
6565
if err != nil {

models/renewals_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ func returnValidRenewal() (Renewal, Account) {
2828
EthPrivateKey: hex.EncodeToString(utils.Encrypt(utils.Env.EncryptionKey, privateKey, account.AccountID)),
2929
PaymentStatus: InitialPaymentInProgress,
3030
OpqCost: renewalCostInOPQ,
31-
UsdCost: utils.Env.Plans[int(account.StorageLimit)].CostInUSD,
31+
//UsdCost: utils.Env.Plans[int(account.StorageLimit)].CostInUSD,
3232
DurationInMonths: 12,
3333
}, account
3434
}
@@ -133,7 +133,7 @@ func Test_Renewal_GetOrCreateRenewal(t *testing.T) {
133133
assert.Equal(t, uPtr.AccountID, renewal.AccountID)
134134
assert.Equal(t, uPtr.EthAddress, renewal.EthAddress)
135135
assert.Equal(t, uPtr.OpqCost, renewal.OpqCost)
136-
assert.Equal(t, uPtr.UsdCost, renewal.UsdCost)
136+
//assert.Equal(t, uPtr.UsdCost, renewal.UsdCost)
137137

138138
// simulate generating a new update with the same AccountID
139139
// although another renewal already exists--price should not change

models/upgrades.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ type Upgrade struct {
2323
ApiVersion int `json:"apiVersion" binding:"omitempty,gte=1" gorm:"default:1"`
2424
PaymentMethod PaymentMethodType `json:"paymentMethod" gorm:"default:0"`
2525
OpqCost float64 `json:"opqCost" binding:"omitempty,gte=0" example:"1.56"`
26-
UsdCost float64 `json:"usdcost" binding:"omitempty,gte=0" example:"39.99"`
26+
//UsdCost float64 `json:"usdcost" binding:"omitempty,gte=0" example:"39.99"`
2727
DurationInMonths int `json:"durationInMonths" gorm:"default:12" binding:"required,gte=1" minimum:"1" example:"12"`
2828
}
2929

@@ -61,7 +61,7 @@ func (upgrade *Upgrade) BeforeDelete(scope *gorm.Scope) error {
6161
}
6262

6363
/*GetOrCreateUpgrade will either get or create an upgrade. If the upgrade already existed it will update the OpqCost
64-
and UsdCost but will not update the EthAddress and EthPrivateKey*/
64+
but will not update the EthAddress and EthPrivateKey*/
6565
func GetOrCreateUpgrade(upgrade Upgrade) (*Upgrade, error) {
6666
var upgradeFromDB Upgrade
6767

@@ -73,8 +73,8 @@ func GetOrCreateUpgrade(upgrade Upgrade) (*Upgrade, error) {
7373
targetTime := time.Now().Add(-60 * time.Minute)
7474
if targetTime.After(upgradeFromDB.UpdatedAt) {
7575
upgradeFromDB.OpqCost = upgrade.OpqCost
76-
upgradeFromDB.UsdCost = upgrade.UsdCost
77-
err = DB.Model(&upgradeFromDB).Updates(Upgrade{OpqCost: upgrade.OpqCost, UsdCost: upgrade.UsdCost}).Error
76+
//upgradeFromDB.UsdCost = upgrade.UsdCost
77+
err = DB.Model(&upgradeFromDB).Updates(Upgrade{OpqCost: upgrade.OpqCost}).Error
7878
}
7979
}
8080

models/upgrades_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ func returnValidUpgrade() (Upgrade, Account) {
2323

2424
upgradeCostInOPQ, _ := account.UpgradeCostInOPQ(utils.Env.Plans[1024].StorageInGB,
2525
12)
26-
upgradeCostInUSD, _ := account.UpgradeCostInUSD(utils.Env.Plans[1024].StorageInGB,
27-
12)
26+
//upgradeCostInUSD, _ := account.UpgradeCostInUSD(utils.Env.Plans[1024].StorageInGB,
27+
// 12)
2828

2929
return Upgrade{
3030
AccountID: account.AccountID,
@@ -34,7 +34,7 @@ func returnValidUpgrade() (Upgrade, Account) {
3434
EthPrivateKey: hex.EncodeToString(utils.Encrypt(utils.Env.EncryptionKey, privateKey, account.AccountID)),
3535
PaymentStatus: InitialPaymentInProgress,
3636
OpqCost: upgradeCostInOPQ,
37-
UsdCost: upgradeCostInUSD,
37+
//UsdCost: upgradeCostInUSD,
3838
DurationInMonths: 12,
3939
}, account
4040
}
@@ -149,7 +149,7 @@ func Test_Upgrade_GetOrCreateUpgrade(t *testing.T) {
149149
assert.Equal(t, uPtr.EthAddress, upgrade.EthAddress)
150150
assert.Equal(t, uPtr.NewStorageLimit, upgrade.NewStorageLimit)
151151
assert.Equal(t, uPtr.OpqCost, upgrade.OpqCost)
152-
assert.Equal(t, uPtr.UsdCost, upgrade.UsdCost)
152+
//assert.Equal(t, uPtr.UsdCost, upgrade.UsdCost)
153153

154154
// simulate generating a new update with the same AccountID and NewStorageLimit
155155
// although another upgrade already exists--price should not change due to 1 hour price

routes/renew_account.go

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"github.com/opacity/storage-node/models"
1111
"github.com/opacity/storage-node/services"
1212
"github.com/opacity/storage-node/utils"
13+
"time"
1314
)
1415

1516
type getRenewalAccountInvoiceObject struct {
@@ -107,7 +108,7 @@ func getAccountRenewalInvoice(c *gin.Context) error {
107108
return InternalErrorResponse(c, err)
108109
}
109110

110-
renewalCostInUSD := utils.Env.Plans[int(account.StorageLimit)].CostInUSD
111+
//renewalCostInUSD := utils.Env.Plans[int(account.StorageLimit)].CostInUSD
111112

112113
ethAddr, privKey, err := services.EthWrapper.GenerateWallet()
113114
if err != nil {
@@ -131,7 +132,6 @@ func getAccountRenewalInvoice(c *gin.Context) error {
131132
EthPrivateKey: hex.EncodeToString(encryptedKeyInBytes),
132133
PaymentStatus: models.InitialPaymentInProgress,
133134
OpqCost: renewalCostInOPQ,
134-
UsdCost: renewalCostInUSD,
135135
DurationInMonths: 12,
136136
}
137137

@@ -163,10 +163,9 @@ func checkRenewalStatus(c *gin.Context) error {
163163
return err
164164
}
165165

166-
// TODO: re-enable this for the actual release
167-
//if err := verifyRenewEligible(account, c); err != nil {
168-
// return err
169-
//}
166+
if err := verifyRenewEligible(account, c); err != nil {
167+
return err
168+
}
170169

171170
renewals, err := models.GetRenewalsFromAccountID(account.AccountID)
172171
if err != nil {
@@ -210,8 +209,11 @@ func renewalAccountAndUpdateExpireDates(account models.Account, request checkRen
210209
}
211210
filesErr := models.UpdateExpiredAt(request.checkRenewalStatusObject.FileHandles,
212211
request.verification.PublicKey, account.ExpirationDate())
212+
213+
// Setting ttls on metadata to 2 months post account expiration date so the metadatas won't
214+
// be deleted too soon
213215
metadatasErr := updateMetadataExpiration(request.checkRenewalStatusObject.MetadataKeys,
214-
request.verification.PublicKey, account.ExpirationDate(), c)
216+
request.verification.PublicKey, account.ExpirationDate().Add(24 * time.Hour * 60), c)
215217

216218
return utils.CollectErrors([]error{filesErr, metadatasErr})
217219
}

routes/renew_account_test.go

Lines changed: 25 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -47,32 +47,31 @@ func Test_GetAccountRenewInvoiceHandler_Returns_Invoice(t *testing.T) {
4747
assert.Contains(t, w.Body.String(), `"opqInvoice":{"cost":16,`)
4848
}
4949

50-
// TODO: re-enable this for the actual release
51-
//func Test_GetAccountRenewInvoiceHandler_ReturnsErrorIfExpirationDateTooFarInFuture(t *testing.T) {
52-
// models.DeleteAccountsForTest(t)
53-
// models.DeleteRenewalsForTest(t)
54-
//
55-
// getInvoiceObj := getRenewalAccountInvoiceObject{}
56-
//
57-
// v, b, _ := returnValidVerificationAndRequestBodyWithRandomPrivateKey(t, getInvoiceObj)
58-
//
59-
// getInvoiceReq := getRenewalAccountInvoiceReq{
60-
// verification: v,
61-
// requestBody: b,
62-
// }
63-
//
64-
// accountID, _ := utils.HashString(v.PublicKey)
65-
// account := CreatePaidAccountForTest(t, accountID)
66-
//
67-
// account.StorageLimit = models.StorageLimitType(1024)
68-
// account.MonthsInSubscription = 13
69-
// models.DB.Save(&account)
70-
//
71-
// w := httpPostRequestHelperForTest(t, AccountRenewInvoicePath, getInvoiceReq)
72-
// // Check to see if the response was what you expected
73-
// assert.Equal(t, http.StatusForbidden, w.Code)
74-
// assert.Contains(t, w.Body.String(), `account has too much time left to renew`)
75-
//}
50+
func Test_GetAccountRenewInvoiceHandler_ReturnsErrorIfExpirationDateTooFarInFuture(t *testing.T) {
51+
models.DeleteAccountsForTest(t)
52+
models.DeleteRenewalsForTest(t)
53+
54+
getInvoiceObj := getRenewalAccountInvoiceObject{}
55+
56+
v, b, _ := returnValidVerificationAndRequestBodyWithRandomPrivateKey(t, getInvoiceObj)
57+
58+
getInvoiceReq := getRenewalAccountInvoiceReq{
59+
verification: v,
60+
requestBody: b,
61+
}
62+
63+
accountID, _ := utils.HashString(v.PublicKey)
64+
account := CreatePaidAccountForTest(t, accountID)
65+
66+
account.StorageLimit = models.StorageLimitType(1024)
67+
account.MonthsInSubscription = 13
68+
models.DB.Save(&account)
69+
70+
w := httpPostRequestHelperForTest(t, AccountRenewInvoicePath, getInvoiceReq)
71+
// Check to see if the response was what you expected
72+
assert.Equal(t, http.StatusForbidden, w.Code)
73+
assert.Contains(t, w.Body.String(), `account has too much time left to renew`)
74+
}
7675

7776
func Test_CheckRenewalStatusHandler_Returns_Status_OPQ_Renew_Success(t *testing.T) {
7877
models.DeleteAccountsForTest(t)
@@ -211,7 +210,6 @@ func returnRenewalForTest(t *testing.T, account models.Account) models.Renewal {
211210
DurationInMonths: models.DefaultMonthsPerSubscription,
212211
PaymentStatus: models.InitialPaymentInProgress,
213212
OpqCost: renewalCostInOPQ,
214-
UsdCost: utils.Env.Plans[int(account.StorageLimit)].CostInUSD,
215213
EthAddress: ethAddress.String(),
216214
EthPrivateKey: hex.EncodeToString(utils.Encrypt(utils.Env.EncryptionKey, privateKey, account.AccountID)),
217215
}

routes/response_utils.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ func verifyUpgradeEligible(account models.Account, newStorageLimit int, c *gin.C
200200
}
201201

202202
func verifyRenewEligible(account models.Account, c *gin.Context) error {
203-
renewalCutoffTimestamp := time.Now().Add(time.Hour * 24 * 365)
203+
renewalCutoffTimestamp := time.Now().Add(time.Hour * 24 * 180)
204204
if account.ExpirationDate().After(renewalCutoffTimestamp) {
205205
return ForbiddenResponse(c, errors.New("account has too much time left to renew"))
206206
}

routes/stripe_payments.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,8 +94,8 @@ func createStripePayment(c *gin.Context) error {
9494
if err := verifyValidStorageLimit(request.createStripePaymentObject.StorageLimit, c); err != nil {
9595
return err
9696
}
97-
costInDollars, _ = account.UpgradeCostInUSD(request.createStripePaymentObject.StorageLimit,
98-
request.createStripePaymentObject.DurationInMonths)
97+
//costInDollars, _ = account.UpgradeCostInUSD(request.createStripePaymentObject.StorageLimit,
98+
// request.createStripePaymentObject.DurationInMonths)
9999
} else {
100100
costInDollars = utils.Env.Plans[int(account.StorageLimit)].CostInUSD
101101
}

0 commit comments

Comments
 (0)