diff --git a/database/tbcd/level/level.go b/database/tbcd/level/level.go index a115ed61e..6b9155fe5 100644 --- a/database/tbcd/level/level.go +++ b/database/tbcd/level/level.go @@ -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 { @@ -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 @@ -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 {