Skip to content

Commit

Permalink
4844: bugfix and improve
Browse files Browse the repository at this point in the history
  • Loading branch information
buddh0 committed Mar 26, 2024
1 parent 79cd522 commit fd71408
Show file tree
Hide file tree
Showing 10 changed files with 39 additions and 26 deletions.
2 changes: 1 addition & 1 deletion core/blockchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion core/blockchain_reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion core/data_availability.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions core/rawdb/accessors_chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions core/rawdb/accessors_chain_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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)
}
}
Expand Down
9 changes: 2 additions & 7 deletions core/rawdb/chain_freezer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}) {
Expand All @@ -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
}
Expand Down
34 changes: 26 additions & 8 deletions core/rawdb/freezer_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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
Expand All @@ -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)
}
2 changes: 1 addition & 1 deletion core/state/pruner/pruner.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion eth/downloader/queue.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion internal/ethapi/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit fd71408

Please sign in to comment.