Skip to content

Commit

Permalink
Update header.go
Browse files Browse the repository at this point in the history
  • Loading branch information
jwinkler2083233 committed Sep 14, 2021
1 parent ca296f5 commit 99186f0
Showing 1 changed file with 11 additions and 7 deletions.
18 changes: 11 additions & 7 deletions model/flow/header.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package flow
import (
"bytes"
"encoding/json"
"sync"
"time"

"github.com/fxamacker/cbor/v2"
Expand Down Expand Up @@ -73,18 +74,17 @@ func (h Header) Fingerprint() []byte {
return fingerprint.Fingerprint(h.Body())
}

var mutex_hdr sync.Mutex
var previd_hdr Identifier
var prev_hdr Header

// this does a binary byte-by-byte comparison of identifiers
// an Identifier is just a 32-byte array
func compareIdentifiers(a Identifier, b Identifier) int {
for i := 0; i < 32; i++ {
if a[i] != b[i] {
return -1
}
if a == b {
return 0
}
return 0
return -1
}

// ID returns a unique ID to singularly identify the header and its block
Expand All @@ -96,6 +96,11 @@ func (h Header) ID() Identifier {
h.Timestamp = h.Timestamp.UTC()
}

// guarantee serialized access
mutex_hdr.Lock()

defer mutex_hdr.Unlock()

for {
// compare these elements individually
if prev_hdr.ParentVoterIDs == nil ||
Expand All @@ -109,8 +114,7 @@ func (h Header) ID() Identifier {
bNotEqual := false
if len(h.ParentVoterIDs) > 0 {
for i, v := range h.ParentVoterIDs {
if len(prev_hdr.ParentVoterIDs) >= i &&
compareIdentifiers(v, prev_hdr.ParentVoterIDs[i]) != 0 {
if compareIdentifiers(v, prev_hdr.ParentVoterIDs[i]) != 0 {
bNotEqual = true
break
}
Expand Down

0 comments on commit 99186f0

Please sign in to comment.