package v2peer import ( "fmt" "time" btcdhash "github.com/btcsuite/btcd/chainhash/v2" "github.com/btcsuite/btcd/wire/v2" "git.smesh.lol/nostrino/pkg/btcwire" "git.smesh.lol/btcd-lite/internal/store" ) // SyncHeaders brings a header store up to what the peer has: the headers above // the store's tip, written in the store's own record. // // The store refuses a record that does not link to the one before it, so what // this adds is the walk: ask, check the linkage in the answer, write, ask // again from the new tip. Nothing is trusted because a peer sent it; the store // is what accepts a header. // // logf receives one progress line per hundred thousand headers and the line // that says the peer is at its tip. An error carries the height it stopped at, // because "the sync failed" without the height says nothing about where. func (p *Peer) SyncHeaders(headers *store.HeaderStore, logf func(string, ...any)) error { if logf == nil { logf = func(string, ...any) {} } tip, tipHash, ok := headers.HeaderTip() if !ok { return fmt.Errorf("v2peer: the header store has no tip") } startTip, start := tip, time.Now() lastReport := uint32(0) // prev is the tip in btcd's order: that is what a locator carries and what // an incoming header holds in its PrevBlock, so the comparison is made // there and the store's order only appears in what is written down. prev := toVendor(tipHash) for { got, err := p.Headers([]btcdhash.Hash{prev}, 2*time.Minute) if err != nil { return fmt.Errorf("v2peer: header sync stopped at %d after %d headers in %s: %w", tip, tip-startTip, time.Since(start).Round(time.Second), err) } // getheaders from a locator repeats the locator's own header in the // first answer; a record above the tip starts after it. if len(got) > 0 && got[0].BlockHash() == prev { got = got[1:] } if len(got) == 0 { logf("v2peer: headers to %d, the peer's tip, %d headers in %s", tip, tip-startTip, time.Since(start).Round(time.Second)) return nil } batch := make([]btcwire.BlockHeader, 0, len(got)) for i, hdr := range got { if hdr.PrevBlock != prev { return fmt.Errorf("v2peer: header %d links to %v, the tip is %v", tip+uint32(i)+1, hdr.PrevBlock, btcdhash.Hash(prev)) } prev = hdr.BlockHash() batch = append(batch, headerFromVendor(hdr)) } if err := headers.PutHeaders(tip+1, batch); err != nil { return fmt.Errorf("v2peer: header store at %d: %w", tip+1, err) } tip += uint32(len(batch)) // One line per hundred thousand headers: a full sync from genesis is // about ten of them, which is enough to see it working and to see the // rate, and few enough to leave in a log. if tip-lastReport >= headerReportEvery { logf("v2peer: headers to %d, %.0f/s, %d in %s", tip, float64(tip-startTip)/time.Since(start).Seconds(), tip-startTip, time.Since(start).Round(time.Second)) lastReport = tip } } } // headerReportEvery is how many headers one progress line accounts for. const headerReportEvery uint32 = 100000 // fromVendor is toVendor the other way: a copy, for the same reason. func fromVendor(h [32]byte) [32]byte { return [32]byte(h) } // The two hash types hold the same bytes: btcd's String reverses them for // display and nostrino's does not, which is a difference in printing and not // in storage. A hash moving between them is a copy. func toVendor(h [32]byte) btcdhash.Hash { return btcdhash.Hash(h) } // fromVendor is btcd's header record in the form the store keeps, which is the // same eighty bytes. func headerFromVendor(hdr *wire.BlockHeader) btcwire.BlockHeader { var out btcwire.BlockHeader out.Version = hdr.Version out.PrevBlock = fromVendor([32]byte(hdr.PrevBlock)) out.MerkleRoot = fromVendor([32]byte(hdr.MerkleRoot)) out.Timestamp = uint32(hdr.Timestamp.Unix()) out.Bits = hdr.Bits out.Nonce = hdr.Nonce return out }