@@ -33,6 +33,7 @@ import (
3333 "github.com/ethereum/go-ethereum/consensus"
3434 "github.com/ethereum/go-ethereum/consensus/beacon"
3535 "github.com/ethereum/go-ethereum/consensus/ethash"
36+ "github.com/ethereum/go-ethereum/core/history"
3637 "github.com/ethereum/go-ethereum/core/rawdb"
3738 "github.com/ethereum/go-ethereum/core/state"
3839 "github.com/ethereum/go-ethereum/core/types"
@@ -4257,13 +4258,7 @@ func testChainReorgSnapSync(t *testing.T, ancientLimit uint64) {
42574258// be persisted without the receipts and bodies; chain after should be persisted
42584259// normally.
42594260func TestInsertChainWithCutoff (t * testing.T ) {
4260- testInsertChainWithCutoff (t , 32 , 32 ) // cutoff = 32, ancientLimit = 32
4261- testInsertChainWithCutoff (t , 32 , 64 ) // cutoff = 32, ancientLimit = 64 (entire chain in ancient)
4262- testInsertChainWithCutoff (t , 32 , 65 ) // cutoff = 32, ancientLimit = 65 (64 blocks in ancient, 1 block in live)
4263- }
4264-
4265- func testInsertChainWithCutoff (t * testing.T , cutoff uint64 , ancientLimit uint64 ) {
4266- // log.SetDefault(log.NewLogger(log.NewTerminalHandlerWithLevel(os.Stderr, log.LevelDebug, true)))
4261+ const chainLength = 64
42674262
42684263 // Configure and generate a sample block chain
42694264 var (
@@ -4278,24 +4273,51 @@ func testInsertChainWithCutoff(t *testing.T, cutoff uint64, ancientLimit uint64)
42784273 signer = types .LatestSigner (gspec .Config )
42794274 engine = beacon .New (ethash .NewFaker ())
42804275 )
4281- _ , blocks , receipts := GenerateChainWithGenesis (gspec , engine , int ( 2 * cutoff ) , func (i int , block * BlockGen ) {
4276+ _ , blocks , receipts := GenerateChainWithGenesis (gspec , engine , chainLength , func (i int , block * BlockGen ) {
42824277 block .SetCoinbase (common.Address {0x00 })
4283-
42844278 tx , err := types .SignTx (types .NewTransaction (block .TxNonce (address ), common.Address {0x00 }, big .NewInt (1000 ), params .TxGas , block .header .BaseFee , nil ), signer , key )
42854279 if err != nil {
42864280 panic (err )
42874281 }
42884282 block .AddTx (tx )
42894283 })
4290- db , _ := rawdb .NewDatabaseWithFreezer (rawdb .NewMemoryDatabase (), "" , "" , false )
4291- defer db .Close ()
42924284
4285+ // Run the actual tests.
4286+ t .Run ("cutoff-32/ancientLimit-32" , func (t * testing.T ) {
4287+ // cutoff = 32, ancientLimit = 32
4288+ testInsertChainWithCutoff (t , 32 , 32 , gspec , blocks , receipts )
4289+ })
4290+ t .Run ("cutoff-32/ancientLimit-64" , func (t * testing.T ) {
4291+ // cutoff = 32, ancientLimit = 64 (entire chain in ancient)
4292+ testInsertChainWithCutoff (t , 32 , 64 , gspec , blocks , receipts )
4293+ })
4294+ t .Run ("cutoff-32/ancientLimit-64" , func (t * testing.T ) {
4295+ // cutoff = 32, ancientLimit = 65 (64 blocks in ancient, 1 block in live)
4296+ testInsertChainWithCutoff (t , 32 , 65 , gspec , blocks , receipts )
4297+ })
4298+ }
4299+
4300+ func testInsertChainWithCutoff (t * testing.T , cutoff uint64 , ancientLimit uint64 , genesis * Genesis , blocks []* types.Block , receipts []types.Receipts ) {
4301+ // log.SetDefault(log.NewLogger(log.NewTerminalHandlerWithLevel(os.Stderr, log.LevelDebug, true)))
4302+
4303+ // Add a known pruning point for the duration of the test.
4304+ ghash := genesis .ToBlock ().Hash ()
42934305 cutoffBlock := blocks [cutoff - 1 ]
4306+ history .PrunePoints [ghash ] = & history.PrunePoint {
4307+ BlockNumber : cutoffBlock .NumberU64 (),
4308+ BlockHash : cutoffBlock .Hash (),
4309+ }
4310+ defer func () {
4311+ delete (history .PrunePoints , ghash )
4312+ }()
4313+
4314+ // Enable pruning in cache config.
42944315 config := DefaultCacheConfigWithScheme (rawdb .PathScheme )
4295- config .HistoryPruningCutoffNumber = cutoffBlock .NumberU64 ()
4296- config .HistoryPruningCutoffHash = cutoffBlock .Hash ()
4316+ config .ChainHistoryMode = history .KeepPostMerge
42974317
4298- chain , _ := NewBlockChain (db , DefaultCacheConfigWithScheme (rawdb .PathScheme ), gspec , nil , beacon .New (ethash .NewFaker ()), vm.Config {}, nil )
4318+ db , _ := rawdb .NewDatabaseWithFreezer (rawdb .NewMemoryDatabase (), "" , "" , false )
4319+ defer db .Close ()
4320+ chain , _ := NewBlockChain (db , DefaultCacheConfigWithScheme (rawdb .PathScheme ), genesis , nil , beacon .New (ethash .NewFaker ()), vm.Config {}, nil )
42994321 defer chain .Stop ()
43004322
43014323 var (
@@ -4326,8 +4348,8 @@ func testInsertChainWithCutoff(t *testing.T, cutoff uint64, ancientLimit uint64)
43264348 t .Errorf ("head header #%d: header mismatch: want: %v, got: %v" , headHeader .Number , blocks [len (blocks )- 1 ].Hash (), headHeader .Hash ())
43274349 }
43284350 headBlock := chain .CurrentBlock ()
4329- if headBlock .Hash () != gspec . ToBlock (). Hash () {
4330- t .Errorf ("head block #%d: header mismatch: want: %v, got: %v" , headBlock .Number , gspec . ToBlock (). Hash () , headBlock .Hash ())
4351+ if headBlock .Hash () != ghash {
4352+ t .Errorf ("head block #%d: header mismatch: want: %v, got: %v" , headBlock .Number , ghash , headBlock .Hash ())
43314353 }
43324354
43334355 // Iterate over all chain data components, and cross reference
0 commit comments