-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathutils.go
44 lines (38 loc) · 966 Bytes
/
utils.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
package main
import (
"crypto/sha256"
"encoding/hex"
"fmt"
"time"
)
func calculateHash(index uint64, term int, prevHash string, data Transaction, timestamp uint64) string {
txData := fmt.Sprint(data.Value) + data.Input + data.Output
payload := fmt.Sprint(index) + fmt.Sprint(term) + prevHash + txData + fmt.Sprint(timestamp)
h := sha256.New()
h.Write([]byte(payload))
hashed := h.Sum(nil)
return hex.EncodeToString(hashed)
}
func calculateHashForBlock(block *Block) string {
return calculateHash(block.Index, block.Term, block.PreviousHash, block.Data, block.Timestamp)
}
func logState(n *Node) string {
switch n.State {
case follower:
return "Follower"
case candidate:
return "Candidate"
case leader:
return "Leader"
case shutdown:
return "Shutdown"
default:
return "Unknown"
}
}
func setTimeout(duration time.Duration) <-chan time.Time {
if duration == 0 {
return nil
}
return time.After(duration + (5 * time.Millisecond))
}