-
Notifications
You must be signed in to change notification settings - Fork 1
/
treehash_gob_encoding.go
58 lines (46 loc) · 1.2 KB
/
treehash_gob_encoding.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
package lms
import (
"bytes"
"encoding/gob"
"github.com/LoCCS/lms/container/stack"
)
// thsEx is the exporting template of TreeHashStack
type thsEx struct {
Leaf uint32
LeafUpper uint32
H uint32
NodeStack []*Node
}
// GobEncode customizes the Gob encoding scheme for TreeHashStack
func (ths TreeHashStack) GobEncode() ([]byte, error) {
thsGob := &thsEx{
Leaf: ths.leaf,
LeafUpper: ths.leafUpper,
H: ths.height,
}
values := ths.nodeStack.ValueSlice()
thsGob.NodeStack = make([]*Node, len(values))
for i := range values {
thsGob.NodeStack[i] = values[i].(*Node)
}
buf := new(bytes.Buffer)
if err := gob.NewEncoder(buf).Encode(thsGob); nil != err {
return nil, err
}
return buf.Bytes(), nil
}
// GobDecode customizes the Gob decoding scheme for TreeHashStack
func (ths *TreeHashStack) GobDecode(data []byte) error {
thsGob := new(thsEx)
if err := gob.NewDecoder(bytes.NewBuffer(data)).Decode(thsGob); nil != err {
return err
}
ths.leaf = thsGob.Leaf
ths.leafUpper = thsGob.LeafUpper
ths.height = thsGob.H
ths.nodeStack = stack.New()
for _, n := range thsGob.NodeStack {
ths.nodeStack.Push(&Node{Height: n.Height, Nu: n.Nu, Index: n.Index})
}
return nil
}