github.com/onflow/flow-go@v0.35.7-crescendo-preview.23-atree-inlining/fvm/storage/snapshot/execution_snapshot.go (about)

     1  package snapshot
     2  
     3  import (
     4  	"strings"
     5  
     6  	"golang.org/x/exp/slices"
     7  
     8  	"github.com/onflow/flow-go/fvm/meter"
     9  	"github.com/onflow/flow-go/model/flow"
    10  )
    11  
    12  type ExecutionSnapshot struct {
    13  	// Note that the ReadSet only include reads from the storage snapshot.
    14  	// Reads from the WriteSet are excluded from the ReadSet.
    15  	ReadSet map[flow.RegisterID]struct{}
    16  
    17  	WriteSet map[flow.RegisterID]flow.RegisterValue
    18  
    19  	// Note that the spock secret may be nil if the view does not support spock.
    20  	SpockSecret []byte
    21  
    22  	// Note that the meter may be nil if the view does not support metering.
    23  	*meter.Meter
    24  }
    25  
    26  // UpdatedRegisters returns all registers that were updated by this view.
    27  // The returned entries are sorted by ids.
    28  func (snapshot *ExecutionSnapshot) UpdatedRegisters() flow.RegisterEntries {
    29  	entries := make(flow.RegisterEntries, 0, len(snapshot.WriteSet))
    30  	for key, value := range snapshot.WriteSet {
    31  		entries = append(entries, flow.RegisterEntry{Key: key, Value: value})
    32  	}
    33  
    34  	slices.SortFunc(entries, func(a, b flow.RegisterEntry) int {
    35  		ownerCmp := strings.Compare(a.Key.Owner, b.Key.Owner)
    36  		if ownerCmp != 0 {
    37  			return ownerCmp
    38  		}
    39  		return strings.Compare(a.Key.Key, b.Key.Key)
    40  	})
    41  
    42  	return entries
    43  }
    44  
    45  // UpdatedRegisterSet returns all registers that were updated by this view.
    46  func (snapshot *ExecutionSnapshot) UpdatedRegisterSet() map[flow.RegisterID]flow.RegisterValue {
    47  	return snapshot.WriteSet
    48  }
    49  
    50  // UpdatedRegisterIDs returns all register ids that were updated by this
    51  // view.  The returned ids are unsorted.
    52  func (snapshot *ExecutionSnapshot) UpdatedRegisterIDs() []flow.RegisterID {
    53  	ids := make([]flow.RegisterID, 0, len(snapshot.WriteSet))
    54  	for key := range snapshot.WriteSet {
    55  		ids = append(ids, key)
    56  	}
    57  	return ids
    58  }
    59  
    60  // ReadRegisterIDs returns a list of register ids that were read.
    61  // The returned ids are unsorted
    62  func (snapshot *ExecutionSnapshot) ReadRegisterIDs() []flow.RegisterID {
    63  	ret := make([]flow.RegisterID, 0, len(snapshot.ReadSet))
    64  	for k := range snapshot.ReadSet {
    65  		ret = append(ret, k)
    66  	}
    67  	return ret
    68  }
    69  
    70  // AllRegisterIDs returns all register ids that were read / write by this
    71  // view. The returned ids are unsorted.
    72  func (snapshot *ExecutionSnapshot) AllRegisterIDs() []flow.RegisterID {
    73  	set := make(
    74  		map[flow.RegisterID]struct{},
    75  		len(snapshot.ReadSet)+len(snapshot.WriteSet))
    76  	for reg := range snapshot.ReadSet {
    77  		set[reg] = struct{}{}
    78  	}
    79  	for reg := range snapshot.WriteSet {
    80  		set[reg] = struct{}{}
    81  	}
    82  	ret := make([]flow.RegisterID, 0, len(set))
    83  	for r := range set {
    84  		ret = append(ret, r)
    85  	}
    86  	return ret
    87  }