Skip to content

Commit

Permalink
neutrino: Add block header writer implementation.
Browse files Browse the repository at this point in the history
This commit introduces a new structure to decouple
the process of writing `wire.BlockHeaders` to the
block header store from the blockmanager.

Signed-off-by: Ononiwu Maureen <[email protected]>
  • Loading branch information
Ononiwu Maureen authored and Chinwendu20 committed Apr 10, 2024
1 parent d44808f commit 23ce89c
Showing 1 changed file with 37 additions and 0 deletions.
37 changes: 37 additions & 0 deletions neutrino.go
Original file line number Diff line number Diff line change
Expand Up @@ -1864,3 +1864,40 @@ func (b *blockHeaderValidator) checkHeaderSanity(blockHeader *wire.BlockHeader,
emptyFlags,
)
}

type blkHeaderWriter struct {
store headerfs.BlockHeaderStore
tipHeight uint32
}

func (b *blkHeaderWriter) ChainTip() (*chainhash.Hash, uint32, error) {
header, height, err := b.store.ChainTip()

hash := header.BlockHash()

return &hash, height, err
}

func (b *blkHeaderWriter) Write(headers []*wire.BlockHeader) error {
headerWriteBatch := make([]headerfs.BlockHeader, 0, len(headers))

tipHeight := b.tipHeight
for _, header := range headers {
tipHeight++
headerWriteBatch = append(
headerWriteBatch, headerfs.BlockHeader{
BlockHeader: header,
Height: tipHeight,
},
)
}

err := b.store.WriteHeaders(headerWriteBatch...)
if err != nil {
return err
}

b.tipHeight = tipHeight

return nil
}

0 comments on commit 23ce89c

Please sign in to comment.