-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.go
136 lines (112 loc) · 3.89 KB
/
main.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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
package main
import (
"../../pkg/dledger"
"flag"
"github.com/asticode/go-astikit"
"github.com/asticode/go-astilectron"
bootstrap "github.com/asticode/go-astilectron-bootstrap"
"log"
"net"
"sync"
"time"
)
var (
w *astilectron.Window
)
const (
defaultPort = "8080" // Use this default port if it isn't specified via command line arguments.
updatePeriod = 50 * time.Millisecond
)
func main() {
flag.Parse()
// Create logger
l := log.New(log.Writer(), log.Prefix(), log.Flags())
var mut sync.Mutex
mut.Lock()
go bootstrap.Run(bootstrap.Options{
AstilectronOptions: astilectron.Options{
AppName: "HashgraphDemo",
SingleInstance: true,
VersionAstilectron: "0.39.0",
VersionElectron: "7.1.10",
},
Logger: l,
OnWait: func(_ *astilectron.Astilectron, ws []*astilectron.Window, _ *astilectron.Menu, _ *astilectron.Tray, _ *astilectron.Menu) error {
w = ws[0]
mut.Unlock()
return nil
},
Windows: []*bootstrap.Window{{
Homepage: "index.html",
MessageHandler: handleMessages,
Options: &astilectron.WindowOptions{
BackgroundColor: astikit.StrPtr("#fff"),
Center: astikit.BoolPtr(true),
Height: astikit.IntPtr(736),
Width: astikit.IntPtr(1080),
},
}},
})
mut.Lock()
port := defaultPort
localAddr := getLocalAddress()
peers := make(map[string]string)
peers[localAddr+":8080"] = "Ahmet"
peers[localAddr+":9090"] = "Erhan"
peers[localAddr+":7070"] = "Öznur"
peers[localAddr+":6060"] = "Waris"
_ = bootstrap.SendMessage(w, "peers", peers)
distributedLedger := dledger.NewDLedgerFromPeers(port, peers)
distributedLedger.WaitForPeers()
distributedLedger.Start()
knownConsensusEvents := 0
knownHashgraphEvents := make(map[string]int, len(peers)+1)
firstRoundOfFameUndecided := make(map[string]uint32, len(peers)+1)
firstRoundOfFameUndecided[distributedLedger.MyAddress] = 0
for addr := range peers {
firstRoundOfFameUndecided[addr] = 0
}
for {
time.Sleep(updatePeriod)
distributedLedger.Node.RWMutex.RLock()
for addr := range distributedLedger.Node.Hashgraph {
for _, event := range distributedLedger.Node.Hashgraph[addr][knownHashgraphEvents[addr]:] {
_ = bootstrap.SendMessage(w, "event", event)
}
knownHashgraphEvents[addr] = len(distributedLedger.Node.Hashgraph[addr])
}
for _, newConsensusEvent := range distributedLedger.Node.ConsensusEvents[knownConsensusEvents:] {
_ = bootstrap.SendMessage(w, "event", newConsensusEvent)
}
for addr, rofu := range distributedLedger.Node.FirstRoundOfFameUndecided {
if rofu > firstRoundOfFameUndecided[addr] {
for i := firstRoundOfFameUndecided[addr]; i < rofu; i++ {
witness, ok := distributedLedger.Node.Witnesses[addr][i]
if ok {
_ = bootstrap.SendMessage(w, "event", witness)
}
}
firstRoundOfFameUndecided[addr] = rofu
}
}
knownConsensusEvents = len(distributedLedger.Node.ConsensusEvents)
distributedLedger.Node.RWMutex.RUnlock()
}
}
func handleMessages(_ *astilectron.Window, m bootstrap.MessageIn) (payload interface{}, err error) {
return
}
func getLocalAddress() string {
conn, err := net.Dial("udp", "eng.ku.edu.tr:80")
handleError(err)
defer func() {
handleError(conn.Close())
}()
localAddr := conn.LocalAddr().(*net.UDPAddr)
return localAddr.IP.String()
}
func handleError(e error) {
if e != nil {
panic(e)
}
}