Skip to content

Commit

Permalink
use pointer SealedEnvelope instead of value SealedEnvelope (#4065)
Browse files Browse the repository at this point in the history
  • Loading branch information
millken authored Jan 29, 2024
1 parent 5728a70 commit 83db41b
Show file tree
Hide file tree
Showing 58 changed files with 285 additions and 285 deletions.
14 changes: 7 additions & 7 deletions action/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ type (
)

// Sign signs the action using sender's private key
func Sign(act Envelope, sk crypto.PrivateKey) (SealedEnvelope, error) {
sealed := SealedEnvelope{
func Sign(act Envelope, sk crypto.PrivateKey) (*SealedEnvelope, error) {
sealed := &SealedEnvelope{
Envelope: act,
srcPubkey: sk.PublicKey(),
}
Expand All @@ -58,8 +58,8 @@ func Sign(act Envelope, sk crypto.PrivateKey) (SealedEnvelope, error) {

// FakeSeal creates a SealedActionEnvelope without signature.
// This method should be only used in tests.
func FakeSeal(act Envelope, pubk crypto.PublicKey) SealedEnvelope {
sealed := SealedEnvelope{
func FakeSeal(act Envelope, pubk crypto.PublicKey) *SealedEnvelope {
sealed := &SealedEnvelope{
Envelope: act,
srcPubkey: pubk,
}
Expand All @@ -68,8 +68,8 @@ func FakeSeal(act Envelope, pubk crypto.PublicKey) SealedEnvelope {

// AssembleSealedEnvelope assembles a SealedEnvelope use Envelope, Sender Address and Signature.
// This method should be only used in tests.
func AssembleSealedEnvelope(act Envelope, pk crypto.PublicKey, sig []byte) SealedEnvelope {
sealed := SealedEnvelope{
func AssembleSealedEnvelope(act Envelope, pk crypto.PublicKey, sig []byte) *SealedEnvelope {
sealed := &SealedEnvelope{
Envelope: act,
srcPubkey: pk,
signature: sig,
Expand All @@ -89,7 +89,7 @@ func CalculateIntrinsicGas(baseIntrinsicGas uint64, payloadGas uint64, payloadSi
}

// IsSystemAction determine whether input action belongs to system action
func IsSystemAction(act SealedEnvelope) bool {
func IsSystemAction(act *SealedEnvelope) bool {
switch act.Action().(type) {
case *GrantReward, *PutPollResult:
return true
Expand Down
4 changes: 2 additions & 2 deletions action/action_deserializer.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ func (ad *Deserializer) SetEvmNetworkID(id uint32) *Deserializer {
}

// ActionToSealedEnvelope converts protobuf to SealedEnvelope
func (ad *Deserializer) ActionToSealedEnvelope(pbAct *iotextypes.Action) (SealedEnvelope, error) {
func (ad *Deserializer) ActionToSealedEnvelope(pbAct *iotextypes.Action) (*SealedEnvelope, error) {
var selp SealedEnvelope
err := selp.loadProto(pbAct, ad.evmNetworkID)
return selp, err
return &selp, err
}
2 changes: 1 addition & 1 deletion action/protocol/generic_validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func NewGenericValidator(sr StateReader, accountState AccountState) *GenericVali
}

// Validate validates a generic action
func (v *GenericValidator) Validate(ctx context.Context, selp action.SealedEnvelope) error {
func (v *GenericValidator) Validate(ctx context.Context, selp *action.SealedEnvelope) error {
intrinsicGas, err := selp.IntrinsicGas()
if err != nil {
return err
Expand Down
4 changes: 2 additions & 2 deletions action/rlp_tx_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ func TestRlpDecodeVerify(t *testing.T) {

// receive from API
proto.Unmarshal(bs, pb)
selp := SealedEnvelope{}
selp := &SealedEnvelope{}
require.NoError(selp.loadProto(pb, _evmNetworkID))
act, ok := selp.Action().(EthCompatibleAction)
require.True(ok)
Expand Down Expand Up @@ -462,7 +462,7 @@ func TestEthTxDecodeVerify(t *testing.T) {

// receive from API
proto.Unmarshal(bs, pb)
selp := SealedEnvelope{}
selp := &SealedEnvelope{}
require.NoError(selp.loadProto(pb, uint32(tx.ChainId().Uint64())))
act, ok := selp.Action().(EthCompatibleAction)
require.True(ok)
Expand Down
6 changes: 3 additions & 3 deletions action/sealedenvelope_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ func TestSealedEnvelope_Proto(t *testing.T) {
}
req.Equal(ac, proto)

se2 := SealedEnvelope{}
se2 := &SealedEnvelope{}
for _, v := range []struct {
encoding iotextypes.Encoding
sig []byte
Expand Down Expand Up @@ -181,7 +181,7 @@ func TestSealedEnvelope_Proto(t *testing.T) {
}
}

func createSealedEnvelope(chainID uint32) (SealedEnvelope, error) {
func createSealedEnvelope(chainID uint32) (*SealedEnvelope, error) {
tsf, _ := NewTransfer(
uint64(10),
unit.ConvertIotxToRau(1000+int64(10)),
Expand All @@ -200,7 +200,7 @@ func createSealedEnvelope(chainID uint32) (SealedEnvelope, error) {
SetChainID(chainID).Build()

cPubKey, err := crypto.HexStringToPublicKey(_publicKey)
se := SealedEnvelope{}
se := &SealedEnvelope{}
se.Envelope = evlp
se.srcPubkey = cPubKey
se.signature = _signByte
Expand Down
2 changes: 1 addition & 1 deletion action/sealedenvelopevalidator.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,5 @@ import (
// SealedEnvelopeValidator is the interface of validating an SealedEnvelope action
type SealedEnvelopeValidator interface {
// Validate returns an error if any validation failed
Validate(context.Context, SealedEnvelope) error
Validate(context.Context, *SealedEnvelope) error
}
62 changes: 31 additions & 31 deletions action/signedaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ var (
)

// SignedTransfer return a signed transfer
func SignedTransfer(recipientAddr string, senderPriKey crypto.PrivateKey, nonce uint64, amount *big.Int, payload []byte, gasLimit uint64, gasPrice *big.Int) (SealedEnvelope, error) {
func SignedTransfer(recipientAddr string, senderPriKey crypto.PrivateKey, nonce uint64, amount *big.Int, payload []byte, gasLimit uint64, gasPrice *big.Int) (*SealedEnvelope, error) {
transfer, err := NewTransfer(nonce, amount, recipientAddr, payload, gasLimit, gasPrice)
if err != nil {
return SealedEnvelope{}, err
return nil, err
}
bd := &EnvelopeBuilder{}
elp := bd.SetNonce(nonce).
Expand All @@ -32,16 +32,16 @@ func SignedTransfer(recipientAddr string, senderPriKey crypto.PrivateKey, nonce
SetAction(transfer).Build()
selp, err := Sign(elp, senderPriKey)
if err != nil {
return SealedEnvelope{}, errors.Wrapf(err, "failed to sign transfer %v", elp)
return nil, errors.Wrapf(err, "failed to sign transfer %v", elp)
}
return selp, nil
}

// SignedExecution return a signed execution
func SignedExecution(contractAddr string, executorPriKey crypto.PrivateKey, nonce uint64, amount *big.Int, gasLimit uint64, gasPrice *big.Int, data []byte) (SealedEnvelope, error) {
func SignedExecution(contractAddr string, executorPriKey crypto.PrivateKey, nonce uint64, amount *big.Int, gasLimit uint64, gasPrice *big.Int, data []byte) (*SealedEnvelope, error) {
execution, err := NewExecution(contractAddr, nonce, amount, gasLimit, gasPrice, data)
if err != nil {
return SealedEnvelope{}, err
return nil, err
}
bd := &EnvelopeBuilder{}
elp := bd.SetNonce(nonce).
Expand All @@ -50,7 +50,7 @@ func SignedExecution(contractAddr string, executorPriKey crypto.PrivateKey, nonc
SetAction(execution).Build()
selp, err := Sign(elp, executorPriKey)
if err != nil {
return SealedEnvelope{}, errors.Wrapf(err, "failed to sign execution %v", elp)
return nil, errors.Wrapf(err, "failed to sign execution %v", elp)
}
return selp, nil
}
Expand All @@ -65,11 +65,11 @@ func SignedCandidateRegister(
gasLimit uint64,
gasPrice *big.Int,
registererPriKey crypto.PrivateKey,
) (SealedEnvelope, error) {
) (*SealedEnvelope, error) {
cr, err := NewCandidateRegister(nonce, name, operatorAddrStr, rewardAddrStr, ownerAddrStr, amountStr,
duration, autoStake, payload, gasLimit, gasPrice)
if err != nil {
return SealedEnvelope{}, err
return nil, err
}
bd := &EnvelopeBuilder{}
elp := bd.SetNonce(nonce).
Expand All @@ -78,7 +78,7 @@ func SignedCandidateRegister(
SetAction(cr).Build()
selp, err := Sign(elp, registererPriKey)
if err != nil {
return SealedEnvelope{}, errors.Wrapf(err, "failed to sign candidate register %v", elp)
return nil, errors.Wrapf(err, "failed to sign candidate register %v", elp)
}
return selp, nil
}
Expand All @@ -90,10 +90,10 @@ func SignedCandidateUpdate(
gasLimit uint64,
gasPrice *big.Int,
registererPriKey crypto.PrivateKey,
) (SealedEnvelope, error) {
) (*SealedEnvelope, error) {
cu, err := NewCandidateUpdate(nonce, name, operatorAddrStr, rewardAddrStr, gasLimit, gasPrice)
if err != nil {
return SealedEnvelope{}, err
return nil, err
}
bd := &EnvelopeBuilder{}
elp := bd.SetNonce(nonce).
Expand All @@ -102,7 +102,7 @@ func SignedCandidateUpdate(
SetAction(cu).Build()
selp, err := Sign(elp, registererPriKey)
if err != nil {
return SealedEnvelope{}, errors.Wrapf(err, "failed to sign candidate update %v", elp)
return nil, errors.Wrapf(err, "failed to sign candidate update %v", elp)
}
return selp, nil
}
Expand All @@ -116,11 +116,11 @@ func SignedCreateStake(nonce uint64,
gasLimit uint64,
gasPrice *big.Int,
stakerPriKey crypto.PrivateKey,
) (SealedEnvelope, error) {
) (*SealedEnvelope, error) {
cs, err := NewCreateStake(nonce, candidateName, amount, duration, autoStake,
payload, gasLimit, gasPrice)
if err != nil {
return SealedEnvelope{}, err
return nil, err
}
bd := &EnvelopeBuilder{}
elp := bd.SetNonce(nonce).
Expand All @@ -129,7 +129,7 @@ func SignedCreateStake(nonce uint64,
SetAction(cs).Build()
selp, err := Sign(elp, stakerPriKey)
if err != nil {
return SealedEnvelope{}, errors.Wrapf(err, "failed to sign create stake %v", elp)
return nil, errors.Wrapf(err, "failed to sign create stake %v", elp)
}
return selp, nil
}
Expand All @@ -143,7 +143,7 @@ func SignedReclaimStake(
gasLimit uint64,
gasPrice *big.Int,
reclaimerPriKey crypto.PrivateKey,
) (SealedEnvelope, error) {
) (*SealedEnvelope, error) {
bd := &EnvelopeBuilder{}
eb := bd.SetNonce(nonce).
SetGasPrice(gasPrice).
Expand All @@ -153,19 +153,19 @@ func SignedReclaimStake(
if !withdraw {
us, err := NewUnstake(nonce, bucketIndex, payload, gasLimit, gasPrice)
if err != nil {
return SealedEnvelope{}, err
return nil, err
}
elp = eb.SetAction(us).Build()
} else {
w, err := NewWithdrawStake(nonce, bucketIndex, payload, gasLimit, gasPrice)
if err != nil {
return SealedEnvelope{}, err
return nil, err
}
elp = eb.SetAction(w).Build()
}
selp, err := Sign(elp, reclaimerPriKey)
if err != nil {
return SealedEnvelope{}, errors.Wrapf(err, "failed to sign reclaim stake %v", elp)
return nil, errors.Wrapf(err, "failed to sign reclaim stake %v", elp)
}
return selp, nil
}
Expand All @@ -179,10 +179,10 @@ func SignedChangeCandidate(
gasLimit uint64,
gasPrice *big.Int,
stakerPriKey crypto.PrivateKey,
) (SealedEnvelope, error) {
) (*SealedEnvelope, error) {
cc, err := NewChangeCandidate(nonce, candName, bucketIndex, payload, gasLimit, gasPrice)
if err != nil {
return SealedEnvelope{}, err
return nil, err
}
bd := &EnvelopeBuilder{}
elp := bd.SetNonce(nonce).
Expand All @@ -191,7 +191,7 @@ func SignedChangeCandidate(
SetAction(cc).Build()
selp, err := Sign(elp, stakerPriKey)
if err != nil {
return SealedEnvelope{}, errors.Wrapf(err, "failed to sign change candidate %v", elp)
return nil, errors.Wrapf(err, "failed to sign change candidate %v", elp)
}
return selp, nil
}
Expand All @@ -205,10 +205,10 @@ func SignedTransferStake(
gasLimit uint64,
gasPrice *big.Int,
stakerPriKey crypto.PrivateKey,
) (SealedEnvelope, error) {
) (*SealedEnvelope, error) {
ts, err := NewTransferStake(nonce, voterAddress, bucketIndex, payload, gasLimit, gasPrice)
if err != nil {
return SealedEnvelope{}, err
return nil, err
}
bd := &EnvelopeBuilder{}
elp := bd.SetNonce(nonce).
Expand All @@ -217,7 +217,7 @@ func SignedTransferStake(
SetAction(ts).Build()
selp, err := Sign(elp, stakerPriKey)
if err != nil {
return SealedEnvelope{}, errors.Wrapf(err, "failed to sign transfer stake %v", elp)
return nil, errors.Wrapf(err, "failed to sign transfer stake %v", elp)
}
return selp, nil
}
Expand All @@ -231,10 +231,10 @@ func SignedDepositToStake(
gasLimit uint64,
gasPrice *big.Int,
depositorPriKey crypto.PrivateKey,
) (SealedEnvelope, error) {
) (*SealedEnvelope, error) {
ds, err := NewDepositToStake(nonce, index, amount, payload, gasLimit, gasPrice)
if err != nil {
return SealedEnvelope{}, err
return nil, err
}
bd := &EnvelopeBuilder{}
elp := bd.SetNonce(nonce).
Expand All @@ -243,7 +243,7 @@ func SignedDepositToStake(
SetAction(ds).Build()
selp, err := Sign(elp, depositorPriKey)
if err != nil {
return SealedEnvelope{}, errors.Wrapf(err, "failed to sign deposit to stake %v", elp)
return nil, errors.Wrapf(err, "failed to sign deposit to stake %v", elp)
}
return selp, nil
}
Expand All @@ -258,10 +258,10 @@ func SignedRestake(
gasLimit uint64,
gasPrice *big.Int,
restakerPriKey crypto.PrivateKey,
) (SealedEnvelope, error) {
) (*SealedEnvelope, error) {
rs, err := NewRestake(nonce, index, duration, autoStake, payload, gasLimit, gasPrice)
if err != nil {
return SealedEnvelope{}, err
return nil, err
}
bd := &EnvelopeBuilder{}
elp := bd.SetNonce(nonce).
Expand All @@ -270,7 +270,7 @@ func SignedRestake(
SetAction(rs).Build()
selp, err := Sign(elp, restakerPriKey)
if err != nil {
return SealedEnvelope{}, errors.Wrapf(err, "failed to sign restake %v", elp)
return nil, errors.Wrapf(err, "failed to sign restake %v", elp)
}
return selp, nil
}
2 changes: 1 addition & 1 deletion actpool/accountpool.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func (ap *accountPool) PutAction(
pendingNonce uint64,
confirmedBalance *big.Int,
expiry time.Duration,
act action.SealedEnvelope,
act *action.SealedEnvelope,
) error {
account, ok := ap.accounts[addr]
if !ok {
Expand Down
Loading

0 comments on commit 83db41b

Please sign in to comment.