-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrpc_server.go
158 lines (134 loc) · 3.59 KB
/
rpc_server.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
package blc
import (
"context"
"encoding/json"
"errors"
"fmt"
log "github.com/treeforest/logger"
"net"
"sync"
"time"
pb "github.com/treeforest/easyblc/proto"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
)
func RunRpcSerer(port int, chain *BlockChain) *rpcServer {
if port < 0 {
panic("port < 0")
}
gSrv := grpc.NewServer(grpc.Creds(insecure.NewCredentials()))
s := &rpcServer{
chain: chain,
gSrv: gSrv,
wg: sync.WaitGroup{},
}
pb.RegisterApiServer(gSrv, s)
lis, err := net.Listen("tcp", fmt.Sprintf("0.0.0.0:%d", port))
if err != nil {
panic(err)
}
c := make(chan struct{}, 1)
s.wg.Add(1)
go func() {
defer s.wg.Done()
log.Info("rpc server listen at ", lis.Addr().String())
c <- struct{}{}
_ = gSrv.Serve(lis)
}()
<-c
return s
}
type rpcServer struct {
chain *BlockChain
gSrv *grpc.Server
wg sync.WaitGroup
stopOnce sync.Once
}
func (s *rpcServer) Stop() {
s.stopOnce.Do(func() {
s.gSrv.Stop()
s.wg.Done()
})
}
func (s *rpcServer) GetHeight(ctx context.Context, req *pb.GetHeightReq) (*pb.GetHeightResp, error) {
if s.chain.GetLatestBlock() == nil {
return nil, errors.New("no block")
}
return &pb.GetHeightResp{Height: s.chain.GetLatestBlock().Height}, nil
}
func (s *rpcServer) GetBlocks(ctx context.Context, req *pb.GetBlocksReq) (*pb.GetBlocksResp, error) {
blocks := make([]Block, 0)
for height := req.StartHeight; height <= req.EndHeight; height++ {
b, err := s.chain.GetBlock(height)
if err != nil {
break
}
blocks = append(blocks, *b)
}
data, _ := json.Marshal(blocks)
return &pb.GetBlocksResp{Blocks: data}, nil
}
func (s *rpcServer) GetBalance(ctx context.Context, req *pb.GetBalanceReq) (*pb.GetBalanceResp, error) {
return &pb.GetBalanceResp{Balance: s.chain.GetBalance(req.Address)}, nil
}
func (s *rpcServer) GetUTXO(ctx context.Context, req *pb.GetUTXOReq) (*pb.GetUTXOResp, error) {
utxoes := make([]pb.UTXO, 0)
s.chain.utxoSet.Traverse(func(txHash [32]byte, index int, output TxOutput) {
utxoes = append(utxoes, pb.UTXO{
TxHash: txHash[:],
Index: int32(index),
TxOut: pb.TxOutput{
Value: output.Value,
Address: output.Address,
ScriptPubKey: output.ScriptPubKey,
},
})
})
return &pb.GetUTXOResp{Utxos: utxoes}, nil
}
func (s *rpcServer) GetTxPool(context.Context, *pb.GetTxPoolReq) (*pb.GetTxPoolResp, error) {
pool, _ := s.chain.GetTxPool().Marshal()
return &pb.GetTxPoolResp{TxPool: pool}, nil
}
func (s *rpcServer) PostTx(ctx context.Context, req *pb.PostTxReq) (*pb.PostTxResp, error) {
hash, err := BytesToByte32(req.Tx.Hash)
if err != nil {
return &pb.PostTxResp{}, errors.New("format error")
}
if len(req.Tx.Ins) == 0 || len(req.Tx.Outs) == 0 {
return &pb.PostTxResp{}, errors.New("format error")
}
if time.Now().UnixNano() < req.Tx.Timestamp {
return &pb.PostTxResp{}, errors.New("timestamp error")
}
vins := make([]TxInput, 0)
vouts := make([]TxOutput, 0)
for _, in := range req.Tx.Ins {
txid, err := BytesToByte32(in.TxId)
if err != nil {
return &pb.PostTxResp{}, errors.New("format error")
}
vins = append(vins, TxInput{
TxId: txid,
Vout: in.Vout,
ScriptSig: in.ScriptSig,
CoinbaseDataSize: int(in.CoinbaseDataSize),
CoinbaseData: in.CoinbaseData,
})
}
for _, out := range req.Tx.Outs {
vouts = append(vouts, TxOutput{
Value: out.Value,
Address: out.Address,
ScriptPubKey: out.ScriptPubKey,
})
}
tx := Transaction{
Hash: hash,
Vins: vins,
Vouts: vouts,
Time: req.Tx.Timestamp,
}
err = s.chain.AddToTxPool(tx)
return &pb.PostTxResp{}, err
}