Skip to content

Commit

Permalink
tbc: fix regression where we were linking the wrong blockheaders (#277)
Browse files Browse the repository at this point in the history
  • Loading branch information
marcopeereboom authored Oct 8, 2024
1 parent fd5c4b2 commit ee45b5c
Showing 1 changed file with 11 additions and 15 deletions.
26 changes: 11 additions & 15 deletions database/tbcd/level/level.go
Original file line number Diff line number Diff line change
Expand Up @@ -431,26 +431,11 @@ func (l *ldb) BlockHeadersInsert(ctx context.Context, bhs *wire.MsgHeaders) (tbc
log.Tracef("BlockHeadersInsert")
defer log.Tracef("BlockHeadersInsert exit")

// XXX at start of day lastRecord contains the last canonical
// downloaded blockheader. Thus if it is on a fork it will not ask for
// headers on what the network may be doing. Not sure how to handle
// that right now but leaving a note.

if len(bhs.Headers) == 0 {
return tbcd.ITInvalid, nil, nil, 0,
errors.New("block headers insert: invalid")
}

// Ensure we can connect these blockheaders prior to starting database
// transaction. This also obtains the starting cumulative difficulty
// and height.
wbh := bhs.Headers[0]
pbh, err := l.BlockHeaderByHash(ctx, &wbh.PrevBlock)
if err != nil {
return tbcd.ITInvalid, nil, nil, 0,
fmt.Errorf("block headers insert: %w", err)
}

// block headers
bhsTx, bhsCommit, bhsDiscard, err := l.startTransaction(level.BlockHeadersDB)
if err != nil {
Expand All @@ -459,6 +444,9 @@ func (l *ldb) BlockHeadersInsert(ctx context.Context, bhs *wire.MsgHeaders) (tbc
}
defer bhsDiscard()

// Ensure we can connect these blockheaders. This also obtains the
// starting cumulative difficulty and height.
//
// Iterate over the block headers and skip block headers we already
// have in the database. Rely on caching to make this not suck terribly.
var x int
Expand All @@ -480,6 +468,14 @@ func (l *ldb) BlockHeadersInsert(ctx context.Context, bhs *wire.MsgHeaders) (tbc
database.DuplicateError("block headers insert duplicate")
}

// Obtain current and previous blockheader.
wbh := bhs.Headers[0]
pbh, err := l.BlockHeaderByHash(ctx, &wbh.PrevBlock)
if err != nil {
return tbcd.ITInvalid, nil, nil, 0,
fmt.Errorf("block headers insert: %w", err)
}

// blocks missing
bmTx, bmCommit, bmDiscard, err := l.startTransaction(level.BlocksMissingDB)
if err != nil {
Expand Down

0 comments on commit ee45b5c

Please sign in to comment.