Skip to content

Commit

Permalink
[api] set candidate id for fetching api (#4324)
Browse files Browse the repository at this point in the history
  • Loading branch information
envestcc committed Jul 8, 2024
1 parent 41258f5 commit 73bdb8f
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 20 deletions.
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 {
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

0 comments on commit 73bdb8f

Please sign in to comment.