github.com/onflow/flow-go@v0.33.17/state/protocol/inmem/snapshot.go (about)

     1  package inmem
     2  
     3  import (
     4  	"github.com/onflow/flow-go/consensus/hotstuff/model"
     5  	"github.com/onflow/flow-go/model/flow"
     6  	"github.com/onflow/flow-go/state/protocol"
     7  )
     8  
     9  // Snapshot is a memory-backed implementation of protocol.Snapshot. The snapshot
    10  // data is stored in the embedded encodable snapshot model, which defines the
    11  // canonical structure of an encoded snapshot for the purposes of serialization.
    12  type Snapshot struct {
    13  	enc EncodableSnapshot
    14  }
    15  
    16  var _ protocol.Snapshot = (*Snapshot)(nil)
    17  
    18  func (s Snapshot) Head() (*flow.Header, error) {
    19  	return s.enc.Head, nil
    20  }
    21  
    22  func (s Snapshot) QuorumCertificate() (*flow.QuorumCertificate, error) {
    23  	return s.enc.QuorumCertificate, nil
    24  }
    25  
    26  func (s Snapshot) Identities(selector flow.IdentityFilter) (flow.IdentityList, error) {
    27  	return s.enc.Identities.Filter(selector), nil
    28  }
    29  
    30  func (s Snapshot) Identity(nodeID flow.Identifier) (*flow.Identity, error) {
    31  	identity, ok := s.enc.Identities.ByNodeID(nodeID)
    32  	if !ok {
    33  		return nil, protocol.IdentityNotFoundError{NodeID: nodeID}
    34  	}
    35  	return identity, nil
    36  }
    37  
    38  func (s Snapshot) Commit() (flow.StateCommitment, error) {
    39  	return s.enc.LatestSeal.FinalState, nil
    40  }
    41  
    42  func (s Snapshot) SealedResult() (*flow.ExecutionResult, *flow.Seal, error) {
    43  	return s.enc.LatestResult, s.enc.LatestSeal, nil
    44  }
    45  
    46  func (s Snapshot) SealingSegment() (*flow.SealingSegment, error) {
    47  	return s.enc.SealingSegment, nil
    48  }
    49  
    50  func (s Snapshot) Descendants() ([]flow.Identifier, error) {
    51  	// canonical snapshots don't have any descendants
    52  	return nil, nil
    53  }
    54  
    55  func (s Snapshot) Phase() (flow.EpochPhase, error) {
    56  	return s.enc.Phase, nil
    57  }
    58  
    59  func (s Snapshot) RandomSource() ([]byte, error) {
    60  	return model.BeaconSignature(s.enc.QuorumCertificate)
    61  }
    62  
    63  func (s Snapshot) Epochs() protocol.EpochQuery {
    64  	return Epochs{s.enc.Epochs}
    65  }
    66  
    67  func (s Snapshot) Params() protocol.GlobalParams {
    68  	return Params{s.enc.Params}
    69  }
    70  
    71  func (s Snapshot) Encodable() EncodableSnapshot {
    72  	return s.enc
    73  }
    74  
    75  func (s Snapshot) VersionBeacon() (*flow.SealedVersionBeacon, error) {
    76  	return s.enc.SealedVersionBeacon, nil
    77  }
    78  
    79  func SnapshotFromEncodable(enc EncodableSnapshot) *Snapshot {
    80  	return &Snapshot{
    81  		enc: enc,
    82  	}
    83  }
    84  
    85  // StrippedInmemSnapshot removes all the networking address in the snapshot
    86  func StrippedInmemSnapshot(snapshot EncodableSnapshot) EncodableSnapshot {
    87  	removeAddress := func(ids flow.IdentityList) {
    88  		for _, identity := range ids {
    89  			identity.Address = ""
    90  		}
    91  	}
    92  
    93  	removeAddressFromEpoch := func(epoch *EncodableEpoch) {
    94  		if epoch == nil {
    95  			return
    96  		}
    97  		removeAddress(epoch.InitialIdentities)
    98  		for _, cluster := range epoch.Clustering {
    99  			removeAddress(cluster)
   100  		}
   101  		for _, c := range epoch.Clusters {
   102  			removeAddress(c.Members)
   103  		}
   104  	}
   105  
   106  	removeAddress(snapshot.Identities)
   107  	removeAddressFromEpoch(snapshot.Epochs.Previous)
   108  	removeAddressFromEpoch(&snapshot.Epochs.Current)
   109  	removeAddressFromEpoch(snapshot.Epochs.Next)
   110  
   111  	for _, event := range snapshot.LatestResult.ServiceEvents {
   112  		switch event.Type {
   113  		case flow.ServiceEventSetup:
   114  			removeAddress(event.Event.(*flow.EpochSetup).Participants)
   115  		}
   116  	}
   117  	return snapshot
   118  }