Skip to content

Commit

Permalink
[action] action hash keep the same with or w/o sidecar (#4408)
Browse files Browse the repository at this point in the history
Co-authored-by: dustinxie <dahuaxie@gmail.com>
  • Loading branch information
envestcc and dustinxie authored Sep 30, 2024
1 parent 2c77273 commit fd01240
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 2 deletions.
2 changes: 2 additions & 0 deletions action/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ type (
gasLimitForCost interface{ GasLimitForCost() }

validateSidecar interface{ ValidateSidecar() error }

protoForRawHash interface{ ProtoForRawHash() *iotextypes.ActionCore }
)

// Sign signs the action using sender's private key
Expand Down
12 changes: 12 additions & 0 deletions action/envelope.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ type (
Action() Action
ToEthTx(uint32, iotextypes.Encoding) (*types.Transaction, error)
Proto() *iotextypes.ActionCore
ProtoForHash() *iotextypes.ActionCore
LoadProto(*iotextypes.ActionCore) error
SetNonce(uint64)
SetGas(uint64)
Expand Down Expand Up @@ -250,6 +251,17 @@ func (elp *envelope) ToEthTx(evmNetworkID uint32, encoding iotextypes.Encoding)
return elp.common.toEthTx(to, tx.Value(), data), nil
}

func (elp *envelope) ProtoForHash() *iotextypes.ActionCore {
var actCore *iotextypes.ActionCore
if pfh, ok := elp.common.(protoForRawHash); ok {
actCore = pfh.ProtoForRawHash()
} else {
actCore = elp.common.toProto()
}
elp.payload.FillAction(actCore)
return actCore
}

// Proto convert Envelope to protobuf format.
func (elp *envelope) Proto() *iotextypes.ActionCore {
actCore := elp.common.toProto()
Expand Down
12 changes: 12 additions & 0 deletions action/envelope_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@ import (

"github.com/iotexproject/iotex-proto/golang/iotextypes"
"github.com/stretchr/testify/require"
"google.golang.org/protobuf/proto"

"github.com/iotexproject/iotex-core/pkg/unit"
"github.com/iotexproject/iotex-core/pkg/util/byteutil"
"github.com/iotexproject/iotex-core/state"
"github.com/iotexproject/iotex-core/test/identityset"
)
Expand Down Expand Up @@ -129,3 +131,13 @@ func createEnvelope() (Envelope, *Transfer) {
SetNonce(10).SetVersion(1).SetChainID(1).Build()
return evlp, tsf
}

func TestEnvelope_Hash(t *testing.T) {
r := require.New(t)
blob := createTestBlobTxData()
e := NewEnvelop(NewBlobTx(1, 2, 3, big.NewInt(1), big.NewInt(1), nil, blob), NewTransfer(big.NewInt(10), "io1", []byte("test")))
blobWithoutSidecar := createTestBlobTxData()
blobWithoutSidecar.sidecar = nil
eWithoutSidecar := NewEnvelop(NewBlobTx(1, 2, 3, big.NewInt(1), big.NewInt(1), nil, blobWithoutSidecar), NewTransfer(big.NewInt(10), "io1", []byte("test")))
r.Equal(byteutil.Must(proto.Marshal(e.ProtoForHash())), byteutil.Must(proto.Marshal(eWithoutSidecar.ProtoForHash())))
}
13 changes: 11 additions & 2 deletions action/sealedenvelope.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func (sealed *SealedEnvelope) envelopeHash() (hash.Hash256, error) {
}
return rlpRawHash(tx, signer)
case iotextypes.Encoding_IOTEX_PROTOBUF:
return hash.Hash256b(byteutil.Must(proto.Marshal(sealed.Envelope.Proto()))), nil
return hash.Hash256b(byteutil.Must(proto.Marshal(sealed.Envelope.ProtoForHash()))), nil
default:
return hash.ZeroHash256, errors.Errorf("unknown encoding type %v", sealed.encoding)
}
Expand Down Expand Up @@ -94,7 +94,7 @@ func (sealed *SealedEnvelope) calcHash() (hash.Hash256, error) {
}
return rlpSignedHash(tx, signer, sealed.Signature())
case iotextypes.Encoding_IOTEX_PROTOBUF:
return hash.Hash256b(byteutil.Must(proto.Marshal(sealed.Proto()))), nil
return hash.Hash256b(byteutil.Must(proto.Marshal(sealed.protoForHash()))), nil
default:
return hash.ZeroHash256, errors.Errorf("unknown encoding type %v", sealed.encoding)
}
Expand Down Expand Up @@ -138,6 +138,15 @@ func (sealed *SealedEnvelope) Proto() *iotextypes.Action {
}
}

func (sealed *SealedEnvelope) protoForHash() *iotextypes.Action {
return &iotextypes.Action{
Core: sealed.Envelope.ProtoForHash(),
SenderPubKey: sealed.srcPubkey.Bytes(),
Signature: sealed.signature,
Encoding: sealed.encoding,
}
}

// loadProto loads from proto scheme.
func (sealed *SealedEnvelope) loadProto(pbAct *iotextypes.Action, evmID uint32) error {
if pbAct == nil {
Expand Down
8 changes: 8 additions & 0 deletions action/tx_blob.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,14 @@ func (tx *BlobTx) toProto() *iotextypes.ActionCore {
return &actCore
}

func (tx *BlobTx) ProtoForRawHash() *iotextypes.ActionCore {
actCore := tx.toProto()
if actCore.BlobTxData != nil {
actCore.BlobTxData.BlobTxSidecar = nil
}
return actCore
}

func (tx *BlobTx) fromProto(pb *iotextypes.ActionCore) error {
var (
feeCap = new(uint256.Int)
Expand Down
14 changes: 14 additions & 0 deletions test/mock/mock_envelope/mock_envelope.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit fd01240

Please sign in to comment.