Skip to content

Commit aebec77

Browse files
Fix replace blobber (#1251)
1 parent 65f699c commit aebec77

File tree

1 file changed

+19
-6
lines changed
  • code/go/0chain.net/blobbercore/allocation

1 file changed

+19
-6
lines changed

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

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package allocation
33
import (
44
"context"
55
"encoding/json"
6+
"github.com/0chain/blobber/code/go/0chain.net/core/node"
67
"time"
78

89
"github.com/0chain/blobber/code/go/0chain.net/blobbercore/datastore"
@@ -101,7 +102,7 @@ func updateWork(ctx context.Context) {
101102
offset += int64(len(allocs))
102103

103104
for _, a := range allocs {
104-
updateAllocation(ctx, a)
105+
updateAllocation(ctx, a, node.Self.ID)
105106
if waitOrQuit(ctx, REQUEST_TIMEOUT) {
106107
return
107108
}
@@ -120,7 +121,7 @@ func shouldFinalize(sa *transaction.StorageAllocation) bool {
120121
return sa.Expiration < now && !sa.Finalized
121122
}
122123

123-
func updateAllocation(ctx context.Context, a *Allocation) {
124+
func updateAllocation(ctx context.Context, a *Allocation, selfBlobberID string) {
124125
if a.Finalized {
125126
cleanupAllocation(ctx, a)
126127
return
@@ -132,6 +133,19 @@ func updateAllocation(ctx context.Context, a *Allocation) {
132133
return
133134
}
134135

136+
removedBlobber := true
137+
for _, d := range sa.BlobberDetails {
138+
if d.BlobberID == selfBlobberID {
139+
removedBlobber = false
140+
break
141+
}
142+
}
143+
144+
if removedBlobber {
145+
logging.Logger.Info("blobber removed from allocation", zap.String("blobber", selfBlobberID), zap.String("allocation", a.ID))
146+
cleanupAllocation(ctx, a)
147+
}
148+
135149
// if new Tx, then we have to update the allocation
136150
if sa.Tx != a.Tx || sa.OwnerID != a.OwnerID || sa.Finalized != a.Finalized {
137151
if a, err = updateAllocationInDB(ctx, a, sa); err != nil {
@@ -142,7 +156,7 @@ func updateAllocation(ctx context.Context, a *Allocation) {
142156

143157
// send finalize allocation transaction
144158
if shouldFinalize(sa) {
145-
sendFinalizeAllocation(a)
159+
sendFinalizeAllocation(a.ID)
146160
return
147161
}
148162

@@ -217,17 +231,16 @@ type finalizeRequest struct {
217231
AllocationID string `json:"allocation_id"`
218232
}
219233

220-
func sendFinalizeAllocation(a *Allocation) {
234+
func sendFinalizeAllocation(allocationID string) {
221235
var tx, err = transaction.NewTransactionEntity()
222236
if err != nil {
223237
logging.Logger.Error("creating new transaction entity", zap.Error(err))
224238
return
225239
}
226240

227241
var request finalizeRequest
228-
request.AllocationID = a.ID
242+
request.AllocationID = allocationID
229243

230-
// TODO should this be verified?
231244
err = tx.ExecuteSmartContract(
232245
transaction.STORAGE_CONTRACT_ADDRESS,
233246
transaction.FINALIZE_ALLOCATION,

0 commit comments

Comments
 (0)