-
Notifications
You must be signed in to change notification settings - Fork 0
/
global.go
268 lines (221 loc) · 6.15 KB
/
global.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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
//
// Copyright 2019 Insolar Technologies GmbH
//
// 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 reference
import (
"encoding/json"
"io"
"github.com/pkg/errors"
"github.com/insolar/insolar/longbits"
)
const (
LocalBinaryHashSize = 28
LocalBinaryPulseAndScopeSize = 4
LocalBinarySize = LocalBinaryPulseAndScopeSize + LocalBinaryHashSize
GlobalBinarySize = 2 * LocalBinarySize
)
/* For LIMITED USE ONLY - can only be used by observer/analytical code */
func NewRecordRef(recID Local) Global {
if recID.getScope() != 0 {
panic("illegal value")
}
return Global{addressLocal: recID}
}
func NewSelfRef(localID Local) Global {
if localID.getScope() == baseScopeReserved {
panic("illegal value")
}
return Global{addressLocal: localID, addressBase: localID}
}
func NewGlobal(domainID, localID Local) Global {
return Global{addressLocal: localID, addressBase: domainID}
}
type Global struct {
addressLocal Local
addressBase Local
}
func (v *Global) GetScope() Scope {
return Scope(v.addressBase.getScope()<<2 | v.addressLocal.getScope())
}
func (v *Global) WriteTo(w io.Writer) (int64, error) {
n, err := v.addressLocal.WriteTo(w)
if err != nil {
return n, err
}
n2, err := v.addressBase.WriteTo(w)
return n + n2, err
}
func (v *Global) Read(p []byte) (int, error) {
n, err := v.addressLocal.Read(p)
if err != nil || n < v.addressLocal.len() {
return n, err
}
n2, err := v.addressBase.Read(p[n:])
return n + n2, err
}
func (v *Global) AsByteString() longbits.ByteString {
return longbits.NewByteString(v.AsBytes())
}
func (v *Global) AsBytes() []byte {
prefix := v.addressLocal.len()
val := make([]byte, prefix+v.addressBase.len())
_, _ = v.addressLocal.Read(val)
_, _ = v.addressBase.Read(val[prefix:])
return val
}
// IsEmpty - check for void
func (v Global) IsEmpty() bool {
return v.addressLocal.IsEmpty() && v.addressBase.IsEmpty()
}
func (v *Global) IsRecordScope() bool {
return v.addressBase.IsEmpty() && !v.addressLocal.IsEmpty() && v.addressLocal.getScope() == baseScopeLifeline
}
func (v *Global) IsObjectReference() bool {
return !v.addressBase.IsEmpty() && !v.addressLocal.IsEmpty() && v.addressLocal.getScope() == baseScopeLifeline
}
func (v *Global) IsSelfScope() bool {
return v.addressBase == v.addressLocal
}
func (v *Global) IsLifelineScope() bool {
return v.addressBase.getScope() == baseScopeLifeline
}
func (v *Global) IsLocalDomainScope() bool {
return v.addressBase.getScope() == baseScopeLocalDomain
}
func (v *Global) IsGlobalScope() bool {
return v.addressBase.getScope() == baseScopeGlobal
}
func (v *Global) GetParity() []byte {
panic("not implemented")
}
func (v *Global) CheckParity(bytes []byte) error {
panic("not implemented")
}
/* ONLY for parser */
func (v *Global) tryConvertToSelf() bool {
if !v.addressBase.IsEmpty() {
return false
}
v.addressBase = v.addressLocal
return true
}
/* ONLY for parser */
func (v *Global) tryApplyBase(base *Global) bool {
if !v.addressBase.IsEmpty() {
return false
}
if !base.IsSelfScope() {
switch base.GetScope() {
case LocalDomainMember, GlobalDomainMember:
break
default:
return false
}
}
v.addressBase = base.addressLocal
return true
}
// GetBase returns base address from Global.
func (v Global) GetBase() *Local {
return &v.addressBase
}
// GetLocal returns local address from Global.
func (v Global) GetLocal() *Local {
return &v.addressLocal
}
// String outputs base58 Reference representation.
func (v Global) String() string {
repr, err := DefaultEncoder().Encode(&v)
if err != nil {
return NilRef
}
return repr
}
// Bytes returns byte slice of Reference
func (v Global) Bytes() []byte {
return v.AsBytes()
}
// Equal checks if reference points to the same record.
func (v Global) Equal(other Global) bool {
return v.addressLocal.Equal(other.addressLocal) && v.addressBase.Equal(other.addressBase)
}
// Compare compares two record references
func (v Global) Compare(other Global) int {
if cmp := v.addressBase.Compare(other.addressBase); cmp != 0 {
return cmp
}
return v.addressLocal.Compare(other.addressLocal)
}
// MarshalJSON serializes reference into JSONFormat.
func (v *Global) MarshalJSON() ([]byte, error) {
if v == nil {
return json.Marshal(nil)
}
return json.Marshal(v.String())
}
func (v Global) Marshal() ([]byte, error) {
return v.AsBytes(), nil
}
func (v Global) MarshalBinary() ([]byte, error) {
return v.Marshal()
}
func (v Global) MarshalTo(data []byte) (int, error) {
if len(data) < GlobalBinarySize {
return 0, errors.New("not enough bytes to marshal reference.Global")
}
return copy(data, v.AsBytes()), nil
}
func (v *Global) UnmarshalJSON(data []byte) error {
var repr interface{}
err := json.Unmarshal(data, &repr)
if err != nil {
return errors.Wrap(err, "failed to unmarshal reference.Global")
}
switch realRepr := repr.(type) {
case string:
*v, err = DefaultDecoder().Decode(realRepr)
if err != nil {
return errors.Wrap(err, "failed to unmarshal reference.Global")
}
case nil:
default:
return errors.Wrapf(err, "unexpected type %T when unmarshal reference.Global", repr)
}
return nil
}
func (v *Global) Unmarshal(data []byte) error {
if len(data) < GlobalBinarySize {
return errors.New("not enough bytes to unmarshal reference.Global")
}
{
localWriter := v.addressLocal.asWriter()
for i := 0; i < LocalBinarySize; i++ {
_ = localWriter.WriteByte(data[i])
}
}
{
baseWriter := v.addressBase.asWriter()
for i := 0; i < LocalBinarySize; i++ {
_ = baseWriter.WriteByte(data[LocalBinarySize+i])
}
}
return nil
}
func (v *Global) UnmarshalBinary(data []byte) error {
return v.Unmarshal(data)
}
func (v Global) Size() int {
return GlobalBinarySize
}