github.com/onflow/flow-go@v0.33.17/fvm/storage/derived/table_invalidator.go (about)

     1  package derived
     2  
     3  import (
     4  	"github.com/onflow/flow-go/fvm/storage/logical"
     5  	"github.com/onflow/flow-go/fvm/storage/snapshot"
     6  )
     7  
     8  type TableInvalidator[TKey comparable, TVal any] interface {
     9  	// This returns true if the this invalidates any data
    10  	ShouldInvalidateEntries() bool
    11  
    12  	// This returns true if the table entry should be invalidated.
    13  	ShouldInvalidateEntry(TKey, TVal, *snapshot.ExecutionSnapshot) bool
    14  }
    15  
    16  type tableInvalidatorAtTime[TKey comparable, TVal any] struct {
    17  	TableInvalidator[TKey, TVal]
    18  
    19  	executionTime logical.Time
    20  }
    21  
    22  // NOTE: chainedInvalidator assumes that the entries are order by non-decreasing
    23  // execution time.
    24  type chainedTableInvalidators[TKey comparable, TVal any] []tableInvalidatorAtTime[TKey, TVal]
    25  
    26  func (chained chainedTableInvalidators[TKey, TVal]) ApplicableInvalidators(
    27  	toValidateTime logical.Time,
    28  ) chainedTableInvalidators[TKey, TVal] {
    29  	// NOTE: switch to bisection search (or reverse iteration) if the list
    30  	// is long.
    31  	for idx, entry := range chained {
    32  		if toValidateTime <= entry.executionTime {
    33  			return chained[idx:]
    34  		}
    35  	}
    36  
    37  	return nil
    38  }
    39  
    40  func (chained chainedTableInvalidators[TKey, TVal]) ShouldInvalidateEntries() bool {
    41  	for _, invalidator := range chained {
    42  		if invalidator.ShouldInvalidateEntries() {
    43  			return true
    44  		}
    45  	}
    46  
    47  	return false
    48  }
    49  
    50  func (chained chainedTableInvalidators[TKey, TVal]) ShouldInvalidateEntry(
    51  	key TKey,
    52  	value TVal,
    53  	snapshot *snapshot.ExecutionSnapshot,
    54  ) bool {
    55  	for _, invalidator := range chained {
    56  		if invalidator.ShouldInvalidateEntry(key, value, snapshot) {
    57  			return true
    58  		}
    59  	}
    60  
    61  	return false
    62  }