Skip to content

Commit

Permalink
Merge branch 'master' into issue4177-eip1153
Browse files Browse the repository at this point in the history
  • Loading branch information
millken committed Apr 11, 2024
2 parents 03ac0dd + 9496ed5 commit 4b76ef0
Show file tree
Hide file tree
Showing 8 changed files with 116 additions and 24 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
55 changes: 44 additions & 11 deletions ioctl/cmd/ws/wsprojectconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,14 @@ var (
Use: "config",
Short: config.TranslateInLang(wsProjectConfigShorts, config.UILanguage),
RunE: func(cmd *cobra.Command, args []string) error {
dataSource, err := cmd.Flags().GetString("data-source")
if err != nil {
return errors.Wrap(err, "failed to get flag data-source")
}
defaultVersion, err := cmd.Flags().GetString("default-version")
if err != nil {
return errors.Wrap(err, "failed to get flag default-version")
}
version, err := cmd.Flags().GetString("version")
if err != nil {
return errors.Wrap(err, "failed to get flag version")
Expand All @@ -47,7 +55,7 @@ var (
return errors.Wrap(err, "failed to get flag output-file")
}

out, err := generateProjectFile(version, vmType, codeFile, confFile, expParam, outputFile)
out, err := generateProjectFile(dataSource, defaultVersion, version, vmType, codeFile, confFile, expParam, outputFile)
if err != nil {
return output.PrintError(err)
}
Expand All @@ -62,6 +70,14 @@ var (
config.Chinese: "生成项目的配置文件",
}

_flagDataSourceUsages = map[config.Language]string{
config.English: "data source of the project",
config.Chinese: "该project的数据源",
}
_flagDefaultVersionUsages = map[config.Language]string{
config.English: "default version for the project config",
config.Chinese: "该project config的默认版本号",
}
_flagVersionUsages = map[config.Language]string{
config.English: "version for the project config",
config.Chinese: "该project config的版本号",
Expand Down Expand Up @@ -89,19 +105,29 @@ var (
)

func init() {
wsProjectConfig.Flags().StringP("data-source", "s", "", config.TranslateInLang(_flagDataSourceUsages, config.UILanguage))
wsProjectConfig.Flags().StringP("default-version", "d", "0.1", config.TranslateInLang(_flagDefaultVersionUsages, config.UILanguage))
wsProjectConfig.Flags().StringP("version", "v", "", config.TranslateInLang(_flagVersionUsages, config.UILanguage))
wsProjectConfig.Flags().StringP("vm-type", "t", "", config.TranslateInLang(_flagVMTypeUsages, config.UILanguage))
wsProjectConfig.Flags().StringP("code-file", "i", "", config.TranslateInLang(_flagCodeFileUsages, config.UILanguage))
wsProjectConfig.Flags().StringP("conf-file", "c", "", config.TranslateInLang(_flagConfFileUsages, config.UILanguage))
wsProjectConfig.Flags().StringP("expand-param", "e", "", config.TranslateInLang(_flagExpandParamUsages, config.UILanguage))
wsProjectConfig.Flags().StringP("output-file", "u", "", config.TranslateInLang(_flagOutputFileUsages, config.UILanguage))

_ = wsProjectConfig.MarkFlagRequired("version")
_ = wsProjectConfig.MarkFlagRequired("data-source")
_ = wsProjectConfig.MarkFlagRequired("vm-type")
_ = wsProjectConfig.MarkFlagRequired("code-file")
}

func generateProjectFile(version, vmType, codeFile, confFile, expParam, outputFile string) (string, error) {
type projectConfig struct {
Version string `json:"version"`
VMType string `json:"vmType"`
Output interface{} `json:"output"`
CodeExpParam string `json:"codeExpParam,omitempty"`
Code string `json:"code"`
}

func generateProjectFile(dataSource, defaultVersion, version, vmType, codeFile, confFile, expParam, outputFile string) (string, error) {
tye, err := stringToVMType(vmType)
if err != nil {
return "", err
Expand All @@ -113,17 +139,21 @@ func generateProjectFile(version, vmType, codeFile, confFile, expParam, outputFi
}

var (
confMaps = make([]map[string]interface{}, 0)
confMap = make(map[string]interface{})
verMap projectConfig
verMaps = make([]projectConfig, 0)
outputMap = make(map[string]interface{})
)

if expParam != "" {
confMap["codeExpParam"] = expParam
verMap.CodeExpParam = expParam
}
verMap.VMType = string(tye)
verMap.Code = hexString
if version == "" {
version = "0.1"
}
confMap["vmType"] = string(tye)
confMap["code"] = hexString
confMap["version"] = version
verMap.Version = version

output := []byte(`{
"type": "stdout"
Expand All @@ -137,10 +167,13 @@ func generateProjectFile(version, vmType, codeFile, confFile, expParam, outputFi
if err := json.Unmarshal(output, &outputMap); err != nil {
return "", errors.Wrap(err, "failed to unmarshal output file")
}
confMap["output"] = outputMap
verMap.Output = outputMap
verMaps = append(verMaps, verMap)

confMaps = append(confMaps, confMap)
jsonConf, err := json.MarshalIndent(confMaps, "", " ")
confMap["datasourceURI"] = dataSource
confMap["defaultVersion"] = defaultVersion
confMap["versions"] = verMaps
jsonConf, err := json.MarshalIndent(confMap, "", " ")
if err != nil {
return "", errors.Wrap(err, "failed to marshal config maps")
}
Expand Down

0 comments on commit 4b76ef0

Please sign in to comment.