Skip to content

Commit

Permalink
candidates v1 compatability
Browse files Browse the repository at this point in the history
  • Loading branch information
envestcc committed Jul 16, 2024
1 parent a1c97bf commit a31275b
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 11 deletions.
13 changes: 11 additions & 2 deletions action/protocol/staking/ethabi/common/candidatebyaddress.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package common

import (
"encoding/hex"
"math"

"github.com/ethereum/go-ethereum/accounts/abi"
"github.com/ethereum/go-ethereum/common"
Expand All @@ -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 {
Expand Down Expand Up @@ -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{
Expand All @@ -59,6 +65,7 @@ func NewCandidateByAddressStateContext(data []byte, methodABI *abi.Method, apiMe
},
Method: methodABI,
},
*cfg,
}, nil
}

Expand All @@ -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
Expand Down
13 changes: 11 additions & 2 deletions action/protocol/staking/ethabi/common/candidatebyname.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package common

import (
"encoding/hex"
"math"

"github.com/ethereum/go-ethereum/accounts/abi"
"github.com/iotexproject/iotex-proto/golang/iotexapi"
Expand All @@ -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 {
Expand Down Expand Up @@ -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{
Expand All @@ -53,6 +59,7 @@ func NewCandidateByNameStateContext(data []byte, methodABI *abi.Method, apiMetho
},
Method: methodABI,
},
*cfg,
}, nil
}

Expand All @@ -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
Expand Down
33 changes: 29 additions & 4 deletions action/protocol/staking/ethabi/common/candidates.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package common

import (
"encoding/hex"
"math"

"github.com/ethereum/go-ethereum/accounts/abi"
"github.com/iotexproject/iotex-proto/golang/iotexapi"
Expand All @@ -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 {
Expand Down Expand Up @@ -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{
Expand All @@ -59,6 +80,7 @@ func NewCandidatesStateContext(data []byte, methodABI *abi.Method, apiMethod iot
},
Method: methodABI,
},
*cfg,
}, nil
}

Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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())

}
2 changes: 1 addition & 1 deletion action/protocol/staking/ethabi/v1/stake_candidatebyname.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}
2 changes: 1 addition & 1 deletion action/protocol/staking/ethabi/v1/stake_candidates.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}

0 comments on commit a31275b

Please sign in to comment.