github.com/koko1123/flow-go-1@v0.29.6/network/stub/hash.go (about)

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