diff --git a/action/protocol/staking/candidate_statereader.go b/action/protocol/staking/candidate_statereader.go index 1173e3b23e..31a091598a 100644 --- a/action/protocol/staking/candidate_statereader.go +++ b/action/protocol/staking/candidate_statereader.go @@ -371,7 +371,7 @@ func (c *candSR) readStateBuckets(ctx context.Context, req *iotexapi.ReadStaking offset := int(req.GetPagination().GetOffset()) limit := int(req.GetPagination().GetLimit()) buckets := getPageOfBuckets(all, offset, limit) - pbBuckets, err := toIoTeXTypesVoteBucketList(buckets) + pbBuckets, err := toIoTeXTypesVoteBucketList(c.SR(), buckets) return pbBuckets, height, err } @@ -396,7 +396,7 @@ func (c *candSR) readStateBucketsByVoter(ctx context.Context, req *iotexapi.Read offset := int(req.GetPagination().GetOffset()) limit := int(req.GetPagination().GetLimit()) buckets = getPageOfBuckets(buckets, offset, limit) - pbBuckets, err := toIoTeXTypesVoteBucketList(buckets) + pbBuckets, err := toIoTeXTypesVoteBucketList(c.SR(), buckets) return pbBuckets, height, err } @@ -421,7 +421,7 @@ func (c *candSR) readStateBucketsByCandidate(ctx context.Context, req *iotexapi. offset := int(req.GetPagination().GetOffset()) limit := int(req.GetPagination().GetLimit()) buckets = getPageOfBuckets(buckets, offset, limit) - pbBuckets, err := toIoTeXTypesVoteBucketList(buckets) + pbBuckets, err := toIoTeXTypesVoteBucketList(c.SR(), buckets) return pbBuckets, height, err } @@ -434,7 +434,7 @@ func (c *candSR) readStateBucketByIndices(ctx context.Context, req *iotexapi.Rea if err != nil { return nil, height, err } - pbBuckets, err := toIoTeXTypesVoteBucketList(buckets) + pbBuckets, err := toIoTeXTypesVoteBucketList(c.SR(), buckets) return pbBuckets, height, err } diff --git a/action/protocol/staking/protocol.go b/action/protocol/staking/protocol.go index dc6891b881..33e85d33df 100644 --- a/action/protocol/staking/protocol.go +++ b/action/protocol/staking/protocol.go @@ -314,7 +314,7 @@ func (p *Protocol) handleStakingIndexer(epochStartHeight uint64, sm protocol.Sta if err != nil && errors.Cause(err) != state.ErrStateNotExist { return err } - buckets, err := toIoTeXTypesVoteBucketList(allBuckets) + buckets, err := toIoTeXTypesVoteBucketList(sm, allBuckets) if err != nil { return err } diff --git a/action/protocol/staking/read_state.go b/action/protocol/staking/read_state.go index ea07e302fb..849a9f9f6b 100644 --- a/action/protocol/staking/read_state.go +++ b/action/protocol/staking/read_state.go @@ -10,11 +10,14 @@ import ( "math/big" "github.com/iotexproject/iotex-proto/golang/iotextypes" + "github.com/pkg/errors" "github.com/iotexproject/iotex-core/action/protocol" + "github.com/iotexproject/iotex-core/state" ) -func toIoTeXTypesVoteBucketList(buckets []*VoteBucket) (*iotextypes.VoteBucketList, error) { +func toIoTeXTypesVoteBucketList(sr protocol.StateReader, buckets []*VoteBucket) (*iotextypes.VoteBucketList, error) { + esr := NewEndorsementStateReader(sr) res := iotextypes.VoteBucketList{ Buckets: make([]*iotextypes.VoteBucket, 0, len(buckets)), } @@ -23,6 +26,17 @@ func toIoTeXTypesVoteBucketList(buckets []*VoteBucket) (*iotextypes.VoteBucketLi if err != nil { return nil, err } + // fill in the endorsement + if b.isNative() { + endorsement, err := esr.Get(b.Index) + switch errors.Cause(err) { + case nil: + typBucket.EndorsementExpireBlockHeight = endorsement.ExpireHeight + case state.ErrStateNotExist: + default: + return nil, err + } + } res.Buckets = append(res.Buckets, typBucket) } return &res, nil diff --git a/action/protocol/staking/staking_statereader.go b/action/protocol/staking/staking_statereader.go index 339565c6ee..affa4be1e4 100644 --- a/action/protocol/staking/staking_statereader.go +++ b/action/protocol/staking/staking_statereader.go @@ -77,7 +77,7 @@ func (c *compositeStakingStateReader) readStateBuckets(ctx context.Context, req if err != nil { return nil, 0, err } - lsdIoTeXBuckets, err := toIoTeXTypesVoteBucketList(lsdBuckets) + lsdIoTeXBuckets, err := toIoTeXTypesVoteBucketList(c.nativeSR.SR(), lsdBuckets) if err != nil { return nil, 0, err } @@ -104,7 +104,7 @@ func (c *compositeStakingStateReader) readStateBucketsByVoter(ctx context.Contex return nil, 0, err } lsdBuckets = filterBucketsByVoter(lsdBuckets, req.GetVoterAddress()) - lsdIoTeXBuckets, err := toIoTeXTypesVoteBucketList(lsdBuckets) + lsdIoTeXBuckets, err := toIoTeXTypesVoteBucketList(c.nativeSR.SR(), lsdBuckets) if err != nil { return nil, 0, err } @@ -135,7 +135,7 @@ func (c *compositeStakingStateReader) readStateBucketsByCandidate(ctx context.Co if err != nil { return nil, 0, err } - lsdIoTeXBuckets, err := toIoTeXTypesVoteBucketList(lsdBuckets) + lsdIoTeXBuckets, err := toIoTeXTypesVoteBucketList(c.nativeSR.SR(), lsdBuckets) if err != nil { return nil, 0, err } @@ -160,7 +160,7 @@ func (c *compositeStakingStateReader) readStateBucketByIndices(ctx context.Conte if err != nil { return nil, 0, err } - lsbIoTeXBuckets, err := toIoTeXTypesVoteBucketList(lsdBuckets) + lsbIoTeXBuckets, err := toIoTeXTypesVoteBucketList(c.nativeSR.SR(), lsdBuckets) if err != nil { return nil, 0, err } diff --git a/action/protocol/staking/staking_statereader_test.go b/action/protocol/staking/staking_statereader_test.go index 82bc5aa263..2c681cb609 100644 --- a/action/protocol/staking/staking_statereader_test.go +++ b/action/protocol/staking/staking_statereader_test.go @@ -155,6 +155,7 @@ func TestStakingStateReader(t *testing.T) { iter := state.NewIterator(states) return uint64(1), iter, nil }).Times(1) + sf.EXPECT().State(gomock.Any(), gomock.Any()).Return(uint64(0), state.ErrStateNotExist).Times(1) req := &iotexapi.ReadStakingDataRequest_VoteBuckets{ Pagination: &iotexapi.PaginationParam{ @@ -177,6 +178,39 @@ func TestStakingStateReader(t *testing.T) { r.Equal(iotexBucket, buckets.Buckets[i+len(testNativeBuckets)]) } }) + t.Run("readStateBucketsWithEndorsement", func(t *testing.T) { + sf, _, stakeSR, ctx, r := prepare(t) + sf.EXPECT().States(gomock.Any(), gomock.Any()).DoAndReturn(func(arg0 ...protocol.StateOption) (uint64, state.Iterator, error) { + iter := state.NewIterator(states) + return uint64(1), iter, nil + }).Times(1) + sf.EXPECT().State(gomock.AssignableToTypeOf(&Endorsement{}), gomock.Any()).DoAndReturn(func(arg0 any, arg1 ...protocol.StateOption) (uint64, error) { + arg0R := arg0.(*Endorsement) + *arg0R = Endorsement{ExpireHeight: 100} + return uint64(1), nil + }).AnyTimes() + + req := &iotexapi.ReadStakingDataRequest_VoteBuckets{ + Pagination: &iotexapi.PaginationParam{ + Offset: 0, + Limit: 100, + }, + } + buckets, height, err := stakeSR.readStateBuckets(ctx, req) + r.NoError(err) + r.EqualValues(1, height) + r.Len(buckets.Buckets, len(testNativeBuckets)+len(testContractBuckets)) + iotexBuckets, err := toIoTeXTypesVoteBucketList(sf, testNativeBuckets) + r.NoError(err) + for i := range testNativeBuckets { + r.Equal(iotexBuckets.Buckets[i], buckets.Buckets[i]) + } + iotexBuckets, err = toIoTeXTypesVoteBucketList(sf, testContractBuckets) + r.NoError(err) + for i := range testContractBuckets { + r.Equal(iotexBuckets.Buckets[i], buckets.Buckets[i+len(testNativeBuckets)]) + } + }) t.Run("readStateBucketsByVoter", func(t *testing.T) { sf, _, stakeSR, ctx, r := prepare(t) @@ -200,6 +234,9 @@ func TestStakingStateReader(t *testing.T) { *arg0R = totalBucketCount{count: 1} return uint64(1), nil }).Times(1) + sf.EXPECT().State(gomock.AssignableToTypeOf(&Endorsement{}), gomock.Any()).DoAndReturn(func(arg0 any, arg1 ...protocol.StateOption) (uint64, error) { + return uint64(0), state.ErrStateNotExist + }).Times(1) req := &iotexapi.ReadStakingDataRequest_VoteBucketsByVoter{ Pagination: &iotexapi.PaginationParam{ @@ -241,6 +278,9 @@ func TestStakingStateReader(t *testing.T) { *arg0R = totalBucketCount{count: 1} return uint64(1), nil }).Times(1) + sf.EXPECT().State(gomock.AssignableToTypeOf(&Endorsement{}), gomock.Any()).DoAndReturn(func(arg0 any, arg1 ...protocol.StateOption) (uint64, error) { + return uint64(0), state.ErrStateNotExist + }).Times(1) contractIndexer.EXPECT().BucketsByCandidate(gomock.Any(), gomock.Any()).DoAndReturn(func(arg0 address.Address, arg1 uint64) ([]*VoteBucket, error) { buckets := []*VoteBucket{} for i := range testContractBuckets { @@ -285,6 +325,9 @@ func TestStakingStateReader(t *testing.T) { *arg0R = totalBucketCount{count: 1} return uint64(1), nil }).Times(1) + sf.EXPECT().State(gomock.AssignableToTypeOf(&Endorsement{}), gomock.Any()).DoAndReturn(func(arg0 any, arg1 ...protocol.StateOption) (uint64, error) { + return uint64(0), state.ErrStateNotExist + }).Times(1) contractIndexer.EXPECT().BucketsByIndices(gomock.Any(), gomock.Any()).DoAndReturn(func(arg0 []uint64, arg1 uint64) ([]*VoteBucket, error) { buckets := []*VoteBucket{} for i := range arg0 { diff --git a/go.mod b/go.mod index 3d83ef6c39..348df63d3b 100644 --- a/go.mod +++ b/go.mod @@ -24,7 +24,7 @@ require ( github.com/iotexproject/iotex-address v0.2.8 github.com/iotexproject/iotex-antenna-go/v2 v2.5.1 github.com/iotexproject/iotex-election v0.3.5-0.20210611041425-20ddf674363d - github.com/iotexproject/iotex-proto v0.5.15-0.20240105060115-b12b162afdde + github.com/iotexproject/iotex-proto v0.6.0 github.com/ipfs/go-ipfs-api v0.2.0 github.com/libp2p/go-libp2p-core v0.8.5 github.com/mackerelio/go-osstat v0.2.4 diff --git a/go.sum b/go.sum index 62696e74ff..1acfc12791 100644 --- a/go.sum +++ b/go.sum @@ -664,8 +664,10 @@ github.com/iotexproject/iotex-antenna-go/v2 v2.5.1/go.mod h1:8pDZcM45M0gY6jm3PoM github.com/iotexproject/iotex-election v0.3.5-0.20210611041425-20ddf674363d h1:/j1xCAC9YiG/8UKqYvycS/v3ddVsb1G7AMyLXOjeYI0= github.com/iotexproject/iotex-election v0.3.5-0.20210611041425-20ddf674363d/go.mod h1:GRWevxtqQ4gPMrd7Qxhr29/7aTgvjiTp+rFI9KMMZEo= github.com/iotexproject/iotex-proto v0.5.0/go.mod h1:Xg6REkv+nTZN+OC22xXIQuqKdTWWHwOAJEXCoMpDwtI= -github.com/iotexproject/iotex-proto v0.5.15-0.20240105060115-b12b162afdde h1:rs5eACTonHCALTwT9rhqMUEVYMQgbnNYMZQ85jJUlBY= -github.com/iotexproject/iotex-proto v0.5.15-0.20240105060115-b12b162afdde/go.mod h1:wQpCk3Df0fPID+K8ohiICGj+cWRmcQ3wanT+aSrnIPo= +github.com/iotexproject/iotex-proto v0.5.15 h1:9+6szZDQ1HhSFKyB2kVlVPXdCFAHHw72VVGcYXQ7P/w= +github.com/iotexproject/iotex-proto v0.5.15/go.mod h1:wQpCk3Df0fPID+K8ohiICGj+cWRmcQ3wanT+aSrnIPo= +github.com/iotexproject/iotex-proto v0.6.0 h1:UIwPq5QuuPwR7G4OZzmyBsbvEJ+YH6oHyzRjxGk9Fow= +github.com/iotexproject/iotex-proto v0.6.0/go.mod h1:wQpCk3Df0fPID+K8ohiICGj+cWRmcQ3wanT+aSrnIPo= github.com/ipfs/go-cid v0.0.1/go.mod h1:GHWU/WuQdMPmIosc4Yn1bcCT7dSeX4lBafM7iqUPQvM= github.com/ipfs/go-cid v0.0.2/go.mod h1:GHWU/WuQdMPmIosc4Yn1bcCT7dSeX4lBafM7iqUPQvM= github.com/ipfs/go-cid v0.0.3/go.mod h1:GHWU/WuQdMPmIosc4Yn1bcCT7dSeX4lBafM7iqUPQvM=