-
Notifications
You must be signed in to change notification settings - Fork 39
/
object.go
228 lines (197 loc) · 4.27 KB
/
object.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
/*
* Copyright 2020 ICON Foundation
*
* 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 icobject
import (
"bytes"
"fmt"
"reflect"
"github.com/icon-project/goloop/common/codec"
"github.com/icon-project/goloop/common/db"
"github.com/icon-project/goloop/common/errors"
"github.com/icon-project/goloop/common/merkle"
"github.com/icon-project/goloop/common/trie"
)
const TypeReserved int = 30
const (
TypeBytes = TypeReserved + iota
)
type Tag int
func (t Tag) Type() int {
return int(t) >> 3
}
func (t Tag) Version() int {
return int(t) & 0x7
}
func MakeTag(t int, v int) Tag {
return Tag(t<<3 | (v & 0x7))
}
type Impl interface {
Version() int
RLPDecodeFields(decoder codec.Decoder) error
RLPEncodeFields(encoder codec.Encoder) error
Reset(dbase db.Database) error
Resolve(builder merkle.Builder) error
ClearCache()
Flush() error
Equal(o Impl) bool
}
type Object struct {
bytes []byte
tag Tag
real Impl
}
var ObjectType = reflect.TypeOf((*Object)(nil))
func (o *Object) Equal(object trie.Object) bool {
oo := object.(*Object)
if oo == o {
return true
}
if o.tag != oo.tag {
return false
}
return o.real.Equal(oo.real)
}
func (o *Object) Resolve(builder merkle.Builder) error {
return o.real.Resolve(builder)
}
func (o *Object) ClearCache() {
o.real.ClearCache()
}
func (o *Object) Reset(dbase db.Database, bs []byte) error {
factory := FactoryOf(dbase)
if factory == nil {
return errors.InvalidStateError.New("FactoryIsNotAttached")
}
d := codec.BC.NewDecoder(bytes.NewReader(bs))
if err := o.RLPDecodeSelf(d, factory); err != nil {
return err
}
if err := d.Close(); err != nil {
return err
}
o.bytes = bs
return o.real.Reset(dbase)
}
func (o *Object) Bytes() []byte {
if o.bytes == nil {
o.bytes = codec.BC.MustMarshalToBytes(o)
}
return o.bytes
}
func (o *Object) BytesValue() []byte {
if o.tag.Type() == TypeBytes {
return o.real.(BytesImpl)
} else {
return nil
}
}
func (o *Object) RLPDecodeSelf(d codec.Decoder, factory ImplFactory) error {
d2, err := d.DecodeList()
if err != nil {
return err
}
var tag Tag
var real Impl
if err := d2.Decode(&tag); err != nil {
return err
}
if tag.Type() == TypeBytes {
var bs []byte
if err := d2.Decode(&bs); err != nil {
return err
}
o.real = BytesImpl(bs)
o.tag = tag
return nil
}
real, err = factory(tag)
if err != nil {
return errors.CriticalFormatError.Wrap(err,
"FailToCreateObjectImpl")
}
err = real.RLPDecodeFields(d2)
if err != nil {
return err
}
o.real = real
o.tag = tag
return nil
}
func (o *Object) RLPEncodeSelf(e codec.Encoder) error {
e2, err := e.EncodeList()
if err != nil {
return err
}
if err := e2.Encode(o.tag); err != nil {
return err
}
return o.real.RLPEncodeFields(e2)
}
func (o *Object) Flush() error {
return o.real.Flush()
}
func (o *Object) Real() Impl {
if o == nil {
return nil
}
return o.real
}
func (o *Object) Tag() Tag {
if o == nil {
return 0
}
return o.tag
}
func (o *Object) Format(f fmt.State, c rune) {
switch c {
case 'v':
if f.Flag('+') {
fmt.Fprintf(f, "Object{tag=%#x obj=%+v}", o.tag, o.real)
return
}
fallthrough
case 's':
fmt.Fprintf(f, "Object{%#x %v}", o.tag, o.real)
default:
if formatter, ok := o.real.(fmt.Formatter); ok {
formatter.Format(f, c)
}
}
}
func New(t int, real Impl) *Object {
return &Object{
tag: MakeTag(t, real.Version()),
real: real,
}
}
type NoDatabase struct{}
func (o *NoDatabase) Flush() error {
return nil
}
func (o *NoDatabase) ClearCache() {
// do nothing
}
func (o *NoDatabase) Reset(dbase db.Database) error {
// do nothing
return nil
}
func (o *NoDatabase) Resolve(bd merkle.Builder) error {
// do nothing
return nil
}
func GetFromMutableForObject(trie trie.MutableForObject, key []byte) (trie.Object, error) {
return trie.Get(key)
}