Skip to content

Commit

Permalink
[action] remove AbstractAction from all actions
Browse files Browse the repository at this point in the history
  • Loading branch information
dustinxie committed Aug 30, 2024
1 parent 742e9ad commit 1e536b5
Show file tree
Hide file tree
Showing 13 changed files with 92 additions and 124 deletions.
17 changes: 0 additions & 17 deletions action/actctx.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,23 +86,6 @@ func (act *AbstractAction) BasicActionSize() uint32 {
return uint32(size)
}

// SetEnvelopeContext sets the struct according to input
func (act *AbstractAction) SetEnvelopeContext(in *AbstractAction) {
if act == nil {
return
}
*act = *in
if in.gasPrice != nil {
act.gasPrice = new(big.Int).Set(in.gasPrice)
}
if in.gasTipCap != nil {
act.gasTipCap = new(big.Int).Set(in.gasTipCap)
}
if in.gasFeeCap != nil {
act.gasFeeCap = new(big.Int).Set(in.gasFeeCap)
}
}

// SanityCheck validates the variables in the action
func (act *AbstractAction) SanityCheck() error {
// Reject execution of negative gas price
Expand Down
6 changes: 4 additions & 2 deletions action/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,7 @@ type (
}

actionPayload interface {
Cost() (*big.Int, error)
IntrinsicGas() (uint64, error)
SetEnvelopeContext(*AbstractAction)
SanityCheck() error
}

Expand All @@ -46,6 +44,10 @@ type (
hasSize interface {
Size() uint32
}

hasAmount interface {
Amount() *big.Int
}
)

// Sign signs the action using sender's private key
Expand Down
7 changes: 1 addition & 6 deletions action/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,6 @@ func (b *EnvelopeBuilder) build() Envelope {
if b.elp.payload == nil {
panic("cannot build Envelope w/o a valid payload")
}
b.elp.payload.SetEnvelopeContext(&b.elp.AbstractAction)
return &b.elp
}

Expand All @@ -156,11 +155,7 @@ func (b *EnvelopeBuilder) BuildTransfer(tx *types.Transaction) (Envelope, error)
return nil, ErrInvalidAct
}
b.setEnvelopeCommonFields(tx)
tsf, err := NewTransfer(tx.Nonce(), tx.Value(), getRecipientAddr(tx.To()), tx.Data(), tx.Gas(), tx.GasPrice())
if err != nil {
return nil, err
}
b.elp.payload = tsf
b.elp.payload = NewTransfer(tx.Value(), getRecipientAddr(tx.To()), tx.Data())
return b.build(), nil
}

Expand Down
33 changes: 30 additions & 3 deletions action/envelope.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import (
type (
// Envelope defines an envelope wrapped on action with some envelope metadata.
Envelope interface {
Version() uint32
CommonAction() AbstractAction
Nonce() uint64
ChainID() uint32
GasLimit() uint64
Expand All @@ -35,6 +35,7 @@ type (
LoadProto(pbAct *iotextypes.ActionCore) error
SetNonce(n uint64)
SetChainID(chainID uint32)
SanityCheck() error
}

envelope struct {
Expand All @@ -43,6 +44,10 @@ type (
}
)

func (elp *envelope) CommonAction() AbstractAction {
return elp.AbstractAction
}

// Destination returns the destination address
func (elp *envelope) Destination() (string, bool) {
r, ok := elp.payload.(hasDestination)
Expand All @@ -55,7 +60,22 @@ func (elp *envelope) Destination() (string, bool) {

// Cost returns cost of actions
func (elp *envelope) Cost() (*big.Int, error) {
return elp.payload.Cost()
var cost *big.Int
switch act := elp.payload.(type) {
case *Execution, *MigrateStake:
cost = new(big.Int).SetUint64(elp.GasLimit())
default:
gas, err := act.IntrinsicGas()
if err != nil {
return nil, errors.Wrap(err, "failed to get payload's intrinsic gas")
}
cost = new(big.Int).SetUint64(gas)
}
cost.Mul(cost, elp.GasPrice())
if amount, ok := elp.payload.(hasAmount); ok {
cost.Add(cost, amount.Amount())
}
return cost, nil
}

// IntrinsicGas returns intrinsic gas of action.
Expand Down Expand Up @@ -278,9 +298,16 @@ func (elp *envelope) LoadProto(pbAct *iotextypes.ActionCore) error {
default:
return errors.Errorf("no applicable action to handle proto type %T", pbAct.Action)
}
elp.payload.SetEnvelopeContext(&elp.AbstractAction)
return nil
}

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

// SanityCheck does the sanity check
func (elp *envelope) SanityCheck() error {
if err := elp.payload.SanityCheck(); err != nil {
return err
}
return elp.AbstractAction.SanityCheck()
}
5 changes: 2 additions & 3 deletions action/protocol/account/protocol.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,8 @@ func FindProtocol(registry *protocol.Registry) *Protocol {

// Handle handles an account
func (p *Protocol) Handle(ctx context.Context, elp action.Envelope, sm protocol.StateManager) (*action.Receipt, error) {
switch act := elp.Action().(type) {
case *action.Transfer:
return p.handleTransfer(ctx, act, sm)
if _, ok := elp.Action().(*action.Transfer); ok {
return p.handleTransfer(ctx, elp, sm)
}
return nil, nil
}
Expand Down
10 changes: 6 additions & 4 deletions action/protocol/account/transfer.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,13 @@ import (
const TransferSizeLimit = 32 * 1024

// handleTransfer handles a transfer
func (p *Protocol) handleTransfer(ctx context.Context, tsf *action.Transfer, sm protocol.StateManager) (*action.Receipt, error) {
func (p *Protocol) handleTransfer(ctx context.Context, elp action.Envelope, sm protocol.StateManager) (*action.Receipt, error) {
var (
fCtx = protocol.MustGetFeatureCtx(ctx)
actionCtx = protocol.MustGetActionCtx(ctx)
blkCtx = protocol.MustGetBlockCtx(ctx)
common = elp.CommonAction()
tsf = elp.Action().(*action.Transfer)
)
accountCreationOpts := []state.AccountCreationOption{}
if fCtx.CreateLegacyNonceAccount {
Expand All @@ -39,7 +41,7 @@ func (p *Protocol) handleTransfer(ctx context.Context, tsf *action.Transfer, sm
return nil, errors.Wrapf(err, "failed to load or create the account of sender %s", actionCtx.Caller.String())
}

gasFee, baseFee, err := protocol.SplitGas(ctx, &tsf.AbstractAction, actionCtx.IntrinsicGas)
gasFee, baseFee, err := protocol.SplitGas(ctx, &common, actionCtx.IntrinsicGas)
if err != nil {
return nil, errors.Wrapf(err, "failed to split gas")
}
Expand Down Expand Up @@ -71,9 +73,9 @@ func (p *Protocol) handleTransfer(ctx context.Context, tsf *action.Transfer, sm
}
}

if fCtx.FixGasAndNonceUpdate || tsf.Nonce() != 0 {
if fCtx.FixGasAndNonceUpdate || common.Nonce() != 0 {
// update sender Nonce
if err := sender.SetPendingNonce(tsf.Nonce() + 1); err != nil {
if err := sender.SetPendingNonce(common.Nonce() + 1); err != nil {
return nil, errors.Wrapf(err, "failed to update pending nonce of sender %s", actionCtx.Caller.String())
}
}
Expand Down
3 changes: 1 addition & 2 deletions action/protocol/generic_validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,5 @@ func (v *GenericValidator) Validate(ctx context.Context, selp *action.SealedEnve
}
}
}

return selp.Action().SanityCheck()
return selp.Envelope.SanityCheck()
}
6 changes: 1 addition & 5 deletions action/signedaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,11 @@ func WithChainID(chainID uint32) SignedActionOption {

// SignedTransfer return a signed transfer
func SignedTransfer(recipientAddr string, senderPriKey crypto.PrivateKey, nonce uint64, amount *big.Int, payload []byte, gasLimit uint64, gasPrice *big.Int, options ...SignedActionOption) (*SealedEnvelope, error) {
transfer, err := NewTransfer(nonce, amount, recipientAddr, payload, gasLimit, gasPrice)
if err != nil {
return nil, err
}
bd := &EnvelopeBuilder{}
bd = bd.SetNonce(nonce).
SetGasPrice(gasPrice).
SetGasLimit(gasLimit).
SetAction(transfer)
SetAction(NewTransfer(amount, recipientAddr, payload))
for _, opt := range options {
opt(bd)
}
Expand Down
30 changes: 4 additions & 26 deletions action/transfer.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import (
"google.golang.org/protobuf/proto"

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

const (
Expand All @@ -28,39 +27,28 @@ const (
var (
_ hasDestination = (*Transfer)(nil)
_ hasSize = (*Transfer)(nil)
_ hasAmount = (*Transfer)(nil)
_ EthCompatibleAction = (*Transfer)(nil)
)

// Transfer defines the struct of account-based transfer
type Transfer struct {
AbstractAction

amount *big.Int
recipient string
payload []byte
}

// NewTransfer returns a Transfer instance
func NewTransfer(
nonce uint64,
amount *big.Int,
recipient string,
payload []byte,
gasLimit uint64,
gasPrice *big.Int,
) (*Transfer, error) {
) *Transfer {
return &Transfer{
AbstractAction: AbstractAction{
version: version.ProtocolVersion,
nonce: nonce,
gasLimit: gasLimit,
gasPrice: gasPrice,
},
recipient: recipient,
amount: amount,
payload: payload,
// SenderPublicKey and Signature will be populated in Sign()
}, nil
}
}

// Amount returns the amount
Expand Down Expand Up @@ -135,23 +123,13 @@ func (tsf *Transfer) IntrinsicGas() (uint64, error) {
return CalculateIntrinsicGas(TransferBaseIntrinsicGas, TransferPayloadGas, payloadSize)
}

// Cost returns the total cost of a transfer
func (tsf *Transfer) Cost() (*big.Int, error) {
intrinsicGas, err := tsf.IntrinsicGas()
if err != nil {
return nil, errors.Wrap(err, "failed to get intrinsic gas for the transfer")
}
transferFee := big.NewInt(0).Mul(tsf.GasPrice(), big.NewInt(0).SetUint64(intrinsicGas))
return big.NewInt(0).Add(tsf.Amount(), transferFee), nil
}

// SanityCheck validates the variables in the action
func (tsf *Transfer) SanityCheck() error {
// Reject transfer of negative amount
if tsf.Amount().Sign() < 0 {
return ErrNegativeValue
}
return tsf.AbstractAction.SanityCheck()
return nil
}

// EthTo returns the address for converting to eth tx
Expand Down
2 changes: 0 additions & 2 deletions action/tx_container.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,6 @@ func (etx *txContainer) IntrinsicGas() (uint64, error) {
return gas, nil
}

func (etx *txContainer) SetEnvelopeContext(*AbstractAction) {}

func (etx *txContainer) SanityCheck() error {
// Reject execution of negative amount
if etx.tx.Value().Sign() < 0 {
Expand Down
Loading

0 comments on commit 1e536b5

Please sign in to comment.