-
Notifications
You must be signed in to change notification settings - Fork 0
/
mattcoin.go
101 lines (87 loc) · 1.91 KB
/
mattcoin.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
package main
import (
"context"
"errors"
"flag"
"fmt"
"github.com/mweiden/basis/problems/blockchain"
"github.com/oklog/oklog/pkg/group"
"io"
"log"
"math/rand"
"net"
"net/http"
"os"
"os/signal"
"syscall"
"time"
)
var (
host = flag.String("host", "localhost", "Bind address for HTTP server")
port = flag.Int("port", 5000, "Bind port for HTTP server")
)
func interrupt(cancel <-chan struct{}) error {
c := make(chan os.Signal, 1)
signal.Notify(c, syscall.SIGINT, syscall.SIGTERM)
select {
case sig := <-c:
return fmt.Errorf("received signal %s", sig)
case <-cancel:
return errors.New("canceled")
}
}
// main
func main() {
log.SetOutput(os.Stdout)
flag.Parse()
listen := fmt.Sprintf("%s:%d", *host, *port)
log.SetPrefix(fmt.Sprintf("[%s] ", listen))
rand.Seed(int64(*port))
api := blockchain.NewAPI(
blockchain.NewBlockchain(blockchain.UnixTime),
listen,
func(url string) io.ReadCloser {
response, _ := http.Get(url)
return response.Body
},
)
var g group.Group
ctx, cancel := context.WithCancel(context.Background())
cronBeat := make(chan bool)
go func() {
for {
// TODO: This is kind of a hack to fake different mining completions
// between instances of mattcoin
sleepTime := (10 + time.Duration(rand.Int()%5)) * time.Second
time.Sleep(sleepTime)
cronBeat <- true
}
}()
// api
g.Add(
func() error { return api.Run(ctx) },
func(error) { cancel() },
)
// mining cron
g.Add(
func() error { return api.Cron(ctx, cronBeat) },
func(error) { cancel() },
)
// http server
mux := http.NewServeMux()
mux.Handle("/", api)
log.Printf("blockchain:web listening on %s", listen)
ln, _ := net.Listen("tcp", listen)
g.Add(
func() error { return http.Serve(ln, api) },
func(error) { ln.Close() },
)
// signal catcher
cancelChan := make(chan struct{})
g.Add(
func() error { return interrupt(cancelChan) },
func(error) { close(cancelChan) },
)
// run
g.Run()
}