Skip to content

Commit

Permalink
feat(taiko-client): improve BlobTransactionBuilder to support `Blob…
Browse files Browse the repository at this point in the history
…TxListOffset`
  • Loading branch information
davidtaikocha authored and YoGhurt111 committed Oct 16, 2024
1 parent 8c9bdca commit 6138b8b
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 8 deletions.
50 changes: 42 additions & 8 deletions packages/taiko-client/proposer/transaction_builder/blob.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,40 +154,74 @@ func (b *BlobTransactionBuilder) BuildOntake(
return nil, fmt.Errorf("ontake transaction builder is not supported before ontake fork")
}

// ABI encode the TaikoL1.proposeBlocksV2 / ProverSet.proposeBlocksV2 parameters.
return b.buildOntake(txListBytesArray)
}

// buildOntake is the inner function to build the real transactions after ontake fork.
func (b *BlobTransactionBuilder) buildOntake(txListBytesArray [][]byte) (*txmgr.TxCandidate, error) {
var (
to = &b.taikoL1Address
data []byte
blobs []*eth.Blob
encodedParamsArray [][]byte
cachedBlobOffset int
cachedBlobData []byte
err error
)
if b.proverSetAddress != rpc.ZeroAddress {
to = &b.proverSetAddress
}

for i := range txListBytesArray {
var blob = &eth.Blob{}
if err := blob.FromData(txListBytesArray[i]); err != nil {
return nil, err
offset := cachedBlobOffset

// Check if the current tx list bytes can be fit into the cached blob data,
// if the current tx list bytes can be fit into the cached blob data, then append it.
cachedBlobReused := (cachedBlobOffset + len(txListBytesArray[i])) <= eth.MaxBlobDataSize

if cachedBlobReused {
cachedBlobData = append(cachedBlobData, txListBytesArray[i]...)
cachedBlobOffset += len(txListBytesArray[i])
} else {
// Otherwise reset the cached blob data and offset.
var blob = &eth.Blob{}
if err := blob.FromData(cachedBlobData); err != nil {
return nil, err
}

blobs = append(blobs, blob)
cachedBlobData = txListBytesArray[i]
cachedBlobOffset = 0
offset = 0
}

blobs = append(blobs, blob)

// ABI encode the TaikoL1.proposeBlocksV2 / ProverSet.proposeBlocksV2 parameters.
encodedParams, err := encoding.EncodeBlockParamsOntake(&encoding.BlockParamsV2{
Coinbase: b.l2SuggestedFeeRecipient,
ParentMetaHash: [32]byte{},
AnchorBlockId: 0,
Timestamp: 0,
BlobTxListOffset: 0,
BlobTxListOffset: uint32(offset),
BlobTxListLength: uint32(len(txListBytesArray[i])),
BlobIndex: uint8(i),
BlobIndex: uint8(len(blobs)),
})
if err != nil {
return nil, err
}

encodedParamsArray = append(encodedParamsArray, encodedParams)

// If the current tx list bytes is the last one, then append the cached blob data to the blobs list.
if i == len(txListBytesArray)-1 {
var blob = &eth.Blob{}
if err := blob.FromData(txListBytesArray[i]); err != nil {
return nil, err
}

blobs = append(blobs, blob)
}
}

txListArray := make([][]byte, len(encodedParamsArray))
if b.proverSetAddress != rpc.ZeroAddress {
data, err = encoding.ProverSetABI.Pack("proposeBlocksV2", encodedParamsArray, txListArray)
Expand Down
27 changes: 27 additions & 0 deletions packages/taiko-client/proposer/transaction_builder/blob_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package builder

import (
"github.com/ethereum-optimism/optimism/op-service/eth"

"github.com/taikoxyz/taiko-mono/packages/taiko-client/internal/testutils"
)

func (s *TransactionBuilderTestSuite) TestBuildBlob() {
tx, err := s.blobTxBuiler.buildOntake([][]byte{{1}})
s.Nil(err)
s.Equal(1, len(tx.Blobs))

tx, err = s.blobTxBuiler.buildOntake([][]byte{{1}, {2}})
s.Nil(err)
s.Equal(1, len(tx.Blobs))

tx, err = s.blobTxBuiler.buildOntake([][]byte{testutils.RandomBytes(eth.MaxBlobDataSize), {2}})
s.Nil(err)
s.Equal(2, len(tx.Blobs))

tx, err = s.blobTxBuiler.buildOntake([][]byte{
testutils.RandomBytes(eth.MaxBlobDataSize), {2}, {3}, testutils.RandomBytes(eth.MaxBlobDataSize)},
)
s.Nil(err)
s.Equal(3, len(tx.Blobs))
}

0 comments on commit 6138b8b

Please sign in to comment.