Skip to content
Open
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
10 changes: 5 additions & 5 deletions action/candidate_register.go
Original file line number Diff line number Diff line change
Expand Up @@ -229,8 +229,8 @@ func (cr *CandidateRegister) Proto() *iotextypes.CandidateRegister {

switch {
case cr.WithBLS():
act.Candidate.PubKey = make([]byte, len(cr.pubKey))
copy(act.Candidate.PubKey, cr.pubKey)
act.Candidate.BlsPubKey = make([]byte, len(cr.pubKey))
copy(act.Candidate.BlsPubKey, cr.pubKey)
if cr.value != nil {
act.StakedAmount = cr.value.String()
}
Expand Down Expand Up @@ -266,10 +266,10 @@ func (cr *CandidateRegister) LoadProto(pbAct *iotextypes.CandidateRegister) erro
cr.duration = pbAct.GetStakedDuration()
cr.autoStake = pbAct.GetAutoStake()

withBLS := len(pbAct.Candidate.GetPubKey()) > 0
withBLS := len(pbAct.Candidate.GetBlsPubKey()) > 0
if withBLS {
cr.pubKey = make([]byte, len(pbAct.Candidate.GetPubKey()))
copy(cr.pubKey, pbAct.Candidate.GetPubKey())
cr.pubKey = make([]byte, len(pbAct.Candidate.GetBlsPubKey()))
copy(cr.pubKey, pbAct.Candidate.GetBlsPubKey())
}
if len(pbAct.GetStakedAmount()) > 0 {
amount, ok := new(big.Int).SetString(pbAct.GetStakedAmount(), 10)
Expand Down
10 changes: 5 additions & 5 deletions action/candidate_update.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,8 +156,8 @@ func (cu *CandidateUpdate) Proto() *iotextypes.CandidateBasicInfo {
}

if len(cu.pubKey) > 0 {
act.PubKey = make([]byte, len(cu.pubKey))
copy(act.PubKey, cu.pubKey)
act.BlsPubKey = make([]byte, len(cu.pubKey))
copy(act.BlsPubKey, cu.pubKey)
}
return act
}
Expand Down Expand Up @@ -185,9 +185,9 @@ func (cu *CandidateUpdate) LoadProto(pbAct *iotextypes.CandidateBasicInfo) error
}
cu.rewardAddress = rewardAddr
}
if len(pbAct.GetPubKey()) > 0 {
cu.pubKey = make([]byte, len(pbAct.GetPubKey()))
copy(cu.pubKey, pbAct.GetPubKey())
if len(pbAct.GetBlsPubKey()) > 0 {
cu.pubKey = make([]byte, len(pbAct.GetBlsPubKey()))
copy(cu.pubKey, pbAct.GetBlsPubKey())
}
return nil
}
Expand Down
5 changes: 5 additions & 0 deletions action/protocol/staking/ethabi/common/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ type (
TotalWeightedVotes *big.Int
SelfStakeBucketIdx uint64
SelfStakingTokens *big.Int
BlsPubKey []byte
}
)

Expand Down Expand Up @@ -138,6 +139,10 @@ func EncodeCandidateToEth(candidate *iotextypes.CandidateV2) (*CandidateEth, err
return nil, err
}
result.Id = addr
if len(candidate.BlsPubKey) > 0 {
result.BlsPubKey = make([]byte, len(candidate.BlsPubKey))
copy(result.BlsPubKey, candidate.BlsPubKey)
}
return result, nil
}

Expand Down
5 changes: 5 additions & 0 deletions action/protocol/staking/ethabi/stake_base.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
v1 "github.com/iotexproject/iotex-core/v2/action/protocol/staking/ethabi/v1"
v2 "github.com/iotexproject/iotex-core/v2/action/protocol/staking/ethabi/v2"
v3 "github.com/iotexproject/iotex-core/v2/action/protocol/staking/ethabi/v3"
v4 "github.com/iotexproject/iotex-core/v2/action/protocol/staking/ethabi/v4"
)

// BuildReadStateRequest decode eth_call data to StateContext
Expand All @@ -26,5 +27,9 @@ func BuildReadStateRequest(data []byte) (protocol.StateContext, error) {
if err == nil {
return methodSig, nil
}
methodSig, err = v4.BuildReadStateRequest(data)
if err == nil {
return methodSig, nil
}
return nil, stakingComm.ErrInvalidCallSig
}
23 changes: 23 additions & 0 deletions action/protocol/staking/ethabi/v4/builder.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package v4

import (
"encoding/hex"

"github.com/iotexproject/iotex-core/v2/action/protocol"
stakingComm "github.com/iotexproject/iotex-core/v2/action/protocol/staking/ethabi/common"
)

func BuildReadStateRequest(data []byte) (protocol.StateContext, error) {
switch methodSig := hex.EncodeToString(data[:4]); methodSig {
case hex.EncodeToString(_candidatesMethod.ID):
return newCandidatesStateContext(data[4:])
case hex.EncodeToString(_candidateByNameMethod.ID):
return newCandidateByNameStateContext(data[4:])
case hex.EncodeToString(_candidateByAddressMethod.ID):
return newCandidateByAddressStateContext(data[4:])
case hex.EncodeToString(_candidateByIDMethod.ID):
return newCandidateByIDStateContext(data[4:])
default:
return nil, stakingComm.ErrInvalidCallSig
}
}
88 changes: 88 additions & 0 deletions action/protocol/staking/ethabi/v4/stake_candidatebyaddress.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
package v4

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

"github.com/iotexproject/iotex-core/v2/action/protocol/abiutil"
stakingComm "github.com/iotexproject/iotex-core/v2/action/protocol/staking/ethabi/common"
)

const _candidateByAddressInterfaceABI = `[
{
"inputs": [
{
"internalType": "address",
"name": "ownerAddress",
"type": "address"
}
],
"name": "candidateByAddressV4",
"outputs": [
{
"components": [
{
"internalType": "address",
"name": "ownerAddress",
"type": "address"
},
{
"internalType": "address",
"name": "operatorAddress",
"type": "address"
},
{
"internalType": "address",
"name": "rewardAddress",
"type": "address"
},
{
"internalType": "string",
"name": "name",
"type": "string"
},
{
"internalType": "uint256",
"name": "totalWeightedVotes",
"type": "uint256"
},
{
"internalType": "uint64",
"name": "selfStakeBucketIdx",
"type": "uint64"
},
{
"internalType": "uint256",
"name": "selfStakingTokens",
"type": "uint256"
},
{
"internalType": "address",
"name": "id",
"type": "address"
},
{
"internalType": "bytes",
"name": "blsPubKey",
"type": "bytes"
}
],
"internalType": "struct IStaking.CandidateV4",
"name": "",
"type": "tuple"
}
],
"stateMutability": "view",
"type": "function"
}
]`

var _candidateByAddressMethod abi.Method

func init() {
_candidateByAddressMethod = abiutil.MustLoadMethod(_candidateByAddressInterfaceABI, "candidateByAddressV4")
}

func newCandidateByAddressStateContext(data []byte) (*stakingComm.CandidateByAddressStateContext, error) {
return stakingComm.NewCandidateByAddressStateContext(data, &_candidateByAddressMethod, iotexapi.ReadStakingDataMethod_CANDIDATE_BY_ADDRESS)
}
136 changes: 136 additions & 0 deletions action/protocol/staking/ethabi/v4/stake_candidatebyid.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
package v4

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"
"google.golang.org/protobuf/proto"

"github.com/iotexproject/iotex-core/v2/action/protocol"
"github.com/iotexproject/iotex-core/v2/action/protocol/abiutil"
stakingComm "github.com/iotexproject/iotex-core/v2/action/protocol/staking/ethabi/common"
)

const _candidateByIDInterfaceABI = `[
{
"inputs": [
{
"internalType": "address",
"name": "id",
"type": "address"
}
],
"name": "candidateByIDV4",
"outputs": [
{
"components": [
{
"internalType": "address",
"name": "ownerAddress",
"type": "address"
},
{
"internalType": "address",
"name": "operatorAddress",
"type": "address"
},
{
"internalType": "address",
"name": "rewardAddress",
"type": "address"
},
{
"internalType": "string",
"name": "name",
"type": "string"
},
{
"internalType": "uint256",
"name": "totalWeightedVotes",
"type": "uint256"
},
{
"internalType": "uint64",
"name": "selfStakeBucketIdx",
"type": "uint64"
},
{
"internalType": "uint256",
"name": "selfStakingTokens",
"type": "uint256"
},
{
"internalType": "address",
"name": "id",
"type": "address"
},
{
"internalType": "bytes",
"name": "blsPubKey",
"type": "bytes"
}
],
"internalType": "struct IStaking.CandidateV4",
"name": "",
"type": "tuple"
}
],
"stateMutability": "view",
"type": "function"
}
]`

var _candidateByIDMethod abi.Method

func init() {
_candidateByIDMethod = abiutil.MustLoadMethod(_candidateByIDInterfaceABI, "candidateByIDV4")
}

func newCandidateByIDStateContext(data []byte) (*stakingComm.CandidateByAddressStateContext, error) {
return NewCandidateByIDStateContext(data, &_candidateByIDMethod, iotexapi.ReadStakingDataMethod_CANDIDATE_BY_ADDRESS)
}

func NewCandidateByIDStateContext(data []byte, methodABI *abi.Method, apiMethod iotexapi.ReadStakingDataMethod_Name) (*stakingComm.CandidateByAddressStateContext, error) {
paramsMap := map[string]interface{}{}
ok := false
if err := methodABI.Inputs.UnpackIntoMap(paramsMap, data); err != nil {
return nil, err
}
var idAddress common.Address
if idAddress, ok = paramsMap["id"].(common.Address); !ok {
return nil, stakingComm.ErrDecodeFailure
}
id, err := address.FromBytes(idAddress[:])
if err != nil {
return nil, err
}

method := &iotexapi.ReadStakingDataMethod{
Method: apiMethod,
}
methodBytes, err := proto.Marshal(method)
if err != nil {
return nil, err
}
arguments := &iotexapi.ReadStakingDataRequest{
Request: &iotexapi.ReadStakingDataRequest_CandidateByAddress_{
CandidateByAddress: &iotexapi.ReadStakingDataRequest_CandidateByAddress{
Id: id.String(),
},
},
}
argumentsBytes, err := proto.Marshal(arguments)
if err != nil {
return nil, err
}
return &stakingComm.CandidateByAddressStateContext{
BaseStateContext: &protocol.BaseStateContext{
Parameter: &protocol.Parameters{
MethodName: methodBytes,
Arguments: [][]byte{argumentsBytes},
},
Method: methodABI,
},
}, nil
}
Loading