Skip to content

Commit

Permalink
feat: update L1Origin
Browse files Browse the repository at this point in the history
  • Loading branch information
davidtaikocha committed Jan 22, 2025
1 parent db376ba commit 83b1591
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 18 deletions.
26 changes: 18 additions & 8 deletions consensus/taiko/consensus.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,12 @@ import (

"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/consensus"
"github.com/ethereum/go-ethereum/core/rawdb"
"github.com/ethereum/go-ethereum/core/state"
"github.com/ethereum/go-ethereum/core/tracing"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/ethdb"
"github.com/ethereum/go-ethereum/log"
"github.com/ethereum/go-ethereum/params"
"github.com/ethereum/go-ethereum/rpc"
Expand Down Expand Up @@ -44,11 +46,12 @@ var (
type Taiko struct {
chainConfig *params.ChainConfig
taikoL2Address common.Address
chainDB ethdb.Database
}

var _ = new(Taiko)

func New(chainConfig *params.ChainConfig) *Taiko {
func New(chainConfig *params.ChainConfig, chainDB ethdb.Database) *Taiko {
taikoL2AddressPrefix := strings.TrimPrefix(chainConfig.ChainID.String(), "0")

return &Taiko{
Expand All @@ -59,6 +62,7 @@ func New(chainConfig *params.ChainConfig) *Taiko {
strings.Repeat("0", common.AddressLength*2-len(taikoL2AddressPrefix)-len(TaikoL2AddressSuffix)) +
TaikoL2AddressSuffix,
),
chainDB: chainDB,
}
}

Expand All @@ -85,7 +89,7 @@ func (t *Taiko) VerifyHeader(chain consensus.ChainHeaderReader, header *types.He
return consensus.ErrUnknownAncestor
}
// Sanity checks passed, do a proper verification
return t.verifyHeader(chain, header, parent, time.Now().Unix())
return t.verifyHeader(header, parent, time.Now().Unix())
}

// VerifyHeaders is similar to VerifyHeader, but verifies a batch of headers
Expand All @@ -112,7 +116,7 @@ func (t *Taiko) VerifyHeaders(chain consensus.ChainHeaderReader, headers []*type
if parent == nil {
err = consensus.ErrUnknownAncestor
} else {
err = t.verifyHeader(chain, header, parent, unixNow)
err = t.verifyHeader(header, parent, unixNow)
}
select {
case <-abort:
Expand All @@ -124,11 +128,7 @@ func (t *Taiko) VerifyHeaders(chain consensus.ChainHeaderReader, headers []*type
return abort, results
}

func (t *Taiko) verifyHeader(chain consensus.ChainHeaderReader, header, parent *types.Header, unixNow int64) error {
if header.Time > uint64(unixNow) {
return consensus.ErrFutureBlock
}

func (t *Taiko) verifyHeader(header, parent *types.Header, unixNow int64) error {
// Ensure that the header's extra-data section is of a reasonable size (<= 32 bytes)
if uint64(len(header.Extra)) > params.MaximumExtraDataSize {
return fmt.Errorf("extra-data too long: %d > %d", len(header.Extra), params.MaximumExtraDataSize)
Expand Down Expand Up @@ -174,6 +174,16 @@ func (t *Taiko) verifyHeader(chain consensus.ChainHeaderReader, header, parent *
return ErrEmptyWithdrawalsHash
}

l1Origin, err := rawdb.ReadL1Origin(t.chainDB, header.Number)
if err != nil {
return err
}

// If the current block is not a preconfirmation block, then check the timestamp.
if l1Origin != nil && !l1Origin.IsPreconfBlock() && header.Time > uint64(unixNow) {
return consensus.ErrFutureBlock
}

return nil
}

Expand Down
2 changes: 1 addition & 1 deletion consensus/taiko/consensus_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func init() {
config.ArrowGlacierBlock = nil
config.Ethash = nil
config.Taiko = true
testEngine = taiko.New(config)
testEngine = taiko.New(config, rawdb.NewMemoryDatabase())

taikoL2AddressPrefix := strings.TrimPrefix(config.ChainID.String(), "0")

Expand Down
8 changes: 4 additions & 4 deletions core/rawdb/gen_taiko_l1_origin.go

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

9 changes: 7 additions & 2 deletions core/rawdb/taiko_l1_origin.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,20 @@ func l1OriginKey(blockID *big.Int) []byte {
type L1Origin struct {
BlockID *big.Int `json:"blockID" gencodec:"required"`
L2BlockHash common.Hash `json:"l2BlockHash"`
L1BlockHeight *big.Int `json:"l1BlockHeight" gencodec:"required"`
L1BlockHash common.Hash `json:"l1BlockHash" gencodec:"required"`
L1BlockHeight *big.Int `json:"l1BlockHeight" gencodec:"required" rlp:"optional"`
L1BlockHash common.Hash `json:"l1BlockHash" gencodec:"required" rlp:"optional"`
}

type l1OriginMarshaling struct {
BlockID *math.HexOrDecimal256
L1BlockHeight *math.HexOrDecimal256
}

// IsPreconfBlock returns true if the L1Origin is for a preconfirmation block.
func (l L1Origin) IsPreconfBlock() bool {
return l.L1BlockHeight == nil
}

// WriteL1Origin stores a L1Origin into the database.
func WriteL1Origin(db ethdb.KeyValueWriter, blockID *big.Int, l1Origin *L1Origin) {
data, err := rlp.EncodeToBytes(l1Origin)
Expand Down
7 changes: 5 additions & 2 deletions eth/catalyst/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -488,8 +488,11 @@ func (api *ConsensusAPI) forkchoiceUpdated(update engine.ForkchoiceStateV1, payl

// Write L1Origin.
rawdb.WriteL1Origin(api.eth.ChainDb(), l1Origin.BlockID, l1Origin)
// Write the head L1Origin.
rawdb.WriteHeadL1Origin(api.eth.ChainDb(), l1Origin.BlockID)

// Write the head L1Origin, only when it's not a preconfirmation block.
if !l1Origin.IsPreconfBlock() {
rawdb.WriteHeadL1Origin(api.eth.ChainDb(), l1Origin.BlockID)
}

return valid(&id), nil
}
Expand Down
2 changes: 1 addition & 1 deletion eth/ethconfig/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ type Config struct {
func CreateConsensusEngine(config *params.ChainConfig, db ethdb.Database) (consensus.Engine, error) {
// CHANGE(taiko): use Taiko consensus engine when the --taiko flag is set.
if config.Taiko {
return taiko.New(config), nil
return taiko.New(config, db), nil
}
// Geth v1.14.0 dropped support for non-merged networks in any consensus
// mode. If such a network is requested, reject startup.
Expand Down

0 comments on commit 83b1591

Please sign in to comment.