Skip to content

Commit

Permalink
allow setting calldata for eoatx scenario
Browse files Browse the repository at this point in the history
  • Loading branch information
pk910 committed Dec 10, 2024
1 parent 5ce5b9b commit 43a7d37
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 12 deletions.
19 changes: 18 additions & 1 deletion scenarios/eoatx/eoatx.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"crypto/rand"
"fmt"
"math/big"
"strings"
"sync"
"time"

Expand All @@ -28,7 +29,9 @@ type ScenarioOptions struct {
Rebroadcast uint64
BaseFee uint64
TipFee uint64
GasLimit uint64
Amount uint64
Data string
RandomAmount bool
RandomTarget bool
}
Expand Down Expand Up @@ -56,7 +59,9 @@ func (s *Scenario) Flags(flags *pflag.FlagSet) error {
flags.Uint64Var(&s.options.Rebroadcast, "rebroadcast", 120, "Number of seconds to wait before re-broadcasting a transaction")
flags.Uint64Var(&s.options.BaseFee, "basefee", 20, "Max fee per gas to use in transfer transactions (in gwei)")
flags.Uint64Var(&s.options.TipFee, "tipfee", 2, "Max tip per gas to use in transfer transactions (in gwei)")
flags.Uint64Var(&s.options.GasLimit, "gaslimit", 21000, "Gas limit to use in transactions")
flags.Uint64Var(&s.options.Amount, "amount", 20, "Transfer amount per transaction (in gwei)")
flags.StringVar(&s.options.Data, "data", "", "Transaction call data to send")
flags.BoolVar(&s.options.RandomAmount, "random-amount", false, "Use random amounts for transactions (with --amount as limit)")
flags.BoolVar(&s.options.RandomTarget, "random-target", false, "Use random to addresses for transactions")
return nil
Expand Down Expand Up @@ -203,12 +208,24 @@ func (s *Scenario) sendTx(txIdx uint64) (*types.Transaction, *txbuilder.Client,
toAddr = common.Address(addrBytes)
}

txCallData := []byte{}

if s.options.Data != "" {
dataBytes, err := txbuilder.ParseBlobRefsBytes(strings.Split(s.options.Data, ","), nil)
if err != nil {
return nil, nil, err
}

txCallData = dataBytes
}

txData, err := txbuilder.DynFeeTx(&txbuilder.TxMetadata{
GasFeeCap: uint256.MustFromBig(feeCap),
GasTipCap: uint256.MustFromBig(tipCap),
Gas: 21000,
Gas: s.options.GasLimit,
To: &toAddr,
Value: amount,
Data: txCallData,
})
if err != nil {
return nil, nil, err
Expand Down
34 changes: 23 additions & 11 deletions txbuilder/blobtx.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func BuildBlobTx(txData *TxMetadata, blobRefs [][]string) (*types.BlobTx, error)
return &tx, nil
}

func parseBlobRefs(tx *types.BlobTx, blobRefs []string) error {
func ParseBlobRefsBytes(blobRefs []string, tx *types.BlobTx) ([]byte, error) {
var err error
var blobBytes []byte

Expand All @@ -61,20 +61,20 @@ func parseBlobRefs(tx *types.BlobTx, blobRefs []string) error {
case "file":
blobRefBytes, err = os.ReadFile(strings.Join(refParts[1:], ":"))
if err != nil {
return err
return nil, err
}
case "url":
blobRefBytes, err = loadUrlRef(strings.Join(refParts[1:], ":"))
if err != nil {
return err
return nil, err
}
case "repeat":
if len(refParts) != 3 {
return fmt.Errorf("invalid repeat ref format: %v", blobRef)
return nil, fmt.Errorf("invalid repeat ref format: %v", blobRef)
}
repeatCount, err := strconv.Atoi(refParts[2])
if err != nil {
return fmt.Errorf("invalid repeat count: %v", refParts[2])
return nil, fmt.Errorf("invalid repeat count: %v", refParts[2])
}
repeatBytes := common.FromHex(refParts[1])
repeatBytesLen := len(repeatBytes)
Expand All @@ -88,36 +88,48 @@ func parseBlobRefs(tx *types.BlobTx, blobRefs []string) error {
var err error
blobLen, err = strconv.Atoi(refParts[2])
if err != nil {
return fmt.Errorf("invalid repeat count: %v", refParts[2])
return nil, fmt.Errorf("invalid repeat count: %v", refParts[2])
}
} else {
blobLen = mathRand.Intn((params.BlobTxFieldElementsPerBlob * (params.BlobTxBytesPerFieldElement - 1)) - len(blobBytes))
}
blobRefBytes, err = randomBlobData(blobLen)
if err != nil {
return err
return nil, err
}
case "copy":
if tx == nil {
return nil, fmt.Errorf("copy ref not supported for non blob transactions: %v", blobRef)
}
if len(refParts) != 2 {
return fmt.Errorf("invalid copy ref format: %v", blobRef)
return nil, fmt.Errorf("invalid copy ref format: %v", blobRef)
}
copyIdx, err := strconv.Atoi(refParts[1])
if err != nil {
return fmt.Errorf("invalid copy index: %v", refParts[1])
return nil, fmt.Errorf("invalid copy index: %v", refParts[1])
}
if copyIdx >= len(tx.Sidecar.Blobs) {
return fmt.Errorf("invalid copy index: %v must be smaller than current blob index", refParts[1])
return nil, fmt.Errorf("invalid copy index: %v must be smaller than current blob index", refParts[1])
}
blobRefBytes = tx.Sidecar.Blobs[copyIdx][:]
}
}

if blobRefBytes == nil {
return fmt.Errorf("unknown blob ref: %v", blobRef)
return nil, fmt.Errorf("unknown blob ref: %v", blobRef)
}
blobBytes = append(blobBytes, blobRefBytes...)
}

return blobBytes, nil
}

func parseBlobRefs(tx *types.BlobTx, blobRefs []string) error {
blobBytes, err := ParseBlobRefsBytes(blobRefs, tx)
if err != nil {
return err
}

blobCommitment, err := EncodeBlob(blobBytes)
if err != nil {
return fmt.Errorf("invalid blob: %w", err)
Expand Down

0 comments on commit 43a7d37

Please sign in to comment.