Skip to content

Commit

Permalink
neutrino: Create checkpoints structure
Browse files Browse the repository at this point in the history
Signed-off-by: Ononiwu Maureen <[email protected]>
  • Loading branch information
Ononiwu Maureen committed Mar 12, 2024
1 parent 317ead5 commit 7d1497c
Showing 1 changed file with 141 additions and 0 deletions.
141 changes: 141 additions & 0 deletions checkpoints.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
package neutrino

import (
"github.com/btcsuite/btcd/chaincfg"
"github.com/btcsuite/btcd/chaincfg/chainhash"
"sort"
)

type filterHdrCheckpoints struct {
checkpoints map[uint32]*chainhash.Hash
checkpointHeight []uint32
}

func newfilterHdrCheckpoints(checkpoints map[uint32]*chainhash.
Hash) *filterHdrCheckpoints {
// Check for nil
var heights []uint32
for height, _ := range checkpoints {
heights = append(heights, height)
}

sort.Slice(heights, func(i, j int) bool {
return heights[i] < heights[j]
})
return &filterHdrCheckpoints{
checkpoints: checkpoints,
checkpointHeight: heights,
}
}

// FetchCheckpoint fetches checkpoint at a particular index
func (b *filterHdrCheckpoints) FetchCheckpoint(idx int32) (uint32,
chainhash.Hash) {

height := b.checkpointHeight[idx]
hash := b.checkpoints[height]

return height, *hash
}

func (b *filterHdrCheckpoints) FindNextHeaderCheckpoint(height int32) (
int32, int32, *chainhash.Hash) {

for i, chkptHeight := range b.checkpointHeight {
if int32(chkptHeight) > height {

return int32(i), int32(chkptHeight), b.checkpoints[chkptHeight]
}
}

return 0, 0, nil
}

func (b *filterHdrCheckpoints) FindPreviousHeaderCheckpoint(height int32) (
int32, int32, *chainhash.Hash) {

for i := b.Len() - 1; i >= 0; i-- {
if int32(b.checkpointHeight[i]) < height {

return int32(i), int32(b.checkpointHeight[i]),
b.checkpoints[b.checkpointHeight[i]]
}
}
// Return genesis cfheader instead
return 0, 0, nil
}

func (b *filterHdrCheckpoints) Len() int {
return len(b.checkpoints)
}

type blockHeaderCheckpoints struct {
checkpoints []chaincfg.Checkpoint
}

func newblockHeaderCheckpoints(checkpoints []chaincfg.
Checkpoint) *blockHeaderCheckpoints {

return &blockHeaderCheckpoints{
checkpoints: checkpoints,
}
}

func (b *blockHeaderCheckpoints) FetchCheckpoint(idx int32) (int32, chainhash.
Hash) {

checkpoint := b.checkpoints[idx]

return checkpoint.Height, *checkpoint.Hash
}

func (b *blockHeaderCheckpoints) FindNextHeaderCheckpoint(height int32) (
int32, int32, *chainhash.Hash) {

checkpoints := b.checkpoints
// There is no next checkpoint if the height is already after the final
// checkpoint.
finalCheckpoint := &checkpoints[len(checkpoints)-1]
if height >= finalCheckpoint.Height {
return 0, 0, nil
}

// Find the next checkpoint.
nextCheckpoint := finalCheckpoint
var i int32
for i = int32(len(checkpoints) - 2); i >= 0; i-- {
if height >= checkpoints[i].Height {
break
}
nextCheckpoint = &checkpoints[i]
}

return i, nextCheckpoint.Height, nextCheckpoint.Hash
}

func (b *blockHeaderCheckpoints) FindPreviousHeaderCheckpoint(height int32) (
int32, int32, chainhash.Hash) {

// Start with the genesis block - earliest checkpoint to which our code
// will want to reset
// prevCheckpoint := &chaincfg.Checkpoint{
// Height: 0,
// Hash: b.cf.GenesisHash,
// }

// Find the latest checkpoint lower than height or return genesis block
// if there are none.
var (
prevCheckpoint *chaincfg.Checkpoint
i int
)
checkpoints := b.checkpoints
for i = 0; i < len(checkpoints); i++ {
if height <= checkpoints[i].Height {
break
}
prevCheckpoint = &checkpoints[i]
}

return int32(i), prevCheckpoint.Height, *prevCheckpoint.Hash
}

0 comments on commit 7d1497c

Please sign in to comment.