|
| 1 | +/* |
| 2 | + * Copyright Hyperledger Besu Contributors |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with |
| 5 | + * the License. You may obtain a copy of the License at |
| 6 | + * |
| 7 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | + * |
| 9 | + * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on |
| 10 | + * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the |
| 11 | + * specific language governing permissions and limitations under the License. |
| 12 | + * |
| 13 | + * SPDX-License-Identifier: Apache-2.0 |
| 14 | + * |
| 15 | + */ |
| 16 | +package org.hyperledger.besu.ethereum.stateless.bintrie; |
| 17 | + |
| 18 | +import static org.assertj.core.api.Assertions.assertThat; |
| 19 | + |
| 20 | +import org.apache.tuweni.bytes.Bytes32; |
| 21 | +import org.junit.jupiter.api.BeforeAll; |
| 22 | +import org.junit.jupiter.api.Test; |
| 23 | + |
| 24 | +public class SimpleBinTrieReferenceTest { |
| 25 | + static BytesPackedBitSequenceFactory keyFactory; |
| 26 | + |
| 27 | + @BeforeAll |
| 28 | + static void setup() { |
| 29 | + keyFactory = new BytesPackedBitSequenceFactory(); |
| 30 | + } |
| 31 | + |
| 32 | + @Test |
| 33 | + public void testOneValue() { |
| 34 | + SimpleBinTrie<BytesPackedBitSequence, Bytes32> trie = new SimpleBinTrie<>(); |
| 35 | + BytesPackedBitSequence key = |
| 36 | + keyFactory.fromHexString( |
| 37 | + "0x0000000000000000000000000000000000000000000000000000000000000000"); |
| 38 | + Bytes32 value = |
| 39 | + Bytes32.fromHexString("0x0101010101010101010101010101010101010101010101010101010101010101"); |
| 40 | + trie.put(key, value); |
| 41 | + Bytes32 expectedRootHash = |
| 42 | + Bytes32.fromHexString("0x694545468677064fd833cddc8455762fe6b21c6cabe2fc172529e0f573181cd5"); |
| 43 | + assertThat(trie.getRootHash()).as("Retrieve root hash").isEqualByComparingTo(expectedRootHash); |
| 44 | + } |
| 45 | + |
| 46 | + @Test |
| 47 | + public void testTwoEntriesDifferentFirstBit() { |
| 48 | + SimpleBinTrie<BytesPackedBitSequence, Bytes32> trie = new SimpleBinTrie<>(); |
| 49 | + BytesPackedBitSequence key = |
| 50 | + keyFactory.fromHexString( |
| 51 | + "0x0000000000000000000000000000000000000000000000000000000000000000"); |
| 52 | + Bytes32 value = |
| 53 | + Bytes32.fromHexString("0x0101010101010101010101010101010101010101010101010101010101010101"); |
| 54 | + trie.put(key, value); |
| 55 | + |
| 56 | + BytesPackedBitSequence key2 = |
| 57 | + keyFactory.fromHexString( |
| 58 | + "0x8000000000000000000000000000000000000000000000000000000000000000"); |
| 59 | + Bytes32 value2 = |
| 60 | + Bytes32.fromHexString("0x0202020202020202020202020202020202020202020202020202020202020202"); |
| 61 | + trie.put(key2, value2); |
| 62 | + Bytes32 expectedRootHash = |
| 63 | + Bytes32.fromHexString("0x85fc622076752a6fcda2c886c18058d639066a83473d9684704b5a29455ed2ed"); |
| 64 | + assertThat(trie.getRootHash()).as("Retrieve root hash").isEqualByComparingTo(expectedRootHash); |
| 65 | + } |
| 66 | + |
| 67 | + @Test |
| 68 | + public void testOneStemColocatedValue() { |
| 69 | + SimpleBinTrie<BytesPackedBitSequence, Bytes32> trie = new SimpleBinTrie<>(); |
| 70 | + BytesPackedBitSequence key = |
| 71 | + keyFactory.fromHexString( |
| 72 | + "0x0000000000000000000000000000000000000000000000000000000000000003"); |
| 73 | + Bytes32 value = |
| 74 | + Bytes32.fromHexString("0x0101010101010101010101010101010101010101010101010101010101010101"); |
| 75 | + trie.put(key, value); |
| 76 | + |
| 77 | + BytesPackedBitSequence key2 = |
| 78 | + keyFactory.fromHexString( |
| 79 | + "0x0000000000000000000000000000000000000000000000000000000000000004"); |
| 80 | + Bytes32 value2 = |
| 81 | + Bytes32.fromHexString("0x0202020202020202020202020202020202020202020202020202020202020202"); |
| 82 | + trie.put(key2, value2); |
| 83 | + |
| 84 | + BytesPackedBitSequence key3 = |
| 85 | + keyFactory.fromHexString( |
| 86 | + "0x0000000000000000000000000000000000000000000000000000000000000009"); |
| 87 | + Bytes32 value3 = |
| 88 | + Bytes32.fromHexString("0x0303030303030303030303030303030303030303030303030303030303030303"); |
| 89 | + trie.put(key3, value3); |
| 90 | + |
| 91 | + BytesPackedBitSequence key4 = |
| 92 | + keyFactory.fromHexString( |
| 93 | + "0x00000000000000000000000000000000000000000000000000000000000000FF"); |
| 94 | + Bytes32 value4 = |
| 95 | + Bytes32.fromHexString("0x0404040404040404040404040404040404040404040404040404040404040404"); |
| 96 | + trie.put(key4, value4); |
| 97 | + Bytes32 expectedRootHash = |
| 98 | + Bytes32.fromHexString("0xaa12acb5689a2dc03e9d7ab0350449c70cdad286750dc8ba1dd092f5e100191a"); |
| 99 | + assertThat(trie.getRootHash()).as("Retrieve root hash").isEqualByComparingTo(expectedRootHash); |
| 100 | + } |
| 101 | + |
| 102 | + @Test |
| 103 | + public void testTwoStemColocatedValue() { |
| 104 | + SimpleBinTrie<BytesPackedBitSequence, Bytes32> trie = new SimpleBinTrie<>(); |
| 105 | + BytesPackedBitSequence key = |
| 106 | + keyFactory.fromHexString( |
| 107 | + "0x0000000000000000000000000000000000000000000000000000000000000003"); |
| 108 | + Bytes32 value = |
| 109 | + Bytes32.fromHexString("0x0101010101010101010101010101010101010101010101010101010101010101"); |
| 110 | + trie.put(key, value); |
| 111 | + |
| 112 | + BytesPackedBitSequence key2 = |
| 113 | + keyFactory.fromHexString( |
| 114 | + "0x0000000000000000000000000000000000000000000000000000000000000004"); |
| 115 | + Bytes32 value2 = |
| 116 | + Bytes32.fromHexString("0x0202020202020202020202020202020202020202020202020202020202020202"); |
| 117 | + trie.put(key2, value2); |
| 118 | + |
| 119 | + BytesPackedBitSequence key3 = |
| 120 | + keyFactory.fromHexString( |
| 121 | + "0x8000000000000000000000000000000000000000000000000000000000000003"); |
| 122 | + Bytes32 value3 = |
| 123 | + Bytes32.fromHexString("0x0101010101010101010101010101010101010101010101010101010101010101"); |
| 124 | + trie.put(key3, value3); |
| 125 | + |
| 126 | + BytesPackedBitSequence key4 = |
| 127 | + keyFactory.fromHexString( |
| 128 | + "0x8000000000000000000000000000000000000000000000000000000000000004"); |
| 129 | + Bytes32 value4 = |
| 130 | + Bytes32.fromHexString("0x0202020202020202020202020202020202020202020202020202020202020202"); |
| 131 | + trie.put(key4, value4); |
| 132 | + Bytes32 expectedRootHash = |
| 133 | + Bytes32.fromHexString("0x6387c711dfeaa802e67f83bedb504d42e1bc89f64c4e44aa69d207351b2a03e5"); |
| 134 | + assertThat(trie.getRootHash()).as("Retrieve root hash").isEqualByComparingTo(expectedRootHash); |
| 135 | + } |
| 136 | +} |
0 commit comments