github.com/MetalBlockchain/metalgo@v1.11.9/snow/engine/snowman/bootstrap/interval/blocks.go (about)

     1  // Copyright (C) 2019-2024, Ava Labs, Inc. All rights reserved.
     2  // See the file LICENSE for licensing terms.
     3  
     4  package interval
     5  
     6  import "github.com/MetalBlockchain/metalgo/database"
     7  
     8  // Add the block to the tree and return if the parent block should be fetched,
     9  // but wasn't desired before.
    10  func Add(
    11  	db database.KeyValueWriterDeleter,
    12  	tree *Tree,
    13  	lastAcceptedHeight uint64,
    14  	height uint64,
    15  	blkBytes []byte,
    16  ) (bool, error) {
    17  	if height <= lastAcceptedHeight || tree.Contains(height) {
    18  		return false, nil
    19  	}
    20  
    21  	if err := PutBlock(db, height, blkBytes); err != nil {
    22  		return false, err
    23  	}
    24  	if err := tree.Add(db, height); err != nil {
    25  		return false, err
    26  	}
    27  
    28  	// We know that height is greater than lastAcceptedHeight here, so height-1
    29  	// is guaranteed not to underflow.
    30  	nextHeight := height - 1
    31  	return nextHeight != lastAcceptedHeight && !tree.Contains(nextHeight), nil
    32  }
    33  
    34  // Remove the block from the tree.
    35  func Remove(
    36  	db database.KeyValueWriterDeleter,
    37  	tree *Tree,
    38  	height uint64,
    39  ) error {
    40  	if err := DeleteBlock(db, height); err != nil {
    41  		return err
    42  	}
    43  	return tree.Remove(db, height)
    44  }