go-ethereum/ethclient/taiko_api_test.go
David a2cbf904ea
feat(beacon): introduce soft blocks (#342)
* feat(beacon): introduce soft blocks

* feat: update api.go

* chore(ci): update CI

* feat: update L1Origin

* feat: update `verifyHeader`

* test: update tests

* feat: update consensus

* feat: update consensus

* feat: update genesis

* feat: remove timestamp check in prepareWork

* feat: merge changes in #281

* Update eth/catalyst/api.go

Co-authored-by: maskpp <maskpp266@gmail.com>

* Update internal/ethapi/taiko_preconf.go

Co-authored-by: maskpp <maskpp266@gmail.com>

* fix consensus test

* revert commit f1df58

* fix consensus test (#349)

* Update eth/catalyst/api.go

Co-authored-by: maskpp <maskpp266@gmail.com>

* feat: add back timestamp check in worker

* add genesis

* temp fix for old l1origin

* nil value

* feat: rename to `L1OriginLegacy`

* feat: change `common.Big0` as the default value for legacy l1Origin, to make `IsSoftblock` return `false`

* feat(beacon): change the reorg log level (#350)

* use debug log level to avoid logging too many logs when frequently soft block reorg.

* use debug log level to avoid logging too many logs when frequently soft block reorg.

* feat: check --taiko flag

---------

Co-authored-by: David <david@taiko.xyz>

* add rlp optional flag (#353)

* fix lint

* fix test case

* feat(l1Origin): remove the reverted l1Origins (#355)

* remove the reverted l1Origins

* feat: add more comments

---------

Co-authored-by: David <david@taiko.xyz>

* only forward txs

* chore: update ci

---------

Co-authored-by: maskpp <maskpp266@gmail.com>
Co-authored-by: Jeffery Walsh <cyberhorsey@gmail.com>
2025-01-07 16:25:44 +08:00

134 lines
3.4 KiB
Go

package ethclient
import (
"context"
"crypto/rand"
"math/big"
"testing"
"github.com/ethereum/go-ethereum"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/rawdb"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/eth"
"github.com/ethereum/go-ethereum/eth/ethconfig"
"github.com/ethereum/go-ethereum/ethdb"
"github.com/ethereum/go-ethereum/log"
"github.com/ethereum/go-ethereum/node"
"github.com/ethereum/go-ethereum/params"
"github.com/ethereum/go-ethereum/rpc"
"github.com/stretchr/testify/require"
)
func newTaikoAPITestClient(t *testing.T) (*Client, []*types.Block, ethdb.Database) {
// Generate test chain.
blocks := generateTestChain()
// Create node
n, err := node.New(&node.Config{})
require.Nil(t, err)
// Create Ethereum Service
config := &ethconfig.Config{Genesis: genesis}
ethservice, err := eth.New(n, config)
require.Nil(t, err)
n.RegisterAPIs([]rpc.API{
{
Namespace: "taiko",
Version: params.VersionWithMeta,
Service: eth.NewTaikoAPIBackend(ethservice),
Public: true,
},
})
// Start node
require.Nil(t, n.Start())
// Insert test blocks
_, err = ethservice.BlockChain().InsertChain(blocks[1:])
require.Nil(t, err)
return NewClient(n.Attach()), blocks, ethservice.ChainDb()
}
func TestHeadL1Origin(t *testing.T) {
ec, blocks, db := newTaikoAPITestClient(t)
headerHash := blocks[len(blocks)-1].Hash()
l1OriginFound, err := ec.HeadL1Origin(context.Background())
require.Equal(t, ethereum.NotFound.Error(), err.Error())
require.Nil(t, l1OriginFound)
testL1Origin := &rawdb.L1Origin{
BlockID: randomBigInt(),
L2BlockHash: headerHash,
L1BlockHeight: randomBigInt(),
L1BlockHash: randomHash(),
BatchID: nil,
EndOfBlock: false,
EndOfPreconf: false,
Preconfer: common.Address{},
}
rawdb.WriteL1Origin(db, testL1Origin.BlockID, testL1Origin)
rawdb.WriteHeadL1Origin(db, testL1Origin.BlockID)
l1OriginFound, err = ec.HeadL1Origin(context.Background())
require.Nil(t, err)
require.Equal(t, testL1Origin, l1OriginFound)
require.False(t, l1OriginFound.IsSoftBlock())
}
func TestL1OriginByID(t *testing.T) {
ec, blocks, db := newTaikoAPITestClient(t)
headerHash := blocks[len(blocks)-1].Hash()
testL1Origin := &rawdb.L1Origin{
BlockID: randomBigInt(),
L2BlockHash: headerHash,
L1BlockHeight: randomBigInt(),
L1BlockHash: randomHash(),
BatchID: common.Big1,
EndOfBlock: false,
EndOfPreconf: false,
Preconfer: testAddr,
}
l1OriginFound, err := ec.L1OriginByID(context.Background(), testL1Origin.BlockID)
require.Equal(t, ethereum.NotFound.Error(), err.Error())
require.Nil(t, l1OriginFound)
rawdb.WriteL1Origin(db, testL1Origin.BlockID, testL1Origin)
rawdb.WriteHeadL1Origin(db, testL1Origin.BlockID)
l1OriginFound, err = ec.L1OriginByID(context.Background(), testL1Origin.BlockID)
require.Nil(t, err)
require.Equal(t, testL1Origin, l1OriginFound)
require.True(t, l1OriginFound.IsSoftBlock())
}
// randomHash generates a random blob of data and returns it as a hash.
func randomHash() common.Hash {
var hash common.Hash
if n, err := rand.Read(hash[:]); n != common.HashLength || err != nil {
panic(err)
}
return hash
}
// randomBigInt generates a random big integer.
func randomBigInt() *big.Int {
randomBigInt, err := rand.Int(rand.Reader, common.Big256)
if err != nil {
log.Crit(err.Error())
}
return randomBigInt
}