diff --git a/action/protocol/staking/ethabi/common/candidatebyaddress.go b/action/protocol/staking/ethabi/common/candidatebyaddress.go index ae78e3f1a7..5109b5dffb 100644 --- a/action/protocol/staking/ethabi/common/candidatebyaddress.go +++ b/action/protocol/staking/ethabi/common/candidatebyaddress.go @@ -2,6 +2,7 @@ package common import ( "encoding/hex" + "math" "github.com/ethereum/go-ethereum/accounts/abi" "github.com/ethereum/go-ethereum/common" @@ -16,9 +17,10 @@ import ( // CandidateByAddressStateContext context for candidateByAddress type CandidateByAddressStateContext struct { *protocol.BaseStateContext + cfg candidateConfig } -func NewCandidateByAddressStateContext(data []byte, methodABI *abi.Method, apiMethod iotexapi.ReadStakingDataMethod_Name) (*CandidateByAddressStateContext, error) { +func NewCandidateByAddressStateContext(data []byte, methodABI *abi.Method, apiMethod iotexapi.ReadStakingDataMethod_Name, opts ...OptionCandidate) (*CandidateByAddressStateContext, error) { paramsMap := map[string]interface{}{} ok := false if err := methodABI.Inputs.UnpackIntoMap(paramsMap, data); err != nil { @@ -51,6 +53,10 @@ func NewCandidateByAddressStateContext(data []byte, methodABI *abi.Method, apiMe if err != nil { return nil, err } + cfg := &candidateConfig{} + for _, opt := range opts { + opt(cfg) + } return &CandidateByAddressStateContext{ &protocol.BaseStateContext{ Parameter: &protocol.Parameters{ @@ -59,6 +65,7 @@ func NewCandidateByAddressStateContext(data []byte, methodABI *abi.Method, apiMe }, Method: methodABI, }, + *cfg, }, nil } @@ -68,7 +75,9 @@ func (r *CandidateByAddressStateContext) EncodeToEth(resp *iotexapi.ReadStateRes if err := proto.Unmarshal(resp.Data, &result); err != nil { return "", err } - + if r.cfg.noSelfStakeBucketIndexAsMaxUint32 && result.SelfStakeBucketIdx == math.MaxUint64 { + result.SelfStakeBucketIdx = math.MaxUint32 + } cand, err := EncodeCandidateToEth(&result) if err != nil { return "", err diff --git a/action/protocol/staking/ethabi/common/candidatebyname.go b/action/protocol/staking/ethabi/common/candidatebyname.go index 8dbb0638b8..0045034807 100644 --- a/action/protocol/staking/ethabi/common/candidatebyname.go +++ b/action/protocol/staking/ethabi/common/candidatebyname.go @@ -2,6 +2,7 @@ package common import ( "encoding/hex" + "math" "github.com/ethereum/go-ethereum/accounts/abi" "github.com/iotexproject/iotex-proto/golang/iotexapi" @@ -14,9 +15,10 @@ import ( // CandidateByNameStateContext context for CandidateByName type CandidateByNameStateContext struct { *protocol.BaseStateContext + cfg candidateConfig } -func NewCandidateByNameStateContext(data []byte, methodABI *abi.Method, apiMethod iotexapi.ReadStakingDataMethod_Name) (*CandidateByNameStateContext, error) { +func NewCandidateByNameStateContext(data []byte, methodABI *abi.Method, apiMethod iotexapi.ReadStakingDataMethod_Name, opts ...OptionCandidate) (*CandidateByNameStateContext, error) { paramsMap := map[string]interface{}{} ok := false if err := methodABI.Inputs.UnpackIntoMap(paramsMap, data); err != nil { @@ -45,6 +47,10 @@ func NewCandidateByNameStateContext(data []byte, methodABI *abi.Method, apiMetho if err != nil { return nil, err } + cfg := &candidateConfig{} + for _, opt := range opts { + opt(cfg) + } return &CandidateByNameStateContext{ &protocol.BaseStateContext{ Parameter: &protocol.Parameters{ @@ -53,6 +59,7 @@ func NewCandidateByNameStateContext(data []byte, methodABI *abi.Method, apiMetho }, Method: methodABI, }, + *cfg, }, nil } @@ -62,7 +69,9 @@ func (r *CandidateByNameStateContext) EncodeToEth(resp *iotexapi.ReadStateRespon if err := proto.Unmarshal(resp.Data, &result); err != nil { return "", err } - + if r.cfg.noSelfStakeBucketIndexAsMaxUint32 && result.SelfStakeBucketIdx == math.MaxUint64 { + result.SelfStakeBucketIdx = math.MaxUint32 + } cand, err := EncodeCandidateToEth(&result) if err != nil { return "", err diff --git a/action/protocol/staking/ethabi/common/candidates.go b/action/protocol/staking/ethabi/common/candidates.go index 8c432bc793..8944c60433 100644 --- a/action/protocol/staking/ethabi/common/candidates.go +++ b/action/protocol/staking/ethabi/common/candidates.go @@ -2,6 +2,7 @@ package common import ( "encoding/hex" + "math" "github.com/ethereum/go-ethereum/accounts/abi" "github.com/iotexproject/iotex-proto/golang/iotexapi" @@ -11,12 +12,28 @@ import ( "github.com/iotexproject/iotex-core/action/protocol" ) -// CandidatesStateContext context for Candidates -type CandidatesStateContext struct { - *protocol.BaseStateContext +type ( + // CandidatesStateContext context for Candidates + CandidatesStateContext struct { + *protocol.BaseStateContext + cfg candidateConfig + } + // OptionCandidate option for candidate + OptionCandidate func(*candidateConfig) + + candidateConfig struct { + noSelfStakeBucketIndexAsMaxUint32 bool + } +) + +// WithNoSelfStakeBucketIndexAsMaxUint32 set noSelfStakeBucketIndex as MaxUint32 +func WithNoSelfStakeBucketIndexAsMaxUint32() OptionCandidate { + return func(c *candidateConfig) { + c.noSelfStakeBucketIndexAsMaxUint32 = true + } } -func NewCandidatesStateContext(data []byte, methodABI *abi.Method, apiMethod iotexapi.ReadStakingDataMethod_Name) (*CandidatesStateContext, error) { +func NewCandidatesStateContext(data []byte, methodABI *abi.Method, apiMethod iotexapi.ReadStakingDataMethod_Name, opts ...OptionCandidate) (*CandidatesStateContext, error) { paramsMap := map[string]interface{}{} ok := false if err := methodABI.Inputs.UnpackIntoMap(paramsMap, data); err != nil { @@ -51,6 +68,10 @@ func NewCandidatesStateContext(data []byte, methodABI *abi.Method, apiMethod iot if err != nil { return nil, err } + cfg := &candidateConfig{} + for _, opt := range opts { + opt(cfg) + } return &CandidatesStateContext{ &protocol.BaseStateContext{ Parameter: &protocol.Parameters{ @@ -59,6 +80,7 @@ func NewCandidatesStateContext(data []byte, methodABI *abi.Method, apiMethod iot }, Method: methodABI, }, + *cfg, }, nil } @@ -71,6 +93,9 @@ func (r *CandidatesStateContext) EncodeToEth(resp *iotexapi.ReadStateResponse) ( args := make([]CandidateEth, len(result.Candidates)) for i, candidate := range result.Candidates { + if r.cfg.noSelfStakeBucketIndexAsMaxUint32 && candidate.SelfStakeBucketIdx == math.MaxUint64 { + candidate.SelfStakeBucketIdx = math.MaxUint32 + } cand, err := EncodeCandidateToEth(candidate) if err != nil { return "", err diff --git a/action/protocol/staking/ethabi/v1/stake_candidatebyaddress.go b/action/protocol/staking/ethabi/v1/stake_candidatebyaddress.go index 940b194b17..624a5a6e1d 100644 --- a/action/protocol/staking/ethabi/v1/stake_candidatebyaddress.go +++ b/action/protocol/staking/ethabi/v1/stake_candidatebyaddress.go @@ -74,6 +74,6 @@ func init() { } func newCandidateByAddressStateContext(data []byte) (*stakingComm.CandidateByAddressStateContext, error) { - return stakingComm.NewCandidateByAddressStateContext(data, &_candidateByAddressMethod, iotexapi.ReadStakingDataMethod_CANDIDATE_BY_ADDRESS) + return stakingComm.NewCandidateByAddressStateContext(data, &_candidateByAddressMethod, iotexapi.ReadStakingDataMethod_CANDIDATE_BY_ADDRESS, stakingComm.WithNoSelfStakeBucketIndexAsMaxUint32()) } diff --git a/action/protocol/staking/ethabi/v1/stake_candidatebyname.go b/action/protocol/staking/ethabi/v1/stake_candidatebyname.go index 7d50bb082d..e629793e6c 100644 --- a/action/protocol/staking/ethabi/v1/stake_candidatebyname.go +++ b/action/protocol/staking/ethabi/v1/stake_candidatebyname.go @@ -74,5 +74,5 @@ func init() { } func newCandidateByNameStateContext(data []byte) (*stakingComm.CandidateByNameStateContext, error) { - return stakingComm.NewCandidateByNameStateContext(data, &_candidateByNameMethod, iotexapi.ReadStakingDataMethod_CANDIDATE_BY_NAME) + return stakingComm.NewCandidateByNameStateContext(data, &_candidateByNameMethod, iotexapi.ReadStakingDataMethod_CANDIDATE_BY_NAME, stakingComm.WithNoSelfStakeBucketIndexAsMaxUint32()) } diff --git a/action/protocol/staking/ethabi/v1/stake_candidates.go b/action/protocol/staking/ethabi/v1/stake_candidates.go index d6f5d478e0..52a30b257a 100644 --- a/action/protocol/staking/ethabi/v1/stake_candidates.go +++ b/action/protocol/staking/ethabi/v1/stake_candidates.go @@ -79,5 +79,5 @@ func init() { } func newCandidatesStateContext(data []byte) (*stakingComm.CandidatesStateContext, error) { - return stakingComm.NewCandidatesStateContext(data, &_candidatesMethod, iotexapi.ReadStakingDataMethod_CANDIDATES) + return stakingComm.NewCandidatesStateContext(data, &_candidatesMethod, iotexapi.ReadStakingDataMethod_CANDIDATES, stakingComm.WithNoSelfStakeBucketIndexAsMaxUint32()) }