Skip to content

Commit

Permalink
calldata tx builder
Browse files Browse the repository at this point in the history
  • Loading branch information
cyberhorsey committed Jul 11, 2024
1 parent 2949acd commit 6e52e4c
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 25 deletions.
47 changes: 41 additions & 6 deletions packages/taiko-client/preconfapi/builder/calldata.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@ package builder

import (
"context"
"math/big"

"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
"github.com/taikoxyz/taiko-mono/packages/taiko-client/bindings/encoding"
"github.com/taikoxyz/taiko-mono/packages/taiko-client/internal/utils"
)

// CalldataTransactionBuilder is responsible for building a TaikoL1.proposeBlock transaction with txList
Expand All @@ -29,11 +32,43 @@ func NewCalldataTransactionBuilder(
// return an unsigned transaction, intended for preconfirmations.
func (b *CalldataTransactionBuilder) BuildUnsigned(
_ context.Context,
_ []byte,
_ uint32,
_ uint64,
_ common.Address,
_ [32]byte,
txListBytes []byte,
l1StateBlockNumber uint32,
timestamp uint64,
coinbase common.Address,
extraData [32]byte,
) (*types.Transaction, error) {
return &types.Transaction{}, nil
compressedTxListBytes, err := utils.Compress(txListBytes)
if err != nil {
return nil, err
}

// ABI encode the TaikoL1.proposeBlock parameters.
encodedParams, err := encoding.EncodeBlockParams(&encoding.BlockParams{
ExtraData: extraData,
Coinbase: coinbase,
Signature: []byte{}, // no longer checked
L1StateBlockNumber: l1StateBlockNumber,
Timestamp: timestamp,
})
if err != nil {
return nil, err
}

data, err := encoding.TaikoL1ABI.Pack("proposeBlock", encodedParams, compressedTxListBytes)
if err != nil {
return nil, err
}

// Create the transaction
tx := types.NewTransaction(
0,
b.taikoL1Address,
nil,
b.gasLimit,
big.NewInt(0),
data,
)

return tx, nil
}
24 changes: 11 additions & 13 deletions packages/taiko-client/preconfapi/preconfapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,21 +29,19 @@ func (p *PreconfAPI) InitFromCli(ctx context.Context, c *cli.Context) error {
}

func (p *PreconfAPI) InitFromConfig(_ context.Context, cfg *Config) (err error) {
var txBuilder builder.TxBuilder
if cfg.BlobAllowed {
txBuilder = builder.NewBlobTransactionBuilder(
cfg.TaikoL1Address,
cfg.ProposeBlockTxGasLimit,
)
} else {
txBuilder = builder.NewCalldataTransactionBuilder(
cfg.TaikoL1Address,
cfg.ProposeBlockTxGasLimit,
)
}
txBuilders := make(map[string]builder.TxBuilder)
txBuilders["blob"] = builder.NewBlobTransactionBuilder(
cfg.TaikoL1Address,
cfg.ProposeBlockTxGasLimit,
)

txBuilders["calldata"] = builder.NewCalldataTransactionBuilder(
cfg.TaikoL1Address,
cfg.ProposeBlockTxGasLimit,
)

if p.server, err = server.New(&server.NewPreconfAPIServerOpts{
TxBuilder: txBuilder,
TxBuilders: txBuilders,
}); err != nil {
return err
}
Expand Down
9 changes: 8 additions & 1 deletion packages/taiko-client/preconfapi/server/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ type buildBlockRequest struct {
SignedTransactions []string `json:"signedTransactions"`
Coinbase string `json:"coinbase"`
ExtraData string `json:"extraData"`
CalldataOrBlob string `json:"calldataOrBlob"`
}

type buildBlockResponse struct {
Expand All @@ -58,7 +59,13 @@ func (s *PreconfAPIServer) BuildBlock(c echo.Context) error {
return c.JSON(http.StatusUnprocessableEntity, err)
}

tx, err := s.txBuilder.BuildUnsigned(
// default to blob
t := req.CalldataOrBlob
if t == "" {
t = "blob"
}

tx, err := s.txBuilders[t].BuildUnsigned(
c.Request().Context(),
txListBytes,
req.L1StateBlockNumber,
Expand Down
10 changes: 5 additions & 5 deletions packages/taiko-client/preconfapi/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,20 +23,20 @@ import (
// @license.url https://github.com/taikoxyz/taiko-mono/blob/main/LICENSE.md
// PreconfAPIServer represents a proposer server instance.
type PreconfAPIServer struct {
echo *echo.Echo
txBuilder builder.TxBuilder
echo *echo.Echo
txBuilders map[string]builder.TxBuilder // calldata or blob map to txbuilder type
}

// NewPreconfAPIServerOpts contains all configurations for creating a prover server instance.
type NewPreconfAPIServerOpts struct {
TxBuilder builder.TxBuilder
TxBuilders map[string]builder.TxBuilder
}

// New creates a new prover server instance.
func New(opts *NewPreconfAPIServerOpts) (*PreconfAPIServer, error) {
srv := &PreconfAPIServer{
echo: echo.New(),
txBuilder: opts.TxBuilder,
echo: echo.New(),
txBuilders: opts.TxBuilders,
}

srv.echo.HideBanner = true
Expand Down

0 comments on commit 6e52e4c

Please sign in to comment.