Repository navigation
Expand file tree
/
Copy pathTrieTest.java
More file actions
153 lines (141 loc) · 5.37 KB
/
Copy pathTrieTest.java
File metadata and controls
153 lines (141 loc) · 5.37 KB
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
/*
* Copyright (c) [2016] [ <ether.camp> ]
* This file is part of the ethereumJ library.
*
* The ethereumJ library is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* The ethereumJ library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with the ethereumJ library. If not, see <http://www.gnu.org/licenses/>.
*/
package org.tron.core.tire;
import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import org.junit.Assert;
import org.junit.Test;
import org.spongycastle.util.Arrays;
import org.tron.core.capsule.utils.RLP;
import org.tron.core.trie.TrieImpl;
import org.tron.core.trie.TrieImpl.Node;
public class TrieTest {
private static String c = "c";
private static String ca = "ca";
private static String cat = "cat";
private static String dog = "dog";
private static String doge = "doge";
private static String test = "test";
private static String dude = "dude";
@Test
public void test() {
TrieImpl trie = new TrieImpl();
trie.put(new byte[]{1}, c.getBytes());
Assert.assertTrue(Arrays.areEqual(trie.get(RLP.encodeInt(1)), c.getBytes()));
trie.put(new byte[]{1, 0}, ca.getBytes());
trie.put(new byte[]{1, 1}, cat.getBytes());
trie.put(new byte[]{1, 2}, dog.getBytes());
trie.put(RLP.encodeInt(5), doge.getBytes());
trie.put(RLP.encodeInt(6), doge.getBytes());
trie.put(RLP.encodeInt(7), doge.getBytes());
trie.put(RLP.encodeInt(11), doge.getBytes());
trie.put(RLP.encodeInt(12), dude.getBytes());
trie.put(RLP.encodeInt(13), test.getBytes());
trie.delete(RLP.encodeInt(3));
byte[] rootHash = trie.getRootHash();
TrieImpl trieCopy = new TrieImpl(trie.getCache(), rootHash);
Assert.assertNull(trie.prove(RLP.encodeInt(111)));
Map<byte[], Node> map = trieCopy.prove(new byte[]{1, 1});
boolean result = trie
.verifyProof(trieCopy.getRootHash(), new byte[]{1, 1}, (LinkedHashMap<byte[], Node>) map);
Assert.assertTrue(result);
System.out.println(trieCopy.prove(RLP.encodeInt(5)));
System.out.println(trieCopy.prove(RLP.encodeInt(6)));
assertTrue(RLP.encodeInt(5), trieCopy);
assertTrue(RLP.encodeInt(5), RLP.encodeInt(6), trieCopy);
assertTrue(RLP.encodeInt(6), trieCopy);
assertTrue(RLP.encodeInt(6), RLP.encodeInt(5), trieCopy);
//
trie.put(RLP.encodeInt(5), doge.getBytes());
byte[] rootHash2 = trie.getRootHash();
Assert.assertFalse(Arrays.areEqual(rootHash, rootHash2));
trieCopy = new TrieImpl(trie.getCache(), rootHash2);
//
assertTrue(RLP.encodeInt(5), trieCopy);
assertFalse(RLP.encodeInt(5), RLP.encodeInt(6), trieCopy);
assertTrue(RLP.encodeInt(6), trieCopy);
assertFalse(RLP.encodeInt(6), RLP.encodeInt(5), trieCopy);
}
@Test
public void test1() {
TrieImpl trie = new TrieImpl();
int n = 100;
for (int i = 1; i < n; i++) {
trie.put(RLP.encodeInt(i), String.valueOf(i).getBytes());
}
byte[] rootHash1 = trie.getRootHash();
TrieImpl trie2 = new TrieImpl();
for (int i = 1; i < n; i++) {
trie2.put(RLP.encodeInt(i), String.valueOf(i).getBytes());
}
byte[] rootHash2 = trie2.getRootHash();
Assert.assertTrue(Arrays.areEqual(rootHash1, rootHash2));
}
@Test
public void test2() {
TrieImpl trie = new TrieImpl();
int n = 100;
for (int i = 1; i < n; i++) {
trie.put(RLP.encodeInt(i), String.valueOf(i).getBytes());
}
byte[] rootHash = trie.getRootHash();
TrieImpl trieCopy = new TrieImpl(trie.getCache(), rootHash);
for (int i = 1; i < n; i++) {
assertTrue(RLP.encodeInt(i), trieCopy);
}
for (int i = 1; i < n; i++) {
for (int j = 1; j < n; j++) {
if (i != j) {
assertFalse(RLP.encodeInt(i), RLP.encodeInt(j), trieCopy);
}
}
}
}
@Test
public void testOrder() {
TrieImpl trie = new TrieImpl();
int n = 100;
List<Integer> value = new ArrayList<>();
for (int i = 1; i < n; i++) {
value.add(i);
trie.put(RLP.encodeInt(i), String.valueOf(i).getBytes());
}
trie.put(RLP.encodeInt(10), String.valueOf(10).getBytes());
value.add(10);
byte[] rootHash1 = trie.getRootHash();
Collections.shuffle(value);
TrieImpl trie2 = new TrieImpl();
for (int i : value) {
trie2.put(RLP.encodeInt(i), String.valueOf(i).getBytes());
}
byte[] rootHash2 = trie2.getRootHash();
Assert.assertTrue(java.util.Arrays.equals(rootHash1, rootHash2));
}
private void assertTrue(byte[] key, TrieImpl trieCopy) {
Assert.assertTrue(trieCopy.verifyProof(trieCopy.getRootHash(), key, trieCopy.prove(key)));
}
private void assertTrue(byte[] key1, byte[] key2, TrieImpl trieCopy) {
Assert.assertTrue(trieCopy.verifyProof(trieCopy.getRootHash(), key2, trieCopy.prove(key1)));
}
private void assertFalse(byte[] key1, byte[] key2, TrieImpl trieCopy) {
Assert.assertFalse(trieCopy.verifyProof(trieCopy.getRootHash(), key2, trieCopy.prove(key1)));
}
}