-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
48 lines (39 loc) · 1.03 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
package main
import (
"log"
"net"
"./proto"
"./server"
"google.golang.org/grpc"
"golang.org/x/net/context"
)
func main(){
listener, err := net.Listen("tcp",":8080")
if err != nil{
log.Fatalf("unable to listen on 8080 port: %v", err)
}
srv := grpc.NewServer()
proto.RegisterBlockchainServer(srv, &Server{
Blockchain: blockchain.NewBlockchain()})
srv.Serve(listener)
}
type Server struct{
Blockchain *blockchain.Blockchain
}
func (s *Server) AddBlock(ctx context.Context,in *proto.AddBlockRequest) (*proto.AddBlockResponse, error){
block := s.Blockchain.AddBlock(in.Data)
return &proto.AddBlockResponse{
Hash: block.Hash,
},nil
}
func (s *Server) GetBlockchain(ctx context.Context,in *proto.GetBlockchainRequest) (*proto.GetBlockchainResponse, error){
resp := new(proto.GetBlockchainResponse)
for _, b := range s.Blockchain.Blocks {
resp.Blocks = append(resp.Blocks, &proto.Block{
PrevBlockHash: b.PrevBlockHash,
Hash: b.Hash,
Data: b.Data,
})
}
return resp, nil
}