/
wired_getchain.go
204 lines (170 loc) · 5.15 KB
/
wired_getchain.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
202
203
204
package wired
import (
"bytes"
"encoding/binary"
"encoding/hex"
"github.com/c0mm4nd/rlp"
"github.com/google/uuid"
"github.com/libp2p/go-libp2p-core/network"
"github.com/libp2p/go-libp2p-core/peer"
"github.com/pkg/errors"
"github.com/ngchain/ngbiz/ngp2p/defaults"
"github.com/ngchain/ngbiz/ngtypes"
"github.com/ngchain/ngbiz/utils"
)
func (w *Wired) SendGetChain(peerID peer.ID, from [][]byte, to []byte) (id []byte, stream network.Stream, err error) {
// avoid nil
if to == nil {
to = ngtypes.GetEmptyHash()
}
payload, err := rlp.EncodeToBytes(&GetChainPayload{
From: from,
To: to,
})
if err != nil {
err = errors.Wrap(err, "failed to encode data into rlp")
log.Debug(err)
return nil, nil, err
}
id, _ = uuid.New().MarshalBinary()
// create message data
req := &Message{
Header: NewHeader(w.host, w.network, id, GetChainMsg),
Payload: payload,
}
// sign the data
signature, err := Signature(w.host, req)
if err != nil {
err = errors.Wrap(err, "failed to sign pb data")
log.Debug(err)
return nil, nil, err
}
// add the signature to the message
req.Header.Sign = signature
stream, err = Send(w.host, w.protocolID, peerID, req)
if err != nil {
log.Debug(err)
return nil, nil, err
}
log.Debugf("getchain to: %s was sent. Message Id: %x, request blocks: %s to %s", peerID, req.Header.ID, fmtFromField(from), string(to))
return req.Header.ID, stream, nil
}
// RULE:
// request [[from a...from b]...to]
// if to is nil, converging mode on
//
// converging mode:
// 1. check all hashes in db and try to find existing one(samepoint)
// 2. if none, return nil
// 3. if index==0,return everything back
//
// sync mode:
// parse request to [[peerHeight], to]
// return [peerHeight+1, ..., to].
func (w *Wired) onGetChain(stream network.Stream, msg *Message) {
log.Debugf("Received getchain request from %s.", stream.Conn().RemotePeer())
var getChainPayload GetChainPayload
err := rlp.DecodeBytes(msg.Payload, &getChainPayload)
if err != nil {
w.sendReject(msg.Header.ID, stream, err)
return
}
blocks := make([]*ngtypes.Block, 0, defaults.MaxBlocks)
if getChainPayload.From == nil || len(getChainPayload.From) == 0 && len(getChainPayload.To) == 16 {
// fetching mode
from := binary.LittleEndian.Uint64(getChainPayload.To[0:8])
to := binary.LittleEndian.Uint64(getChainPayload.To[8:16])
for blockHeight := from; blockHeight <= to; blockHeight++ {
cur, err := w.chain.GetBlockByHeight(blockHeight)
if err != nil {
err := errors.Wrapf(err, "chain lacks block@%d", blockHeight)
log.Error(err)
w.sendReject(msg.Header.ID, stream, err)
return
}
blocks = append(blocks, cur)
}
w.sendChain(msg.Header.ID, stream, blocks...)
return
}
log.Debugf("getchain requests from %x to %x", getChainPayload.From[0], getChainPayload.To)
// init cur
cur, err := w.chain.GetBlockByHash(getChainPayload.From[0])
if err != nil {
err = errors.Wrapf(err, "cannot get block by hash %x", getChainPayload.From[0])
log.Error(err)
w.sendReject(msg.Header.ID, stream, err)
return
}
// run converging mode
if len(getChainPayload.To) == 16 {
var samepointIndex int
// do hashes check first
for samepointIndex < len(getChainPayload.From) {
_, err := w.chain.GetBlockByHash(getChainPayload.From[samepointIndex])
if err == nil {
// err == nil means found the samepoint
break
}
samepointIndex++
}
if samepointIndex == len(getChainPayload.From) {
// not found samepoint, return nil
from := binary.LittleEndian.Uint64(getChainPayload.To[0:8])
to := binary.LittleEndian.Uint64(getChainPayload.To[8:16])
for blockHeight := from; blockHeight <= to; blockHeight++ {
cur, err = w.chain.GetBlockByHeight(blockHeight)
if err != nil {
err := errors.Wrapf(err, "chain lacks block@%d", blockHeight)
log.Debug(err)
w.sendReject(msg.Header.ID, stream, err)
return
}
blocks = append(blocks, cur)
}
w.sendChain(msg.Header.ID, stream, blocks...)
return
}
// not include this point
cur, err = w.chain.GetBlockByHash(getChainPayload.From[samepointIndex])
if err != nil {
w.sendReject(msg.Header.ID, stream, err)
return
}
for i := 0; i < len(getChainPayload.From)-1-samepointIndex; i++ {
blockHeight := cur.Header.Height + 1
cur, err = w.chain.GetBlockByHeight(blockHeight)
if err != nil {
err := errors.Wrapf(err, "chain lacks block@%d", blockHeight)
log.Debug(err)
w.sendReject(msg.Header.ID, stream, err)
return
}
blocks = append(blocks, cur)
}
} else if len(getChainPayload.To) == 32 {
// fetch mode
for i := 0; i < defaults.MaxBlocks; i++ {
// never reach To
if bytes.Equal(cur.GetHash(), getChainPayload.To) {
break
}
nextHeight := cur.Header.Height + 1
cur, err = w.chain.GetBlockByHeight(nextHeight)
if err != nil {
log.Debugf("local chain lacks block@%d: %s", nextHeight, err)
break
}
blocks = append(blocks, cur)
}
}
w.sendChain(msg.Header.ID, stream, blocks...)
}
func fmtFromField(from [][]byte) string {
hashes := make([]string, len(from))
for i := 0; i < len(from); i++ {
hashes[i] = hex.EncodeToString(from[i])
}
json, _ := utils.JSON.MarshalToString(hashes)
return json
}