forked from elastos/Elastos.ELA
/
verack.go
79 lines (68 loc) · 2.31 KB
/
verack.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
package message
import (
"Elastos.ELA/common/log"
. "Elastos.ELA/net/protocol"
"encoding/hex"
"errors"
"strconv"
)
type verACK struct {
messageHeader
// No payload
}
func NewVerack() ([]byte, error) {
var msg verACK
// Fixme the check is the []byte{0} instead of 0
var sum []byte
sum = []byte{0x5d, 0xf6, 0xe0, 0xe2}
msg.messageHeader.init("verack", sum, 0)
buf, err := msg.Serialization()
if err != nil {
return nil, err
}
str := hex.EncodeToString(buf)
log.Debug("The message tx verack length is ", len(buf), ", ", str)
return buf, err
}
/*
* The node state switch table after rx message, there is time limitation for each action
* The Hanshake status will switch to Init after TIMEOUT if not received the VerACK
* in this time window
* _______________________________________________________________________
* | | Init | HandShake | Establish | Inactive |
* |-----------------------------------------------------------------------|
* | version | HandShake(timer)| | | HandShake(timer)|
* | | if helloTime > 3| Tx verack | Depend on | if helloTime > 3|
* | | Tx version | | node update| Tx version |
* | | then Tx verack | | | then Tx verack |
* |-----------------------------------------------------------------------|
* | verack | | Establish | | |
* | | No Action | | No Action | No Action |
* |------------------------------------------------------------------------
*
*/
// TODO The process should be adjusted based on above table
func (msg verACK) Handle(node Noder) error {
s := node.GetState()
if s != HandShake && s != HandShaked {
log.Warn("Unknow status to received verack")
return errors.New("Unknow status to received verack")
}
node.SetState(Establish)
if s == HandShake {
buf, _ := NewVerack()
node.Tx(buf)
}
//node.DumpInfo()
// Fixme, there is a race condition here,
// but it doesn't matter to access the invalid
// node which will trigger a warning
if node.LocalNode().NeedMoreAddresses() {
node.ReqNeighborList()
}
addr := node.GetAddr()
port := node.GetPort()
nodeAddr := addr + ":" + strconv.Itoa(int(port))
node.LocalNode().RemoveAddrInConnectingList(nodeAddr)
return nil
}