-
Notifications
You must be signed in to change notification settings - Fork 99
/
serialize.go
180 lines (144 loc) · 4.72 KB
/
serialize.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
package cell
import (
"encoding/binary"
"errors"
"hash/crc32"
"math"
)
var ErrTooBigValue = errors.New("too big value")
var ErrNegative = errors.New("value should be non negative")
var ErrRefCannotBeNil = errors.New("ref cannot be nil")
var ErrSmallSlice = errors.New("too small slice for this size")
var ErrTooBigSize = errors.New("too big size")
var ErrTooMuchRefs = errors.New("too much refs")
var ErrNotFit1023 = errors.New("cell data size should fit into 1023 bits")
var ErrNoMoreRefs = errors.New("no more refs exists")
var ErrAddressTypeNotSupported = errors.New("address type is not supported")
func (c *Cell) ToBOC() []byte {
return c.ToBOCWithFlags(true)
}
func (c *Cell) ToBOCWithFlags(flags ...bool) []byte {
return ToBOCWithFlags([]*Cell{c}, flags...)
}
// ToBOCWithFlags - flags are: first - withCRC, second - withIndex, third - withCache
func ToBOCWithFlags(roots []*Cell, flags ...bool) []byte {
if len(roots) == 0 {
return nil
}
withCRC := len(flags) > 0 && flags[0]
withIndex := len(flags) > 1 && flags[1]
withCache := len(flags) > 2 && flags[2]
withTopHash := len(flags) > 3 && flags[3]
withIntHashes := len(flags) > 4 && flags[4]
if withTopHash || withIntHashes {
panic("hashes serialization is not yet supported")
}
// recursively go through cells, build hash index and store unique in slice
sortedCells, index := flattenIndex(roots, withTopHash, withIntHashes)
// bytes needed to store num of cells
cellSizeBits := math.Log2(float64(len(sortedCells)) + 1)
cellSizeBytes := byte(math.Ceil(cellSizeBits / 8))
dynBuffer := make([]byte, 8)
var payload []byte
for i := 0; i < len(sortedCells); i++ {
// serialize each cell
payload = append(payload, sortedCells[i].cell.serialize(uint(cellSizeBytes), index, sortedCells[i].withHash, dynBuffer)...)
sortedCells[i].dataIndex = len(payload)
}
// bytes needed to store len of payload
sizeBits := math.Log2(float64(len(payload)) + 1)
sizeBytes := byte(math.Ceil(sizeBits / 8))
// has_idx 1bit, hash_crc32 1bit, has_cache_bits 1bit, flags 2bit, size_bytes 3 bit
flagsByte := byte(0b0_0_0_00_000)
if withIndex {
flagsByte |= 0b1_0_0_00_000
}
if withCRC {
flagsByte |= 0b0_1_0_00_000
}
if withCache {
flagsByte |= 0b0_0_1_00_000
}
flagsByte |= cellSizeBytes
var data []byte
data = append(data, bocMagic...)
data = append(data, flagsByte)
// bytes needed to store size
data = append(data, sizeBytes)
// cells num
data = append(data, dynamicIntBytes(uint64(len(sortedCells)), uint(cellSizeBytes), dynBuffer)...)
// roots num
data = append(data, dynamicIntBytes(uint64(len(roots)), uint(cellSizeBytes), dynBuffer)...)
// complete BOCs = 0
data = append(data, dynamicIntBytes(0, uint(cellSizeBytes), dynBuffer)...)
// len of data
data = append(data, dynamicIntBytes(uint64(len(payload)), uint(sizeBytes), dynBuffer)...)
// root index
for _, r := range roots {
data = append(data, dynamicIntBytes(index[string(r.Hash())].index, uint(cellSizeBytes), dynBuffer)...)
}
cached := 0
if withIndex {
for _, cell := range sortedCells {
idx := cell.dataIndex
if withCache {
idx *= 2
if cell.repeats > 0 {
// cache cells which has refs
idx++
cached++
}
}
data = append(data, dynamicIntBytes(uint64(idx), uint(sizeBytes), dynBuffer)...)
}
}
data = append(data, payload...)
if withCRC {
checksum := make([]byte, 4)
binary.LittleEndian.PutUint32(checksum, crc32.Checksum(data, crc32.MakeTable(crc32.Castagnoli)))
data = append(data, checksum...)
}
return data
}
func (c *Cell) serialize(refIndexSzBytes uint, index map[string]*idxItem, withHash bool, dynBuffer []byte) []byte {
body := c.data // optimization
if c.bitsSz%8 != 0 {
body = c.BeginParse().MustLoadSlice(c.bitsSz)
unusedBits := 8 - (c.bitsSz % 8)
// we need to set a bit at the end if not whole byte was used
body[len(body)-1] += 1 << (unusedBits - 1)
}
refsLn := len(c.refs) * int(refIndexSzBytes)
bufLn := 2 + len(body) + refsLn
// if withHash {
// bufLn += c.levelMask.getHashIndex() * (32 + 2)
//}
data := make([]byte, bufLn)
data[0], data[1] = c.descriptors(c.levelMask)
//if withHash {
// TODO: support hash serialization
// data[0] |= 16
//}
copy(data[2:], body)
refsOffset := bufLn - refsLn
for i, ref := range c.refs {
copy(data[refsOffset+i*int(refIndexSzBytes):], dynamicIntBytes(index[string(ref.Hash())].index, refIndexSzBytes, dynBuffer))
}
return data
}
func (c *Cell) descriptors(lvl LevelMask) (byte, byte) {
// calc size
ln := (c.bitsSz / 8) * 2
if c.bitsSz%8 != 0 {
ln++
}
specBit := byte(0)
if c.special {
specBit = 8
}
return byte(len(c.refs)) + specBit + lvl.Mask*32, byte(ln)
}
func dynamicIntBytes(val uint64, sz uint, buffer []byte) []byte {
binary.BigEndian.PutUint64(buffer, val)
return buffer[8-sz:]
}