Skip to content

Commit 3690c77

Browse files
authored
Modified readpool to single balance configuration (#723)
1 parent ee6faea commit 3690c77

File tree

3 files changed

+15
-34
lines changed

3 files changed

+15
-34
lines changed

code/go/0chain.net/blobbercore/allocation/entity.go

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -197,9 +197,8 @@ func (Terms) TableName() string {
197197
// one for the allocations that the client (client_id) owns
198198
// and the other for the allocations that the client (client_id) doesn't own
199199
type ReadPool struct {
200-
ClientID string `gorm:"column:client_id;size:64;primaryKey" json:"client_id"`
201-
OwnerBalance int64 `gorm:"column:owner_balance;not null" json:"owner_balance"`
202-
VisitorBalance int64 `gorm:"column:visitor_balance;not null" json:"visitor_balance"`
200+
ClientID string `gorm:"column:client_id;size:64;primaryKey" json:"client_id"`
201+
Balance int64 `gorm:"column:balance;not null" json:"balance"`
203202
}
204203

205204
func (ReadPool) TableName() string {
@@ -223,16 +222,13 @@ func GetReadPool(db *gorm.DB, clientID string) (*ReadPool, error) {
223222
return &rp, db.Model(&ReadPool{}).Where("client_id = ?", clientID).Scan(&rp).Error
224223
}
225224

226-
func GetReadPoolsBalance(db *gorm.DB, clientID string, isOwner bool) (int64, error) {
225+
func GetReadPoolsBalance(db *gorm.DB, clientID string) (int64, error) {
227226
rp, err := GetReadPool(db, clientID)
228227
if err != nil {
229228
return 0, err
230229
}
231230

232-
if isOwner {
233-
return rp.OwnerBalance, nil
234-
}
235-
return rp.VisitorBalance, nil
231+
return rp.Balance, nil
236232
}
237233

238234
func SetReadPool(db *gorm.DB, rp *ReadPool) error {
@@ -242,7 +238,7 @@ func SetReadPool(db *gorm.DB, rp *ReadPool) error {
242238
return err
243239
}
244240

245-
if erp.OwnerBalance == rp.OwnerBalance && erp.VisitorBalance == rp.VisitorBalance {
241+
if erp.Balance == rp.Balance {
246242
return nil
247243
}
248244
// update existing
@@ -251,8 +247,7 @@ func SetReadPool(db *gorm.DB, rp *ReadPool) error {
251247

252248
func UpdateReadPool(db *gorm.DB, rp *ReadPool) error {
253249
return db.Model(&ReadPool{}).Where("client_id = ?", rp.ClientID).Updates(map[string]interface{}{
254-
"owner_balance": rp.OwnerBalance,
255-
"visitor_balance": rp.VisitorBalance,
250+
"balance": rp.Balance,
256251
}).Error
257252
}
258253

code/go/0chain.net/blobbercore/handler/object_operation_handler.go

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -56,15 +56,14 @@ func readPreRedeem(ctx context.Context, alloc *allocation.Allocation, numBlocks,
5656
return // skip if read price is zero
5757
}
5858

59-
isOwner := alloc.OwnerID == payerID
60-
readPoolsBalance, err := allocation.GetReadPoolsBalance(db, payerID, isOwner)
59+
readPoolBalance, err := allocation.GetReadPoolsBalance(db, payerID)
6160
if err != nil {
6261
return common.NewError("read_pre_redeem", "database error while reading read pools balance")
6362
}
6463

6564
requiredBalance := alloc.GetRequiredReadBalance(blobberID, numBlocks+pendNumBlocks)
6665

67-
if float64(readPoolsBalance) < requiredBalance {
66+
if float64(readPoolBalance) < requiredBalance {
6867
rp, err := allocation.RequestReadPoolStat(payerID)
6968
if err != nil {
7069
return common.NewErrorf("read_pre_redeem", "can't request read pools from sharders: %v", err)
@@ -76,13 +75,9 @@ func readPreRedeem(ctx context.Context, alloc *allocation.Allocation, numBlocks,
7675
return common.NewErrorf("read_pre_redeem", "can't save requested read pools: %v", err)
7776
}
7877

79-
if isOwner {
80-
readPoolsBalance = rp.OwnerBalance
81-
} else {
82-
readPoolsBalance = rp.VisitorBalance
83-
}
78+
readPoolBalance = rp.Balance
8479

85-
if float64(readPoolsBalance) < requiredBalance {
80+
if float64(readPoolBalance) < requiredBalance {
8681
return common.NewError("read_pre_redeem",
8782
"not enough tokens in client's read pools associated with the allocation->blobber")
8883
}

code/go/0chain.net/blobbercore/handler/object_operation_handler_test.go

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -162,15 +162,13 @@ func TestDownloadFile(t *testing.T) {
162162
require.EqualValues(t, scAddress, transaction.STORAGE_CONTRACT_ADDRESS)
163163
switch relativePath {
164164
case "/getReadPoolStat":
165-
//require.False(t, p.isFundedBlobber) //todo: why is this relevant?
166165
require.EqualValues(t, p.payerId.ClientID, params["client_id"])
167166
var funds int64
168167
if p.isFunded0Chain {
169168
funds = mockBigBalance
170169
}
171170
rp := allocation.ReadPool{
172-
OwnerBalance: funds,
173-
VisitorBalance: funds,
171+
Balance: funds,
174172
}
175173

176174
mbytes, err := json.Marshal(&rp)
@@ -264,17 +262,10 @@ func TestDownloadFile(t *testing.T) {
264262
funds = mockBigBalance
265263
}
266264

267-
if p.isOwner {
268-
mocket.Catcher.NewMock().WithCallback(func(par1 string, args []driver.NamedValue) {
269-
}).OneTime().WithQuery(`SELECT * FROM "read_pools" WHERE`).WithReply(
270-
[]map[string]interface{}{{"client_id": p.payerId.ClientID, "owner_balance": funds}},
271-
)
272-
} else {
273-
mocket.Catcher.NewMock().WithCallback(func(par1 string, args []driver.NamedValue) {
274-
}).OneTime().WithQuery(`SELECT * FROM "read_pools" WHERE`).WithReply(
275-
[]map[string]interface{}{{"client_id": p.payerId.ClientID, "visitor_balance": funds}},
276-
)
277-
}
265+
mocket.Catcher.NewMock().WithCallback(func(par1 string, args []driver.NamedValue) {
266+
}).OneTime().WithQuery(`SELECT * FROM "read_pools" WHERE`).WithReply(
267+
[]map[string]interface{}{{"client_id": p.payerId.ClientID, "balance": funds}},
268+
)
278269

279270
if p.useAuthTicket {
280271
mocket.Catcher.NewMock().OneTime().WithQuery(

0 commit comments

Comments
 (0)