Skip to content

Commit 9cc8496

Browse files
authored
[persistence/contract] list project add count judge condition (#528)
1 parent 0a01e13 commit 9cc8496

File tree

2 files changed

+94
-29
lines changed

2 files changed

+94
-29
lines changed

persistence/contract/project.go

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -168,9 +168,14 @@ func listProject(client *ethclient.Client, projectContractAddress, blockNumberCo
168168
if err != nil {
169169
return nil, 0, 0, errors.Wrap(err, "failed to pack block number call data")
170170
}
171+
projectCountCallData, err := projectABI.Pack("count")
172+
if err != nil {
173+
return nil, 0, 0, errors.Wrap(err, "failed to pack project count call data")
174+
}
171175
ps := []*Project{}
172176
minBlockNumber := uint64(math.MaxUint64)
173177
maxBlockNumber := uint64(0)
178+
listedCount := uint64(0)
174179
for projectID := uint64(1); ; projectID++ {
175180
isValidProjectCallData, err := projectABI.Pack("isValidProject", new(big.Int).SetUint64(projectID))
176181
if err != nil {
@@ -207,9 +212,11 @@ func listProject(client *ethclient.Client, projectContractAddress, blockNumberCo
207212
projectContractAddress,
208213
projectContractAddress,
209214
projectContractAddress,
215+
projectContractAddress,
210216
},
211217
[][]byte{
212218
blockNumberCallData,
219+
projectCountCallData,
213220
isValidProjectCallData,
214221
configCallData,
215222
isPausedCallData,
@@ -232,41 +239,47 @@ func listProject(client *ethclient.Client, projectContractAddress, blockNumberCo
232239
minBlockNumber = min(minBlockNumber, blockNumber)
233240
maxBlockNumber = max(maxBlockNumber, blockNumber)
234241

235-
out, err = projectABI.Unpack("isValidProject", result[1])
242+
out, err = projectABI.Unpack("count", result[1])
243+
if err != nil {
244+
return nil, 0, 0, errors.Wrapf(err, "failed to unpack project count result, project_id %v", projectID)
245+
}
246+
projectCount := *abi.ConvertType(out[0], new(*big.Int)).(**big.Int)
247+
248+
out, err = projectABI.Unpack("isValidProject", result[2])
236249
if err != nil {
237250
return nil, 0, 0, errors.Wrapf(err, "failed to unpack is valid project result, project_id %v", projectID)
238251
}
239252
isValidProject := *abi.ConvertType(out[0], new(bool)).(*bool)
240253

241254
if !isValidProject {
242-
break
255+
continue
243256
}
244257

245-
out, err = projectABI.Unpack("config", result[2])
258+
out, err = projectABI.Unpack("config", result[3])
246259
if err != nil {
247260
return nil, 0, 0, errors.Wrapf(err, "failed to unpack project config result, project_id %v", projectID)
248261
}
249262
config := *abi.ConvertType(out[0], new(project.W3bstreamProjectProjectConfig)).(*project.W3bstreamProjectProjectConfig)
250263

251-
out, err = projectABI.Unpack("isPaused", result[3])
264+
out, err = projectABI.Unpack("isPaused", result[4])
252265
if err != nil {
253266
return nil, 0, 0, errors.Wrapf(err, "failed to unpack project is paused result, project_id %v", projectID)
254267
}
255268
isPaused := *abi.ConvertType(out[0], new(bool)).(*bool)
256269

257-
out, err = projectABI.Unpack("attributes", result[4])
270+
out, err = projectABI.Unpack("attributes", result[5])
258271
if err != nil {
259272
return nil, 0, 0, errors.Wrapf(err, "failed to unpack project attributes result, project_id %v", projectID)
260273
}
261274
proverAmt := *abi.ConvertType(out[0], new([]byte)).(*[]byte)
262275

263-
out, err = projectABI.Unpack("attributes", result[5])
276+
out, err = projectABI.Unpack("attributes", result[6])
264277
if err != nil {
265278
return nil, 0, 0, errors.Wrapf(err, "failed to unpack project attributes result, project_id %v", projectID)
266279
}
267280
vmType := *abi.ConvertType(out[0], new([]byte)).(*[]byte)
268281

269-
out, err = projectABI.Unpack("attributes", result[6])
282+
out, err = projectABI.Unpack("attributes", result[7])
270283
if err != nil {
271284
return nil, 0, 0, errors.Wrapf(err, "failed to unpack project attributes result, project_id %v", projectID)
272285
}
@@ -291,6 +304,11 @@ func listProject(client *ethclient.Client, projectContractAddress, blockNumberCo
291304
Hash: config.Hash,
292305
Attributes: attributes,
293306
})
307+
308+
listedCount++
309+
if listedCount >= projectCount.Uint64() {
310+
break
311+
}
294312
}
295313
return ps, minBlockNumber, maxBlockNumber, nil
296314
}

persistence/contract/project_test.go

Lines changed: 69 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,7 @@ func TestListProject(t *testing.T) {
223223
_, _, _, err := listProject(nil, addr, addr, addr)
224224
r.ErrorContains(err, t.Name())
225225
})
226-
t.Run("FailedToPackIsValidProjectCallData", func(t *testing.T) {
226+
t.Run("FailedToPackProjectCountCallData", func(t *testing.T) {
227227
p := gomonkey.NewPatches()
228228
defer p.Reset()
229229

@@ -242,7 +242,7 @@ func TestListProject(t *testing.T) {
242242
_, _, _, err := listProject(nil, addr, addr, addr)
243243
r.ErrorContains(err, t.Name())
244244
})
245-
t.Run("FailedToPackProjectConfigCallData", func(t *testing.T) {
245+
t.Run("FailedToPackIsValidProjectCallData", func(t *testing.T) {
246246
p := gomonkey.NewPatches()
247247
defer p.Reset()
248248

@@ -261,7 +261,7 @@ func TestListProject(t *testing.T) {
261261
_, _, _, err := listProject(nil, addr, addr, addr)
262262
r.ErrorContains(err, t.Name())
263263
})
264-
t.Run("FailedToPackProjectIsPausedCallData", func(t *testing.T) {
264+
t.Run("FailedToPackProjectConfigCallData", func(t *testing.T) {
265265
p := gomonkey.NewPatches()
266266
defer p.Reset()
267267

@@ -280,7 +280,7 @@ func TestListProject(t *testing.T) {
280280
_, _, _, err := listProject(nil, addr, addr, addr)
281281
r.ErrorContains(err, t.Name())
282282
})
283-
t.Run("FailedToPackProjectAttributesProverAmountCallData", func(t *testing.T) {
283+
t.Run("FailedToPackProjectIsPausedCallData", func(t *testing.T) {
284284
p := gomonkey.NewPatches()
285285
defer p.Reset()
286286

@@ -299,7 +299,7 @@ func TestListProject(t *testing.T) {
299299
_, _, _, err := listProject(nil, addr, addr, addr)
300300
r.ErrorContains(err, t.Name())
301301
})
302-
t.Run("FailedToPackProjectAttributesVmTypeCallData", func(t *testing.T) {
302+
t.Run("FailedToPackProjectAttributesProverAmountCallData", func(t *testing.T) {
303303
p := gomonkey.NewPatches()
304304
defer p.Reset()
305305

@@ -318,7 +318,7 @@ func TestListProject(t *testing.T) {
318318
_, _, _, err := listProject(nil, addr, addr, addr)
319319
r.ErrorContains(err, t.Name())
320320
})
321-
t.Run("FailedToPackProjectAttributesManagementAddressCallData", func(t *testing.T) {
321+
t.Run("FailedToPackProjectAttributesVmTypeCallData", func(t *testing.T) {
322322
p := gomonkey.NewPatches()
323323
defer p.Reset()
324324

@@ -337,6 +337,25 @@ func TestListProject(t *testing.T) {
337337
_, _, _, err := listProject(nil, addr, addr, addr)
338338
r.ErrorContains(err, t.Name())
339339
})
340+
t.Run("FailedToPackProjectAttributesManagementAddressCallData", func(t *testing.T) {
341+
p := gomonkey.NewPatches()
342+
defer p.Reset()
343+
344+
p.ApplyFuncReturn(multicall.NewMulticall, nil, nil)
345+
p.ApplyMethodSeq(abi.ABI{}, "Pack", []gomonkey.OutputCell{
346+
{
347+
Values: gomonkey.Params{[]byte{}, nil},
348+
Times: 7,
349+
},
350+
{
351+
Values: gomonkey.Params{nil, errors.New(t.Name())},
352+
Times: 1,
353+
},
354+
})
355+
addr := common.Address{}
356+
_, _, _, err := listProject(nil, addr, addr, addr)
357+
r.ErrorContains(err, t.Name())
358+
})
340359
t.Run("FailedToMultiCall", func(t *testing.T) {
341360
p := gomonkey.NewPatches()
342361
defer p.Reset()
@@ -349,7 +368,7 @@ func TestListProject(t *testing.T) {
349368
_, _, _, err := listProject(nil, addr, addr, addr)
350369
r.ErrorContains(err, t.Name())
351370
})
352-
multiCallRes := [][]byte{{}, {}, {}, {}, {}, {}, {}}
371+
multiCallRes := [][]byte{{}, {}, {}, {}, {}, {}, {}, {}}
353372
t.Run("FailedToUnpackBlockNumberResult", func(t *testing.T) {
354373
p := gomonkey.NewPatches()
355374
defer p.Reset()
@@ -363,7 +382,7 @@ func TestListProject(t *testing.T) {
363382
_, _, _, err := listProject(nil, addr, addr, addr)
364383
r.ErrorContains(err, t.Name())
365384
})
366-
t.Run("FailedToUnpackIsValidProjectResult", func(t *testing.T) {
385+
t.Run("FailedToUnpackProjectCountResult", func(t *testing.T) {
367386
p := gomonkey.NewPatches()
368387
defer p.Reset()
369388

@@ -391,6 +410,34 @@ func TestListProject(t *testing.T) {
391410
_, _, _, err := listProject(nil, addr, addr, addr)
392411
r.ErrorContains(err, t.Name())
393412
})
413+
t.Run("FailedToUnpackIsValidProjectResult", func(t *testing.T) {
414+
p := gomonkey.NewPatches()
415+
defer p.Reset()
416+
417+
p.ApplyFuncReturn(multicall.NewMulticall, &multicall.Multicall{}, nil)
418+
caller := &multicall.MulticallCaller{}
419+
p.ApplyMethodReturn(caller, "MultiCall", multiCallRes, nil)
420+
n := new(big.Int).SetUint64(1)
421+
p.ApplyMethodSeq(abi.ABI{}, "Unpack", []gomonkey.OutputCell{
422+
{
423+
Values: gomonkey.Params{[]interface{}{&n}, nil},
424+
Times: 2,
425+
},
426+
{
427+
Values: gomonkey.Params{nil, errors.New(t.Name())},
428+
Times: 1,
429+
},
430+
})
431+
p.ApplyFuncSeq(abi.ConvertType, []gomonkey.OutputCell{
432+
{
433+
Values: gomonkey.Params{&n},
434+
Times: 2,
435+
},
436+
})
437+
addr := common.Address{}
438+
_, _, _, err := listProject(nil, addr, addr, addr)
439+
r.ErrorContains(err, t.Name())
440+
})
394441
t.Run("FailedToUnpackProjectConfigResult", func(t *testing.T) {
395442
p := gomonkey.NewPatches()
396443
defer p.Reset()
@@ -403,7 +450,7 @@ func TestListProject(t *testing.T) {
403450
p.ApplyMethodSeq(abi.ABI{}, "Unpack", []gomonkey.OutputCell{
404451
{
405452
Values: gomonkey.Params{[]interface{}{&n}, nil},
406-
Times: 1,
453+
Times: 2,
407454
},
408455
{
409456
Values: gomonkey.Params{[]interface{}{&isValidProject}, nil},
@@ -417,7 +464,7 @@ func TestListProject(t *testing.T) {
417464
p.ApplyFuncSeq(abi.ConvertType, []gomonkey.OutputCell{
418465
{
419466
Values: gomonkey.Params{&n},
420-
Times: 1,
467+
Times: 2,
421468
},
422469
{
423470
Values: gomonkey.Params{&isValidProject},
@@ -442,7 +489,7 @@ func TestListProject(t *testing.T) {
442489
p.ApplyMethodSeq(abi.ABI{}, "Unpack", []gomonkey.OutputCell{
443490
{
444491
Values: gomonkey.Params{[]interface{}{&n}, nil},
445-
Times: 1,
492+
Times: 2,
446493
},
447494
{
448495
Values: gomonkey.Params{[]interface{}{&isValidProject}, nil},
@@ -460,7 +507,7 @@ func TestListProject(t *testing.T) {
460507
p.ApplyFuncSeq(abi.ConvertType, []gomonkey.OutputCell{
461508
{
462509
Values: gomonkey.Params{&n},
463-
Times: 1,
510+
Times: 2,
464511
},
465512
{
466513
Values: gomonkey.Params{&isValidProject},
@@ -489,7 +536,7 @@ func TestListProject(t *testing.T) {
489536
p.ApplyMethodSeq(abi.ABI{}, "Unpack", []gomonkey.OutputCell{
490537
{
491538
Values: gomonkey.Params{[]interface{}{&n}, nil},
492-
Times: 1,
539+
Times: 2,
493540
},
494541
{
495542
Values: gomonkey.Params{[]interface{}{&isValidProject}, nil},
@@ -511,7 +558,7 @@ func TestListProject(t *testing.T) {
511558
p.ApplyFuncSeq(abi.ConvertType, []gomonkey.OutputCell{
512559
{
513560
Values: gomonkey.Params{&n},
514-
Times: 1,
561+
Times: 2,
515562
},
516563
{
517564
Values: gomonkey.Params{&isValidProject},
@@ -545,7 +592,7 @@ func TestListProject(t *testing.T) {
545592
p.ApplyMethodSeq(abi.ABI{}, "Unpack", []gomonkey.OutputCell{
546593
{
547594
Values: gomonkey.Params{[]interface{}{&n}, nil},
548-
Times: 1,
595+
Times: 2,
549596
},
550597
{
551598
Values: gomonkey.Params{[]interface{}{&isValidProject}, nil},
@@ -571,7 +618,7 @@ func TestListProject(t *testing.T) {
571618
p.ApplyFuncSeq(abi.ConvertType, []gomonkey.OutputCell{
572619
{
573620
Values: gomonkey.Params{&n},
574-
Times: 1,
621+
Times: 2,
575622
},
576623
{
577624
Values: gomonkey.Params{&isValidProject},
@@ -609,7 +656,7 @@ func TestListProject(t *testing.T) {
609656
p.ApplyMethodSeq(abi.ABI{}, "Unpack", []gomonkey.OutputCell{
610657
{
611658
Values: gomonkey.Params{[]interface{}{&n}, nil},
612-
Times: 1,
659+
Times: 2,
613660
},
614661
{
615662
Values: gomonkey.Params{[]interface{}{&isValidProject}, nil},
@@ -639,7 +686,7 @@ func TestListProject(t *testing.T) {
639686
p.ApplyFuncSeq(abi.ConvertType, []gomonkey.OutputCell{
640687
{
641688
Values: gomonkey.Params{&n},
642-
Times: 1,
689+
Times: 2,
643690
},
644691
{
645692
Values: gomonkey.Params{&isValidProject},
@@ -682,7 +729,7 @@ func TestListProject(t *testing.T) {
682729
p.ApplyMethodSeq(abi.ABI{}, "Unpack", []gomonkey.OutputCell{
683730
{
684731
Values: gomonkey.Params{[]interface{}{&n}, nil},
685-
Times: 1,
732+
Times: 2,
686733
},
687734
{
688735
Values: gomonkey.Params{[]interface{}{&isValidProject}, nil},
@@ -710,7 +757,7 @@ func TestListProject(t *testing.T) {
710757
},
711758
{
712759
Values: gomonkey.Params{[]interface{}{&n}, nil},
713-
Times: 1,
760+
Times: 2,
714761
},
715762
{
716763
Values: gomonkey.Params{[]interface{}{&isInValidProject}, nil},
@@ -720,7 +767,7 @@ func TestListProject(t *testing.T) {
720767
p.ApplyFuncSeq(abi.ConvertType, []gomonkey.OutputCell{
721768
{
722769
Values: gomonkey.Params{&n},
723-
Times: 1,
770+
Times: 2,
724771
},
725772
{
726773
Values: gomonkey.Params{&isValidProject},
@@ -748,7 +795,7 @@ func TestListProject(t *testing.T) {
748795
},
749796
{
750797
Values: gomonkey.Params{&n},
751-
Times: 1,
798+
Times: 2,
752799
},
753800
{
754801
Values: gomonkey.Params{&isInValidProject},

0 commit comments

Comments
 (0)