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(beacon): introduce soft blocks #342

Merged
merged 35 commits into from
Jan 7, 2025
Merged
Show file tree
Hide file tree
Changes from 33 commits
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
aaf9be2
feat(beacon): introduce soft blocks
davidtaikocha Oct 28, 2024
028fdd2
feat: update api.go
davidtaikocha Oct 28, 2024
bafb545
Merge branch 'taiko' into softblock
davidtaikocha Oct 29, 2024
3ea9a79
chore(ci): update CI
davidtaikocha Oct 29, 2024
47538d5
feat: update L1Origin
davidtaikocha Oct 29, 2024
c4cbc82
feat: update `verifyHeader`
davidtaikocha Oct 30, 2024
43ab184
test: update tests
davidtaikocha Oct 31, 2024
5e6011a
feat: update consensus
davidtaikocha Oct 31, 2024
a50a061
feat: update consensus
davidtaikocha Oct 31, 2024
d93f445
Merge branch 'taiko' into softblock
davidtaikocha Nov 3, 2024
d4cb83f
feat: update genesis
davidtaikocha Nov 3, 2024
93ed527
feat: remove timestamp check in prepareWork
davidtaikocha Nov 3, 2024
266c8ca
feat: merge changes in #281
davidtaikocha Nov 5, 2024
2d3d130
Update eth/catalyst/api.go
davidtaikocha Nov 5, 2024
e8dccf2
Update internal/ethapi/taiko_preconf.go
davidtaikocha Nov 5, 2024
f1df584
fix consensus test
mask-pp Nov 6, 2024
c8a5a9b
Merge branch 'taiko' into softblock
mask-pp Nov 6, 2024
3f6b0f5
revert commit f1df58
mask-pp Nov 6, 2024
e692d34
fix consensus test (#349)
mask-pp Nov 6, 2024
11e6061
Update eth/catalyst/api.go
davidtaikocha Nov 6, 2024
185e4c3
feat: add back timestamp check in worker
davidtaikocha Nov 6, 2024
cf0c053
add genesis
cyberhorsey Nov 6, 2024
b06f6ba
temp fix for old l1origin
cyberhorsey Nov 6, 2024
fd9b48a
nil value
cyberhorsey Nov 6, 2024
c18e4db
feat: rename to `L1OriginLegacy`
davidtaikocha Nov 7, 2024
3cd4472
feat: change `common.Big0` as the default value for legacy l1Origin, …
davidtaikocha Nov 7, 2024
ca660bd
feat(beacon): change the reorg log level (#350)
mask-pp Nov 17, 2024
51d42ec
add rlp optional flag (#353)
mask-pp Nov 17, 2024
dd04f6c
fix lint
mask-pp Nov 17, 2024
5c649ba
fix test case
mask-pp Nov 17, 2024
e3eb72f
feat(l1Origin): remove the reverted l1Origins (#355)
mask-pp Nov 27, 2024
d39b410
only forward txs
cyberhorsey Dec 3, 2024
2572112
Merge branch 'softblock' of github.com:taikochain/taiko-geth into sof…
cyberhorsey Dec 3, 2024
cd16465
Merge branch 'taiko' into softblock
davidtaikocha Jan 6, 2025
d55da97
chore: update ci
davidtaikocha Jan 7, 2025
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
8 changes: 4 additions & 4 deletions .github/workflows/docker.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name: "Push multi-arch docker image to GAR"

on:
push:
branches: [ taiko ]
branches: [ taiko, softblock ]
tags:
- "v*"

Expand All @@ -19,9 +19,9 @@ jobs:
platform: linux/amd64
- runner: arc-runner-set-arm64
platform: linux/arm64

runs-on: ${{ matrix.runner }}

steps:
- name: Prepare Environment
run: |
Expand Down Expand Up @@ -80,7 +80,7 @@ jobs:
with:
context: .
cache-from: type=gha
cache-to: type=gha,mode=max
cache-to: type=gha,mode=max
platforms: ${{ matrix.platform }}
push: true
tags: ${{ env.REGISTRY_IMAGE }}
Expand Down
5 changes: 5 additions & 0 deletions cmd/geth/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,11 @@ func makeFullNode(ctx *cli.Context) *node.Node {
cfg.Eth.OverrideVerkle = &v
}

// CHANGE(taiko): set preconfirmation forwarding URL.
if ctx.IsSet(utils.PreconfirmationForwardingURLFlag.Name) {
cfg.Eth.PreconfirmationForwardingURL = ctx.String(utils.PreconfirmationForwardingURLFlag.Name)
}

backend, eth := utils.RegisterEthService(stack, &cfg.Eth)

// CHANGE(TAIKO): register Taiko RPC APIs.
Expand Down
2 changes: 1 addition & 1 deletion cmd/geth/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ func init() {
metricsFlags,
)
// CHANGE(taiko): append Taiko flags into the original GETH flags
app.Flags = append(app.Flags, &utils.TaikoFlag)
app.Flags = append(app.Flags, utils.TaikoFlag, utils.PreconfirmationForwardingURLFlag)

flags.AutoEnvVars(app.Flags, "GETH")

Expand Down
13 changes: 10 additions & 3 deletions cmd/utils/taiko_flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,23 @@ import (

"github.com/ethereum/go-ethereum/eth"
"github.com/ethereum/go-ethereum/eth/ethconfig"
"github.com/ethereum/go-ethereum/internal/flags"
"github.com/ethereum/go-ethereum/node"
"github.com/ethereum/go-ethereum/params"
"github.com/ethereum/go-ethereum/rpc"
"github.com/urfave/cli/v2"
)

var (
TaikoFlag = cli.BoolFlag{
Name: "taiko",
Usage: "Taiko network",
TaikoFlag = &cli.BoolFlag{
Name: "taiko",
Usage: "Taiko network",
Category: flags.TaikoCategory,
}
PreconfirmationForwardingURLFlag = &cli.StringFlag{
Name: "taiko.preconfirmationForwardingUrl",
Usage: "URL to forward RPC requests before confirmation",
Category: flags.TaikoCategory,
}
)

Expand Down
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 All @@ -40,12 +42,13 @@ var (
// Taiko is a consensus engine used by L2 rollup.
type Taiko struct {
chainConfig *params.ChainConfig
chainDB ethdb.Database
taikoL2Address common.Address
}

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 @@ -56,6 +59,7 @@ func New(chainConfig *params.ChainConfig) *Taiko {
strings.Repeat("0", common.AddressLength*2-len(taikoL2AddressPrefix)-len(TaikoL2AddressSuffix)) +
TaikoL2AddressSuffix,
),
chainDB: chainDB,
}
}

Expand All @@ -82,7 +86,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 @@ -109,7 +113,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 @@ -121,11 +125,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 @@ -171,6 +171,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 soft block, then check the timestamp.
if l1Origin != nil && !l1Origin.IsSoftBlock() && header.Time > uint64(unixNow) {
return consensus.ErrFutureBlock
}

return nil
}

Expand Down
81 changes: 42 additions & 39 deletions 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 Expand Up @@ -93,70 +93,73 @@ func init() {
}
}

func newTestBackend(t *testing.T) (*eth.Ethereum, []*types.Block) {
// Generate test chain.
blocks := generateTestChain()
func generateTestChain(t *testing.T) ([]*types.Block, *eth.Ethereum) {
generate := func(i int, g *core.BlockGen) {
g.OffsetTime(5)

g.SetExtra([]byte("test_taiko"))
g.SetDifficulty(common.Big0)

for i, tx := range txs {
if i == 0 {
if err := tx.MarkAsAnchor(); err != nil {
panic(err)
}
}
g.AddTx(tx)
}
}

// Create node
n, err := node.New(&node.Config{})
n, err := node.New(&node.Config{
DataDir: t.TempDir(),
})
if err != nil {
t.Fatalf("can't create new node: %v", err)
}

// Create Ethereum Service
config := &ethconfig.Config{
ethService, err := eth.New(n, &ethconfig.Config{
Genesis: genesis,
}

ethservice, err := eth.New(n, config)
})
if err != nil {
t.Fatalf("can't create new ethereum service: %v", err)
}

db := ethService.ChainDb()

gblock := genesis.MustCommit(db, triedb.NewDatabase(db, triedb.HashDefaults))
blocks, _ := core.GenerateChain(genesis.Config, gblock, testEngine, db, 1, generate)
blocks = append([]*types.Block{gblock}, blocks...)

// Insert L1Origins.
for _, block := range blocks {
rawdb.WriteL1Origin(db, block.Number(), &rawdb.L1Origin{
BlockID: block.Number(),
L1BlockHeight: block.Number(),
L1BlockHash: block.Hash(),
})
}

// Import the test chain.
if err := n.Start(); err != nil {
t.Fatalf("can't start test node: %v", err)
}

if _, err := ethservice.BlockChain().InsertChain(blocks[1:]); err != nil {
if _, err := ethService.BlockChain().InsertChain(blocks[1:]); err != nil {
t.Fatalf("can't import test blocks: %v", err)
}

if _, ok := ethservice.Engine().(*taiko.Taiko); !ok {
if _, ok := ethService.Engine().(*taiko.Taiko); !ok {
t.Fatalf("not use taiko engine")
}

return ethservice, blocks
}

func generateTestChain() []*types.Block {
db := rawdb.NewMemoryDatabase()
generate := func(i int, g *core.BlockGen) {
g.OffsetTime(5)

g.SetExtra([]byte("test_taiko"))
g.SetDifficulty(common.Big0)

for i, tx := range txs {
if i == 0 {
if err := tx.MarkAsAnchor(); err != nil {
panic(err)
}
}
g.AddTx(tx)
}
}

gblock := genesis.MustCommit(db, triedb.NewDatabase(db, triedb.HashDefaults))

blocks, _ := core.GenerateChain(genesis.Config, gblock, testEngine, db, 1, generate)

blocks = append([]*types.Block{gblock}, blocks...)
return blocks
return blocks, ethService
}

func TestVerifyHeader(t *testing.T) {
ethService, blocks := newTestBackend(t)
// Generate test chain.
blocks, ethService := generateTestChain(t)

for _, b := range blocks {
err := testEngine.VerifyHeader(ethService.BlockChain(), b.Header())
Expand Down
7 changes: 6 additions & 1 deletion core/blockchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -2269,7 +2269,12 @@ func (bc *BlockChain) reorg(oldHead *types.Header, newHead *types.Block) error {
} else {
// len(newChain) == 0 && len(oldChain) > 0
// rewind the canonical chain to a lower point.
log.Error("Impossible reorg, please file an issue", "oldnum", oldBlock.Number(), "oldhash", oldBlock.Hash(), "oldblocks", len(oldChain), "newnum", newBlock.Number(), "newhash", newBlock.Hash(), "newblocks", len(newChain))
// CHANGE(taiko): use debug log level to avoid logging too many logs when frequently soft block rollback.
if bc.chainConfig.Taiko {
log.Debug("Impossible reorg, please file an issue", "oldnum", oldBlock.Number(), "oldhash", oldBlock.Hash(), "oldblocks", len(oldChain), "newnum", newBlock.Number(), "newhash", newBlock.Hash(), "newblocks", len(newChain))
} else {
log.Error("Impossible reorg, please file an issue", "oldnum", oldBlock.Number(), "oldhash", oldBlock.Hash(), "oldblocks", len(oldChain), "newnum", newBlock.Number(), "newhash", newBlock.Hash(), "newblocks", len(newChain))
}
}
// Acquire the tx-lookup lock before mutation. This step is essential
// as the txlookups should be changed atomically, and all subsequent
Expand Down
42 changes: 32 additions & 10 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.

Loading