Skip to content

Commit

Permalink
[persistence/contract] list project add count judge condition (#528)
Browse files Browse the repository at this point in the history
  • Loading branch information
huangzhiran authored May 27, 2024
1 parent 0a01e13 commit 9cc8496
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 29 deletions.
32 changes: 25 additions & 7 deletions persistence/contract/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,9 +168,14 @@ func listProject(client *ethclient.Client, projectContractAddress, blockNumberCo
if err != nil {
return nil, 0, 0, errors.Wrap(err, "failed to pack block number call data")
}
projectCountCallData, err := projectABI.Pack("count")
if err != nil {
return nil, 0, 0, errors.Wrap(err, "failed to pack project count call data")
}
ps := []*Project{}
minBlockNumber := uint64(math.MaxUint64)
maxBlockNumber := uint64(0)
listedCount := uint64(0)
for projectID := uint64(1); ; projectID++ {
isValidProjectCallData, err := projectABI.Pack("isValidProject", new(big.Int).SetUint64(projectID))
if err != nil {
Expand Down Expand Up @@ -207,9 +212,11 @@ func listProject(client *ethclient.Client, projectContractAddress, blockNumberCo
projectContractAddress,
projectContractAddress,
projectContractAddress,
projectContractAddress,
},
[][]byte{
blockNumberCallData,
projectCountCallData,
isValidProjectCallData,
configCallData,
isPausedCallData,
Expand All @@ -232,41 +239,47 @@ func listProject(client *ethclient.Client, projectContractAddress, blockNumberCo
minBlockNumber = min(minBlockNumber, blockNumber)
maxBlockNumber = max(maxBlockNumber, blockNumber)

out, err = projectABI.Unpack("isValidProject", result[1])
out, err = projectABI.Unpack("count", result[1])
if err != nil {
return nil, 0, 0, errors.Wrapf(err, "failed to unpack project count result, project_id %v", projectID)
}
projectCount := *abi.ConvertType(out[0], new(*big.Int)).(**big.Int)

out, err = projectABI.Unpack("isValidProject", result[2])
if err != nil {
return nil, 0, 0, errors.Wrapf(err, "failed to unpack is valid project result, project_id %v", projectID)
}
isValidProject := *abi.ConvertType(out[0], new(bool)).(*bool)

if !isValidProject {
break
continue
}

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

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

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

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

out, err = projectABI.Unpack("attributes", result[6])
out, err = projectABI.Unpack("attributes", result[7])
if err != nil {
return nil, 0, 0, errors.Wrapf(err, "failed to unpack project attributes result, project_id %v", projectID)
}
Expand All @@ -291,6 +304,11 @@ func listProject(client *ethclient.Client, projectContractAddress, blockNumberCo
Hash: config.Hash,
Attributes: attributes,
})

listedCount++
if listedCount >= projectCount.Uint64() {
break
}
}
return ps, minBlockNumber, maxBlockNumber, nil
}
Expand Down
91 changes: 69 additions & 22 deletions persistence/contract/project_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ func TestListProject(t *testing.T) {
_, _, _, err := listProject(nil, addr, addr, addr)
r.ErrorContains(err, t.Name())
})
t.Run("FailedToPackIsValidProjectCallData", func(t *testing.T) {
t.Run("FailedToPackProjectCountCallData", func(t *testing.T) {
p := gomonkey.NewPatches()
defer p.Reset()

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

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

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

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

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

Expand All @@ -337,6 +337,25 @@ func TestListProject(t *testing.T) {
_, _, _, err := listProject(nil, addr, addr, addr)
r.ErrorContains(err, t.Name())
})
t.Run("FailedToPackProjectAttributesManagementAddressCallData", func(t *testing.T) {
p := gomonkey.NewPatches()
defer p.Reset()

p.ApplyFuncReturn(multicall.NewMulticall, nil, nil)
p.ApplyMethodSeq(abi.ABI{}, "Pack", []gomonkey.OutputCell{
{
Values: gomonkey.Params{[]byte{}, nil},
Times: 7,
},
{
Values: gomonkey.Params{nil, errors.New(t.Name())},
Times: 1,
},
})
addr := common.Address{}
_, _, _, err := listProject(nil, addr, addr, addr)
r.ErrorContains(err, t.Name())
})
t.Run("FailedToMultiCall", func(t *testing.T) {
p := gomonkey.NewPatches()
defer p.Reset()
Expand All @@ -349,7 +368,7 @@ func TestListProject(t *testing.T) {
_, _, _, err := listProject(nil, addr, addr, addr)
r.ErrorContains(err, t.Name())
})
multiCallRes := [][]byte{{}, {}, {}, {}, {}, {}, {}}
multiCallRes := [][]byte{{}, {}, {}, {}, {}, {}, {}, {}}
t.Run("FailedToUnpackBlockNumberResult", func(t *testing.T) {
p := gomonkey.NewPatches()
defer p.Reset()
Expand All @@ -363,7 +382,7 @@ func TestListProject(t *testing.T) {
_, _, _, err := listProject(nil, addr, addr, addr)
r.ErrorContains(err, t.Name())
})
t.Run("FailedToUnpackIsValidProjectResult", func(t *testing.T) {
t.Run("FailedToUnpackProjectCountResult", func(t *testing.T) {
p := gomonkey.NewPatches()
defer p.Reset()

Expand Down Expand Up @@ -391,6 +410,34 @@ func TestListProject(t *testing.T) {
_, _, _, err := listProject(nil, addr, addr, addr)
r.ErrorContains(err, t.Name())
})
t.Run("FailedToUnpackIsValidProjectResult", func(t *testing.T) {
p := gomonkey.NewPatches()
defer p.Reset()

p.ApplyFuncReturn(multicall.NewMulticall, &multicall.Multicall{}, nil)
caller := &multicall.MulticallCaller{}
p.ApplyMethodReturn(caller, "MultiCall", multiCallRes, nil)
n := new(big.Int).SetUint64(1)
p.ApplyMethodSeq(abi.ABI{}, "Unpack", []gomonkey.OutputCell{
{
Values: gomonkey.Params{[]interface{}{&n}, nil},
Times: 2,
},
{
Values: gomonkey.Params{nil, errors.New(t.Name())},
Times: 1,
},
})
p.ApplyFuncSeq(abi.ConvertType, []gomonkey.OutputCell{
{
Values: gomonkey.Params{&n},
Times: 2,
},
})
addr := common.Address{}
_, _, _, err := listProject(nil, addr, addr, addr)
r.ErrorContains(err, t.Name())
})
t.Run("FailedToUnpackProjectConfigResult", func(t *testing.T) {
p := gomonkey.NewPatches()
defer p.Reset()
Expand All @@ -403,7 +450,7 @@ func TestListProject(t *testing.T) {
p.ApplyMethodSeq(abi.ABI{}, "Unpack", []gomonkey.OutputCell{
{
Values: gomonkey.Params{[]interface{}{&n}, nil},
Times: 1,
Times: 2,
},
{
Values: gomonkey.Params{[]interface{}{&isValidProject}, nil},
Expand All @@ -417,7 +464,7 @@ func TestListProject(t *testing.T) {
p.ApplyFuncSeq(abi.ConvertType, []gomonkey.OutputCell{
{
Values: gomonkey.Params{&n},
Times: 1,
Times: 2,
},
{
Values: gomonkey.Params{&isValidProject},
Expand All @@ -442,7 +489,7 @@ func TestListProject(t *testing.T) {
p.ApplyMethodSeq(abi.ABI{}, "Unpack", []gomonkey.OutputCell{
{
Values: gomonkey.Params{[]interface{}{&n}, nil},
Times: 1,
Times: 2,
},
{
Values: gomonkey.Params{[]interface{}{&isValidProject}, nil},
Expand All @@ -460,7 +507,7 @@ func TestListProject(t *testing.T) {
p.ApplyFuncSeq(abi.ConvertType, []gomonkey.OutputCell{
{
Values: gomonkey.Params{&n},
Times: 1,
Times: 2,
},
{
Values: gomonkey.Params{&isValidProject},
Expand Down Expand Up @@ -489,7 +536,7 @@ func TestListProject(t *testing.T) {
p.ApplyMethodSeq(abi.ABI{}, "Unpack", []gomonkey.OutputCell{
{
Values: gomonkey.Params{[]interface{}{&n}, nil},
Times: 1,
Times: 2,
},
{
Values: gomonkey.Params{[]interface{}{&isValidProject}, nil},
Expand All @@ -511,7 +558,7 @@ func TestListProject(t *testing.T) {
p.ApplyFuncSeq(abi.ConvertType, []gomonkey.OutputCell{
{
Values: gomonkey.Params{&n},
Times: 1,
Times: 2,
},
{
Values: gomonkey.Params{&isValidProject},
Expand Down Expand Up @@ -545,7 +592,7 @@ func TestListProject(t *testing.T) {
p.ApplyMethodSeq(abi.ABI{}, "Unpack", []gomonkey.OutputCell{
{
Values: gomonkey.Params{[]interface{}{&n}, nil},
Times: 1,
Times: 2,
},
{
Values: gomonkey.Params{[]interface{}{&isValidProject}, nil},
Expand All @@ -571,7 +618,7 @@ func TestListProject(t *testing.T) {
p.ApplyFuncSeq(abi.ConvertType, []gomonkey.OutputCell{
{
Values: gomonkey.Params{&n},
Times: 1,
Times: 2,
},
{
Values: gomonkey.Params{&isValidProject},
Expand Down Expand Up @@ -609,7 +656,7 @@ func TestListProject(t *testing.T) {
p.ApplyMethodSeq(abi.ABI{}, "Unpack", []gomonkey.OutputCell{
{
Values: gomonkey.Params{[]interface{}{&n}, nil},
Times: 1,
Times: 2,
},
{
Values: gomonkey.Params{[]interface{}{&isValidProject}, nil},
Expand Down Expand Up @@ -639,7 +686,7 @@ func TestListProject(t *testing.T) {
p.ApplyFuncSeq(abi.ConvertType, []gomonkey.OutputCell{
{
Values: gomonkey.Params{&n},
Times: 1,
Times: 2,
},
{
Values: gomonkey.Params{&isValidProject},
Expand Down Expand Up @@ -682,7 +729,7 @@ func TestListProject(t *testing.T) {
p.ApplyMethodSeq(abi.ABI{}, "Unpack", []gomonkey.OutputCell{
{
Values: gomonkey.Params{[]interface{}{&n}, nil},
Times: 1,
Times: 2,
},
{
Values: gomonkey.Params{[]interface{}{&isValidProject}, nil},
Expand Down Expand Up @@ -710,7 +757,7 @@ func TestListProject(t *testing.T) {
},
{
Values: gomonkey.Params{[]interface{}{&n}, nil},
Times: 1,
Times: 2,
},
{
Values: gomonkey.Params{[]interface{}{&isInValidProject}, nil},
Expand All @@ -720,7 +767,7 @@ func TestListProject(t *testing.T) {
p.ApplyFuncSeq(abi.ConvertType, []gomonkey.OutputCell{
{
Values: gomonkey.Params{&n},
Times: 1,
Times: 2,
},
{
Values: gomonkey.Params{&isValidProject},
Expand Down Expand Up @@ -748,7 +795,7 @@ func TestListProject(t *testing.T) {
},
{
Values: gomonkey.Params{&n},
Times: 1,
Times: 2,
},
{
Values: gomonkey.Params{&isInValidProject},
Expand Down

0 comments on commit 9cc8496

Please sign in to comment.