-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
123 lines (101 loc) · 2.37 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
package main
import (
// "code.google.com/p/gocask"
"encoding/gob"
"fmt"
"github.com/conformal/btcwire"
"log"
"time"
"os"
"os/signal"
)
var (
conf = Config{"btcfs", 0}
// db, _ = gocask.NewGocask(".")
getheaders = btcwire.NewMsgGetHeaders()
blockchain, dberr = InitializeBlockChain()
blockheaderchan = make(chan *btcwire.BlockHeader)
)
func init() {
gob.Register(btcwire.BlockHeader{})
getheaders.AddBlockLocatorHash(&btcwire.GenesisMerkleRoot)
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt)
go func(){
for sig := range c {
blockchain.Database.Close()
fmt.Println("Closed Database\n")
fmt.Println(sig)
return
}
}()
}
func main() {
addrs, err := FindPeers(10)
if err != nil {
fmt.Println(err)
return
}
fmt.Println("haters gonna hate")
vchan := make(chan bool)
for _, addr := range addrs {
peer := NewBTCPeer(addr, btcwire.MainPort, conf)
go func() {
if err := peer.Connect(); err != nil {
log.Fatal(err)
}
if err := peer.DoVersion(); err != nil {
log.Fatal(err)
}
fmt.Println("haters gonna hate")
peer.Write(getheaders)
fmt.Println("haters gonna hate")
go func() {
err := SrvHeaders(blockheaderchan)
if err != nil {
log.Printf("SrvHeaders failed: %s", err)
}
}()
vchan <- true
}()
select {
case <-vchan:
case <-time.After(5 * time.Second):
continue
}
ProcessMessages(peer)
}
}
func ProcessMessages(n *BTCPeer) error {
for {
in, err := n.Read()
if err != nil {
return fmt.Errorf("ProcessMessages: peer.Read: %s", err)
}
ProcessMessage(n, in.Command(), in)
}
return nil
}
func ProcessMessage(from *BTCPeer, msg string, data btcwire.Message) {
//log.Printf("ProcessMessage: %s %#v", msg, data)
switch msg {
case "headers":
hdrs := data.(*btcwire.MsgHeaders)
log.Printf("Received %d headers", len(hdrs.Headers))
for _, h := range hdrs.Headers {
blockheaderchan <- h
_, err := blockchain.InitializeBlock(h)
if err != nil {
log.Print(err)
}
}
// blockchain.Genesis.PrintSubtree(1)
log.Printf("Blockchain Head Depth: %d\n", blockchain.ChainHeadDepth)
log.Printf("Chain Head: %#v\n", blockchain.ChainHead.String())
getheaders := btcwire.NewMsgGetHeaders()
locator := blockchain.CreateLocator()
getheaders.BlockLocatorHashes = locator
from.Write(getheaders)
default:
}
}