diff --git a/actions/app.go b/actions/app.go index 88380762..5480ad46 100644 --- a/actions/app.go +++ b/actions/app.go @@ -70,42 +70,8 @@ func App() *buffalo.App { app.GET("/metrics", buffalo.WrapHandler(prometheus.Handler())) - apiV2 := app.Group("/api/v2") - apiV3 := app.Group("/api/v3") - - // UploadSessions - uploadSessionResourceV2 := actions_v2.UploadSessionResourceV2{} - apiV2.POST("upload-sessions", uploadSessionResourceV2.Create) - apiV2.PUT("upload-sessions/{id}", uploadSessionResourceV2.Update) - apiV2.POST("upload-sessions/beta", uploadSessionResourceV2.CreateBeta) - apiV2.GET("upload-sessions/{id}", uploadSessionResourceV2.GetPaymentStatus) - - uploadSessionResourceV3 := actions_v3.UploadSessionResourceV3{} - apiV3.POST("upload-sessions", uploadSessionResourceV2.Create) - apiV3.PUT("upload-sessions/{id}", uploadSessionResourceV3.Update) - apiV3.POST("upload-sessions/beta", uploadSessionResourceV2.CreateBeta) - apiV3.GET("upload-sessions/{id}", uploadSessionResourceV2.GetPaymentStatus) - - // Webnodes - webnodeResource := actions_v2.WebnodeResource{} - apiV2.POST("supply/webnodes", webnodeResource.Create) - - // Transactions - transactionBrokernodeResource := actions_v2.TransactionBrokernodeResource{} - apiV2.POST("demand/transactions/brokernodes", transactionBrokernodeResource.Create) - apiV2.PUT("demand/transactions/brokernodes/{id}", transactionBrokernodeResource.Update) - - transactionGenesisHashResource := actions_v2.TransactionGenesisHashResource{} - apiV2.POST("demand/transactions/genesis_hashes", transactionGenesisHashResource.Create) - apiV2.PUT("demand/transactions/genesis_hashes/{id}", transactionGenesisHashResource.Update) - - // Treasures - treasures := actions_v2.TreasuresResource{} - apiV2.POST("treasures", treasures.VerifyAndClaim) - - // Status - statusResource := StatusResource{} - apiV2.GET("status", statusResource.CheckStatus) + registerV2Api(app) + registerV3Api(app) } oyster_utils.StartProfile() @@ -113,3 +79,45 @@ func App() *buffalo.App { return app } + +func registerV2Api(app *buffalo.App) { + apiV2 := app.Group("/api/v2") + + // UploadSessions + uploadSessionResourceV2 := actions_v2.UploadSessionResourceV2{} + apiV2.POST("upload-sessions", uploadSessionResourceV2.Create) + apiV2.PUT("upload-sessions/{id}", uploadSessionResourceV2.Update) + apiV2.POST("upload-sessions/beta", uploadSessionResourceV2.CreateBeta) + apiV2.GET("upload-sessions/{id}", uploadSessionResourceV2.GetPaymentStatus) + + // Webnodes + webnodeResource := actions_v2.WebnodeResource{} + apiV2.POST("supply/webnodes", webnodeResource.Create) + + // Transactions + transactionBrokernodeResource := actions_v2.TransactionBrokernodeResource{} + apiV2.POST("demand/transactions/brokernodes", transactionBrokernodeResource.Create) + apiV2.PUT("demand/transactions/brokernodes/{id}", transactionBrokernodeResource.Update) + + transactionGenesisHashResource := actions_v2.TransactionGenesisHashResource{} + apiV2.POST("demand/transactions/genesis_hashes", transactionGenesisHashResource.Create) + apiV2.PUT("demand/transactions/genesis_hashes/{id}", transactionGenesisHashResource.Update) + + // Treasures + treasures := actions_v2.TreasuresResource{} + apiV2.POST("treasures", treasures.VerifyAndClaim) + + // Status + statusResource := StatusResource{} + apiV2.GET("status", statusResource.CheckStatus) +} + +func registerV3Api(app *buffalo.App) { + apiV3 := app.Group("/api/v3") + + uploadSessionResourceV3 := actions_v3.UploadSessionResourceV3{} + apiV3.POST("upload-sessions", uploadSessionResourceV2.Create) + apiV3.PUT("upload-sessions/{id}", uploadSessionResourceV3.Update) + apiV3.POST("upload-sessions/beta", uploadSessionResourceV2.CreateBeta) + apiV3.GET("upload-sessions/{id}", uploadSessionResourceV2.GetPaymentStatus) +} diff --git a/actions/v3/actions_test.go b/actions/v3/actions_test.go new file mode 100644 index 00000000..cf031d3e --- /dev/null +++ b/actions/v3/actions_test.go @@ -0,0 +1,20 @@ +package actions_v3 + +import ( + "github.com/gobuffalo/suite" + "github.com/oysterprotocol/brokernode/services" + "github.com/oysterprotocol/brokernode/utils" +) + +type ActionSuite struct { + *suite.Action +} + +func (suite *ActionSuite) SetupTest() { + suite.Action.SetupTest() + + suite.Nil(oyster_utils.InitKvStore()) + + EthWrapper = services.EthWrapper + IotaWrapper = services.IotaWrapper +} diff --git a/actions/v3/upload_sessions.go b/actions/v3/upload_sessions.go index 84a6b29d..d4abe2aa 100644 --- a/actions/v3/upload_sessions.go +++ b/actions/v3/upload_sessions.go @@ -20,10 +20,6 @@ type UploadSessionUpdateReqV3 struct { Chunks []models.ChunkReq `json:"chunks"` } -func init() { - -} - // Update uploads a chunk associated with an upload session. func (usr *UploadSessionResourceV3) Update(c buffalo.Context) error { start := PrometheusWrapper.TimeNow() diff --git a/utils/kv_store.go b/utils/kv_store.go index 32c44f33..c8309329 100644 --- a/utils/kv_store.go +++ b/utils/kv_store.go @@ -172,12 +172,11 @@ func buildBadgerName(names []string, separator string) string { /*InitUniqueKvStore creates a new K:V store associated with a particular upload*/ func InitUniqueKvStore(dbID []string) error { dbName := GetBadgerDBName(dbID) - dirPath := GetBadgerDirName(dbID) - val, ok := dbMap.Get(dbName) - if ok == true && val != nil { + if _, ok := dbMap.Get(dbName); ok { return nil } + dirPath := GetBadgerDirName(dbID) opts := badger.DefaultOptions if os.Getenv("GO_ENV") == "test" { @@ -240,7 +239,7 @@ func InitKvStore() (err error) { /*CloseUniqueKvStore closes the K:V store associated with a particular upload.*/ func CloseUniqueKvStore(dbName string) error { value, ok := dbMap.Get(dbName) - if value == nil || ok != true { + if !ok { return nil } dbData := value.(DBData) @@ -269,7 +268,7 @@ func CloseKvStore() error { /*RemoveAllUniqueKvStoreData removes all the data associated with a particular K:V store.*/ func RemoveAllUniqueKvStoreData(dbName string) error { value, ok := dbMap.Get(dbName) - if value == nil || ok != true { + if !ok { return errors.New("did not find database with name: " + dbName + " in RemoveAllUniqueKvStoreData") } @@ -299,9 +298,8 @@ func RemoveAllKvStoreDataFromAllKvStores() []error { allDBs := dbMap.Keys() for _, dbName := range allDBs { value, ok := dbMap.Get(dbName) - if value == nil || ok != true { - errArray = append(errArray, errors.New("did not find database with name: "+ - dbName+" in RemoveAllKvStoreDataFromAllKvStores")) + if !ok { + continue } dbData := value.(DBData) directoryPath := dbData.DirectoryPath @@ -342,7 +340,7 @@ func RemoveAllKvStoreData() error { /*GetUniqueBadgerDb returns a database associated with an upload. If not initialized this will return nil. */ func GetUniqueBadgerDb(dbName string) *badger.DB { value, ok := dbMap.Get(dbName) - if value == nil || ok != true { + if !ok { return nil } dbData := value.(DBData) @@ -369,20 +367,16 @@ func GetOrInitUniqueBadgerDB(dbID []string) *badger.DB { if db != nil { return db } - err = InitUniqueKvStore(dbID) - LogIfError(err, nil) - if err == nil { + if err := InitUniqueKvStore(dbID); err == nil { break } if timesRetried >= timesToRetry { - LogIfError(errors.New("retried InitUniqueKvStore too many times"), nil) + LogIfError(errors.New("retried InitUniqueKvStore too many times"), map[string]interface{}{"RetryCount": timesToRetry, "BadgerKey": dbID}) break } timesRetried++ } } - - LogIfError(err, nil) return GetUniqueBadgerDb(dbName) } @@ -632,7 +626,7 @@ func BatchSetToUniqueDB(dbID []string, kvs *KVPairs, ttl time.Duration) error { if e == badger.ErrTxnTooBig { e = nil - if commitErr := txn.Commit(nil); commitErr != nil { + if commitErr := txn.Commit(); commitErr != nil { e = commitErr } else { txn = db.NewTransaction(true) @@ -648,7 +642,7 @@ func BatchSetToUniqueDB(dbID []string, kvs *KVPairs, ttl time.Duration) error { defer txn.Discard() if err == nil { - err = txn.Commit(nil) + err = txn.Commit() } LogIfError(err, map[string]interface{}{"batchSize": len(*kvs)}) return err @@ -676,7 +670,7 @@ func BatchSet(kvs *KVPairs, ttl time.Duration) error { if e == badger.ErrTxnTooBig { e = nil - if commitErr := txn.Commit(nil); commitErr != nil { + if commitErr := txn.Commit(); commitErr != nil { e = commitErr } else { txn = badgerDB.NewTransaction(true) @@ -692,7 +686,7 @@ func BatchSet(kvs *KVPairs, ttl time.Duration) error { defer txn.Discard() if err == nil { - err = txn.Commit(nil) + err = txn.Commit() } LogIfError(err, map[string]interface{}{"batchSize": len(*kvs)}) return err @@ -721,7 +715,7 @@ func BatchDeleteFromUniqueDB(dbID []string, ks *KVKeys) error { if e == badger.ErrTxnTooBig { e = nil - if commitErr := txn.Commit(nil); commitErr != nil { + if commitErr := txn.Commit(); commitErr != nil { e = commitErr } else { txn = db.NewTransaction(true) @@ -737,7 +731,7 @@ func BatchDeleteFromUniqueDB(dbID []string, ks *KVKeys) error { defer txn.Discard() if err == nil { - err = txn.Commit(nil) + err = txn.Commit() } LogIfError(err, map[string]interface{}{"batchSize": len(*ks)}) @@ -760,7 +754,7 @@ func BatchDelete(ks *KVKeys) error { if e == badger.ErrTxnTooBig { e = nil - if commitErr := txn.Commit(nil); commitErr != nil { + if commitErr := txn.Commit(); commitErr != nil { e = commitErr } else { txn = badgerDB.NewTransaction(true) @@ -776,7 +770,7 @@ func BatchDelete(ks *KVKeys) error { defer txn.Discard() if err == nil { - err = txn.Commit(nil) + err = txn.Commit() } LogIfError(err, map[string]interface{}{"batchSize": len(*ks)})