Skip to content

Commit

Permalink
feat(taiko-client): introduce TaikoDataBlockV2 (#17936)
Browse files Browse the repository at this point in the history
Co-authored-by: maskpp <[email protected]>
  • Loading branch information
YoGhurt111 and mask-pp authored Aug 16, 2024
1 parent 890e4fe commit c608116
Show file tree
Hide file tree
Showing 14 changed files with 169 additions and 33 deletions.
2 changes: 1 addition & 1 deletion packages/taiko-client/bindings/.githead
Original file line number Diff line number Diff line change
@@ -1 +1 @@
920bd6873d3e9e1bbb00751fb9c0056ac85b8554
e46cf294862c293b73b817574669115b85e973a7
15 changes: 15 additions & 0 deletions packages/taiko-client/bindings/encoding/struct.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,21 @@ func BlockVerifiedEventToV2(e *bindings.TaikoL1ClientBlockVerified) *bindings.Ta
}
}

// BlockVerifiedEventToV2 converts a *bindings.TaikoDataBlock to *bindings.TaikoDataBlockV2.
func TaikoDataBlockToV2(b *bindings.TaikoDataBlock) *bindings.TaikoDataBlockV2 {
return &bindings.TaikoDataBlockV2{
MetaHash: b.MetaHash,
AssignedProver: b.AssignedProver,
LivenessBond: b.LivenessBond,
BlockId: b.BlockId,
ProposedAt: b.ProposedAt,
ProposedIn: b.ProposedIn,
NextTransitionId: big.NewInt(int64(b.NextTransitionId)),
LivenessBondReturned: false,
VerifiedTransitionId: big.NewInt(int64(b.VerifiedTransitionId)),
}
}

// BloomToBytes converts a types.Bloom to [8][32]byte slice.
func BloomToBytes(bloom types.Bloom) [8][32]byte {
b := [8][32]byte{}
Expand Down
57 changes: 50 additions & 7 deletions packages/taiko-client/bindings/gen_taiko_l1.go

Large diffs are not rendered by default.

26 changes: 13 additions & 13 deletions packages/taiko-client/bindings/gen_tier_provider.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 9 additions & 1 deletion packages/taiko-client/driver/chain_syncer/beaconsync/syncer.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"fmt"
"math/big"

"github.com/taikoxyz/taiko-mono/packages/taiko-client/bindings"

"github.com/ethereum/go-ethereum/beacon/engine"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/eth/downloader"
Expand Down Expand Up @@ -104,7 +106,13 @@ func (s *Syncer) getBlockPayload(ctx context.Context, blockID uint64) (*engine.E

// If the sync mode is `full`, we need to verify the protocol verified block hash before syncing.
if s.syncMode == downloader.FullSync.String() {
blockInfo, err := s.rpc.GetL2BlockInfo(ctx, new(big.Int).SetUint64(blockID))
blockNum := new(big.Int).SetUint64(blockID)
var blockInfo bindings.TaikoDataBlockV2
if s.state.IsOnTake(blockNum) {
blockInfo, err = s.rpc.GetL2BlockInfoV2(ctx, blockNum)
} else {
blockInfo, err = s.rpc.GetL2BlockInfo(ctx, blockNum)
}
if err != nil {
return nil, err
}
Expand Down
11 changes: 10 additions & 1 deletion packages/taiko-client/driver/chain_syncer/blob/syncer.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (
"net/url"
"time"

"github.com/taikoxyz/taiko-mono/packages/taiko-client/bindings"

"github.com/ethereum/go-ethereum"
"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/beacon/engine"
Expand Down Expand Up @@ -615,7 +617,14 @@ func (s *Syncer) retrievePastBlock(
currentBlockID = 0
}

blockInfo, err := s.rpc.GetL2BlockInfo(ctx, new(big.Int).SetUint64(currentBlockID))
blockNum := new(big.Int).SetUint64(currentBlockID)
var blockInfo bindings.TaikoDataBlockV2
if s.state.IsOnTake(blockNum) {
blockInfo, err = s.rpc.GetL2BlockInfoV2(ctx, blockNum)
} else {
blockInfo, err = s.rpc.GetL2BlockInfo(ctx, blockNum)
}

if err != nil {
return nil, err
}
Expand Down
12 changes: 11 additions & 1 deletion packages/taiko-client/driver/state/l1_current.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"errors"
"math/big"

"github.com/taikoxyz/taiko-mono/packages/taiko-client/bindings"

"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/log"
Expand Down Expand Up @@ -45,7 +47,15 @@ func (s *State) ResetL1Current(ctx context.Context, blockID *big.Int) error {
}

// Fetch the block info from TaikoL1 contract, and set the L1 height.
blockInfo, err := s.rpc.GetL2BlockInfo(ctx, blockID)
var (
blockInfo bindings.TaikoDataBlockV2
err error
)
if s.IsOnTake(blockID) {
blockInfo, err = s.rpc.GetL2BlockInfoV2(ctx, blockID)
} else {
blockInfo, err = s.rpc.GetL2BlockInfo(ctx, blockID)
}
if err != nil {
return err
}
Expand Down
16 changes: 15 additions & 1 deletion packages/taiko-client/driver/state/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"sync"
"sync/atomic"

"github.com/taikoxyz/taiko-mono/packages/taiko-client/bindings/encoding"

"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
Expand All @@ -28,7 +30,8 @@ type State struct {
l1Current atomic.Value // Current L1 block sync cursor

// Constants
GenesisL1Height *big.Int
GenesisL1Height *big.Int
OnTakeForkHeight *big.Int

// RPC clients
rpc *rpc.Client
Expand Down Expand Up @@ -64,6 +67,9 @@ func (s *State) init(ctx context.Context) error {
log.Info("Genesis L1 height", "height", stateVars.A.GenesisHeight)
s.GenesisL1Height = new(big.Int).SetUint64(stateVars.A.GenesisHeight)

s.OnTakeForkHeight = new(big.Int).SetUint64(encoding.GetProtocolConfig(s.rpc.L2.ChainID.Uint64()).OntakeForkHeight)
log.Info("OnTake fork height", "L2 height", s.OnTakeForkHeight)

// Set the L2 head's latest known L1 origin as current L1 sync cursor.
latestL2KnownL1Header, err := s.rpc.LatestL2KnownL1Header(ctx)
if err != nil {
Expand Down Expand Up @@ -232,3 +238,11 @@ func (s *State) GetHeadBlockID() *big.Int {
func (s *State) SubL1HeadsFeed(ch chan *types.Header) event.Subscription {
return s.l1HeadsFeed.Subscribe(ch)
}

// IsOnTake returns whether num is either equal to the ontake block or greater.
func (s *State) IsOnTake(num *big.Int) bool {
if s.OnTakeForkHeight == nil || num == nil {
return false
}
return s.OnTakeForkHeight.Cmp(num) <= 0
}
16 changes: 14 additions & 2 deletions packages/taiko-client/pkg/rpc/methods.go
Original file line number Diff line number Diff line change
Expand Up @@ -420,11 +420,23 @@ func (c *Client) GetProtocolStateVariables(opts *bind.CallOpts) (*struct {
}

// GetL2BlockInfo fetches the L2 block information from the protocol.
func (c *Client) GetL2BlockInfo(ctx context.Context, blockID *big.Int) (bindings.TaikoDataBlock, error) {
func (c *Client) GetL2BlockInfo(ctx context.Context, blockID *big.Int) (bindings.TaikoDataBlockV2, error) {
ctxWithTimeout, cancel := CtxWithTimeoutOrDefault(ctx, defaultTimeout)
defer cancel()

return c.TaikoL1.GetBlock(&bind.CallOpts{Context: ctxWithTimeout}, blockID.Uint64())
blockInfo, err := c.TaikoL1.GetBlock(&bind.CallOpts{Context: ctxWithTimeout}, blockID.Uint64())
if err != nil {
return bindings.TaikoDataBlockV2{}, err
}
return *encoding.TaikoDataBlockToV2(&blockInfo), nil
}

// GetL2BlockInfoV2 fetches the V2 L2 block information from the protocol.
func (c *Client) GetL2BlockInfoV2(ctx context.Context, blockID *big.Int) (bindings.TaikoDataBlockV2, error) {
ctxWithTimeout, cancel := CtxWithTimeoutOrDefault(ctx, defaultTimeout)
defer cancel()

return c.TaikoL1.GetBlockV2(&bind.CallOpts{Context: ctxWithTimeout}, blockID.Uint64())
}

// GetTransition fetches the L2 block's corresponding transition state from the protocol.
Expand Down
2 changes: 1 addition & 1 deletion packages/taiko-client/proposer/transaction_builder/blob.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ func (b *BlobTransactionBuilder) Build(
err error
)
if includeParentMetaHash {
if parentMetaHash, err = getParentMetaHash(ctx, b.rpc); err != nil {
if parentMetaHash, err = getParentMetaHash(ctx, b.rpc, b.chainConfig.OnTakeBlock); err != nil {
return nil, err
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func (b *CalldataTransactionBuilder) Build(
err error
)
if includeParentMetaHash {
if parentMetaHash, err = getParentMetaHash(ctx, b.rpc); err != nil {
if parentMetaHash, err = getParentMetaHash(ctx, b.rpc, b.chainConfig.OnTakeBlock); err != nil {
return nil, err
}
}
Expand Down
21 changes: 19 additions & 2 deletions packages/taiko-client/proposer/transaction_builder/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,40 @@ import (
"context"
"math/big"

"github.com/taikoxyz/taiko-mono/packages/taiko-client/bindings"

"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/common"

"github.com/taikoxyz/taiko-mono/packages/taiko-client/pkg/rpc"
)

// getParentMetaHash returns the meta hash of the parent block of the latest proposed block in protocol.
func getParentMetaHash(ctx context.Context, rpc *rpc.Client) (common.Hash, error) {
func getParentMetaHash(ctx context.Context, rpc *rpc.Client, forkHeight *big.Int) (common.Hash, error) {
state, err := rpc.TaikoL1.State(&bind.CallOpts{Context: ctx})
if err != nil {
return common.Hash{}, err
}

parent, err := rpc.GetL2BlockInfo(ctx, new(big.Int).SetUint64(state.SlotB.NumBlocks-1))
blockNum := new(big.Int).SetUint64(state.SlotB.NumBlocks - 1)
var parent bindings.TaikoDataBlockV2
if isBlockForked(forkHeight, blockNum) {
parent, err = rpc.GetL2BlockInfoV2(ctx, blockNum)
} else {
parent, err = rpc.GetL2BlockInfo(ctx, blockNum)
}
if err != nil {
return common.Hash{}, err
}

return parent.MetaHash, nil
}

// isBlockForked returns whether a fork scheduled at block s is active at the
// given head block.
func isBlockForked(s, head *big.Int) bool {
if s == nil || head == nil {
return false
}
return s.Cmp(head) <= 0
}
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func (s *TransactionBuilderTestSuite) SetupTest() {
}

func (s *TransactionBuilderTestSuite) TestGetParentMetaHash() {
metahash, err := getParentMetaHash(context.Background(), s.RPCClient)
metahash, err := getParentMetaHash(context.Background(), s.RPCClient, s.calldataTxBuilder.chainConfig.OnTakeBlock)
s.Nil(err)
s.NotEmpty(metahash)
}
Expand Down
10 changes: 9 additions & 1 deletion packages/taiko-client/prover/proof_submitter/proof_submitter.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,10 @@ func NewProofSubmitter(

// RequestProof implements the Submitter interface.
func (s *ProofSubmitter) RequestProof(ctx context.Context, meta metadata.TaikoBlockMetaData) error {
var (
blockInfo bindings.TaikoDataBlockV2
)

header, err := s.rpc.WaitL2Header(ctx, meta.GetBlockID())
if err != nil {
return fmt.Errorf("failed to fetch l2 Header, blockID: %d, error: %w", meta.GetBlockID(), err)
Expand All @@ -103,7 +107,11 @@ func (s *ProofSubmitter) RequestProof(ctx context.Context, meta metadata.TaikoBl
return fmt.Errorf("failed to get the L2 parent block by hash (%s): %w", header.ParentHash, err)
}

blockInfo, err := s.rpc.GetL2BlockInfo(ctx, meta.GetBlockID())
if meta.IsOntakeBlock() {
blockInfo, err = s.rpc.GetL2BlockInfoV2(ctx, meta.GetBlockID())
} else {
blockInfo, err = s.rpc.GetL2BlockInfo(ctx, meta.GetBlockID())
}
if err != nil {
return err
}
Expand Down

0 comments on commit c608116

Please sign in to comment.