github.com/koko1123/flow-go-1@v0.29.6/state/protocol/inmem/snapshot.go (about)

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