diff --git a/core/blockchain.go b/core/blockchain.go index 0045932e9d..a0023b460d 100644 --- a/core/blockchain.go +++ b/core/blockchain.go @@ -973,6 +973,7 @@ func (bc *BlockChain) setHeadBeyondRoot(head uint64, time uint64, root common.Ha // The header, total difficulty and canonical hash will be // removed in the hc.SetHead function. rawdb.DeleteBody(db, hash, num) + rawdb.DeleteBlobSidecars(db, hash, num) rawdb.DeleteReceipts(db, hash, num) } // Todo(rjl493456442) txlookup, bloombits, etc @@ -1405,7 +1406,6 @@ func (bc *BlockChain) InsertReceiptChain(blockChain types.Blocks, receiptChain [ // Write all chain data to ancients. td := bc.GetTd(first.Hash(), first.NumberU64()) writeSize, err := rawdb.WriteAncientBlocks(bc.db, blockChain, receiptChain, td) - if err != nil { log.Error("Error importing chain data to ancients", "err", err) return 0, err diff --git a/core/blockchain_reader.go b/core/blockchain_reader.go index 227da1eb69..2023bcf4d6 100644 --- a/core/blockchain_reader.go +++ b/core/blockchain_reader.go @@ -256,7 +256,7 @@ func (bc *BlockChain) GetSidecarsByHash(hash common.Hash) types.BlobSidecars { if number == nil { return nil } - sidecars := rawdb.ReadRawBlobSidecars(bc.db, hash, *number) + sidecars := rawdb.ReadBlobSidecars(bc.db, hash, *number) if sidecars == nil { return nil } diff --git a/core/data_availability.go b/core/data_availability.go index bd807e3541..73502065ff 100644 --- a/core/data_availability.go +++ b/core/data_availability.go @@ -65,7 +65,7 @@ func IsDataAvailable(chain consensus.ChainHeaderReader, block *types.Block) (err highest = current } defer func() { - log.Info("IsDataAvailable", "block", block.Number(), "hash", block.Hash(), "highest", highest.Number, "sidecars", len(block.Sidecars()), "err", err) + log.Debug("IsDataAvailable", "block", block.Number(), "hash", block.Hash(), "highest", highest.Number, "sidecars", len(block.Sidecars()), "err", err) }() if block.NumberU64()+params.MinBlocksForBlobRequests < highest.Number.Uint64() { // if we needn't check DA of this block, just clean it diff --git a/core/rawdb/accessors_chain.go b/core/rawdb/accessors_chain.go index 9cd37899dd..dd56ca1b64 100644 --- a/core/rawdb/accessors_chain.go +++ b/core/rawdb/accessors_chain.go @@ -853,8 +853,8 @@ func ReadBlobSidecarsRLP(db ethdb.Reader, hash common.Hash, number uint64) rlp.R return data } -// ReadRawBlobSidecars retrieves all the transaction blobs belonging to a block. -func ReadRawBlobSidecars(db ethdb.Reader, hash common.Hash, number uint64) types.BlobSidecars { +// ReadBlobSidecars retrieves all the transaction blobs belonging to a block. +func ReadBlobSidecars(db ethdb.Reader, hash common.Hash, number uint64) types.BlobSidecars { data := ReadBlobSidecarsRLP(db, hash, number) if len(data) == 0 { return nil diff --git a/core/rawdb/accessors_chain_test.go b/core/rawdb/accessors_chain_test.go index 01f5a823ba..df9cda4768 100644 --- a/core/rawdb/accessors_chain_test.go +++ b/core/rawdb/accessors_chain_test.go @@ -455,13 +455,13 @@ func TestBlockBlobSidecarsStorage(t *testing.T) { sidecars := types.BlobSidecars{types.NewBlobSidecarFromTx(tx1)} // Check that no sidecars entries are in a pristine database - if bs := ReadRawBlobSidecars(db, blkHash, 0); len(bs) != 0 { + if bs := ReadBlobSidecars(db, blkHash, 0); len(bs) != 0 { t.Fatalf("non existent sidecars returned: %v", bs) } WriteBody(db, blkHash, 0, body) WriteBlobSidecars(db, blkHash, 0, sidecars) - if bs := ReadRawBlobSidecars(db, blkHash, 0); len(bs) == 0 { + if bs := ReadBlobSidecars(db, blkHash, 0); len(bs) == 0 { t.Fatalf("no sidecars returned") } else { if err := checkBlobSidecarsRLP(bs, sidecars); err != nil { @@ -470,7 +470,7 @@ func TestBlockBlobSidecarsStorage(t *testing.T) { } DeleteBlobSidecars(db, blkHash, 0) - if bs := ReadRawBlobSidecars(db, blkHash, 0); len(bs) != 0 { + if bs := ReadBlobSidecars(db, blkHash, 0); len(bs) != 0 { t.Fatalf("deleted sidecars returned: %v", bs) } } diff --git a/core/rawdb/chain_freezer.go b/core/rawdb/chain_freezer.go index 2da59a3960..5d9443ad0f 100644 --- a/core/rawdb/chain_freezer.go +++ b/core/rawdb/chain_freezer.go @@ -312,9 +312,8 @@ func (f *chainFreezer) freezeRangeWithBlobs(nfdb *nofreezedb, number, limit uint var ( cancunNumber uint64 - found bool + preHashes []common.Hash ) - for i := number; i <= limit; i++ { hash := ReadCanonicalHash(nfdb, i) if hash == (common.Hash{}) { @@ -326,16 +325,12 @@ func (f *chainFreezer) freezeRangeWithBlobs(nfdb *nofreezedb, number, limit uint } if isCancun(env, h.Number, h.Time) { cancunNumber = i - found = true break } } - if !found { - return f.freezeRange(nfdb, number, limit) - } // freeze pre cancun - preHashes, err := f.freezeRange(nfdb, number, cancunNumber-1) + preHashes, err = f.freezeRange(nfdb, number, cancunNumber-1) if err != nil { return preHashes, err } diff --git a/core/rawdb/freezer_table.go b/core/rawdb/freezer_table.go index 8dd99d4f49..3fc313e749 100644 --- a/core/rawdb/freezer_table.go +++ b/core/rawdb/freezer_table.go @@ -1035,14 +1035,15 @@ func (t *freezerTable) resetItems(tail, head uint64) (*freezerTable, error) { if t.readonly { return nil, errors.New("resetItems in readonly mode") } + itemHidden := t.itemHidden.Load() items := t.items.Load() - if tail != head && (itemHidden > tail || items < head) { + if tail > head || (tail < head && (itemHidden > tail || items < head)) { return nil, errors.New("cannot reset to non-exist range") } var err error - if tail != head { + if tail < head { if err = t.truncateHead(head); err != nil { return nil, err } @@ -1052,15 +1053,27 @@ func (t *freezerTable) resetItems(tail, head uint64) (*freezerTable, error) { return t, nil } + t.lock.Lock() + defer t.lock.Unlock() + // if tail == head, it means table reset to 0 item - t.releaseFilesAfter(t.tailId-1, true) + // remove all data files t.head.Close() - os.Remove(t.head.Name()) - t.index.Close() - os.Remove(t.index.Name()) + t.releaseFilesAfter(0, true) + t.releaseFile(0) + + // reset meta data file + if err := writeMetadata(t.meta, newMetadata(items)); err != nil { + return nil, err + } + if err := t.meta.Sync(); err != nil { + return nil, err + } t.meta.Close() - os.Remove(t.meta.Name()) + // reset the index file + t.index.Close() + os.Remove(t.index.Name()) var idxName string if t.noCompression { idxName = fmt.Sprintf("%s.ridx", t.name) // raw index file @@ -1072,11 +1085,16 @@ func (t *freezerTable) resetItems(tail, head uint64) (*freezerTable, error) { return nil, err } tailIndex := indexEntry{ - offset: uint32(tail), + filenum: 0, + offset: uint32(tail), } if _, err = index.Write(tailIndex.append(nil)); err != nil { return nil, err } + if err := index.Sync(); err != nil { + return nil, err + } + index.Close() return newFreezerTable(t.path, t.name, t.noCompression, t.readonly) } diff --git a/core/state/pruner/pruner.go b/core/state/pruner/pruner.go index 6adfbbede7..d307fd8e1d 100644 --- a/core/state/pruner/pruner.go +++ b/core/state/pruner/pruner.go @@ -454,7 +454,7 @@ func (p *BlockPruner) backUpOldDb(name string, cache, handles int, namespace str return consensus.ErrUnknownAncestor } // if there has blobs, it needs to back up too. - blobs := rawdb.ReadRawBlobSidecars(chainDb, blockHash, blockNumber) + blobs := rawdb.ReadBlobSidecars(chainDb, blockHash, blockNumber) block.WithSidecars(blobs) // Write into new ancient_back db. if _, err := rawdb.WriteAncientBlocks(frdbBack, []*types.Block{block}, []types.Receipts{receipts}, td); err != nil { diff --git a/eth/downloader/queue.go b/eth/downloader/queue.go index 8a2de4104a..b97cfdec6d 100644 --- a/eth/downloader/queue.go +++ b/eth/downloader/queue.go @@ -827,7 +827,7 @@ func (q *queue) DeliverBodies(id string, txLists [][]*types.Transaction, txListH if want := *header.BlobGasUsed / params.BlobTxBlobGasPerBlob; uint64(blobs) != want { // div because the header is surely good vs the body might be bloated return errInvalidBody } - if blobs > params.MaxBlobGasPerBlock { + if blobs > params.MaxBlobGasPerBlock/params.BlobTxBlobGasPerBlob { return errInvalidBody } } else { diff --git a/internal/ethapi/api_test.go b/internal/ethapi/api_test.go index a496315636..5123f01d95 100644 --- a/internal/ethapi/api_test.go +++ b/internal/ethapi/api_test.go @@ -574,7 +574,7 @@ func (b testBackend) GetBlobSidecars(ctx context.Context, hash common.Hash) (typ if header == nil || err != nil { return nil, err } - blobSidecars := rawdb.ReadRawBlobSidecars(b.db, hash, header.Number.Uint64()) + blobSidecars := rawdb.ReadBlobSidecars(b.db, hash, header.Number.Uint64()) return blobSidecars, nil } func (b testBackend) GetTd(ctx context.Context, hash common.Hash) *big.Int {