From 23ce89c8f1de0df1de17f63bd077d219cfaafdc8 Mon Sep 17 00:00:00 2001 From: Ononiwu Maureen Date: Tue, 19 Mar 2024 07:50:04 +0100 Subject: [PATCH] neutrino: Add block header writer implementation. 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 --- neutrino.go | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/neutrino.go b/neutrino.go index e9f2c91b..4a64cfb8 100644 --- a/neutrino.go +++ b/neutrino.go @@ -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 +}