Skip to content

Commit

Permalink
[action] enable tx container (#4416)
Browse files Browse the repository at this point in the history
  • Loading branch information
dustinxie authored Oct 11, 2024
1 parent e873aa9 commit 1893ab7
Show file tree
Hide file tree
Showing 12 changed files with 290 additions and 105 deletions.
13 changes: 7 additions & 6 deletions action/blob_data.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,13 +162,14 @@ func (tx *BlobTxData) SanityCheck() error {
}

func (tx *BlobTxData) ValidateSidecar() error {
var (
size = len(tx.blobHashes)
sidecar = tx.sidecar
)
if sidecar == nil {
if tx.sidecar == nil {
return errors.New("sidecar is missing")
}
return verifySidecar(tx.sidecar, tx.blobHashes)
}

func verifySidecar(sidecar *types.BlobTxSidecar, hashes []common.Hash) error {
size := len(hashes)
// Verify the size of hashes, commitments and proofs
if len(sidecar.Blobs) != size {
return errors.New("number of blobs and hashes mismatch")
Expand All @@ -182,7 +183,7 @@ func (tx *BlobTxData) ValidateSidecar() error {
// Blob quantities match up, validate that the provers match with the
// transaction hash before getting to the cryptography
hasher := sha256.New()
for i, vhash := range tx.blobHashes {
for i, vhash := range hashes {
computed := kzg4844.CalcBlobHashV1(hasher, &sidecar.Commitments[i])
if vhash != computed {
return errors.Errorf("blob %d: computed hash %x mismatches transaction one %x", i, computed, vhash)
Expand Down
14 changes: 9 additions & 5 deletions action/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,13 +196,17 @@ func (b *EnvelopeBuilder) setEnvelopeCommonFields(tx *types.Transaction) error {
b.ab.nonce = tx.Nonce()
b.ab.gasPrice = tx.GasPrice()
b.ab.gasLimit = tx.Gas()
b.ab.accessList = tx.AccessList()
if acl := tx.AccessList(); len(acl) > 0 {
b.ab.accessList = tx.AccessList()
}
b.ab.gasFeeCap = tx.GasFeeCap()
b.ab.gasTipCap = tx.GasTipCap()
b.ab.blobData = &BlobTxData{
blobFeeCap: uint256.MustFromBig(tx.BlobGasFeeCap()),
blobHashes: tx.BlobHashes(),
sidecar: tx.BlobTxSidecar(),
if hashes := tx.BlobHashes(); len(hashes) > 0 {
b.ab.blobData = &BlobTxData{
blobFeeCap: uint256.MustFromBig(tx.BlobGasFeeCap()),
blobHashes: hashes,
sidecar: tx.BlobTxSidecar(),
}
}
return nil
}
Expand Down
10 changes: 2 additions & 8 deletions action/envelope.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,8 @@ type (
}
)

// NewEnvelop creates a new envelope
func NewEnvelop(common TxCommonInternal, payload actionPayload) Envelope {
// NewEnvelope creates a new envelope
func NewEnvelope(common TxCommonInternal, payload actionPayload) Envelope {
return &envelope{
common: common,
payload: payload,
Expand Down Expand Up @@ -428,12 +428,6 @@ func (elp *envelope) loadProtoActionPayload(pbAct *iotextypes.ActionCore) error
return err
}
elp.payload = act
case pbAct.GetTxContainer() != nil:
act := &txContainer{}
if err := act.loadProto(pbAct.GetTxContainer()); err != nil {
return err
}
elp.payload = act
case pbAct.GetStakeMigrate() != nil:
act := &MigrateStake{}
if err := act.LoadProto(pbAct.GetStakeMigrate()); err != nil {
Expand Down
4 changes: 2 additions & 2 deletions action/envelope_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,9 +135,9 @@ func createEnvelope() (Envelope, *Transfer) {
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")))
e := NewEnvelope(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")))
eWithoutSidecar := NewEnvelope(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())))
}
2 changes: 2 additions & 0 deletions action/protocol/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ type (
EnableBlobTransaction bool
SufficentBalanceGuarantee bool
EnableCancunEVM bool
UnfoldContainerBeforeValidate bool
}

// FeatureWithHeightCtx provides feature check functions.
Expand Down Expand Up @@ -299,6 +300,7 @@ func WithFeatureCtx(ctx context.Context) context.Context {
EnableBlobTransaction: g.IsVanuatu(height),
SufficentBalanceGuarantee: g.IsVanuatu(height),
EnableCancunEVM: g.IsVanuatu(height),
UnfoldContainerBeforeValidate: g.IsVanuatu(height),
},
)
}
Expand Down
37 changes: 37 additions & 0 deletions action/rlp_tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/ethereum/go-ethereum/core/types"
"github.com/iotexproject/go-pkgs/crypto"
"github.com/iotexproject/go-pkgs/hash"
"github.com/iotexproject/iotex-address/address"
"github.com/iotexproject/iotex-proto/golang/iotextypes"
"github.com/pkg/errors"
)
Expand Down Expand Up @@ -130,3 +131,39 @@ func ExtractTypeSigPubkey(tx *types.Transaction) (iotextypes.Encoding, []byte, c
pubkey, err = crypto.RecoverPubkey(rawHash[:], sig)
return encoding, sig, pubkey, err
}

func StakingRewardingTxToEnvelope(chainID uint32, tx *types.Transaction) (Envelope, error) {
if tx.To() == nil {
return nil, nil
}
var (
ioAddr, _ = address.FromBytes(tx.To().Bytes())
to = ioAddr.String()
elpBuilder = (&EnvelopeBuilder{}).SetChainID(chainID)
)
if to == address.StakingProtocolAddr {
return elpBuilder.BuildStakingAction(tx)
}
if to == address.RewardingProtocol {
return elpBuilder.BuildRewardingAction(tx)
}
return nil, nil
}

func EthRawToContainer(chainID uint32, rawString string) (*iotextypes.ActionCore, error) {
if strings.HasPrefix(rawString, "0x") || strings.HasPrefix(rawString, "0X") {
rawString = rawString[2:]
}
rawBytes, err := hex.DecodeString(rawString)
if err != nil {
return nil, err
}
return &iotextypes.ActionCore{
ChainID: chainID,
Action: &iotextypes.ActionCore_TxContainer{
TxContainer: &iotextypes.TxContainer{
Raw: rawBytes,
},
},
}, nil
}
16 changes: 3 additions & 13 deletions action/rlp_tx_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -394,20 +394,8 @@ func TestEthTxDecodeVerify(t *testing.T) {
pb[0].Signature = sig

// test tx container
rawBytes, _ := hex.DecodeString(raw)
pb[1] = &iotextypes.Action{
Core: &iotextypes.ActionCore{
Version: 1,
Nonce: tx.Nonce(),
GasLimit: tx.Gas(),
GasPrice: tx.GasPrice().String(),
ChainID: 1,
Action: &iotextypes.ActionCore_TxContainer{
TxContainer: &iotextypes.TxContainer{
Raw: rawBytes,
},
},
},
Core: MustNoErrorV(EthRawToContainer(1, raw)),
SenderPubKey: pubkey.Bytes(),
Signature: sig,
Encoding: iotextypes.Encoding_TX_CONTAINER,
Expand Down Expand Up @@ -435,6 +423,7 @@ func TestEthTxDecodeVerify(t *testing.T) {

// evm tx conversion
if i == 1 {
require.EqualValues(tx.Type()+1, selp.Version())
// tx unfolding
_, ok := selp.Action().(*txContainer)
require.True(ok)
Expand All @@ -444,6 +433,7 @@ func TestEthTxDecodeVerify(t *testing.T) {
require.True(bytes.Equal(sig, selp.signature))
checkSelp(selp, tx, v)
require.Equal(v.encoding, selp.encoding)
require.EqualValues(tx.Type()+1, selp.Version())
// selp converted to actual tx
_, ok = selp.Action().(TxContainer)
require.False(ok)
Expand Down
27 changes: 22 additions & 5 deletions action/sealedenvelope.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ type SealedEnvelope struct {
func (sealed *SealedEnvelope) envelopeHash() (hash.Hash256, error) {
switch sealed.encoding {
case iotextypes.Encoding_TX_CONTAINER:
act, ok := sealed.Action().(*txContainer)
act, ok := sealed.Envelope.(*txContainer)
if !ok {
return hash.ZeroHash256, ErrInvalidAct
}
Expand Down Expand Up @@ -78,7 +78,7 @@ func (sealed *SealedEnvelope) Hash() (hash.Hash256, error) {
func (sealed *SealedEnvelope) calcHash() (hash.Hash256, error) {
switch sealed.encoding {
case iotextypes.Encoding_TX_CONTAINER:
act, ok := sealed.Action().(*txContainer)
act, ok := sealed.Envelope.(*txContainer)
if !ok {
return hash.ZeroHash256, ErrInvalidAct
}
Expand Down Expand Up @@ -160,8 +160,8 @@ func (sealed *SealedEnvelope) loadProto(pbAct *iotextypes.Action, evmID uint32)
return errors.Errorf("invalid signature length = %d, expecting 65", sigSize)
}

var elp Envelope = &envelope{}
if err := elp.LoadProto(pbAct.GetCore()); err != nil {
elp, err := protoToEnvelope(pbAct)
if err != nil {
return err
}
// populate pubkey and signature
Expand All @@ -173,7 +173,7 @@ func (sealed *SealedEnvelope) loadProto(pbAct *iotextypes.Action, evmID uint32)
switch encoding {
case iotextypes.Encoding_TX_CONTAINER:
// verify it is container format
if _, ok := elp.Action().(TxContainer); !ok {
if _, ok := elp.(TxContainer); !ok {
return ErrInvalidAct
}
sealed.evmNetworkID = evmID
Expand Down Expand Up @@ -208,6 +208,23 @@ func (sealed *SealedEnvelope) loadProto(pbAct *iotextypes.Action, evmID uint32)
return nil
}

func protoToEnvelope(pbAct *iotextypes.Action) (Envelope, error) {
var (
elp = &envelope{}
cnt = &txContainer{}
)
if pbAct.GetEncoding() == iotextypes.Encoding_TX_CONTAINER {
if err := cnt.LoadProto(pbAct.GetCore()); err != nil {
return nil, err
}
return cnt, nil
}
if err := elp.LoadProto(pbAct.GetCore()); err != nil {
return nil, err
}
return elp, nil
}

// VerifySignature verifies the action using sender's public key
func (sealed *SealedEnvelope) VerifySignature() error {
if sealed.SrcPubkey() == nil {
Expand Down
Loading

0 comments on commit 1893ab7

Please sign in to comment.