-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpartialmpt_test.go
126 lines (105 loc) · 4.15 KB
/
partialmpt_test.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
package mpt
import (
"bytes"
"encoding/hex"
"testing"
)
func TestPartialMpt(t *testing.T) {
k1, _ := hex.DecodeString("1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef") // Key 1
v1, _ := hex.DecodeString("fedcba0987654321fedcba0987654321fedcba0987654321fedcba0987654321") // Value 1
k2, _ := hex.DecodeString("4043567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef") // Key 2
v2, _ := hex.DecodeString("efdcba0987654321fedcba0987654321fedcba0987654321fedcba0987654321") // Value 2
k3, _ := hex.DecodeString("1843567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef") // Key 3
v3, _ := hex.DecodeString("efdcba0987654321fedcba0987654321fedcba0987654321fedcba0987654321") // Value 3
k4, _ := hex.DecodeString("ff34567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef") // Key 4
v4, _ := hex.DecodeString("efdcba0987654321fedcba0987654321fedcba0987654321fedcba0987654321") // Value 4
k5, _ := hex.DecodeString("bf34567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef") // Key 5
v5, _ := hex.DecodeString("efdcba0987654321fedcba0987654321fedcba0987654321fedcba0987654321") // Value 5
k6, _ := hex.DecodeString("d724567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef") // Key 6
v6, _ := hex.DecodeString("efdcba0987654321fedcba0987654321fedcba0987654321fedcba0987654321") // Value 6
mpt, err := NewFullMPT()
if err != nil {
t.Error(err.Error())
}
mpt.Insert(k1, v1)
mpt.Insert(k5, v5)
mpt.Insert(k6, v6)
mpt.Insert(k4, v4)
mpt.Insert(k2, v2)
mpt.Insert(k3, v3)
partialMpt, err := NewPartialMPT(mpt)
if err != nil {
t.Error(err.Error())
}
if !bytes.Equal(partialMpt.Commitment(), mpt.Commitment()) {
t.Errorf("Expected commitment of partial and full MPT to match. They don't")
}
partialMpt, err = NewPartialMPTIncludingKey(mpt, k1)
if err != nil {
t.Error(err.Error())
}
if !bytes.Equal(partialMpt.Commitment(), mpt.Commitment()) {
t.Errorf("Expected commitment of partial and full MPT to match. They don't")
}
v11, err := partialMpt.Get(k1)
if err != nil {
t.Error(err.Error())
}
if !bytes.Equal(v11, v1) {
t.Errorf("Expected value of k1 in partial and full MPT to match. They don't")
}
partialMpt, err = NewPartialMPTIncludingKeys(mpt, [][]byte{k1, k5})
if err != nil {
t.Error(err.Error())
}
if !bytes.Equal(partialMpt.Commitment(), mpt.Commitment()) {
t.Errorf("Expected commitment of partial and full MPT to match. They don't")
}
v11, err = partialMpt.Get(k1)
if err != nil {
t.Error(err.Error())
}
if !bytes.Equal(v11, v1) {
t.Errorf("Expected value of k1 in partial and full MPT to match. They don't")
}
v21, err := partialMpt.Get(k2) // absent
if err == nil && v21 != nil {
t.Errorf("Expected value to be missing from partial MPT, but found it anyway?")
}
partialMpt2, err := NewPartialMPTFromBytes(partialMpt.Bytes())
if err != nil {
t.Error(err.Error())
}
if !bytes.Equal(partialMpt2.Commitment(), mpt.Commitment()) {
t.Errorf("Expected commitment of deserialized partial MPT and full MPT to match. They don't")
}
partialMpt, _ = NewPartialMPTIncludingKey(mpt, k1)
mpt.Insert(k1, v2)
delta, _ := NewDeltaMPT(mpt)
partialMpt.ProcessUpdates(delta)
if !bytes.Equal(partialMpt.Commitment(), mpt.Commitment()) {
t.Errorf("Expected commitment of deserialized partial MPT after update and full MPT to match. They don't")
}
partialMpt, _ = NewPartialMPTIncludingKey(mpt, k1)
mpt.Insert(k1, v2)
delta, _ = NewDeltaMPT(mpt)
partialMpt.ProcessUpdatesFromBytes(delta.Bytes())
if !bytes.Equal(partialMpt.Commitment(), mpt.Commitment()) {
t.Errorf("Expected commitment of deserialized partial MPT after update (via bytes) and full MPT to match. They don't")
}
err = partialMpt.ProcessUpdatesFromBytes([]byte{})
if err == nil {
t.Errorf("Expected error in ProcessUpdateFromBytes with invalid slice. Got none")
}
}
func TestPartialMptSerialize(t *testing.T) {
_, err := NewPartialMPTFromBytes([]byte{})
if err == nil {
t.Error("Expected error on deserialize with invalid input, but got none")
}
eln, _ := NewEmptyLeafNode()
_, err = NewPartialMPTFromBytes(eln.Bytes())
if err == nil {
t.Error("Expected error on deserialize with invalid input, but got none")
}
}