-
Notifications
You must be signed in to change notification settings - Fork 11
/
chainsync.go
115 lines (107 loc) · 2.63 KB
/
chainsync.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
package chainsync
import (
"github.com/cloudstruct/go-ouroboros-network/protocol"
)
const (
PROTOCOL_NAME = "chain-sync"
PROTOCOL_ID_NTN uint16 = 2
PROTOCOL_ID_NTC uint16 = 5
)
var (
STATE_IDLE = protocol.NewState(1, "Idle")
STATE_CAN_AWAIT = protocol.NewState(2, "CanAwait")
STATE_MUST_REPLY = protocol.NewState(3, "MustReply")
STATE_INTERSECT = protocol.NewState(4, "Intersect")
STATE_DONE = protocol.NewState(5, "Done")
)
var StateMap = protocol.StateMap{
STATE_IDLE: protocol.StateMapEntry{
Agency: protocol.AGENCY_CLIENT,
Transitions: []protocol.StateTransition{
{
MsgType: MESSAGE_TYPE_REQUEST_NEXT,
NewState: STATE_CAN_AWAIT,
},
{
MsgType: MESSAGE_TYPE_FIND_INTERSECT,
NewState: STATE_INTERSECT,
},
{
MsgType: MESSAGE_TYPE_DONE,
NewState: STATE_DONE,
},
},
},
STATE_CAN_AWAIT: protocol.StateMapEntry{
Agency: protocol.AGENCY_SERVER,
Transitions: []protocol.StateTransition{
{
MsgType: MESSAGE_TYPE_AWAIT_REPLY,
NewState: STATE_MUST_REPLY,
},
{
MsgType: MESSAGE_TYPE_ROLL_FORWARD,
NewState: STATE_IDLE,
},
{
MsgType: MESSAGE_TYPE_ROLL_BACKWARD,
NewState: STATE_IDLE,
},
},
},
STATE_INTERSECT: protocol.StateMapEntry{
Agency: protocol.AGENCY_SERVER,
Transitions: []protocol.StateTransition{
{
MsgType: MESSAGE_TYPE_INTERSECT_FOUND,
NewState: STATE_IDLE,
},
{
MsgType: MESSAGE_TYPE_INTERSECT_NOT_FOUND,
NewState: STATE_IDLE,
},
},
},
STATE_MUST_REPLY: protocol.StateMapEntry{
Agency: protocol.AGENCY_SERVER,
Transitions: []protocol.StateTransition{
{
MsgType: MESSAGE_TYPE_ROLL_FORWARD,
NewState: STATE_IDLE,
},
{
MsgType: MESSAGE_TYPE_ROLL_BACKWARD,
NewState: STATE_IDLE,
},
},
},
STATE_DONE: protocol.StateMapEntry{
Agency: protocol.AGENCY_NONE,
},
}
type ChainSync struct {
Client *Client
Server *Server
}
type Config struct {
AwaitReplyFunc AwaitReplyFunc
RollBackwardFunc RollBackwardFunc
RollForwardFunc RollForwardFunc
IntersectFoundFunc IntersectFoundFunc
IntersectNotFoundFunc IntersectNotFoundFunc
DoneFunc DoneFunc
}
// Callback function types
type AwaitReplyFunc func() error
type RollBackwardFunc func(interface{}, interface{}) error
type RollForwardFunc func(uint, interface{}) error
type IntersectFoundFunc func(interface{}, interface{}) error
type IntersectNotFoundFunc func(interface{}) error
type DoneFunc func() error
func New(protoOptions protocol.ProtocolOptions, cfg *Config) *ChainSync {
c := &ChainSync{
Client: NewClient(protoOptions, cfg),
Server: NewServer(protoOptions, cfg),
}
return c
}