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