-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
70 lines (63 loc) · 1.65 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
package main
import (
"crypto/sha256"
"github.com/jinzhu/configor"
log "github.com/sirupsen/logrus"
"github.com/u-speak/poc/chain"
"github.com/u-speak/poc/util"
"google.golang.org/grpc"
"strconv"
"time"
)
const version = "0.0.1"
func main() {
err := configor.Load(&Config, "config.yml")
if err != nil {
log.Fatal(err)
}
log.Infof("Starting uspeak poc version %s", version)
log.SetLevel(log.DebugLevel)
c := chain.New(validateBeginningZero)
mineAndAdd("Bootstrap Block 1", c)
mineAndAdd("Bootstrap Block 2", c)
mineAndAdd("foo", c)
mineAndAdd("bar", c)
ns := &NodeServer{chain: c, remoteConnections: make(map[string]*grpc.ClientConn)}
go ns.Run()
// Connect the node to itself
err = ns.Connect(Config.NodeNetwork.Interface + ":" + strconv.Itoa(Config.NodeNetwork.Port))
if err != nil {
log.Fatal(err)
}
go startWeb(c, ns)
repl(c, ns)
log.Info("Shutting down by interactive command")
ns.Shutdown()
}
func mineAndAdd(content string, c *chain.Chain) uint {
nonce := mine(content, c.LastHash())
logIfError(c.AddData(content, nonce))
return nonce
}
func validateBeginningZero(h [32]byte) bool {
return h[0] == 0
}
func logIfError(err error) {
if err != nil {
log.Error(err)
}
}
func mine(content string, prev [32]byte) uint {
start := time.Now()
log.WithField("prev", util.CompactEmoji(prev)).Debugf("Started mining %s", content)
var i uint
for i = 0; i < ^uint(0); i++ {
h := sha256.Sum256([]byte(string(prev[:]) + content + string(i)))
if validateBeginningZero(h) {
elapsed := time.Since(start)
log.WithField("elapsed", elapsed).Debugf("Found Nonce %d for %s", i, content)
return i
}
}
panic("Block impossible to mine")
}