Skip to content

Commit

Permalink
[action] use AbstractAction in envelope
Browse files Browse the repository at this point in the history
  • Loading branch information
dustinxie committed Apr 24, 2024
1 parent 0f4c441 commit 597ea18
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 52 deletions.
32 changes: 32 additions & 0 deletions action/actctx.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ package action

import (
"math/big"

"github.com/iotexproject/iotex-proto/golang/iotextypes"
"github.com/pkg/errors"
)

// AbstractAction is an abstract implementation of Action interface
Expand Down Expand Up @@ -85,3 +88,32 @@ func (act *AbstractAction) SanityCheck() error {
}
return nil
}

func (act *AbstractAction) toProto() *iotextypes.ActionCore {
actCore := iotextypes.ActionCore{
Version: act.version,
Nonce: act.nonce,
GasLimit: act.gasLimit,
ChainID: act.chainID,
}
if act.gasPrice != nil {
actCore.GasPrice = act.gasPrice.String()
}
return &actCore
}

func (act *AbstractAction) fromProto(pb *iotextypes.ActionCore) error {
act.version = pb.GetVersion()
act.nonce = pb.GetNonce()
act.gasLimit = pb.GetGasLimit()
act.chainID = pb.GetChainID()
if price := pb.GetGasPrice(); price == "" {
act.gasPrice = &big.Int{}
} else {
var ok bool
if act.gasPrice, ok = new(big.Int).SetString(price, 10); !ok {
return errors.Errorf("invalid gas prcie %s", price)
}
}
return nil
}
57 changes: 5 additions & 52 deletions action/envelope.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,24 +28,11 @@ type (
}

envelope struct {
version uint32
chainID uint32
nonce uint64
gasLimit uint64
gasPrice *big.Int
payload actionPayload
AbstractAction
payload actionPayload
}
)

// Version returns the version
func (elp *envelope) Version() uint32 { return elp.version }

// ChainID return the chainID value
func (elp *envelope) ChainID() uint32 { return elp.chainID }

// Nonce returns the nonce
func (elp *envelope) Nonce() uint64 { return elp.nonce }

// Destination returns the destination address
func (elp *envelope) Destination() (string, bool) {
r, ok := elp.payload.(hasDestination)
Expand All @@ -56,18 +43,6 @@ func (elp *envelope) Destination() (string, bool) {
return r.Destination(), true
}

// GasLimit returns the gas limit
func (elp *envelope) GasLimit() uint64 { return elp.gasLimit }

// GasPrice returns the gas price
func (elp *envelope) GasPrice() *big.Int {
p := &big.Int{}
if elp.gasPrice == nil {
return p
}
return p.Set(elp.gasPrice)
}

// Cost returns cost of actions
func (elp *envelope) Cost() (*big.Int, error) {
return elp.payload.Cost()
Expand All @@ -83,15 +58,7 @@ func (elp *envelope) Action() Action { return elp.payload }

// Proto convert Envelope to protobuf format.
func (elp *envelope) Proto() *iotextypes.ActionCore {
actCore := &iotextypes.ActionCore{
Version: elp.version,
Nonce: elp.nonce,
GasLimit: elp.gasLimit,
ChainID: elp.chainID,
}
if elp.gasPrice != nil {
actCore.GasPrice = elp.gasPrice.String()
}
actCore := elp.AbstractAction.toProto()

// TODO assert each action
switch act := elp.Action().(type) {
Expand Down Expand Up @@ -143,19 +110,8 @@ func (elp *envelope) LoadProto(pbAct *iotextypes.ActionCore) error {
if elp == nil {
return ErrNilAction
}
*elp = envelope{}
elp.version = pbAct.GetVersion()
elp.nonce = pbAct.GetNonce()
elp.gasLimit = pbAct.GetGasLimit()
elp.chainID = pbAct.GetChainID()
if pbAct.GetGasPrice() == "" {
elp.gasPrice = big.NewInt(0)
} else {
gp, ok := new(big.Int).SetString(pbAct.GetGasPrice(), 10)
if !ok {
return errors.Errorf("invalid gas prcie %s", pbAct.GetGasPrice())
}
elp.gasPrice = gp
if err := elp.AbstractAction.fromProto(pbAct); err != nil {
return err
}

switch {
Expand Down Expand Up @@ -269,8 +225,5 @@ func (elp *envelope) LoadProto(pbAct *iotextypes.ActionCore) error {
return nil
}

// SetNonce sets the nonce value
func (elp *envelope) SetNonce(n uint64) { elp.nonce = n }

// SetChainID sets the chainID value
func (elp *envelope) SetChainID(chainID uint32) { elp.chainID = chainID }

0 comments on commit 597ea18

Please sign in to comment.