Skip to content

Commit

Permalink
refactor action (#4409)
Browse files Browse the repository at this point in the history
  • Loading branch information
envestcc authored Sep 30, 2024
1 parent 3c2aa0d commit 2c77273
Show file tree
Hide file tree
Showing 26 changed files with 162 additions and 56 deletions.
2 changes: 2 additions & 0 deletions action/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (

"github.com/ethereum/go-ethereum/common"
"github.com/iotexproject/go-pkgs/crypto"
"github.com/iotexproject/iotex-proto/golang/iotextypes"
"github.com/pkg/errors"
)

Expand Down Expand Up @@ -42,6 +43,7 @@ type (
actionPayload interface {
IntrinsicGas() (uint64, error)
SanityCheck() error
FillAction(*iotextypes.ActionCore)
}

hasDestination interface{ Destination() string }
Expand Down
8 changes: 8 additions & 0 deletions action/blob_data.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,14 @@ type BlobTxData struct {
sidecar *types.BlobTxSidecar
}

func NewBlobTxData(blobFeeCap *uint256.Int, blobHashes []common.Hash, sidecar *types.BlobTxSidecar) *BlobTxData {
return &BlobTxData{
blobFeeCap: blobFeeCap,
blobHashes: blobHashes,
sidecar: sidecar,
}
}

func (tx *BlobTxData) gasFeeCap() *uint256.Int {
p := uint256.Int{}
if tx.blobFeeCap != nil {
Expand Down
4 changes: 4 additions & 0 deletions action/candidate_activate.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,10 @@ func (cr *CandidateActivate) SanityCheck() error {
return nil
}

func (cr *CandidateActivate) FillAction(act *iotextypes.ActionCore) {
act.Action = &iotextypes.ActionCore_CandidateActivate{CandidateActivate: cr.Proto()}
}

// Proto converts CandidateActivate to protobuf's Action
func (cr *CandidateActivate) Proto() *iotextypes.CandidateActivate {
return &iotextypes.CandidateActivate{
Expand Down
4 changes: 4 additions & 0 deletions action/candidate_endorsement.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,10 @@ func (act *CandidateEndorsement) Op() CandidateEndorsementOp {
return act.op
}

func (act *CandidateEndorsement) FillAction(core *iotextypes.ActionCore) {
core.Action = &iotextypes.ActionCore_CandidateEndorsement{CandidateEndorsement: act.Proto()}
}

// Proto converts CandidateEndorsement to protobuf's Action
func (act *CandidateEndorsement) Proto() *iotextypes.CandidateEndorsement {
return &iotextypes.CandidateEndorsement{
Expand Down
4 changes: 4 additions & 0 deletions action/candidate_register.go
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,10 @@ func (cr *CandidateRegister) Serialize() []byte {
return byteutil.Must(proto.Marshal(cr.Proto()))
}

func (act *CandidateRegister) FillAction(core *iotextypes.ActionCore) {
core.Action = &iotextypes.ActionCore_CandidateRegister{CandidateRegister: act.Proto()}
}

// Proto converts to protobuf CandidateRegister Action
func (cr *CandidateRegister) Proto() *iotextypes.CandidateRegister {
act := iotextypes.CandidateRegister{
Expand Down
4 changes: 4 additions & 0 deletions action/candidate_transfer_ownership.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,10 @@ func (act *CandidateTransferOwnership) Serialize() []byte {
return byteutil.Must(proto.Marshal(act.Proto()))
}

func (act *CandidateTransferOwnership) FillAction(core *iotextypes.ActionCore) {
core.Action = &iotextypes.ActionCore_CandidateTransferOwnership{CandidateTransferOwnership: act.Proto()}
}

// Proto converts to protobuf CandidateTransferOwnership Action
func (act *CandidateTransferOwnership) Proto() *iotextypes.CandidateTransferOwnership {
ac := iotextypes.CandidateTransferOwnership{
Expand Down
7 changes: 6 additions & 1 deletion action/candidate_update.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@ import (
"google.golang.org/protobuf/proto"

"github.com/iotexproject/iotex-address/address"
"github.com/iotexproject/iotex-core/pkg/util/byteutil"
"github.com/iotexproject/iotex-proto/golang/iotextypes"

"github.com/iotexproject/iotex-core/pkg/util/byteutil"
)

const (
Expand Down Expand Up @@ -112,6 +113,10 @@ func (cu *CandidateUpdate) Serialize() []byte {
return byteutil.Must(proto.Marshal(cu.Proto()))
}

func (act *CandidateUpdate) FillAction(core *iotextypes.ActionCore) {
core.Action = &iotextypes.ActionCore_CandidateUpdate{CandidateUpdate: act.Proto()}
}

// Proto converts to protobuf CandidateUpdate Action
func (cu *CandidateUpdate) Proto() *iotextypes.CandidateBasicInfo {
act := &iotextypes.CandidateBasicInfo{
Expand Down
8 changes: 6 additions & 2 deletions action/claimreward.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ import (
"strings"

"github.com/ethereum/go-ethereum/accounts/abi"
"github.com/iotexproject/iotex-address/address"
"github.com/iotexproject/iotex-proto/golang/iotextypes"
"github.com/pkg/errors"
"google.golang.org/protobuf/proto"

"github.com/iotexproject/iotex-address/address"
"github.com/iotexproject/iotex-core/pkg/util/byteutil"
"github.com/iotexproject/iotex-proto/golang/iotextypes"
)

const _claimRewardingInterfaceABI = `[
Expand Down Expand Up @@ -125,6 +125,10 @@ func (c *ClaimFromRewardingFund) Serialize() []byte {
return byteutil.Must(proto.Marshal(c.Proto()))
}

func (act *ClaimFromRewardingFund) FillAction(core *iotextypes.ActionCore) {
core.Action = &iotextypes.ActionCore_ClaimFromRewardingFund{ClaimFromRewardingFund: act.Proto()}
}

// Proto converts a claim action struct to a claim action protobuf
func (c *ClaimFromRewardingFund) Proto() *iotextypes.ClaimFromRewardingFund {
var addr string
Expand Down
6 changes: 5 additions & 1 deletion action/depositreward.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ import (
"strings"

"github.com/ethereum/go-ethereum/accounts/abi"
"github.com/iotexproject/iotex-proto/golang/iotextypes"
"github.com/pkg/errors"
"google.golang.org/protobuf/proto"

"github.com/iotexproject/iotex-core/pkg/util/byteutil"
"github.com/iotexproject/iotex-proto/golang/iotextypes"
)

const _depositRewardInterfaceABI = `[
Expand Down Expand Up @@ -87,6 +87,10 @@ func (d *DepositToRewardingFund) Serialize() []byte {
return byteutil.Must(proto.Marshal(d.Proto()))
}

func (act *DepositToRewardingFund) FillAction(core *iotextypes.ActionCore) {
core.Action = &iotextypes.ActionCore_DepositToRewardingFund{DepositToRewardingFund: act.Proto()}
}

// Proto converts a deposit action struct to a deposit action protobuf
func (d *DepositToRewardingFund) Proto() *iotextypes.DepositToRewardingFund {
return &iotextypes.DepositToRewardingFund{
Expand Down
57 changes: 9 additions & 48 deletions action/envelope.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ import (
"github.com/ethereum/go-ethereum/core/types"
"github.com/iotexproject/iotex-proto/golang/iotextypes"
"github.com/pkg/errors"

"github.com/iotexproject/iotex-core/pkg/log"
)

type (
Expand Down Expand Up @@ -87,6 +85,14 @@ type (
}
)

// NewEnvelop creates a new envelope
func NewEnvelop(common TxCommonInternal, payload actionPayload) Envelope {
return &envelope{
common: common,
payload: payload,
}
}

func (elp *envelope) Version() uint32 {
return elp.common.Version()
}
Expand Down Expand Up @@ -247,52 +253,7 @@ func (elp *envelope) ToEthTx(evmNetworkID uint32, encoding iotextypes.Encoding)
// Proto convert Envelope to protobuf format.
func (elp *envelope) Proto() *iotextypes.ActionCore {
actCore := elp.common.toProto()

// TODO assert each action
switch act := elp.Action().(type) {
case *Transfer:
actCore.Action = &iotextypes.ActionCore_Transfer{Transfer: act.Proto()}
case *Execution:
actCore.Action = &iotextypes.ActionCore_Execution{Execution: act.Proto()}
case *GrantReward:
actCore.Action = &iotextypes.ActionCore_GrantReward{GrantReward: act.Proto()}
case *ClaimFromRewardingFund:
actCore.Action = &iotextypes.ActionCore_ClaimFromRewardingFund{ClaimFromRewardingFund: act.Proto()}
case *DepositToRewardingFund:
actCore.Action = &iotextypes.ActionCore_DepositToRewardingFund{DepositToRewardingFund: act.Proto()}
case *PutPollResult:
actCore.Action = &iotextypes.ActionCore_PutPollResult{PutPollResult: act.Proto()}
case *CreateStake:
actCore.Action = &iotextypes.ActionCore_StakeCreate{StakeCreate: act.Proto()}
case *Unstake:
actCore.Action = &iotextypes.ActionCore_StakeUnstake{StakeUnstake: act.Proto()}
case *WithdrawStake:
actCore.Action = &iotextypes.ActionCore_StakeWithdraw{StakeWithdraw: act.Proto()}
case *DepositToStake:
actCore.Action = &iotextypes.ActionCore_StakeAddDeposit{StakeAddDeposit: act.Proto()}
case *Restake:
actCore.Action = &iotextypes.ActionCore_StakeRestake{StakeRestake: act.Proto()}
case *ChangeCandidate:
actCore.Action = &iotextypes.ActionCore_StakeChangeCandidate{StakeChangeCandidate: act.Proto()}
case *TransferStake:
actCore.Action = &iotextypes.ActionCore_StakeTransferOwnership{StakeTransferOwnership: act.Proto()}
case *CandidateRegister:
actCore.Action = &iotextypes.ActionCore_CandidateRegister{CandidateRegister: act.Proto()}
case *CandidateUpdate:
actCore.Action = &iotextypes.ActionCore_CandidateUpdate{CandidateUpdate: act.Proto()}
case *CandidateActivate:
actCore.Action = &iotextypes.ActionCore_CandidateActivate{CandidateActivate: act.Proto()}
case *CandidateEndorsement:
actCore.Action = &iotextypes.ActionCore_CandidateEndorsement{CandidateEndorsement: act.Proto()}
case *CandidateTransferOwnership:
actCore.Action = &iotextypes.ActionCore_CandidateTransferOwnership{CandidateTransferOwnership: act.Proto()}
case *txContainer:
actCore.Action = &iotextypes.ActionCore_TxContainer{TxContainer: act.proto()}
case *MigrateStake:
actCore.Action = &iotextypes.ActionCore_StakeMigrate{StakeMigrate: act.Proto()}
default:
log.S().Panicf("Cannot convert type of action %T.\r\n", act)
}
elp.payload.FillAction(actCore)
return actCore
}

Expand Down
4 changes: 4 additions & 0 deletions action/execution.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,10 @@ func (ex *Execution) Serialize() []byte {
return byteutil.Must(proto.Marshal(ex.Proto()))
}

func (act *Execution) FillAction(core *iotextypes.ActionCore) {
core.Action = &iotextypes.ActionCore_Execution{Execution: act.Proto()}
}

// Proto converts Execution to protobuf's Execution
func (ex *Execution) Proto() *iotextypes.Execution {
act := &iotextypes.Execution{
Expand Down
8 changes: 6 additions & 2 deletions action/grantreward.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ package action
import (
"strings"

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

"github.com/ethereum/go-ethereum/accounts/abi"
"github.com/iotexproject/iotex-core/pkg/util/byteutil"
"github.com/iotexproject/iotex-proto/golang/iotextypes"
)

var (
Expand Down Expand Up @@ -85,6 +85,10 @@ func (g *GrantReward) Serialize() []byte {
return byteutil.Must(proto.Marshal(g.Proto()))
}

func (act *GrantReward) FillAction(core *iotextypes.ActionCore) {
core.Action = &iotextypes.ActionCore_GrantReward{GrantReward: act.Proto()}
}

// Proto converts a grant reward action struct to a grant reward action protobuf
func (g *GrantReward) Proto() *iotextypes.GrantReward {
gProto := iotextypes.GrantReward{
Expand Down
4 changes: 4 additions & 0 deletions action/putpollresult.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ func (r *PutPollResult) LoadProto(putPollResultPb *iotextypes.PutPollResult) err
return r.candidates.LoadProto(putPollResultPb.Candidates)
}

func (act *PutPollResult) FillAction(core *iotextypes.ActionCore) {
core.Action = &iotextypes.ActionCore_PutPollResult{PutPollResult: act.Proto()}
}

// Proto converts put poll result action into a proto message.
func (r *PutPollResult) Proto() *iotextypes.PutPollResult {
return &iotextypes.PutPollResult{
Expand Down
4 changes: 4 additions & 0 deletions action/stake_adddeposit.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,10 @@ func (ds *DepositToStake) Serialize() []byte {
return byteutil.Must(proto.Marshal(ds.Proto()))
}

func (act *DepositToStake) FillAction(core *iotextypes.ActionCore) {
core.Action = &iotextypes.ActionCore_StakeAddDeposit{StakeAddDeposit: act.Proto()}
}

// Proto converts to protobuf DepositToStake Action
func (ds *DepositToStake) Proto() *iotextypes.StakeAddDeposit {
act := &iotextypes.StakeAddDeposit{
Expand Down
4 changes: 4 additions & 0 deletions action/stake_changecandidate.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,10 @@ func (cc *ChangeCandidate) Serialize() []byte {
return byteutil.Must(proto.Marshal(cc.Proto()))
}

func (act *ChangeCandidate) FillAction(core *iotextypes.ActionCore) {
core.Action = &iotextypes.ActionCore_StakeChangeCandidate{StakeChangeCandidate: act.Proto()}
}

// Proto converts change candidate to protobuf
func (cc *ChangeCandidate) Proto() *iotextypes.StakeChangeCandidate {
act := &iotextypes.StakeChangeCandidate{
Expand Down
4 changes: 4 additions & 0 deletions action/stake_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,10 @@ func (cs *CreateStake) Serialize() []byte {
return byteutil.Must(proto.Marshal(cs.Proto()))
}

func (act *CreateStake) FillAction(core *iotextypes.ActionCore) {
core.Action = &iotextypes.ActionCore_StakeCreate{StakeCreate: act.Proto()}
}

// Proto converts to protobuf CreateStake Action
func (cs *CreateStake) Proto() *iotextypes.StakeCreate {
act := iotextypes.StakeCreate{
Expand Down
4 changes: 4 additions & 0 deletions action/stake_migrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,10 @@ func (ms *MigrateStake) Serialize() []byte {
return byteutil.Must(proto.Marshal(ms.Proto()))
}

func (act *MigrateStake) FillAction(core *iotextypes.ActionCore) {
core.Action = &iotextypes.ActionCore_StakeMigrate{StakeMigrate: act.Proto()}
}

// Proto converts to protobuf Restake Action
func (ms *MigrateStake) Proto() *iotextypes.StakeMigrate {
act := &iotextypes.StakeMigrate{
Expand Down
8 changes: 8 additions & 0 deletions action/stake_reclaim.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,10 @@ func (su *Unstake) EthData() ([]byte, error) {
return append(_unstakeMethod.ID, data...), nil
}

func (act *Unstake) FillAction(core *iotextypes.ActionCore) {
core.Action = &iotextypes.ActionCore_StakeUnstake{StakeUnstake: act.Proto()}
}

// NewUnstakeFromABIBinary decodes data into WithdrawStake action
func NewUnstakeFromABIBinary(data []byte) (*Unstake, error) {
var (
Expand Down Expand Up @@ -214,6 +218,10 @@ func (sw *WithdrawStake) EthData() ([]byte, error) {
return append(_withdrawStakeMethod.ID, data...), nil
}

func (act *WithdrawStake) FillAction(core *iotextypes.ActionCore) {
core.Action = &iotextypes.ActionCore_StakeWithdraw{StakeWithdraw: act.Proto()}
}

// NewWithdrawStakeFromABIBinary decodes data into WithdrawStake action
func NewWithdrawStakeFromABIBinary(data []byte) (*WithdrawStake, error) {
var (
Expand Down
4 changes: 4 additions & 0 deletions action/stake_restake.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,10 @@ func (rs *Restake) Serialize() []byte {
return byteutil.Must(proto.Marshal(rs.Proto()))
}

func (act *Restake) FillAction(core *iotextypes.ActionCore) {
core.Action = &iotextypes.ActionCore_StakeRestake{StakeRestake: act.Proto()}
}

// Proto converts to protobuf Restake Action
func (rs *Restake) Proto() *iotextypes.StakeRestake {
act := &iotextypes.StakeRestake{
Expand Down
8 changes: 6 additions & 2 deletions action/stake_transferownership.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ 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/iotextypes"
"github.com/pkg/errors"
"google.golang.org/protobuf/proto"

"github.com/iotexproject/iotex-address/address"
"github.com/iotexproject/iotex-core/pkg/util/byteutil"
"github.com/iotexproject/iotex-proto/golang/iotextypes"
)

const (
Expand Down Expand Up @@ -104,6 +104,10 @@ func (ts *TransferStake) Serialize() []byte {
return byteutil.Must(proto.Marshal(ts.Proto()))
}

func (act *TransferStake) FillAction(core *iotextypes.ActionCore) {
core.Action = &iotextypes.ActionCore_StakeTransferOwnership{StakeTransferOwnership: act.Proto()}
}

// Proto converts transfer stake to protobuf
func (ts *TransferStake) Proto() *iotextypes.StakeTransferOwnership {
act := &iotextypes.StakeTransferOwnership{
Expand Down
4 changes: 4 additions & 0 deletions action/transfer.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,10 @@ func (tsf *Transfer) Serialize() []byte {
return byteutil.Must(proto.Marshal(tsf.Proto()))
}

func (tsf *Transfer) FillAction(act *iotextypes.ActionCore) {
act.Action = &iotextypes.ActionCore_Transfer{Transfer: tsf.Proto()}
}

// Proto converts Transfer to protobuf's Action
func (tsf *Transfer) Proto() *iotextypes.Transfer {
// used by account-based model
Expand Down
Loading

0 comments on commit 2c77273

Please sign in to comment.