diff --git a/action/actctx.go b/action/actctx.go index 4597fbf622..26fd70ed98 100644 --- a/action/actctx.go +++ b/action/actctx.go @@ -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 @@ -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 +} diff --git a/action/envelope.go b/action/envelope.go index c07e69743b..9631a634ed 100644 --- a/action/envelope.go +++ b/action/envelope.go @@ -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) @@ -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() @@ -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) { @@ -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 { @@ -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 }