Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[api] set candidate id for fetching api #4324

Merged
merged 3 commits into from
Jul 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions action/protocol/staking/candidate.go
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,7 @@ func (d *Candidate) toIoTeXTypes() *iotextypes.CandidateV2 {
TotalWeightedVotes: d.Votes.String(),
SelfStakeBucketIdx: d.SelfStakeBucketIdx,
SelfStakingTokens: d.SelfStake.String(),
Id: d.GetIdentifier().String(),
}
}

Expand Down
6 changes: 6 additions & 0 deletions action/protocol/staking/candidate_buckets_indexer.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,12 @@ func (cbi *CandidatesBucketsIndexer) GetCandidates(height uint64, offset, limit
end = uint32(len(candidateList.Candidates))
}
candidateList.Candidates = candidateList.Candidates[offset:end]
// fill id if it's empty for backward compatibility
for i := range candidateList.Candidates {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ListV2 is array of pointer, so can simplify

for _, v := range {
    v.Id = v.OwnerAddress
}

if candidateList.Candidates[i].Id == "" {
candidateList.Candidates[i].Id = candidateList.Candidates[i].OwnerAddress
}
}
return candidateList, height, nil
}

Expand Down
3 changes: 3 additions & 0 deletions action/protocol/staking/candidate_buckets_indexer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ func TestCandidatesBucketsIndexer_PutGetCandidates(t *testing.T) {
OwnerAddress: "owner1",
Name: "abc",
SelfStakeBucketIdx: 123,
Id: "owner1",
},
}
cand2 := &iotextypes.CandidateListV2{
Expand All @@ -53,6 +54,7 @@ func TestCandidatesBucketsIndexer_PutGetCandidates(t *testing.T) {
OwnerAddress: "owner2",
Name: "xyz",
SelfStakeBucketIdx: 456,
Id: "owner2",
}),
}

Expand Down Expand Up @@ -126,6 +128,7 @@ func TestCandidatesBucketsIndexer_PutGetCandidates(t *testing.T) {
OwnerAddress: "ownermax",
Name: "alphabeta",
SelfStakeBucketIdx: 789,
Id: "ownermax",
}),
}
require.NoError(cbi.PutCandidates(height, candMax))
Expand Down
2 changes: 1 addition & 1 deletion e2etest/contract_staking_v2_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ func TestContractStakingV2(t *testing.T) {
act: &actionWithTime{mustNoErr(action.SignedExecution(contractAddress, identityset.PrivateKey(stakerID), test.nonceMgr.pop(identityset.Address(stakerID).String()), stakeAmount, gasLimit, gasPrice, mustCallData("stake(uint256,address)", stakeDurationBlocks, common.BytesToAddress(identityset.Address(candOwnerID).Bytes())), action.WithChainID(chainID))), stakeTime},
expect: []actionExpect{successExpect,
&bucketExpect{&iotextypes.VoteBucket{Index: 1, ContractAddress: contractAddress, Owner: identityset.Address(stakerID).String(), CandidateAddress: identityset.Address(candOwnerID).String(), StakedDuration: uint32(stakeDurationBlocks.Uint64() / uint64(blocksPerDay)), StakedDurationBlockNumber: stakeDurationBlocks.Uint64(), CreateTime: timestamppb.New(time.Time{}), StakeStartTime: timestamppb.New(time.Time{}), StakeStartBlockHeight: 3, CreateBlockHeight: 3, UnstakeStartTime: timestamppb.New(time.Time{}), UnstakeStartBlockHeight: uint64(math.MaxUint64), StakedAmount: stakeAmount.String(), AutoStake: true}},
&candidateExpect{"cand1", &iotextypes.CandidateV2{OwnerAddress: identityset.Address(candOwnerID).String(), OperatorAddress: identityset.Address(1).String(), RewardAddress: identityset.Address(1).String(), Name: "cand1", TotalWeightedVotes: "1256001586604779503009155", SelfStakingTokens: registerAmount.String(), SelfStakeBucketIdx: 0}},
&candidateExpect{"cand1", &iotextypes.CandidateV2{OwnerAddress: identityset.Address(candOwnerID).String(), Id: identityset.Address(candOwnerID).String(), OperatorAddress: identityset.Address(1).String(), RewardAddress: identityset.Address(1).String(), Name: "cand1", TotalWeightedVotes: "1256001586604779503009155", SelfStakingTokens: registerAmount.String(), SelfStakeBucketIdx: 0}},
&functionExpect{func(test *e2etest, act *action.SealedEnvelope, receipt *action.Receipt, err error) {
candidate, err := test.getCandidateByName("cand1")
require.NoError(err)
Expand Down
Loading
Loading