-
Notifications
You must be signed in to change notification settings - Fork 0
/
base.go
207 lines (185 loc) · 5.45 KB
/
base.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
/*
* Copyright 2019 Insolar Technologies
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
// Package reply represents responses to messages of the messagebus
package reply
import (
"bytes"
"encoding/gob"
"io"
"github.com/insolar/insolar/core"
"github.com/pkg/errors"
)
const (
// Generic
// TypeError is reply with error.
TypeError = core.ReplyType(iota + 1)
// TypeOK is a generic reply for signaling a positive result.
TypeOK
// TypeNotOK is a generic reply for signaling a negative result.
TypeNotOK
// TypeGetCodeRedirect is a redirect reply for code-call
TypeGetCodeRedirect
// TypeGetObjectRedirect is a redirect reply for object-call
TypeGetObjectRedirect
// TypeGetChildrenRedirect is a redirect reply for children-call
TypeGetChildrenRedirect
// Logicrunner
// TypeCallMethod - two binary fields: data and results.
TypeCallMethod
// TypeCallConstructor - reference on created object
TypeCallConstructor
// TypeRegisterRequest - request for execution was registered
TypeRegisterRequest
// Ledger
// TypeCode is code from storage.
TypeCode
// TypeObject is object from storage.
TypeObject
// TypeDelegate is delegate reference from storage.
TypeDelegate
// TypeID is common reply for methods returning id to lifeline states.
TypeID
// TypeChildren is a reply for fetching objects children in chunks.
TypeChildren
// TypeObjectIndex contains serialized object index. It can be stored in DB without processing.
TypeObjectIndex
// TypeJetMiss is returned for miscalculated jets due to incomplete jet tree.
TypeJetMiss
// TypePendingRequests contains unclosed requests for an object.
TypePendingRequests
// TypeJet contains jet.
TypeJet
// TypeRequest contains request.
TypeRequest
// TypeHeavyError carries heavy record sync
TypeHeavyError
TypeNodeSign
)
// ErrType is used to determine and compare reply errors.
type ErrType int
const (
// ErrDeactivated returned when requested object is deactivated.
ErrDeactivated = iota + 1
// ErrStateNotAvailable is returned when requested object is deactivated.
ErrStateNotAvailable
// ErrHotDataTimeout is returned when no hot data received for a specific jet
ErrHotDataTimeout
// ErrNoPendingRequests is returned when there are no pending requests on current LME
ErrNoPendingRequests
// ErrTooManyPendingRequests is returned when a limit of pending requests has been reached
ErrTooManyPendingRequests
)
func getEmptyReply(t core.ReplyType) (core.Reply, error) {
switch t {
case TypeCallMethod:
return &CallMethod{}, nil
case TypeCallConstructor:
return &CallConstructor{}, nil
case TypeRegisterRequest:
return &RegisterRequest{}, nil
case TypeCode:
return &Code{}, nil
case TypeObject:
return &Object{}, nil
case TypeDelegate:
return &Delegate{}, nil
case TypeID:
return &ID{}, nil
case TypeChildren:
return &Children{}, nil
case TypeError:
return &Error{}, nil
case TypeHeavyError:
return &HeavyError{}, nil
case TypeOK:
return &OK{}, nil
case TypeObjectIndex:
return &ObjectIndex{}, nil
case TypeGetCodeRedirect:
return &GetCodeRedirectReply{}, nil
case TypeGetObjectRedirect:
return &GetObjectRedirectReply{}, nil
case TypeGetChildrenRedirect:
return &GetChildrenRedirectReply{}, nil
case TypeJetMiss:
return &JetMiss{}, nil
case TypePendingRequests:
return &HasPendingRequests{}, nil
case TypeJet:
return &Jet{}, nil
case TypeRequest:
return &Request{}, nil
case TypeNodeSign:
return &NodeSign{}, nil
default:
return nil, errors.Errorf("unimplemented reply type: '%d'", t)
}
}
// Serialize returns encoded reply.
func Serialize(reply core.Reply) (io.Reader, error) {
buff := &bytes.Buffer{}
_, err := buff.Write([]byte{byte(reply.Type())})
if err != nil {
return nil, err
}
enc := gob.NewEncoder(buff)
err = enc.Encode(reply)
return buff, err
}
// Deserialize returns decoded reply.
func Deserialize(buff io.Reader) (core.Reply, error) {
b := make([]byte, 1)
_, err := buff.Read(b)
if err != nil {
return nil, errors.New("too short input to deserialize a message reply")
}
reply, err := getEmptyReply(core.ReplyType(b[0]))
if err != nil {
return nil, err
}
enc := gob.NewDecoder(buff)
err = enc.Decode(reply)
return reply, err
}
// ToBytes deserializes reply to bytes.
func ToBytes(rep core.Reply) []byte {
repBuff, err := Serialize(rep)
if err != nil {
panic("failed to serialize reply")
}
return repBuff.(*bytes.Buffer).Bytes()
}
func init() {
gob.Register(&CallMethod{})
gob.Register(&CallConstructor{})
gob.Register(&RegisterRequest{})
gob.Register(&Code{})
gob.Register(&Object{})
gob.Register(&Delegate{})
gob.Register(&ID{})
gob.Register(&Children{})
gob.Register(&Error{})
gob.Register(&OK{})
gob.Register(&ObjectIndex{})
gob.Register(&GetCodeRedirectReply{})
gob.Register(&GetObjectRedirectReply{})
gob.Register(&GetChildrenRedirectReply{})
gob.Register(&HeavyError{})
gob.Register(&JetMiss{})
gob.Register(&NodeSign{})
gob.Register(&HasPendingRequests{})
gob.Register(&Request{})
}