Skip to content

Commit 697233e

Browse files
Fix/get open challenges (#1181)
* Fix * Fix * Fix * Fix * Fix * Fix * Fix * Improved logging * Fix challenge table
1 parent 32bc5de commit 697233e

File tree

5 files changed

+29
-21
lines changed

5 files changed

+29
-21
lines changed

code/go/0chain.net/blobbercore/challenge/challenge.go

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -24,25 +24,23 @@ type BCChallengeResponse struct {
2424
Challenges []*ChallengeEntity `json:"challenges"`
2525
}
2626

27-
var lastChallengeTimestamp int
27+
var lastChallengeRound int64
2828

2929
func syncOpenChallenges(ctx context.Context) {
30-
const incrOffset = 20
3130
defer func() {
3231
if r := recover(); r != nil {
3332
logging.Logger.Error("[recover]challenge", zap.Any("err", r))
3433
}
3534
}()
3635

37-
offset := 0
3836
params := make(map[string]string)
3937
params["blobber"] = node.Self.ID
40-
params["offset"] = strconv.Itoa(offset)
41-
params["limit"] = "20"
42-
if lastChallengeTimestamp > 0 {
43-
params["from"] = strconv.Itoa(lastChallengeTimestamp)
38+
39+
params["limit"] = "50"
40+
if lastChallengeRound > 0 {
41+
params["from"] = strconv.FormatInt(lastChallengeRound, 10)
4442
}
45-
logging.Logger.Info("[challenge]sync:pull", zap.Any("params", params))
43+
4644
start := time.Now()
4745

4846
var downloadElapsed, jsonElapsed time.Duration
@@ -54,6 +52,9 @@ func syncOpenChallenges(ctx context.Context) {
5452
return
5553
default:
5654
}
55+
56+
logging.Logger.Info("[challenge]sync:pull", zap.Any("params", params))
57+
5758
var challenges BCChallengeResponse
5859
var challengeIDs []string
5960
challenges.Challenges = make([]*ChallengeEntity, 0)
@@ -75,13 +76,13 @@ func syncOpenChallenges(ctx context.Context) {
7576
break
7677
}
7778
sort.Slice(challenges.Challenges, func(i, j int) bool {
78-
return challenges.Challenges[i].CreatedAt < challenges.Challenges[j].CreatedAt
79+
return challenges.Challenges[i].RoundCreatedAt < challenges.Challenges[j].RoundCreatedAt
7980
})
8081
count += len(challenges.Challenges)
8182
for _, c := range challenges.Challenges {
8283
challengeIDs = append(challengeIDs, c.ChallengeID)
83-
if c.CreatedAt > common.Timestamp(lastChallengeTimestamp) {
84-
lastChallengeTimestamp = int(c.CreatedAt)
84+
if c.RoundCreatedAt >= lastChallengeRound {
85+
lastChallengeRound = c.RoundCreatedAt
8586
}
8687
toProcessChallenge <- c
8788
}
@@ -93,8 +94,6 @@ func syncOpenChallenges(ctx context.Context) {
9394
if len(challenges.Challenges) == 0 {
9495
break
9596
}
96-
offset += incrOffset
97-
params["offset"] = strconv.Itoa(offset)
9897
}
9998

10099
dbTimeStart := time.Now()
@@ -110,6 +109,11 @@ func syncOpenChallenges(ctx context.Context) {
110109

111110
func validateOnValidators(c *ChallengeEntity) {
112111

112+
logging.Logger.Info("[challenge]validate: ",
113+
zap.Any("challenge", c),
114+
zap.String("challenge_id", c.ChallengeID),
115+
)
116+
113117
ctx := datastore.GetStore().CreateTransaction(context.TODO())
114118
defer ctx.Done()
115119

@@ -119,7 +123,7 @@ func validateOnValidators(c *ChallengeEntity) {
119123
logging.Logger.Error("[challengetiming]add: ",
120124
zap.String("challenge_id", c.ChallengeID),
121125
zap.Error(err))
122-
deleteChallenge(int64(c.CreatedAt))
126+
deleteChallenge(c.RoundCreatedAt)
123127
tx.Rollback()
124128
}
125129

@@ -150,7 +154,7 @@ func validateOnValidators(c *ChallengeEntity) {
150154
zap.Time("created", createdTime),
151155
zap.Error(err))
152156
//TODO: Should we delete the challenge from map or send it back to the todo channel?
153-
deleteChallenge(int64(c.CreatedAt))
157+
deleteChallenge(c.RoundCreatedAt)
154158
tx.Rollback()
155159
return
156160
}

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,8 +97,9 @@ type ChallengeEntity struct {
9797
Timestamp common.Timestamp `gorm:"column:timestamp;not null;default:0" json:"timestamp"`
9898

9999
// This time is taken from Blockchain challenge object.
100-
CreatedAt common.Timestamp `gorm:"created_at" json:"created"`
101-
UpdatedAt time.Time `gorm:"updated_at;type:timestamp without time zone;not null;default:current_timestamp" json:"-"`
100+
RoundCreatedAt int64 `gorm:"round_created_at" json:"round_created_at"`
101+
CreatedAt common.Timestamp `gorm:"created_at" json:"created"`
102+
UpdatedAt time.Time `gorm:"updated_at;type:timestamp without time zone;not null;default:current_timestamp" json:"-"`
102103
}
103104

104105
func (ChallengeEntity) TableName() string {

code/go/0chain.net/blobbercore/challenge/protocol.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ type ChallengeResponse struct {
4848
func (cr *ChallengeEntity) CancelChallenge(ctx context.Context, errReason error) {
4949
cancellation := time.Now()
5050
db := datastore.GetStore().GetTransaction(ctx)
51-
deleteChallenge(int64(cr.CreatedAt))
51+
deleteChallenge(cr.RoundCreatedAt)
5252
cr.Status = Cancelled
5353
cr.StatusMessage = errReason.Error()
5454
cr.UpdatedAt = cancellation.UTC()

code/go/0chain.net/blobbercore/challenge/worker.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,8 @@ func challengeProcessor(ctx context.Context) {
8181
return
8282

8383
case it := <-toProcessChallenge:
84+
85+
logging.Logger.Info("processing_challenge", zap.Any("challenge_id", it.ChallengeID))
8486
if ok := it.createChallenge(); !ok {
8587
continue
8688
}
@@ -142,7 +144,7 @@ func commitOnChainWorker(ctx context.Context) {
142144
}()
143145
err := challenge.VerifyChallengeTransaction(txn)
144146
if err == nil || err != ErrEntityNotFound {
145-
deleteChallenge(int64(challenge.CreatedAt))
147+
deleteChallenge(int64(challenge.RoundCreatedAt))
146148
}
147149
}(&chall)
148150
}
@@ -175,11 +177,11 @@ func getBatch(batchSize int) (chall []ChallengeEntity) {
175177

176178
func (it *ChallengeEntity) createChallenge() bool {
177179
challengeMapLock.Lock()
178-
if _, ok := challengeMap.Get(int64(it.CreatedAt)); ok {
180+
if _, ok := challengeMap.Get(it.RoundCreatedAt); ok {
179181
challengeMapLock.Unlock()
180182
return false
181183
}
182-
challengeMap.Put(int64(it.CreatedAt), it)
184+
challengeMap.Put(it.RoundCreatedAt, it)
183185
challengeMapLock.Unlock()
184186
return true
185187
}

goose/migrations/001_blobber_meta.sql

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,7 @@ CREATE TABLE public.challenges (
140140
object_path jsonb,
141141
sequence bigint,
142142
"timestamp" bigint DEFAULT 0 NOT NULL,
143+
round_created_at bigint,
143144
created_at bigint,
144145
updated_at timestamp without time zone DEFAULT CURRENT_TIMESTAMP NOT NULL
145146
);

0 commit comments

Comments
 (0)