github.com/onflow/flow-go@v0.35.7-crescendo-preview.23-atree-inlining/network/stub/hash.go (about)

     1  package stub
     2  
     3  import (
     4  	"encoding/hex"
     5  	"fmt"
     6  
     7  	"github.com/onflow/crypto/hash"
     8  
     9  	"github.com/onflow/flow-go/model/encoding/json"
    10  	"github.com/onflow/flow-go/model/flow"
    11  	"github.com/onflow/flow-go/network/channels"
    12  )
    13  
    14  // eventKey generates a unique fingerprint for the tuple of (sender, event, type of event, channel)
    15  func eventKey(from flow.Identifier, channel channels.Channel, event interface{}) (string, error) {
    16  	marshaler := json.NewMarshaler()
    17  
    18  	tag, err := marshaler.Marshal([]byte(fmt.Sprintf("testthenetwork %s %T", channel, event)))
    19  	if err != nil {
    20  		return "", fmt.Errorf("could not encode the tag: %w", err)
    21  	}
    22  
    23  	payload, err := marshaler.Marshal(event)
    24  	if err != nil {
    25  		return "", fmt.Errorf("could not encode event: %w", err)
    26  	}
    27  
    28  	sender, err := marshaler.Marshal(from)
    29  	if err != nil {
    30  		return "", fmt.Errorf("could not encode sender: %w", err)
    31  	}
    32  
    33  	hasher := hash.NewSHA3_256()
    34  	_, err = hasher.Write(tag)
    35  	if err != nil {
    36  		return "", fmt.Errorf("could not write to hasher: %w", err)
    37  	}
    38  
    39  	_, err = hasher.Write(payload)
    40  	if err != nil {
    41  		return "", fmt.Errorf("could not write to hasher: %w", err)
    42  	}
    43  
    44  	_, err = hasher.Write(sender)
    45  	if err != nil {
    46  		return "", fmt.Errorf("could not write to hasher: %w", err)
    47  	}
    48  
    49  	hash := hasher.SumHash()
    50  	key := hex.EncodeToString(hash)
    51  	return key, nil
    52  
    53  }