mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-07-26 14:46:42 +00:00
core: restore test
This commit is contained in:
parent
9aa4df467c
commit
93bb4057fb
1 changed files with 133 additions and 111 deletions
|
|
@ -33,6 +33,7 @@ import (
|
||||||
"github.com/ethereum/go-ethereum/consensus"
|
"github.com/ethereum/go-ethereum/consensus"
|
||||||
"github.com/ethereum/go-ethereum/consensus/beacon"
|
"github.com/ethereum/go-ethereum/consensus/beacon"
|
||||||
"github.com/ethereum/go-ethereum/consensus/ethash"
|
"github.com/ethereum/go-ethereum/consensus/ethash"
|
||||||
|
"github.com/ethereum/go-ethereum/core/history"
|
||||||
"github.com/ethereum/go-ethereum/core/rawdb"
|
"github.com/ethereum/go-ethereum/core/rawdb"
|
||||||
"github.com/ethereum/go-ethereum/core/state"
|
"github.com/ethereum/go-ethereum/core/state"
|
||||||
"github.com/ethereum/go-ethereum/core/types"
|
"github.com/ethereum/go-ethereum/core/types"
|
||||||
|
|
@ -4256,115 +4257,136 @@ func testChainReorgSnapSync(t *testing.T, ancientLimit uint64) {
|
||||||
// chain cutoff point. In this case the chain segment before the cutoff should
|
// chain cutoff point. In this case the chain segment before the cutoff should
|
||||||
// be persisted without the receipts and bodies; chain after should be persisted
|
// be persisted without the receipts and bodies; chain after should be persisted
|
||||||
// normally.
|
// normally.
|
||||||
// func TestInsertChainWithCutoff(t *testing.T) {
|
func TestInsertChainWithCutoff(t *testing.T) {
|
||||||
// testInsertChainWithCutoff(t, 32, 32) // cutoff = 32, ancientLimit = 32
|
const chainLength = 64
|
||||||
// testInsertChainWithCutoff(t, 32, 64) // cutoff = 32, ancientLimit = 64 (entire chain in ancient)
|
|
||||||
// testInsertChainWithCutoff(t, 32, 65) // cutoff = 32, ancientLimit = 65 (64 blocks in ancient, 1 block in live)
|
|
||||||
// }
|
|
||||||
|
|
||||||
// func testInsertChainWithCutoff(t *testing.T, cutoff uint64, ancientLimit uint64) {
|
// Configure and generate a sample block chain
|
||||||
// // log.SetDefault(log.NewLogger(log.NewTerminalHandlerWithLevel(os.Stderr, log.LevelDebug, true)))
|
var (
|
||||||
//
|
key, _ = crypto.HexToECDSA("b71c71a67e1177ad4e901695e1b4b9ee17ae16c6668d313eac2f96dbcda3f291")
|
||||||
// // Configure and generate a sample block chain
|
address = crypto.PubkeyToAddress(key.PublicKey)
|
||||||
// var (
|
funds = big.NewInt(1000000000000000)
|
||||||
// key, _ = crypto.HexToECDSA("b71c71a67e1177ad4e901695e1b4b9ee17ae16c6668d313eac2f96dbcda3f291")
|
gspec = &Genesis{
|
||||||
// address = crypto.PubkeyToAddress(key.PublicKey)
|
Config: params.TestChainConfig,
|
||||||
// funds = big.NewInt(1000000000000000)
|
Alloc: types.GenesisAlloc{address: {Balance: funds}},
|
||||||
// gspec = &Genesis{
|
BaseFee: big.NewInt(params.InitialBaseFee),
|
||||||
// Config: params.TestChainConfig,
|
}
|
||||||
// Alloc: types.GenesisAlloc{address: {Balance: funds}},
|
signer = types.LatestSigner(gspec.Config)
|
||||||
// BaseFee: big.NewInt(params.InitialBaseFee),
|
engine = beacon.New(ethash.NewFaker())
|
||||||
// }
|
)
|
||||||
// signer = types.LatestSigner(gspec.Config)
|
_, blocks, receipts := GenerateChainWithGenesis(gspec, engine, chainLength, func(i int, block *BlockGen) {
|
||||||
// engine = beacon.New(ethash.NewFaker())
|
block.SetCoinbase(common.Address{0x00})
|
||||||
// )
|
tx, err := types.SignTx(types.NewTransaction(block.TxNonce(address), common.Address{0x00}, big.NewInt(1000), params.TxGas, block.header.BaseFee, nil), signer, key)
|
||||||
// _, blocks, receipts := GenerateChainWithGenesis(gspec, engine, int(2*cutoff), func(i int, block *BlockGen) {
|
if err != nil {
|
||||||
// block.SetCoinbase(common.Address{0x00})
|
panic(err)
|
||||||
//
|
}
|
||||||
// tx, err := types.SignTx(types.NewTransaction(block.TxNonce(address), common.Address{0x00}, big.NewInt(1000), params.TxGas, block.header.BaseFee, nil), signer, key)
|
block.AddTx(tx)
|
||||||
// if err != nil {
|
})
|
||||||
// panic(err)
|
|
||||||
// }
|
// Run the actual tests.
|
||||||
// block.AddTx(tx)
|
t.Run("cutoff-32/ancientLimit-32", func(t *testing.T) {
|
||||||
// })
|
// cutoff = 32, ancientLimit = 32
|
||||||
// db, _ := rawdb.NewDatabaseWithFreezer(rawdb.NewMemoryDatabase(), "", "", false)
|
testInsertChainWithCutoff(t, 32, 32, gspec, blocks, receipts)
|
||||||
// defer db.Close()
|
})
|
||||||
//
|
t.Run("cutoff-32/ancientLimit-64", func(t *testing.T) {
|
||||||
// cutoffBlock := blocks[cutoff-1]
|
// cutoff = 32, ancientLimit = 64 (entire chain in ancient)
|
||||||
// config := DefaultCacheConfigWithScheme(rawdb.PathScheme)
|
testInsertChainWithCutoff(t, 32, 64, gspec, blocks, receipts)
|
||||||
// config.HistoryPruningCutoffNumber = cutoffBlock.NumberU64()
|
})
|
||||||
// config.HistoryPruningCutoffHash = cutoffBlock.Hash()
|
t.Run("cutoff-32/ancientLimit-64", func(t *testing.T) {
|
||||||
//
|
// cutoff = 32, ancientLimit = 65 (64 blocks in ancient, 1 block in live)
|
||||||
// chain, _ := NewBlockChain(db, DefaultCacheConfigWithScheme(rawdb.PathScheme), gspec, nil, beacon.New(ethash.NewFaker()), vm.Config{}, nil)
|
testInsertChainWithCutoff(t, 32, 65, gspec, blocks, receipts)
|
||||||
// defer chain.Stop()
|
})
|
||||||
//
|
}
|
||||||
// var (
|
|
||||||
// headersBefore []*types.Header
|
func testInsertChainWithCutoff(t *testing.T, cutoff uint64, ancientLimit uint64, genesis *Genesis, blocks []*types.Block, receipts []types.Receipts) {
|
||||||
// blocksAfter []*types.Block
|
// log.SetDefault(log.NewLogger(log.NewTerminalHandlerWithLevel(os.Stderr, log.LevelDebug, true)))
|
||||||
// receiptsAfter []types.Receipts
|
|
||||||
// )
|
// Add a known pruning point for the duration of the test.
|
||||||
// for i, b := range blocks {
|
ghash := genesis.ToBlock().Hash()
|
||||||
// if b.NumberU64() < cutoffBlock.NumberU64() {
|
cutoffBlock := blocks[cutoff-1]
|
||||||
// headersBefore = append(headersBefore, b.Header())
|
history.PrunePoints[ghash] = &history.PrunePoint{
|
||||||
// } else {
|
BlockNumber: cutoffBlock.NumberU64(),
|
||||||
// blocksAfter = append(blocksAfter, b)
|
BlockHash: cutoffBlock.Hash(),
|
||||||
// receiptsAfter = append(receiptsAfter, receipts[i])
|
}
|
||||||
// }
|
defer func() {
|
||||||
// }
|
delete(history.PrunePoints, ghash)
|
||||||
// if n, err := chain.InsertHeadersBeforeCutoff(headersBefore); err != nil {
|
}()
|
||||||
// t.Fatalf("failed to insert headers before cutoff %d: %v", n, err)
|
|
||||||
// }
|
// Enable pruning in cache config.
|
||||||
// if n, err := chain.InsertReceiptChain(blocksAfter, receiptsAfter, ancientLimit); err != nil {
|
config := DefaultCacheConfigWithScheme(rawdb.PathScheme)
|
||||||
// t.Fatalf("failed to insert receipt %d: %v", n, err)
|
config.ChainHistoryMode = history.KeepPostMerge
|
||||||
// }
|
|
||||||
// headSnap := chain.CurrentSnapBlock()
|
db, _ := rawdb.NewDatabaseWithFreezer(rawdb.NewMemoryDatabase(), "", "", false)
|
||||||
// if headSnap.Hash() != blocks[len(blocks)-1].Hash() {
|
defer db.Close()
|
||||||
// t.Errorf("head snap block #%d: header mismatch: want: %v, got: %v", headSnap.Number, blocks[len(blocks)-1].Hash(), headSnap.Hash())
|
chain, _ := NewBlockChain(db, DefaultCacheConfigWithScheme(rawdb.PathScheme), genesis, nil, beacon.New(ethash.NewFaker()), vm.Config{}, nil)
|
||||||
// }
|
defer chain.Stop()
|
||||||
// headHeader := chain.CurrentHeader()
|
|
||||||
// if headHeader.Hash() != blocks[len(blocks)-1].Hash() {
|
var (
|
||||||
// t.Errorf("head header #%d: header mismatch: want: %v, got: %v", headHeader.Number, blocks[len(blocks)-1].Hash(), headHeader.Hash())
|
headersBefore []*types.Header
|
||||||
// }
|
blocksAfter []*types.Block
|
||||||
// headBlock := chain.CurrentBlock()
|
receiptsAfter []types.Receipts
|
||||||
// if headBlock.Hash() != gspec.ToBlock().Hash() {
|
)
|
||||||
// t.Errorf("head block #%d: header mismatch: want: %v, got: %v", headBlock.Number, gspec.ToBlock().Hash(), headBlock.Hash())
|
for i, b := range blocks {
|
||||||
// }
|
if b.NumberU64() < cutoffBlock.NumberU64() {
|
||||||
//
|
headersBefore = append(headersBefore, b.Header())
|
||||||
// // Iterate over all chain data components, and cross reference
|
} else {
|
||||||
// for i := 0; i < len(blocks); i++ {
|
blocksAfter = append(blocksAfter, b)
|
||||||
// num, hash := blocks[i].NumberU64(), blocks[i].Hash()
|
receiptsAfter = append(receiptsAfter, receipts[i])
|
||||||
//
|
}
|
||||||
// // Canonical headers should be visible regardless of cutoff
|
}
|
||||||
// header := chain.GetHeaderByNumber(num)
|
if n, err := chain.InsertHeadersBeforeCutoff(headersBefore); err != nil {
|
||||||
// if header.Hash() != hash {
|
t.Fatalf("failed to insert headers before cutoff %d: %v", n, err)
|
||||||
// t.Errorf("block #%d: header mismatch: want: %v, got: %v", num, hash, header.Hash())
|
}
|
||||||
// }
|
if n, err := chain.InsertReceiptChain(blocksAfter, receiptsAfter, ancientLimit); err != nil {
|
||||||
// tail, err := db.Tail()
|
t.Fatalf("failed to insert receipt %d: %v", n, err)
|
||||||
// if err != nil {
|
}
|
||||||
// t.Fatalf("Failed to get chain tail, %v", err)
|
headSnap := chain.CurrentSnapBlock()
|
||||||
// }
|
if headSnap.Hash() != blocks[len(blocks)-1].Hash() {
|
||||||
// if tail != cutoffBlock.NumberU64() {
|
t.Errorf("head snap block #%d: header mismatch: want: %v, got: %v", headSnap.Number, blocks[len(blocks)-1].Hash(), headSnap.Hash())
|
||||||
// t.Fatalf("Unexpected chain tail, want: %d, got: %d", cutoffBlock.NumberU64(), tail)
|
}
|
||||||
// }
|
headHeader := chain.CurrentHeader()
|
||||||
// // Block bodies and receipts before the cutoff should be non-existent
|
if headHeader.Hash() != blocks[len(blocks)-1].Hash() {
|
||||||
// if num < cutoffBlock.NumberU64() {
|
t.Errorf("head header #%d: header mismatch: want: %v, got: %v", headHeader.Number, blocks[len(blocks)-1].Hash(), headHeader.Hash())
|
||||||
// body := chain.GetBody(hash)
|
}
|
||||||
// if body != nil {
|
headBlock := chain.CurrentBlock()
|
||||||
// t.Fatalf("Unexpected block body: %d, cutoff: %d", num, cutoffBlock.NumberU64())
|
if headBlock.Hash() != ghash {
|
||||||
// }
|
t.Errorf("head block #%d: header mismatch: want: %v, got: %v", headBlock.Number, ghash, headBlock.Hash())
|
||||||
// receipts := chain.GetReceiptsByHash(hash)
|
}
|
||||||
// if receipts != nil {
|
|
||||||
// t.Fatalf("Unexpected block receipts: %d, cutoff: %d", num, cutoffBlock.NumberU64())
|
// Iterate over all chain data components, and cross reference
|
||||||
// }
|
for i := 0; i < len(blocks); i++ {
|
||||||
// } else {
|
num, hash := blocks[i].NumberU64(), blocks[i].Hash()
|
||||||
// body := chain.GetBody(hash)
|
|
||||||
// if body == nil || len(body.Transactions) != 1 {
|
// Canonical headers should be visible regardless of cutoff
|
||||||
// t.Fatalf("Missed block body: %d, cutoff: %d", num, cutoffBlock.NumberU64())
|
header := chain.GetHeaderByNumber(num)
|
||||||
// }
|
if header.Hash() != hash {
|
||||||
// receipts := chain.GetReceiptsByHash(hash)
|
t.Errorf("block #%d: header mismatch: want: %v, got: %v", num, hash, header.Hash())
|
||||||
// if receipts == nil || len(receipts) != 1 {
|
}
|
||||||
// t.Fatalf("Missed block receipts: %d, cutoff: %d", num, cutoffBlock.NumberU64())
|
tail, err := db.Tail()
|
||||||
// }
|
if err != nil {
|
||||||
// }
|
t.Fatalf("Failed to get chain tail, %v", err)
|
||||||
// }
|
}
|
||||||
// }
|
if tail != cutoffBlock.NumberU64() {
|
||||||
|
t.Fatalf("Unexpected chain tail, want: %d, got: %d", cutoffBlock.NumberU64(), tail)
|
||||||
|
}
|
||||||
|
// Block bodies and receipts before the cutoff should be non-existent
|
||||||
|
if num < cutoffBlock.NumberU64() {
|
||||||
|
body := chain.GetBody(hash)
|
||||||
|
if body != nil {
|
||||||
|
t.Fatalf("Unexpected block body: %d, cutoff: %d", num, cutoffBlock.NumberU64())
|
||||||
|
}
|
||||||
|
receipts := chain.GetReceiptsByHash(hash)
|
||||||
|
if receipts != nil {
|
||||||
|
t.Fatalf("Unexpected block receipts: %d, cutoff: %d", num, cutoffBlock.NumberU64())
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
body := chain.GetBody(hash)
|
||||||
|
if body == nil || len(body.Transactions) != 1 {
|
||||||
|
t.Fatalf("Missed block body: %d, cutoff: %d", num, cutoffBlock.NumberU64())
|
||||||
|
}
|
||||||
|
receipts := chain.GetReceiptsByHash(hash)
|
||||||
|
if receipts == nil || len(receipts) != 1 {
|
||||||
|
t.Fatalf("Missed block receipts: %d, cutoff: %d", num, cutoffBlock.NumberU64())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue