Skip to content

Commit

Permalink
[staking] Add endorsement info in bucket fetch apis (#4207)
Browse files Browse the repository at this point in the history
  • Loading branch information
envestcc committed Apr 10, 2024
1 parent b4f0ec3 commit 1791f82
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 13 deletions.
8 changes: 4 additions & 4 deletions action/protocol/staking/candidate_statereader.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand All @@ -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
}

Expand All @@ -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
}

Expand All @@ -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
}

Expand Down
2 changes: 1 addition & 1 deletion action/protocol/staking/protocol.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
16 changes: 15 additions & 1 deletion action/protocol/staking/read_state.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)),
}
Expand All @@ -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
Expand Down
8 changes: 4 additions & 4 deletions action/protocol/staking/staking_statereader.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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
}
Expand Down Expand Up @@ -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
}
Expand All @@ -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
}
Expand Down
43 changes: 43 additions & 0 deletions action/protocol/staking/staking_statereader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand All @@ -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)
Expand All @@ -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{
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 4 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down

0 comments on commit 1791f82

Please sign in to comment.