Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[staking] refactor to split staking abi into separate version #4272

Merged
merged 4 commits into from
Jun 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 72 additions & 0 deletions action/protocol/staking/ethabi/common/buckets.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package common

import (
"github.com/ethereum/go-ethereum/accounts/abi"
"google.golang.org/protobuf/proto"

"github.com/iotexproject/iotex-proto/golang/iotexapi"
"github.com/iotexproject/iotex-proto/golang/iotextypes"

"github.com/iotexproject/iotex-core/action/protocol"
)

// BucketsStateContext context for Buckets
type BucketsStateContext struct {
*protocol.BaseStateContext
}

func NewBucketsStateContext(data []byte, methodABI *abi.Method, apiMethod iotexapi.ReadStakingDataMethod_Name) (*BucketsStateContext, error) {
paramsMap := map[string]interface{}{}
ok := false
if err := methodABI.Inputs.UnpackIntoMap(paramsMap, data); err != nil {
return nil, err
}
var offset, limit uint32
if offset, ok = paramsMap["offset"].(uint32); !ok {
return nil, ErrDecodeFailure
}
if limit, ok = paramsMap["limit"].(uint32); !ok {
return nil, ErrDecodeFailure
}

method := &iotexapi.ReadStakingDataMethod{
Method: apiMethod,
}
methodBytes, err := proto.Marshal(method)
if err != nil {
return nil, err
}
arguments := &iotexapi.ReadStakingDataRequest{
Request: &iotexapi.ReadStakingDataRequest_Buckets{
Buckets: &iotexapi.ReadStakingDataRequest_VoteBuckets{
Pagination: &iotexapi.PaginationParam{
Offset: offset,
Limit: limit,
},
},
},
}
argumentsBytes, err := proto.Marshal(arguments)
if err != nil {
return nil, err
}
return &BucketsStateContext{
&protocol.BaseStateContext{
Parameter: &protocol.Parameters{
MethodName: methodBytes,
Arguments: [][]byte{argumentsBytes},
},
Method: methodABI,
},
}, nil
}

// EncodeToEth encode proto to eth
func (r *BucketsStateContext) EncodeToEth(resp *iotexapi.ReadStateResponse) (string, error) {
var result iotextypes.VoteBucketList
if err := proto.Unmarshal(resp.Data, &result); err != nil {
return "", err
}

return EncodeVoteBucketListToEth(r.Method.Outputs, &result)
}
76 changes: 76 additions & 0 deletions action/protocol/staking/ethabi/common/bucketsbycandidate.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package common

import (
"github.com/ethereum/go-ethereum/accounts/abi"
"github.com/iotexproject/iotex-proto/golang/iotexapi"
"github.com/iotexproject/iotex-proto/golang/iotextypes"
"google.golang.org/protobuf/proto"

"github.com/iotexproject/iotex-core/action/protocol"
)

// BucketsByCandidateStateContext context for BucketsByCandidate
type BucketsByCandidateStateContext struct {
*protocol.BaseStateContext
}

func NewBucketsByCandidateStateContext(data []byte, methodABI *abi.Method, apiMethod iotexapi.ReadStakingDataMethod_Name) (*BucketsByCandidateStateContext, error) {
paramsMap := map[string]interface{}{}
ok := false
if err := methodABI.Inputs.UnpackIntoMap(paramsMap, data); err != nil {
return nil, err
}
var candName string
if candName, ok = paramsMap["candName"].(string); !ok {
return nil, ErrDecodeFailure
}
var offset, limit uint32
if offset, ok = paramsMap["offset"].(uint32); !ok {
return nil, ErrDecodeFailure
}
if limit, ok = paramsMap["limit"].(uint32); !ok {
return nil, ErrDecodeFailure
}

method := &iotexapi.ReadStakingDataMethod{
Method: apiMethod,
}
methodBytes, err := proto.Marshal(method)
if err != nil {
return nil, err
}
arguments := &iotexapi.ReadStakingDataRequest{
Request: &iotexapi.ReadStakingDataRequest_BucketsByCandidate{
BucketsByCandidate: &iotexapi.ReadStakingDataRequest_VoteBucketsByCandidate{
CandName: candName,
Pagination: &iotexapi.PaginationParam{
Offset: offset,
Limit: limit,
},
},
},
}
argumentsBytes, err := proto.Marshal(arguments)
if err != nil {
return nil, err
}
return &BucketsByCandidateStateContext{
&protocol.BaseStateContext{
Parameter: &protocol.Parameters{
MethodName: methodBytes,
Arguments: [][]byte{argumentsBytes},
},
Method: methodABI,
},
}, nil
}

// EncodeToEth encode proto to eth
func (r *BucketsByCandidateStateContext) EncodeToEth(resp *iotexapi.ReadStateResponse) (string, error) {
var result iotextypes.VoteBucketList
if err := proto.Unmarshal(resp.Data, &result); err != nil {
return "", err
}

return EncodeVoteBucketListToEth(r.Method.Outputs, &result)
envestcc marked this conversation as resolved.
Show resolved Hide resolved
}
65 changes: 65 additions & 0 deletions action/protocol/staking/ethabi/common/bucketsbyindexes.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package common

import (
"github.com/ethereum/go-ethereum/accounts/abi"
"github.com/iotexproject/iotex-proto/golang/iotexapi"
"github.com/iotexproject/iotex-proto/golang/iotextypes"
"google.golang.org/protobuf/proto"

"github.com/iotexproject/iotex-core/action/protocol"
)

// BucketsByIndexesStateContext context for BucketsByIndexes
type BucketsByIndexesStateContext struct {
*protocol.BaseStateContext
}

func NewBucketsByIndexesStateContext(data []byte, methodABI *abi.Method, apiMethod iotexapi.ReadStakingDataMethod_Name) (*BucketsByIndexesStateContext, error) {
paramsMap := map[string]interface{}{}
ok := false
if err := methodABI.Inputs.UnpackIntoMap(paramsMap, data); err != nil {
return nil, err
}
var index []uint64
if index, ok = paramsMap["indexes"].([]uint64); !ok {
return nil, ErrDecodeFailure
}

method := &iotexapi.ReadStakingDataMethod{
Method: apiMethod,
}
methodBytes, err := proto.Marshal(method)
if err != nil {
return nil, err
}
arguments := &iotexapi.ReadStakingDataRequest{
Request: &iotexapi.ReadStakingDataRequest_BucketsByIndexes{
BucketsByIndexes: &iotexapi.ReadStakingDataRequest_VoteBucketsByIndexes{
Index: index,
},
},
}
argumentsBytes, err := proto.Marshal(arguments)
if err != nil {
return nil, err
}
return &BucketsByIndexesStateContext{
&protocol.BaseStateContext{
Parameter: &protocol.Parameters{
MethodName: methodBytes,
Arguments: [][]byte{argumentsBytes},
},
Method: methodABI,
},
}, nil
}

// EncodeToEth encode proto to eth
func (r *BucketsByIndexesStateContext) EncodeToEth(resp *iotexapi.ReadStateResponse) (string, error) {
var result iotextypes.VoteBucketList
if err := proto.Unmarshal(resp.Data, &result); err != nil {
return "", err
}

return EncodeVoteBucketListToEth(r.Method.Outputs, &result)
}
82 changes: 82 additions & 0 deletions action/protocol/staking/ethabi/common/bucketsbyvoter.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package common

import (
"github.com/ethereum/go-ethereum/accounts/abi"
"github.com/ethereum/go-ethereum/common"
"github.com/iotexproject/iotex-address/address"
"github.com/iotexproject/iotex-proto/golang/iotexapi"
"github.com/iotexproject/iotex-proto/golang/iotextypes"
"google.golang.org/protobuf/proto"

"github.com/iotexproject/iotex-core/action/protocol"
)

// BucketsByVoterStateContext context for BucketsByVoter
type BucketsByVoterStateContext struct {
*protocol.BaseStateContext
}

func NewBucketsByVoterStateContext(data []byte, methodABI *abi.Method, apiMethod iotexapi.ReadStakingDataMethod_Name) (*BucketsByVoterStateContext, error) {
paramsMap := map[string]interface{}{}
ok := false
if err := methodABI.Inputs.UnpackIntoMap(paramsMap, data); err != nil {
return nil, err
}
var voter common.Address
if voter, ok = paramsMap["voter"].(common.Address); !ok {
return nil, ErrDecodeFailure
}
voterAddress, err := address.FromBytes(voter[:])
if err != nil {
return nil, err
}
var offset, limit uint32
if offset, ok = paramsMap["offset"].(uint32); !ok {
return nil, ErrDecodeFailure
}
if limit, ok = paramsMap["limit"].(uint32); !ok {
return nil, ErrDecodeFailure
}

method := &iotexapi.ReadStakingDataMethod{
Method: apiMethod,
}
methodBytes, err := proto.Marshal(method)
if err != nil {
return nil, err
}
arguments := &iotexapi.ReadStakingDataRequest{
Request: &iotexapi.ReadStakingDataRequest_BucketsByVoter{
BucketsByVoter: &iotexapi.ReadStakingDataRequest_VoteBucketsByVoter{
VoterAddress: voterAddress.String(),
Pagination: &iotexapi.PaginationParam{
Offset: offset,
Limit: limit,
},
},
},
}
argumentsBytes, err := proto.Marshal(arguments)
if err != nil {
return nil, err
}
return &BucketsByVoterStateContext{
&protocol.BaseStateContext{
Parameter: &protocol.Parameters{
MethodName: methodBytes,
Arguments: [][]byte{argumentsBytes},
},
Method: methodABI,
},
}, nil
}

// EncodeToEth encode proto to eth
func (r *BucketsByVoterStateContext) EncodeToEth(resp *iotexapi.ReadStateResponse) (string, error) {
var result iotextypes.VoteBucketList
if err := proto.Unmarshal(resp.Data, &result); err != nil {
return "", err
}

return EncodeVoteBucketListToEth(r.Method.Outputs, &result)
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package ethabi
package common

import (
"encoding/hex"
Expand All @@ -9,44 +9,16 @@ import (
"google.golang.org/protobuf/proto"

"github.com/iotexproject/iotex-core/action/protocol"
"github.com/iotexproject/iotex-core/action/protocol/abiutil"
)

var _bucketsCountInterfaceABI = `[
{
"inputs": [],
"name": "bucketsCount",
"outputs": [
{
"internalType": "uint64",
"name": "total",
"type": "uint64"
},
{
"internalType": "uint64",
"name": "active",
"type": "uint64"
}
],
"stateMutability": "view",
"type": "function"
}
]`

var _bucketsCountMethod abi.Method

func init() {
_bucketsCountMethod = abiutil.MustLoadMethod(_bucketsCountInterfaceABI, "bucketsCount")
}

// BucketsCountStateContext context for BucketsCount
type BucketsCountStateContext struct {
*protocol.BaseStateContext
}

func newBucketsCountStateContext() (*BucketsCountStateContext, error) {
func NewBucketsCountStateContext(data []byte, methodABI *abi.Method, apiMethod iotexapi.ReadStakingDataMethod_Name) (*BucketsCountStateContext, error) {
method := &iotexapi.ReadStakingDataMethod{
Method: iotexapi.ReadStakingDataMethod_BUCKETS_COUNT,
Method: apiMethod,
}
methodBytes, err := proto.Marshal(method)
if err != nil {
Expand All @@ -67,6 +39,7 @@ func newBucketsCountStateContext() (*BucketsCountStateContext, error) {
MethodName: methodBytes,
Arguments: [][]byte{argumentsBytes},
},
Method: methodABI,
},
}, nil
}
Expand All @@ -78,7 +51,7 @@ func (r *BucketsCountStateContext) EncodeToEth(resp *iotexapi.ReadStateResponse)
return "", err
}

data, err := _bucketsCountMethod.Outputs.Pack(result.Total, result.Active)
data, err := r.Method.Outputs.Pack(result.Total, result.Active)
if err != nil {
return "", err
}
Expand Down
Loading
Loading