Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(protocol): introduce AssembleAnchorV2Tx method in AnchorTxConstructor #17850

Merged
merged 1 commit into from
Jul 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,45 @@ func (c *AnchorTxConstructor) AssembleAnchorTx(
return c.rpc.TaikoL2.Anchor(opts, l1Hash, l1Header.Root, l1Height.Uint64(), uint32(parentGasUsed))
}

// AssembleAnchorV2Tx assembles a signed TaikoL2.anchorV2 transaction.
func (c *AnchorTxConstructor) AssembleAnchorV2Tx(
ctx context.Context,
// Parameters of the TaikoL2.anchorV2 transaction.
anchorBlockID *big.Int,
parentGasUsed uint64,
anchorStateRoot common.Hash,
blockGasIssuance uint32,
basefeeAdjustmentQuotient uint8,
// Height of the L2 block which including the TaikoL2.anchorV2 transaction.
l2Height *big.Int,
baseFee *big.Int,
) (*types.Transaction, error) {
opts, err := c.transactOpts(ctx, l2Height, baseFee)
if err != nil {
return nil, err
}

log.Info(
"AnchorV2 arguments",
"l2Height", l2Height,
"anchorBlockId", anchorBlockID,
"parentGasUsed", parentGasUsed,
"anchorStateRoot", anchorStateRoot,
"blockGasIssuance", blockGasIssuance,
"basefeeAdjustmentQuotient", basefeeAdjustmentQuotient,
"baseFee", utils.WeiToGWei(baseFee),
)

return c.rpc.TaikoL2.AnchorV2(
opts,
anchorBlockID.Uint64(),
anchorStateRoot,
uint32(parentGasUsed),
blockGasIssuance,
basefeeAdjustmentQuotient,
)
}

// transactOpts is a utility method to create some transact options of the anchor transaction in given L2 block with
// golden touch account's private key.
func (c *AnchorTxConstructor) transactOpts(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,21 @@ func (s *AnchorTxConstructorTestSuite) TestAssembleAnchorTx() {
s.NotNil(tx)
}

func (s *AnchorTxConstructorTestSuite) TestAssembleAnchorV2Tx() {
tx, err := s.c.AssembleAnchorV2Tx(
context.Background(),
s.l1Height,
1024,
s.l1Hash,
20_000_000,
8,
common.Big1,
common.Big256,
)
s.Nil(err)
s.NotNil(tx)
}

func (s *AnchorTxConstructorTestSuite) TestNewAnchorTransactor() {
goldenTouchAddress, err := s.RPCClient.TaikoL2.GOLDENTOUCHADDRESS(nil)
s.Nil(err)
Expand Down
93 changes: 70 additions & 23 deletions packages/taiko-client/driver/chain_syncer/blob/syncer.go
Original file line number Diff line number Diff line change
Expand Up @@ -325,23 +325,83 @@ func (s *Syncer) insertNewHead(
"l1Origin", l1Origin,
)

// Insert a TaikoL2.anchor transaction at transactions list head
var txList []*types.Transaction
// Insert a TaikoL2.anchor / TaikoL2.anchorV2 transaction at transactions list head
var (
txList []*types.Transaction
anchorTx *types.Transaction
baseFeeInfo struct {
Basefee *big.Int
GasExcess uint64
}
err error
)
if len(txListBytes) != 0 {
if err := rlp.DecodeBytes(txListBytes, &txList); err != nil {
log.Error("Invalid txList bytes", "blockID", meta.GetBlockID())
return nil, err
}
}

// Get L2 baseFee
baseFeeInfo, err := s.rpc.TaikoL2.GetBasefee(
&bind.CallOpts{BlockNumber: parent.Number, Context: ctx},
meta.GetRawBlockHeight().Uint64()-1,
uint32(parent.GasUsed),
)
if err != nil {
return nil, fmt.Errorf("failed to get L2 baseFee: %w", encoding.TryParsingCustomError(err))
if !meta.IsOntakeBlock() {
// Get L2 baseFee
baseFeeInfo, err = s.rpc.TaikoL2.GetBasefee(
&bind.CallOpts{BlockNumber: parent.Number, Context: ctx},
meta.GetRawBlockHeight().Uint64()-1,
uint32(parent.GasUsed),
)
if err != nil {
return nil, fmt.Errorf("failed to get L2 baseFee: %w", encoding.TryParsingCustomError(err))
}

// Assemble a TaikoL2.anchor transaction
anchorTx, err = s.anchorConstructor.AssembleAnchorTx(
ctx,
new(big.Int).SetUint64(meta.GetAnchorBlockID()),
meta.GetAnchorBlockHash(),
new(big.Int).Add(parent.Number, common.Big1),
baseFeeInfo.Basefee,
parent.GasUsed,
)
if err != nil {
return nil, fmt.Errorf("failed to create TaikoL2.anchor transaction: %w", err)
}
} else {
gasExcess, err := s.rpc.TaikoL2.GasExcess(&bind.CallOpts{
BlockNumber: parent.Number, Context: ctx,
})
if err != nil {
return nil, fmt.Errorf("failed to fetch gas excess: %w", err)
}
// Get L2 baseFee
baseFeeInfo, err = s.rpc.TaikoL2.CalculateBaseFee(
&bind.CallOpts{BlockNumber: parent.Number, Context: ctx},
meta.GetBlockGasIssuance(),
meta.GetBasefeeAdjustmentQuotient(),
gasExcess,
uint32(parent.GasUsed),
)
if err != nil {
return nil, fmt.Errorf("failed to get L2 baseFee: %w", encoding.TryParsingCustomError(err))
}

// Assemble a TaikoL2.anchorV2 transaction
anchorBlockHeader, err := s.rpc.L1.HeaderByHash(ctx, meta.GetAnchorBlockHash())
if err != nil {
return nil, fmt.Errorf("failed to fetch anchor block: %w", err)
}
anchorTx, err = s.anchorConstructor.AssembleAnchorV2Tx(
ctx,
new(big.Int).SetUint64(meta.GetAnchorBlockID()),
parent.GasUsed,
anchorBlockHeader.Root,
meta.GetBlockGasIssuance(),
meta.GetBasefeeAdjustmentQuotient(),
new(big.Int).Add(parent.Number, common.Big1),
baseFeeInfo.Basefee,
)
if err != nil {
return nil, fmt.Errorf("failed to create TaikoL2.anchorV2 transaction: %w", err)
}
}

log.Info(
Expand All @@ -352,19 +412,6 @@ func (s *Syncer) insertNewHead(
"parentGasUsed", parent.GasUsed,
)

// Assemble a TaikoL2.anchor transaction
anchorTx, err := s.anchorConstructor.AssembleAnchorTx(
ctx,
new(big.Int).SetUint64(meta.GetAnchorBlockID()),
meta.GetAnchorBlockHash(),
new(big.Int).Add(parent.Number, common.Big1),
baseFeeInfo.Basefee,
parent.GasUsed,
)
if err != nil {
return nil, fmt.Errorf("failed to create TaikoL2.anchor transaction: %w", err)
}

// Insert the anchor transaction at the head of the transactions list
txList = append([]*types.Transaction{anchorTx}, txList...)
if txListBytes, err = rlp.EncodeToBytes(txList); err != nil {
Expand Down
Loading