Skip to content

Commit

Permalink
Merge branch 'pacaya_fork_client' of https://github.com/taikoxyz/taik…
Browse files Browse the repository at this point in the history
…o-mono into pacaya_fork_client
  • Loading branch information
RogerLamTd committed Jan 22, 2025
2 parents e522820 + 99f4499 commit 7c46ca6
Show file tree
Hide file tree
Showing 4 changed files with 136 additions and 4 deletions.
10 changes: 10 additions & 0 deletions packages/taiko-client/bindings/encoding/input.go
Original file line number Diff line number Diff line change
Expand Up @@ -702,3 +702,13 @@ func UnpackTxListBytes(txData []byte) ([]byte, error) {

return inputs, nil
}

// EncodeBaseFeeConfig encodes the block.extraData field from the given base fee config.
func EncodeBaseFeeConfig(baseFeeConfig *pacayaBindings.LibSharedDataBaseFeeConfig) [32]byte {
var (
bytes32Value [32]byte
uintValue = new(big.Int).SetUint64(uint64(baseFeeConfig.SharingPctg))
)
copy(bytes32Value[32-len(uintValue.Bytes()):], uintValue.Bytes())
return bytes32Value
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (

"github.com/taikoxyz/taiko-mono/packages/taiko-client/bindings/encoding"
"github.com/taikoxyz/taiko-mono/packages/taiko-client/bindings/metadata"
pacayaBindings "github.com/taikoxyz/taiko-mono/packages/taiko-client/bindings/pacaya"
anchorTxConstructor "github.com/taikoxyz/taiko-mono/packages/taiko-client/driver/anchor_tx_constructor"
"github.com/taikoxyz/taiko-mono/packages/taiko-client/driver/chain_syncer/beaconsync"
txListDecompressor "github.com/taikoxyz/taiko-mono/packages/taiko-client/driver/txlist_decompressor"
Expand Down Expand Up @@ -230,3 +231,117 @@ func (i *BlocksInserterPacaya) InsertBlocks(

return nil
}

// InsertPreconfBlockFromTransactionsBatch inserts a preconf block from transactions batch.
func (i *BlocksInserterPacaya) InsertPreconfBlockFromTransactionsBatch(
ctx context.Context,
executableData *engine.ExecutableData,
anchorBlockID uint64,
anchorStateRoot common.Hash,
anchorInput [32]byte,
signalSlots [][32]byte,
baseFeeConfig *pacayaBindings.LibSharedDataBaseFeeConfig,
) (*types.Header, error) {
parentHeader, err := i.rpc.L2.HeaderByHash(ctx, executableData.ParentHash)
if err != nil {
return nil, fmt.Errorf("failed to fetch parent block: %w", err)
}

if parentHeader.Number.Uint64()+1 != executableData.Number {
return nil, fmt.Errorf("invalid parent hash %s", executableData.ParentHash)
}

baseFee, err := i.rpc.CalculateBaseFee(
ctx,
parentHeader,
new(big.Int).SetUint64(anchorBlockID),
true,
baseFeeConfig,
executableData.Timestamp,
)
if err != nil {
return nil, fmt.Errorf("failed to calculate base fee: %w", err)
}

log.Info(
"L2 baseFee for the next preconfirmation block",
"blockID", executableData.Number,
"baseFee", utils.WeiToGWei(baseFee),
"parentGasUsed", parentHeader.GasUsed,
)
anchorBlockHeader, err := i.rpc.L1.HeaderByNumber(ctx, new(big.Int).SetUint64(anchorBlockID))
if err != nil {
return nil, fmt.Errorf("failed to fetch anchor block: %w", err)
}
if anchorBlockHeader.Root != anchorStateRoot {
return nil, fmt.Errorf("invalid anchor state root %s", anchorStateRoot)
}

// Assemble a TaikoAnchor.anchorV3 transaction
anchorTx, err := i.anchorConstructor.AssembleAnchorV3Tx(
ctx,
new(big.Int).SetUint64(anchorBlockID),
anchorStateRoot,
anchorInput,
parentHeader.GasUsed,
baseFeeConfig,
signalSlots,
new(big.Int).Add(parentHeader.Number, common.Big1),
baseFee,
)
if err != nil {
return nil, fmt.Errorf("failed to create TaikoAnchor.anchorV3 transaction: %w", err)
}
difficulty, err := encoding.CalculatePacayaDifficulty(new(big.Int).SetUint64(executableData.Number))
if err != nil {
return nil, fmt.Errorf("failed to calculate difficulty: %w", err)
}
extraData := encoding.EncodeBaseFeeConfig(baseFeeConfig)

payloadData, err := createPayloadAndSetHead(
ctx,
i.rpc,
&createPayloadAndSetHeadMetaData{
createExecutionPayloadsMetaData: &createExecutionPayloadsMetaData{
BlockID: new(big.Int).SetUint64(executableData.Number),
ExtraData: extraData[:],
SuggestedFeeRecipient: executableData.FeeRecipient,
GasLimit: executableData.GasLimit,
Difficulty: common.BytesToHash(difficulty),
Timestamp: executableData.Timestamp,
ParentHash: executableData.ParentHash,
L1Origin: &rawdb.L1Origin{
BlockID: new(big.Int).SetUint64(executableData.Number),
L2BlockHash: common.Hash{}, // Will be set by taiko-geth.
L1BlockHeight: nil,
L1BlockHash: common.Hash{},
},
Txs: append(
types.Transactions{anchorTx},
i.txListDecompressor.TryDecompress(i.rpc.L2.ChainID, executableData.Transactions[0], true, true)...,
),
Withdrawals: make([]*types.Withdrawal, 0),
BaseFee: baseFee,
},
AnchorBlockID: new(big.Int).SetUint64(anchorBlockID),
AnchorBlockHash: anchorBlockHeader.Hash(),
BaseFeeConfig: baseFeeConfig,
Parent: parentHeader,
},
anchorTx,
)
if err != nil {
return nil, fmt.Errorf("failed to insert new preconfirmation head to L2 execution engine: %w", err)
}

log.Info(
"⏰ New preconfirmation L2 block inserted",
"blockID", executableData.Number,
"hash", payloadData.BlockHash,
"transactions", len(payloadData.Transactions[0]),
"baseFee", utils.WeiToGWei(payloadData.BaseFeePerGas),
"withdrawals", len(payloadData.Withdrawals),
)

return i.rpc.L2.HeaderByHash(ctx, payloadData.BlockHash)
}
12 changes: 9 additions & 3 deletions packages/taiko-client/driver/preconf_blocks/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/ethereum/go-ethereum/log"
"github.com/ethereum/go-ethereum/rlp"
"github.com/labstack/echo/v4"
pacayaBindings "github.com/taikoxyz/taiko-mono/packages/taiko-client/bindings/pacaya"
)

// ValidateSignature validates the signature of the request body.
Expand Down Expand Up @@ -39,9 +40,10 @@ type BuildPreconfBlockRequestBody struct {
// @param anchorBlockID uint64 `_anchorBlockId` parameter of the `anchorV3` transaction in the preconf block
AnchorBlockID uint64 `json:"anchorBlockID"`
// @param anchorStateRoot string `_anchorStateRoot` parameter of the `anchorV3` transaction in the preconf block
AnchorStateRoot common.Hash `json:"anchorStateRoot"`
AnchorInput [32]byte `json:"anchorInput"`
SignalSlots [][32]byte `json:"signalSlots"`
AnchorStateRoot common.Hash `json:"anchorStateRoot"`
AnchorInput [32]byte `json:"anchorInput"`
SignalSlots [][32]byte `json:"signalSlots"`
BaseFeeConfig *pacayaBindings.LibSharedDataBaseFeeConfig `json:"baseFeeConfig"`
}

// BuildPreconfBlockResponseBody represents a response body when handling preconf
Expand Down Expand Up @@ -96,6 +98,9 @@ func (s *PreconfBlockAPIServer) BuildPreconfBlock(c echo.Context) error {
if reqBody.ExecutableData.FeeRecipient == (common.Address{}) {
return s.returnError(c, http.StatusBadRequest, errors.New("empty L2 fee recipient"))
}
if len(reqBody.ExecutableData.Transactions) != 1 {
return s.returnError(c, http.StatusBadRequest, errors.New("only one transaction list is allowed"))
}

// If the `--preconfBlock.signatureCheck` flag is enabled, validate the signature.
if s.checkSig {
Expand Down Expand Up @@ -130,6 +135,7 @@ func (s *PreconfBlockAPIServer) BuildPreconfBlock(c echo.Context) error {
reqBody.AnchorStateRoot,
reqBody.AnchorInput,
reqBody.SignalSlots,
reqBody.BaseFeeConfig,
)
if err != nil {
return s.returnError(c, http.StatusInternalServerError, err)
Expand Down
3 changes: 2 additions & 1 deletion packages/taiko-client/driver/preconf_blocks/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/labstack/echo/v4"
"github.com/labstack/echo/v4/middleware"

pacayaBindings "github.com/taikoxyz/taiko-mono/packages/taiko-client/bindings/pacaya"
txListDecompressor "github.com/taikoxyz/taiko-mono/packages/taiko-client/driver/txlist_decompressor"
"github.com/taikoxyz/taiko-mono/packages/taiko-client/pkg/rpc"
)
Expand All @@ -25,8 +26,8 @@ type preconfBlockChainSyncer interface {
anchorStateRoot common.Hash,
anchorInput [32]byte,
signalSlots [][32]byte,
baseFeeConfig *pacayaBindings.LibSharedDataBaseFeeConfig,
) (*types.Header, error)
RemovePreconfBlocks(ctx context.Context, newLastBlockID uint64) error
}

// @title Taiko Preconfirmation Block Server API
Expand Down

0 comments on commit 7c46ca6

Please sign in to comment.