Skip to content

Commit dca8600

Browse files
committed
Separate hash from stable hash.
1 parent 923e9a1 commit dca8600

File tree

1 file changed

+15
-7
lines changed

1 file changed

+15
-7
lines changed

pkg/parser/tree.go

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -686,30 +686,38 @@ func Hash(h string) string {
686686
return base58.Encode(hash[:])
687687
}
688688

689-
// HashRule to provide a unique stable identity for the rule. It can be used for dupe detection.
690-
// The hash is based on the rule's content, excluding metadata that is not semantically important.
689+
// HashRule to provide a unique identity for the rule.
690+
// The hash is based on the rule's content, excluding previous hash calculations.
691691

692692
func HashRule(rule ParseRuleT) (string, error) {
693+
rule.Metadata.Hash = "" // Hash is what we are generating here, not semantically important
694+
return _hashRule(rule)
695+
}
696+
697+
// StableHash to provide a unique stable identity for the rule. It can be used for dupe detection.
698+
// The hash is based on the rule's content, excluding metadata that is not semantically important.
693699

694-
// Strip out versioning metadata before calculating the hash.
700+
func StableHash(rule ParseRuleT) (string, error) {
701+
702+
// Strip out versioning metadata before calculating the stable hash.
695703
// The versioning metadata is not semantically important for the rule's content,
696704
// so we can safely ignore it for the purpose of hashing.
697705
// This is important to ensure that the hash remains consistent across changes
698706
// that do not affect the rule's content, such as version bumps or metadata changes.
699707

700-
// The field rule.Metadata.Id is considered part of the rules identity and should be included in the hash.
708+
// The field rule.Metadata.Id is considered part of the rules identity and should be included in the stable hash.
701709
// Rules can change over time having the following properties:
702710
// - Metadata.Id: Unique identifier for the rule, which is immutable for the lifetime of the rule.
703711
// - Metadata.Hash: A hash of the rule's content, which is regenerated on every semantic change.
704712
// - Metadata.Version: A version string that *should* be incremented on changes, but is not semantically important.
705713
// - Metadata.Gen: A generation counter that is incremented on every change, but is not semantically important.
706714

707-
// NOTE: Modify 'rule' is acceptable because parameter passed by value.
708-
709715
rule.Metadata.Gen = 0 // Gen is bumped on every semantic change, so we don't want it in the hash
710-
rule.Metadata.Hash = "" // Hash is what we are generating here, not semantically important
711716
rule.Metadata.Version = "" // Version may be bumped on change, also not semantically important
717+
return HashRule(rule)
718+
}
712719

720+
func _hashRule(rule ParseRuleT) (string, error) {
713721
// json.Marshal to produce deterministic output
714722
jsonBytes, err := json.Marshal(rule)
715723
if err != nil {

0 commit comments

Comments
 (0)