Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reorder the db WriteBlock codes to reduce the harm of partial-write #52

Merged
merged 1 commit into from
May 23, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 17 additions & 16 deletions blockchain/blockchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -688,12 +688,6 @@ func (b *Blockchain) WriteBlock(block *types.Block) error {
return err
}

// Write the header to the chain
evnt := &Event{}
if err := b.writeHeaderImpl(evnt, header); err != nil {
return err
}

// write the receipts, do it only after the header has been written.
// Otherwise, a client might ask for a header once the receipt is valid
// but before it is written into the storage
Expand All @@ -706,6 +700,12 @@ func (b *Blockchain) WriteBlock(block *types.Block) error {
return err
}

// Write the header to the chain
evnt := &Event{}
if err := b.writeHeaderImpl(evnt, header); err != nil {
return err
}

b.dispatchEvent(evnt)

// Update the average gas price
Expand Down Expand Up @@ -915,16 +915,6 @@ func (b *Blockchain) dispatchEvent(evnt *Event) {
func (b *Blockchain) writeHeaderImpl(evnt *Event, header *types.Header) error {
currentHeader := b.Header()

// Write the data
if header.ParentHash == currentHeader.Hash {
// Fast path to save the new canonical header
return b.writeCanonicalHeader(evnt, header)
}

if err := b.db.WriteHeader(header); err != nil {
return err
}

currentTD, ok := b.readTotalDifficulty(currentHeader.Hash)
if !ok {
panic("failed to get header difficulty")
Expand All @@ -951,6 +941,17 @@ func (b *Blockchain) writeHeaderImpl(evnt *Event, header *types.Header) error {
return err
}

// Write header
if err := b.db.WriteHeader(header); err != nil {
return err
}

// Write canonical header
if header.ParentHash == currentHeader.Hash {
// Fast path to save the new canonical header
return b.writeCanonicalHeader(evnt, header)
}

// Update the headers cache
b.headersCache.Add(header.Hash, header)

Expand Down