-
Notifications
You must be signed in to change notification settings - Fork 211
/
handler.go
201 lines (175 loc) · 5.74 KB
/
handler.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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
package sync
import (
"github.com/spacemeshos/go-spacemesh/common/types"
"github.com/spacemeshos/go-spacemesh/common/util"
"github.com/spacemeshos/go-spacemesh/database"
"github.com/spacemeshos/go-spacemesh/log"
"github.com/spacemeshos/go-spacemesh/mesh"
)
func newLayerHashRequestHandler(layers *mesh.Mesh, logger log.Log) func(msg []byte) []byte {
return func(msg []byte) []byte {
lyrid := types.LayerID(util.BytesToUint64(msg))
logger.With().Info("handle layer hash request", lyrid)
layer, err := layers.GetLayer(lyrid)
if err != nil {
if err == database.ErrNotFound {
logger.With().Warning("hashes requested for unfamiliar layer ", lyrid)
return nil
}
logger.With().Error("Error handling layer request message", lyrid, log.Err(err))
return nil
}
return layer.Hash().Bytes()
}
}
func newAtxHashRequestHandler(s *Syncer, logger log.Log) func(msg []byte) []byte {
return func(msg []byte) []byte {
ep := types.EpochID(util.BytesToUint64(msg))
logger.With().Info("handle atx hash request", ep)
atxs := s.atxDb.GetEpochAtxs(ep)
return types.CalcATXIdsHash32(atxs, nil).Bytes()
}
}
func newLayerBlockIdsRequestHandler(layers *mesh.Mesh, logger log.Log) func(msg []byte) []byte {
return func(msg []byte) []byte {
logger.Debug("handle blockIds request")
lyrid := types.LayerID(util.BytesToUint64(msg))
layer, err := layers.GetLayer(lyrid)
if err != nil {
if err == database.ErrNotFound {
logger.With().Warning("block ids requested for unfamiliar layer (id: %s)", lyrid)
return nil
}
logger.With().Error("Error handling ids request message", lyrid, log.Err(err))
return nil
}
blocks := layer.Blocks()
ids := make([]types.BlockID, 0, len(blocks))
for _, b := range blocks {
ids = append(ids, b.ID())
}
idbytes, err := types.BlockIdsToBytes(ids)
if err != nil {
logger.Error("Error marshaling response message, with blocks IDs: %v and error:", ids, err)
return nil
}
logger.Debug("send ids response layer %v", lyrid)
return idbytes
}
}
func newEpochAtxsRequestHandler(s *Syncer, logger log.Log) func(msg []byte) []byte {
return func(msg []byte) []byte {
logger.Debug("handle atxId request")
ep := types.EpochID(util.BytesToUint64(msg))
atxs := s.atxDb.GetEpochAtxs(ep)
idbytes, err := types.ATXIdsToBytes(atxs)
if err != nil {
s.Log.With().Error("Error marshaling response message with atx IDs", types.AtxIdsField(atxs), log.Err(err))
return nil
}
logger.Debug("send atx ids response epoch %v", ep)
return idbytes
}
}
//todo better logs
func newBlockRequestHandler(msh *mesh.Mesh, logger log.Log) func(msg []byte) []byte {
return func(msg []byte) []byte {
var blockids []types.Hash32
if err := types.BytesToInterface(msg, &blockids); err != nil {
logger.With().Error("error unmarshalling block request message", log.Err(err))
return nil
}
var blocks []types.Block
logger.Info("handle block request ids: %s", concatShortIds(blockids))
for _, bid := range blockids {
blockID := types.BlockID(bid.ToHash20())
logger.With().Info("handle block request", blockID)
blk, err := msh.GetBlock(blockID)
if err != nil {
if err == database.ErrNotFound {
logger.With().Warning("unfamiliar block was requested", blockID, log.Err(err))
continue
}
logger.With().Error("error handling block request message", blockID, log.Err(err))
continue
}
blocks = append(blocks, *blk)
}
bbytes, err := types.InterfaceToBytes(blocks)
if err != nil {
logger.With().Error("error marshaling response message (FetchBlockResp)", log.Err(err))
return nil
}
logger.Info("send block response")
return bbytes
}
}
func newTxsRequestHandler(s *Syncer, logger log.Log) func(msg []byte) []byte {
return func(msg []byte) []byte {
var txids []types.TransactionID
err := types.BytesToInterface(msg, &txids)
if err != nil {
logger.Error("error marshalling request", err)
return nil
}
logger.With().Info("handle tx request", types.TxIdsField(txids))
txs, missingDB := s.GetTransactions(txids)
for t := range missingDB {
if tx, err := s.txpool.Get(t); err == nil {
txs = append(txs, tx)
} else {
logger.With().Warning("unfamiliar tx was requested", t)
}
}
bbytes, err := types.InterfaceToBytes(txs)
if err != nil {
logger.Error("Error marshaling transactions response message, with ids %v and err:", txs, err)
return nil
}
logger.Info("send tx response")
return bbytes
}
}
func newAtxsRequestHandler(s *Syncer, logger log.Log) func(msg []byte) []byte {
return func(msg []byte) []byte {
var atxids []types.ATXID
err := types.BytesToInterface(msg, &atxids)
if err != nil {
logger.Error("Unable to marshal request", err)
return nil
}
logger.With().Info("handle atx request", types.AtxIdsField(atxids))
atxs, unknownAtx := s.GetATXs(atxids)
for _, t := range unknownAtx {
if tx, err := s.atxDb.GetFullAtx(t); err == nil {
atxs[t] = tx
} else {
logger.With().Warning("unfamiliar atx requested", t)
}
}
var transactions []types.ActivationTx
for _, value := range atxs {
tx := *value
transactions = append(transactions, tx)
}
bbytes, err := types.InterfaceToBytes(transactions)
if err != nil {
logger.Error("Unable to marshal atx response message", err)
return nil
}
logger.Info("send atx response ")
return bbytes
}
}
func newPoetRequestHandler(s *Syncer, logger log.Log) func(msg []byte) []byte {
return func(proofRef []byte) []byte {
proofMessage, err := s.poetDb.GetProofMessage(proofRef)
shortPoetRef := proofRef[:util.Min(5, len(proofRef))]
if err != nil {
logger.Warning("unfamiliar PoET proof was requested (id: %x): %v", shortPoetRef, err)
return nil
}
logger.Info("returning PoET proof (id: %x) to neighbor", shortPoetRef)
return proofMessage
}
}