github.com/decred/dcrlnd@v0.7.6/sweep/bucket_list.go (about)

     1  package sweep
     2  
     3  // bucket contains a set of inputs that are not mutually exclusive.
     4  type bucket pendingInputs
     5  
     6  // tryAdd tries to add a new input to this bucket.
     7  func (b bucket) tryAdd(input *pendingInput) bool {
     8  	exclusiveGroup := input.params.ExclusiveGroup
     9  	if exclusiveGroup != nil {
    10  		for _, input := range b {
    11  			existingGroup := input.params.ExclusiveGroup
    12  
    13  			// Don't add an exclusive group input if other inputs
    14  			// are non-exclusive. The exclusive group input may be
    15  			// invalid (for example in the case of commitment
    16  			// anchors) and could thereby block sweeping of the
    17  			// other inputs.
    18  			if existingGroup == nil {
    19  				return false
    20  			}
    21  
    22  			// Don't combine inputs from the same exclusive group.
    23  			// Because only one input is valid, this may result in
    24  			// txes that are always invalid.
    25  			if *existingGroup == *exclusiveGroup {
    26  				return false
    27  			}
    28  		}
    29  	}
    30  
    31  	b[*input.OutPoint()] = input
    32  
    33  	return true
    34  }
    35  
    36  // bucketList is a list of buckets that contain non-mutually exclusive inputs.
    37  type bucketList struct {
    38  	buckets []bucket
    39  }
    40  
    41  // add adds a new input. If the input is not accepted by any of the existing
    42  // buckets, a new bucket will be created.
    43  func (b *bucketList) add(input *pendingInput) {
    44  	for _, existingBucket := range b.buckets {
    45  		if existingBucket.tryAdd(input) {
    46  			return
    47  		}
    48  	}
    49  
    50  	// Create a new bucket and add the input. It is not necessary to check
    51  	// the return value of tryAdd because it will always succeed on an empty
    52  	// bucket.
    53  	newBucket := make(bucket)
    54  	newBucket.tryAdd(input)
    55  	b.buckets = append(b.buckets, newBucket)
    56  }