Skip to content

Commit

Permalink
fix consensus test (#349)
Browse files Browse the repository at this point in the history
  • Loading branch information
mask-pp authored Nov 6, 2024
1 parent 3f6b0f5 commit e692d34
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 48 deletions.
20 changes: 10 additions & 10 deletions consensus/taiko/consensus.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,16 +126,6 @@ func (t *Taiko) VerifyHeaders(chain consensus.ChainHeaderReader, headers []*type
}

func (t *Taiko) verifyHeader(header, parent *types.Header, unixNow int64) error {
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.IsSoftblock() && header.Time > uint64(unixNow) {
return consensus.ErrFutureBlock
}

// 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 @@ -181,6 +171,16 @@ func (t *Taiko) verifyHeader(header, parent *types.Header, unixNow int64) error
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
79 changes: 41 additions & 38 deletions consensus/taiko/consensus_test.go
Original file line number Diff line number Diff line change
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

0 comments on commit e692d34

Please sign in to comment.