forked from elastos/Elastos.ELA
/
inventory.go
212 lines (183 loc) · 4.82 KB
/
inventory.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
205
206
207
208
209
210
211
212
package message
import (
. "Elastos.ELA/common"
"Elastos.ELA/common/config"
"Elastos.ELA/common/log"
"Elastos.ELA/common/serialization"
"Elastos.ELA/core/ledger"
. "Elastos.ELA/net/protocol"
"bytes"
"crypto/sha256"
"encoding/binary"
"encoding/hex"
"fmt"
"io"
"time"
)
type InvPayload struct {
Cnt uint32
Blk []byte
}
type Inv struct {
messageHeader
P InvPayload
}
func SendMsgSyncBlockHeaders(node Noder, blocator []Uint256, hash Uint256) {
if node.LocalNode().GetStartHash() == blocator[0] &&
node.LocalNode().GetStopHash() == hash {
return
}
buf, err := NewBlocksReq(blocator, hash)
if err != nil {
log.Error("failed build a new getblocksReq")
} else {
node.LocalNode().SetSyncHeaders(true)
node.SetSyncHeaders(true)
go node.Tx(buf)
node.LocalNode().SetStartHash(blocator[0])
node.LocalNode().SetStopHash(hash)
}
}
func NewBlocksReq(blocator []Uint256, hash Uint256) ([]byte, error) {
var msg blocksReq
msg.Magic = config.Parameters.Magic
cmd := "getblocks"
copy(msg.CMD[0:len(cmd)], cmd)
tmpBuffer := bytes.NewBuffer([]byte{})
msg.p.len = uint32(len(blocator))
msg.p.hashStart = blocator
serialization.WriteUint32(tmpBuffer, uint32(msg.p.len))
for _, hash := range blocator {
_, err := hash.Serialize(tmpBuffer)
if err != nil {
return nil, err
}
}
msg.p.hashEnd = hash
_, err := msg.p.hashEnd.Serialize(tmpBuffer)
if err != nil {
return nil, err
}
p := new(bytes.Buffer)
err = binary.Write(p, binary.LittleEndian, tmpBuffer.Bytes())
if err != nil {
log.Error("Binary Write failed at new Msg")
return nil, err
}
s := sha256.Sum256(p.Bytes())
s2 := s[:]
s = sha256.Sum256(s2)
buf := bytes.NewBuffer(s[:4])
binary.Read(buf, binary.LittleEndian, &(msg.Checksum))
msg.Length = uint32(len(p.Bytes()))
log.Debug("The message payload length is ", msg.Length)
m, err := msg.Serialization()
if err != nil {
log.Error("Error Convert net message ", err.Error())
return nil, err
}
return m, nil
}
func (msg Inv) Handle(node Noder) error {
log.Debug()
var id Uint256
str := hex.EncodeToString(msg.P.Blk)
log.Debug(fmt.Sprintf("The inv type: no one. block len: %d, %s\n", len(msg.P.Blk), str))
log.Debug("RX block message")
if node.LocalNode().IsSyncHeaders() == true && node.IsSyncHeaders() == false {
return nil
}
//return nil
var i uint32
count := msg.P.Cnt
hashes := []Uint256{}
for i = 0; i < count; i++ {
id.Deserialize(bytes.NewReader(msg.P.Blk[HASHLEN*i:]))
hashes = append(hashes, id)
if ledger.DefaultLedger.Blockchain.IsKnownOrphan(&id) {
orphanRoot := ledger.DefaultLedger.Blockchain.GetOrphanRoot(&id)
locator, err := ledger.DefaultLedger.Blockchain.LatestBlockLocator()
if err != nil {
log.Errorf(" Failed to get block "+
"locator for the latest block: "+
"%v", err)
continue
}
SendMsgSyncBlockHeaders(node, locator, *orphanRoot)
continue
}
if i == (count - 1) {
var emptyHash Uint256
blocator := ledger.DefaultLedger.Blockchain.BlockLocatorFromHash(&id)
SendMsgSyncBlockHeaders(node, blocator, emptyHash)
}
}
for _, h := range hashes {
// TODO check the ID queue
if !ledger.DefaultLedger.BlockInLedger(h) {
if !(node.LocalNode().RequestedBlockExisted(h) || ledger.DefaultLedger.Blockchain.IsKnownOrphan(&h)) {
<-time.After(time.Millisecond * 50)
ReqBlkData(node, h)
}
}
}
return nil
}
func (msg Inv) Serialization() ([]byte, error) {
hdrBuf, err := msg.messageHeader.Serialization()
if err != nil {
return nil, err
}
buf := bytes.NewBuffer(hdrBuf)
msg.P.Serialization(buf)
return buf.Bytes(), err
}
func (msg *Inv) Deserialization(p []byte) error {
err := msg.messageHeader.Deserialization(p)
if err != nil {
return err
}
buf := bytes.NewBuffer(p[MSGHDRLEN:])
if err != nil {
return err
}
msg.P.Cnt, err = serialization.ReadUint32(buf)
if err != nil {
return err
}
msg.P.Blk = make([]byte, msg.P.Cnt*HASHLEN)
err = binary.Read(buf, binary.LittleEndian, &(msg.P.Blk))
return err
}
func NewInv(inv *InvPayload) ([]byte, error) {
var msg Inv
msg.P.Blk = inv.Blk
msg.P.Cnt = inv.Cnt
msg.Magic = config.Parameters.Magic
cmd := "inv"
copy(msg.CMD[0:len(cmd)], cmd)
tmpBuffer := bytes.NewBuffer([]byte{})
inv.Serialization(tmpBuffer)
b := new(bytes.Buffer)
err := binary.Write(b, binary.LittleEndian, tmpBuffer.Bytes())
if err != nil {
log.Error("Binary Write failed at new Msg", err.Error())
return nil, err
}
s := sha256.Sum256(b.Bytes())
s2 := s[:]
s = sha256.Sum256(s2)
buf := bytes.NewBuffer(s[:4])
binary.Read(buf, binary.LittleEndian, &(msg.Checksum))
msg.Length = uint32(len(b.Bytes()))
m, err := msg.Serialization()
if err != nil {
log.Error("Error Convert net message ", err.Error())
return nil, err
}
return m, nil
}
func (msg *InvPayload) Serialization(w io.Writer) {
serialization.WriteUint32(w, msg.Cnt)
binary.Write(w, binary.LittleEndian, msg.Blk)
}