Skip to content

Commit

Permalink
Blob support fix without type (#52)
Browse files Browse the repository at this point in the history
* build only blob tx

* more logs

* sending blob hashes for eth_estimateGas

* Compering blobs and calldata costs
  • Loading branch information
mskrzypkows authored Jan 8, 2025
1 parent 2781634 commit a68af76
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 21 deletions.
91 changes: 70 additions & 21 deletions packages/taiko-client/proposer/proposer.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package proposer

import (
"context"
"crypto/sha256"
"errors"
"fmt"
"math/big"
Expand All @@ -16,6 +17,7 @@ import (
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/crypto/kzg4844"
"github.com/ethereum/go-ethereum/log"
"github.com/ethereum/go-ethereum/rlp"
"github.com/urfave/cli/v2"
Expand Down Expand Up @@ -511,7 +513,7 @@ func (p *Proposer) buildCheaperOnTakeTransaction(ctx context.Context,
return nil, nil, err
}
} else {
cost, err = p.getTransactionCost(txCallData)
cost, err = p.getTransactionCost(txCallData, nil)
if err != nil {
return nil, nil, err
}
Expand All @@ -536,25 +538,23 @@ func (p *Proposer) chooseCheaperTransaction(
txCallData *txmgr.TxCandidate,
txBlob *txmgr.TxCandidate,
) (*txmgr.TxCandidate, *big.Int, error) {
calldataTxCost, err := p.getTransactionCost(txCallData)
log.Debug("Choosing cheaper transaction")
calldataTxCost, err := p.getTransactionCost(txCallData, nil)
if err != nil {
return nil, nil, err
}

blobTxCost, err := p.getTransactionCost(txBlob)
totalBlobCost, err := p.getBlobTxCost(txBlob)
if err != nil {
return nil, nil, err
}
blobCost, err := p.getBlobCost(txBlob.Blobs)
if err != nil {
return nil, nil, err
}
totalBlobCost := new(big.Int).Add(blobTxCost, blobCost)

if calldataTxCost.Cmp(totalBlobCost) > 0 {
log.Info("Using blob tx", "totalBlobCost", totalBlobCost)
return txBlob, totalBlobCost, nil
}

log.Info("Using calldata tx", "calldataTxCost", calldataTxCost)
return txCallData, calldataTxCost, nil
}

Expand Down Expand Up @@ -674,34 +674,83 @@ func get75PercentOf(num *big.Int) *big.Int {
return new(big.Int).Div(result, big.NewInt(4))
}

func (p *Proposer) getTransactionCost(txCandidate *txmgr.TxCandidate) (*big.Int, error) {
func (p *Proposer) getBlobTxCost(txCandidate *txmgr.TxCandidate) (*big.Int, error) {
// Get current blob base fee
blobBaseFee, err := p.rpc.L1.BlobBaseFee(p.ctx)
if err != nil {
return nil, err
}

blobTxCost, err := p.getTransactionCost(txCandidate, blobBaseFee)
if err != nil {
return nil, err
}
blobCost, err := p.getBlobCost(txCandidate.Blobs, blobBaseFee)
if err != nil {
return nil, err
}
return new(big.Int).Add(blobTxCost, blobCost), nil
}

func (p *Proposer) getTransactionCost(txCandidate *txmgr.TxCandidate, blobBaseFee *big.Int) (*big.Int, error) {
log.Debug("getTransactionCost", "blobBaseFee", blobBaseFee)
// Get the current L1 gas price
gasPrice, err := p.rpc.L1.SuggestGasPrice(p.ctx)
if err != nil {
return nil, fmt.Errorf("getTransactionCost: failed to get gas price: %w", err)
}

estimatedGasUsage, err := p.rpc.L1.EstimateGas(p.ctx, ethereum.CallMsg{
From: p.proposerAddress,
To: txCandidate.To,
Data: txCandidate.TxData,
Gas: 0,
Value: nil,
})
var msg ethereum.CallMsg
if blobBaseFee != nil {
blobHashes, err := calculateBlobHashes(txCandidate.Blobs)
if err != nil {
return nil, fmt.Errorf("getTransactionCost: failed to calculate blob hashes: %w", err)
}
msg = ethereum.CallMsg{
From: p.proposerAddress,
To: txCandidate.To,
Data: txCandidate.TxData,
Gas: 0,
Value: nil,
BlobGasFeeCap: blobBaseFee,
BlobHashes: blobHashes,
}
} else {
msg = ethereum.CallMsg{
From: p.proposerAddress,
To: txCandidate.To,
Data: txCandidate.TxData,
Gas: 0,
Value: nil,
}
}

estimatedGasUsage, err := p.rpc.L1.EstimateGas(p.ctx, msg)
if err != nil {
return nil, fmt.Errorf("getTransactionCost: failed to estimate gas: %w", err)
}

log.Debug("getTransactionCost", "estimatedGasUsage", estimatedGasUsage)

return new(big.Int).Mul(gasPrice, new(big.Int).SetUint64(estimatedGasUsage)), nil
}

func (p *Proposer) getBlobCost(blobs []*eth.Blob) (*big.Int, error) {
// Get current blob base fee
blobBaseFee, err := p.rpc.L1.BlobBaseFee(p.ctx)
if err != nil {
return nil, err
func calculateBlobHashes(blobs []*eth.Blob) ([]common.Hash, error) {
log.Debug("Calculating blob hashes")

var blobHashes []common.Hash
for _, blob := range blobs {
commitment, err := blob.ComputeKZGCommitment()
if err != nil {
return nil, err
}
blobHash := kzg4844.CalcBlobHashV1(sha256.New(), &commitment)
blobHashes = append(blobHashes, blobHash)
}
return blobHashes, nil
}

func (p *Proposer) getBlobCost(blobs []*eth.Blob, blobBaseFee *big.Int) (*big.Int, error) {
// Each blob costs 1 blob gas
totalBlobGas := uint64(len(blobs))

Expand Down
2 changes: 2 additions & 0 deletions packages/taiko-client/proposer/transaction_builder/blob.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/crypto/kzg4844"
"github.com/ethereum/go-ethereum/log"

"github.com/taikoxyz/taiko-mono/packages/taiko-client/bindings/encoding"
"github.com/taikoxyz/taiko-mono/packages/taiko-client/pkg/config"
Expand Down Expand Up @@ -144,6 +145,7 @@ func (b *BlobTransactionBuilder) BuildOntake(
ctx context.Context,
txListBytesArray [][]byte,
) (*txmgr.TxCandidate, error) {
log.Debug("Building blob tx ontake")
// Check if the current L2 chain is after ontake fork.
state, err := rpc.GetProtocolStateVariables(b.rpc.TaikoL1, &bind.CallOpts{Context: ctx})
if err != nil {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/log"

"github.com/taikoxyz/taiko-mono/packages/taiko-client/bindings/encoding"
"github.com/taikoxyz/taiko-mono/packages/taiko-client/pkg/config"
Expand Down Expand Up @@ -134,6 +135,7 @@ func (b *CalldataTransactionBuilder) BuildOntake(
ctx context.Context,
txListBytesArray [][]byte,
) (*txmgr.TxCandidate, error) {
log.Debug("Building calldata tx ontake")
// Check if the current L2 chain is after ontake fork.
state, err := rpc.GetProtocolStateVariables(b.rpc.TaikoL1, &bind.CallOpts{Context: ctx})
if err != nil {
Expand Down

0 comments on commit a68af76

Please sign in to comment.