From 1791f82354c672631690e7b759de6bb51cb2a031 Mon Sep 17 00:00:00 2001 From: Chen Chen <34592639+envestcc@users.noreply.github.com> Date: Wed, 10 Apr 2024 14:28:42 +0800 Subject: [PATCH 1/2] [staking] Add endorsement info in bucket fetch apis (#4207) --- .../protocol/staking/candidate_statereader.go | 8 ++-- action/protocol/staking/protocol.go | 2 +- action/protocol/staking/read_state.go | 16 ++++++- .../protocol/staking/staking_statereader.go | 8 ++-- .../staking/staking_statereader_test.go | 43 +++++++++++++++++++ go.mod | 2 +- go.sum | 6 ++- 7 files changed, 72 insertions(+), 13 deletions(-) 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= From 9496ed530c261730625a87200265a425a5c6b91c Mon Sep 17 00:00:00 2001 From: hunshenshi <289517357@qq.com> Date: Thu, 11 Apr 2024 11:37:07 +0800 Subject: [PATCH 2/2] [ioctl]: update ws project format (#4224) * feat(project): update project format * feat(ioctl): add projectConfig struct in ws --- ioctl/cmd/ws/wsprojectconfig.go | 55 ++++++++++++++++++++++++++------- 1 file changed, 44 insertions(+), 11 deletions(-) diff --git a/ioctl/cmd/ws/wsprojectconfig.go b/ioctl/cmd/ws/wsprojectconfig.go index f50d94ccbe..f67cb81b0e 100644 --- a/ioctl/cmd/ws/wsprojectconfig.go +++ b/ioctl/cmd/ws/wsprojectconfig.go @@ -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") @@ -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) } @@ -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的版本号", @@ -89,6 +105,8 @@ 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)) @@ -96,12 +114,20 @@ func init() { 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 @@ -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" @@ -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") }